Whamcloud - gitweb
76469cb4de52ca8f46dcccec5bb6f11171c0381a
[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/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 int ptlrpc_replay_next(struct obd_import *imp)
125 {
126         int rc = 0;
127         struct list_head *tmp, *pos;
128         struct ptlrpc_request *req;
129         unsigned long flags;
130         __u64 last_transno;
131         int sent_req = 0;
132         ENTRY;
133
134         /* It might have committed some after we last spoke, so make sure we
135          * get rid of them now.
136          */
137         spin_lock_irqsave(&imp->imp_lock, flags);
138         ptlrpc_free_committed(imp);
139         last_transno = imp->imp_last_replay_transno;
140         spin_unlock_irqrestore(&imp->imp_lock, flags);
141
142         CDEBUG(D_HA, "import %p from %s has committed "LPD64"\n",
143                imp, imp->imp_target_uuid.uuid, imp->imp_peer_committed_transno);
144         /* Do I need to hold a lock across this iteration?  We shouldn't be
145          * racing with any additions to the list, because we're in recovery
146          * and are therefore not processing additional requests to add.  Calls
147          * to ptlrpc_free_committed might commit requests, but nothing "newer"
148          * than the one we're replaying (it can't be committed until it's
149          * replayed, and we're doing that here).  l_f_e_safe protects against
150          * problems with the current request being committed, in the unlikely
151          * event of that race.  So, in conclusion, I think that it's safe to
152          * perform this list-walk without the imp_lock held.
153          *
154          * But, the {mdc,osc}_replay_open callbacks both iterate
155          * request lists, and have comments saying they assume the
156          * imp_lock is being held by ptlrpc_replay, but it's not. it's
157          * just a little race...
158          */
159         list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
160                 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
161                 if (req->rq_transno > last_transno) {
162                         /* remove from list so ptlrpcd can send the
163                            req, it should be reinserted after it is
164                            sent and replied.  Perhaps better solution
165                            would be to add req->rq_replay_list so the
166                            req can be saved for replay and still go
167                            through the normal send thread. */
168                         rc = ptlrpc_replay_req(req);
169                         if (rc) {
170                                 CERROR("recovery replay error %d for req "LPD64"\n",
171                                        rc, req->rq_xid);
172                                 RETURN(rc);
173                         }
174                         sent_req = 1;
175                         break;
176                 }
177
178         }
179
180         RETURN(sent_req);
181 }
182
183 int ptlrpc_resend(struct obd_import *imp)
184 {
185         struct list_head *tmp, *pos;
186         struct ptlrpc_request *req;
187         unsigned long flags;
188
189         ENTRY;
190
191         /* As long as we're in recovery, nothing should be added to the sending
192          * list, so we don't need to hold the lock during this iteration and
193          * resend process.
194          */
195         /* Well... what if lctl recover is called twice at the same time?
196          */
197         spin_lock_irqsave(&imp->imp_lock, flags);
198         if (imp->imp_state != LUSTRE_IMP_RECOVER) {
199                 spin_unlock_irqrestore(&imp->imp_lock, flags);
200                 RETURN(-1);
201         }
202         spin_unlock_irqrestore(&imp->imp_lock, flags);
203
204         list_for_each_safe(tmp, pos, &imp->imp_sending_list) {
205                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
206                 ptlrpc_resend_req(req);
207         }
208
209         RETURN(0);
210 }
211
212 void ptlrpc_wake_delayed(struct obd_import *imp)
213 {
214         unsigned long flags;
215         struct list_head *tmp, *pos;
216         struct ptlrpc_request *req;
217
218         spin_lock_irqsave(&imp->imp_lock, flags);
219         list_for_each_safe(tmp, pos, &imp->imp_delayed_list) {
220                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
221
222                 DEBUG_REQ(D_HA, req, "waking (set %p):", req->rq_set);
223                 ptlrpc_wake_client_req(req);
224         }
225         spin_unlock_irqrestore(&imp->imp_lock, flags);
226 }
227
228 inline void ptlrpc_invalidate_import_state(struct obd_import *imp)
229 {
230         struct obd_device *obd = imp->imp_obd;
231         struct ldlm_namespace *ns = obd->obd_namespace;
232
233         ptlrpc_abort_inflight(imp);
234
235         obd_invalidate_import(obd, imp);
236
237         ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
238 }
239
240 void ptlrpc_handle_failed_import(struct obd_import *imp)
241 {
242         ENTRY;
243
244         if (!imp->imp_replayable) {
245                 CDEBUG(D_HA,
246                        "import %s@%s for %s not replayable, deactivating\n",
247                        imp->imp_target_uuid.uuid,
248                        imp->imp_connection->c_remote_uuid.uuid,
249                        imp->imp_obd->obd_name);
250                 ptlrpc_set_import_active(imp, 0);
251         }
252
253         ptlrpc_run_failed_import_upcall(imp);
254         EXIT;
255 }
256
257 void ptlrpc_request_handle_notconn(struct ptlrpc_request *failed_req)
258 {
259         int rc;
260         struct obd_import *imp= failed_req->rq_import;
261         unsigned long flags;
262         ENTRY;
263
264         CDEBUG(D_HA, "import %s of %s@%s abruptly disconnected: reconnecting\n",
265                imp->imp_obd->obd_name,
266                imp->imp_target_uuid.uuid,
267                imp->imp_connection->c_remote_uuid.uuid);
268         
269         ptlrpc_set_import_discon(imp);
270
271         rc = ptlrpc_connect_import(imp, NULL);
272         
273         /* Wait for recovery to complete and resend. If evicted, then
274            this request will be errored out later.*/
275         spin_lock_irqsave(&failed_req->rq_lock, flags);
276         if (!failed_req->rq_no_resend)
277                 failed_req->rq_resend = 1;
278         spin_unlock_irqrestore(&failed_req->rq_lock, flags);
279         
280         EXIT;
281 }
282
283 int ptlrpc_set_import_active(struct obd_import *imp, int active)
284 {
285         struct obd_device *obd = imp->imp_obd;
286         unsigned long flags;
287
288         LASSERT(obd);
289
290         /* When deactivating, mark import invalid, and abort in-flight
291          * requests. */
292         if (!active) {
293                 spin_lock_irqsave(&imp->imp_lock, flags);
294                 /* This is a bit of a hack, but invalidating replayable
295                  * imports makes a temporary reconnect failure into a much more
296                  * ugly -- and hard to remedy -- situation. */
297                 if (!imp->imp_replayable) {
298                         CDEBUG(D_HA, "setting import %s INVALID\n",
299                                imp->imp_target_uuid.uuid);
300                         imp->imp_invalid = 1;
301                 }
302                 imp->imp_generation++;
303                 spin_unlock_irqrestore(&imp->imp_lock, flags);
304                 ptlrpc_invalidate_import_state(imp);
305         }
306
307         /* When activating, mark import valid */
308         if (active) {
309                 CDEBUG(D_HA, "setting import %s VALID\n",
310                        imp->imp_target_uuid.uuid);
311                 spin_lock_irqsave(&imp->imp_lock, flags);
312                 imp->imp_invalid = 0;
313                 spin_unlock_irqrestore(&imp->imp_lock, flags);
314         }
315
316         if (obd->obd_observer)
317                 RETURN(obd_notify(obd->obd_observer, obd, active));
318
319         RETURN(0);
320 }
321
322 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid)
323 {
324         int rc;
325         ENTRY;
326         
327         /* force import to be disconnected. */
328         ptlrpc_set_import_discon(imp);
329         
330         rc = ptlrpc_recover_import_no_retry(imp, new_uuid);
331
332         RETURN(rc);
333 }
334
335 int ptlrpc_import_in_recovery(struct obd_import *imp)
336 {
337         unsigned long flags;
338         int in_recovery = 1;
339         spin_lock_irqsave(&imp->imp_lock, flags);
340         if (imp->imp_state == LUSTRE_IMP_FULL ||
341             imp->imp_state == LUSTRE_IMP_CLOSED ||
342             imp->imp_state == LUSTRE_IMP_DISCON)
343                 in_recovery = 0;
344         spin_unlock_irqrestore(&imp->imp_lock, flags);
345         return in_recovery;
346 }
347
348 static int ptlrpc_recover_import_no_retry(struct obd_import *imp,
349                                           char *new_uuid)
350 {
351         int rc;
352         unsigned long flags;
353         int in_recovery = 0;
354         struct l_wait_info lwi;
355         ENTRY;
356
357         spin_lock_irqsave(&imp->imp_lock, flags);
358         if (imp->imp_state != LUSTRE_IMP_DISCON) {
359                 in_recovery = 1;
360         } 
361         spin_unlock_irqrestore(&imp->imp_lock, flags);
362
363         if (in_recovery == 1)
364                 RETURN(-EALREADY);
365
366         
367         rc = ptlrpc_connect_import(imp, new_uuid);
368         if (rc)
369                 RETURN(rc);
370
371         CDEBUG(D_ERROR, "%s: recovery started, waiting\n", 
372                imp->imp_client->cli_name);
373
374         lwi = LWI_TIMEOUT(MAX(obd_timeout * HZ, 1), NULL, NULL);
375         rc = l_wait_event(imp->imp_recovery_waitq, 
376                           !ptlrpc_import_in_recovery(imp), &lwi);
377         CDEBUG(D_ERROR, "%s: recovery finished\n", 
378                imp->imp_client->cli_name);
379
380         RETURN(rc);
381         
382 }
383
384 void ptlrpc_fail_export(struct obd_export *exp)
385 {
386         int rc, already_failed;
387         unsigned long flags;
388
389         spin_lock_irqsave(&exp->exp_lock, flags);
390         already_failed = exp->exp_failed;
391         exp->exp_failed = 1;
392         spin_unlock_irqrestore(&exp->exp_lock, flags);
393
394         if (already_failed) {
395                 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
396                        exp, exp->exp_client_uuid.uuid);
397                 return;
398         }
399
400         CDEBUG(D_HA, "disconnecting export %p/%s\n",
401                exp, exp->exp_client_uuid.uuid);
402
403         /* Most callers into obd_disconnect are removing their own reference
404          * (request, for example) in addition to the one from the hash table.
405          * We don't have such a reference here, so make one. */
406         class_export_get(exp);
407         rc = obd_disconnect(exp, 0);
408         if (rc)
409                 CERROR("disconnecting export %p failed: %d\n", exp, rc);
410 }