1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Mike Shaver <shaver@clusterfs.com>
7 * This file is part of the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * You may have signed or agreed to another license before downloading
11 * this software. If so, you are bound by the terms and conditions
12 * of that agreement, and the following does not apply to you. See the
13 * LICENSE file included with this distribution for more information.
15 * If you did not agree to a different license, then this copy of Lustre
16 * is open source software; you can redistribute it and/or modify it
17 * under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
20 * In either case, Lustre is distributed in the hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * license text for more details.
26 #define DEBUG_SUBSYSTEM S_RPC
28 # include <liblustre.h>
31 #include <obd_support.h>
32 #include <lustre_ha.h>
33 #include <lustre_net.h>
34 #include <lustre_import.h>
35 #include <lustre_export.h>
37 #include <obd_class.h>
38 #include <lustre_ver.h>
40 #include "ptlrpc_internal.h"
42 struct ptlrpc_connect_async_args {
43 __u64 pcaa_peer_committed;
44 int pcaa_initial_connect;
47 /* A CLOSED import should remain so. */
48 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
50 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
51 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
52 imp, obd2cli_tgt(imp->imp_obd), \
53 ptlrpc_import_state_name(imp->imp_state), \
54 ptlrpc_import_state_name(state)); \
55 imp->imp_state = state; \
59 #define IMPORT_SET_STATE(imp, state) \
61 spin_lock(&imp->imp_lock); \
62 IMPORT_SET_STATE_NOLOCK(imp, state); \
63 spin_unlock(&imp->imp_lock); \
67 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
69 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
71 /* Only this function is allowed to change the import state when it is
72 * CLOSED. I would rather refcount the import and free it after
73 * disconnection like we do with exports. To do that, the client_obd
74 * will need to save the peer info somewhere other than in the import,
76 int ptlrpc_init_import(struct obd_import *imp)
78 spin_lock(&imp->imp_lock);
80 imp->imp_generation++;
81 imp->imp_state = LUSTRE_IMP_NEW;
83 spin_unlock(&imp->imp_lock);
87 EXPORT_SYMBOL(ptlrpc_init_import);
89 #define UUID_STR "_UUID"
90 static void deuuidify(char *uuid, const char *prefix, char **uuid_start,
93 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
94 ? uuid : uuid + strlen(prefix);
96 *uuid_len = strlen(*uuid_start);
98 if (*uuid_len < strlen(UUID_STR))
101 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
102 UUID_STR, strlen(UUID_STR)))
103 *uuid_len -= strlen(UUID_STR);
106 /* Returns true if import was FULL, false if import was already not
108 * @imp - import to be disconnected
109 * @conn_cnt - connection count (epoch) of the request that timed out
110 * and caused the disconnection. In some cases, multiple
111 * inflight requests can fail to a single target (e.g. OST
112 * bulk requests) and if one has already caused a reconnection
113 * (increasing the import->conn_cnt) the older failure should
114 * not also cause a reconnection. If zero it forces a reconnect.
116 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
120 spin_lock(&imp->imp_lock);
122 if (imp->imp_state == LUSTRE_IMP_FULL &&
123 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
127 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
128 &target_start, &target_len);
130 LCONSOLE_ERROR("%s: Connection to service %.*s via nid %s was "
131 "lost; in progress operations using this "
132 "service will %s.\n", imp->imp_obd->obd_name,
133 target_len, target_start,
134 libcfs_nid2str(imp->imp_connection->c_peer.nid),
135 imp->imp_replayable ?
136 "wait for recovery to complete" : "fail");
138 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
139 spin_unlock(&imp->imp_lock);
141 if (obd_dump_on_timeout)
142 libcfs_debug_dumplog();
144 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
147 spin_unlock(&imp->imp_lock);
148 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
149 imp->imp_client->cli_name, imp,
150 (imp->imp_state == LUSTRE_IMP_FULL &&
151 imp->imp_conn_cnt > conn_cnt) ?
152 "reconnected" : "not connected", imp->imp_conn_cnt,
153 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
160 * This acts as a barrier; all existing requests are rejected, and
161 * no new requests will be accepted until the import is valid again.
163 void ptlrpc_deactivate_import(struct obd_import *imp)
167 spin_lock(&imp->imp_lock);
168 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
169 imp->imp_invalid = 1;
170 imp->imp_generation++;
171 spin_unlock(&imp->imp_lock);
173 ptlrpc_abort_inflight(imp);
174 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
178 * This function will invalidate the import, if necessary, then block
179 * for all the RPC completions, and finally notify the obd to
180 * invalidate its state (ie cancel locks, clear pending requests,
183 void ptlrpc_invalidate_import(struct obd_import *imp)
185 struct l_wait_info lwi;
188 if (!imp->imp_invalid)
189 ptlrpc_deactivate_import(imp);
191 LASSERT(imp->imp_invalid);
193 /* wait for all requests to error out and call completion callbacks */
194 lwi = LWI_TIMEOUT_INTERVAL(cfs_timeout_cap(cfs_time_seconds(obd_timeout)),
196 rc = l_wait_event(imp->imp_recovery_waitq,
197 (atomic_read(&imp->imp_inflight) == 0),
201 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
202 obd2cli_tgt(imp->imp_obd), rc,
203 atomic_read(&imp->imp_inflight));
205 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
208 /* unset imp_invalid */
209 void ptlrpc_activate_import(struct obd_import *imp)
211 struct obd_device *obd = imp->imp_obd;
213 spin_lock(&imp->imp_lock);
214 imp->imp_invalid = 0;
215 spin_unlock(&imp->imp_lock);
217 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
220 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
224 LASSERT(!imp->imp_dlm_fake);
226 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
227 if (!imp->imp_replayable) {
228 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
229 "auto-deactivating\n",
230 obd2cli_tgt(imp->imp_obd),
231 imp->imp_connection->c_remote_uuid.uuid,
232 imp->imp_obd->obd_name);
233 ptlrpc_deactivate_import(imp);
236 CDEBUG(D_HA, "%s: waking up pinger\n",
237 obd2cli_tgt(imp->imp_obd));
239 spin_lock(&imp->imp_lock);
240 imp->imp_force_verify = 1;
241 spin_unlock(&imp->imp_lock);
243 ptlrpc_pinger_wake_up();
248 static int import_select_connection(struct obd_import *imp)
250 struct obd_import_conn *imp_conn = NULL, *conn;
251 struct obd_export *dlmexp;
254 spin_lock(&imp->imp_lock);
256 if (list_empty(&imp->imp_conn_list)) {
257 CERROR("%s: no connections available\n",
258 imp->imp_obd->obd_name);
259 spin_unlock(&imp->imp_lock);
263 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
264 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
265 imp->imp_obd->obd_name,
266 libcfs_nid2str(conn->oic_conn->c_peer.nid),
267 conn->oic_last_attempt);
268 /* Throttle the reconnect rate to once per RECONNECT_INTERVAL */
269 if (cfs_time_before_64(conn->oic_last_attempt +
270 RECONNECT_INTERVAL * HZ,
271 cfs_time_current_64())) {
272 /* If we have never tried this connection since the
273 the last successful attempt, go with this one */
274 if (cfs_time_before_64(conn->oic_last_attempt,
275 imp->imp_last_success_conn)) {
280 /* Both of these connections have already been tried
281 since the last successful connection; just choose the
282 least recently used */
285 else if (cfs_time_before_64(conn->oic_last_attempt,
286 imp_conn->oic_last_attempt))
291 /* if not found, simply choose the current one */
293 LASSERT(imp->imp_conn_current);
294 imp_conn = imp->imp_conn_current;
296 LASSERT(imp_conn->oic_conn);
298 imp_conn->oic_last_attempt = cfs_time_current_64();
300 /* switch connection, don't mind if it's same as the current one */
301 if (imp->imp_connection)
302 ptlrpc_put_connection(imp->imp_connection);
303 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
305 dlmexp = class_conn2export(&imp->imp_dlm_handle);
306 LASSERT(dlmexp != NULL);
307 if (dlmexp->exp_connection)
308 ptlrpc_put_connection(dlmexp->exp_connection);
309 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
310 class_export_put(dlmexp);
312 if (imp->imp_conn_current != imp_conn) {
313 LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
314 imp->imp_obd->obd_name, imp_conn->oic_uuid.uuid,
315 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
316 imp->imp_conn_current = imp_conn;
319 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
320 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
321 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
323 spin_unlock(&imp->imp_lock);
328 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
330 struct obd_device *obd = imp->imp_obd;
331 int initial_connect = 0;
333 __u64 committed_before_reconnect = 0;
334 struct ptlrpc_request *request;
335 int size[] = { sizeof(struct ptlrpc_body),
336 sizeof(imp->imp_obd->u.cli.cl_target_uuid),
337 sizeof(obd->obd_uuid),
338 sizeof(imp->imp_dlm_handle),
339 sizeof(imp->imp_connect_data) };
340 char *tmp[] = { NULL,
341 obd2cli_tgt(imp->imp_obd),
343 (char *)&imp->imp_dlm_handle,
344 (char *)&imp->imp_connect_data };
345 struct ptlrpc_connect_async_args *aa;
348 spin_lock(&imp->imp_lock);
349 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
350 spin_unlock(&imp->imp_lock);
351 CERROR("can't connect to a closed import\n");
353 } else if (imp->imp_state == LUSTRE_IMP_FULL &&
354 imp->imp_force_reconnect == 0) {
355 spin_unlock(&imp->imp_lock);
356 CERROR("already connected\n");
358 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
359 spin_unlock(&imp->imp_lock);
360 CERROR("already connecting\n");
364 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
367 imp->imp_resend_replay = 0;
369 if (!lustre_handle_is_used(&imp->imp_remote_handle))
372 committed_before_reconnect = imp->imp_peer_committed_transno;
374 spin_unlock(&imp->imp_lock);
377 struct obd_uuid uuid;
379 obd_str2uuid(&uuid, new_uuid);
380 rc = import_set_conn_priority(imp, &uuid);
385 rc = import_select_connection(imp);
389 /* last in connection list */
390 if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
391 if (imp->imp_initial_recov_bk && initial_connect) {
392 CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
393 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
394 /* Don't retry if connect fails */
396 obd_set_info_async(obd->obd_self_export,
397 strlen(KEY_INIT_RECOV),
399 sizeof(rc), &rc, NULL);
401 if (imp->imp_recon_bk) {
402 CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
403 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
404 imp->imp_last_recon = 1;
408 /* Reset connect flags to the originally requested flags, in case
409 * the server is updated on-the-fly we will get the new features. */
410 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
411 rc = obd_reconnect(imp->imp_obd->obd_self_export, obd,
412 &obd->obd_uuid, &imp->imp_connect_data);
416 request = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, imp->imp_connect_op,
419 GOTO(out, rc = -ENOMEM);
422 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
424 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
426 request->rq_send_state = LUSTRE_IMP_CONNECTING;
427 /* Allow a slightly larger reply for future growth compatibility */
428 size[REPLY_REC_OFF] = sizeof(struct obd_connect_data) +
430 ptlrpc_req_set_repsize(request, 2, size);
431 request->rq_interpret_reply = ptlrpc_connect_interpret;
433 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
434 aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
435 memset(aa, 0, sizeof *aa);
437 aa->pcaa_peer_committed = committed_before_reconnect;
438 aa->pcaa_initial_connect = initial_connect;
440 if (aa->pcaa_initial_connect) {
441 imp->imp_replayable = 1;
442 /* On an initial connect, we don't know which one of a
443 failover server pair is up. Don't wait long. */
444 request->rq_timeout = max((int)(obd_timeout / 20), 5);
447 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
448 ptlrpcd_add_req(request);
452 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
457 EXPORT_SYMBOL(ptlrpc_connect_import);
459 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
461 struct obd_import_conn *imp_conn;
466 spin_lock(&imp->imp_lock);
467 if (list_empty(&imp->imp_conn_list))
470 imp_conn = list_entry(imp->imp_conn_list.prev,
471 struct obd_import_conn,
474 if (imp->imp_conn_current != imp_conn) {
475 ptlrpc_ping_import_soon(imp);
480 spin_unlock(&imp->imp_lock);
483 ptlrpc_pinger_wake_up();
488 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
491 struct ptlrpc_connect_async_args *aa = data;
492 struct obd_import *imp = request->rq_import;
493 struct client_obd *cli = &imp->imp_obd->u.cli;
494 struct lustre_handle old_hdl;
498 spin_lock(&imp->imp_lock);
499 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
500 spin_unlock(&imp->imp_lock);
503 imp->imp_force_reconnect = 0;
504 spin_unlock(&imp->imp_lock);
509 rc = sptlrpc_cli_install_rvs_ctx(imp, request->rq_cli_ctx);
513 LASSERT(imp->imp_conn_current);
515 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
517 /* All imports are pingable */
518 imp->imp_pingable = 1;
520 if (aa->pcaa_initial_connect) {
521 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
522 CDEBUG(D_HA, "connected to replayable target: %s\n",
523 obd2cli_tgt(imp->imp_obd));
524 imp->imp_replayable = 1;
526 imp->imp_replayable = 0;
529 if (msg_flags & MSG_CONNECT_NEXT_VER) {
530 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
531 CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v2\n",
532 obd2cli_tgt(imp->imp_obd));
534 CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v1\n",
535 obd2cli_tgt(imp->imp_obd));
538 imp->imp_remote_handle =
539 *lustre_msg_get_handle(request->rq_repmsg);
541 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
542 GOTO(finish, rc = 0);
545 /* Determine what recovery state to move the import to. */
546 if (MSG_CONNECT_RECONNECT & msg_flags) {
547 memset(&old_hdl, 0, sizeof(old_hdl));
548 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
550 CERROR("%s@%s didn't like our handle "LPX64
551 ", failed\n", obd2cli_tgt(imp->imp_obd),
552 imp->imp_connection->c_remote_uuid.uuid,
553 imp->imp_dlm_handle.cookie);
554 GOTO(out, rc = -ENOTCONN);
557 if (memcmp(&imp->imp_remote_handle,
558 lustre_msg_get_handle(request->rq_repmsg),
559 sizeof(imp->imp_remote_handle))) {
561 /* The MGC does this all the time */
562 if (strcmp(imp->imp_obd->obd_type->typ_name,
563 LUSTRE_MGC_NAME) == 0) {
567 "%s@%s changed handle from "LPX64" to "LPX64
568 "; copying, but this may foreshadow disaster\n",
569 obd2cli_tgt(imp->imp_obd),
570 imp->imp_connection->c_remote_uuid.uuid,
571 imp->imp_remote_handle.cookie,
572 lustre_msg_get_handle(request->rq_repmsg)->
574 imp->imp_remote_handle =
575 *lustre_msg_get_handle(request->rq_repmsg);
577 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
578 obd2cli_tgt(imp->imp_obd),
579 imp->imp_connection->c_remote_uuid.uuid);
582 if (imp->imp_invalid) {
583 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
584 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
585 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
586 imp->imp_obd->obd_name,
587 obd2cli_tgt(imp->imp_obd));
588 imp->imp_resend_replay = 1;
589 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
591 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
593 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
594 LASSERT(imp->imp_replayable);
595 imp->imp_remote_handle =
596 *lustre_msg_get_handle(request->rq_repmsg);
597 imp->imp_last_replay_transno = 0;
598 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
600 imp->imp_remote_handle =
601 *lustre_msg_get_handle(request->rq_repmsg);
602 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
605 /* Sanity checks for a reconnected import. */
606 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
607 CERROR("imp_replayable flag does not match server "
608 "after reconnect. We should LBUG right here.\n");
611 if (lustre_msg_get_last_committed(request->rq_repmsg) <
612 aa->pcaa_peer_committed) {
613 CERROR("%s went back in time (transno "LPD64
614 " was previously committed, server now claims "LPD64
615 ")! See https://bugzilla.clusterfs.com/"
616 "long_list.cgi?buglist=9646\n",
617 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
618 lustre_msg_get_last_committed(request->rq_repmsg));
622 rc = ptlrpc_import_recovery_state_machine(imp);
624 if (rc == -ENOTCONN) {
625 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
626 "invalidating and reconnecting\n",
627 obd2cli_tgt(imp->imp_obd),
628 imp->imp_connection->c_remote_uuid.uuid);
629 ptlrpc_connect_import(imp, NULL);
633 struct obd_connect_data *ocd;
634 struct obd_export *exp;
636 ocd = lustre_swab_repbuf(request, REPLY_REC_OFF, sizeof(*ocd),
637 lustre_swab_connect);
639 spin_lock(&imp->imp_lock);
640 list_del(&imp->imp_conn_current->oic_item);
641 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
642 imp->imp_last_success_conn =
643 imp->imp_conn_current->oic_last_attempt;
646 spin_unlock(&imp->imp_lock);
647 CERROR("Wrong connect data from server\n");
652 imp->imp_connect_data = *ocd;
654 exp = class_conn2export(&imp->imp_dlm_handle);
655 spin_unlock(&imp->imp_lock);
657 /* check that server granted subset of flags we asked for. */
658 LASSERTF((ocd->ocd_connect_flags &
659 imp->imp_connect_flags_orig) ==
660 ocd->ocd_connect_flags, LPX64" != "LPX64,
661 imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
664 /* This could happen if export is cleaned during the
666 spin_unlock(&imp->imp_lock);
667 CERROR("Missing export for %s\n",
668 imp->imp_obd->obd_name);
669 GOTO(out, rc = -ENODEV);
671 exp->exp_connect_flags = ocd->ocd_connect_flags;
672 class_export_put(exp);
674 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
676 if (!ocd->ocd_ibits_known &&
677 ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
678 CERROR("Inodebits aware server returned zero compatible"
681 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
682 (ocd->ocd_version > LUSTRE_VERSION_CODE +
683 LUSTRE_VERSION_OFFSET_WARN)) {
684 /* Sigh, some compilers do not like #ifdef in the middle
685 of macro arguments */
687 const char *action = "upgrading this client";
689 const char *action = "recompiling this application";
692 CWARN("Server %s version (%d.%d.%d.%d) is much newer. "
693 "Consider %s (%s).\n",
694 obd2cli_tgt(imp->imp_obd),
695 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
696 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
697 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
698 OBD_OCD_VERSION_FIX(ocd->ocd_version),
699 action, LUSTRE_VERSION_STRING);
702 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
703 cli->cl_max_pages_per_rpc = ocd->ocd_brw_size >> PAGE_SHIFT;
705 cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
706 (int)(1024 * 1024 >> PAGE_SHIFT));
709 LASSERT(cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES &&
710 cli->cl_max_pages_per_rpc > 0);
716 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
717 if (aa->pcaa_initial_connect && !imp->imp_initial_recov)
718 ptlrpc_deactivate_import(imp);
720 if (imp->imp_recon_bk && imp->imp_last_recon) {
721 /* Give up trying to reconnect */
722 imp->imp_obd->obd_no_recov = 1;
723 ptlrpc_deactivate_import(imp);
727 struct obd_connect_data *ocd;
729 /* reply message might not be ready */
730 if (request->rq_repmsg != NULL)
733 ocd = lustre_swab_repbuf(request, REPLY_REC_OFF,
735 lustre_swab_connect);
737 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
738 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
739 /* Actually servers are only supposed to refuse
740 connection from liblustre clients, so we should
741 never see this from VFS context */
742 CERROR("Server %s version (%d.%d.%d.%d) "
743 "refused connection from this client "
744 "as too old version (%s). Client must "
746 obd2cli_tgt(imp->imp_obd),
747 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
748 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
749 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
750 OBD_OCD_VERSION_FIX(ocd->ocd_version),
751 LUSTRE_VERSION_STRING);
752 ptlrpc_deactivate_import(imp);
753 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
758 ptlrpc_maybe_ping_import_soon(imp);
760 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
761 obd2cli_tgt(imp->imp_obd),
762 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
765 imp->imp_last_recon = 0;
767 cfs_waitq_signal(&imp->imp_recovery_waitq);
771 static int completed_replay_interpret(struct ptlrpc_request *req,
775 atomic_dec(&req->rq_import->imp_replay_inflight);
776 if (req->rq_status == 0) {
777 ptlrpc_import_recovery_state_machine(req->rq_import);
779 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
781 req->rq_import->imp_obd->obd_name, req->rq_status);
782 ptlrpc_connect_import(req->rq_import, NULL);
788 static int signal_completed_replay(struct obd_import *imp)
790 struct ptlrpc_request *req;
793 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
794 atomic_inc(&imp->imp_replay_inflight);
796 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 1, NULL, NULL);
798 atomic_dec(&imp->imp_replay_inflight);
802 ptlrpc_req_set_repsize(req, 1, NULL);
803 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
804 lustre_msg_add_flags(req->rq_reqmsg, MSG_LAST_REPLAY);
805 req->rq_timeout *= 3;
806 req->rq_interpret_reply = completed_replay_interpret;
808 ptlrpcd_add_req(req);
813 static int ptlrpc_invalidate_import_thread(void *data)
815 struct obd_import *imp = data;
819 ptlrpc_daemonize("ll_imp_inval");
821 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
822 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
823 imp->imp_connection->c_remote_uuid.uuid);
825 ptlrpc_invalidate_import(imp);
827 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
828 ptlrpc_import_recovery_state_machine(imp);
834 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
842 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
843 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
844 &target_start, &target_len);
845 LCONSOLE_ERROR("This client was evicted by %.*s; in progress "
846 "operations using this service will fail.\n",
847 target_len, target_start);
848 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
849 obd2cli_tgt(imp->imp_obd),
850 imp->imp_connection->c_remote_uuid.uuid);
853 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
854 CLONE_VM | CLONE_FILES);
856 CERROR("error starting invalidate thread: %d\n", rc);
861 ptlrpc_invalidate_import(imp);
863 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
867 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
868 CDEBUG(D_HA, "replay requested by %s\n",
869 obd2cli_tgt(imp->imp_obd));
870 rc = ptlrpc_replay_next(imp, &inflight);
872 atomic_read(&imp->imp_replay_inflight) == 0) {
873 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
874 rc = ldlm_replay_locks(imp);
881 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
882 if (atomic_read(&imp->imp_replay_inflight) == 0) {
883 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
884 rc = signal_completed_replay(imp);
891 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
892 if (atomic_read(&imp->imp_replay_inflight) == 0) {
893 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
897 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
898 CDEBUG(D_HA, "reconnected to %s@%s\n",
899 obd2cli_tgt(imp->imp_obd),
900 imp->imp_connection->c_remote_uuid.uuid);
902 rc = ptlrpc_resend(imp);
905 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
906 ptlrpc_activate_import(imp);
908 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
909 &target_start, &target_len);
910 LCONSOLE_INFO("%s: Connection restored to service %.*s "
911 "using nid %s.\n", imp->imp_obd->obd_name,
912 target_len, target_start,
913 libcfs_nid2str(imp->imp_connection->c_peer.nid));
916 if (imp->imp_state == LUSTRE_IMP_FULL) {
917 cfs_waitq_signal(&imp->imp_recovery_waitq);
918 ptlrpc_wake_delayed(imp);
925 static int back_to_sleep(void *unused)
930 int ptlrpc_disconnect_import(struct obd_import *imp)
932 struct ptlrpc_request *req;
936 switch (imp->imp_connect_op) {
937 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
938 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
939 case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
941 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
942 obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
946 if (ptlrpc_import_in_recovery(imp)) {
947 struct l_wait_info lwi;
948 cfs_duration_t timeout = cfs_time_seconds(obd_timeout);
950 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
951 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
952 rc = l_wait_event(imp->imp_recovery_waitq,
953 !ptlrpc_import_in_recovery(imp), &lwi);
957 spin_lock(&imp->imp_lock);
958 if (imp->imp_state != LUSTRE_IMP_FULL)
961 spin_unlock(&imp->imp_lock);
963 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, rq_opc, 1, NULL, NULL);
965 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
966 * it fails. We can get through the above with a down server
967 * if the client doesn't know the server is gone yet. */
968 req->rq_no_resend = 1;
970 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
971 req->rq_send_state = LUSTRE_IMP_CONNECTING;
972 ptlrpc_req_set_repsize(req, 1, NULL);
973 rc = ptlrpc_queue_wait(req);
974 ptlrpc_req_finished(req);
977 spin_lock(&imp->imp_lock);
979 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
980 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
981 spin_unlock(&imp->imp_lock);