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>
28 #include <linux/list.h>
30 #define DEBUG_SUBSYSTEM S_RPC
32 #include <linux/obd_support.h>
33 #include <linux/obd_class.h>
34 #include <linux/lustre_net.h>
36 void ptlrpc_init_client(int dev, int req_portal, int rep_portal,
37 struct ptlrpc_client *cl)
39 memset(cl, 0, sizeof(*cl));
40 spin_lock_init(&cl->cli_lock);
42 cl->cli_generation = 1;
44 cl->cli_bootcount = 0;
46 cl->cli_request_portal = req_portal;
47 cl->cli_reply_portal = rep_portal;
48 INIT_LIST_HEAD(&cl->cli_sending_head);
49 INIT_LIST_HEAD(&cl->cli_sent_head);
50 sema_init(&cl->cli_rpc_sem, 32);
53 int ptlrpc_connect_client(int dev, char *uuid, struct ptlrpc_client *cl)
58 err = kportal_uuid_to_peer(uuid, &cl->cli_server);
60 CERROR("cannot find peer %s!\n", uuid);
65 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct lustre_peer *peer)
67 struct ptlrpc_bulk_desc *bulk;
69 OBD_ALLOC(bulk, sizeof(*bulk));
71 memcpy(&bulk->b_peer, peer, sizeof(*peer));
72 init_waitqueue_head(&bulk->b_waitq);
78 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl, int opcode,
79 int count, int *lengths, char **bufs)
81 struct ptlrpc_request *request;
85 OBD_ALLOC(request, sizeof(*request));
87 CERROR("request allocation out of memory\n");
91 spin_lock(&cl->cli_lock);
92 request->rq_xid = cl->cli_xid++;
93 spin_unlock(&cl->cli_lock);
95 rc = lustre_pack_msg(count, lengths, bufs,
96 &request->rq_reqlen, &request->rq_reqbuf);
98 CERROR("cannot pack request %d\n", rc);
101 request->rq_type = PTL_RPC_REQUEST;
102 request->rq_reqmsg = (struct lustre_msg *)request->rq_reqbuf;
103 request->rq_reqmsg->opc = HTON__u32(opcode);
104 request->rq_reqmsg->xid = HTON__u32(request->rq_xid);
105 request->rq_reqmsg->type = HTON__u32(request->rq_type);
110 void ptlrpc_free_req(struct ptlrpc_request *request)
115 if (request->rq_repbuf != NULL)
116 OBD_FREE(request->rq_repbuf, request->rq_replen);
117 OBD_FREE(request, sizeof(*request));
120 static int ptlrpc_check_reply(struct ptlrpc_request *req)
124 if (req->rq_repbuf != NULL) {
125 req->rq_flags = PTL_RPC_REPLY;
129 if (sigismember(&(current->pending.signal), SIGKILL) ||
130 sigismember(&(current->pending.signal), SIGTERM) ||
131 sigismember(&(current->pending.signal), SIGINT)) {
132 req->rq_flags = PTL_RPC_INTR;
140 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
145 CERROR("err is %d\n", err);
150 CERROR("req == NULL\n");
154 if (req->rq_repmsg == NULL) {
155 CERROR("req->rq_repmsg == NULL\n");
159 if (req->rq_repmsg->status != 0) {
160 CERROR("req->rq_repmsg->status is %d\n",
161 req->rq_repmsg->status);
162 /* XXX: translate this error from net to host */
163 RETURN(req->rq_repmsg->status);
169 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
171 OBD_FREE(request->rq_reqbuf, request->rq_reqlen);
172 request->rq_reqbuf = NULL;
173 request->rq_reqlen = 0;
176 /* Abort this request and cleanup any resources associated with it. */
177 static int ptlrpc_abort(struct ptlrpc_request *request)
179 /* First remove the ME for the reply; in theory, this means
180 * that we can tear down the buffer safely. */
181 PtlMEUnlink(request->rq_reply_me_h);
182 OBD_FREE(request->rq_reply_md.start, request->rq_replen);
183 request->rq_repbuf = NULL;
184 request->rq_replen = 0;
188 int ptlrpc_queue_wait(struct ptlrpc_client *cl, struct ptlrpc_request *req)
193 init_waitqueue_head(&req->rq_wait_for_rep);
196 req->rq_req_portal = cl->cli_request_portal;
197 req->rq_reply_portal = cl->cli_reply_portal;
198 rc = ptl_send_rpc(req, cl);
200 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
201 ptlrpc_cleanup_request_buf(req);
202 up(&cl->cli_rpc_sem);
206 CDEBUG(D_OTHER, "-- sleeping\n");
207 wait_event_interruptible(req->rq_wait_for_rep, ptlrpc_check_reply(req));
208 CDEBUG(D_OTHER, "-- done\n");
209 ptlrpc_cleanup_request_buf(req);
210 up(&cl->cli_rpc_sem);
211 if (req->rq_flags == PTL_RPC_INTR) {
212 /* Clean up the dangling reply buffers */
214 GOTO(out, rc = -EINTR);
217 if (req->rq_flags != PTL_RPC_REPLY) {
218 CERROR("Unknown reason for wakeup\n");
219 /* XXX Phil - I end up here when I kill obdctl */
221 GOTO(out, rc = -EINTR);
224 rc = lustre_unpack_msg(req->rq_repbuf, req->rq_replen);
225 req->rq_repmsg = (struct lustre_msg *)req->rq_repbuf;
227 CERROR("unpack_rep failed: %d\n", rc);
230 CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
232 if (req->rq_repmsg->status == 0)
233 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repbuf,
234 req->rq_replen, req->rq_repmsg->status);