Whamcloud - gitweb
Use debugging macros to aid in tracing.
[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_ha.h>
28
29 void ptlrpc_init_client(struct recovd_obd *recovd, int req_portal,
30                         int rep_portal, struct ptlrpc_client *cl)
31 {
32         memset(cl, 0, sizeof(*cl));
33         cl->cli_recovd = recovd;
34         if (recovd)
35                 connmgr_cli_manage(recovd, cl);
36         cl->cli_obd = NULL;
37         cl->cli_request_portal = req_portal;
38         cl->cli_reply_portal = rep_portal;
39         INIT_LIST_HEAD(&cl->cli_sending_head);
40         INIT_LIST_HEAD(&cl->cli_sent_head);
41         spin_lock_init(&cl->cli_lock);
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 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk)
78 {
79         if (bulk == NULL)
80                 return;
81
82         ptlrpc_put_connection(bulk->b_connection);
83
84         OBD_FREE(bulk, sizeof(*bulk));
85 }
86
87 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl,
88                                        struct ptlrpc_connection *conn,
89                                        int opcode, int count, int *lengths,
90                                        char **bufs)
91 {
92         struct ptlrpc_request *request;
93         int rc;
94         ENTRY;
95
96         OBD_ALLOC(request, sizeof(*request));
97         if (!request) {
98                 CERROR("request allocation out of memory\n");
99                 RETURN(NULL);
100         }
101
102         rc = lustre_pack_msg(count, lengths, bufs,
103                              &request->rq_reqlen, &request->rq_reqmsg);
104         if (rc) {
105                 CERROR("cannot pack request %d\n", rc);
106                 RETURN(NULL);
107         }
108
109         request->rq_time = CURRENT_TIME;
110         request->rq_type = PTL_RPC_TYPE_REQUEST;
111         request->rq_connection = ptlrpc_connection_addref(conn);
112
113         request->rq_reqmsg->conn = (__u64)(unsigned long)conn->c_remote_conn;
114         request->rq_reqmsg->token = conn->c_remote_token;
115         request->rq_reqmsg->opc = HTON__u32(opcode);
116         request->rq_reqmsg->type = HTON__u32(PTL_RPC_MSG_REQUEST);
117         INIT_LIST_HEAD(&request->rq_list);
118
119         spin_lock(&conn->c_lock);
120         request->rq_reqmsg->xid = HTON__u32(++conn->c_xid_out);
121         spin_unlock(&conn->c_lock);
122
123         request->rq_client = cl;
124
125         RETURN(request);
126 }
127
128 void ptlrpc_free_req(struct ptlrpc_request *request)
129 {
130         if (request == NULL)
131                 return;
132
133         if (request->rq_repmsg != NULL)
134                 OBD_FREE(request->rq_repmsg, request->rq_replen);
135
136         if (request->rq_client) {
137                 spin_lock(&request->rq_client->cli_lock);
138                 list_del(&request->rq_list);
139                 spin_unlock(&request->rq_client->cli_lock);
140         }
141
142         ptlrpc_put_connection(request->rq_connection);
143
144         OBD_FREE(request, sizeof(*request));
145 }
146
147 static int ptlrpc_check_reply(struct ptlrpc_request *req)
148 {
149         int rc = 0;
150
151         schedule_timeout(3 * HZ);  /* 3 second timeout */
152         if (req->rq_repmsg != NULL) {
153                 req->rq_flags |= PTL_RPC_FL_REPLY;
154                 GOTO(out, rc = 1);
155         }
156
157         if (CURRENT_TIME - req->rq_time >= 3) {
158                 CERROR("-- REQ TIMEOUT --\n");
159                 req->rq_flags |= PTL_RPC_FL_TIMEOUT;
160                 if (req->rq_client && req->rq_client->cli_recovd)
161                         connmgr_cli_fail(req->rq_client);
162                 return 0;
163         }
164
165         if (sigismember(&(current->pending.signal), SIGKILL) ||
166             sigismember(&(current->pending.signal), SIGTERM) ||
167             sigismember(&(current->pending.signal), SIGINT)) {
168                 req->rq_flags |= PTL_RPC_FL_INTR;
169                 GOTO(out, rc = 1);
170         }
171
172  out:
173         return rc;
174 }
175
176 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
177 {
178         ENTRY;
179
180         if (err != 0) {
181                 CERROR("err is %d\n", err);
182                 RETURN(err);
183         }
184
185         if (req == NULL) {
186                 CERROR("req == NULL\n");
187                 RETURN(-ENOMEM);
188         }
189
190         if (req->rq_repmsg == NULL) {
191                 CERROR("req->rq_repmsg == NULL\n");
192                 RETURN(-ENOMEM);
193         }
194
195         if (req->rq_repmsg->type == NTOH__u32(PTL_RPC_MSG_ERR)) {
196                 CERROR("req->rq_repmsg->type == PTL_RPC_MSG_ERR\n");
197                 RETURN(-EINVAL);
198         }
199
200         if (req->rq_repmsg->status != 0) {
201                 CERROR("req->rq_repmsg->status is %d\n",
202                        req->rq_repmsg->status);
203                 /* XXX: translate this error from net to host */
204                 RETURN(req->rq_repmsg->status);
205         }
206
207         RETURN(0);
208 }
209
210 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
211 {
212         OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
213         request->rq_reqmsg = NULL;
214         request->rq_reqlen = 0;
215 }
216
217 /* Abort this request and cleanup any resources associated with it. */
218 static int ptlrpc_abort(struct ptlrpc_request *request)
219 {
220         /* First remove the ME for the reply; in theory, this means
221          * that we can tear down the buffer safely. */
222         PtlMEUnlink(request->rq_reply_me_h);
223         OBD_FREE(request->rq_reply_md.start, request->rq_replen);
224         request->rq_repmsg = NULL;
225         request->rq_replen = 0;
226         return 0;
227 }
228
229 int ptlrpc_queue_wait(struct ptlrpc_request *req)
230 {
231         int rc = 0;
232         ENTRY;
233
234         init_waitqueue_head(&req->rq_wait_for_rep);
235
236         rc = ptl_send_rpc(req);
237         if (rc) {
238                 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
239                 ptlrpc_cleanup_request_buf(req);
240                 up(&req->rq_client->cli_rpc_sem);
241                 RETURN(-rc);
242         }
243
244         CDEBUG(D_OTHER, "-- sleeping\n");
245         wait_event_interruptible(req->rq_wait_for_rep, ptlrpc_check_reply(req));
246         CDEBUG(D_OTHER, "-- done\n");
247         ptlrpc_cleanup_request_buf(req);
248         up(&req->rq_client->cli_rpc_sem);
249         if (req->rq_flags & PTL_RPC_FL_INTR) {
250                 /* Clean up the dangling reply buffers */
251                 ptlrpc_abort(req);
252                 GOTO(out, rc = -EINTR);
253         }
254
255         if (! (req->rq_flags & PTL_RPC_FL_REPLY)) {
256                 CERROR("Unknown reason for wakeup\n");
257                 /* XXX Phil - I end up here when I kill obdctl */
258                 ptlrpc_abort(req);
259                 GOTO(out, rc = -EINTR);
260         }
261
262         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
263         if (rc) {
264                 CERROR("unpack_rep failed: %d\n", rc);
265                 GOTO(out, rc);
266         }
267         CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
268
269         if (req->rq_repmsg->status == 0)
270                 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
271                        req->rq_replen, req->rq_repmsg->status);
272
273         EXIT;
274  out:
275         return rc;
276 }