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