Whamcloud - gitweb
WARNING - if an RPC times out you will crash older UML's.
[fs/lustre-release.git] / lustre / ptlrpc / client.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define EXPORT_SYMTAB
24
25 #define DEBUG_SUBSYSTEM S_RPC
26
27 #include <linux/lustre_net.h>
28
29
30 void ptlrpc_init_client(struct connmgr_obd *mgr, int req_portal,
31                         int rep_portal, struct ptlrpc_client *cl)
32 {
33         memset(cl, 0, sizeof(*cl));
34         cl->cli_ha_mgr = mgr;
35         if (mgr)
36                 connmgr_cli_manage(mgr, cl);
37         cl->cli_obd = NULL;
38         cl->cli_request_portal = req_portal;
39         cl->cli_reply_portal = rep_portal;
40         INIT_LIST_HEAD(&cl->cli_sending_head);
41         INIT_LIST_HEAD(&cl->cli_sent_head);
42         sema_init(&cl->cli_rpc_sem, 32);
43 }
44
45 struct ptlrpc_connection *ptlrpc_uuid_to_connection(char *uuid)
46 {
47         struct ptlrpc_connection *c;
48         struct lustre_peer peer;
49         int err;
50
51         err = kportal_uuid_to_peer(uuid, &peer);
52         if (err != 0) {
53                 CERROR("cannot find peer %s!\n", uuid);
54                 return NULL;
55         }
56
57         c = ptlrpc_get_connection(&peer);
58         if (c)
59                 c->c_epoch++;
60
61         return c;
62 }
63
64 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *conn)
65 {
66         struct ptlrpc_bulk_desc *bulk;
67
68         OBD_ALLOC(bulk, sizeof(*bulk));
69         if (bulk != NULL) {
70                 bulk->b_connection = ptlrpc_connection_addref(conn);
71                 init_waitqueue_head(&bulk->b_waitq);
72         }
73
74         return bulk;
75 }
76
77 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl,
78                                        struct ptlrpc_connection *conn,
79                                        int opcode, int count, int *lengths,
80                                        char **bufs)
81 {
82         struct ptlrpc_request *request;
83         int rc;
84         ENTRY;
85
86         OBD_ALLOC(request, sizeof(*request));
87         if (!request) {
88                 CERROR("request allocation out of memory\n");
89                 RETURN(NULL);
90         }
91
92         rc = lustre_pack_msg(count, lengths, bufs,
93                              &request->rq_reqlen, &request->rq_reqmsg);
94         if (rc) {
95                 CERROR("cannot pack request %d\n", rc);
96                 RETURN(NULL);
97         }
98
99         request->rq_time = CURRENT_TIME;
100         request->rq_type = PTL_RPC_REQUEST;
101         request->rq_connection = ptlrpc_connection_addref(conn);
102
103         request->rq_reqmsg->conn = (__u64)(unsigned long)conn->c_remote_conn;
104         request->rq_reqmsg->token = conn->c_remote_token;
105         request->rq_reqmsg->opc = HTON__u32(opcode);
106         request->rq_reqmsg->type = HTON__u32(request->rq_type);
107
108         spin_lock(&conn->c_lock);
109         request->rq_reqmsg->xid = HTON__u32(++conn->c_xid_out);
110         spin_unlock(&conn->c_lock);
111
112         request->rq_client = cl;
113
114         RETURN(request);
115 }
116
117 void ptlrpc_free_req(struct ptlrpc_request *request)
118 {
119         if (request == NULL)
120                 return;
121
122         if (request->rq_repmsg != NULL)
123                 OBD_FREE(request->rq_repmsg, request->rq_replen);
124
125         ptlrpc_put_connection(request->rq_connection);
126
127         OBD_FREE(request, sizeof(*request));
128 }
129
130 static int ptlrpc_check_reply(struct ptlrpc_request *req)
131 {
132         int rc = 0;
133
134         schedule_timeout(3 * HZ);  /* 3 second timeout */
135         if (req->rq_repmsg != NULL) {
136                 req->rq_flags = PTL_RPC_REPLY;
137                 GOTO(out, rc = 1);
138         }
139
140         if (CURRENT_TIME - req->rq_time >= 3) { 
141                 CERROR("-- REQ TIMEOUT --\n"); 
142                 if (req->rq_client && req->rq_client->cli_ha_mgr)
143                         connmgr_cli_fail(req->rq_client); 
144                 return 0;
145         }
146
147         if (sigismember(&(current->pending.signal), SIGKILL) ||
148             sigismember(&(current->pending.signal), SIGTERM) ||
149             sigismember(&(current->pending.signal), SIGINT)) {
150                 req->rq_flags = PTL_RPC_INTR;
151                 GOTO(out, rc = 1);
152         }
153
154  out:
155         return rc;
156 }
157
158 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
159 {
160         ENTRY;
161
162         if (err != 0) {
163                 CERROR("err is %d\n", err);
164                 RETURN(err);
165         }
166
167         if (req == NULL) {
168                 CERROR("req == NULL\n");
169                 RETURN(-ENOMEM);
170         }
171
172         if (req->rq_repmsg == NULL) {
173                 CERROR("req->rq_repmsg == NULL\n");
174                 RETURN(-ENOMEM);
175         }
176
177         if (req->rq_repmsg->status != 0) {
178                 CERROR("req->rq_repmsg->status is %d\n",
179                        req->rq_repmsg->status);
180                 /* XXX: translate this error from net to host */
181                 RETURN(req->rq_repmsg->status);
182         }
183
184         RETURN(0);
185 }
186
187 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
188 {
189         OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
190         request->rq_reqmsg = NULL;
191         request->rq_reqlen = 0;
192 }
193
194 /* Abort this request and cleanup any resources associated with it. */
195 static int ptlrpc_abort(struct ptlrpc_request *request)
196 {
197         /* First remove the ME for the reply; in theory, this means
198          * that we can tear down the buffer safely. */
199         PtlMEUnlink(request->rq_reply_me_h);
200         OBD_FREE(request->rq_reply_md.start, request->rq_replen);
201         request->rq_repmsg = NULL;
202         request->rq_replen = 0;
203         return 0;
204 }
205
206 int ptlrpc_queue_wait(struct ptlrpc_request *req)
207 {
208         int rc = 0;
209         ENTRY;
210
211         init_waitqueue_head(&req->rq_wait_for_rep);
212
213         rc = ptl_send_rpc(req);
214         if (rc) {
215                 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
216                 ptlrpc_cleanup_request_buf(req);
217                 up(&req->rq_client->cli_rpc_sem);
218                 RETURN(-rc);
219         }
220
221         CDEBUG(D_OTHER, "-- sleeping\n");
222         wait_event_interruptible(req->rq_wait_for_rep, ptlrpc_check_reply(req));
223         CDEBUG(D_OTHER, "-- done\n");
224         ptlrpc_cleanup_request_buf(req);
225         up(&req->rq_client->cli_rpc_sem);
226         if (req->rq_flags == PTL_RPC_INTR) {
227                 /* Clean up the dangling reply buffers */
228                 ptlrpc_abort(req);
229                 GOTO(out, rc = -EINTR);
230         }
231
232         if (req->rq_flags != PTL_RPC_REPLY) {
233                 CERROR("Unknown reason for wakeup\n");
234                 /* XXX Phil - I end up here when I kill obdctl */
235                 ptlrpc_abort(req);
236                 GOTO(out, rc = -EINTR);
237         }
238
239         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
240         if (rc) {
241                 CERROR("unpack_rep failed: %d\n", rc);
242                 GOTO(out, rc);
243         }
244         CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
245
246         if (req->rq_repmsg->status == 0)
247                 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
248                        req->rq_replen, req->rq_repmsg->status);
249
250         EXIT;
251  out:
252         return rc;
253 }