Whamcloud - gitweb
- landing of b_hd_cleanup_merge to HEAD.
[fs/lustre-release.git] / lustre / ptlrpc / recover.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Portal-RPC reconnection and replay operations, for use in recovery.
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *   Author: Mike Shaver <shaver@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #define DEBUG_SUBSYSTEM S_RPC
26 #ifdef __KERNEL__
27 # include <linux/config.h>
28 # include <linux/module.h>
29 # include <linux/kmod.h>
30 #else
31 # include <liblustre.h>
32 #endif
33
34 #include <linux/obd_support.h>
35 #include <linux/lustre_ha.h>
36 #include <linux/lustre_net.h>
37 #include <linux/lustre_import.h>
38 #include <linux/lustre_export.h>
39 #include <linux/obd.h>
40 #include <linux/obd_ost.h>
41 #include <linux/obd_class.h>
42 #include <linux/obd_lov.h> /* for IOC_LOV_SET_OSC_ACTIVE */
43
44 #include "ptlrpc_internal.h"
45
46 static int ptlrpc_recover_import_no_retry(struct obd_import *, char *);
47
48 void ptlrpc_run_recovery_over_upcall(struct obd_device *obd)
49 {
50         char *argv[4];
51         char *envp[3];
52         int rc;
53         ENTRY;
54
55         argv[0] = obd_lustre_upcall;
56         argv[1] = "RECOVERY_OVER";
57         argv[2] = obd->obd_uuid.uuid;
58         argv[3] = NULL;
59         
60         envp[0] = "HOME=/";
61         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
62         envp[2] = NULL;
63
64         rc = USERMODEHELPER(argv[0], argv, envp);
65         if (rc < 0) {
66                 CERROR("Error invoking recovery upcall %s %s %s: %d; check "
67                        "/proc/sys/lustre/upcall\n",
68                        argv[0], argv[1], argv[2], rc);
69
70         } else {
71                 CERROR("Invoked upcall %s %s %s\n",
72                        argv[0], argv[1], argv[2]);
73         }
74 }
75
76 void ptlrpc_run_failed_import_upcall(struct obd_import* imp)
77 {
78 #ifdef __KERNEL__
79         unsigned long flags;
80         char *argv[7];
81         char *envp[3];
82         int rc;
83         ENTRY;
84
85         spin_lock_irqsave(&imp->imp_lock, flags);
86         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
87                 spin_unlock_irqrestore(&imp->imp_lock, flags);
88                 EXIT;
89                 return;
90         }
91         spin_unlock_irqrestore(&imp->imp_lock, flags);
92         
93         argv[0] = obd_lustre_upcall;
94         argv[1] = "FAILED_IMPORT";
95         argv[2] = imp->imp_target_uuid.uuid;
96         argv[3] = imp->imp_obd->obd_name;
97         argv[4] = imp->imp_connection->c_remote_uuid.uuid;
98         argv[5] = imp->imp_obd->obd_uuid.uuid;
99         argv[6] = NULL;
100
101         envp[0] = "HOME=/";
102         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
103         envp[2] = NULL;
104
105         rc = USERMODEHELPER(argv[0], argv, envp);
106         if (rc < 0) {
107                 CERROR("Error invoking recovery upcall %s %s %s %s %s: %d; "
108                        "check /proc/sys/lustre/upcall\n",
109                        argv[0], argv[1], argv[2], argv[3], argv[4],rc);
110
111         } else {
112                 CERROR("Invoked upcall %s %s %s %s %s\n",
113                        argv[0], argv[1], argv[2], argv[3], argv[4]);
114         }
115 #else
116         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
117                 EXIT;
118                 return;
119         }
120         ptlrpc_recover_import(imp, NULL);
121 #endif
122 }
123
124 /* This might block waiting for the upcall to start, so it should
125  * not be called from a thread that shouldn't block. (Like ptlrpcd) */
126 void ptlrpc_initiate_recovery(struct obd_import *imp)
127 {
128         ENTRY;
129
130         LASSERT (obd_lustre_upcall != NULL);
131         
132         if (strcmp(obd_lustre_upcall, "DEFAULT") == 0) {
133                 CDEBUG(D_HA, "%s: starting recovery without upcall\n",
134                         imp->imp_target_uuid.uuid);
135                 ptlrpc_connect_import(imp, NULL);
136         } 
137         else if (strcmp(obd_lustre_upcall, "NONE") == 0) {
138                 CDEBUG(D_HA, "%s: recovery disabled\n",
139                         imp->imp_target_uuid.uuid);
140         } 
141         else {
142                 CDEBUG(D_HA, "%s: calling upcall to start recovery\n",
143                         imp->imp_target_uuid.uuid);
144                 ptlrpc_run_failed_import_upcall(imp);
145         }
146
147         EXIT;
148 }
149
150 int ptlrpc_replay_next(struct obd_import *imp, int *inflight)
151 {
152         int rc = 0;
153         struct list_head *tmp, *pos;
154         struct ptlrpc_request *req = NULL;
155         unsigned long flags;
156         __u64 last_transno;
157         ENTRY;
158
159         *inflight = 0;
160
161         /* It might have committed some after we last spoke, so make sure we
162          * get rid of them now.
163          */
164         spin_lock_irqsave(&imp->imp_lock, flags);
165         ptlrpc_free_committed(imp);
166         last_transno = imp->imp_last_replay_transno;
167         spin_unlock_irqrestore(&imp->imp_lock, flags);
168
169         CDEBUG(D_HA, "import %p from %s committed "LPU64" last "LPU64"\n",
170                imp, imp->imp_target_uuid.uuid, imp->imp_peer_committed_transno,
171                last_transno);
172
173         /* Do I need to hold a lock across this iteration?  We shouldn't be
174          * racing with any additions to the list, because we're in recovery
175          * and are therefore not processing additional requests to add.  Calls
176          * to ptlrpc_free_committed might commit requests, but nothing "newer"
177          * than the one we're replaying (it can't be committed until it's
178          * replayed, and we're doing that here).  l_f_e_safe protects against
179          * problems with the current request being committed, in the unlikely
180          * event of that race.  So, in conclusion, I think that it's safe to
181          * perform this list-walk without the imp_lock held.
182          *
183          * But, the {mdc,osc}_replay_open callbacks both iterate
184          * request lists, and have comments saying they assume the
185          * imp_lock is being held by ptlrpc_replay, but it's not. it's
186          * just a little race...
187          */
188         list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
189                 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
190
191                 /* If need to resend, stop on the matching one first. It's 
192                    possible though it's already been committed, so in that case 
193                    we'll just continue with replay */
194                 if (imp->imp_resend_replay && 
195                     req->rq_transno == last_transno) {
196                         lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
197                         break;
198                 }
199
200                 if (req->rq_transno > last_transno) {
201                         imp->imp_last_replay_transno = req->rq_transno;
202                         break;
203                 }
204
205                 req = NULL;
206         }
207
208         imp->imp_resend_replay = 0;
209
210         if (req != NULL) {
211                 rc = ptlrpc_replay_req(req);
212                 if (rc) {
213                         CERROR("recovery replay error %d for req "
214                                LPD64"\n", rc, req->rq_xid);
215                         RETURN(rc);
216                 }
217                 *inflight = 1;
218         }
219         RETURN(rc);
220 }
221
222 int ptlrpc_resend(struct obd_import *imp)
223 {
224         struct list_head *tmp, *pos;
225         struct ptlrpc_request *req;
226         unsigned long flags;
227
228         ENTRY;
229
230         /* As long as we're in recovery, nothing should be added to the sending
231          * list, so we don't need to hold the lock during this iteration and
232          * resend process.
233          */
234         /* Well... what if lctl recover is called twice at the same time?
235          */
236         spin_lock_irqsave(&imp->imp_lock, flags);
237         if (imp->imp_state != LUSTRE_IMP_RECOVER) {
238                 spin_unlock_irqrestore(&imp->imp_lock, flags);
239                 RETURN(-1);
240         }
241         spin_unlock_irqrestore(&imp->imp_lock, flags);
242
243         list_for_each_safe(tmp, pos, &imp->imp_sending_list) {
244                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
245                 ptlrpc_resend_req(req);
246         }
247
248         RETURN(0);
249 }
250
251 void ptlrpc_wake_delayed(struct obd_import *imp)
252 {
253         unsigned long flags;
254         struct list_head *tmp, *pos;
255         struct ptlrpc_request *req;
256
257         spin_lock_irqsave(&imp->imp_lock, flags);
258         list_for_each_safe(tmp, pos, &imp->imp_delayed_list) {
259                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
260
261                 DEBUG_REQ(D_HA, req, "waking (set %p):", req->rq_set);
262                 ptlrpc_wake_client_req(req);
263         }
264         spin_unlock_irqrestore(&imp->imp_lock, flags);
265 }
266
267 void ptlrpc_request_handle_notconn(struct ptlrpc_request *failed_req)
268 {
269         int rc;
270         struct obd_import *imp= failed_req->rq_import;
271         unsigned long flags;
272         ENTRY;
273
274         CDEBUG(D_HA, "import %s of %s@%s abruptly disconnected: reconnecting\n",
275                imp->imp_obd->obd_name,
276                imp->imp_target_uuid.uuid,
277                imp->imp_connection->c_remote_uuid.uuid);
278         
279         if (ptlrpc_set_import_discon(imp)) {
280                 if (!imp->imp_replayable) {
281                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
282                                "auto-deactivating\n",
283                                imp->imp_target_uuid.uuid,
284                                imp->imp_connection->c_remote_uuid.uuid,
285                                imp->imp_obd->obd_name);
286                         ptlrpc_deactivate_import(imp);
287                 }
288
289                 rc = ptlrpc_connect_import(imp, NULL);
290         }
291
292         
293         /* Wait for recovery to complete and resend. If evicted, then
294            this request will be errored out later.*/
295         spin_lock_irqsave(&failed_req->rq_lock, flags);
296         failed_req->rq_resend = 1;
297         spin_unlock_irqrestore(&failed_req->rq_lock, flags);
298         
299         EXIT;
300 }
301
302 /*
303  * This should only be called by the ioctl interface, currently
304  * with the lctl deactivate and activate commands.
305  */
306 int ptlrpc_set_import_active(struct obd_import *imp, int active)
307 {
308         struct obd_device *obd = imp->imp_obd;
309         int rc = 0;
310
311         LASSERT(obd);
312
313         /* When deactivating, mark import invalid, and abort in-flight
314          * requests. */
315         if (!active) {
316                 ptlrpc_invalidate_import(imp, 0);
317         } 
318
319         /* When activating, mark import valid, and attempt recovery */
320         if (active) {
321                 CDEBUG(D_HA, "setting import %s VALID\n",
322                        imp->imp_target_uuid.uuid);
323                 rc = ptlrpc_recover_import(imp, NULL);
324         }
325
326         RETURN(rc);
327 }
328
329 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid)
330 {
331         int rc;
332         ENTRY;
333         
334         /* force import to be disconnected. */
335         ptlrpc_set_import_discon(imp);
336         
337         rc = ptlrpc_recover_import_no_retry(imp, new_uuid);
338
339         RETURN(rc);
340 }
341
342 int ptlrpc_import_in_recovery(struct obd_import *imp)
343 {
344         unsigned long flags;
345         int in_recovery = 1;
346         spin_lock_irqsave(&imp->imp_lock, flags);
347         if (imp->imp_state == LUSTRE_IMP_FULL ||
348             imp->imp_state == LUSTRE_IMP_CLOSED ||
349             imp->imp_state == LUSTRE_IMP_DISCON)
350                 in_recovery = 0;
351         spin_unlock_irqrestore(&imp->imp_lock, flags);
352         return in_recovery;
353 }
354
355 static int ptlrpc_recover_import_no_retry(struct obd_import *imp,
356                                           char *new_uuid)
357 {
358         int rc;
359         unsigned long flags;
360         int in_recovery = 0;
361         struct l_wait_info lwi;
362         ENTRY;
363
364         spin_lock_irqsave(&imp->imp_lock, flags);
365         if (imp->imp_state != LUSTRE_IMP_DISCON) {
366                 in_recovery = 1;
367         }
368         spin_unlock_irqrestore(&imp->imp_lock, flags);
369
370         if (in_recovery == 1)
371                 RETURN(-EALREADY);
372
373         rc = ptlrpc_connect_import(imp, new_uuid);
374         if (rc)
375                 RETURN(rc);
376
377         CDEBUG(D_HA, "%s: recovery started, waiting\n",
378                imp->imp_target_uuid.uuid);
379
380         lwi = LWI_TIMEOUT(MAX(obd_timeout * HZ, 1), NULL, NULL);
381         rc = l_wait_event(imp->imp_recovery_waitq,
382                           !ptlrpc_import_in_recovery(imp), &lwi);
383         CDEBUG(D_HA, "%s: recovery finished\n",
384                imp->imp_target_uuid.uuid);
385
386         RETURN(rc);
387 }
388
389 void ptlrpc_fail_export(struct obd_export *exp)
390 {
391         int rc, already_failed;
392         unsigned long flags;
393
394         spin_lock_irqsave(&exp->exp_lock, flags);
395         already_failed = exp->exp_failed;
396         exp->exp_failed = 1;
397         spin_unlock_irqrestore(&exp->exp_lock, flags);
398
399         if (already_failed) {
400                 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
401                        exp, exp->exp_client_uuid.uuid);
402                 return;
403         }
404
405         CDEBUG(D_HA, "disconnecting export %p/%s\n",
406                exp, exp->exp_client_uuid.uuid);
407
408         if (obd_dump_on_timeout)
409                 portals_debug_dumplog();
410
411         /* Most callers into obd_disconnect are removing their own reference
412          * (request, for example) in addition to the one from the hash table.
413          * We don't have such a reference here, so make one. */
414         class_export_get(exp);
415         rc = obd_disconnect(exp, 0);
416         if (rc)
417                 CERROR("disconnecting export %p failed: %d\n", exp, rc);
418 }