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