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 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
104 spin_unlock_irqrestore(&imp->imp_lock, flags);
105 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
108 spin_unlock_irqrestore(&imp->imp_lock, flags);
109 CDEBUG(D_HA, "%p %s: import already not connected: %s\n",
110 imp,imp->imp_client->cli_name,
111 ptlrpc_import_state_name(imp->imp_state));
118 * This acts as a barrier; all existing requests are rejected, and
119 * no new requests will be accepted until the import is valid again.
121 void ptlrpc_deactivate_import(struct obd_import *imp)
126 spin_lock_irqsave(&imp->imp_lock, flags);
127 CDEBUG(D_HA, "setting import %s INVALID\n",
128 imp->imp_target_uuid.uuid);
129 imp->imp_invalid = 1;
130 imp->imp_generation++;
131 spin_unlock_irqrestore(&imp->imp_lock, flags);
133 ptlrpc_abort_inflight(imp);
134 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
138 * This function will invalidate the import, if necessary, then block
139 * for all the RPC completions, and finally notify the obd to
140 * invalidate its state (ie cancel locks, clear pending requests,
143 * in_rpc: true if this is called while processing an rpc, like
144 * CONNECT. It will allow for one RPC to be inflight while
145 * waiting for requests to complete. Ugly, yes, but I don't see an
146 * cleaner way right now.
148 void ptlrpc_invalidate_import(struct obd_import *imp, int in_rpc)
150 struct l_wait_info lwi;
151 unsigned long timeout;
155 if (!imp->imp_invalid)
156 ptlrpc_deactivate_import(imp);
158 LASSERT(imp->imp_invalid);
163 /* wait for all requests to error out and call completion
165 if (imp->imp_server_timeout)
166 timeout = obd_timeout / 2;
168 timeout = obd_timeout;
169 timeout = MAX(timeout * HZ, 1);
170 lwi = LWI_TIMEOUT_INTR(timeout, NULL, NULL, NULL);
171 rc = l_wait_event(imp->imp_recovery_waitq,
172 (atomic_read(&imp->imp_inflight) == inflight),
176 CERROR("%s: rc = %d waiting for callback (%d != %d)\n",
177 imp->imp_target_uuid.uuid, rc,
178 atomic_read(&imp->imp_inflight), inflight);
180 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
183 void ptlrpc_activate_import(struct obd_import *imp)
185 struct obd_device *obd = imp->imp_obd;
188 spin_lock_irqsave(&imp->imp_lock, flags);
189 imp->imp_invalid = 0;
190 spin_unlock_irqrestore(&imp->imp_lock, flags);
192 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
195 void ptlrpc_fail_import(struct obd_import *imp, int generation)
199 LASSERT (!imp->imp_dlm_fake);
201 if (ptlrpc_set_import_discon(imp)) {
204 if (!imp->imp_replayable) {
205 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
206 "auto-deactivating\n",
207 imp->imp_target_uuid.uuid,
208 imp->imp_connection->c_remote_uuid.uuid,
209 imp->imp_obd->obd_name);
210 ptlrpc_deactivate_import(imp);
213 CDEBUG(D_HA, "%s: waking up pinger\n",
214 imp->imp_target_uuid.uuid);
216 spin_lock_irqsave(&imp->imp_lock, flags);
217 imp->imp_force_verify = 1;
218 spin_unlock_irqrestore(&imp->imp_lock, flags);
220 ptlrpc_pinger_wake_up();
225 int ptlrpc_connect_import(struct obd_import *imp, char * new_uuid)
227 struct obd_device *obd = imp->imp_obd;
228 int initial_connect = 0;
230 __u64 committed_before_reconnect = 0;
231 struct ptlrpc_request *request;
232 int size[] = {sizeof(imp->imp_target_uuid),
233 sizeof(obd->obd_uuid),
234 sizeof(imp->imp_dlm_handle)};
235 char *tmp[] = {imp->imp_target_uuid.uuid,
237 (char *)&imp->imp_dlm_handle};
238 struct ptlrpc_connect_async_args *aa;
241 spin_lock_irqsave(&imp->imp_lock, flags);
242 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
243 spin_unlock_irqrestore(&imp->imp_lock, flags);
244 CERROR("can't connect to a closed import\n");
246 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
247 spin_unlock_irqrestore(&imp->imp_lock, flags);
248 CERROR("already connected\n");
250 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
251 spin_unlock_irqrestore(&imp->imp_lock, flags);
252 CERROR("already connecting\n");
256 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
258 imp->imp_resend_replay = 0;
260 if (imp->imp_remote_handle.cookie == 0) {
263 committed_before_reconnect = imp->imp_peer_committed_transno;;
268 spin_unlock_irqrestore(&imp->imp_lock, flags);
271 struct ptlrpc_connection *conn;
272 struct obd_uuid uuid;
273 struct obd_export *dlmexp;
275 obd_str2uuid(&uuid, new_uuid);
277 conn = ptlrpc_uuid_to_connection(&uuid);
279 GOTO(out, rc = -ENOENT);
281 CDEBUG(D_HA, "switching import %s/%s from %s to %s\n",
282 imp->imp_target_uuid.uuid, imp->imp_obd->obd_name,
283 imp->imp_connection->c_remote_uuid.uuid,
284 conn->c_remote_uuid.uuid);
286 /* Switch the import's connection and the DLM export's
287 * connection (which are almost certainly the same, but we
288 * keep distinct refs just to make things clearer. I think. */
289 if (imp->imp_connection)
290 ptlrpc_put_connection(imp->imp_connection);
291 /* We hand off the ref from ptlrpc_get_connection. */
292 imp->imp_connection = conn;
294 dlmexp = class_conn2export(&imp->imp_dlm_handle);
296 LASSERT(dlmexp != NULL);
298 if (dlmexp->exp_connection)
299 ptlrpc_put_connection(dlmexp->exp_connection);
300 dlmexp->exp_connection = ptlrpc_connection_addref(conn);
301 class_export_put(dlmexp);
305 request = ptlrpc_prep_req(imp, imp->imp_connect_op, 3, size, tmp);
307 GOTO(out, rc = -ENOMEM);
310 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
313 request->rq_send_state = LUSTRE_IMP_CONNECTING;
314 request->rq_replen = lustre_msg_size(0, NULL);
315 request->rq_interpret_reply = ptlrpc_connect_interpret;
317 LASSERT (sizeof (*aa) <= sizeof (request->rq_async_args));
318 aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
319 memset(aa, 0, sizeof *aa);
321 aa->pcaa_peer_committed = committed_before_reconnect;
322 aa->pcaa_initial_connect = initial_connect;
324 if (aa->pcaa_initial_connect) {
325 lustre_msg_add_op_flags(request->rq_reqmsg,
326 MSG_CONNECT_INITIAL);
327 imp->imp_replayable = 1;
330 ptlrpcd_add_req(request);
332 imp->imp_connect_start = jiffies;
335 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
341 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
344 struct ptlrpc_connect_async_args *aa = data;
345 struct obd_import *imp = request->rq_import;
346 struct lustre_handle old_hdl;
351 spin_lock_irqsave(&imp->imp_lock, flags);
352 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
353 spin_unlock_irqrestore(&imp->imp_lock, flags);
356 spin_unlock_irqrestore(&imp->imp_lock, flags);
361 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
363 if (aa->pcaa_initial_connect) {
364 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
365 CDEBUG(D_HA, "connected to replayable target: %s\n",
366 imp->imp_target_uuid.uuid);
367 imp->imp_pingable = imp->imp_replayable = 1;
369 imp->imp_replayable = 0;
371 LASSERTF(imp->imp_conn_cnt < request->rq_repmsg->conn_cnt,
372 "imp conn_cnt %d req conn_cnt %d",
373 imp->imp_conn_cnt, request->rq_repmsg->conn_cnt);
374 imp->imp_conn_cnt = request->rq_repmsg->conn_cnt;
375 imp->imp_remote_handle = request->rq_repmsg->handle;
376 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
377 GOTO(finish, rc = 0);
380 /* Determine what recovery state to move the import to. */
381 if (MSG_CONNECT_RECONNECT & msg_flags) {
382 memset(&old_hdl, 0, sizeof(old_hdl));
383 if (!memcmp(&old_hdl, &request->rq_repmsg->handle,
385 CERROR("%s@%s didn't like our handle "LPX64
386 ", failed\n", imp->imp_target_uuid.uuid,
387 imp->imp_connection->c_remote_uuid.uuid,
388 imp->imp_dlm_handle.cookie);
389 GOTO(out, rc = -ENOTCONN);
392 if (memcmp(&imp->imp_remote_handle, &request->rq_repmsg->handle,
393 sizeof(imp->imp_remote_handle))) {
394 CERROR("%s@%s changed handle from "LPX64" to "LPX64
395 "; copying, but this may foreshadow disaster\n",
396 imp->imp_target_uuid.uuid,
397 imp->imp_connection->c_remote_uuid.uuid,
398 imp->imp_remote_handle.cookie,
399 request->rq_repmsg->handle.cookie);
400 imp->imp_remote_handle = request->rq_repmsg->handle;
402 CERROR("reconnected to %s@%s after partition\n",
403 imp->imp_target_uuid.uuid,
404 imp->imp_connection->c_remote_uuid.uuid);
407 if (imp->imp_invalid) {
408 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
409 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
410 imp->imp_resend_replay = 1;
411 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
413 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
415 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
416 LASSERT(imp->imp_replayable);
417 imp->imp_remote_handle = request->rq_repmsg->handle;
418 imp->imp_last_replay_transno = 0;
419 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
421 CWARN("oops! we get evicted from %s\n", imp->imp_target_uuid.uuid);
422 imp->imp_remote_handle = request->rq_repmsg->handle;
423 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
426 /* Sanity checks for a reconnected import. */
427 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
428 CERROR("imp_replayable flag does not match server "
429 "after reconnect. We should LBUG right here.\n");
432 if (request->rq_repmsg->last_committed < aa->pcaa_peer_committed) {
433 CERROR("%s went back in time (transno "LPD64
434 " was previously committed, server now claims "LPD64
435 ")! is shared storage not coherent?\n",
436 imp->imp_target_uuid.uuid,
437 aa->pcaa_peer_committed,
438 request->rq_repmsg->last_committed);
442 rc = ptlrpc_import_recovery_state_machine(imp);
444 if (rc == -ENOTCONN) {
445 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
446 "invalidating and reconnecting\n",
447 imp->imp_target_uuid.uuid,
448 imp->imp_connection->c_remote_uuid.uuid);
449 ptlrpc_connect_import(imp, NULL);
455 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
456 if (aa->pcaa_initial_connect && !imp->imp_initial_recov) {
457 ptlrpc_deactivate_import(imp);
459 if (rc == -ETIMEDOUT && (jiffies - imp->imp_connect_start) > HZ) {
460 CDEBUG(D_ERROR, "recovery of %s on %s failed (timeout)\n",
461 imp->imp_target_uuid.uuid,
462 (char *)imp->imp_connection->c_remote_uuid.uuid);
463 ptlrpc_connect_import(imp, NULL);
466 CDEBUG(D_ERROR, "recovery of %s on %s failed (%d)\n",
467 imp->imp_target_uuid.uuid,
468 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
471 wake_up(&imp->imp_recovery_waitq);
475 static int completed_replay_interpret(struct ptlrpc_request *req,
478 atomic_dec(&req->rq_import->imp_replay_inflight);
479 ptlrpc_import_recovery_state_machine(req->rq_import);
483 static int signal_completed_replay(struct obd_import *imp)
485 struct ptlrpc_request *req;
488 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
489 atomic_inc(&imp->imp_replay_inflight);
491 req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL, NULL);
495 req->rq_replen = lustre_msg_size(0, NULL);
496 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
497 req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
498 req->rq_timeout *= 3;
499 req->rq_interpret_reply = completed_replay_interpret;
501 ptlrpcd_add_req(req);
505 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
510 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
511 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
512 imp->imp_target_uuid.uuid,
513 imp->imp_connection->c_remote_uuid.uuid);
515 ptlrpc_invalidate_import(imp, 1);
517 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
520 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
521 CDEBUG(D_HA, "replay requested by %s\n",
522 imp->imp_target_uuid.uuid);
523 rc = ptlrpc_replay_next(imp, &inflight);
525 atomic_read(&imp->imp_replay_inflight) == 0) {
526 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
527 rc = ldlm_replay_locks(imp);
534 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
535 if (atomic_read(&imp->imp_replay_inflight) == 0) {
536 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
537 rc = signal_completed_replay(imp);
544 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
545 if (atomic_read(&imp->imp_replay_inflight) == 0) {
546 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
550 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
551 CDEBUG(D_HA, "reconnected to %s@%s\n",
552 imp->imp_target_uuid.uuid,
553 imp->imp_connection->c_remote_uuid.uuid);
555 rc = ptlrpc_resend(imp);
558 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
559 ptlrpc_activate_import(imp);
562 if (imp->imp_state == LUSTRE_IMP_FULL) {
563 wake_up(&imp->imp_recovery_waitq);
564 ptlrpc_wake_delayed(imp);
571 static int back_to_sleep(void *unused)
576 int ptlrpc_disconnect_import(struct obd_import *imp)
578 struct ptlrpc_request *request;
584 switch (imp->imp_connect_op) {
585 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
586 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
587 case MGMT_CONNECT:rq_opc = MGMT_DISCONNECT;break;
589 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
590 imp->imp_target_uuid.uuid, imp->imp_connect_op);
595 if (ptlrpc_import_in_recovery(imp)) {
596 struct l_wait_info lwi;
597 unsigned long timeout;
598 if (imp->imp_server_timeout)
599 timeout = obd_timeout / 2;
601 timeout = obd_timeout;
602 timeout = MAX(timeout * HZ, 1);
603 lwi = LWI_TIMEOUT_INTR(obd_timeout, back_to_sleep, NULL, NULL);
604 rc = l_wait_event(imp->imp_recovery_waitq,
605 !ptlrpc_import_in_recovery(imp), &lwi);
609 spin_lock_irqsave(&imp->imp_lock, flags);
610 if (imp->imp_state != LUSTRE_IMP_FULL) {
613 spin_unlock_irqrestore(&imp->imp_lock, flags);
615 request = ptlrpc_prep_req(imp, rq_opc, 0, NULL, NULL);
617 /* For non-replayable connections, don't attempt
618 reconnect if this fails */
619 if (!imp->imp_replayable) {
620 request->rq_no_resend = 1;
621 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
622 request->rq_send_state = LUSTRE_IMP_CONNECTING;
624 request->rq_replen = lustre_msg_size(0, NULL);
625 rc = ptlrpc_queue_wait(request);
626 ptlrpc_req_finished(request);
629 spin_lock_irqsave(&imp->imp_lock, flags);
631 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
632 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
633 spin_unlock_irqrestore(&imp->imp_lock, flags);