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 int ptlrpc_reconnect_import(struct obd_import *imp,
46 struct ptlrpc_request **reqptr)
48 struct obd_device *obd = imp->imp_obd;
49 int flags, rc, size[] = {sizeof(imp->imp_target_uuid),
50 sizeof(obd->obd_uuid),
51 sizeof(imp->imp_dlm_handle)};
52 char *tmp[] = {imp->imp_target_uuid.uuid,
54 (char *)&imp->imp_dlm_handle};
55 struct ptlrpc_connection *conn = imp->imp_connection;
56 struct ptlrpc_request *req;
57 struct lustre_handle old_hdl;
59 spin_lock_irqsave(&imp->imp_lock, flags);
60 imp->imp_generation++;
61 spin_unlock_irqrestore(&imp->imp_lock, flags);
63 CERROR("reconnect handle "LPX64"\n",
64 imp->imp_dlm_handle.cookie);
66 req = ptlrpc_prep_req(imp, imp->imp_connect_op, 3, size, tmp);
69 req->rq_level = LUSTRE_CONN_NEW;
70 req->rq_replen = lustre_msg_size(0, NULL);
71 rc = ptlrpc_queue_wait(req);
73 CERROR("cannot connect to %s@%s: rc = %d\n",
74 imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid, rc);
78 if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT) {
79 memset(&old_hdl, 0, sizeof(old_hdl));
80 if (!memcmp(&old_hdl, &req->rq_repmsg->handle,
82 CERROR("%s@%s didn't like our handle "LPX64
83 ", failed\n", imp->imp_target_uuid.uuid,
84 conn->c_remote_uuid.uuid,
85 imp->imp_dlm_handle.cookie);
86 GOTO(out_disc, rc = -ENOTCONN);
89 if (memcmp(&imp->imp_remote_handle, &req->rq_repmsg->handle,
90 sizeof(imp->imp_remote_handle))) {
91 CERROR("%s@%s changed handle from "LPX64" to "LPX64
92 "; copying, but this may foreshadow disaster\n",
93 imp->imp_target_uuid.uuid,
94 conn->c_remote_uuid.uuid,
95 imp->imp_remote_handle.cookie,
96 req->rq_repmsg->handle.cookie);
97 imp->imp_remote_handle = req->rq_repmsg->handle;
98 GOTO(out_disc, rc = 0);
101 CERROR("reconnected to %s@%s after partition\n",
102 imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid);
103 GOTO(out_disc, rc = 0);
106 old_hdl = imp->imp_remote_handle;
107 imp->imp_remote_handle = req->rq_repmsg->handle;
108 CERROR("reconnected to %s@%s ("LPX64", was "LPX64")!\n",
109 imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid,
110 imp->imp_remote_handle.cookie, old_hdl.cookie);
111 GOTO(out_disc, rc = 0);
118 void ptlrpc_run_recovery_over_upcall(struct obd_device *obd)
125 argv[0] = obd_lustre_upcall;
126 argv[1] = "RECOVERY_OVER";
127 argv[2] = obd->obd_uuid.uuid;
131 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
134 rc = USERMODEHELPER(argv[0], argv, envp);
136 CERROR("Error invoking recovery upcall %s %s %s: %d; check "
137 "/proc/sys/lustre/upcall\n",
138 argv[0], argv[1], argv[2], rc);
141 CERROR("Invoked upcall %s %s %s",
142 argv[0], argv[1], argv[2]);
146 void ptlrpc_run_failed_import_upcall(struct obd_import* imp)
153 argv[0] = obd_lustre_upcall;
154 argv[1] = "FAILED_IMPORT";
155 argv[2] = imp->imp_target_uuid.uuid;
156 argv[3] = imp->imp_obd->obd_uuid.uuid;
157 argv[4] = imp->imp_connection->c_remote_uuid.uuid;
161 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
164 rc = USERMODEHELPER(argv[0], argv, envp);
166 CERROR("Error invoking recovery upcall %s %s %s %s %s: %d; check "
167 "/proc/sys/lustre/lustre_upcall\n",
168 argv[0], argv[1], argv[2], argv[3], argv[4],rc);
171 CERROR("Invoked upcall %s %s %s %s %s\n",
172 argv[0], argv[1], argv[2], argv[3], argv[4]);
176 int ptlrpc_replay(struct obd_import *imp)
179 struct list_head *tmp, *pos;
180 struct ptlrpc_request *req;
182 __u64 committed = imp->imp_peer_committed_transno;
185 /* It might have committed some after we last spoke, so make sure we
186 * get rid of them now.
188 spin_lock_irqsave(&imp->imp_lock, flags);
189 ptlrpc_free_committed(imp);
190 spin_unlock_irqrestore(&imp->imp_lock, flags);
192 CDEBUG(D_HA, "import %p from %s has committed "LPD64"\n",
193 imp, imp->imp_target_uuid.uuid, committed);
195 list_for_each(tmp, &imp->imp_replay_list) {
196 req = list_entry(tmp, struct ptlrpc_request, rq_list);
197 DEBUG_REQ(D_HA, req, "RETAINED: ");
200 /* Do I need to hold a lock across this iteration? We shouldn't be
201 * racing with any additions to the list, because we're in recovery
202 * and are therefore not processing additional requests to add. Calls
203 * to ptlrpc_free_committed might commit requests, but nothing "newer"
204 * than the one we're replaying (it can't be committed until it's
205 * replayed, and we're doing that here). l_f_e_safe protects against
206 * problems with the current request being committed, in the unlikely
207 * event of that race. So, in conclusion, I think that it's safe to
208 * perform this list-walk without the imp_lock held.
210 * But, the {mdc,osc}_replay_open callbacks both iterate
211 * request lists, and have comments saying they assume the
212 * imp_lock is being held by ptlrpc_replay, but it's not. it's
213 * just a little race...
215 list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
216 req = list_entry(tmp, struct ptlrpc_request, rq_list);
218 DEBUG_REQ(D_HA, req, "REPLAY:");
220 rc = ptlrpc_replay_req(req);
223 CERROR("recovery replay error %d for req "LPD64"\n",
232 int ptlrpc_resend(struct obd_import *imp)
234 struct list_head *tmp, *pos;
235 struct ptlrpc_request *req;
240 /* As long as we're in recovery, nothing should be added to the sending
241 * list, so we don't need to hold the lock during this iteration and
244 spin_lock_irqsave(&imp->imp_lock, flags);
245 LASSERT(imp->imp_level < LUSTRE_CONN_FULL);
246 spin_unlock_irqrestore(&imp->imp_lock, flags);
248 list_for_each_safe(tmp, pos, &imp->imp_sending_list) {
249 req = list_entry(tmp, struct ptlrpc_request, rq_list);
250 ptlrpc_resend_req(req);
256 void ptlrpc_wake_delayed(struct obd_import *imp)
259 struct list_head *tmp, *pos;
260 struct ptlrpc_request *req;
262 spin_lock_irqsave(&imp->imp_lock, flags);
263 list_for_each_safe(tmp, pos, &imp->imp_delayed_list) {
264 req = list_entry(tmp, struct ptlrpc_request, rq_list);
266 ptlrpc_put_connection(req->rq_connection);
268 ptlrpc_connection_addref(req->rq_import->imp_connection);
271 DEBUG_REQ(D_HA, req, "waking (set %p):", req->rq_set);
272 wake_up(&req->rq_set->set_waitq);
274 DEBUG_REQ(D_HA, req, "waking:");
275 wake_up(&req->rq_wait_for_rep);
278 spin_unlock_irqrestore(&imp->imp_lock, flags);
281 inline void ptlrpc_invalidate_import_state(struct obd_import *imp)
283 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
284 if (ptlrpc_ldlm_namespace_cleanup == NULL)
285 CERROR("ptlrpc/ldlm hook is NULL! Please tell phil\n");
287 ptlrpc_ldlm_namespace_cleanup(ns, 1 /* no network ops */);
288 ptlrpc_abort_inflight(imp);
291 int ptlrpc_request_handle_eviction(struct ptlrpc_request *failed_req)
293 int rc = 0, in_recovery = 0;
294 struct obd_import *imp= failed_req->rq_import;
296 struct ptlrpc_request *req;
298 spin_lock_irqsave(&imp->imp_lock, flags);
300 if (imp->imp_level == LUSTRE_CONN_NOTCONN)
303 if (failed_req->rq_import_generation == imp->imp_generation)
304 imp->imp_level = LUSTRE_CONN_NOTCONN;
308 spin_unlock_irqrestore(&imp->imp_lock, flags);
311 ptlrpc_resend_req(failed_req);
315 CDEBUG(D_HA, "import %s of %s@%s evicted: reconnecting\n",
316 imp->imp_obd->obd_name,
317 imp->imp_target_uuid.uuid,
318 imp->imp_connection->c_remote_uuid.uuid);
319 rc = ptlrpc_reconnect_import(imp, &req);
321 ptlrpc_resend_req(failed_req);
322 ptlrpc_fail_import(imp, imp->imp_generation);
324 spin_lock_irqsave (&failed_req->rq_lock, flags);
325 failed_req->rq_err = 1;
326 spin_unlock_irqrestore (&failed_req->rq_lock, flags);
327 spin_lock_irqsave(&imp->imp_lock, flags);
328 imp->imp_level = LUSTRE_CONN_FULL;
329 imp->imp_invalid = 0;
330 spin_unlock_irqrestore(&imp->imp_lock, flags);
331 ptlrpc_invalidate_import_state(imp/*, req->rq_import_generation*/);
333 ptlrpc_req_finished(req);
337 int ptlrpc_set_import_active(struct obd_import *imp, int active)
339 struct obd_device *notify_obd;
343 LASSERT(imp->imp_obd);
345 notify_obd = imp->imp_obd->u.cli.cl_containing_lov;
347 /* When deactivating, mark import invalid, and
348 abort in-flight requests. */
350 spin_lock_irqsave(&imp->imp_lock, flags);
351 imp->imp_invalid = 1;
352 spin_unlock_irqrestore(&imp->imp_lock, flags);
354 ptlrpc_abort_inflight(imp);
357 imp->imp_invalid = !active;
359 if (notify_obd == NULL)
362 /* How gross is _this_? */
363 if (!list_empty(¬ify_obd->obd_exports)) {
364 struct lustre_handle fakeconn;
365 struct obd_ioctl_data ioc_data = { 0 };
366 struct obd_export *exp =
367 list_entry(notify_obd->obd_exports.next,
368 struct obd_export, exp_obd_chain);
370 fakeconn.cookie = exp->exp_handle.h_cookie;
371 ioc_data.ioc_inlbuf1 = (char *)&imp->imp_target_uuid;
372 ioc_data.ioc_offset = active;
373 rc = obd_iocontrol(IOC_LOV_SET_OSC_ACTIVE, &fakeconn,
374 sizeof ioc_data, &ioc_data, NULL);
376 CERROR("error %sabling %s on LOV %p/%s: %d\n",
377 active ? "en" : "dis",
378 imp->imp_target_uuid.uuid, notify_obd,
379 notify_obd->obd_uuid.uuid, rc);
381 CDEBUG(D_HA, "No exports for obd %p/%s, can't notify about "
382 "%p\n", notify_obd, notify_obd->obd_uuid.uuid,
383 imp->imp_obd->obd_uuid.uuid);
388 /* When activating, mark import valid */
390 spin_lock_irqsave(&imp->imp_lock, flags);
391 imp->imp_invalid = 0;
392 spin_unlock_irqrestore(&imp->imp_lock, flags);
398 void ptlrpc_fail_import(struct obd_import *imp, int generation)
404 LASSERT (!imp->imp_dlm_fake);
406 /* If we were already in recovery, or if the import's connection to its
407 * service is newer than the failing operation's original attempt, then
408 * we don't want to recover again. */
409 spin_lock_irqsave(&imp->imp_lock, flags);
411 if (imp->imp_level == LUSTRE_CONN_RECOVD)
414 if (generation == imp->imp_generation) {
415 imp->imp_level = LUSTRE_CONN_RECOVD;
416 imp->imp_generation++;
421 spin_unlock_irqrestore(&imp->imp_lock, flags);
428 if (!imp->imp_replayable) {
430 "import %s@%s for %s not replayable, deactivating\n",
431 imp->imp_target_uuid.uuid,
432 imp->imp_connection->c_remote_uuid.uuid,
433 imp->imp_obd->obd_name);
434 ptlrpc_set_import_active(imp, 0);
437 ptlrpc_run_failed_import_upcall(imp);
441 static int signal_completed_replay(struct obd_import *imp)
443 struct ptlrpc_request *req;
447 req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL, NULL);
451 req->rq_replen = lustre_msg_size(0, NULL);
452 req->rq_level = LUSTRE_CONN_RECOVD;
453 req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
455 rc = ptlrpc_queue_wait(req);
457 ptlrpc_req_finished(req);
461 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid)
463 int msg_flags = 0, rc;
465 struct ptlrpc_request *req;
468 spin_lock_irqsave(&imp->imp_lock, flags);
469 if (imp->imp_level == LUSTRE_CONN_FULL) {
470 imp->imp_level = LUSTRE_CONN_RECOVD;
471 imp->imp_generation++;
473 spin_unlock_irqrestore(&imp->imp_lock, flags);
476 struct ptlrpc_connection *conn;
477 struct obd_uuid uuid;
478 struct ptlrpc_peer peer;
479 struct obd_export *dlmexp;
481 obd_str2uuid(&uuid, new_uuid);
482 if (ptlrpc_uuid_to_peer(&uuid, &peer)) {
483 CERROR("no connection found for UUID %s\n", new_uuid);
487 conn = ptlrpc_get_connection(&peer, &uuid);
491 CDEBUG(D_HA, "switching import %s/%s from %s to %s\n",
492 imp->imp_target_uuid.uuid, imp->imp_obd->obd_name,
493 imp->imp_connection->c_remote_uuid.uuid,
494 conn->c_remote_uuid.uuid);
496 /* Switch the import's connection and the DLM export's
497 * connection (which are almost certainly the same, but we
498 * keep distinct refs just to make things clearer. I think. */
499 if (imp->imp_connection)
500 ptlrpc_put_connection(imp->imp_connection);
501 /* We hand off the ref from ptlrpc_get_connection. */
502 imp->imp_connection = conn;
504 dlmexp = class_conn2export(&imp->imp_dlm_handle);
505 if (dlmexp->exp_connection)
506 ptlrpc_put_connection(dlmexp->exp_connection);
507 dlmexp->exp_connection = ptlrpc_connection_addref(conn);
508 class_export_put(dlmexp);
512 rc = ptlrpc_reconnect_import(imp, &req);
515 CERROR("failed to reconnect to %s@%s: %d\n",
516 imp->imp_target_uuid.uuid,
517 imp->imp_connection->c_remote_uuid.uuid, rc);
522 msg_flags = lustre_msg_get_op_flags(req->rq_repmsg);
524 if (msg_flags & MSG_CONNECT_RECOVERING) {
525 CDEBUG(D_HA, "replay requested by %s\n",
526 imp->imp_target_uuid.uuid);
527 rc = ptlrpc_replay(imp);
531 if (ptlrpc_ldlm_replay_locks == NULL)
532 CERROR("ptlrpc/ldlm hook is NULL! Please tell phil\n");
534 rc = ptlrpc_ldlm_replay_locks(imp);
538 rc = signal_completed_replay(imp);
541 } else if (msg_flags & MSG_CONNECT_RECONNECT) {
542 CDEBUG(D_HA, "reconnected to %s@%s\n",
543 imp->imp_target_uuid.uuid,
544 imp->imp_connection->c_remote_uuid.uuid);
546 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
547 imp->imp_target_uuid.uuid,
548 imp->imp_connection->c_remote_uuid.uuid);
549 ptlrpc_invalidate_import_state(imp);
552 rc = ptlrpc_resend(imp);
554 spin_lock_irqsave(&imp->imp_lock, flags);
555 imp->imp_level = LUSTRE_CONN_FULL;
556 imp->imp_invalid = 0;
557 spin_unlock_irqrestore(&imp->imp_lock, flags);
559 ptlrpc_wake_delayed(imp);
562 ptlrpc_req_finished(req);
566 void ptlrpc_fail_export(struct obd_export *exp)
568 int rc, already_failed;
569 struct lustre_handle hdl;
572 spin_lock_irqsave(&exp->exp_lock, flags);
573 already_failed = exp->exp_failed;
575 spin_unlock_irqrestore(&exp->exp_lock, flags);
577 if (already_failed) {
578 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
579 exp, exp->exp_client_uuid.uuid);
583 CDEBUG(D_HA, "disconnecting export %p/%s\n",
584 exp, exp->exp_client_uuid.uuid);
585 hdl.cookie = exp->exp_handle.h_cookie;
586 rc = obd_disconnect(&hdl, 0);
588 CERROR("disconnecting export %p failed: %d\n", exp, rc);