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 Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_RPC
25 # include <linux/config.h>
26 # include <linux/module.h>
27 # include <linux/kmod.h>
29 # include <liblustre.h>
32 #include <linux/obd_support.h>
33 #include <linux/lustre_ha.h>
34 #include <linux/lustre_net.h>
35 #include <linux/lustre_import.h>
36 #include <linux/lustre_export.h>
37 #include <linux/obd.h>
38 #include <linux/obd_class.h>
40 #include "ptlrpc_internal.h"
42 /* should this take an imp_sem to ensure connect is single threaded? */
43 int ptlrpc_connect_import(struct obd_import *imp)
45 struct obd_device *obd = imp->imp_obd;
47 int initial_connect = 0;
49 __u64 committed_before_reconnect = 0;
50 struct ptlrpc_request *request;
51 struct lustre_handle old_hdl;
52 int size[] = {sizeof(imp->imp_target_uuid),
53 sizeof(obd->obd_uuid),
54 sizeof(imp->imp_dlm_handle)};
55 char *tmp[] = {imp->imp_target_uuid.uuid,
57 (char *)&imp->imp_dlm_handle};
60 spin_lock_irqsave(&imp->imp_lock, flags);
61 if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
62 spin_unlock_irqrestore(&imp->imp_lock, flags);
65 LASSERT(imp->imp_state == LUSTRE_IMP_DISCON);
67 CDEBUG(D_HA, "%s: new state: CONNECTING\n",
68 imp->imp_client->cli_name);
69 imp->imp_state = LUSTRE_IMP_CONNECTING;
71 if (imp->imp_remote_handle.cookie == 0) {
74 committed_before_reconnect = imp->imp_peer_committed_transno;
76 spin_unlock_irqrestore(&imp->imp_lock, flags);
78 request = ptlrpc_prep_req(imp, imp->imp_connect_op, 3, size, tmp);
80 GOTO(out, rc = -ENOMEM);
82 request->rq_send_state = LUSTRE_IMP_CONNECTING;
83 request->rq_replen = lustre_msg_size(0, NULL);
85 // lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_PEER);
87 rc = ptlrpc_queue_wait(request);
92 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
94 if (initial_connect) {
95 CDEBUG(D_HA, "%s: new state: FULL\n",
96 imp->imp_client->cli_name);
97 if (msg_flags & MSG_CONNECT_REPLAYABLE)
98 imp->imp_replayable = 1;
99 imp->imp_remote_handle = request->rq_repmsg->handle;
100 imp->imp_state = LUSTRE_IMP_FULL;
101 GOTO(free_req, rc = 0);
104 /* Determine what recovery state to move the import to. */
105 if (MSG_CONNECT_RECONNECT & msg_flags) {
106 memset(&old_hdl, 0, sizeof(old_hdl));
107 if (!memcmp(&old_hdl, &request->rq_repmsg->handle,
109 CERROR("%s@%s didn't like our handle "LPX64
110 ", failed\n", imp->imp_target_uuid.uuid,
111 imp->imp_connection->c_remote_uuid.uuid,
112 imp->imp_dlm_handle.cookie);
113 GOTO(free_req, rc = -ENOTCONN);
116 if (memcmp(&imp->imp_remote_handle, &request->rq_repmsg->handle,
117 sizeof(imp->imp_remote_handle))) {
118 CERROR("%s@%s changed handle from "LPX64" to "LPX64
119 "; copying, but this may foreshadow disaster\n",
120 imp->imp_target_uuid.uuid,
121 imp->imp_connection->c_remote_uuid.uuid,
122 imp->imp_remote_handle.cookie,
123 request->rq_repmsg->handle.cookie);
124 imp->imp_remote_handle = request->rq_repmsg->handle;
126 CERROR("reconnected to %s@%s after partition\n",
127 imp->imp_target_uuid.uuid,
128 imp->imp_connection->c_remote_uuid.uuid);
130 CDEBUG(D_HA, "%s: new state: RECOVER\n",
131 imp->imp_client->cli_name);
132 imp->imp_state = LUSTRE_IMP_RECOVER;
134 else if (MSG_CONNECT_RECOVERING & msg_flags) {
135 CDEBUG(D_HA, "%s: new state: REPLAY\n",
136 imp->imp_client->cli_name);
137 LASSERT(imp->imp_replayable);
138 imp->imp_state = LUSTRE_IMP_RECOVER;
139 imp->imp_remote_handle = request->rq_repmsg->handle;
140 imp->imp_state = LUSTRE_IMP_REPLAY;
143 CDEBUG(D_HA, "%s: new state: EVICTED\n",
144 imp->imp_client->cli_name);
145 imp->imp_remote_handle = request->rq_repmsg->handle;
146 imp->imp_state = LUSTRE_IMP_EVICTED;
149 /* Sanity checks for a reconnected import. */
150 if (!(imp->imp_replayable) !=
151 !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
152 CERROR("imp_replayable flag does not match server "
153 "after reconnect. We should LBUG right here.\n");
156 if (request->rq_repmsg->last_committed < committed_before_reconnect) {
157 CERROR("%s went back in time (transno "LPD64
158 " was previously committed, server now claims "LPD64
159 ")! is shared storage not coherent?\n",
160 imp->imp_target_uuid.uuid,
161 committed_before_reconnect,
162 request->rq_repmsg->last_committed);
166 ptlrpc_req_finished(request);
170 imp->imp_state = LUSTRE_IMP_DISCON;
176 int ptlrpc_disconnect_import(struct obd_import *imp)
178 struct ptlrpc_request *request;
183 switch (imp->imp_connect_op) {
184 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
185 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
186 case MGMT_CONNECT:rq_opc = MGMT_DISCONNECT;break;
188 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
189 imp->imp_target_uuid.uuid, imp->imp_connect_op);
193 request = ptlrpc_prep_req(imp, rq_opc, 0, NULL, NULL);
195 /* For non-replayable connections, don't attempt
196 reconnect if this fails */
197 if (!imp->imp_obd->obd_replayable) {
198 imp->imp_state = LUSTRE_IMP_DISCON;
199 request->rq_send_state = LUSTRE_IMP_DISCON;
201 request->rq_replen = lustre_msg_size(0, NULL);
202 rc = ptlrpc_queue_wait(request);
203 ptlrpc_req_finished(request);
206 imp->imp_state = LUSTRE_IMP_DISCON;
207 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));