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