Merge branch 'ras-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.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.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, 2015, 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 "../../include/linux/libcfs/libcfs.h"
39
40 #include "../include/obd_support.h"
41 #include "../include/lustre_ha.h"
42 #include "../include/lustre_net.h"
43 #include "../include/lustre_import.h"
44 #include "../include/lustre_export.h"
45 #include "../include/obd.h"
46 #include "../include/obd_class.h"
47 #include <linux/list.h>
48
49 #include "ptlrpc_internal.h"
50
51 /**
52  * Start recovery on disconnected import.
53  * This is done by just attempting a connect
54  */
55 void ptlrpc_initiate_recovery(struct obd_import *imp)
56 {
57         CDEBUG(D_HA, "%s: starting recovery\n", obd2cli_tgt(imp->imp_obd));
58         ptlrpc_connect_import(imp);
59 }
60
61 /**
62  * Identify what request from replay list needs to be replayed next
63  * (based on what we have already replayed) and send it to server.
64  */
65 int ptlrpc_replay_next(struct obd_import *imp, int *inflight)
66 {
67         int rc = 0;
68         struct list_head *tmp, *pos;
69         struct ptlrpc_request *req = NULL;
70         __u64 last_transno;
71
72         *inflight = 0;
73
74         /* It might have committed some after we last spoke, so make sure we
75          * get rid of them now.
76          */
77         spin_lock(&imp->imp_lock);
78         imp->imp_last_transno_checked = 0;
79         ptlrpc_free_committed(imp);
80         last_transno = imp->imp_last_replay_transno;
81         spin_unlock(&imp->imp_lock);
82
83         CDEBUG(D_HA, "import %p from %s committed %llu last %llu\n",
84                imp, obd2cli_tgt(imp->imp_obd),
85                imp->imp_peer_committed_transno, last_transno);
86
87         /* Do I need to hold a lock across this iteration?  We shouldn't be
88          * racing with any additions to the list, because we're in recovery
89          * and are therefore not processing additional requests to add.  Calls
90          * to ptlrpc_free_committed might commit requests, but nothing "newer"
91          * than the one we're replaying (it can't be committed until it's
92          * replayed, and we're doing that here).  l_f_e_safe protects against
93          * problems with the current request being committed, in the unlikely
94          * event of that race.  So, in conclusion, I think that it's safe to
95          * perform this list-walk without the imp_lock held.
96          *
97          * But, the {mdc,osc}_replay_open callbacks both iterate
98          * request lists, and have comments saying they assume the
99          * imp_lock is being held by ptlrpc_replay, but it's not. it's
100          * just a little race...
101          */
102
103         /* Replay all the committed open requests on committed_list first */
104         if (!list_empty(&imp->imp_committed_list)) {
105                 tmp = imp->imp_committed_list.prev;
106                 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
107
108                 /* The last request on committed_list hasn't been replayed */
109                 if (req->rq_transno > last_transno) {
110                         /* Since the imp_committed_list is immutable before
111                          * all of it's requests being replayed, it's safe to
112                          * use a cursor to accelerate the search
113                          */
114                         imp->imp_replay_cursor = imp->imp_replay_cursor->next;
115
116                         while (imp->imp_replay_cursor !=
117                                &imp->imp_committed_list) {
118                                 req = list_entry(imp->imp_replay_cursor,
119                                                  struct ptlrpc_request,
120                                                  rq_replay_list);
121                                 if (req->rq_transno > last_transno)
122                                         break;
123
124                                 req = NULL;
125                                 imp->imp_replay_cursor =
126                                         imp->imp_replay_cursor->next;
127                         }
128                 } else {
129                         /* All requests on committed_list have been replayed */
130                         imp->imp_replay_cursor = &imp->imp_committed_list;
131                         req = NULL;
132                 }
133         }
134
135         /* All the requests in committed list have been replayed, let's replay
136          * the imp_replay_list
137          */
138         if (!req) {
139                 list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
140                         req = list_entry(tmp, struct ptlrpc_request,
141                                          rq_replay_list);
142
143                         if (req->rq_transno > last_transno)
144                                 break;
145                         req = NULL;
146                 }
147         }
148
149         /* If need to resend the last sent transno (because a reconnect
150          * has occurred), then stop on the matching req and send it again.
151          * If, however, the last sent transno has been committed then we
152          * continue replay from the next request.
153          */
154         if (req && imp->imp_resend_replay)
155                 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
156
157         spin_lock(&imp->imp_lock);
158         imp->imp_resend_replay = 0;
159         spin_unlock(&imp->imp_lock);
160
161         if (req) {
162                 rc = ptlrpc_replay_req(req);
163                 if (rc) {
164                         CERROR("recovery replay error %d for req %llu\n",
165                                rc, req->rq_xid);
166                         return rc;
167                 }
168                 *inflight = 1;
169         }
170         return rc;
171 }
172
173 /**
174  * Schedule resending of request on sending_list. This is done after
175  * we completed replaying of requests and locks.
176  */
177 int ptlrpc_resend(struct obd_import *imp)
178 {
179         struct ptlrpc_request *req, *next;
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                 if (!ptlrpc_no_resend(req))
198                         ptlrpc_resend_req(req);
199         }
200         spin_unlock(&imp->imp_lock);
201
202         return 0;
203 }
204 EXPORT_SYMBOL(ptlrpc_resend);
205
206 /**
207  * Go through all requests in delayed list and wake their threads
208  * for resending
209  */
210 void ptlrpc_wake_delayed(struct obd_import *imp)
211 {
212         struct list_head *tmp, *pos;
213         struct ptlrpc_request *req;
214
215         spin_lock(&imp->imp_lock);
216         list_for_each_safe(tmp, pos, &imp->imp_delayed_list) {
217                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
218
219                 DEBUG_REQ(D_HA, req, "waking (set %p):", req->rq_set);
220                 ptlrpc_client_wake_req(req);
221         }
222         spin_unlock(&imp->imp_lock);
223 }
224 EXPORT_SYMBOL(ptlrpc_wake_delayed);
225
226 void ptlrpc_request_handle_notconn(struct ptlrpc_request *failed_req)
227 {
228         struct obd_import *imp = failed_req->rq_import;
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,
235                               lustre_msg_get_conn_cnt(failed_req->rq_reqmsg))) {
236                 if (!imp->imp_replayable) {
237                         CDEBUG(D_HA, "import %s@%s for %s not replayable, auto-deactivating\n",
238                                obd2cli_tgt(imp->imp_obd),
239                                imp->imp_connection->c_remote_uuid.uuid,
240                                imp->imp_obd->obd_name);
241                         ptlrpc_deactivate_import(imp);
242                 }
243                 /* to control recovery via lctl {disable|enable}_recovery */
244                 if (imp->imp_deactive == 0)
245                         ptlrpc_connect_import(imp);
246         }
247
248         /* Wait for recovery to complete and resend. If evicted, then
249          * this request will be errored out later.
250          */
251         spin_lock(&failed_req->rq_lock);
252         if (!failed_req->rq_no_resend)
253                 failed_req->rq_resend = 1;
254         spin_unlock(&failed_req->rq_lock);
255 }
256
257 /**
258  * Administratively active/deactive a client.
259  * This should only be called by the ioctl interface, currently
260  *  - the lctl deactivate and activate commands
261  *  - echo 0/1 >> /sys/fs/lustre/osc/XXX/active
262  *  - client umount -f (ll_umount_begin)
263  */
264 int ptlrpc_set_import_active(struct obd_import *imp, int active)
265 {
266         struct obd_device *obd = imp->imp_obd;
267         int rc = 0;
268
269         LASSERT(obd);
270
271         /* When deactivating, mark import invalid, and abort in-flight
272          * requests.
273          */
274         if (!active) {
275                 LCONSOLE_WARN("setting import %s INACTIVE by administrator request\n",
276                               obd2cli_tgt(imp->imp_obd));
277
278                 /* set before invalidate to avoid messages about imp_inval
279                  * set without imp_deactive in ptlrpc_import_delay_req
280                  */
281                 spin_lock(&imp->imp_lock);
282                 imp->imp_deactive = 1;
283                 spin_unlock(&imp->imp_lock);
284
285                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DEACTIVATE);
286
287                 ptlrpc_invalidate_import(imp);
288         }
289
290         /* When activating, mark import valid, and attempt recovery */
291         if (active) {
292                 CDEBUG(D_HA, "setting import %s VALID\n",
293                        obd2cli_tgt(imp->imp_obd));
294
295                 spin_lock(&imp->imp_lock);
296                 imp->imp_deactive = 0;
297                 spin_unlock(&imp->imp_lock);
298                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_ACTIVATE);
299
300                 rc = ptlrpc_recover_import(imp, NULL, 0);
301         }
302
303         return rc;
304 }
305 EXPORT_SYMBOL(ptlrpc_set_import_active);
306
307 /* Attempt to reconnect an import */
308 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid, int async)
309 {
310         int rc = 0;
311
312         spin_lock(&imp->imp_lock);
313         if (imp->imp_state == LUSTRE_IMP_NEW || imp->imp_deactive ||
314             atomic_read(&imp->imp_inval_count))
315                 rc = -EINVAL;
316         spin_unlock(&imp->imp_lock);
317         if (rc)
318                 goto out;
319
320         /* force import to be disconnected. */
321         ptlrpc_set_import_discon(imp, 0);
322
323         if (new_uuid) {
324                 struct obd_uuid uuid;
325
326                 /* intruct import to use new uuid */
327                 obd_str2uuid(&uuid, new_uuid);
328                 rc = import_set_conn_priority(imp, &uuid);
329                 if (rc)
330                         goto out;
331         }
332
333         /* Check if reconnect is already in progress */
334         spin_lock(&imp->imp_lock);
335         if (imp->imp_state != LUSTRE_IMP_DISCON) {
336                 imp->imp_force_verify = 1;
337                 rc = -EALREADY;
338         }
339         spin_unlock(&imp->imp_lock);
340         if (rc)
341                 goto out;
342
343         rc = ptlrpc_connect_import(imp);
344         if (rc)
345                 goto out;
346
347         if (!async) {
348                 struct l_wait_info lwi;
349                 int secs = cfs_time_seconds(obd_timeout);
350
351                 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
352                        obd2cli_tgt(imp->imp_obd), secs);
353
354                 lwi = LWI_TIMEOUT(secs, NULL, NULL);
355                 rc = l_wait_event(imp->imp_recovery_waitq,
356                                   !ptlrpc_import_in_recovery(imp), &lwi);
357                 CDEBUG(D_HA, "%s: recovery finished\n",
358                        obd2cli_tgt(imp->imp_obd));
359         }
360
361 out:
362         return rc;
363 }
364 EXPORT_SYMBOL(ptlrpc_recover_import);
365
366 int ptlrpc_import_in_recovery(struct obd_import *imp)
367 {
368         int in_recovery = 1;
369
370         spin_lock(&imp->imp_lock);
371         if (imp->imp_state == LUSTRE_IMP_FULL ||
372             imp->imp_state == LUSTRE_IMP_CLOSED ||
373             imp->imp_state == LUSTRE_IMP_DISCON ||
374             imp->imp_obd->obd_no_recov)
375                 in_recovery = 0;
376         spin_unlock(&imp->imp_lock);
377
378         return in_recovery;
379 }