4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ptlrpc/recover.c
34 * Author: Mike Shaver <shaver@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_RPC
38 #include <linux/list.h>
39 #include <libcfs/libcfs.h>
40 #include <obd_support.h>
41 #include <lustre_ha.h>
42 #include <lustre_net.h>
43 #include <lustre_import.h>
44 #include <lustre_export.h>
46 #include <obd_class.h>
48 #include "ptlrpc_internal.h"
51 * Start recovery on disconnected import.
52 * This is done by just attempting a connect
54 void ptlrpc_initiate_recovery(struct obd_import *imp)
58 CDEBUG(D_HA, "%s: starting recovery\n", obd2cli_tgt(imp->imp_obd));
59 ptlrpc_connect_import(imp);
65 * Identify what request from replay list needs to be replayed next
66 * (based on what we have already replayed) and send it to server.
68 int ptlrpc_replay_next(struct obd_import *imp, int *inflight)
71 struct list_head *tmp, *pos;
72 struct ptlrpc_request *req = NULL;
78 /* It might have committed some after we last spoke, so make sure we
79 * get rid of them now.
81 spin_lock(&imp->imp_lock);
82 imp->imp_last_transno_checked = 0;
83 ptlrpc_free_committed(imp);
84 last_transno = imp->imp_last_replay_transno;
86 CDEBUG(D_HA, "import %p from %s committed %llu last %llu\n",
87 imp, obd2cli_tgt(imp->imp_obd),
88 imp->imp_peer_committed_transno, last_transno);
90 /* Replay all the committed open requests on committed_list first */
91 if (!list_empty(&imp->imp_committed_list)) {
92 tmp = imp->imp_committed_list.prev;
93 req = list_entry(tmp, struct ptlrpc_request,
96 /* The last request on committed_list hasn't been replayed */
97 if (req->rq_transno > last_transno) {
98 if (!imp->imp_resend_replay ||
99 imp->imp_replay_cursor == &imp->imp_committed_list)
100 imp->imp_replay_cursor =
101 imp->imp_replay_cursor->next;
103 while (imp->imp_replay_cursor !=
104 &imp->imp_committed_list) {
105 req = list_entry(imp->imp_replay_cursor,
106 struct ptlrpc_request,
108 if (req->rq_transno > last_transno)
112 LASSERT(!list_empty(imp->imp_replay_cursor));
113 imp->imp_replay_cursor =
114 imp->imp_replay_cursor->next;
117 /* All requests on committed_list have been replayed */
118 imp->imp_replay_cursor = &imp->imp_committed_list;
123 /* All the requests in committed list have been replayed, let's replay
124 * the imp_replay_list */
126 list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
127 req = list_entry(tmp, struct ptlrpc_request,
130 if (req->rq_transno > last_transno)
136 /* If need to resend the last sent transno (because a reconnect
137 * has occurred), then stop on the matching req and send it again.
138 * If, however, the last sent transno has been committed then we
139 * continue replay from the next request. */
140 if (req != NULL && imp->imp_resend_replay)
141 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
143 /* The resend replay request may have been removed from the
145 if (req != NULL && imp->imp_resend_replay &&
146 list_empty(&req->rq_unreplied_list)) {
147 ptlrpc_add_unreplied(req);
148 imp->imp_known_replied_xid = ptlrpc_known_replied_xid(imp);
151 imp->imp_resend_replay = 0;
152 spin_unlock(&imp->imp_lock);
155 /* The request should have been added back in unreplied list
156 * by ptlrpc_prepare_replay(). */
157 LASSERT(!list_empty(&req->rq_unreplied_list));
159 rc = ptlrpc_replay_req(req);
161 CERROR("recovery replay error %d for req "
162 "%llu\n", rc, req->rq_xid);
171 * Schedule resending of request on sending_list. This is done after
172 * we completed replaying of requests and locks.
174 int ptlrpc_resend(struct obd_import *imp)
176 struct ptlrpc_request *req, *next;
180 /* As long as we're in recovery, nothing should be added to the sending
181 * list, so we don't need to hold the lock during this iteration and
184 /* Well... what if lctl recover is called twice at the same time?
186 spin_lock(&imp->imp_lock);
187 if (imp->imp_state != LUSTRE_IMP_RECOVER) {
188 spin_unlock(&imp->imp_lock);
192 list_for_each_entry_safe(req, next, &imp->imp_sending_list, rq_list) {
193 LASSERTF((long)req > PAGE_SIZE && req != LP_POISON,
194 "req %p bad\n", req);
195 LASSERTF(req->rq_type != LI_POISON, "req %p freed\n", req);
197 /* If the request is allowed to be sent during replay and it
198 * is not timeout yet, then it does not need to be resent. */
199 if (!ptlrpc_no_resend(req) &&
200 (req->rq_timedout || !req->rq_allow_replay))
201 ptlrpc_resend_req(req);
203 spin_unlock(&imp->imp_lock);
205 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT, 2);
210 * Go through all requests in delayed list and wake their threads
213 void ptlrpc_wake_delayed(struct obd_import *imp)
215 struct list_head *tmp, *pos;
216 struct ptlrpc_request *req;
218 spin_lock(&imp->imp_lock);
219 list_for_each_safe(tmp, pos, &imp->imp_delayed_list) {
220 req = list_entry(tmp, struct ptlrpc_request, rq_list);
222 DEBUG_REQ(D_HA, req, "waking (set %p):", req->rq_set);
223 ptlrpc_client_wake_req(req);
225 spin_unlock(&imp->imp_lock);
228 void ptlrpc_request_handle_notconn(struct ptlrpc_request *failed_req)
230 struct obd_import *imp = failed_req->rq_import;
233 CDEBUG(D_HA, "import %s of %s@%s abruptly disconnected: reconnecting\n",
234 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
235 imp->imp_connection->c_remote_uuid.uuid);
237 if (ptlrpc_set_import_discon(imp,
238 lustre_msg_get_conn_cnt(failed_req->rq_reqmsg))) {
239 if (!imp->imp_replayable) {
240 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
241 "auto-deactivating\n",
242 obd2cli_tgt(imp->imp_obd),
243 imp->imp_connection->c_remote_uuid.uuid,
244 imp->imp_obd->obd_name);
245 ptlrpc_deactivate_import(imp);
247 /* to control recovery via lctl {disable|enable}_recovery */
248 if (imp->imp_deactive == 0)
249 ptlrpc_connect_import(imp);
252 /* Wait for recovery to complete and resend. If evicted, then
253 this request will be errored out later.*/
254 spin_lock(&failed_req->rq_lock);
255 if (!failed_req->rq_no_resend)
256 failed_req->rq_resend = 1;
257 spin_unlock(&failed_req->rq_lock);
263 * Administratively active/deactive a client.
264 * This should only be called by the ioctl interface, currently
265 * - the lctl deactivate and activate commands
266 * - echo 0/1 >> /proc/osc/XXX/active
267 * - client umount -f (ll_umount_begin)
269 int ptlrpc_set_import_active(struct obd_import *imp, int active)
271 struct obd_device *obd = imp->imp_obd;
277 /* When deactivating, mark import invalid, and abort in-flight
280 LCONSOLE_WARN("setting import %s INACTIVE by administrator "
281 "request\n", obd2cli_tgt(imp->imp_obd));
283 /* set before invalidate to avoid messages about imp_inval
284 * set without imp_deactive in ptlrpc_import_delay_req */
285 spin_lock(&imp->imp_lock);
286 imp->imp_deactive = 1;
287 spin_unlock(&imp->imp_lock);
289 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DEACTIVATE);
291 ptlrpc_invalidate_import(imp);
294 /* When activating, mark import valid, and attempt recovery */
296 CDEBUG(D_HA, "setting import %s VALID\n",
297 obd2cli_tgt(imp->imp_obd));
299 spin_lock(&imp->imp_lock);
300 imp->imp_deactive = 0;
301 spin_unlock(&imp->imp_lock);
302 obd_import_event(imp->imp_obd, imp, IMP_EVENT_ACTIVATE);
304 rc = ptlrpc_recover_import(imp, NULL, 0);
309 EXPORT_SYMBOL(ptlrpc_set_import_active);
311 /* Attempt to reconnect an import */
312 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid, int async)
317 spin_lock(&imp->imp_lock);
318 if (imp->imp_state == LUSTRE_IMP_NEW || imp->imp_deactive ||
319 atomic_read(&imp->imp_inval_count))
321 spin_unlock(&imp->imp_lock);
325 /* force import to be disconnected. */
326 ptlrpc_set_import_discon(imp, 0);
329 struct obd_uuid uuid;
331 /* intruct import to use new uuid */
332 obd_str2uuid(&uuid, new_uuid);
333 rc = import_set_conn_priority(imp, &uuid);
338 /* Check if reconnect is already in progress */
339 spin_lock(&imp->imp_lock);
340 if (imp->imp_state != LUSTRE_IMP_DISCON) {
341 imp->imp_force_verify = 1;
344 spin_unlock(&imp->imp_lock);
348 rc = ptlrpc_connect_import(imp);
353 struct l_wait_info lwi;
354 int secs = cfs_time_seconds(obd_timeout);
356 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
357 obd2cli_tgt(imp->imp_obd), secs);
359 lwi = LWI_TIMEOUT(secs, NULL, NULL);
360 rc = l_wait_event(imp->imp_recovery_waitq,
361 !ptlrpc_import_in_recovery(imp), &lwi);
362 CDEBUG(D_HA, "%s: recovery finished\n",
363 obd2cli_tgt(imp->imp_obd));
370 EXPORT_SYMBOL(ptlrpc_recover_import);
372 int ptlrpc_import_in_recovery(struct obd_import *imp)
376 spin_lock(&imp->imp_lock);
377 if (imp->imp_state == LUSTRE_IMP_FULL ||
378 imp->imp_state == LUSTRE_IMP_CLOSED ||
379 imp->imp_state == LUSTRE_IMP_DISCON ||
380 imp->imp_obd->obd_no_recov)
382 spin_unlock(&imp->imp_lock);