Whamcloud - gitweb
LU-14487 modules: remove references to Sun Trademark.
[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  *
31  * lustre/ptlrpc/recover.c
32  *
33  * Author: Mike Shaver <shaver@clusterfs.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_RPC
37 #include <linux/list.h>
38 #include <libcfs/libcfs.h>
39 #include <obd_support.h>
40 #include <lustre_ha.h>
41 #include <lustre_net.h>
42 #include <lustre_import.h>
43 #include <lustre_export.h>
44 #include <obd.h>
45 #include <obd_class.h>
46
47 #include "ptlrpc_internal.h"
48
49 /**
50  * Start recovery on disconnected import.
51  * This is done by just attempting a connect
52  */
53 void ptlrpc_initiate_recovery(struct obd_import *imp)
54 {
55         ENTRY;
56
57         CDEBUG(D_HA, "%s: starting recovery\n", obd2cli_tgt(imp->imp_obd));
58         ptlrpc_connect_import(imp);
59
60         EXIT;
61 }
62
63 /**
64  * Identify what request from replay list needs to be replayed next
65  * (based on what we have already replayed) and send it to server.
66  */
67 int ptlrpc_replay_next(struct obd_import *imp, int *inflight)
68 {
69         int rc = 0;
70         struct ptlrpc_request *req = NULL;
71         __u64 last_transno;
72         ENTRY;
73
74         *inflight = 0;
75
76         /* It might have committed some after we last spoke, so make sure we
77          * get rid of them now.
78          */
79         spin_lock(&imp->imp_lock);
80         imp->imp_last_transno_checked = 0;
81         ptlrpc_free_committed(imp);
82         last_transno = imp->imp_last_replay_transno;
83
84         CDEBUG(D_HA, "import %p from %s committed %llu last %llu\n",
85                imp, obd2cli_tgt(imp->imp_obd),
86                imp->imp_peer_committed_transno, last_transno);
87
88         /* Replay all the committed open requests on committed_list first */
89         if (!list_empty(&imp->imp_committed_list)) {
90                 req = list_last_entry(&imp->imp_committed_list,
91                                       struct ptlrpc_request, rq_replay_list);
92
93                 /* The last request on committed_list hasn't been replayed */
94                 if (req->rq_transno > last_transno) {
95                         if (!imp->imp_resend_replay ||
96                             imp->imp_replay_cursor == &imp->imp_committed_list)
97                                 imp->imp_replay_cursor =
98                                         imp->imp_replay_cursor->next;
99
100                         while (imp->imp_replay_cursor !=
101                                &imp->imp_committed_list) {
102                                 req = list_entry(imp->imp_replay_cursor,
103                                                  struct ptlrpc_request,
104                                                  rq_replay_list);
105                                 if (req->rq_transno > last_transno)
106                                         break;
107
108                                 req = NULL;
109                                 LASSERT(!list_empty(imp->imp_replay_cursor));
110                                 imp->imp_replay_cursor =
111                                         imp->imp_replay_cursor->next;
112                         }
113                 } else {
114                         /* All requests on committed_list have been replayed */
115                         imp->imp_replay_cursor = &imp->imp_committed_list;
116                         req = NULL;
117                 }
118         }
119
120         /* All the requests in committed list have been replayed, let's replay
121          * the imp_replay_list */
122         if (req == NULL) {
123                 struct ptlrpc_request *tmp;
124
125                 list_for_each_entry(tmp, &imp->imp_replay_list,
126                                     rq_replay_list) {
127                         if (tmp->rq_transno > last_transno) {
128                                 req = tmp;
129                                 break;
130                         }
131                 }
132         }
133
134         /* If need to resend the last sent transno (because a reconnect
135          * has occurred), then stop on the matching req and send it again.
136          * If, however, the last sent transno has been committed then we
137          * continue replay from the next request. */
138         if (req != NULL && imp->imp_resend_replay)
139                 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
140
141         /* ptlrpc_prepare_replay() may fail to add the reqeust into unreplied
142          * list if the request hasn't been added to replay list then. Another
143          * exception is that resend replay could have been removed from the
144          * unreplied list. */
145         if (req != NULL && list_empty(&req->rq_unreplied_list)) {
146                 DEBUG_REQ(D_HA, req, "resend_replay=%d, last_transno=%llu",
147                           imp->imp_resend_replay, last_transno);
148                 ptlrpc_add_unreplied(req);
149                 imp->imp_known_replied_xid = ptlrpc_known_replied_xid(imp);
150         }
151
152         imp->imp_resend_replay = 0;
153         spin_unlock(&imp->imp_lock);
154
155         if (req != NULL) {
156                 LASSERT(!list_empty(&req->rq_unreplied_list));
157
158                 rc = ptlrpc_replay_req(req);
159                 if (rc) {
160                         CERROR("recovery replay error %d for req %llu\n",
161                                rc, req->rq_xid);
162                         RETURN(rc);
163                 }
164                 *inflight = 1;
165         }
166         RETURN(rc);
167 }
168
169 /**
170  * Schedule resending of request on sending_list. This is done after
171  * we completed replaying of requests and locks.
172  */
173 int ptlrpc_resend(struct obd_import *imp)
174 {
175         struct ptlrpc_request *req;
176
177         ENTRY;
178
179         /* As long as we're in recovery, nothing should be added to the sending
180          * list, so we don't need to hold the lock during this iteration and
181          * resend process.
182          */
183         /* Well... what if lctl recover is called twice at the same time?
184          */
185         spin_lock(&imp->imp_lock);
186         if (imp->imp_state != LUSTRE_IMP_RECOVER) {
187                 spin_unlock(&imp->imp_lock);
188                 RETURN(-1);
189         }
190
191         list_for_each_entry(req, &imp->imp_sending_list, rq_list) {
192                 LASSERTF((long)req > PAGE_SIZE && req != LP_POISON,
193                          "req %p bad\n", req);
194                 LASSERTF(req->rq_type != LI_POISON, "req %p freed\n", req);
195
196                 /* If the request is allowed to be sent during replay and it
197                  * is not timeout yet, then it does not need to be resent. */
198                 if (!ptlrpc_no_resend(req) &&
199                     (req->rq_timedout || !req->rq_allow_replay))
200                         ptlrpc_resend_req(req);
201         }
202         spin_unlock(&imp->imp_lock);
203
204         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT, 2);
205         RETURN(0);
206 }
207
208 /**
209  * Go through all requests in delayed list and wake their threads
210  * for resending
211  */
212 void ptlrpc_wake_delayed(struct obd_import *imp)
213 {
214         struct ptlrpc_request *req;
215
216         spin_lock(&imp->imp_lock);
217         list_for_each_entry(req, &imp->imp_delayed_list, rq_list) {
218                 DEBUG_REQ(D_HA, req, "waking (set %p):", req->rq_set);
219                 ptlrpc_client_wake_req(req);
220         }
221         spin_unlock(&imp->imp_lock);
222 }
223
224 void ptlrpc_request_handle_notconn(struct ptlrpc_request *failed_req)
225 {
226         struct obd_import *imp = failed_req->rq_import;
227         int conn = lustre_msg_get_conn_cnt(failed_req->rq_reqmsg);
228         ENTRY;
229
230         CDEBUG(D_HA, "import %s of %s@%s abruptly disconnected: reconnecting\n",
231                 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
232                 imp->imp_connection->c_remote_uuid.uuid);
233
234         if (ptlrpc_set_import_discon(imp, conn, true)) {
235                 /* to control recovery via lctl {disable|enable}_recovery */
236                 if (imp->imp_deactive == 0)
237                         ptlrpc_connect_import(imp);
238         }
239
240         /* Wait for recovery to complete and resend. If evicted, then
241            this request will be errored out later.*/
242         spin_lock(&failed_req->rq_lock);
243         if (!failed_req->rq_no_resend)
244                 failed_req->rq_resend = 1;
245         spin_unlock(&failed_req->rq_lock);
246
247         EXIT;
248 }
249
250 /**
251  * Administratively active/deactive a client.
252  * This should only be called by the ioctl interface, currently
253  *  - the lctl deactivate and activate commands
254  *  - echo 0/1 >> /proc/osc/XXX/active
255  *  - client umount -f (ll_umount_begin)
256  */
257 int ptlrpc_set_import_active(struct obd_import *imp, int active)
258 {
259         struct obd_device *obd = imp->imp_obd;
260         int rc = 0;
261
262         ENTRY;
263         LASSERT(obd);
264
265         /* When deactivating, mark import invalid, and abort in-flight
266          * requests. */
267         if (!active) {
268                 LCONSOLE_WARN("setting import %s INACTIVE by administrator "
269                               "request\n", obd2cli_tgt(imp->imp_obd));
270
271                 /* set before invalidate to avoid messages about imp_inval
272                  * set without imp_deactive in ptlrpc_import_delay_req */
273                 spin_lock(&imp->imp_lock);
274                 imp->imp_deactive = 1;
275                 spin_unlock(&imp->imp_lock);
276
277                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DEACTIVATE);
278
279                 ptlrpc_invalidate_import(imp);
280         }
281
282         /* When activating, mark import valid, and attempt recovery */
283         if (active) {
284                 CDEBUG(D_HA, "setting import %s VALID\n",
285                        obd2cli_tgt(imp->imp_obd));
286
287                 spin_lock(&imp->imp_lock);
288                 imp->imp_deactive = 0;
289                 spin_unlock(&imp->imp_lock);
290                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_ACTIVATE);
291
292                 rc = ptlrpc_recover_import(imp, NULL, 0);
293         }
294
295         RETURN(rc);
296 }
297 EXPORT_SYMBOL(ptlrpc_set_import_active);
298
299 /* Attempt to reconnect an import */
300 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid, int async)
301 {
302         int rc = 0;
303         ENTRY;
304
305         spin_lock(&imp->imp_lock);
306         if (imp->imp_state == LUSTRE_IMP_NEW || imp->imp_deactive ||
307             atomic_read(&imp->imp_inval_count))
308                 rc = -EINVAL;
309         spin_unlock(&imp->imp_lock);
310         if (rc)
311                 GOTO(out, rc);
312
313         /* force import to be disconnected. */
314         ptlrpc_set_import_discon(imp, 0, false);
315
316         if (new_uuid) {
317                 struct obd_uuid uuid;
318
319                 /* intruct import to use new uuid */
320                 obd_str2uuid(&uuid, new_uuid);
321                 rc = import_set_conn_priority(imp, &uuid);
322                 if (rc)
323                         GOTO(out, rc);
324         }
325
326         /* Check if reconnect is already in progress */
327         spin_lock(&imp->imp_lock);
328         if (imp->imp_state != LUSTRE_IMP_DISCON) {
329                 imp->imp_force_verify = 1;
330                 rc = -EALREADY;
331         }
332         spin_unlock(&imp->imp_lock);
333         if (rc)
334                 GOTO(out, rc);
335
336         OBD_RACE(OBD_FAIL_PTLRPC_CONNECT_RACE);
337
338         rc = ptlrpc_connect_import(imp);
339         if (rc)
340                 GOTO(out, rc);
341
342         if (!async) {
343                 long timeout = cfs_time_seconds(obd_timeout);
344
345                 CDEBUG(D_HA, "%s: recovery started, waiting %u jiffies\n",
346                        obd2cli_tgt(imp->imp_obd), obd_timeout);
347
348                 rc = wait_event_idle_timeout(imp->imp_recovery_waitq,
349                                              !ptlrpc_import_in_recovery(imp),
350                                              timeout);
351                 if (rc == 0)
352                         rc = -ETIMEDOUT;
353                 else
354                         rc = 0;
355                 CDEBUG(D_HA, "%s: recovery finished\n",
356                        obd2cli_tgt(imp->imp_obd));
357         }
358         EXIT;
359
360 out:
361         return rc;
362 }
363 EXPORT_SYMBOL(ptlrpc_recover_import);
364
365 int ptlrpc_import_in_recovery(struct obd_import *imp)
366 {
367         int in_recovery = 1;
368
369         spin_lock(&imp->imp_lock);
370         if (imp->imp_state <= LUSTRE_IMP_DISCON ||
371             imp->imp_state >= LUSTRE_IMP_FULL ||
372             imp->imp_obd->obd_no_recov)
373                 in_recovery = 0;
374         spin_unlock(&imp->imp_lock);
375
376         return in_recovery;
377 }