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 #define ATTEMPT_TOO_SOON(last) \
230 ((last) && ((long)(jiffies - (last)) <= (long)(obd_timeout * 2 * HZ)))
232 static int import_select_connection(struct obd_import *imp)
234 struct obd_import_conn *imp_conn, *tmp;
235 struct obd_export *dlmexp;
239 spin_lock(&imp->imp_lock);
241 if (list_empty(&imp->imp_conn_list)) {
242 CERROR("no available connections on imp %p@%s\n",
243 imp, imp->imp_obd->obd_name);
244 spin_unlock(&imp->imp_lock);
248 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
249 if (!ATTEMPT_TOO_SOON(imp_conn->oic_last_attempt)) {
255 /* if not found, simply choose the current one */
257 CWARN("obd %s imp 0x%p: all connections have been "
258 "tried recently\n", imp->imp_obd->obd_name, imp);
259 LASSERT(imp->imp_conn_current);
260 imp_conn = imp->imp_conn_current;
262 LASSERT(imp_conn->oic_conn);
264 imp_conn->oic_last_attempt = jiffies;
266 /* move the items ahead of the selected one to list tail */
268 tmp= list_entry(imp->imp_conn_list.next,
269 struct obd_import_conn, oic_item);
272 list_del(&tmp->oic_item);
273 list_add_tail(&tmp->oic_item, &imp->imp_conn_list);
276 /* switch connection, don't mind if it's same as the current one */
277 if (imp->imp_connection)
278 ptlrpc_put_connection(imp->imp_connection);
279 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
281 dlmexp = class_conn2export(&imp->imp_dlm_handle);
282 LASSERT(dlmexp != NULL);
283 if (dlmexp->exp_connection)
284 ptlrpc_put_connection(imp->imp_connection);
285 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
286 class_export_put(dlmexp);
288 imp->imp_conn_current = imp_conn;
289 CWARN("obd %s imp 0x%p: select conn %s\n",
290 imp->imp_obd->obd_name, imp,
291 imp_conn->oic_uuid.uuid);
292 spin_unlock(&imp->imp_lock);
299 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid)
301 struct obd_device *obd = imp->imp_obd;
302 int initial_connect = 0;
304 __u64 committed_before_reconnect = 0;
305 struct ptlrpc_request *request;
306 int size[] = {sizeof(imp->imp_target_uuid),
307 sizeof(obd->obd_uuid),
308 sizeof(imp->imp_dlm_handle),
309 sizeof(unsigned long)};
310 char *tmp[] = {imp->imp_target_uuid.uuid,
312 (char *)&imp->imp_dlm_handle,
313 (char *)&imp->imp_connect_flags}; /* XXX: make this portable! */
314 struct ptlrpc_connect_async_args *aa;
317 spin_lock_irqsave(&imp->imp_lock, flags);
318 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
319 spin_unlock_irqrestore(&imp->imp_lock, flags);
320 CERROR("can't connect to a closed import\n");
322 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
323 spin_unlock_irqrestore(&imp->imp_lock, flags);
324 CERROR("already connected\n");
326 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
327 spin_unlock_irqrestore(&imp->imp_lock, flags);
328 CERROR("already connecting\n");
332 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
334 imp->imp_resend_replay = 0;
336 if (imp->imp_remote_handle.cookie == 0) {
339 committed_before_reconnect = imp->imp_peer_committed_transno;;
344 spin_unlock_irqrestore(&imp->imp_lock, flags);
347 struct obd_uuid uuid;
349 obd_str2uuid(&uuid, new_uuid);
351 rc = import_set_conn_priority(imp, &uuid);
355 rc = import_select_connection(imp);
359 request = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION,
360 imp->imp_connect_op, 4, size, tmp);
362 GOTO(out, rc = -ENOMEM);
365 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
368 request->rq_send_state = LUSTRE_IMP_CONNECTING;
369 request->rq_replen = lustre_msg_size(0, NULL);
370 request->rq_interpret_reply = ptlrpc_connect_interpret;
372 LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
373 aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
374 memset(aa, 0, sizeof *aa);
376 aa->pcaa_peer_committed = committed_before_reconnect;
377 aa->pcaa_initial_connect = initial_connect;
379 if (aa->pcaa_initial_connect) {
380 lustre_msg_add_op_flags(request->rq_reqmsg,
381 MSG_CONNECT_INITIAL);
382 imp->imp_replayable = 1;
385 ptlrpcd_add_req(request);
387 imp->imp_connect_start = jiffies;
390 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
396 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
399 struct ptlrpc_connect_async_args *aa = data;
400 struct obd_import *imp = request->rq_import;
401 struct lustre_handle old_hdl;
406 spin_lock_irqsave(&imp->imp_lock, flags);
407 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
408 spin_unlock_irqrestore(&imp->imp_lock, flags);
411 spin_unlock_irqrestore(&imp->imp_lock, flags);
415 LASSERT(imp->imp_conn_current);
416 imp->imp_conn_current->oic_last_attempt = 0;
418 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
420 if (aa->pcaa_initial_connect) {
421 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
422 CDEBUG(D_HA, "connected to replayable target: %s\n",
423 imp->imp_target_uuid.uuid);
424 imp->imp_pingable = imp->imp_replayable = 1;
426 imp->imp_replayable = 0;
428 LASSERTF(imp->imp_conn_cnt < request->rq_repmsg->conn_cnt,
429 "imp conn_cnt %d req conn_cnt %d",
430 imp->imp_conn_cnt, request->rq_repmsg->conn_cnt);
431 imp->imp_conn_cnt = request->rq_repmsg->conn_cnt;
432 imp->imp_remote_handle = request->rq_repmsg->handle;
433 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
434 GOTO(finish, rc = 0);
437 /* Determine what recovery state to move the import to. */
438 if (MSG_CONNECT_RECONNECT & msg_flags) {
439 memset(&old_hdl, 0, sizeof(old_hdl));
440 if (!memcmp(&old_hdl, &request->rq_repmsg->handle,
442 CERROR("%s@%s didn't like our handle "LPX64
443 ", failed\n", imp->imp_target_uuid.uuid,
444 imp->imp_connection->c_remote_uuid.uuid,
445 imp->imp_dlm_handle.cookie);
446 GOTO(out, rc = -ENOTCONN);
449 if (memcmp(&imp->imp_remote_handle, &request->rq_repmsg->handle,
450 sizeof(imp->imp_remote_handle))) {
451 CERROR("%s@%s changed handle from "LPX64" to "LPX64
452 "; copying, but this may foreshadow disaster\n",
453 imp->imp_target_uuid.uuid,
454 imp->imp_connection->c_remote_uuid.uuid,
455 imp->imp_remote_handle.cookie,
456 request->rq_repmsg->handle.cookie);
457 imp->imp_remote_handle = request->rq_repmsg->handle;
459 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
460 imp->imp_target_uuid.uuid,
461 imp->imp_connection->c_remote_uuid.uuid);
464 if (imp->imp_invalid) {
465 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
466 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
467 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
468 imp->imp_obd->obd_name,
469 imp->imp_target_uuid.uuid);
470 imp->imp_resend_replay = 1;
471 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
473 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
475 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
476 LASSERT(imp->imp_replayable);
477 imp->imp_remote_handle = request->rq_repmsg->handle;
478 imp->imp_last_replay_transno = 0;
479 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
481 CDEBUG(D_HA, "oops! we get evicted from %s\n", imp->imp_target_uuid.uuid);
482 imp->imp_remote_handle = request->rq_repmsg->handle;
483 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
486 /* Sanity checks for a reconnected import. */
487 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
488 CERROR("imp_replayable flag does not match server "
489 "after reconnect. We should LBUG right here.\n");
492 if (request->rq_repmsg->last_committed < aa->pcaa_peer_committed) {
493 CERROR("%s went back in time (transno "LPD64
494 " was previously committed, server now claims "LPD64
495 ")! is shared storage not coherent?\n",
496 imp->imp_target_uuid.uuid,
497 aa->pcaa_peer_committed,
498 request->rq_repmsg->last_committed);
502 rc = ptlrpc_import_recovery_state_machine(imp);
504 if (rc == -ENOTCONN) {
505 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
506 "invalidating and reconnecting\n",
507 imp->imp_target_uuid.uuid,
508 imp->imp_connection->c_remote_uuid.uuid);
509 ptlrpc_connect_import(imp, NULL);
515 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
516 if (aa->pcaa_initial_connect && !imp->imp_initial_recov)
517 ptlrpc_deactivate_import(imp);
518 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
519 imp->imp_target_uuid.uuid,
520 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
523 wake_up(&imp->imp_recovery_waitq);
527 static int completed_replay_interpret(struct ptlrpc_request *req,
530 atomic_dec(&req->rq_import->imp_replay_inflight);
531 if (req->rq_status == 0) {
532 ptlrpc_import_recovery_state_machine(req->rq_import);
534 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
536 req->rq_import->imp_obd->obd_name, req->rq_status);
537 ptlrpc_connect_import(req->rq_import, NULL);
543 static int signal_completed_replay(struct obd_import *imp)
545 struct ptlrpc_request *req;
548 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
549 atomic_inc(&imp->imp_replay_inflight);
551 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 0, NULL, NULL);
555 req->rq_replen = lustre_msg_size(0, NULL);
556 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
557 req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
558 req->rq_timeout *= 3;
559 req->rq_interpret_reply = completed_replay_interpret;
561 ptlrpcd_add_req(req);
565 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
570 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
571 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
572 imp->imp_target_uuid.uuid,
573 imp->imp_connection->c_remote_uuid.uuid);
575 ptlrpc_invalidate_import(imp, 1);
577 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
580 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
581 CDEBUG(D_HA, "replay requested by %s\n",
582 imp->imp_target_uuid.uuid);
583 rc = ptlrpc_replay_next(imp, &inflight);
585 atomic_read(&imp->imp_replay_inflight) == 0) {
586 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
587 rc = ldlm_replay_locks(imp);
594 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
595 if (atomic_read(&imp->imp_replay_inflight) == 0) {
596 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
597 rc = signal_completed_replay(imp);
604 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
605 if (atomic_read(&imp->imp_replay_inflight) == 0) {
606 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
610 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
611 CDEBUG(D_HA, "reconnected to %s@%s\n",
612 imp->imp_target_uuid.uuid,
613 imp->imp_connection->c_remote_uuid.uuid);
615 rc = ptlrpc_resend(imp);
618 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
619 ptlrpc_activate_import(imp);
620 CERROR("%s: connection restored to %s@%s\n",
621 imp->imp_obd->obd_name,
622 imp->imp_target_uuid.uuid,
623 imp->imp_connection->c_remote_uuid.uuid);
626 if (imp->imp_state == LUSTRE_IMP_FULL) {
627 wake_up(&imp->imp_recovery_waitq);
628 ptlrpc_wake_delayed(imp);
635 static int back_to_sleep(void *unused)
640 int ptlrpc_disconnect_import(struct obd_import *imp)
642 struct ptlrpc_request *request;
648 switch (imp->imp_connect_op) {
649 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
650 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
651 case MGMT_CONNECT: rq_opc = MGMT_DISCONNECT; break;
653 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
654 imp->imp_target_uuid.uuid, imp->imp_connect_op);
659 if (ptlrpc_import_in_recovery(imp)) {
660 struct l_wait_info lwi;
661 unsigned long timeout;
662 if (imp->imp_server_timeout)
663 timeout = obd_timeout / 2;
665 timeout = obd_timeout;
666 timeout = MAX(timeout * HZ, 1);
667 lwi = LWI_TIMEOUT_INTR(obd_timeout, back_to_sleep, NULL, NULL);
668 rc = l_wait_event(imp->imp_recovery_waitq,
669 !ptlrpc_import_in_recovery(imp), &lwi);
673 spin_lock_irqsave(&imp->imp_lock, flags);
674 if (imp->imp_state != LUSTRE_IMP_FULL) {
677 spin_unlock_irqrestore(&imp->imp_lock, flags);
679 request = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, rq_opc,
682 /* For non-replayable connections, don't attempt
683 reconnect if this fails */
684 if (!imp->imp_replayable) {
685 request->rq_no_resend = 1;
686 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
687 request->rq_send_state = LUSTRE_IMP_CONNECTING;
689 request->rq_replen = lustre_msg_size(0, NULL);
690 rc = ptlrpc_queue_wait(request);
691 ptlrpc_req_finished(request);
694 spin_lock_irqsave(&imp->imp_lock, flags);
696 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
697 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
698 imp->imp_conn_cnt = 0;
699 spin_unlock_irqrestore(&imp->imp_lock, flags);