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 struct ptlrpc_connect_async_args {
43 __u64 pcaa_peer_committed;
44 int pcaa_initial_connect;
48 /* A CLOSED import should remain so. */
49 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
51 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
52 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
53 imp, imp->imp_target_uuid.uuid, \
54 ptlrpc_import_state_name(imp->imp_state), \
55 ptlrpc_import_state_name(state)); \
56 imp->imp_state = state; \
60 #define IMPORT_SET_STATE(imp, state) \
62 unsigned long flags; \
64 spin_lock_irqsave(&imp->imp_lock, flags); \
65 IMPORT_SET_STATE_NOLOCK(imp, state); \
66 spin_unlock_irqrestore(&imp->imp_lock, flags); \
70 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
72 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
74 /* Only this function is allowed to change the import state when it is
75 * CLOSED. I would rather refcount the import and free it after
76 * disconnection like we do with exports. To do that, the client_obd
77 * will need to save the peer info somewhere other than in the import,
79 int ptlrpc_init_import(struct obd_import *imp)
83 spin_lock_irqsave(&imp->imp_lock, flags);
85 imp->imp_generation++;
86 imp->imp_state = LUSTRE_IMP_NEW;
88 spin_unlock_irqrestore(&imp->imp_lock, flags);
93 /* Returns true if import was FULL, false if import was already not
96 int ptlrpc_set_import_discon(struct obd_import *imp)
101 spin_lock_irqsave(&imp->imp_lock, flags);
103 if (imp->imp_state == LUSTRE_IMP_FULL) {
104 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
107 CDEBUG(D_HA, "%p %s: import already not connected: %s\n",
108 imp,imp->imp_client->cli_name,
109 ptlrpc_import_state_name(imp->imp_state));
111 spin_unlock_irqrestore(&imp->imp_lock, flags);
116 void ptlrpc_fail_import(struct obd_import *imp, int generation)
120 LASSERT (!imp->imp_dlm_fake);
122 if (ptlrpc_set_import_discon(imp))
123 ptlrpc_handle_failed_import(imp);
128 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid)
130 struct obd_device *obd = imp->imp_obd;
131 int initial_connect = 0;
133 __u64 committed_before_reconnect = 0;
135 struct ptlrpc_request *request;
136 int size[] = {sizeof(imp->imp_target_uuid),
137 sizeof(obd->obd_uuid),
138 sizeof(imp->imp_dlm_handle)};
139 char *tmp[] = {imp->imp_target_uuid.uuid,
141 (char *)&imp->imp_dlm_handle};
142 struct ptlrpc_connect_async_args *aa;
145 spin_lock_irqsave(&imp->imp_lock, flags);
146 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
147 spin_unlock_irqrestore(&imp->imp_lock, flags);
148 CERROR("can't connect to a closed import\n");
150 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
151 spin_unlock_irqrestore(&imp->imp_lock, flags);
152 CERROR("already connected\n");
154 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
155 spin_unlock_irqrestore(&imp->imp_lock, flags);
156 CERROR("already connecting\n");
160 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
163 imp->imp_last_replay_transno = 0;
165 if (imp->imp_remote_handle.cookie == 0) {
168 committed_before_reconnect = imp->imp_peer_committed_transno;;
172 if (imp->imp_invalid) {
173 imp->imp_invalid = 0;
177 spin_unlock_irqrestore(&imp->imp_lock, flags);
180 struct ptlrpc_connection *conn;
181 struct obd_uuid uuid;
182 struct obd_export *dlmexp;
184 obd_str2uuid(&uuid, new_uuid);
186 conn = ptlrpc_uuid_to_connection(&uuid);
188 GOTO(out, rc = -ENOENT);
190 CDEBUG(D_HA, "switching import %s/%s from %s to %s\n",
191 imp->imp_target_uuid.uuid, imp->imp_obd->obd_name,
192 imp->imp_connection->c_remote_uuid.uuid,
193 conn->c_remote_uuid.uuid);
195 /* Switch the import's connection and the DLM export's
196 * connection (which are almost certainly the same, but we
197 * keep distinct refs just to make things clearer. I think. */
198 if (imp->imp_connection)
199 ptlrpc_put_connection(imp->imp_connection);
200 /* We hand off the ref from ptlrpc_get_connection. */
201 imp->imp_connection = conn;
203 dlmexp = class_conn2export(&imp->imp_dlm_handle);
205 LASSERT(dlmexp != NULL);
207 if (dlmexp->exp_connection)
208 ptlrpc_put_connection(dlmexp->exp_connection);
209 dlmexp->exp_connection = ptlrpc_connection_addref(conn);
210 class_export_put(dlmexp);
214 request = ptlrpc_prep_req(imp, imp->imp_connect_op, 3, size, tmp);
216 GOTO(out, rc = -ENOMEM);
218 request->rq_send_state = LUSTRE_IMP_CONNECTING;
219 request->rq_replen = lustre_msg_size(0, NULL);
220 request->rq_interpret_reply = ptlrpc_connect_interpret;
222 LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
223 aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
224 memset(aa, 0, sizeof *aa);
226 aa->pcaa_peer_committed = committed_before_reconnect;
227 aa->pcaa_initial_connect = initial_connect;
228 aa->pcaa_was_invalid = was_invalid;
230 if (aa->pcaa_initial_connect)
231 imp->imp_replayable = 1;
232 ptlrpcd_add_req(request);
236 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
242 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
245 struct ptlrpc_connect_async_args *aa = data;
246 struct obd_import *imp = request->rq_import;
247 struct lustre_handle old_hdl;
252 spin_lock_irqsave(&imp->imp_lock, flags);
253 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
254 spin_unlock_irqrestore(&imp->imp_lock, flags);
257 spin_unlock_irqrestore(&imp->imp_lock, flags);
262 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
264 if (aa->pcaa_initial_connect) {
265 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
266 CDEBUG(D_HA, "connected to replayable target: %s\n",
267 imp->imp_target_uuid.uuid);
268 imp->imp_replayable = 1;
269 ptlrpc_pinger_add_import(imp);
271 imp->imp_replayable = 0;
273 imp->imp_remote_handle = request->rq_repmsg->handle;
274 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
275 GOTO(finish, rc = 0);
278 /* Determine what recovery state to move the import to. */
279 if (MSG_CONNECT_RECONNECT & msg_flags) {
280 memset(&old_hdl, 0, sizeof(old_hdl));
281 if (!memcmp(&old_hdl, &request->rq_repmsg->handle,
283 CERROR("%s@%s didn't like our handle "LPX64
284 ", failed\n", imp->imp_target_uuid.uuid,
285 imp->imp_connection->c_remote_uuid.uuid,
286 imp->imp_dlm_handle.cookie);
287 GOTO(out, rc = -ENOTCONN);
290 if (memcmp(&imp->imp_remote_handle, &request->rq_repmsg->handle,
291 sizeof(imp->imp_remote_handle))) {
292 CERROR("%s@%s changed handle from "LPX64" to "LPX64
293 "; copying, but this may foreshadow disaster\n",
294 imp->imp_target_uuid.uuid,
295 imp->imp_connection->c_remote_uuid.uuid,
296 imp->imp_remote_handle.cookie,
297 request->rq_repmsg->handle.cookie);
298 imp->imp_remote_handle = request->rq_repmsg->handle;
300 CERROR("reconnected to %s@%s after partition\n",
301 imp->imp_target_uuid.uuid,
302 imp->imp_connection->c_remote_uuid.uuid);
304 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
306 else if (MSG_CONNECT_RECOVERING & msg_flags) {
307 LASSERT(imp->imp_replayable);
308 imp->imp_state = LUSTRE_IMP_RECOVER;
309 imp->imp_remote_handle = request->rq_repmsg->handle;
310 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
313 imp->imp_remote_handle = request->rq_repmsg->handle;
314 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
317 /* Sanity checks for a reconnected import. */
318 if (!(imp->imp_replayable) !=
319 !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
320 CERROR("imp_replayable flag does not match server "
321 "after reconnect. We should LBUG right here.\n");
324 if (request->rq_repmsg->last_committed < aa->pcaa_peer_committed) {
325 CERROR("%s went back in time (transno "LPD64
326 " was previously committed, server now claims "LPD64
327 ")! is shared storage not coherent?\n",
328 imp->imp_target_uuid.uuid,
329 aa->pcaa_peer_committed,
330 request->rq_repmsg->last_committed);
334 rc = ptlrpc_import_recovery_state_machine(imp);
336 if (aa->pcaa_was_invalid) {
337 ptlrpc_set_import_active(imp, 0);
340 if (rc == -ENOTCONN) {
341 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
342 "invalidating and reconnecting\n",
343 imp->imp_target_uuid.uuid,
344 imp->imp_connection->c_remote_uuid.uuid);
345 ptlrpc_connect_import(imp, NULL);
351 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
352 if (aa->pcaa_initial_connect && !imp->imp_initial_recov)
355 "recovery of %s on %s failed (%d); restarting\n",
356 imp->imp_target_uuid.uuid,
357 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
358 ptlrpc_handle_failed_import(imp);
362 wake_up(&imp->imp_recovery_waitq);
366 static int completed_replay_interpret(struct ptlrpc_request *req,
369 atomic_dec(&req->rq_import->imp_replay_inflight);
370 ptlrpc_import_recovery_state_machine(req->rq_import);
374 static int signal_completed_replay(struct obd_import *imp)
376 struct ptlrpc_request *req;
379 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
380 atomic_inc(&imp->imp_replay_inflight);
382 req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL, NULL);
386 req->rq_replen = lustre_msg_size(0, NULL);
387 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
388 req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
389 req->rq_timeout *= 3;
390 req->rq_interpret_reply = completed_replay_interpret;
392 ptlrpcd_add_req(req);
397 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
401 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
402 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
403 imp->imp_target_uuid.uuid,
404 imp->imp_connection->c_remote_uuid.uuid);
405 ptlrpc_set_import_active(imp, 0);
406 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
409 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
410 CDEBUG(D_HA, "replay requested by %s\n",
411 imp->imp_target_uuid.uuid);
412 rc = ptlrpc_replay_next(imp);
413 if (rc == 0 && atomic_read(&imp->imp_replay_inflight) == 0) {
414 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
415 rc = ldlm_replay_locks(imp);
422 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
423 if (atomic_read(&imp->imp_replay_inflight) == 0) {
424 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
425 rc = signal_completed_replay(imp);
432 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
433 if (atomic_read(&imp->imp_replay_inflight) == 0) {
434 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
438 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
439 CDEBUG(D_HA, "reconnected to %s@%s\n",
440 imp->imp_target_uuid.uuid,
441 imp->imp_connection->c_remote_uuid.uuid);
443 ptlrpc_set_import_active(imp, 1);
445 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
448 if (imp->imp_state == LUSTRE_IMP_FULL) {
449 wake_up(&imp->imp_recovery_waitq);
450 ptlrpc_wake_delayed(imp);
457 static int back_to_sleep(void *unused)
462 int ptlrpc_disconnect_import(struct obd_import *imp)
464 struct ptlrpc_request *request;
470 switch (imp->imp_connect_op) {
471 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
472 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
473 case MGMT_CONNECT:rq_opc = MGMT_DISCONNECT;break;
475 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
476 imp->imp_target_uuid.uuid, imp->imp_connect_op);
481 if (ptlrpc_import_in_recovery(imp)) {
482 struct l_wait_info lwi;
483 lwi = LWI_TIMEOUT_INTR(MAX(obd_timeout * HZ, 1), back_to_sleep,
485 rc = l_wait_event(imp->imp_recovery_waitq,
486 !ptlrpc_import_in_recovery(imp), &lwi);
490 spin_lock_irqsave(&imp->imp_lock, flags);
491 if (imp->imp_state != LUSTRE_IMP_FULL) {
494 spin_unlock_irqrestore(&imp->imp_lock, flags);
496 request = ptlrpc_prep_req(imp, rq_opc, 0, NULL, NULL);
498 /* For non-replayable connections, don't attempt
499 reconnect if this fails */
500 if (!imp->imp_replayable) {
501 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
502 request->rq_send_state = LUSTRE_IMP_DISCON;
504 request->rq_replen = lustre_msg_size(0, NULL);
505 rc = ptlrpc_queue_wait(request);
506 ptlrpc_req_finished(request);
509 spin_lock_irqsave(&imp->imp_lock, flags);
511 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
512 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
513 spin_unlock_irqrestore(&imp->imp_lock, flags);