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);
105 spin_unlock_irqrestore(&imp->imp_lock, flags);
106 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
109 spin_unlock_irqrestore(&imp->imp_lock, flags);
110 CDEBUG(D_HA, "%p %s: import already not connected: %s\n",
111 imp,imp->imp_client->cli_name,
112 ptlrpc_import_state_name(imp->imp_state));
118 void ptlrpc_invalidate_import(struct obd_import *imp)
120 struct obd_device *obd = imp->imp_obd;
124 spin_lock_irqsave(&imp->imp_lock, flags);
125 /* This is a bit of a hack, but invalidating replayable
126 * imports makes a temporary reconnect failure into a much more
127 * ugly -- and hard to remedy -- situation. */
128 if (!imp->imp_replayable) {
129 CDEBUG(D_HA, "setting import %s INVALID\n",
130 imp->imp_target_uuid.uuid);
131 imp->imp_invalid = 1;
133 imp->imp_generation++;
134 spin_unlock_irqrestore(&imp->imp_lock, flags);
136 ptlrpc_abort_inflight(imp);
137 obd_import_event(obd, imp, IMP_EVENT_INVALIDATE);
140 void ptlrpc_validate_import(struct obd_import *imp)
142 struct obd_device *obd = imp->imp_obd;
145 spin_lock_irqsave(&imp->imp_lock, flags);
146 imp->imp_invalid = 0;
147 spin_unlock_irqrestore(&imp->imp_lock, flags);
149 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
152 void ptlrpc_fail_import(struct obd_import *imp, int generation)
156 LASSERT (!imp->imp_dlm_fake);
158 if (ptlrpc_set_import_discon(imp)) {
161 if (!imp->imp_replayable) {
162 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
163 "auto-deactivating\n",
164 imp->imp_target_uuid.uuid,
165 imp->imp_connection->c_remote_uuid.uuid,
166 imp->imp_obd->obd_name);
167 ptlrpc_invalidate_import(imp);
170 CDEBUG(D_HA, "%s: waking up pinger\n",
171 imp->imp_target_uuid.uuid);
173 spin_lock_irqsave(&imp->imp_lock, flags);
174 imp->imp_force_verify = 1;
175 spin_unlock_irqrestore(&imp->imp_lock, flags);
177 ptlrpc_pinger_wake_up();
183 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid)
185 struct obd_device *obd = imp->imp_obd;
186 int initial_connect = 0;
188 __u64 committed_before_reconnect = 0;
190 struct ptlrpc_request *request;
191 int size[] = {sizeof(imp->imp_target_uuid),
192 sizeof(obd->obd_uuid),
193 sizeof(imp->imp_dlm_handle)};
194 char *tmp[] = {imp->imp_target_uuid.uuid,
196 (char *)&imp->imp_dlm_handle};
197 struct ptlrpc_connect_async_args *aa;
200 spin_lock_irqsave(&imp->imp_lock, flags);
201 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
202 spin_unlock_irqrestore(&imp->imp_lock, flags);
203 CERROR("can't connect to a closed import\n");
205 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
206 spin_unlock_irqrestore(&imp->imp_lock, flags);
207 CERROR("already connected\n");
209 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
210 spin_unlock_irqrestore(&imp->imp_lock, flags);
211 CERROR("already connecting\n");
215 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
218 imp->imp_last_replay_transno = 0;
220 if (imp->imp_remote_handle.cookie == 0) {
223 committed_before_reconnect = imp->imp_peer_committed_transno;;
227 if (imp->imp_invalid) {
228 imp->imp_invalid = 0;
232 spin_unlock_irqrestore(&imp->imp_lock, flags);
235 struct ptlrpc_connection *conn;
236 struct obd_uuid uuid;
237 struct obd_export *dlmexp;
239 obd_str2uuid(&uuid, new_uuid);
241 conn = ptlrpc_uuid_to_connection(&uuid);
243 GOTO(out, rc = -ENOENT);
245 CDEBUG(D_HA, "switching import %s/%s from %s to %s\n",
246 imp->imp_target_uuid.uuid, imp->imp_obd->obd_name,
247 imp->imp_connection->c_remote_uuid.uuid,
248 conn->c_remote_uuid.uuid);
250 /* Switch the import's connection and the DLM export's
251 * connection (which are almost certainly the same, but we
252 * keep distinct refs just to make things clearer. I think. */
253 if (imp->imp_connection)
254 ptlrpc_put_connection(imp->imp_connection);
255 /* We hand off the ref from ptlrpc_get_connection. */
256 imp->imp_connection = conn;
258 dlmexp = class_conn2export(&imp->imp_dlm_handle);
260 LASSERT(dlmexp != NULL);
262 if (dlmexp->exp_connection)
263 ptlrpc_put_connection(dlmexp->exp_connection);
264 dlmexp->exp_connection = ptlrpc_connection_addref(conn);
265 class_export_put(dlmexp);
269 request = ptlrpc_prep_req(imp, imp->imp_connect_op, 3, size, tmp);
271 GOTO(out, rc = -ENOMEM);
274 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
277 request->rq_send_state = LUSTRE_IMP_CONNECTING;
278 request->rq_replen = lustre_msg_size(0, NULL);
279 request->rq_interpret_reply = ptlrpc_connect_interpret;
281 LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
282 aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
283 memset(aa, 0, sizeof *aa);
285 aa->pcaa_peer_committed = committed_before_reconnect;
286 aa->pcaa_initial_connect = initial_connect;
287 aa->pcaa_was_invalid = was_invalid;
289 if (aa->pcaa_initial_connect)
290 imp->imp_replayable = 1;
292 ptlrpcd_add_req(request);
296 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
302 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
305 struct ptlrpc_connect_async_args *aa = data;
306 struct obd_import *imp = request->rq_import;
307 struct lustre_handle old_hdl;
312 spin_lock_irqsave(&imp->imp_lock, flags);
313 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
314 spin_unlock_irqrestore(&imp->imp_lock, flags);
317 spin_unlock_irqrestore(&imp->imp_lock, flags);
322 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
324 if (aa->pcaa_initial_connect) {
325 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
326 CDEBUG(D_HA, "connected to replayable target: %s\n",
327 imp->imp_target_uuid.uuid);
328 imp->imp_pingable = imp->imp_replayable = 1;
330 imp->imp_replayable = 0;
332 imp->imp_remote_handle = request->rq_repmsg->handle;
333 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
334 GOTO(finish, rc = 0);
337 /* Determine what recovery state to move the import to. */
338 if (MSG_CONNECT_RECONNECT & msg_flags) {
339 memset(&old_hdl, 0, sizeof(old_hdl));
340 if (!memcmp(&old_hdl, &request->rq_repmsg->handle,
342 CERROR("%s@%s didn't like our handle "LPX64
343 ", failed\n", imp->imp_target_uuid.uuid,
344 imp->imp_connection->c_remote_uuid.uuid,
345 imp->imp_dlm_handle.cookie);
346 GOTO(out, rc = -ENOTCONN);
349 if (memcmp(&imp->imp_remote_handle, &request->rq_repmsg->handle,
350 sizeof(imp->imp_remote_handle))) {
351 CERROR("%s@%s changed handle from "LPX64" to "LPX64
352 "; copying, but this may foreshadow disaster\n",
353 imp->imp_target_uuid.uuid,
354 imp->imp_connection->c_remote_uuid.uuid,
355 imp->imp_remote_handle.cookie,
356 request->rq_repmsg->handle.cookie);
357 imp->imp_remote_handle = request->rq_repmsg->handle;
359 CERROR("reconnected to %s@%s after partition\n",
360 imp->imp_target_uuid.uuid,
361 imp->imp_connection->c_remote_uuid.uuid);
363 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
365 else if (MSG_CONNECT_RECOVERING & msg_flags) {
366 LASSERT(imp->imp_replayable);
367 imp->imp_state = LUSTRE_IMP_RECOVER;
368 imp->imp_remote_handle = request->rq_repmsg->handle;
369 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
372 imp->imp_remote_handle = request->rq_repmsg->handle;
373 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
376 /* Sanity checks for a reconnected import. */
377 if (!(imp->imp_replayable) !=
378 !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
379 CERROR("imp_replayable flag does not match server "
380 "after reconnect. We should LBUG right here.\n");
383 if (request->rq_repmsg->last_committed < aa->pcaa_peer_committed) {
384 CERROR("%s went back in time (transno "LPD64
385 " was previously committed, server now claims "LPD64
386 ")! is shared storage not coherent?\n",
387 imp->imp_target_uuid.uuid,
388 aa->pcaa_peer_committed,
389 request->rq_repmsg->last_committed);
393 rc = ptlrpc_import_recovery_state_machine(imp);
395 if (aa->pcaa_was_invalid)
396 ptlrpc_invalidate_import(imp);
398 if (rc == -ENOTCONN) {
399 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
400 "invalidating and reconnecting\n",
401 imp->imp_target_uuid.uuid,
402 imp->imp_connection->c_remote_uuid.uuid);
403 ptlrpc_connect_import(imp, NULL);
409 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
410 if (aa->pcaa_initial_connect && !imp->imp_initial_recov) {
411 ptlrpc_invalidate_import(imp);
413 CDEBUG(D_ERROR, "recovery of %s on %s failed (%d)\n",
414 imp->imp_target_uuid.uuid,
415 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
418 wake_up(&imp->imp_recovery_waitq);
422 static int completed_replay_interpret(struct ptlrpc_request *req,
425 atomic_dec(&req->rq_import->imp_replay_inflight);
426 ptlrpc_import_recovery_state_machine(req->rq_import);
430 static int signal_completed_replay(struct obd_import *imp)
432 struct ptlrpc_request *req;
435 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
436 atomic_inc(&imp->imp_replay_inflight);
438 req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL, NULL);
442 req->rq_replen = lustre_msg_size(0, NULL);
443 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
444 req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
445 req->rq_timeout *= 3;
446 req->rq_interpret_reply = completed_replay_interpret;
448 ptlrpcd_add_req(req);
453 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
458 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
459 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
460 imp->imp_target_uuid.uuid,
461 imp->imp_connection->c_remote_uuid.uuid);
462 ptlrpc_invalidate_import(imp);
463 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
466 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
467 CDEBUG(D_HA, "replay requested by %s\n",
468 imp->imp_target_uuid.uuid);
469 rc = ptlrpc_replay_next(imp, &inflight);
471 atomic_read(&imp->imp_replay_inflight) == 0) {
472 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
473 rc = ldlm_replay_locks(imp);
480 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
481 if (atomic_read(&imp->imp_replay_inflight) == 0) {
482 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
483 rc = signal_completed_replay(imp);
490 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
491 if (atomic_read(&imp->imp_replay_inflight) == 0) {
492 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
496 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
497 CDEBUG(D_HA, "reconnected to %s@%s\n",
498 imp->imp_target_uuid.uuid,
499 imp->imp_connection->c_remote_uuid.uuid);
501 rc = ptlrpc_resend(imp);
504 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
505 ptlrpc_validate_import(imp);
508 if (imp->imp_state == LUSTRE_IMP_FULL) {
509 wake_up(&imp->imp_recovery_waitq);
510 ptlrpc_wake_delayed(imp);
517 static int back_to_sleep(void *unused)
522 int ptlrpc_disconnect_import(struct obd_import *imp)
524 struct ptlrpc_request *request;
530 switch (imp->imp_connect_op) {
531 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
532 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
533 case MGMT_CONNECT:rq_opc = MGMT_DISCONNECT;break;
535 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
536 imp->imp_target_uuid.uuid, imp->imp_connect_op);
541 if (ptlrpc_import_in_recovery(imp)) {
542 struct l_wait_info lwi;
543 lwi = LWI_TIMEOUT_INTR(MAX(obd_timeout * HZ, 1), back_to_sleep,
545 rc = l_wait_event(imp->imp_recovery_waitq,
546 !ptlrpc_import_in_recovery(imp), &lwi);
550 spin_lock_irqsave(&imp->imp_lock, flags);
551 if (imp->imp_state != LUSTRE_IMP_FULL) {
554 spin_unlock_irqrestore(&imp->imp_lock, flags);
556 request = ptlrpc_prep_req(imp, rq_opc, 0, NULL, NULL);
558 /* For non-replayable connections, don't attempt
559 reconnect if this fails */
560 if (!imp->imp_replayable) {
561 request->rq_no_resend = 1;
562 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
563 request->rq_send_state = LUSTRE_IMP_CONNECTING;
565 request->rq_replen = lustre_msg_size(0, NULL);
566 rc = ptlrpc_queue_wait(request);
567 ptlrpc_req_finished(request);
570 spin_lock_irqsave(&imp->imp_lock, flags);
572 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
573 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
574 spin_unlock_irqrestore(&imp->imp_lock, flags);