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