Whamcloud - gitweb
Send the correct (I think) client-LDLM export address when reconnecting.
[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  * Lustre Light Super operations
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * Copryright (C) 1996 Peter J. Braam <braam@stelias.com>
10  * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
11  * Copryright (C) 1999 Seagate Technology Inc.
12  * Copryright (C) 2001 Mountain View Data, Inc.
13  * Copryright (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_LLITE
22
23 #include <linux/lustre_lite.h>
24 #include <linux/lustre_ha.h>
25
26 int ll_reconnect(struct ptlrpc_connection *conn) 
27 {
28         struct list_head *tmp;
29         int rc = -EINVAL;
30
31         /* XXX c_lock semantics! */
32         conn->c_level = LUSTRE_CONN_CON;
33
34         /* XXX this code MUST be shared with class_obd_connect! */
35         list_for_each(tmp, &conn->c_imports) {
36                 struct obd_import *imp = list_entry(tmp, struct obd_import,
37                                                     imp_chain);
38                 struct obd_device *obd = imp->imp_obd;
39                 struct client_obd *cli = &obd->u.cli;
40                 int rq_opc = (obd->obd_type->typ_ops->o_brw)
41                         ? OST_CONNECT : MDS_CONNECT;
42                 int size[] = { sizeof(cli->cl_target_uuid),
43                                sizeof(obd->obd_uuid) };
44                 char *tmp[] = {cli->cl_target_uuid, obd->obd_uuid };
45                 struct lustre_handle old_hdl;
46                 struct ptlrpc_request *request; 
47                 struct obd_export *ldlmexp;
48
49                 LASSERT(imp->imp_connection == conn);
50                 request = ptlrpc_prep_req(imp, rq_opc, 2, size, tmp);
51                 request->rq_level = LUSTRE_CONN_NEW;
52                 request->rq_replen = lustre_msg_size(0, NULL);
53                 /*
54                  * This address is the export that represents our client-side
55                  * LDLM service (for ASTs).  We should only have one on this
56                  * list, so we 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                 request->rq_reqmsg->addr = (__u64)(unsigned long)ldlmexp;
63                 request->rq_reqmsg->cookie = ldlmexp->exp_cookie;
64                 rc = ptlrpc_queue_wait(request);
65                 rc = ptlrpc_check_status(request, rc);
66                 if (rc) {
67                         CERROR("cannot connect to %s@%s: rc = %d\n",
68                                cli->cl_target_uuid, conn->c_remote_uuid, rc);
69                         ptlrpc_free_req(request);
70                         GOTO(out_disc, rc = -ENOTCONN);
71                 }
72
73                 old_hdl = imp->imp_handle;
74                 imp->imp_handle.addr = request->rq_repmsg->addr;
75                 imp->imp_handle.cookie = request->rq_repmsg->cookie;
76                 CDEBUG(D_HA, "reconnected to %s@%s (%Lx/%Lx, was %Lx/%Lx)!\n",
77                        cli->cl_target_uuid, conn->c_remote_uuid,
78                        imp->imp_handle.addr, imp->imp_handle.cookie,
79                        old_hdl.addr, old_hdl.cookie);
80                 ptlrpc_free_req(request);
81         }
82         conn->c_level = LUSTRE_CONN_RECOVD;
83
84  out_disc:
85         return rc;
86 }
87
88 static int ll_recover_upcall(struct ptlrpc_connection *conn)
89 {
90         char *argv[3];
91         char *envp[3];
92         int rc;
93
94         ENTRY;
95         conn->c_level = LUSTRE_CONN_RECOVD;
96
97         argv[0] = obd_recovery_upcall;
98         argv[1] = conn->c_remote_uuid;
99         argv[2] = NULL;
100
101         envp[0] = "HOME=/";
102         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
103         envp[2] = NULL;
104
105         rc = call_usermodehelper(argv[0], argv, envp);
106         if (rc < 0) {
107                 /*
108                  * Tragically, this will never be run, because call_umh doesn't
109                  * report errors like -ENOENT to its caller.
110                  */
111                 CERROR("Error invoking recovery upcall (%s): %d\n",
112                        obd_recovery_upcall, rc);
113                 CERROR("Check /proc/sys/lustre/recovery_upcall?\n");
114         } else {
115                 CDEBUG(D_HA, "Invoked upcall %s for connection %s\n",
116                        argv[0], argv[1]);
117         }
118         RETURN(rc);
119 }
120
121 static int ll_recover_reconnect(struct ptlrpc_connection *conn)
122 {
123         int rc = 0;
124         struct list_head *tmp, *pos;
125         struct ptlrpc_request *req;
126         ENTRY;
127
128         /* 1. reconnect */
129         rc = ll_reconnect(conn);
130         if (rc)
131                 RETURN(rc);
132         
133         /* 2. walk the request list */
134         spin_lock(&conn->c_lock);
135
136         CDEBUG(D_HA, "connection %p to %s has last_xid "LPD64"\n",
137                conn, conn->c_remote_uuid, conn->c_last_xid);
138
139         list_for_each_safe(tmp, pos, &conn->c_sending_head) { 
140                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
141                 
142                 /* replay what needs to be replayed */
143                 if (req->rq_flags & PTL_RPC_FL_REPLAY) {
144                         CDEBUG(D_HA, "FL_REPLAY: xid "LPD64" op %d @ %d\n",
145                                req->rq_xid, req->rq_reqmsg->opc,
146                                req->rq_import->imp_client->cli_request_portal);
147                         rc = ptlrpc_replay_req(req);
148 #if 0
149 #error We should not hold a spinlock over such a lengthy operation.
150 #error If necessary, drop spinlock, do operation, re-get spinlock, restart loop.
151 #error If we need to avoid re-processint items, then delete them from the list
152 #error as they are replayed and re-add at the tail of this list, so the next
153 #error item to process will always be at the head of the list.
154 #endif
155                         if (rc) {
156                                 CERROR("recovery replay error %d for req %Ld\n",
157                                        rc, req->rq_xid);
158                                 GOTO(out, rc);
159                         }
160                 }
161
162                 /* server has seen req, we have reply: skip */
163                 if ((req->rq_flags & PTL_RPC_FL_REPLIED)  &&
164                     req->rq_xid <= conn->c_last_xid) { 
165                         CDEBUG(D_HA, "REPLIED SKIP: xid "LPD64" op %d @ %d\n",
166                                req->rq_xid, req->rq_reqmsg->opc,
167                                req->rq_import->imp_client->cli_request_portal);
168                         continue;
169                 }
170
171                 /* server has lost req, we have reply: resend, ign reply */
172                 if ((req->rq_flags & PTL_RPC_FL_REPLIED)  &&
173                     req->rq_xid > conn->c_last_xid) { 
174                         CDEBUG(D_HA, "REPLIED RESEND: xid "LPD64" op %d @ %d\n",
175                                req->rq_xid, req->rq_reqmsg->opc,
176                                req->rq_import->imp_client->cli_request_portal);
177                         rc = ptlrpc_replay_req(req); 
178                         if (rc) {
179                                 CERROR("request resend error %d for req %Ld\n", 
180                                        rc, req->rq_xid); 
181                                 GOTO(out, rc);
182                         }
183                 }
184
185                 /* server has seen req, we have lost reply: -ERESTARTSYS */
186                 if ( !(req->rq_flags & PTL_RPC_FL_REPLIED)  &&
187                      req->rq_xid <= conn->c_last_xid) { 
188                         CDEBUG(D_HA, "RESTARTSYS: xid "LPD64" op %d @ %d\n",
189                                req->rq_xid, req->rq_reqmsg->opc,
190                                req->rq_import->imp_client->cli_request_portal);
191                         ptlrpc_restart_req(req);
192                 }
193
194                 /* service has not seen req, no reply: resend */
195                 if ( !(req->rq_flags & PTL_RPC_FL_REPLIED)  &&
196                      req->rq_xid > conn->c_last_xid) {
197                         CDEBUG(D_HA, "RESEND: xid "LPD64" op %d @ %d\n",
198                                req->rq_xid, req->rq_reqmsg->opc,
199                                req->rq_import->imp_client->cli_request_portal);
200                         ptlrpc_resend_req(req);
201                 }
202
203         }
204
205         conn->c_level = LUSTRE_CONN_FULL;
206         recovd_conn_fixed(conn);
207
208         CERROR("recovery complete on conn %p(%s), waking delayed reqs\n",
209                conn, conn->c_remote_uuid);
210         /* Finally, continue what we delayed since recovery started */
211         list_for_each_safe(tmp, pos, &conn->c_delayed_head) { 
212                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
213                 ptlrpc_continue_req(req);
214         }
215
216         EXIT;
217  out:
218         spin_unlock(&conn->c_lock);
219         return rc;
220 }
221
222 static int ll_retry_recovery(struct ptlrpc_connection *conn)
223 {
224         CDEBUG(D_HA, "Recovery has failed on conn %p\n", conn);
225 #if 0
226         /* XXX use a timer, sideshow bob */
227         recovd_conn_fail(conn);
228         /* XXX this is disabled until I fix it so that we don't just keep
229          * XXX retrying in the case of a missing upcall.
230          */
231 #endif
232         return 0;
233 }
234
235 int ll_recover(struct recovd_data *rd, int phase)
236 {
237         struct ptlrpc_connection *conn = class_rd2conn(rd);
238
239         LASSERT(conn);
240         ENTRY;
241
242         switch (phase) {
243             case PTLRPC_RECOVD_PHASE_PREPARE:
244                 RETURN(ll_recover_upcall(conn));
245             case PTLRPC_RECOVD_PHASE_RECOVER:
246                 RETURN(ll_recover_reconnect(conn));
247             case PTLRPC_RECOVD_PHASE_FAILURE:
248                 RETURN(ll_retry_recovery(conn));
249         }
250
251         LBUG();
252         RETURN(-ENOSYS);
253 }