Whamcloud - gitweb
a90df0ebb2946dd79fe5893812f5da1eb6220662
[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_class.h>
41 #include <linux/obd_lov.h> /* for IOC_LOV_SET_OSC_ACTIVE */
42
43 #include "ptlrpc_internal.h"
44
45 int ptlrpc_reconnect_import(struct obd_import *imp,
46                             struct ptlrpc_request **reqptr)
47 {
48         struct obd_device *obd = imp->imp_obd;
49         int flags, rc, size[] = {sizeof(imp->imp_target_uuid),
50                                  sizeof(obd->obd_uuid),
51                                  sizeof(imp->imp_dlm_handle)};
52         char *tmp[] = {imp->imp_target_uuid.uuid,
53                        obd->obd_uuid.uuid,
54                        (char *)&imp->imp_dlm_handle};
55         struct ptlrpc_connection *conn = imp->imp_connection;
56         struct ptlrpc_request *req;
57         struct lustre_handle old_hdl;
58
59         spin_lock_irqsave(&imp->imp_lock, flags);
60         imp->imp_generation++;
61         spin_unlock_irqrestore(&imp->imp_lock, flags);
62
63         CERROR("reconnect handle "LPX64"\n", 
64                imp->imp_dlm_handle.cookie);
65
66         req = ptlrpc_prep_req(imp, imp->imp_connect_op, 3, size, tmp);
67         if (!req)
68                 RETURN(-ENOMEM);
69         req->rq_level = LUSTRE_CONN_NEW;
70         req->rq_replen = lustre_msg_size(0, NULL);
71         rc = ptlrpc_queue_wait(req);
72         if (rc) {
73                 CERROR("cannot connect to %s@%s: rc = %d\n",
74                        imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid, rc);
75                 GOTO(out_disc, rc);
76         }
77
78         if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT) {
79                 memset(&old_hdl, 0, sizeof(old_hdl));
80                 if (!memcmp(&old_hdl, &req->rq_repmsg->handle,
81                             sizeof (old_hdl))) {
82                         CERROR("%s@%s didn't like our handle "LPX64
83                                ", failed\n", imp->imp_target_uuid.uuid,
84                                conn->c_remote_uuid.uuid,
85                                imp->imp_dlm_handle.cookie);
86                         GOTO(out_disc, rc = -ENOTCONN);
87                 }
88
89                 if (memcmp(&imp->imp_remote_handle, &req->rq_repmsg->handle, 
90                            sizeof(imp->imp_remote_handle))) {
91                         CERROR("%s@%s changed handle from "LPX64" to "LPX64
92                                "; copying, but this may foreshadow disaster\n",
93                                imp->imp_target_uuid.uuid,
94                                conn->c_remote_uuid.uuid,
95                                imp->imp_remote_handle.cookie,
96                                req->rq_repmsg->handle.cookie);
97                         imp->imp_remote_handle = req->rq_repmsg->handle;
98                         GOTO(out_disc, rc = 0);
99                 }
100
101                 CERROR("reconnected to %s@%s after partition\n",
102                        imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid);
103                 GOTO(out_disc, rc = 0);
104         }
105
106         old_hdl = imp->imp_remote_handle;
107         imp->imp_remote_handle = req->rq_repmsg->handle;
108         CERROR("reconnected to %s@%s ("LPX64", was "LPX64")!\n",
109                imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid,
110                imp->imp_remote_handle.cookie, old_hdl.cookie);
111         GOTO(out_disc, rc = 0);
112
113  out_disc:
114         *reqptr = req;
115         return rc;
116 }
117
118 void ptlrpc_run_recovery_over_upcall(struct obd_device *obd)
119 {
120         char *argv[4];
121         char *envp[3];
122         int rc;
123
124         ENTRY;
125         argv[0] = obd_lustre_upcall;
126         argv[1] = "RECOVERY_OVER";
127         argv[2] = obd->obd_uuid.uuid;
128         argv[3] = NULL;
129
130         envp[0] = "HOME=/";
131         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
132         envp[2] = NULL;
133
134         rc = USERMODEHELPER(argv[0], argv, envp);
135         if (rc < 0) {
136                 CERROR("Error invoking recovery upcall %s %s %s: %d; check "
137                        "/proc/sys/lustre/upcall\n",                
138                        argv[0], argv[1], argv[2], rc);
139                 
140         } else {
141                 CERROR("Invoked upcall %s %s %s",
142                        argv[0], argv[1], argv[2]);
143         }
144 }
145
146 void ptlrpc_run_failed_import_upcall(struct obd_import* imp)
147 {
148         char *argv[6];
149         char *envp[3];
150         int rc;
151
152         ENTRY;
153         argv[0] = obd_lustre_upcall;
154         argv[1] = "FAILED_IMPORT";
155         argv[2] = imp->imp_target_uuid.uuid;
156         argv[3] = imp->imp_obd->obd_uuid.uuid;
157         argv[4] = imp->imp_connection->c_remote_uuid.uuid;
158         argv[5] = NULL;
159
160         envp[0] = "HOME=/";
161         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
162         envp[2] = NULL;
163
164         rc = USERMODEHELPER(argv[0], argv, envp);
165         if (rc < 0) {
166                 CERROR("Error invoking recovery upcall %s %s %s %s %s: %d; check "
167                        "/proc/sys/lustre/lustre_upcall\n",                
168                        argv[0], argv[1], argv[2], argv[3], argv[4],rc);
169                 
170         } else {
171                 CERROR("Invoked upcall %s %s %s %s %s\n",
172                        argv[0], argv[1], argv[2], argv[3], argv[4]);
173         }
174 }
175
176 int ptlrpc_replay(struct obd_import *imp)
177 {
178         int rc = 0;
179         struct list_head *tmp, *pos;
180         struct ptlrpc_request *req;
181         unsigned long flags;
182         __u64 committed = imp->imp_peer_committed_transno;
183         ENTRY;
184
185         /* It might have committed some after we last spoke, so make sure we
186          * get rid of them now.
187          */
188         spin_lock_irqsave(&imp->imp_lock, flags);
189         ptlrpc_free_committed(imp);
190         spin_unlock_irqrestore(&imp->imp_lock, flags);
191
192         CDEBUG(D_HA, "import %p from %s has committed "LPD64"\n",
193                imp, imp->imp_target_uuid.uuid, committed);
194
195         list_for_each(tmp, &imp->imp_replay_list) {
196                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
197                 DEBUG_REQ(D_HA, req, "RETAINED: ");
198         }
199
200         /* Do I need to hold a lock across this iteration?  We shouldn't be
201          * racing with any additions to the list, because we're in recovery
202          * and are therefore not processing additional requests to add.  Calls
203          * to ptlrpc_free_committed might commit requests, but nothing "newer"
204          * than the one we're replaying (it can't be committed until it's
205          * replayed, and we're doing that here).  l_f_e_safe protects against
206          * problems with the current request being committed, in the unlikely
207          * event of that race.  So, in conclusion, I think that it's safe to 
208          * perform this list-walk without the imp_lock held.
209          *
210          * But, the {mdc,osc}_replay_open callbacks both iterate
211          * request lists, and have comments saying they assume the
212          * imp_lock is being held by ptlrpc_replay, but it's not. it's
213          * just a little race...
214          */
215         list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
216                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
217
218                 DEBUG_REQ(D_HA, req, "REPLAY:");
219
220                 rc = ptlrpc_replay_req(req);
221         
222                 if (rc) {
223                         CERROR("recovery replay error %d for req "LPD64"\n",
224                                rc, req->rq_xid);
225                         RETURN(rc);
226                 }
227         }
228
229         RETURN(0);
230 }
231
232 int ptlrpc_resend(struct obd_import *imp)
233 {
234         struct list_head *tmp, *pos;
235         struct ptlrpc_request *req;
236         unsigned long flags;
237
238         ENTRY;
239
240         /* As long as we're in recovery, nothing should be added to the sending
241          * list, so we don't need to hold the lock during this iteration and
242          * resend process.
243          */
244         spin_lock_irqsave(&imp->imp_lock, flags);
245         LASSERT(imp->imp_level < LUSTRE_CONN_FULL);
246         spin_unlock_irqrestore(&imp->imp_lock, flags);
247
248         list_for_each_safe(tmp, pos, &imp->imp_sending_list) {
249                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
250                 ptlrpc_resend_req(req);
251         }
252
253         RETURN(0);
254 }
255
256 void ptlrpc_wake_delayed(struct obd_import *imp)
257 {
258         unsigned long flags;
259         struct list_head *tmp, *pos;
260         struct ptlrpc_request *req;
261
262         spin_lock_irqsave(&imp->imp_lock, flags);
263         list_for_each_safe(tmp, pos, &imp->imp_delayed_list) {
264                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
265
266                 ptlrpc_put_connection(req->rq_connection);
267                 req->rq_connection =
268                        ptlrpc_connection_addref(req->rq_import->imp_connection);
269
270                 if (req->rq_set) {
271                         DEBUG_REQ(D_HA, req, "waking (set %p):", req->rq_set);
272                         wake_up(&req->rq_set->set_waitq);
273                 } else {
274                         DEBUG_REQ(D_HA, req, "waking:");
275                         wake_up(&req->rq_wait_for_rep);
276                 }
277         }
278         spin_unlock_irqrestore(&imp->imp_lock, flags);
279 }
280
281 inline void ptlrpc_invalidate_import_state(struct obd_import *imp)
282 {
283         struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
284         if (ptlrpc_ldlm_namespace_cleanup == NULL)
285                 CERROR("ptlrpc/ldlm hook is NULL!  Please tell phil\n");
286         else
287                 ptlrpc_ldlm_namespace_cleanup(ns, 1 /* no network ops */);
288         ptlrpc_abort_inflight(imp);
289 }
290
291 int ptlrpc_request_handle_eviction(struct ptlrpc_request *failed_req)
292 {
293         int rc = 0, in_recovery = 0;
294         struct obd_import *imp= failed_req->rq_import;
295         unsigned long flags;
296         struct ptlrpc_request *req;
297
298         spin_lock_irqsave(&imp->imp_lock, flags);
299
300         if (imp->imp_level == LUSTRE_CONN_NOTCONN)
301                 in_recovery = 1;
302
303         if (failed_req->rq_import_generation == imp->imp_generation)
304                 imp->imp_level = LUSTRE_CONN_NOTCONN;
305         else
306                 in_recovery = 1;
307
308         spin_unlock_irqrestore(&imp->imp_lock, flags);
309
310         if (in_recovery) {
311                 ptlrpc_resend_req(failed_req);
312                 RETURN(rc);
313         }
314
315         CDEBUG(D_HA, "import %s of %s@%s evicted: reconnecting\n",
316                imp->imp_obd->obd_name,
317                imp->imp_target_uuid.uuid,
318                imp->imp_connection->c_remote_uuid.uuid);
319         rc = ptlrpc_reconnect_import(imp, &req);
320         if (rc) {
321                 ptlrpc_resend_req(failed_req);
322                 ptlrpc_fail_import(imp, imp->imp_generation);
323         } else {
324                 spin_lock_irqsave (&failed_req->rq_lock, flags);
325                 failed_req->rq_err = 1;
326                 spin_unlock_irqrestore (&failed_req->rq_lock, flags);
327                 spin_lock_irqsave(&imp->imp_lock, flags);
328                 imp->imp_level = LUSTRE_CONN_FULL;
329                 imp->imp_invalid = 0;
330                 spin_unlock_irqrestore(&imp->imp_lock, flags);
331                 ptlrpc_invalidate_import_state(imp/*, req->rq_import_generation*/);
332         }
333         ptlrpc_req_finished(req);
334         RETURN(rc);
335 }
336
337 int ptlrpc_set_import_active(struct obd_import *imp, int active)
338 {
339         struct obd_device *notify_obd;
340         unsigned long flags;
341         int rc;
342
343         LASSERT(imp->imp_obd);
344
345         notify_obd = imp->imp_obd->u.cli.cl_containing_lov;
346
347         /* When deactivating, mark import invalid, and 
348            abort in-flight requests. */
349         if (!active) {
350                 spin_lock_irqsave(&imp->imp_lock, flags);
351                 imp->imp_invalid = 1;
352                 spin_unlock_irqrestore(&imp->imp_lock, flags);
353
354                 ptlrpc_abort_inflight(imp);
355         } 
356
357         imp->imp_invalid = !active;
358
359         if (notify_obd == NULL)
360                 GOTO(out, rc = 0);
361
362         /* How gross is _this_? */
363         if (!list_empty(&notify_obd->obd_exports)) {
364                 struct lustre_handle fakeconn;
365                 struct obd_ioctl_data ioc_data = { 0 };
366                 struct obd_export *exp =
367                         list_entry(notify_obd->obd_exports.next,
368                                    struct obd_export, exp_obd_chain);
369
370                 fakeconn.cookie = exp->exp_handle.h_cookie;
371                 ioc_data.ioc_inlbuf1 = (char *)&imp->imp_target_uuid;
372                 ioc_data.ioc_offset = active;
373                 rc = obd_iocontrol(IOC_LOV_SET_OSC_ACTIVE, &fakeconn,
374                                    sizeof ioc_data, &ioc_data, NULL);
375                 if (rc)
376                         CERROR("error %sabling %s on LOV %p/%s: %d\n",
377                                active ? "en" : "dis",
378                                imp->imp_target_uuid.uuid, notify_obd,
379                                notify_obd->obd_uuid.uuid, rc);
380         } else {
381                 CDEBUG(D_HA, "No exports for obd %p/%s, can't notify about "
382                        "%p\n", notify_obd, notify_obd->obd_uuid.uuid,
383                        imp->imp_obd->obd_uuid.uuid);
384                 rc = -ENOENT;
385         }
386
387 out:
388         /* When activating, mark import valid */
389         if (active) {
390                 spin_lock_irqsave(&imp->imp_lock, flags);
391                 imp->imp_invalid = 0;
392                 spin_unlock_irqrestore(&imp->imp_lock, flags);
393         }
394
395         RETURN(rc);
396 }
397
398 void ptlrpc_fail_import(struct obd_import *imp, int generation)
399 {
400         unsigned long flags;
401         int in_recovery = 0;
402         ENTRY;
403
404         LASSERT (!imp->imp_dlm_fake);
405         
406         /* If we were already in recovery, or if the import's connection to its
407          * service is newer than the failing operation's original attempt, then
408          * we don't want to recover again. */
409         spin_lock_irqsave(&imp->imp_lock, flags);
410
411         if (imp->imp_level == LUSTRE_CONN_RECOVD)
412                 in_recovery = 1;
413
414         if (generation == imp->imp_generation) {
415                 imp->imp_level = LUSTRE_CONN_RECOVD;
416                 imp->imp_generation++;
417         } else {
418                 in_recovery = 1;
419         }
420
421         spin_unlock_irqrestore(&imp->imp_lock, flags);
422
423         if (in_recovery) {
424                 EXIT;
425                 return;
426         }
427
428         if (!imp->imp_replayable) {
429                 CDEBUG(D_HA,
430                        "import %s@%s for %s not replayable, deactivating\n",
431                        imp->imp_target_uuid.uuid,
432                        imp->imp_connection->c_remote_uuid.uuid,
433                        imp->imp_obd->obd_name);
434                 ptlrpc_set_import_active(imp, 0);
435         }
436
437         ptlrpc_run_failed_import_upcall(imp);
438         EXIT;
439 }
440
441 static int signal_completed_replay(struct obd_import *imp)
442 {
443         struct ptlrpc_request *req;
444         int rc;
445         ENTRY;
446
447         req = ptlrpc_prep_req(imp, OBD_PING, 0, NULL, NULL);
448         if (!req)
449                 RETURN(-ENOMEM);
450
451         req->rq_replen = lustre_msg_size(0, NULL);
452         req->rq_level = LUSTRE_CONN_RECOVD;
453         req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
454
455         rc = ptlrpc_queue_wait(req);
456
457         ptlrpc_req_finished(req);
458         RETURN(rc);
459 }
460
461 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid)
462 {
463         int msg_flags = 0, rc;
464         unsigned long flags;
465         struct ptlrpc_request *req;
466         ENTRY;
467
468         spin_lock_irqsave(&imp->imp_lock, flags);
469         if (imp->imp_level == LUSTRE_CONN_FULL) {
470                 imp->imp_level = LUSTRE_CONN_RECOVD;
471                 imp->imp_generation++;
472         }
473         spin_unlock_irqrestore(&imp->imp_lock, flags);
474
475         if (new_uuid) {
476                 struct ptlrpc_connection *conn;
477                 struct obd_uuid uuid;
478                 struct ptlrpc_peer peer;
479                 struct obd_export *dlmexp;
480
481                 obd_str2uuid(&uuid, new_uuid);
482                 if (ptlrpc_uuid_to_peer(&uuid, &peer)) {
483                         CERROR("no connection found for UUID %s\n", new_uuid);
484                         RETURN(-EINVAL);
485                 }
486
487                 conn = ptlrpc_get_connection(&peer, &uuid);
488                 if (!conn)
489                         RETURN(-ENOMEM);
490
491                 CDEBUG(D_HA, "switching import %s/%s from %s to %s\n",
492                        imp->imp_target_uuid.uuid, imp->imp_obd->obd_name,
493                        imp->imp_connection->c_remote_uuid.uuid,
494                        conn->c_remote_uuid.uuid);
495
496                 /* Switch the import's connection and the DLM export's
497                  * connection (which are almost certainly the same, but we
498                  * keep distinct refs just to make things clearer. I think. */
499                 if (imp->imp_connection)
500                         ptlrpc_put_connection(imp->imp_connection);
501                 /* We hand off the ref from ptlrpc_get_connection. */
502                 imp->imp_connection = conn;
503
504                 dlmexp = class_conn2export(&imp->imp_dlm_handle);
505                 if (dlmexp->exp_connection)
506                         ptlrpc_put_connection(dlmexp->exp_connection);
507                 dlmexp->exp_connection = ptlrpc_connection_addref(conn);
508                 class_export_put(dlmexp);
509
510         }
511
512         rc = ptlrpc_reconnect_import(imp, &req);
513
514         if (rc) {
515                 CERROR("failed to reconnect to %s@%s: %d\n",
516                        imp->imp_target_uuid.uuid,
517                        imp->imp_connection->c_remote_uuid.uuid, rc);
518                 RETURN(rc);
519         }
520
521         if (req->rq_repmsg)
522                 msg_flags = lustre_msg_get_op_flags(req->rq_repmsg);
523
524         if (msg_flags & MSG_CONNECT_RECOVERING) {
525                 CDEBUG(D_HA, "replay requested by %s\n",
526                        imp->imp_target_uuid.uuid);
527                 rc = ptlrpc_replay(imp);
528                 if (rc)
529                         GOTO(out, rc);
530
531                 if (ptlrpc_ldlm_replay_locks == NULL)
532                         CERROR("ptlrpc/ldlm hook is NULL!  Please tell phil\n");
533                 else
534                         rc = ptlrpc_ldlm_replay_locks(imp);
535                 if (rc)
536                         GOTO(out, rc);
537
538                 rc = signal_completed_replay(imp);
539                 if (rc)
540                         GOTO(out, rc);
541         } else if (msg_flags & MSG_CONNECT_RECONNECT) {
542                 CDEBUG(D_HA, "reconnected to %s@%s\n",
543                        imp->imp_target_uuid.uuid,
544                        imp->imp_connection->c_remote_uuid.uuid);
545         } else {
546                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
547                        imp->imp_target_uuid.uuid,
548                        imp->imp_connection->c_remote_uuid.uuid);
549                 ptlrpc_invalidate_import_state(imp);
550         }
551
552         rc = ptlrpc_resend(imp);
553
554         spin_lock_irqsave(&imp->imp_lock, flags);
555         imp->imp_level = LUSTRE_CONN_FULL;
556         imp->imp_invalid = 0;
557         spin_unlock_irqrestore(&imp->imp_lock, flags);
558
559         ptlrpc_wake_delayed(imp);
560         EXIT;
561  out:
562         ptlrpc_req_finished(req);
563         return rc;
564 }
565
566 void ptlrpc_fail_export(struct obd_export *exp)
567 {
568         int rc, already_failed;
569         struct lustre_handle hdl;
570         unsigned long flags;
571
572         spin_lock_irqsave(&exp->exp_lock, flags);
573         already_failed = exp->exp_failed;
574         exp->exp_failed = 1;
575         spin_unlock_irqrestore(&exp->exp_lock, flags);
576
577         if (already_failed) {
578                 CDEBUG(D_HA, "disconnecting dead export %p/%s; skipping\n",
579                        exp, exp->exp_client_uuid.uuid);
580                 return;
581         }
582
583         CDEBUG(D_HA, "disconnecting export %p/%s\n",
584                exp, exp->exp_client_uuid.uuid);
585         hdl.cookie = exp->exp_handle.h_cookie;
586         rc = obd_disconnect(&hdl, 0);
587         if (rc)
588                 CERROR("disconnecting export %p failed: %d\n", exp, rc);
589 }