Whamcloud - gitweb
2be7a3c1750cba5475f75334734587cfe6a96e89
[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 connmgr_obd *mgr, int req_portal,
30                         int rep_portal, struct ptlrpc_client *cl)
31 {
32         memset(cl, 0, sizeof(*cl));
33         cl->cli_ha_mgr = mgr;
34         if (mgr)
35                 connmgr_cli_manage(mgr, 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         sema_init(&cl->cli_rpc_sem, 32);
42 }
43
44 struct ptlrpc_connection *ptlrpc_uuid_to_connection(char *uuid)
45 {
46         struct ptlrpc_connection *c;
47         struct lustre_peer peer;
48         int err;
49
50         err = kportal_uuid_to_peer(uuid, &peer);
51         if (err != 0) {
52                 CERROR("cannot find peer %s!\n", uuid);
53                 return NULL;
54         }
55
56         c = ptlrpc_get_connection(&peer);
57         if (c)
58                 c->c_epoch++;
59
60         return c;
61 }
62
63 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *conn)
64 {
65         struct ptlrpc_bulk_desc *bulk;
66
67         OBD_ALLOC(bulk, sizeof(*bulk));
68         if (bulk != NULL) {
69                 bulk->b_connection = ptlrpc_connection_addref(conn);
70                 init_waitqueue_head(&bulk->b_waitq);
71         }
72
73         return bulk;
74 }
75
76 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl,
77                                        struct ptlrpc_connection *conn,
78                                        int opcode, int count, int *lengths,
79                                        char **bufs)
80 {
81         struct ptlrpc_request *request;
82         int rc;
83         ENTRY;
84
85         OBD_ALLOC(request, sizeof(*request));
86         if (!request) {
87                 CERROR("request allocation out of memory\n");
88                 RETURN(NULL);
89         }
90
91         rc = lustre_pack_msg(count, lengths, bufs,
92                              &request->rq_reqlen, &request->rq_reqmsg);
93         if (rc) {
94                 CERROR("cannot pack request %d\n", rc);
95                 RETURN(NULL);
96         }
97
98         request->rq_time = CURRENT_TIME;
99         request->rq_type = PTL_RPC_REQUEST;
100         request->rq_connection = ptlrpc_connection_addref(conn);
101
102         request->rq_reqmsg->conn = (__u64)(unsigned long)conn->c_remote_conn;
103         request->rq_reqmsg->token = conn->c_remote_token;
104         request->rq_reqmsg->opc = HTON__u32(opcode);
105         request->rq_reqmsg->type = HTON__u32(request->rq_type);
106
107         spin_lock(&conn->c_lock);
108         request->rq_reqmsg->xid = HTON__u32(++conn->c_xid_out);
109         spin_unlock(&conn->c_lock);
110
111         request->rq_client = cl;
112
113         RETURN(request);
114 }
115
116 void ptlrpc_free_req(struct ptlrpc_request *request)
117 {
118         if (request == NULL)
119                 return;
120
121         if (request->rq_repmsg != NULL)
122                 OBD_FREE(request->rq_repmsg, request->rq_replen);
123
124         ptlrpc_put_connection(request->rq_connection);
125
126         OBD_FREE(request, sizeof(*request));
127 }
128
129 static int ptlrpc_check_reply(struct ptlrpc_request *req)
130 {
131         int rc = 0;
132
133         schedule_timeout(3 * HZ);  /* 3 second timeout */
134         if (req->rq_repmsg != NULL) {
135                 req->rq_flags = PTL_RPC_REPLY;
136                 GOTO(out, rc = 1);
137         }
138
139         if (CURRENT_TIME - req->rq_time >= 3) { 
140                 CERROR("-- REQ TIMEOUT --\n"); 
141                 if (req->rq_client && req->rq_client->cli_ha_mgr)
142                         connmgr_cli_fail(req->rq_client); 
143                 return 0;
144         }
145
146         if (sigismember(&(current->pending.signal), SIGKILL) ||
147             sigismember(&(current->pending.signal), SIGTERM) ||
148             sigismember(&(current->pending.signal), SIGINT)) {
149                 req->rq_flags = PTL_RPC_INTR;
150                 GOTO(out, rc = 1);
151         }
152
153  out:
154         return rc;
155 }
156
157 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
158 {
159         ENTRY;
160
161         if (err != 0) {
162                 CERROR("err is %d\n", err);
163                 RETURN(err);
164         }
165
166         if (req == NULL) {
167                 CERROR("req == NULL\n");
168                 RETURN(-ENOMEM);
169         }
170
171         if (req->rq_repmsg == NULL) {
172                 CERROR("req->rq_repmsg == NULL\n");
173                 RETURN(-ENOMEM);
174         }
175
176         if (req->rq_repmsg->status != 0) {
177                 CERROR("req->rq_repmsg->status is %d\n",
178                        req->rq_repmsg->status);
179                 /* XXX: translate this error from net to host */
180                 RETURN(req->rq_repmsg->status);
181         }
182
183         RETURN(0);
184 }
185
186 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
187 {
188         OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
189         request->rq_reqmsg = NULL;
190         request->rq_reqlen = 0;
191 }
192
193 /* Abort this request and cleanup any resources associated with it. */
194 static int ptlrpc_abort(struct ptlrpc_request *request)
195 {
196         /* First remove the ME for the reply; in theory, this means
197          * that we can tear down the buffer safely. */
198         PtlMEUnlink(request->rq_reply_me_h);
199         OBD_FREE(request->rq_reply_md.start, request->rq_replen);
200         request->rq_repmsg = NULL;
201         request->rq_replen = 0;
202         return 0;
203 }
204
205 int ptlrpc_queue_wait(struct ptlrpc_request *req)
206 {
207         int rc = 0;
208         ENTRY;
209
210         init_waitqueue_head(&req->rq_wait_for_rep);
211
212         rc = ptl_send_rpc(req);
213         if (rc) {
214                 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
215                 ptlrpc_cleanup_request_buf(req);
216                 up(&req->rq_client->cli_rpc_sem);
217                 RETURN(-rc);
218         }
219
220         CDEBUG(D_OTHER, "-- sleeping\n");
221         wait_event_interruptible(req->rq_wait_for_rep, ptlrpc_check_reply(req));
222         CDEBUG(D_OTHER, "-- done\n");
223         ptlrpc_cleanup_request_buf(req);
224         up(&req->rq_client->cli_rpc_sem);
225         if (req->rq_flags == PTL_RPC_INTR) {
226                 /* Clean up the dangling reply buffers */
227                 ptlrpc_abort(req);
228                 GOTO(out, rc = -EINTR);
229         }
230
231         if (req->rq_flags != PTL_RPC_REPLY) {
232                 CERROR("Unknown reason for wakeup\n");
233                 /* XXX Phil - I end up here when I kill obdctl */
234                 ptlrpc_abort(req);
235                 GOTO(out, rc = -EINTR);
236         }
237
238         rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
239         if (rc) {
240                 CERROR("unpack_rep failed: %d\n", rc);
241                 GOTO(out, rc);
242         }
243         CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
244
245         if (req->rq_repmsg->status == 0)
246                 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
247                        req->rq_replen, req->rq_repmsg->status);
248
249         EXIT;
250  out:
251         return rc;
252 }