1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
29 #define DEBUG_SUBSYSTEM S_RPC
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/lustre_net.h>
35 int ptlrpc_enqueue(struct ptlrpc_client *peer, struct ptlrpc_request *req)
37 struct ptlrpc_request *srv_req;
42 OBD_ALLOC(srv_req, sizeof(*srv_req));
46 CDEBUG(0, "peer obd minor %d, incoming req %p, srv_req %p\n",
47 peer->cli_obd->obd_minor, req, srv_req);
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;
54 /* remember where it came from */
55 srv_req->rq_reply_handle = req;
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);
64 int ptlrpc_connect_client(int dev, char *uuid, int req_portal, int rep_portal,
65 struct ptlrpc_client *cl)
69 memset(cl, 0, sizeof(*cl));
70 spin_lock_init(&cl->cli_lock);
73 cl->cli_request_portal = req_portal;
74 cl->cli_reply_portal = rep_portal;
75 sema_init(&cl->cli_rpc_sem, 32);
77 /* non networked client */
78 if (dev >= 0 && dev < MAX_OBD_DEVICES) {
79 struct obd_device *obd = &obd_dev[dev];
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);
86 if (strcmp(obd->obd_type->typ_name, "ost") &&
87 strcmp(obd->obd_type->typ_name, "mds"))
90 cl->cli_obd = &obd_dev[dev];
95 err = kportal_uuid_to_peer(uuid, &cl->cli_server);
97 CERROR("cannot find peer %s!\n", uuid);
102 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct lustre_peer *peer)
104 struct ptlrpc_bulk_desc *bulk;
106 OBD_ALLOC(bulk, sizeof(*bulk));
108 memcpy(&bulk->b_peer, peer, sizeof(*peer));
109 init_waitqueue_head(&bulk->b_waitq);
115 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl, int opcode,
116 int count, int *lengths, char **bufs)
118 struct ptlrpc_request *request;
122 OBD_ALLOC(request, sizeof(*request));
124 CERROR("request allocation out of memory\n");
128 spin_lock(&cl->cli_lock);
129 request->rq_xid = cl->cli_xid++;
130 spin_unlock(&cl->cli_lock);
132 rc = lustre_pack_msg(count, lengths, bufs,
133 &request->rq_reqlen, &request->rq_reqbuf);
135 CERROR("cannot pack request %d\n", rc);
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);
147 void ptlrpc_free_req(struct ptlrpc_request *request)
152 if (request->rq_repbuf != NULL)
153 OBD_FREE(request->rq_repbuf, request->rq_replen);
154 OBD_FREE(request, sizeof(*request));
157 static int ptlrpc_check_reply(struct ptlrpc_request *req)
161 if (req->rq_repbuf != NULL) {
162 req->rq_flags = PTL_RPC_REPLY;
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;
177 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
182 CERROR("err is %d\n", err);
187 CERROR("req == NULL\n");
191 if (req->rq_repmsg == NULL) {
192 CERROR("req->rq_repmsg == NULL\n");
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);
206 /* Abort this request and cleanup any resources associated with it. */
207 int ptlrpc_abort(struct ptlrpc_request *request)
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;
219 int ptlrpc_queue_wait(struct ptlrpc_client *cl, struct ptlrpc_request *req)
224 init_waitqueue_head(&req->rq_wait_for_rep);
228 down(&cl->cli_rpc_sem);
229 rc = ptlrpc_enqueue(cl, req);
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);
237 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
238 up(&cl->cli_rpc_sem);
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 */
249 GOTO(out, rc = -EINTR);
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 */
256 GOTO(out, rc = -EINTR);
259 rc = lustre_unpack_msg(req->rq_repbuf, req->rq_replen);
260 req->rq_repmsg = (struct lustre_msg *)req->rq_repbuf;
262 CERROR("unpack_rep failed: %d\n", rc);
265 CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
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);