Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[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  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * Copyright (C) 1996 Peter J. Braam <braam@stelias.com>
10  * Copyright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
11  * Copyright (C) 1999 Seagate Technology Inc.
12  * Copyright (C) 2001 Mountain View Data, Inc.
13  * Copyright (C) 2002 Cluster File Systems, Inc.
14  *
15  */
16
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/kmod.h>
20
21 #define DEBUG_SUBSYSTEM S_RPC
22
23 #include <linux/lustre_ha.h>
24 #include <linux/lustre_net.h>
25 #include <linux/obd.h>
26
27 int ptlrpc_reconnect_import(struct obd_import *imp, int rq_opc,
28                             struct ptlrpc_request **reqptr)
29 {
30         struct obd_device *obd = imp->imp_obd;
31         struct client_obd *cli = &obd->u.cli;
32         int size[] = { sizeof(cli->cl_target_uuid), sizeof(obd->obd_uuid) };
33         char *tmp[] = {cli->cl_target_uuid, obd->obd_uuid };
34         struct ptlrpc_connection *conn = imp->imp_connection;
35         struct lustre_handle old_hdl;
36         struct ptlrpc_request *request;
37         struct obd_export *ldlmexp;
38         int rc;
39
40         request = ptlrpc_prep_req(imp, rq_opc, 2, size, tmp);
41         if (!request)
42                 RETURN(-ENOMEM);
43         request->rq_level = LUSTRE_CONN_NEW;
44         request->rq_replen = lustre_msg_size(0, NULL);
45         /*
46          * This address is the export that represents our client-side LDLM
47          * service (for ASTs).  We should only have one on this list, so we
48          * just grab the first one.
49          *
50          * XXX tear down export, call class_obd_connect?
51          */
52         ldlmexp = list_entry(obd->obd_exports.next, struct obd_export,
53                              exp_obd_chain);
54         request->rq_reqmsg->addr = (__u64)(unsigned long)ldlmexp;
55         request->rq_reqmsg->cookie = ldlmexp->exp_cookie;
56         rc = ptlrpc_queue_wait(request);
57         switch (rc) {
58             case EALREADY:
59             case -EALREADY:
60                 /* already connected! */
61                 memset(&old_hdl, 0, sizeof(old_hdl));
62                 if (!memcmp(&old_hdl.addr, &request->rq_repmsg->addr,
63                             sizeof (old_hdl.addr)) &&
64                     !memcmp(&old_hdl.cookie, &request->rq_repmsg->cookie,
65                             sizeof (old_hdl.cookie))) {
66                         CERROR("%s@%s didn't like our handle "LPX64"/"LPX64", failed\n",
67                                cli->cl_target_uuid, conn->c_remote_uuid,
68                                (__u64)(unsigned long)ldlmexp,
69                                ldlmexp->exp_cookie);
70                         GOTO(out_disc, rc = -ENOTCONN);
71                 }
72
73                 old_hdl.addr = request->rq_repmsg->addr;
74                 old_hdl.cookie = request->rq_repmsg->cookie;
75                 if (memcmp(&imp->imp_handle, &old_hdl, sizeof(old_hdl))) {
76                         CERROR("%s@%s changed handle from "LPX64"/"LPX64" to "LPX64"/"LPX64"; "
77                                "copying, but this may foreshadow disaster\n",
78                                cli->cl_target_uuid, conn->c_remote_uuid,
79                                old_hdl.addr, old_hdl.cookie,
80                                imp->imp_handle.addr, imp->imp_handle.cookie);
81                         imp->imp_handle.addr = request->rq_repmsg->addr;
82                         imp->imp_handle.cookie = request->rq_repmsg->cookie;
83                         GOTO(out_disc, rc = EALREADY);
84                 }
85
86                 CERROR("reconnected to %s@%s after partition\n",
87                        cli->cl_target_uuid, conn->c_remote_uuid);
88                 GOTO(out_disc, rc = EALREADY);
89             case 0:
90                 old_hdl = imp->imp_handle;
91                 imp->imp_handle.addr = request->rq_repmsg->addr;
92                 imp->imp_handle.cookie = request->rq_repmsg->cookie;
93                 CERROR("now connected to %s@%s ("LPX64"/"LPX64", was "LPX64"/"LPX64")!\n",
94                        cli->cl_target_uuid, conn->c_remote_uuid,
95                        imp->imp_handle.addr, imp->imp_handle.cookie,
96                        old_hdl.addr, old_hdl.cookie);
97                 GOTO(out_disc, rc = 0);
98             default:
99                 CERROR("cannot connect to %s@%s: rc = %d\n",
100                        cli->cl_target_uuid, conn->c_remote_uuid, rc);
101                 GOTO(out_disc, rc = -ENOTCONN); /* XXX preserve rc? */
102         }
103
104  out_disc:
105         *reqptr = request;
106         return rc;
107 }
108
109 int ptlrpc_run_recovery_upcall(struct ptlrpc_connection *conn)
110 {
111         char *argv[3];
112         char *envp[3];
113         int rc;
114
115         ENTRY;
116         argv[0] = obd_recovery_upcall;
117         argv[1] = conn->c_remote_uuid;
118         argv[2] = NULL;
119
120         envp[0] = "HOME=/";
121         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
122         envp[2] = NULL;
123
124         rc = call_usermodehelper(argv[0], argv, envp);
125         if (rc < 0) {
126                 CERROR("Error invoking recovery upcall %s for %s: %d\n",
127                        argv[0], argv[1], rc);
128                 CERROR("Check /proc/sys/lustre/recovery_upcall?\n");
129         } else {
130                 CERROR("Invoked upcall %s for connection %s\n",
131                        argv[0], argv[1]);
132         }
133
134         /*
135          * We don't want to make this a "failed" recovery, because the system
136          * administrator -- or, perhaps, tester -- may well be able to rescue
137          * things by running the correct upcall.
138          */
139         RETURN(0);
140 }
141
142 int ptlrpc_replay(struct obd_import *imp)
143 {
144         int rc = 0;
145         struct list_head *tmp, *pos;
146         struct ptlrpc_request *req;
147         unsigned long flags;
148         __u64 committed = imp->imp_peer_committed_transno;
149         ENTRY;
150
151         /* It might have committed some after we last spoke, so make sure we
152          * get rid of them now.
153          */
154         spin_lock_irqsave(&imp->imp_lock, flags);
155
156         ptlrpc_free_committed(imp);
157
158         CDEBUG(D_HA, "import %p from %s has committed "LPD64"\n",
159                imp, imp->imp_obd->u.cli.cl_target_uuid, committed);
160
161         list_for_each(tmp, &imp->imp_replay_list) {
162                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
163                 DEBUG_REQ(D_HA, req, "RETAINED: ");
164         }
165
166         list_for_each_safe(tmp, pos, &imp->imp_replay_list) {
167                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
168
169                 DEBUG_REQ(D_HA, req, "REPLAY:");
170
171                 /* XXX locking WRT failure during replay? */
172                 rc = ptlrpc_replay_req(req);
173
174                 if (rc) {
175                         CERROR("recovery replay error %d for req "LPD64"\n",
176                                rc, req->rq_xid);
177                         GOTO(out, rc);
178                 }
179         }
180
181  out:
182         spin_unlock_irqrestore(&imp->imp_lock, flags);
183         return rc;
184 }
185
186 #define NO_RESEND     0 /* No action required. */
187 #define RESEND        1 /* Resend required. */
188 #define RESEND_IGNORE 2 /* Resend, ignore the reply (already saw it). */
189 #define RESTART       3 /* Have to restart the call, sorry! */
190
191 static int resend_type(struct ptlrpc_request *req, __u64 committed)
192 {
193         if (req->rq_transno && req->rq_transno < committed) {
194                 if (req->rq_flags & PTL_RPC_FL_REPLIED) {
195                         /* Saw the reply and it was committed, no biggie. */
196                         DEBUG_REQ(D_HA, req, "NO_RESEND");
197                         return NO_RESEND;
198                 }
199                 /* Request committed, but no reply: have to restart. */
200                 return RESTART;
201         }
202
203         if (req->rq_flags & PTL_RPC_FL_REPLIED) {
204                 /* Saw reply, so resend and ignore new reply. */
205                 return RESEND_IGNORE;
206         }
207
208         /* Didn't see reply either, so resend. */
209         return RESEND;
210
211 }
212
213 int ptlrpc_resend(struct obd_import *imp)
214 {
215         int rc = 0;
216         struct list_head *tmp, *pos;
217         struct ptlrpc_request *req;
218         unsigned long flags;
219         __u64 committed = imp->imp_peer_committed_transno;
220
221         ENTRY;
222
223         spin_lock_irqsave(&imp->imp_lock, flags);
224         list_for_each(tmp, &imp->imp_sending_list) {
225                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
226                 DEBUG_REQ(D_HA, req, "SENDING: ");
227         }
228
229         list_for_each_safe(tmp, pos, &imp->imp_sending_list) {
230                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
231
232                 switch(resend_type(req, committed)) {
233                     case NO_RESEND:
234                         break;
235
236                     case RESTART:
237                         DEBUG_REQ(D_HA, req, "RESTART:");
238                         ptlrpc_restart_req(req);
239                         break;
240
241                     case RESEND_IGNORE:
242                         DEBUG_REQ(D_HA, req, "RESEND_IGNORE:");
243                         rc = ptlrpc_replay_req(req);
244                         if (rc) {
245                                 DEBUG_REQ(D_ERROR, req, "error %d resending:",
246                                           rc);
247                                 ptlrpc_restart_req(req); /* might as well */
248                         }
249                         break;
250
251                     case RESEND:
252                         DEBUG_REQ(D_HA, req, "RESEND:");
253                         ptlrpc_resend_req(req);
254                         break;
255
256                     default:
257                         LBUG();
258                 }
259         }
260
261         spin_unlock_irqrestore(&imp->imp_lock, flags);
262         RETURN(rc);
263 }
264
265 void ptlrpc_wake_delayed(struct obd_import *imp)
266 {
267         unsigned long flags;
268         struct list_head *tmp, *pos;
269         struct ptlrpc_request *req;
270
271         spin_lock_irqsave(&imp->imp_lock, flags);
272         list_for_each_safe(tmp, pos, &imp->imp_delayed_list) {
273                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
274                 DEBUG_REQ(D_HA, req, "waking:");
275                 wake_up(&req->rq_wait_for_rep);
276         }
277         spin_unlock_irqrestore(&imp->imp_lock, flags);
278 }