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