1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Portal-RPC reconnection and replay operations, for use in recovery.
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
9 * Copyright (C) 1996 Peter J. Braam <braam@stelias.com>
10 * Copyright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
11 * Copyright (C) 1999 Seagate Technology Inc.
12 * Copyright (C) 2001 Mountain View Data, Inc.
13 * Copyright (C) 2002 Cluster File Systems, Inc.
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/kmod.h>
21 #define DEBUG_SUBSYSTEM S_RPC
23 #include <linux/lustre_ha.h>
24 #include <linux/lustre_net.h>
25 #include <linux/obd.h>
27 int ptlrpc_reconnect_import(struct obd_import *imp, int rq_opc)
29 struct obd_device *obd = imp->imp_obd;
30 struct client_obd *cli = &obd->u.cli;
31 int size[] = { sizeof(cli->cl_target_uuid), sizeof(obd->obd_uuid) };
32 char *tmp[] = {cli->cl_target_uuid, obd->obd_uuid };
33 struct ptlrpc_connection *conn = imp->imp_connection;
34 struct lustre_handle old_hdl;
35 struct ptlrpc_request *request;
36 struct obd_export *ldlmexp;
39 request = ptlrpc_prep_req(imp, rq_opc, 2, size, tmp);
40 request->rq_level = LUSTRE_CONN_NEW;
41 request->rq_replen = lustre_msg_size(0, NULL);
43 * This address is the export that represents our client-side LDLM
44 * service (for ASTs). We should only have one on this list, so we
45 * just grab the first one.
47 * XXX tear down export, call class_obd_connect?
49 ldlmexp = list_entry(obd->obd_exports.next, struct obd_export,
51 request->rq_reqmsg->addr = (__u64)(unsigned long)ldlmexp;
52 request->rq_reqmsg->cookie = ldlmexp->exp_cookie;
53 rc = ptlrpc_queue_wait(request);
57 /* already connected! */
58 memset(&old_hdl, 0, sizeof(old_hdl));
59 if (!memcmp(&old_hdl.addr, &request->rq_repmsg->addr,
60 sizeof (old_hdl.addr)) &&
61 !memcmp(&old_hdl.cookie, &request->rq_repmsg->cookie,
62 sizeof (old_hdl.cookie))) {
63 CERROR("%s@%s didn't like our handle %Lx/%Lx, failed\n",
64 cli->cl_target_uuid, conn->c_remote_uuid,
65 (__u64)(unsigned long)ldlmexp,
67 GOTO(out_disc, rc = -ENOTCONN);
70 old_hdl.addr = request->rq_repmsg->addr;
71 old_hdl.cookie = request->rq_repmsg->cookie;
72 if (memcmp(&imp->imp_handle, &old_hdl, sizeof(old_hdl))) {
73 CERROR("%s@%s changed handle from %Lx/%Lx to %Lx/%Lx; "
74 "copying, but this may foreshadow disaster\n",
75 cli->cl_target_uuid, conn->c_remote_uuid,
76 old_hdl.addr, old_hdl.cookie,
77 imp->imp_handle.addr, imp->imp_handle.cookie);
78 imp->imp_handle.addr = request->rq_repmsg->addr;
79 imp->imp_handle.cookie = request->rq_repmsg->cookie;
80 GOTO(out_disc, rc = EALREADY);
83 CERROR("reconnected to %s@%s after partition\n",
84 cli->cl_target_uuid, conn->c_remote_uuid);
85 GOTO(out_disc, rc = EALREADY);
87 old_hdl = imp->imp_handle;
88 imp->imp_handle.addr = request->rq_repmsg->addr;
89 imp->imp_handle.cookie = request->rq_repmsg->cookie;
90 CERROR("now connected to %s@%s (%Lx/%Lx, was %Lx/%Lx)!\n",
91 cli->cl_target_uuid, conn->c_remote_uuid,
92 imp->imp_handle.addr, imp->imp_handle.cookie,
93 old_hdl.addr, old_hdl.cookie);
94 GOTO(out_disc, rc = 0);
96 CERROR("cannot connect to %s@%s: rc = %d\n",
97 cli->cl_target_uuid, conn->c_remote_uuid, rc);
98 GOTO(out_disc, rc = -ENOTCONN); /* XXX preserve rc? */
102 ptlrpc_req_finished(request);
106 int ptlrpc_run_recovery_upcall(struct ptlrpc_connection *conn)
113 conn->c_level = LUSTRE_CONN_RECOVD;
115 argv[0] = obd_recovery_upcall;
116 argv[1] = conn->c_remote_uuid;
120 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
123 rc = call_usermodehelper(argv[0], argv, envp);
125 CERROR("Error invoking recovery upcall %s for %s: %d\n",
126 argv[0], argv[1], rc);
127 CERROR("Check /proc/sys/lustre/recovery_upcall?\n");
129 CERROR("Invoked upcall %s for connection %s\n",
134 * We don't want to make this a "failed" recovery, because the system
135 * administrator -- or, perhaps, tester -- may well be able to rescue
136 * things by running the correct upcall.
141 #define REPLAY_COMMITTED 0 /* Fully processed (commit + reply). */
142 #define REPLAY_REPLAY 1 /* Forced-replay (e.g. open). */
143 #define REPLAY_RESEND 2 /* Resend required. */
144 #define REPLAY_RESEND_IGNORE 3 /* Resend, ignore the reply (already saw it). */
145 #define REPLAY_RESTART 4 /* Have to restart the call, sorry! */
147 static int replay_state(struct ptlrpc_request *req, __u64 committed)
149 /* This request must always be replayed. */
150 if (req->rq_flags & PTL_RPC_FL_REPLAY)
151 return REPLAY_REPLAY;
153 /* Uncommitted request */
154 if (req->rq_transno > committed) {
155 if (req->rq_flags & PTL_RPC_FL_REPLIED) {
156 /* Saw reply, so resend and ignore new reply. */
157 return REPLAY_RESEND_IGNORE;
160 /* Didn't see reply either, so resend. */
161 return REPLAY_RESEND;
164 /* This request has been committed and we saw the reply. Goodbye! */
165 if (req->rq_flags & PTL_RPC_FL_REPLIED)
166 return REPLAY_COMMITTED;
168 /* Request committed, but we didn't see the reply: have to restart. */
169 return REPLAY_RESTART;
172 static char *replay_state2str(int state) {
173 static char *state_strings[] = {
174 "COMMITTED", "REPLAY", "RESEND", "RESEND_IGNORE", "RESTART",
176 static char *unknown_state = "UNKNOWN";
179 state > (sizeof(state_strings) / sizeof(state_strings[0]))) {
180 return unknown_state;
183 return state_strings[state];
186 int ptlrpc_replay(struct obd_import *imp, int unreplied_only)
189 struct list_head *tmp, *pos;
190 struct ptlrpc_request *req;
191 struct ptlrpc_connection *conn = imp->imp_connection;
192 __u64 committed = imp->imp_peer_committed_transno;
195 spin_lock(&imp->imp_lock);
197 CDEBUG(D_HA, "import %p from %s has committed "LPD64"\n",
198 imp, imp->imp_obd->u.cli.cl_target_uuid, committed);
200 list_for_each(tmp, &imp->imp_request_list) {
201 req = list_entry(tmp, struct ptlrpc_request, rq_list);
202 state = replay_state(req, committed);
203 DEBUG_REQ(D_HA, req, "SENDING: %s: ", replay_state2str(state));
206 list_for_each(tmp, &conn->c_delayed_head) {
207 req = list_entry(tmp, struct ptlrpc_request, rq_list);
208 state = replay_state(req, committed);
209 DEBUG_REQ(D_HA, req, "DELAYED: %s: ", replay_state2str(state));
212 list_for_each_safe(tmp, pos, &imp->imp_request_list) {
213 req = list_entry(tmp, struct ptlrpc_request, rq_list);
215 if (unreplied_only) {
216 if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
217 DEBUG_REQ(D_HA, req, "UNREPLIED:");
218 ptlrpc_restart_req(req);
223 state = replay_state(req, committed);
225 if (req->rq_transno == imp->imp_max_transno) {
226 req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
227 DEBUG_REQ(D_HA, req, "last for replay");
228 LASSERT(state != REPLAY_COMMITTED);
233 DEBUG_REQ(D_HA, req, "REPLAY:");
234 rc = ptlrpc_replay_req(req);
236 #error We should not hold a spinlock over such a lengthy operation.
237 #error If necessary, drop spinlock, do operation, re-get spinlock, restart loop.
238 #error If we need to avoid re-processint items, then delete them from the list
239 #error as they are replayed and re-add at the tail of this list, so the next
240 #error item to process will always be at the head of the list.
243 CERROR("recovery replay error %d for req %Ld\n",
249 case REPLAY_COMMITTED:
250 DEBUG_REQ(D_ERROR, req, "COMMITTED:");
251 /* XXX commit now? */
254 case REPLAY_RESEND_IGNORE:
255 DEBUG_REQ(D_HA, req, "RESEND_IGNORE:");
256 rc = ptlrpc_replay_req(req);
258 CERROR("request resend error %d for req %Ld\n",
265 DEBUG_REQ(D_HA, req, "RESTART:");
266 ptlrpc_restart_req(req);
270 DEBUG_REQ(D_HA, req, "RESEND:");
271 ptlrpc_resend_req(req);
280 conn->c_level = LUSTRE_CONN_FULL;
281 recovd_conn_fixed(conn);
283 CERROR("recovery complete on conn %p(%s), waking delayed reqs\n",
284 conn, conn->c_remote_uuid);
285 /* Finally, continue processing requests that blocked for recovery. */
286 list_for_each_safe(tmp, pos, &conn->c_delayed_head) {
287 req = list_entry(tmp, struct ptlrpc_request, rq_list);
288 DEBUG_REQ(D_HA, req, "WAKING: ");
289 ptlrpc_continue_req(req);
294 spin_unlock(&conn->c_lock);