1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Portal-RPC reconnection and replay operations, for use in recovery.
6 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7 * Author: Mike Shaver <shaver@clusterfs.com>
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DEBUG_SUBSYSTEM S_RPC
27 # include <linux/config.h>
28 # include <linux/module.h>
29 # include <linux/kmod.h>
31 # include <liblustre.h>
34 #include <linux/obd_support.h>
35 #include <linux/lustre_ha.h>
36 #include <linux/lustre_net.h>
37 #include <linux/lustre_import.h>
38 #include <linux/lustre_export.h>
39 #include <linux/obd.h>
40 #include <linux/obd_class.h>
41 #include <linux/obd_lov.h> /* for IOC_LOV_SET_OSC_ACTIVE */
43 #include "ptlrpc_internal.h"
45 enum reconnect_result {
46 RECON_RESULT_RECOVERING = 1,
47 RECON_RESULT_RECONNECTED = 2,
48 RECON_RESULT_EVICTED = 3,
51 int ptlrpc_reconnect_import(struct obd_import *imp)
53 struct obd_device *obd = imp->imp_obd;
54 int rc, size[] = {sizeof(imp->imp_target_uuid),
55 sizeof(obd->obd_uuid),
56 sizeof(imp->imp_dlm_handle)};
57 char *tmp[] = {imp->imp_target_uuid.uuid,
59 (char *)&imp->imp_dlm_handle};
60 struct ptlrpc_connection *conn = imp->imp_connection;
61 struct ptlrpc_request *req;
62 struct lustre_handle old_hdl;
63 __u64 committed_before_reconnect = imp->imp_peer_committed_transno;
65 CERROR("reconnect handle "LPX64"\n",
66 imp->imp_dlm_handle.cookie);
68 req = ptlrpc_prep_req(imp, imp->imp_connect_op, 3, size, tmp);
71 req->rq_level = LUSTRE_CONN_NEW;
72 req->rq_replen = lustre_msg_size(0, NULL);
73 rc = ptlrpc_queue_wait(req);
75 /* what if rc > 0 ??*/
76 CERROR("cannot connect to %s@%s: rc = %d\n",
77 imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid, rc);
81 if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT) {
82 memset(&old_hdl, 0, sizeof(old_hdl));
83 if (!memcmp(&old_hdl, &req->rq_repmsg->handle,
85 CERROR("%s@%s didn't like our handle "LPX64
86 ", failed\n", imp->imp_target_uuid.uuid,
87 conn->c_remote_uuid.uuid,
88 imp->imp_dlm_handle.cookie);
89 GOTO(out_disc, rc = -ENOTCONN);
92 if (memcmp(&imp->imp_remote_handle, &req->rq_repmsg->handle,
93 sizeof(imp->imp_remote_handle))) {
94 CERROR("%s@%s changed handle from "LPX64" to "LPX64
95 "; copying, but this may foreshadow disaster\n",
96 imp->imp_target_uuid.uuid,
97 conn->c_remote_uuid.uuid,
98 imp->imp_remote_handle.cookie,
99 req->rq_repmsg->handle.cookie);
100 imp->imp_remote_handle = req->rq_repmsg->handle;
101 GOTO(out_disc, rc = RECON_RESULT_RECONNECTED);
104 CERROR("reconnected to %s@%s after partition\n",
105 imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid);
106 GOTO(out_disc, rc = RECON_RESULT_RECONNECTED);
107 } else if (lustre_msg_get_op_flags(req->rq_repmsg) &
108 MSG_CONNECT_RECOVERING) {
109 rc = RECON_RESULT_RECOVERING;
111 rc = RECON_RESULT_EVICTED;
114 old_hdl = imp->imp_remote_handle;
115 imp->imp_remote_handle = req->rq_repmsg->handle;
116 CERROR("reconnected to %s@%s ("LPX64", was "LPX64")!\n",
117 imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid,
118 imp->imp_remote_handle.cookie, old_hdl.cookie);
119 if (req->rq_repmsg->last_committed < committed_before_reconnect) {
120 CERROR("%s went back in time (transno "LPD64
121 " was committed, server claims "LPD64
122 ")! is shared storage not coherent?\n",
123 imp->imp_target_uuid.uuid,
124 imp->imp_peer_committed_transno,
125 req->rq_repmsg->last_committed);
131 ptlrpc_req_finished(req);
135 void ptlrpc_run_recovery_over_upcall(struct obd_device *obd)
142 argv[0] = obd_lustre_upcall;
143 argv[1] = "RECOVERY_OVER";
144 argv[2] = obd->obd_uuid.uuid;
148 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
151 rc = USERMODEHELPER(argv[0], argv, envp);
153 CERROR("Error invoking recovery upcall %s %s %s: %d; check "
154 "/proc/sys/lustre/upcall\n",
155 argv[0], argv[1], argv[2], rc);
158 CERROR("Invoked upcall %s %s %s",
159 argv[0], argv[1], argv[2]);
163 void ptlrpc_run_failed_import_upcall(struct obd_import* imp)
170 argv[0] = obd_lustre_upcall;
171 argv[1] = "FAILED_IMPORT";
172 argv[2] = imp->imp_target_uuid.uuid;
173 argv[3] = imp->imp_obd->obd_name;
174 argv[4] = imp->imp_connection->c_remote_uuid.uuid;
175 argv[5] = imp->imp_obd->obd_uuid.uuid;
179 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
182 rc = USERMODEHELPER(argv[0], argv, envp);
184 CERROR("Error invoking recovery upcall %s %s %s %s %s: %d; "
185 "check /proc/sys/lustre/lustre_upcall\n",
186 argv[0], argv[1], argv[2], argv[3], argv[4],rc);
189 CERROR("Invoked upcall %s %s %s %s %s\n",
190 argv[0], argv[1], argv[2], argv[3], argv[4]);
194 int ptlrpc_replay(struct obd_import *imp)
197 struct list_head *tmp, *pos;
198 struct ptlrpc_request *req;
202 /* It might have committed some after we last spoke, so make sure we
203 * get rid of them now.
205 spin_lock_irqsave(&imp->imp_lock, flags);
206 ptlrpc_free_committed(imp);
207 spin_unlock_irqrestore(&imp->imp_lock, flags);
209 CDEBUG(D_HA, "import %p from %s has committed "LPD64"\n",
210 imp, imp->imp_target_uuid.uuid, imp->imp_peer_committed_transno);
212 list_for_each(tmp, &imp->imp_replay_list) {
213 req = list_entry(tmp, struct ptlrpc_request, rq_list);
214 DEBUG_REQ(D_HA, req, "RETAINED: ");
217 /* Do I need to hold a lock across this iteration? We shouldn't be
218 * racing with any additions to the list, because we're in recovery
219 * and are therefore not processing additional requests to add. Calls
220 * to ptlrpc_free_committed might commit requests, but nothing "newer"
221 * than the one we're replaying (it can't be committed until it's
222 * replayed, and we're doing that here). l_f_e_safe protects against
223 * problems with the current request being committed, in the unlikely
224 * event of that race. So, in conclusion, I think that it's safe to
225 * perform this list-walk without the imp_lock held.
227 * But, the {mdc,osc}_replay_open callbacks both iterate
228 * request lists, and have comments saying they assume the
229 * imp_lock is being held by ptlrpc_replay, but it's not. it's
230 * just a little race...
232 list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
233 req = list_entry(tmp, struct ptlrpc_request, rq_list);
235 DEBUG_REQ(D_HA, req, "REPLAY:");
237 rc = ptlrpc_replay_req(req);
240 CERROR("recovery replay error %d for req "LPD64"\n",
249 int ptlrpc_resend(struct obd_import *imp)
251 struct list_head *tmp, *pos;
252 struct ptlrpc_request *req;
257 /* As long as we're in recovery, nothing should be added to the sending
258 * list, so we don't need to hold the lock during this iteration and
261 /* Well... what if lctl recover is called twice at the same time?
263 spin_lock_irqsave(&imp->imp_lock, flags);
264 LASSERT(imp->imp_level == LUSTRE_CONN_RECOVER);
265 spin_unlock_irqrestore(&imp->imp_lock, flags);
267 list_for_each_safe(tmp, pos, &imp->imp_sending_list) {
268 req = list_entry(tmp, struct ptlrpc_request, rq_list);
269 ptlrpc_resend_req(req);
275 void ptlrpc_wake_delayed(struct obd_import *imp)
278 struct list_head *tmp, *pos;
279 struct ptlrpc_request *req;
281 spin_lock_irqsave(&imp->imp_lock, flags);
282 list_for_each_safe(tmp, pos, &imp->imp_delayed_list) {
283 req = list_entry(tmp, struct ptlrpc_request, rq_list);
285 ptlrpc_put_connection(req->rq_connection);
287 ptlrpc_connection_addref(req->rq_import->imp_connection);
290 DEBUG_REQ(D_HA, req, "waking (set %p):", req->rq_set);
291 wake_up(&req->rq_set->set_waitq);
293 DEBUG_REQ(D_HA, req, "waking:");
294 wake_up(&req->rq_wait_for_rep);
297 spin_unlock_irqrestore(&imp->imp_lock, flags);
300 inline void ptlrpc_invalidate_import_state(struct obd_import *imp)
302 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
303 if (ptlrpc_ldlm_namespace_cleanup == NULL)
304 CERROR("ptlrpc/ldlm hook is NULL! Please tell phil\n");
306 ptlrpc_ldlm_namespace_cleanup(ns, 1 /* no network ops */);
307 ptlrpc_abort_inflight(imp);
310 void ptlrpc_handle_failed_import(struct obd_import *imp)
313 if (!imp->imp_replayable) {
315 "import %s@%s for %s not replayable, deactivating\n",
316 imp->imp_target_uuid.uuid,
317 imp->imp_connection->c_remote_uuid.uuid,
318 imp->imp_obd->obd_name);
319 ptlrpc_set_import_active(imp, 0);
322 ptlrpc_run_failed_import_upcall(imp);
326 void ptlrpc_request_handle_eviction(struct ptlrpc_request *failed_req)
329 struct obd_import *imp= failed_req->rq_import;
333 CDEBUG(D_HA, "import %s of %s@%s evicted: reconnecting\n",
334 imp->imp_obd->obd_name,
335 imp->imp_target_uuid.uuid,
336 imp->imp_connection->c_remote_uuid.uuid);
337 rc = ptlrpc_recover_import(imp, NULL);
339 ptlrpc_resend_req(failed_req);
341 ptlrpc_handle_failed_import(imp);
343 LASSERT(failed_req->rq_import_generation < imp->imp_generation);
344 spin_lock_irqsave (&failed_req->rq_lock, flags);
345 failed_req->rq_err = 1;
346 spin_unlock_irqrestore (&failed_req->rq_lock, flags);
351 int ptlrpc_set_import_active(struct obd_import *imp, int active)
353 struct obd_device *notify_obd;
357 LASSERT(imp->imp_obd);
359 notify_obd = imp->imp_obd->u.cli.cl_containing_lov;
361 /* When deactivating, mark import invalid, and abort in-flight
364 spin_lock_irqsave(&imp->imp_lock, flags);
365 /* This is a bit of a hack, but invalidating replayable
366 * imports makes a temporary reconnect failure into a much more
367 * ugly -- and hard to remedy -- situation. */
368 if (!imp->imp_replayable) {
369 CDEBUG(D_HA, "setting import %s INVALID\n",
370 imp->imp_target_uuid.uuid);
371 imp->imp_invalid = 1;
373 imp->imp_generation++;
374 spin_unlock_irqrestore(&imp->imp_lock, flags);
375 ptlrpc_invalidate_import_state(imp);
376 //ptlrpc_abort_inflight(imp);
379 if (notify_obd == NULL)
382 /* How gross is _this_? */
383 if (!list_empty(¬ify_obd->obd_exports)) {
384 struct lustre_handle fakeconn;
385 struct obd_ioctl_data ioc_data = { 0 };
386 struct obd_export *exp =
387 list_entry(notify_obd->obd_exports.next,
388 struct obd_export, exp_obd_chain);
390 fakeconn.cookie = exp->exp_handle.h_cookie;
391 ioc_data.ioc_inlbuf1 = (char *)&imp->imp_target_uuid;
392 ioc_data.ioc_offset = active;
393 rc = obd_iocontrol(IOC_LOV_SET_OSC_ACTIVE, &fakeconn,
394 sizeof ioc_data, &ioc_data, NULL);
396 CERROR("error %sabling %s on LOV %p/%s: %d\n",
397 active ? "en" : "dis",
398 imp->imp_target_uuid.uuid, notify_obd,
399 notify_obd->obd_uuid.uuid, rc);
401 CDEBUG(D_HA, "No exports for obd %p/%s, can't notify about "
402 "%p\n", notify_obd, notify_obd->obd_uuid.uuid,
403 imp->imp_obd->obd_uuid.uuid);
408 /* When activating, mark import valid */
410 CDEBUG(D_HA, "setting import %s VALID\n",
411 imp->imp_target_uuid.uuid);
412 spin_lock_irqsave(&imp->imp_lock, flags);
413 imp->imp_invalid = 0;
414 spin_unlock_irqrestore(&imp->imp_lock, flags);
420 void ptlrpc_fail_import(struct obd_import *imp, int generation)
426 LASSERT (!imp->imp_dlm_fake);
428 spin_lock_irqsave(&imp->imp_lock, flags);
429 if (imp->imp_level != LUSTRE_CONN_FULL)
432 imp->imp_level = LUSTRE_CONN_NOTCONN;
433 spin_unlock_irqrestore(&imp->imp_lock, flags);
440 ptlrpc_handle_failed_import(imp);
444 static int signal_completed_replay(struct obd_import *imp)
446 struct ptlrpc_request *req;
450 req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL, NULL);
454 req->rq_replen = lustre_msg_size(0, NULL);
455 req->rq_level = LUSTRE_CONN_RECOVER;
456 req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
458 rc = ptlrpc_queue_wait(req);
460 ptlrpc_req_finished(req);
464 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid)
472 spin_lock_irqsave(&imp->imp_lock, flags);
473 if (imp->imp_level == LUSTRE_CONN_FULL ||
474 imp->imp_level == LUSTRE_CONN_NOTCONN)
475 imp->imp_level = LUSTRE_CONN_RECOVER;
478 spin_unlock_irqrestore(&imp->imp_lock, flags);
484 struct ptlrpc_connection *conn;
485 struct obd_uuid uuid;
486 struct ptlrpc_peer peer;
487 struct obd_export *dlmexp;
489 obd_str2uuid(&uuid, new_uuid);
490 if (ptlrpc_uuid_to_peer(&uuid, &peer)) {
491 CERROR("no connection found for UUID %s\n", new_uuid);
495 conn = ptlrpc_get_connection(&peer, &uuid);
499 CDEBUG(D_HA, "switching import %s/%s from %s to %s\n",
500 imp->imp_target_uuid.uuid, imp->imp_obd->obd_name,
501 imp->imp_connection->c_remote_uuid.uuid,
502 conn->c_remote_uuid.uuid);
504 /* Switch the import's connection and the DLM export's
505 * connection (which are almost certainly the same, but we
506 * keep distinct refs just to make things clearer. I think. */
507 if (imp->imp_connection)
508 ptlrpc_put_connection(imp->imp_connection);
509 /* We hand off the ref from ptlrpc_get_connection. */
510 imp->imp_connection = conn;
512 dlmexp = class_conn2export(&imp->imp_dlm_handle);
513 if (dlmexp->exp_connection)
514 ptlrpc_put_connection(dlmexp->exp_connection);
515 dlmexp->exp_connection = ptlrpc_connection_addref(conn);
516 class_export_put(dlmexp);
520 recon_result = ptlrpc_reconnect_import(imp);
522 if (recon_result < 0) {
523 CERROR("failed to reconnect to %s@%s: %d\n",
524 imp->imp_target_uuid.uuid,
525 imp->imp_connection->c_remote_uuid.uuid, recon_result);
526 spin_lock_irqsave(&imp->imp_lock, flags);
527 imp->imp_level = LUSTRE_CONN_NOTCONN;
528 spin_unlock_irqrestore(&imp->imp_lock, flags);
529 RETURN(recon_result);
532 if (recon_result == RECON_RESULT_RECOVERING) {
533 CDEBUG(D_HA, "replay requested by %s\n",
534 imp->imp_target_uuid.uuid);
535 rc = ptlrpc_replay(imp);
539 if (ptlrpc_ldlm_replay_locks == NULL)
540 CERROR("ptlrpc/ldlm hook is NULL! Please tell phil\n");
542 rc = ptlrpc_ldlm_replay_locks(imp);
546 rc = signal_completed_replay(imp);
549 } else if (recon_result == RECON_RESULT_RECONNECTED) {
550 CDEBUG(D_HA, "reconnected to %s@%s\n",
551 imp->imp_target_uuid.uuid,
552 imp->imp_connection->c_remote_uuid.uuid);
553 } else if (recon_result == RECON_RESULT_EVICTED) {
554 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
555 imp->imp_target_uuid.uuid,
556 imp->imp_connection->c_remote_uuid.uuid);
557 ptlrpc_set_import_active(imp, 0);
558 // ptlrpc_invalidate_import_state(imp);
563 ptlrpc_set_import_active(imp, 1);
565 rc = ptlrpc_resend(imp);
567 spin_lock_irqsave(&imp->imp_lock, flags);
568 imp->imp_level = LUSTRE_CONN_FULL;
569 spin_unlock_irqrestore(&imp->imp_lock, flags);
571 ptlrpc_wake_delayed(imp);
577 void ptlrpc_fail_export(struct obd_export *exp)
579 int rc, already_failed;
580 struct lustre_handle hdl;
583 spin_lock_irqsave(&exp->exp_lock, flags);
584 already_failed = exp->exp_failed;
586 spin_unlock_irqrestore(&exp->exp_lock, flags);
588 if (already_failed) {
589 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
590 exp, exp->exp_client_uuid.uuid);
594 CDEBUG(D_HA, "disconnecting export %p/%s\n",
595 exp, exp->exp_client_uuid.uuid);
596 hdl.cookie = exp->exp_handle.h_cookie;
597 rc = obd_disconnect(&hdl, 0);
599 CERROR("disconnecting export %p failed: %d\n", exp, rc);