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);
206 sptlrpc_import_flush_all_ctx(imp);
209 /* unset imp_invalid */
210 void ptlrpc_activate_import(struct obd_import *imp)
212 struct obd_device *obd = imp->imp_obd;
214 spin_lock(&imp->imp_lock);
215 imp->imp_invalid = 0;
216 spin_unlock(&imp->imp_lock);
218 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
221 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
225 LASSERT(!imp->imp_dlm_fake);
227 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
228 if (!imp->imp_replayable) {
229 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
230 "auto-deactivating\n",
231 obd2cli_tgt(imp->imp_obd),
232 imp->imp_connection->c_remote_uuid.uuid,
233 imp->imp_obd->obd_name);
234 ptlrpc_deactivate_import(imp);
237 CDEBUG(D_HA, "%s: waking up pinger\n",
238 obd2cli_tgt(imp->imp_obd));
240 spin_lock(&imp->imp_lock);
241 imp->imp_force_verify = 1;
242 spin_unlock(&imp->imp_lock);
244 ptlrpc_pinger_wake_up();
249 static int import_select_connection(struct obd_import *imp)
251 struct obd_import_conn *imp_conn = NULL, *conn;
252 struct obd_export *dlmexp;
255 spin_lock(&imp->imp_lock);
257 if (list_empty(&imp->imp_conn_list)) {
258 CERROR("%s: no connections available\n",
259 imp->imp_obd->obd_name);
260 spin_unlock(&imp->imp_lock);
264 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
265 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
266 imp->imp_obd->obd_name,
267 libcfs_nid2str(conn->oic_conn->c_peer.nid),
268 conn->oic_last_attempt);
269 /* Throttle the reconnect rate to once per RECONNECT_INTERVAL */
270 if (cfs_time_before_64(conn->oic_last_attempt +
271 RECONNECT_INTERVAL * HZ,
272 cfs_time_current_64())) {
273 /* If we have never tried this connection since the
274 the last successful attempt, go with this one */
275 if (cfs_time_before_64(conn->oic_last_attempt,
276 imp->imp_last_success_conn)) {
281 /* Both of these connections have already been tried
282 since the last successful connection; just choose the
283 least recently used */
286 else if (cfs_time_before_64(conn->oic_last_attempt,
287 imp_conn->oic_last_attempt))
292 /* if not found, simply choose the current one */
294 LASSERT(imp->imp_conn_current);
295 imp_conn = imp->imp_conn_current;
297 LASSERT(imp_conn->oic_conn);
299 imp_conn->oic_last_attempt = cfs_time_current_64();
301 /* switch connection, don't mind if it's same as the current one */
302 if (imp->imp_connection)
303 ptlrpc_put_connection(imp->imp_connection);
304 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
306 dlmexp = class_conn2export(&imp->imp_dlm_handle);
307 LASSERT(dlmexp != NULL);
308 if (dlmexp->exp_connection)
309 ptlrpc_put_connection(dlmexp->exp_connection);
310 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
311 class_export_put(dlmexp);
313 if (imp->imp_conn_current != imp_conn) {
314 if (imp->imp_conn_current)
315 LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
316 imp->imp_obd->obd_name,
317 imp_conn->oic_uuid.uuid,
318 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
319 imp->imp_conn_current = imp_conn;
322 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
323 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
324 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
326 spin_unlock(&imp->imp_lock);
332 * must be called under imp_lock
334 int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
336 struct ptlrpc_request *req;
337 struct list_head *tmp;
339 if (list_empty(&imp->imp_replay_list))
341 tmp = imp->imp_replay_list.next;
342 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
343 *transno = req->rq_transno;
347 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
349 struct obd_device *obd = imp->imp_obd;
350 int initial_connect = 0;
353 __u64 committed_before_reconnect = 0;
354 struct ptlrpc_request *request;
355 int size[] = { sizeof(struct ptlrpc_body),
356 sizeof(imp->imp_obd->u.cli.cl_target_uuid),
357 sizeof(obd->obd_uuid),
358 sizeof(imp->imp_dlm_handle),
359 sizeof(imp->imp_connect_data) };
360 char *tmp[] = { NULL,
361 obd2cli_tgt(imp->imp_obd),
363 (char *)&imp->imp_dlm_handle,
364 (char *)&imp->imp_connect_data };
365 struct ptlrpc_connect_async_args *aa;
368 spin_lock(&imp->imp_lock);
369 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
370 spin_unlock(&imp->imp_lock);
371 CERROR("can't connect to a closed import\n");
373 } else if (imp->imp_state == LUSTRE_IMP_FULL &&
374 imp->imp_force_reconnect == 0) {
375 spin_unlock(&imp->imp_lock);
376 CERROR("already connected\n");
378 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
379 spin_unlock(&imp->imp_lock);
380 CERROR("already connecting\n");
384 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
387 imp->imp_resend_replay = 0;
389 if (!lustre_handle_is_used(&imp->imp_remote_handle))
392 committed_before_reconnect = imp->imp_peer_committed_transno;
394 set_transno = ptlrpc_first_transno(imp, &imp->imp_connect_data.ocd_transno);
395 spin_unlock(&imp->imp_lock);
398 struct obd_uuid uuid;
400 obd_str2uuid(&uuid, new_uuid);
401 rc = import_set_conn_priority(imp, &uuid);
406 rc = import_select_connection(imp);
410 /* last in connection list */
411 if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
412 if (imp->imp_initial_recov_bk && initial_connect) {
413 CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
414 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
415 /* Don't retry if connect fails */
417 obd_set_info_async(obd->obd_self_export,
418 strlen(KEY_INIT_RECOV),
420 sizeof(rc), &rc, NULL);
422 if (imp->imp_recon_bk) {
423 CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
424 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
425 imp->imp_last_recon = 1;
429 /* Reset connect flags to the originally requested flags, in case
430 * the server is updated on-the-fly we will get the new features. */
431 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
432 rc = obd_reconnect(imp->imp_obd->obd_self_export, obd,
433 &obd->obd_uuid, &imp->imp_connect_data);
437 request = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, imp->imp_connect_op,
440 GOTO(out, rc = -ENOMEM);
443 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
445 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
447 request->rq_send_state = LUSTRE_IMP_CONNECTING;
448 /* Allow a slightly larger reply for future growth compatibility */
449 size[REPLY_REC_OFF] = sizeof(struct obd_connect_data) +
451 ptlrpc_req_set_repsize(request, 2, size);
452 request->rq_interpret_reply = ptlrpc_connect_interpret;
454 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
455 aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
456 memset(aa, 0, sizeof *aa);
458 aa->pcaa_peer_committed = committed_before_reconnect;
459 aa->pcaa_initial_connect = initial_connect;
461 if (aa->pcaa_initial_connect) {
462 imp->imp_replayable = 1;
463 /* On an initial connect, we don't know which one of a
464 failover server pair is up. Don't wait long. */
465 request->rq_timeout = max((int)(obd_timeout / 20), 5);
466 lustre_msg_add_op_flags(request->rq_reqmsg,
467 MSG_CONNECT_INITIAL);
472 lustre_msg_add_op_flags(request->rq_reqmsg,
473 MSG_CONNECT_TRANSNO);
475 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
476 ptlrpcd_add_req(request);
480 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
485 EXPORT_SYMBOL(ptlrpc_connect_import);
487 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
489 struct obd_import_conn *imp_conn;
494 spin_lock(&imp->imp_lock);
495 if (list_empty(&imp->imp_conn_list))
498 imp_conn = list_entry(imp->imp_conn_list.prev,
499 struct obd_import_conn,
502 if (imp->imp_conn_current != imp_conn) {
503 ptlrpc_ping_import_soon(imp);
508 spin_unlock(&imp->imp_lock);
511 ptlrpc_pinger_wake_up();
516 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
519 struct ptlrpc_connect_async_args *aa = data;
520 struct obd_import *imp = request->rq_import;
521 struct client_obd *cli = &imp->imp_obd->u.cli;
522 struct lustre_handle old_hdl;
526 spin_lock(&imp->imp_lock);
527 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
528 spin_unlock(&imp->imp_lock);
531 imp->imp_force_reconnect = 0;
532 spin_unlock(&imp->imp_lock);
537 rc = sptlrpc_cli_install_rvs_ctx(imp, request->rq_cli_ctx);
541 LASSERT(imp->imp_conn_current);
543 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
545 /* All imports are pingable */
546 imp->imp_pingable = 1;
548 if (aa->pcaa_initial_connect) {
549 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
550 CDEBUG(D_HA, "connected to replayable target: %s\n",
551 obd2cli_tgt(imp->imp_obd));
552 imp->imp_replayable = 1;
554 imp->imp_replayable = 0;
557 if (msg_flags & MSG_CONNECT_NEXT_VER) {
558 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
559 CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v2\n",
560 obd2cli_tgt(imp->imp_obd));
562 CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v1\n",
563 obd2cli_tgt(imp->imp_obd));
566 imp->imp_remote_handle =
567 *lustre_msg_get_handle(request->rq_repmsg);
569 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
570 GOTO(finish, rc = 0);
573 /* Determine what recovery state to move the import to. */
574 if (MSG_CONNECT_RECONNECT & msg_flags) {
575 memset(&old_hdl, 0, sizeof(old_hdl));
576 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
578 CERROR("%s@%s didn't like our handle "LPX64
579 ", failed\n", obd2cli_tgt(imp->imp_obd),
580 imp->imp_connection->c_remote_uuid.uuid,
581 imp->imp_dlm_handle.cookie);
582 GOTO(out, rc = -ENOTCONN);
585 if (memcmp(&imp->imp_remote_handle,
586 lustre_msg_get_handle(request->rq_repmsg),
587 sizeof(imp->imp_remote_handle))) {
589 /* The MGC does this all the time */
590 if (strcmp(imp->imp_obd->obd_type->typ_name,
591 LUSTRE_MGC_NAME) == 0) {
595 "%s@%s changed handle from "LPX64" to "LPX64
596 "; copying, but this may foreshadow disaster\n",
597 obd2cli_tgt(imp->imp_obd),
598 imp->imp_connection->c_remote_uuid.uuid,
599 imp->imp_remote_handle.cookie,
600 lustre_msg_get_handle(request->rq_repmsg)->
602 imp->imp_remote_handle =
603 *lustre_msg_get_handle(request->rq_repmsg);
605 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
606 obd2cli_tgt(imp->imp_obd),
607 imp->imp_connection->c_remote_uuid.uuid);
610 if (imp->imp_invalid) {
611 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
612 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
613 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
614 imp->imp_obd->obd_name,
615 obd2cli_tgt(imp->imp_obd));
616 imp->imp_resend_replay = 1;
617 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
619 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
621 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
622 LASSERT(imp->imp_replayable);
623 imp->imp_remote_handle =
624 *lustre_msg_get_handle(request->rq_repmsg);
625 imp->imp_last_replay_transno = 0;
626 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
628 imp->imp_remote_handle =
629 *lustre_msg_get_handle(request->rq_repmsg);
630 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
633 /* Sanity checks for a reconnected import. */
634 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
635 CERROR("imp_replayable flag does not match server "
636 "after reconnect. We should LBUG right here.\n");
639 if (lustre_msg_get_last_committed(request->rq_repmsg) <
640 aa->pcaa_peer_committed) {
641 CERROR("%s went back in time (transno "LPD64
642 " was previously committed, server now claims "LPD64
643 ")! See https://bugzilla.clusterfs.com/"
644 "long_list.cgi?buglist=9646\n",
645 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
646 lustre_msg_get_last_committed(request->rq_repmsg));
650 rc = ptlrpc_import_recovery_state_machine(imp);
652 if (rc == -ENOTCONN) {
653 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
654 "invalidating and reconnecting\n",
655 obd2cli_tgt(imp->imp_obd),
656 imp->imp_connection->c_remote_uuid.uuid);
657 ptlrpc_connect_import(imp, NULL);
661 struct obd_connect_data *ocd;
662 struct obd_export *exp;
664 ocd = lustre_swab_repbuf(request, REPLY_REC_OFF, sizeof(*ocd),
665 lustre_swab_connect);
667 spin_lock(&imp->imp_lock);
668 list_del(&imp->imp_conn_current->oic_item);
669 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
670 imp->imp_last_success_conn =
671 imp->imp_conn_current->oic_last_attempt;
674 spin_unlock(&imp->imp_lock);
675 CERROR("Wrong connect data from server\n");
680 imp->imp_connect_data = *ocd;
682 exp = class_conn2export(&imp->imp_dlm_handle);
683 spin_unlock(&imp->imp_lock);
685 /* check that server granted subset of flags we asked for. */
686 LASSERTF((ocd->ocd_connect_flags &
687 imp->imp_connect_flags_orig) ==
688 ocd->ocd_connect_flags, LPX64" != "LPX64,
689 imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
692 /* This could happen if export is cleaned during the
694 spin_unlock(&imp->imp_lock);
695 CERROR("Missing export for %s\n",
696 imp->imp_obd->obd_name);
697 GOTO(out, rc = -ENODEV);
699 exp->exp_connect_flags = ocd->ocd_connect_flags;
700 class_export_put(exp);
702 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
704 if (!ocd->ocd_ibits_known &&
705 ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
706 CERROR("Inodebits aware server returned zero compatible"
709 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
710 (ocd->ocd_version > LUSTRE_VERSION_CODE +
711 LUSTRE_VERSION_OFFSET_WARN)) {
712 /* Sigh, some compilers do not like #ifdef in the middle
713 of macro arguments */
715 const char *action = "upgrading this client";
717 const char *action = "recompiling this application";
720 CWARN("Server %s version (%d.%d.%d.%d) is much newer. "
721 "Consider %s (%s).\n",
722 obd2cli_tgt(imp->imp_obd),
723 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
724 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
725 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
726 OBD_OCD_VERSION_FIX(ocd->ocd_version),
727 action, LUSTRE_VERSION_STRING);
730 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
731 cli->cl_max_pages_per_rpc = ocd->ocd_brw_size >> PAGE_SHIFT;
733 cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
734 (int)(1024 * 1024 >> PAGE_SHIFT));
737 LASSERT(cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES &&
738 cli->cl_max_pages_per_rpc > 0);
744 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
745 if (aa->pcaa_initial_connect && !imp->imp_initial_recov)
746 ptlrpc_deactivate_import(imp);
748 if (imp->imp_recon_bk && imp->imp_last_recon) {
749 /* Give up trying to reconnect */
750 imp->imp_obd->obd_no_recov = 1;
751 ptlrpc_deactivate_import(imp);
755 struct obd_connect_data *ocd;
757 /* reply message might not be ready */
758 if (request->rq_repmsg != NULL)
761 ocd = lustre_swab_repbuf(request, REPLY_REC_OFF,
763 lustre_swab_connect);
765 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
766 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
767 /* Actually servers are only supposed to refuse
768 connection from liblustre clients, so we should
769 never see this from VFS context */
770 CERROR("Server %s version (%d.%d.%d.%d) "
771 "refused connection from this client "
772 "as too old version (%s). Client must "
774 obd2cli_tgt(imp->imp_obd),
775 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
776 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
777 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
778 OBD_OCD_VERSION_FIX(ocd->ocd_version),
779 LUSTRE_VERSION_STRING);
780 ptlrpc_deactivate_import(imp);
781 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
786 ptlrpc_maybe_ping_import_soon(imp);
788 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
789 obd2cli_tgt(imp->imp_obd),
790 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
793 imp->imp_last_recon = 0;
795 cfs_waitq_signal(&imp->imp_recovery_waitq);
799 static int completed_replay_interpret(struct ptlrpc_request *req,
803 atomic_dec(&req->rq_import->imp_replay_inflight);
804 if (req->rq_status == 0) {
805 ptlrpc_import_recovery_state_machine(req->rq_import);
807 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
809 req->rq_import->imp_obd->obd_name, req->rq_status);
810 ptlrpc_connect_import(req->rq_import, NULL);
816 static int signal_completed_replay(struct obd_import *imp)
818 struct ptlrpc_request *req;
821 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
822 atomic_inc(&imp->imp_replay_inflight);
824 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 1, NULL, NULL);
826 atomic_dec(&imp->imp_replay_inflight);
830 ptlrpc_req_set_repsize(req, 1, NULL);
831 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
832 lustre_msg_add_flags(req->rq_reqmsg,
833 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
834 req->rq_timeout *= 3;
835 req->rq_interpret_reply = completed_replay_interpret;
837 ptlrpcd_add_req(req);
842 static int ptlrpc_invalidate_import_thread(void *data)
844 struct obd_import *imp = data;
848 ptlrpc_daemonize("ll_imp_inval");
850 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
851 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
852 imp->imp_connection->c_remote_uuid.uuid);
854 ptlrpc_invalidate_import(imp);
856 if (obd_dump_on_eviction) {
857 CERROR("dump the log upon eviction\n");
858 libcfs_debug_dumplog();
861 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
862 ptlrpc_import_recovery_state_machine(imp);
868 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
876 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
877 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
878 &target_start, &target_len);
879 LCONSOLE_ERROR("This client was evicted by %.*s; in progress "
880 "operations using this service will fail.\n",
881 target_len, target_start);
882 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
883 obd2cli_tgt(imp->imp_obd),
884 imp->imp_connection->c_remote_uuid.uuid);
887 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
888 CLONE_VM | CLONE_FILES);
890 CERROR("error starting invalidate thread: %d\n", rc);
895 ptlrpc_invalidate_import(imp);
897 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
901 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
902 CDEBUG(D_HA, "replay requested by %s\n",
903 obd2cli_tgt(imp->imp_obd));
904 rc = ptlrpc_replay_next(imp, &inflight);
906 atomic_read(&imp->imp_replay_inflight) == 0) {
907 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
908 rc = ldlm_replay_locks(imp);
915 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
916 if (atomic_read(&imp->imp_replay_inflight) == 0) {
917 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
918 rc = signal_completed_replay(imp);
925 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
926 if (atomic_read(&imp->imp_replay_inflight) == 0) {
927 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
931 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
932 CDEBUG(D_HA, "reconnected to %s@%s\n",
933 obd2cli_tgt(imp->imp_obd),
934 imp->imp_connection->c_remote_uuid.uuid);
936 rc = ptlrpc_resend(imp);
939 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
940 ptlrpc_activate_import(imp);
942 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
943 &target_start, &target_len);
944 LCONSOLE_INFO("%s: Connection restored to service %.*s "
945 "using nid %s.\n", imp->imp_obd->obd_name,
946 target_len, target_start,
947 libcfs_nid2str(imp->imp_connection->c_peer.nid));
950 if (imp->imp_state == LUSTRE_IMP_FULL) {
951 cfs_waitq_signal(&imp->imp_recovery_waitq);
952 ptlrpc_wake_delayed(imp);
959 static int back_to_sleep(void *unused)
964 int ptlrpc_disconnect_import(struct obd_import *imp)
966 struct ptlrpc_request *req;
970 switch (imp->imp_connect_op) {
971 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
972 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
973 case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
975 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
976 obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
980 if (ptlrpc_import_in_recovery(imp)) {
981 struct l_wait_info lwi;
982 cfs_duration_t timeout = cfs_time_seconds(obd_timeout);
984 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
985 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
986 rc = l_wait_event(imp->imp_recovery_waitq,
987 !ptlrpc_import_in_recovery(imp), &lwi);
991 spin_lock(&imp->imp_lock);
992 if (imp->imp_state != LUSTRE_IMP_FULL)
995 spin_unlock(&imp->imp_lock);
997 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, rq_opc, 1, NULL, NULL);
999 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1000 * it fails. We can get through the above with a down server
1001 * if the client doesn't know the server is gone yet. */
1002 req->rq_no_resend = 1;
1003 req->rq_timeout = 5;
1004 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1005 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1006 ptlrpc_req_set_repsize(req, 1, NULL);
1007 rc = ptlrpc_queue_wait(req);
1008 ptlrpc_req_finished(req);
1011 spin_lock(&imp->imp_lock);
1013 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1014 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1015 spin_unlock(&imp->imp_lock);