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;
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, imp->imp_target_uuid.uuid, \
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 unsigned long flags; \
63 spin_lock_irqsave(&imp->imp_lock, flags); \
64 IMPORT_SET_STATE_NOLOCK(imp, state); \
65 spin_unlock_irqrestore(&imp->imp_lock, flags); \
69 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
71 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
73 /* Only this function is allowed to change the import state when it is
74 * CLOSED. I would rather refcount the import and free it after
75 * disconnection like we do with exports. To do that, the client_obd
76 * will need to save the peer info somewhere other than in the import,
78 int ptlrpc_init_import(struct obd_import *imp)
82 spin_lock_irqsave(&imp->imp_lock, flags);
84 imp->imp_generation++;
85 imp->imp_state = LUSTRE_IMP_NEW;
87 spin_unlock_irqrestore(&imp->imp_lock, flags);
92 /* Returns true if import was FULL, false if import was already not
95 int ptlrpc_set_import_discon(struct obd_import *imp)
100 spin_lock_irqsave(&imp->imp_lock, flags);
102 if (imp->imp_state == LUSTRE_IMP_FULL) {
103 CERROR("%s: connection lost to %s@%s\n",
104 imp->imp_obd->obd_name,
105 imp->imp_target_uuid.uuid,
106 imp->imp_connection->c_remote_uuid.uuid);
107 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
108 spin_unlock_irqrestore(&imp->imp_lock, flags);
109 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
112 spin_unlock_irqrestore(&imp->imp_lock, flags);
113 CDEBUG(D_HA, "%p %s: import already not connected: %s\n",
114 imp,imp->imp_client->cli_name,
115 ptlrpc_import_state_name(imp->imp_state));
122 * This acts as a barrier; all existing requests are rejected, and
123 * no new requests will be accepted until the import is valid again.
125 void ptlrpc_deactivate_import(struct obd_import *imp)
130 spin_lock_irqsave(&imp->imp_lock, flags);
131 CDEBUG(D_HA, "setting import %s INVALID\n",
132 imp->imp_target_uuid.uuid);
133 imp->imp_invalid = 1;
134 imp->imp_generation++;
135 spin_unlock_irqrestore(&imp->imp_lock, flags);
137 ptlrpc_abort_inflight(imp);
138 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
142 * This function will invalidate the import, if necessary, then block
143 * for all the RPC completions, and finally notify the obd to
144 * invalidate its state (ie cancel locks, clear pending requests,
147 * in_rpc: true if this is called while processing an rpc, like
148 * CONNECT. It will allow for one RPC to be inflight while
149 * waiting for requests to complete. Ugly, yes, but I don't see an
150 * cleaner way right now.
152 void ptlrpc_invalidate_import(struct obd_import *imp, int in_rpc)
154 struct l_wait_info lwi;
155 unsigned long timeout;
159 if (!imp->imp_invalid)
160 ptlrpc_deactivate_import(imp);
162 LASSERT(imp->imp_invalid);
167 /* wait for all requests to error out and call completion
169 if (imp->imp_server_timeout)
170 timeout = obd_timeout / 2;
172 timeout = obd_timeout;
173 timeout = MAX(timeout * HZ, 1);
174 lwi = LWI_TIMEOUT_INTR(timeout, NULL, NULL, NULL);
175 rc = l_wait_event(imp->imp_recovery_waitq,
176 (atomic_read(&imp->imp_inflight) == inflight),
180 CERROR("%s: rc = %d waiting for callback (%d != %d)\n",
181 imp->imp_target_uuid.uuid, rc,
182 atomic_read(&imp->imp_inflight), inflight);
184 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
187 void ptlrpc_activate_import(struct obd_import *imp)
189 struct obd_device *obd = imp->imp_obd;
192 spin_lock_irqsave(&imp->imp_lock, flags);
193 imp->imp_invalid = 0;
194 spin_unlock_irqrestore(&imp->imp_lock, flags);
196 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
199 void ptlrpc_fail_import(struct obd_import *imp, int generation)
203 LASSERT (!imp->imp_dlm_fake);
205 if (ptlrpc_set_import_discon(imp)) {
208 if (!imp->imp_replayable) {
209 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
210 "auto-deactivating\n",
211 imp->imp_target_uuid.uuid,
212 imp->imp_connection->c_remote_uuid.uuid,
213 imp->imp_obd->obd_name);
214 ptlrpc_deactivate_import(imp);
217 CDEBUG(D_HA, "%s: waking up pinger\n",
218 imp->imp_target_uuid.uuid);
220 spin_lock_irqsave(&imp->imp_lock, flags);
221 imp->imp_force_verify = 1;
222 spin_unlock_irqrestore(&imp->imp_lock, flags);
224 ptlrpc_pinger_wake_up();
229 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid)
231 struct obd_device *obd = imp->imp_obd;
232 int initial_connect = 0;
234 __u64 committed_before_reconnect = 0;
235 struct ptlrpc_request *request;
236 int size[] = {sizeof(imp->imp_target_uuid),
237 sizeof(obd->obd_uuid),
238 sizeof(imp->imp_dlm_handle),
239 sizeof(unsigned long)};
240 char *tmp[] = {imp->imp_target_uuid.uuid,
242 (char *)&imp->imp_dlm_handle,
243 (char *)&imp->imp_connect_flags}; /* XXX: make this portable! */
244 struct ptlrpc_connect_async_args *aa;
247 spin_lock_irqsave(&imp->imp_lock, flags);
248 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
249 spin_unlock_irqrestore(&imp->imp_lock, flags);
250 CERROR("can't connect to a closed import\n");
252 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
253 spin_unlock_irqrestore(&imp->imp_lock, flags);
254 CERROR("already connected\n");
256 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
257 spin_unlock_irqrestore(&imp->imp_lock, flags);
258 CERROR("already connecting\n");
262 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
264 imp->imp_resend_replay = 0;
266 if (imp->imp_remote_handle.cookie == 0) {
269 committed_before_reconnect = imp->imp_peer_committed_transno;;
274 spin_unlock_irqrestore(&imp->imp_lock, flags);
277 struct ptlrpc_connection *conn;
278 struct obd_uuid uuid;
279 struct obd_export *dlmexp;
281 obd_str2uuid(&uuid, new_uuid);
283 conn = ptlrpc_uuid_to_connection(&uuid);
285 GOTO(out, rc = -ENOENT);
287 CDEBUG(D_HA, "switching import %s/%s from %s to %s\n",
288 imp->imp_target_uuid.uuid, imp->imp_obd->obd_name,
289 imp->imp_connection->c_remote_uuid.uuid,
290 conn->c_remote_uuid.uuid);
292 /* Switch the import's connection and the DLM export's
293 * connection (which are almost certainly the same, but we
294 * keep distinct refs just to make things clearer. I think. */
295 if (imp->imp_connection)
296 ptlrpc_put_connection(imp->imp_connection);
297 /* We hand off the ref from ptlrpc_get_connection. */
298 imp->imp_connection = conn;
300 dlmexp = class_conn2export(&imp->imp_dlm_handle);
302 LASSERT(dlmexp != NULL);
304 if (dlmexp->exp_connection)
305 ptlrpc_put_connection(dlmexp->exp_connection);
306 dlmexp->exp_connection = ptlrpc_connection_addref(conn);
307 class_export_put(dlmexp);
311 request = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION,
312 imp->imp_connect_op, 4, size, tmp);
314 GOTO(out, rc = -ENOMEM);
317 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
320 request->rq_send_state = LUSTRE_IMP_CONNECTING;
321 request->rq_replen = lustre_msg_size(0, NULL);
322 request->rq_interpret_reply = ptlrpc_connect_interpret;
324 LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
325 aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
326 memset(aa, 0, sizeof *aa);
328 aa->pcaa_peer_committed = committed_before_reconnect;
329 aa->pcaa_initial_connect = initial_connect;
331 if (aa->pcaa_initial_connect) {
332 lustre_msg_add_op_flags(request->rq_reqmsg,
333 MSG_CONNECT_INITIAL);
334 imp->imp_replayable = 1;
337 ptlrpcd_add_req(request);
339 imp->imp_connect_start = jiffies;
342 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
348 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
351 struct ptlrpc_connect_async_args *aa = data;
352 struct obd_import *imp = request->rq_import;
353 struct lustre_handle old_hdl;
358 spin_lock_irqsave(&imp->imp_lock, flags);
359 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
360 spin_unlock_irqrestore(&imp->imp_lock, flags);
363 spin_unlock_irqrestore(&imp->imp_lock, flags);
368 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
370 if (aa->pcaa_initial_connect) {
371 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
372 CDEBUG(D_HA, "connected to replayable target: %s\n",
373 imp->imp_target_uuid.uuid);
374 imp->imp_pingable = imp->imp_replayable = 1;
376 imp->imp_replayable = 0;
378 LASSERTF(imp->imp_conn_cnt < request->rq_repmsg->conn_cnt,
379 "imp conn_cnt %d req conn_cnt %d",
380 imp->imp_conn_cnt, request->rq_repmsg->conn_cnt);
381 imp->imp_conn_cnt = request->rq_repmsg->conn_cnt;
382 imp->imp_remote_handle = request->rq_repmsg->handle;
383 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
384 GOTO(finish, rc = 0);
387 /* Determine what recovery state to move the import to. */
388 if (MSG_CONNECT_RECONNECT & msg_flags) {
389 memset(&old_hdl, 0, sizeof(old_hdl));
390 if (!memcmp(&old_hdl, &request->rq_repmsg->handle,
392 CERROR("%s@%s didn't like our handle "LPX64
393 ", failed\n", imp->imp_target_uuid.uuid,
394 imp->imp_connection->c_remote_uuid.uuid,
395 imp->imp_dlm_handle.cookie);
396 GOTO(out, rc = -ENOTCONN);
399 if (memcmp(&imp->imp_remote_handle, &request->rq_repmsg->handle,
400 sizeof(imp->imp_remote_handle))) {
401 CERROR("%s@%s changed handle from "LPX64" to "LPX64
402 "; copying, but this may foreshadow disaster\n",
403 imp->imp_target_uuid.uuid,
404 imp->imp_connection->c_remote_uuid.uuid,
405 imp->imp_remote_handle.cookie,
406 request->rq_repmsg->handle.cookie);
407 imp->imp_remote_handle = request->rq_repmsg->handle;
409 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
410 imp->imp_target_uuid.uuid,
411 imp->imp_connection->c_remote_uuid.uuid);
414 if (imp->imp_invalid) {
415 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
416 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
417 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
418 imp->imp_obd->obd_name,
419 imp->imp_target_uuid.uuid);
420 imp->imp_resend_replay = 1;
421 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
423 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
425 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
426 LASSERT(imp->imp_replayable);
427 imp->imp_remote_handle = request->rq_repmsg->handle;
428 imp->imp_last_replay_transno = 0;
429 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
431 CDEBUG(D_HA, "oops! we get evicted from %s\n", imp->imp_target_uuid.uuid);
432 imp->imp_remote_handle = request->rq_repmsg->handle;
433 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
436 /* Sanity checks for a reconnected import. */
437 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
438 CERROR("imp_replayable flag does not match server "
439 "after reconnect. We should LBUG right here.\n");
442 if (request->rq_repmsg->last_committed < aa->pcaa_peer_committed) {
443 CERROR("%s went back in time (transno "LPD64
444 " was previously committed, server now claims "LPD64
445 ")! is shared storage not coherent?\n",
446 imp->imp_target_uuid.uuid,
447 aa->pcaa_peer_committed,
448 request->rq_repmsg->last_committed);
452 rc = ptlrpc_import_recovery_state_machine(imp);
454 if (rc == -ENOTCONN) {
455 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
456 "invalidating and reconnecting\n",
457 imp->imp_target_uuid.uuid,
458 imp->imp_connection->c_remote_uuid.uuid);
459 ptlrpc_connect_import(imp, NULL);
465 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
466 if (aa->pcaa_initial_connect && !imp->imp_initial_recov) {
467 ptlrpc_deactivate_import(imp);
469 if (rc == -ETIMEDOUT && (jiffies - imp->imp_connect_start) > HZ) {
470 CDEBUG(D_ERROR, "recovery of %s on %s failed (timeout)\n",
471 imp->imp_target_uuid.uuid,
472 (char *)imp->imp_connection->c_remote_uuid.uuid);
473 ptlrpc_connect_import(imp, NULL);
476 CDEBUG(D_ERROR, "recovery of %s on %s failed (%d)\n",
477 imp->imp_target_uuid.uuid,
478 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
481 wake_up(&imp->imp_recovery_waitq);
485 static int completed_replay_interpret(struct ptlrpc_request *req,
488 atomic_dec(&req->rq_import->imp_replay_inflight);
489 if (req->rq_status == 0) {
490 ptlrpc_import_recovery_state_machine(req->rq_import);
492 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
494 req->rq_import->imp_obd->obd_name, req->rq_status);
495 ptlrpc_connect_import(req->rq_import, NULL);
501 static int signal_completed_replay(struct obd_import *imp)
503 struct ptlrpc_request *req;
506 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
507 atomic_inc(&imp->imp_replay_inflight);
509 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 0, NULL, NULL);
513 req->rq_replen = lustre_msg_size(0, NULL);
514 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
515 req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
516 req->rq_timeout *= 3;
517 req->rq_interpret_reply = completed_replay_interpret;
519 ptlrpcd_add_req(req);
523 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
528 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
529 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
530 imp->imp_target_uuid.uuid,
531 imp->imp_connection->c_remote_uuid.uuid);
533 ptlrpc_invalidate_import(imp, 1);
535 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
538 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
539 CDEBUG(D_HA, "replay requested by %s\n",
540 imp->imp_target_uuid.uuid);
541 rc = ptlrpc_replay_next(imp, &inflight);
543 atomic_read(&imp->imp_replay_inflight) == 0) {
544 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
545 rc = ldlm_replay_locks(imp);
552 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
553 if (atomic_read(&imp->imp_replay_inflight) == 0) {
554 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
555 rc = signal_completed_replay(imp);
562 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
563 if (atomic_read(&imp->imp_replay_inflight) == 0) {
564 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
568 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
569 CDEBUG(D_HA, "reconnected to %s@%s\n",
570 imp->imp_target_uuid.uuid,
571 imp->imp_connection->c_remote_uuid.uuid);
573 rc = ptlrpc_resend(imp);
576 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
577 ptlrpc_activate_import(imp);
578 CERROR("%s: connection restored to %s@%s\n",
579 imp->imp_obd->obd_name,
580 imp->imp_target_uuid.uuid,
581 imp->imp_connection->c_remote_uuid.uuid);
584 if (imp->imp_state == LUSTRE_IMP_FULL) {
585 wake_up(&imp->imp_recovery_waitq);
586 ptlrpc_wake_delayed(imp);
593 static int back_to_sleep(void *unused)
598 int ptlrpc_disconnect_import(struct obd_import *imp)
600 struct ptlrpc_request *request;
606 switch (imp->imp_connect_op) {
607 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
608 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
609 case MGMT_CONNECT:rq_opc = MGMT_DISCONNECT;break;
611 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
612 imp->imp_target_uuid.uuid, imp->imp_connect_op);
617 if (ptlrpc_import_in_recovery(imp)) {
618 struct l_wait_info lwi;
619 unsigned long timeout;
620 if (imp->imp_server_timeout)
621 timeout = obd_timeout / 2;
623 timeout = obd_timeout;
624 timeout = MAX(timeout * HZ, 1);
625 lwi = LWI_TIMEOUT_INTR(obd_timeout, back_to_sleep, NULL, NULL);
626 rc = l_wait_event(imp->imp_recovery_waitq,
627 !ptlrpc_import_in_recovery(imp), &lwi);
631 spin_lock_irqsave(&imp->imp_lock, flags);
632 if (imp->imp_state != LUSTRE_IMP_FULL) {
635 spin_unlock_irqrestore(&imp->imp_lock, flags);
637 request = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, rq_opc,
640 /* For non-replayable connections, don't attempt
641 reconnect if this fails */
642 if (!imp->imp_replayable) {
643 request->rq_no_resend = 1;
644 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
645 request->rq_send_state = LUSTRE_IMP_CONNECTING;
647 request->rq_replen = lustre_msg_size(0, NULL);
648 rc = ptlrpc_queue_wait(request);
649 ptlrpc_req_finished(request);
652 spin_lock_irqsave(&imp->imp_lock, flags);
654 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
655 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
656 imp->imp_conn_cnt = 0;
657 spin_unlock_irqrestore(&imp->imp_lock, flags);