Whamcloud - gitweb
- Chain imports on connection, like exports.
[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                 CERROR("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
84         ENTRY;
85         conn->c_level = LUSTRE_CONN_RECOVD;
86
87         argv[0] = obd_recovery_upcall;
88         argv[1] = conn->c_remote_uuid;
89         argv[2] = NULL;
90
91         envp[0] = "HOME=/";
92         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
93         envp[2] = NULL;
94
95         RETURN(call_usermodehelper(argv[0], argv, envp));
96 }
97
98 static int ll_recover_reconnect(struct ptlrpc_connection *conn)
99 {
100         int rc = 0;
101         struct list_head *tmp, *pos;
102         struct ptlrpc_request *req;
103         ENTRY;
104
105         /* 1. reconnect */
106         rc = ll_reconnect(conn);
107         if (rc)
108                 RETURN(rc);
109         
110         /* 2. walk the request list */
111         spin_lock(&conn->c_lock);
112         list_for_each_safe(tmp, pos, &conn->c_sending_head) { 
113                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
114                 
115                 /* replay what needs to be replayed */
116                 if (req->rq_flags & PTL_RPC_FL_REPLAY) {
117                         CDEBUG(D_INODE, "req %Ld needs replay [last rcvd %Ld]\n",
118                                req->rq_xid, conn->c_last_xid);
119                         rc = ptlrpc_replay_req(req);
120 #if 0
121 #error We should not hold a spinlock over such a lengthy operation.
122 #error If necessary, drop spinlock, do operation, re-get spinlock, restart loop.
123 #error If we need to avoid re-processint items, then delete them from the list
124 #error as they are replayed and re-add at the tail of this list, so the next
125 #error item to process will always be at the head of the list.
126 #endif
127                         if (rc) {
128                                 CERROR("recovery replay error %d for req %Ld\n",
129                                        rc, req->rq_xid);
130                                 GOTO(out, rc);
131                         }
132                 }
133
134                 /* server has seen req, we have reply: skip */
135                 if ((req->rq_flags & PTL_RPC_FL_REPLIED)  &&
136                     req->rq_xid <= conn->c_last_xid) { 
137                         CDEBUG(D_INODE,
138                                "req %Ld was complete: skip [last rcvd %Ld]\n", 
139                                req->rq_xid, conn->c_last_xid);
140                         continue;
141                 }
142
143                 /* server has lost req, we have reply: resend, ign reply */
144                 if ((req->rq_flags & PTL_RPC_FL_REPLIED)  &&
145                     req->rq_xid > conn->c_last_xid) { 
146                         CDEBUG(D_INODE, "lost req %Ld have rep: replay [last "
147                                "rcvd %Ld]\n", req->rq_xid, conn->c_last_xid);
148                         rc = ptlrpc_replay_req(req); 
149                         if (rc) {
150                                 CERROR("request resend error %d for req %Ld\n", 
151                                        rc, req->rq_xid); 
152                                 GOTO(out, rc);
153                         }
154                 }
155
156                 /* server has seen req, we have lost reply: -ERESTARTSYS */
157                 if ( !(req->rq_flags & PTL_RPC_FL_REPLIED)  &&
158                      req->rq_xid <= conn->c_last_xid) { 
159                         CDEBUG(D_INODE, "lost rep %Ld srv did req: restart "
160                                "[last rcvd %Ld]\n", 
161                                req->rq_xid, conn->c_last_xid);
162                         ptlrpc_restart_req(req);
163                 }
164
165                 /* service has not seen req, no reply: resend */
166                 if ( !(req->rq_flags & PTL_RPC_FL_REPLIED)  &&
167                      req->rq_xid > conn->c_last_xid) {
168                         CDEBUG(D_INODE,
169                                "lost rep/req %Ld: resend [last rcvd %Ld]\n", 
170                                req->rq_xid, conn->c_last_xid);
171                         ptlrpc_resend_req(req);
172                 }
173
174         }
175
176         conn->c_level = LUSTRE_CONN_FULL;
177         recovd_conn_fixed(conn);
178
179         /* Finally, continue what we delayed since recovery started */
180         list_for_each_safe(tmp, pos, &conn->c_delayed_head) { 
181                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
182                 ptlrpc_continue_req(req);
183         }
184
185         EXIT;
186  out:
187         spin_unlock(&conn->c_lock);
188         return rc;
189 }
190
191 int ll_recover(struct recovd_data *rd, int phase)
192 {
193         struct ptlrpc_connection *conn = class_rd2conn(rd);
194
195         LASSERT(conn);
196         ENTRY;
197
198         switch (phase) {
199             case PTLRPC_RECOVD_PHASE_PREPARE:
200                 RETURN(ll_recover_upcall(conn));
201             case PTLRPC_RECOVD_PHASE_RECOVER:
202                 RETURN(ll_recover_reconnect(conn));
203             case PTLRPC_RECOVD_PHASE_FAILURE:
204                 fixme();
205                 RETURN(0);
206         }
207
208         LBUG();
209         RETURN(-ENOSYS);
210 }