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_ost.h>
41 #include <linux/obd_class.h>
42 #include <linux/obd_lov.h> /* for IOC_LOV_SET_OSC_ACTIVE */
44 #include "ptlrpc_internal.h"
46 static int ptlrpc_recover_import_no_retry(struct obd_import *, char *);
48 void ptlrpc_run_recovery_over_upcall(struct obd_device *obd)
55 argv[0] = obd_lustre_upcall;
56 argv[1] = "RECOVERY_OVER";
57 argv[2] = obd->obd_uuid.uuid;
61 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
64 rc = USERMODEHELPER(argv[0], argv, envp);
66 CERROR("Error invoking recovery upcall %s %s %s: %d; check "
67 "/proc/sys/lustre/upcall\n",
68 argv[0], argv[1], argv[2], rc);
71 CERROR("Invoked upcall %s %s %s\n",
72 argv[0], argv[1], argv[2]);
76 void ptlrpc_run_failed_import_upcall(struct obd_import* imp)
85 spin_lock_irqsave(&imp->imp_lock, flags);
86 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
87 spin_unlock_irqrestore(&imp->imp_lock, flags);
91 spin_unlock_irqrestore(&imp->imp_lock, flags);
93 argv[0] = obd_lustre_upcall;
94 argv[1] = "FAILED_IMPORT";
95 argv[2] = imp->imp_target_uuid.uuid;
96 argv[3] = imp->imp_obd->obd_name;
97 argv[4] = imp->imp_connection->c_remote_uuid.uuid;
98 argv[5] = imp->imp_obd->obd_uuid.uuid;
102 envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
105 rc = USERMODEHELPER(argv[0], argv, envp);
107 CERROR("Error invoking recovery upcall %s %s %s %s %s: %d; "
108 "check /proc/sys/lustre/lustre_upcall\n",
109 argv[0], argv[1], argv[2], argv[3], argv[4],rc);
112 CERROR("Invoked upcall %s %s %s %s %s\n",
113 argv[0], argv[1], argv[2], argv[3], argv[4]);
116 ptlrpc_recover_import(imp, NULL);
120 int ptlrpc_replay_next(struct obd_import *imp)
123 struct list_head *tmp, *pos;
124 struct ptlrpc_request *req;
130 /* It might have committed some after we last spoke, so make sure we
131 * get rid of them now.
133 spin_lock_irqsave(&imp->imp_lock, flags);
134 ptlrpc_free_committed(imp);
135 last_transno = imp->imp_last_replay_transno;
136 spin_unlock_irqrestore(&imp->imp_lock, flags);
138 CDEBUG(D_HA, "import %p from %s has committed "LPD64"\n",
139 imp, imp->imp_target_uuid.uuid, imp->imp_peer_committed_transno);
140 /* Do I need to hold a lock across this iteration? We shouldn't be
141 * racing with any additions to the list, because we're in recovery
142 * and are therefore not processing additional requests to add. Calls
143 * to ptlrpc_free_committed might commit requests, but nothing "newer"
144 * than the one we're replaying (it can't be committed until it's
145 * replayed, and we're doing that here). l_f_e_safe protects against
146 * problems with the current request being committed, in the unlikely
147 * event of that race. So, in conclusion, I think that it's safe to
148 * perform this list-walk without the imp_lock held.
150 * But, the {mdc,osc}_replay_open callbacks both iterate
151 * request lists, and have comments saying they assume the
152 * imp_lock is being held by ptlrpc_replay, but it's not. it's
153 * just a little race...
155 list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
156 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
157 if (req->rq_transno > last_transno) {
158 /* remove from list so ptlrpcd can send the
159 req, it should be reinserted after it is
160 sent and replied. Perhaps better solution
161 would be to add req->rq_replay_list so the
162 req can be saved for replay and still go
163 through the normal send thread. */
164 rc = ptlrpc_replay_req(req);
166 CERROR("recovery replay error %d for req "LPD64"\n",
179 int ptlrpc_resend(struct obd_import *imp)
181 struct list_head *tmp, *pos;
182 struct ptlrpc_request *req;
187 /* As long as we're in recovery, nothing should be added to the sending
188 * list, so we don't need to hold the lock during this iteration and
191 /* Well... what if lctl recover is called twice at the same time?
193 spin_lock_irqsave(&imp->imp_lock, flags);
194 if (imp->imp_state != LUSTRE_IMP_RECOVER) {
195 spin_unlock_irqrestore(&imp->imp_lock, flags);
198 spin_unlock_irqrestore(&imp->imp_lock, flags);
200 list_for_each_safe(tmp, pos, &imp->imp_sending_list) {
201 req = list_entry(tmp, struct ptlrpc_request, rq_list);
202 ptlrpc_resend_req(req);
208 void ptlrpc_wake_delayed(struct obd_import *imp)
211 struct list_head *tmp, *pos;
212 struct ptlrpc_request *req;
214 spin_lock_irqsave(&imp->imp_lock, flags);
215 list_for_each_safe(tmp, pos, &imp->imp_delayed_list) {
216 req = list_entry(tmp, struct ptlrpc_request, rq_list);
219 DEBUG_REQ(D_HA, req, "waking (set %p):", req->rq_set);
220 wake_up(&req->rq_set->set_waitq);
222 DEBUG_REQ(D_HA, req, "waking:");
223 wake_up(&req->rq_reply_waitq);
226 spin_unlock_irqrestore(&imp->imp_lock, flags);
229 inline void ptlrpc_invalidate_import_state(struct obd_import *imp)
231 struct obd_device *obd = imp->imp_obd;
232 struct ldlm_namespace *ns = obd->obd_namespace;
234 ptlrpc_abort_inflight(imp);
236 obd_invalidate_import(obd, imp);
238 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
241 void ptlrpc_handle_failed_import(struct obd_import *imp)
245 if (!imp->imp_replayable) {
247 "import %s@%s for %s not replayable, deactivating\n",
248 imp->imp_target_uuid.uuid,
249 imp->imp_connection->c_remote_uuid.uuid,
250 imp->imp_obd->obd_name);
251 ptlrpc_set_import_active(imp, 0);
254 ptlrpc_run_failed_import_upcall(imp);
258 void ptlrpc_request_handle_notconn(struct ptlrpc_request *failed_req)
261 struct obd_import *imp= failed_req->rq_import;
265 CDEBUG(D_HA, "import %s of %s@%s abruptly disconnected: reconnecting\n",
266 imp->imp_obd->obd_name,
267 imp->imp_target_uuid.uuid,
268 imp->imp_connection->c_remote_uuid.uuid);
270 ptlrpc_set_import_discon(imp);
272 rc = ptlrpc_connect_import(imp, NULL);
274 /* Wait for recovery to complete and resend. If evicted, then
275 this request will be errored out later.*/
276 spin_lock_irqsave(&failed_req->rq_lock, flags);
277 if (!failed_req->rq_no_resend)
278 failed_req->rq_resend = 1;
279 spin_unlock_irqrestore(&failed_req->rq_lock, flags);
284 int ptlrpc_set_import_active(struct obd_import *imp, int active)
286 struct obd_device *obd = imp->imp_obd;
291 /* When deactivating, mark import invalid, and abort in-flight
294 spin_lock_irqsave(&imp->imp_lock, flags);
295 /* This is a bit of a hack, but invalidating replayable
296 * imports makes a temporary reconnect failure into a much more
297 * ugly -- and hard to remedy -- situation. */
298 if (!imp->imp_replayable) {
299 CDEBUG(D_HA, "setting import %s INVALID\n",
300 imp->imp_target_uuid.uuid);
301 imp->imp_invalid = 1;
303 imp->imp_generation++;
304 spin_unlock_irqrestore(&imp->imp_lock, flags);
305 ptlrpc_invalidate_import_state(imp);
308 /* When activating, mark import valid */
310 CDEBUG(D_HA, "setting import %s VALID\n",
311 imp->imp_target_uuid.uuid);
312 spin_lock_irqsave(&imp->imp_lock, flags);
313 imp->imp_invalid = 0;
314 spin_unlock_irqrestore(&imp->imp_lock, flags);
317 if (obd->obd_observer)
318 RETURN(obd_notify(obd->obd_observer, obd, active));
323 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid)
328 /* force import to be disconnected. */
329 ptlrpc_set_import_discon(imp);
331 rc = ptlrpc_recover_import_no_retry(imp, new_uuid);
336 int ptlrpc_import_in_recovery(struct obd_import *imp)
340 spin_lock_irqsave(&imp->imp_lock, flags);
341 if (imp->imp_state == LUSTRE_IMP_FULL ||
342 imp->imp_state == LUSTRE_IMP_CLOSED ||
343 imp->imp_state == LUSTRE_IMP_DISCON)
345 spin_unlock_irqrestore(&imp->imp_lock, flags);
349 static int ptlrpc_recover_import_no_retry(struct obd_import *imp,
355 struct l_wait_info lwi;
358 spin_lock_irqsave(&imp->imp_lock, flags);
359 if (imp->imp_state != LUSTRE_IMP_DISCON) {
362 spin_unlock_irqrestore(&imp->imp_lock, flags);
364 if (in_recovery == 1)
368 rc = ptlrpc_connect_import(imp, new_uuid);
372 CDEBUG(D_ERROR, "%s: recovery started, waiting\n",
373 imp->imp_client->cli_name);
375 lwi = LWI_TIMEOUT(MAX(obd_timeout * HZ, 1), NULL, NULL);
376 rc = l_wait_event(imp->imp_recovery_waitq,
377 !ptlrpc_import_in_recovery(imp), &lwi);
378 CDEBUG(D_ERROR, "%s: recovery finished\n",
379 imp->imp_client->cli_name);
385 void ptlrpc_fail_export(struct obd_export *exp)
387 int rc, already_failed;
390 spin_lock_irqsave(&exp->exp_lock, flags);
391 already_failed = exp->exp_failed;
393 spin_unlock_irqrestore(&exp->exp_lock, flags);
395 if (already_failed) {
396 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
397 exp, exp->exp_client_uuid.uuid);
401 CDEBUG(D_HA, "disconnecting export %p/%s\n",
402 exp, exp->exp_client_uuid.uuid);
404 /* Most callers into obd_disconnect are removing their own reference
405 * (request, for example) in addition to the one from the hash table.
406 * We don't have such a reference here, so make one. */
407 class_export_get(exp);
408 rc = obd_disconnect(exp, 0);
410 CERROR("disconnecting export %p failed: %d\n", exp, rc);