Whamcloud - gitweb
LU-7558 ptlrpc: connect vs import invalidate race
[fs/lustre-release.git] / lustre / ptlrpc / recover.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ptlrpc/recover.c
33  *
34  * Author: Mike Shaver <shaver@clusterfs.com>
35  */
36
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>
45 #include <obd.h>
46 #include <obd_class.h>
47
48 #include "ptlrpc_internal.h"
49
50 /**
51  * Start recovery on disconnected import.
52  * This is done by just attempting a connect
53  */
54 void ptlrpc_initiate_recovery(struct obd_import *imp)
55 {
56         ENTRY;
57
58         CDEBUG(D_HA, "%s: starting recovery\n", obd2cli_tgt(imp->imp_obd));
59         ptlrpc_connect_import(imp);
60
61         EXIT;
62 }
63
64 /**
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.
67  */
68 int ptlrpc_replay_next(struct obd_import *imp, int *inflight)
69 {
70         int rc = 0;
71         struct list_head *tmp, *pos;
72         struct ptlrpc_request *req = NULL;
73         __u64 last_transno;
74         ENTRY;
75
76         *inflight = 0;
77
78         /* It might have committed some after we last spoke, so make sure we
79          * get rid of them now.
80          */
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;
85
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);
89
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,
94                                      rq_replay_list);
95
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;
102
103                         while (imp->imp_replay_cursor !=
104                                &imp->imp_committed_list) {
105                                 req = list_entry(imp->imp_replay_cursor,
106                                                      struct ptlrpc_request,
107                                                      rq_replay_list);
108                                 if (req->rq_transno > last_transno)
109                                         break;
110
111                                 req = NULL;
112                                 LASSERT(!list_empty(imp->imp_replay_cursor));
113                                 imp->imp_replay_cursor =
114                                         imp->imp_replay_cursor->next;
115                         }
116                 } else {
117                         /* All requests on committed_list have been replayed */
118                         imp->imp_replay_cursor = &imp->imp_committed_list;
119                         req = NULL;
120                 }
121         }
122
123         /* All the requests in committed list have been replayed, let's replay
124          * the imp_replay_list */
125         if (req == NULL) {
126                 list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
127                         req = list_entry(tmp, struct ptlrpc_request,
128                                              rq_replay_list);
129
130                         if (req->rq_transno > last_transno)
131                                 break;
132                         req = NULL;
133                 }
134         }
135
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);
142
143         /* ptlrpc_prepare_replay() may fail to add the reqeust into unreplied
144          * list if the request hasn't been added to replay list then. Another
145          * exception is that resend replay could have been removed from the
146          * unreplied list. */
147         if (req != NULL && list_empty(&req->rq_unreplied_list)) {
148                 DEBUG_REQ(D_HA, req, "resend_replay: %d, last_transno: %llu\n",
149                           imp->imp_resend_replay, last_transno);
150                 ptlrpc_add_unreplied(req);
151                 imp->imp_known_replied_xid = ptlrpc_known_replied_xid(imp);
152         }
153
154         imp->imp_resend_replay = 0;
155         spin_unlock(&imp->imp_lock);
156
157         if (req != NULL) {
158                 LASSERT(!list_empty(&req->rq_unreplied_list));
159
160                 rc = ptlrpc_replay_req(req);
161                 if (rc) {
162                         CERROR("recovery replay error %d for req "
163                                "%llu\n", rc, req->rq_xid);
164                         RETURN(rc);
165                 }
166                 *inflight = 1;
167         }
168         RETURN(rc);
169 }
170
171 /**
172  * Schedule resending of request on sending_list. This is done after
173  * we completed replaying of requests and locks.
174  */
175 int ptlrpc_resend(struct obd_import *imp)
176 {
177         struct ptlrpc_request *req, *next;
178
179         ENTRY;
180
181         /* As long as we're in recovery, nothing should be added to the sending
182          * list, so we don't need to hold the lock during this iteration and
183          * resend process.
184          */
185         /* Well... what if lctl recover is called twice at the same time?
186          */
187         spin_lock(&imp->imp_lock);
188         if (imp->imp_state != LUSTRE_IMP_RECOVER) {
189                 spin_unlock(&imp->imp_lock);
190                 RETURN(-1);
191         }
192
193         list_for_each_entry_safe(req, next, &imp->imp_sending_list, rq_list) {
194                 LASSERTF((long)req > PAGE_SIZE && req != LP_POISON,
195                          "req %p bad\n", req);
196                 LASSERTF(req->rq_type != LI_POISON, "req %p freed\n", req);
197
198                 /* If the request is allowed to be sent during replay and it
199                  * is not timeout yet, then it does not need to be resent. */
200                 if (!ptlrpc_no_resend(req) &&
201                     (req->rq_timedout || !req->rq_allow_replay))
202                         ptlrpc_resend_req(req);
203         }
204         spin_unlock(&imp->imp_lock);
205
206         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT, 2);
207         RETURN(0);
208 }
209
210 /**
211  * Go through all requests in delayed list and wake their threads
212  * for resending
213  */
214 void ptlrpc_wake_delayed(struct obd_import *imp)
215 {
216         struct list_head *tmp, *pos;
217         struct ptlrpc_request *req;
218
219         spin_lock(&imp->imp_lock);
220         list_for_each_safe(tmp, pos, &imp->imp_delayed_list) {
221                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
222
223                 DEBUG_REQ(D_HA, req, "waking (set %p):", req->rq_set);
224                 ptlrpc_client_wake_req(req);
225         }
226         spin_unlock(&imp->imp_lock);
227 }
228
229 void ptlrpc_request_handle_notconn(struct ptlrpc_request *failed_req)
230 {
231         struct obd_import *imp = failed_req->rq_import;
232         ENTRY;
233
234         CDEBUG(D_HA, "import %s of %s@%s abruptly disconnected: reconnecting\n",
235                imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
236                imp->imp_connection->c_remote_uuid.uuid);
237
238         if (ptlrpc_set_import_discon(imp,
239                               lustre_msg_get_conn_cnt(failed_req->rq_reqmsg))) {
240                 if (!imp->imp_replayable) {
241                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
242                                "auto-deactivating\n",
243                                obd2cli_tgt(imp->imp_obd),
244                                imp->imp_connection->c_remote_uuid.uuid,
245                                imp->imp_obd->obd_name);
246                         ptlrpc_deactivate_import(imp);
247                 }
248                 /* to control recovery via lctl {disable|enable}_recovery */
249                 if (imp->imp_deactive == 0)
250                         ptlrpc_connect_import(imp);
251         }
252
253         /* Wait for recovery to complete and resend. If evicted, then
254            this request will be errored out later.*/
255         spin_lock(&failed_req->rq_lock);
256         if (!failed_req->rq_no_resend)
257                 failed_req->rq_resend = 1;
258         spin_unlock(&failed_req->rq_lock);
259
260         EXIT;
261 }
262
263 /**
264  * Administratively active/deactive a client. 
265  * This should only be called by the ioctl interface, currently
266  *  - the lctl deactivate and activate commands
267  *  - echo 0/1 >> /proc/osc/XXX/active
268  *  - client umount -f (ll_umount_begin)
269  */
270 int ptlrpc_set_import_active(struct obd_import *imp, int active)
271 {
272         struct obd_device *obd = imp->imp_obd;
273         int rc = 0;
274
275         ENTRY;
276         LASSERT(obd);
277
278         /* When deactivating, mark import invalid, and abort in-flight
279          * requests. */
280         if (!active) {
281                 LCONSOLE_WARN("setting import %s INACTIVE by administrator "
282                               "request\n", obd2cli_tgt(imp->imp_obd));
283
284                 /* set before invalidate to avoid messages about imp_inval
285                  * set without imp_deactive in ptlrpc_import_delay_req */
286                 spin_lock(&imp->imp_lock);
287                 imp->imp_deactive = 1;
288                 spin_unlock(&imp->imp_lock);
289
290                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DEACTIVATE);
291
292                 ptlrpc_invalidate_import(imp);
293         }
294
295         /* When activating, mark import valid, and attempt recovery */
296         if (active) {
297                 CDEBUG(D_HA, "setting import %s VALID\n",
298                        obd2cli_tgt(imp->imp_obd));
299
300                 spin_lock(&imp->imp_lock);
301                 imp->imp_deactive = 0;
302                 spin_unlock(&imp->imp_lock);
303                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_ACTIVATE);
304
305                 rc = ptlrpc_recover_import(imp, NULL, 0);
306         }
307
308         RETURN(rc);
309 }
310 EXPORT_SYMBOL(ptlrpc_set_import_active);
311
312 /* Attempt to reconnect an import */
313 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid, int async)
314 {
315         int rc = 0;
316         ENTRY;
317
318         spin_lock(&imp->imp_lock);
319         if (imp->imp_state == LUSTRE_IMP_NEW || imp->imp_deactive ||
320             atomic_read(&imp->imp_inval_count))
321                 rc = -EINVAL;
322         spin_unlock(&imp->imp_lock);
323         if (rc)
324                 GOTO(out, rc);
325
326         /* force import to be disconnected. */
327         ptlrpc_set_import_discon(imp, 0);
328
329         if (new_uuid) {
330                 struct obd_uuid uuid;
331
332                 /* intruct import to use new uuid */
333                 obd_str2uuid(&uuid, new_uuid);
334                 rc = import_set_conn_priority(imp, &uuid);
335                 if (rc)
336                         GOTO(out, rc);
337         }
338
339         /* Check if reconnect is already in progress */
340         spin_lock(&imp->imp_lock);
341         if (imp->imp_state != LUSTRE_IMP_DISCON) {
342                 imp->imp_force_verify = 1;
343                 rc = -EALREADY;
344         }
345         spin_unlock(&imp->imp_lock);
346         if (rc)
347                 GOTO(out, rc);
348
349         OBD_RACE(OBD_FAIL_PTLRPC_CONNECT_RACE);
350
351         rc = ptlrpc_connect_import(imp);
352         if (rc)
353                 GOTO(out, rc);
354
355         if (!async) {
356                 struct l_wait_info lwi;
357                 long secs = cfs_time_seconds(obd_timeout);
358
359                 CDEBUG(D_HA, "%s: recovery started, waiting %lu seconds\n",
360                        obd2cli_tgt(imp->imp_obd), secs);
361
362                 lwi = LWI_TIMEOUT(secs, NULL, NULL);
363                 rc = l_wait_event(imp->imp_recovery_waitq,
364                                   !ptlrpc_import_in_recovery(imp), &lwi);
365                 CDEBUG(D_HA, "%s: recovery finished\n",
366                        obd2cli_tgt(imp->imp_obd));
367         }
368         EXIT;
369
370 out:
371         return rc;
372 }
373 EXPORT_SYMBOL(ptlrpc_recover_import);
374
375 int ptlrpc_import_in_recovery(struct obd_import *imp)
376 {
377         int in_recovery = 1;
378
379         spin_lock(&imp->imp_lock);
380         if (imp->imp_state == LUSTRE_IMP_FULL ||
381             imp->imp_state == LUSTRE_IMP_CLOSED ||
382             imp->imp_state == LUSTRE_IMP_DISCON ||
383             imp->imp_obd->obd_no_recov)
384                 in_recovery = 0;
385         spin_unlock(&imp->imp_lock);
386
387         return in_recovery;
388 }