Merge remote-tracking branch 'asoc/topic/max98371' into asoc-next
[cascardo/linux.git] / drivers / staging / lustre / 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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/recover.c
37  *
38  * Author: Mike Shaver <shaver@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_RPC
42 #include "../../include/linux/libcfs/libcfs.h"
43
44 #include "../include/obd_support.h"
45 #include "../include/lustre_ha.h"
46 #include "../include/lustre_net.h"
47 #include "../include/lustre_import.h"
48 #include "../include/lustre_export.h"
49 #include "../include/obd.h"
50 #include "../include/obd_class.h"
51 #include <linux/list.h>
52
53 #include "ptlrpc_internal.h"
54
55 /**
56  * Start recovery on disconnected import.
57  * This is done by just attempting a connect
58  */
59 void ptlrpc_initiate_recovery(struct obd_import *imp)
60 {
61         CDEBUG(D_HA, "%s: starting recovery\n", obd2cli_tgt(imp->imp_obd));
62         ptlrpc_connect_import(imp);
63 }
64
65 /**
66  * Identify what request from replay list needs to be replayed next
67  * (based on what we have already replayed) and send it to server.
68  */
69 int ptlrpc_replay_next(struct obd_import *imp, int *inflight)
70 {
71         int rc = 0;
72         struct list_head *tmp, *pos;
73         struct ptlrpc_request *req = NULL;
74         __u64 last_transno;
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         spin_unlock(&imp->imp_lock);
86
87         CDEBUG(D_HA, "import %p from %s committed %llu last %llu\n",
88                imp, obd2cli_tgt(imp->imp_obd),
89                imp->imp_peer_committed_transno, last_transno);
90
91         /* Do I need to hold a lock across this iteration?  We shouldn't be
92          * racing with any additions to the list, because we're in recovery
93          * and are therefore not processing additional requests to add.  Calls
94          * to ptlrpc_free_committed might commit requests, but nothing "newer"
95          * than the one we're replaying (it can't be committed until it's
96          * replayed, and we're doing that here).  l_f_e_safe protects against
97          * problems with the current request being committed, in the unlikely
98          * event of that race.  So, in conclusion, I think that it's safe to
99          * perform this list-walk without the imp_lock held.
100          *
101          * But, the {mdc,osc}_replay_open callbacks both iterate
102          * request lists, and have comments saying they assume the
103          * imp_lock is being held by ptlrpc_replay, but it's not. it's
104          * just a little race...
105          */
106
107         /* Replay all the committed open requests on committed_list first */
108         if (!list_empty(&imp->imp_committed_list)) {
109                 tmp = imp->imp_committed_list.prev;
110                 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
111
112                 /* The last request on committed_list hasn't been replayed */
113                 if (req->rq_transno > last_transno) {
114                         /* Since the imp_committed_list is immutable before
115                          * all of it's requests being replayed, it's safe to
116                          * use a cursor to accelerate the search
117                          */
118                         imp->imp_replay_cursor = imp->imp_replay_cursor->next;
119
120                         while (imp->imp_replay_cursor !=
121                                &imp->imp_committed_list) {
122                                 req = list_entry(imp->imp_replay_cursor,
123                                                  struct ptlrpc_request,
124                                                  rq_replay_list);
125                                 if (req->rq_transno > last_transno)
126                                         break;
127
128                                 req = NULL;
129                                 imp->imp_replay_cursor =
130                                         imp->imp_replay_cursor->next;
131                         }
132                 } else {
133                         /* All requests on committed_list have been replayed */
134                         imp->imp_replay_cursor = &imp->imp_committed_list;
135                         req = NULL;
136                 }
137         }
138
139         /* All the requests in committed list have been replayed, let's replay
140          * the imp_replay_list
141          */
142         if (!req) {
143                 list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
144                         req = list_entry(tmp, struct ptlrpc_request,
145                                          rq_replay_list);
146
147                         if (req->rq_transno > last_transno)
148                                 break;
149                         req = NULL;
150                 }
151         }
152
153         /* If need to resend the last sent transno (because a reconnect
154          * has occurred), then stop on the matching req and send it again.
155          * If, however, the last sent transno has been committed then we
156          * continue replay from the next request.
157          */
158         if (req && imp->imp_resend_replay)
159                 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
160
161         spin_lock(&imp->imp_lock);
162         imp->imp_resend_replay = 0;
163         spin_unlock(&imp->imp_lock);
164
165         if (req) {
166                 rc = ptlrpc_replay_req(req);
167                 if (rc) {
168                         CERROR("recovery replay error %d for req %llu\n",
169                                rc, req->rq_xid);
170                         return rc;
171                 }
172                 *inflight = 1;
173         }
174         return rc;
175 }
176
177 /**
178  * Schedule resending of request on sending_list. This is done after
179  * we completed replaying of requests and locks.
180  */
181 int ptlrpc_resend(struct obd_import *imp)
182 {
183         struct ptlrpc_request *req, *next;
184
185         /* As long as we're in recovery, nothing should be added to the sending
186          * list, so we don't need to hold the lock during this iteration and
187          * resend process.
188          */
189         /* Well... what if lctl recover is called twice at the same time?
190          */
191         spin_lock(&imp->imp_lock);
192         if (imp->imp_state != LUSTRE_IMP_RECOVER) {
193                 spin_unlock(&imp->imp_lock);
194                 return -1;
195         }
196
197         list_for_each_entry_safe(req, next, &imp->imp_sending_list, rq_list) {
198                 LASSERTF((long)req > PAGE_SIZE && req != LP_POISON,
199                          "req %p bad\n", req);
200                 LASSERTF(req->rq_type != LI_POISON, "req %p freed\n", req);
201                 if (!ptlrpc_no_resend(req))
202                         ptlrpc_resend_req(req);
203         }
204         spin_unlock(&imp->imp_lock);
205
206         return 0;
207 }
208 EXPORT_SYMBOL(ptlrpc_resend);
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 EXPORT_SYMBOL(ptlrpc_wake_delayed);
229
230 void ptlrpc_request_handle_notconn(struct ptlrpc_request *failed_req)
231 {
232         struct obd_import *imp = failed_req->rq_import;
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, 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);
246                 }
247                 /* to control recovery via lctl {disable|enable}_recovery */
248                 if (imp->imp_deactive == 0)
249                         ptlrpc_connect_import(imp);
250         }
251
252         /* Wait for recovery to complete and resend. If evicted, then
253          * this request will be errored out later.
254          */
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
261 /**
262  * Administratively active/deactive a client.
263  * This should only be called by the ioctl interface, currently
264  *  - the lctl deactivate and activate commands
265  *  - echo 0/1 >> /sys/fs/lustre/osc/XXX/active
266  *  - client umount -f (ll_umount_begin)
267  */
268 int ptlrpc_set_import_active(struct obd_import *imp, int active)
269 {
270         struct obd_device *obd = imp->imp_obd;
271         int rc = 0;
272
273         LASSERT(obd);
274
275         /* When deactivating, mark import invalid, and abort in-flight
276          * requests.
277          */
278         if (!active) {
279                 LCONSOLE_WARN("setting import %s INACTIVE by administrator request\n",
280                               obd2cli_tgt(imp->imp_obd));
281
282                 /* set before invalidate to avoid messages about imp_inval
283                  * set without imp_deactive in ptlrpc_import_delay_req
284                  */
285                 spin_lock(&imp->imp_lock);
286                 imp->imp_deactive = 1;
287                 spin_unlock(&imp->imp_lock);
288
289                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DEACTIVATE);
290
291                 ptlrpc_invalidate_import(imp);
292         }
293
294         /* When activating, mark import valid, and attempt recovery */
295         if (active) {
296                 CDEBUG(D_HA, "setting import %s VALID\n",
297                        obd2cli_tgt(imp->imp_obd));
298
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);
303
304                 rc = ptlrpc_recover_import(imp, NULL, 0);
305         }
306
307         return rc;
308 }
309 EXPORT_SYMBOL(ptlrpc_set_import_active);
310
311 /* Attempt to reconnect an import */
312 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid, int async)
313 {
314         int rc = 0;
315
316         spin_lock(&imp->imp_lock);
317         if (imp->imp_state == LUSTRE_IMP_NEW || imp->imp_deactive ||
318             atomic_read(&imp->imp_inval_count))
319                 rc = -EINVAL;
320         spin_unlock(&imp->imp_lock);
321         if (rc)
322                 goto out;
323
324         /* force import to be disconnected. */
325         ptlrpc_set_import_discon(imp, 0);
326
327         if (new_uuid) {
328                 struct obd_uuid uuid;
329
330                 /* intruct import to use new uuid */
331                 obd_str2uuid(&uuid, new_uuid);
332                 rc = import_set_conn_priority(imp, &uuid);
333                 if (rc)
334                         goto out;
335         }
336
337         /* Check if reconnect is already in progress */
338         spin_lock(&imp->imp_lock);
339         if (imp->imp_state != LUSTRE_IMP_DISCON) {
340                 imp->imp_force_verify = 1;
341                 rc = -EALREADY;
342         }
343         spin_unlock(&imp->imp_lock);
344         if (rc)
345                 goto out;
346
347         rc = ptlrpc_connect_import(imp);
348         if (rc)
349                 goto out;
350
351         if (!async) {
352                 struct l_wait_info lwi;
353                 int secs = cfs_time_seconds(obd_timeout);
354
355                 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
356                        obd2cli_tgt(imp->imp_obd), secs);
357
358                 lwi = LWI_TIMEOUT(secs, NULL, NULL);
359                 rc = l_wait_event(imp->imp_recovery_waitq,
360                                   !ptlrpc_import_in_recovery(imp), &lwi);
361                 CDEBUG(D_HA, "%s: recovery finished\n",
362                        obd2cli_tgt(imp->imp_obd));
363         }
364
365 out:
366         return rc;
367 }
368 EXPORT_SYMBOL(ptlrpc_recover_import);
369
370 int ptlrpc_import_in_recovery(struct obd_import *imp)
371 {
372         int in_recovery = 1;
373
374         spin_lock(&imp->imp_lock);
375         if (imp->imp_state == LUSTRE_IMP_FULL ||
376             imp->imp_state == LUSTRE_IMP_CLOSED ||
377             imp->imp_state == LUSTRE_IMP_DISCON ||
378             imp->imp_obd->obd_no_recov)
379                 in_recovery = 0;
380         spin_unlock(&imp->imp_lock);
381
382         return in_recovery;
383 }