Whamcloud - gitweb
4755dda9834457b9856a38c928cd111ca489ef97
[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 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28
29 #define DEBUG_SUBSYSTEM S_RPC
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/lustre_net.h>
34
35 int ptlrpc_enqueue(struct ptlrpc_client *peer, struct ptlrpc_request *req)
36 {
37         struct ptlrpc_request *srv_req;
38         
39         if (!peer->cli_obd) { 
40                 EXIT;
41                 return -1;
42         }
43
44         OBD_ALLOC(srv_req, sizeof(*srv_req));
45         if (!srv_req) { 
46                 EXIT;
47                 return -ENOMEM;
48         }
49
50         CDEBUG(0, "peer obd minor %d, incoming req %p, srv_req %p\n",
51                peer->cli_obd->obd_minor, req, srv_req);
52
53         memset(srv_req, 0, sizeof(*req)); 
54
55         /* move the request buffer */
56         srv_req->rq_reqbuf = req->rq_reqbuf;
57         srv_req->rq_reqlen = req->rq_reqlen;
58         srv_req->rq_obd = peer->cli_obd;
59
60         /* remember where it came from */
61         srv_req->rq_reply_handle = req;
62
63         list_add(&srv_req->rq_list, &peer->cli_obd->obd_req_list); 
64         wake_up(&peer->cli_obd->obd_req_waitq);
65         return 0;
66 }
67
68 int ptlrpc_connect_client(int dev, char *uuid, int req_portal, int rep_portal, 
69                           req_pack_t req_pack, rep_unpack_t rep_unpack,
70                           struct ptlrpc_client *cl)
71 {
72         int err; 
73
74         memset(cl, 0, sizeof(*cl));
75         spin_lock_init(&cl->cli_lock);
76         cl->cli_xid = 1;
77         cl->cli_obd = NULL; 
78         cl->cli_request_portal = req_portal;
79         cl->cli_reply_portal = rep_portal;
80         cl->cli_rep_unpack = rep_unpack;
81         cl->cli_req_pack = req_pack;
82
83         /* non networked client */
84         if (dev >= 0 && dev < MAX_OBD_DEVICES) {
85                 struct obd_device *obd = &obd_dev[dev];
86                 
87                 if ((!obd->obd_flags & OBD_ATTACHED) ||
88                     (!obd->obd_flags & OBD_SET_UP)) { 
89                         CERROR("target device %d not att or setup\n", dev);
90                         return -EINVAL;
91                 }
92                 if (strcmp(obd->obd_type->typ_name, "ost") && 
93                     strcmp(obd->obd_type->typ_name, "mds")) { 
94                         return -EINVAL;
95                 }
96
97                 cl->cli_obd = &obd_dev[dev];
98                 return 0;
99         }
100
101         /* networked */
102         err = kportal_uuid_to_peer(uuid, &cl->cli_server);
103         if (err != 0) { 
104                 CERROR("cannot find peer %s!", uuid); 
105         }
106
107         return err;
108 }
109
110 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct lustre_peer *peer)
111 {
112         struct ptlrpc_bulk_desc *bulk;
113
114         OBD_ALLOC(bulk, sizeof(*bulk));
115         if (bulk != NULL) {
116                 memset(bulk, 0, sizeof(*bulk));
117                 memcpy(&bulk->b_peer, peer, sizeof(*peer));
118                 init_waitqueue_head(&bulk->b_waitq);
119         }
120
121         return bulk;
122 }
123
124 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl, 
125                                        int opcode, int namelen, char *name,
126                                        int tgtlen, char *tgt)
127 {
128         struct ptlrpc_request *request;
129         int rc;
130         ENTRY; 
131
132         OBD_ALLOC(request, sizeof(*request));
133         if (!request) { 
134                 CERROR("request allocation out of memory\n");
135                 return NULL;
136         }
137
138         memset(request, 0, sizeof(*request));
139
140         spin_lock(&cl->cli_lock);
141         request->rq_xid = cl->cli_xid++;
142         spin_unlock(&cl->cli_lock);
143
144         rc = cl->cli_req_pack(name, namelen, tgt, tgtlen,
145                           &request->rq_reqhdr, &request->rq_req,
146                           &request->rq_reqlen, &request->rq_reqbuf);
147         if (rc) { 
148                 CERROR("cannot pack request %d\n", rc); 
149                 return NULL;
150         }
151         request->rq_reqhdr->opc = opcode;
152         request->rq_reqhdr->xid = request->rq_xid;
153
154         EXIT;
155         return request;
156 }
157
158 void ptlrpc_free_req(struct ptlrpc_request *request)
159 {
160         if (request == NULL)
161                 return;
162
163         if (request->rq_repbuf != NULL)
164                 OBD_FREE(request->rq_repbuf, request->rq_replen);
165         OBD_FREE(request, sizeof(*request));
166 }
167
168 static int ptlrpc_check_reply(struct ptlrpc_request *req)
169 {
170         if (req->rq_repbuf != NULL) {
171                 req->rq_flags = PTL_RPC_REPLY;
172                 EXIT;
173                 return 1;
174         }
175
176         if (sigismember(&(current->pending.signal), SIGKILL) ||
177             sigismember(&(current->pending.signal), SIGSTOP) ||
178             sigismember(&(current->pending.signal), SIGINT)) { 
179                 req->rq_flags = PTL_RPC_INTR;
180                 EXIT;
181                 return 1;
182         }
183
184         return 0;
185 }
186
187 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
188 {
189         ENTRY;
190
191         if (err != 0) {
192                 CERROR("err is %d\n", err);
193                 EXIT;
194                 return err;
195         }
196
197         if (req == NULL) {
198                 CERROR("req == NULL\n");
199                 EXIT;
200                 return -ENOMEM;
201         }
202
203         if (req->rq_rephdr == NULL) {
204                 CERROR("req->rq_rephdr == NULL\n");
205                 EXIT;
206                 return -ENOMEM;
207         }
208
209         if (req->rq_rephdr->status != 0) {
210                 CERROR("req->rq_rephdr->status is %d\n",
211                        req->rq_rephdr->status);
212                 EXIT;
213                 /* XXX: translate this error from net to host */
214                 return req->rq_rephdr->status;
215         }
216
217         EXIT;
218         return 0;
219 }
220
221 /* Abort this request and cleanup any resources associated with it. */
222 int ptlrpc_abort(struct ptlrpc_request *request)
223 {
224         /* First remove the MD for the reply; in theory, this means
225          * that we can tear down the buffer safely. */
226         PtlMEUnlink(request->rq_reply_me_h);
227         PtlMDUnlink(request->rq_reply_md_h);
228         OBD_FREE(request->rq_repbuf, request->rq_replen);
229         request->rq_repbuf = NULL;
230         request->rq_replen = 0;
231
232         return 0;
233 }
234
235 int ptlrpc_queue_wait(struct ptlrpc_client *cl, struct ptlrpc_request *req)
236                              
237 {
238         int rc;
239         ENTRY;
240
241         init_waitqueue_head(&req->rq_wait_for_rep);
242
243         if (cl->cli_obd) {
244                 /* Local delivery */
245                 ENTRY;
246                 rc = ptlrpc_enqueue(cl, req); 
247         } else {
248                 /* Remote delivery via portals. */
249                 req->rq_req_portal = cl->cli_request_portal;
250                 req->rq_reply_portal = cl->cli_reply_portal;
251                 rc = ptl_send_rpc(req, &cl->cli_server);
252         }
253         if (rc) { 
254                 CERROR("error %d, opcode %d\n", rc, req->rq_reqhdr->opc);
255                 return -rc;
256         }
257
258         CDEBUG(D_OTHER, "-- sleeping\n");
259         wait_event_interruptible(req->rq_wait_for_rep, ptlrpc_check_reply(req));
260         CDEBUG(D_OTHER, "-- done\n");
261         
262         if (req->rq_flags == PTL_RPC_INTR) { 
263                 /* Clean up the dangling reply buffers */
264                 ptlrpc_abort(req);
265                 EXIT;
266                 return -EINTR;
267         }
268
269         if (req->rq_flags != PTL_RPC_REPLY) { 
270                 CERROR("Unknown reason for wakeup\n");
271                 /* XXX Phil - I end up here when I kill obdctl */
272                 ptlrpc_abort(req); 
273                 //BUG();
274                 EXIT;
275                 return -EINTR;
276         }
277
278         rc = cl->cli_rep_unpack(req->rq_repbuf, req->rq_replen,
279                                 &req->rq_rephdr, &req->rq_rep);
280         if (rc) {
281                 CERROR("unpack_rep failed: %d\n", rc);
282                 return rc;
283         }
284         CDEBUG(D_NET, "got rep %d\n", req->rq_rephdr->xid);
285
286         if ( req->rq_rephdr->status == 0 )
287                 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repbuf,
288                        req->rq_replen, req->rq_rephdr->status);
289
290         EXIT;
291         return 0;
292 }