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