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 #define DEBUG_SUBSYSTEM S_RPC
27 #include <linux/lustre_net.h>
29 void llite_ha_conn_manage(struct lustre_ha_mgr *mgr, struct ptlrpc_client *cli)
32 cli->cli_ha_mgr = mgr;
33 spin_lock(&mgr->mgr_lock);
34 list_add(&cli->cli_ha_item, &mgr->mgr_connections_lh);
35 spin_unlock(&mgr->mgr_lock);
39 void llite_ha_conn_fail(struct ptlrpc_client *cli)
42 spin_lock(&cli->cli_ha_mgr->mgr_lock);
43 list_del(&cli->cli_ha_item);
44 list_add(&cli->cli_ha_item, &cli->cli_ha_mgr->mgr_troubled_lh);
45 spin_unlock(&cli->cli_ha_mgr->mgr_lock);
46 wake_up(&cli->cli_ha_mgr->mgr_waitq);
50 void ptlrpc_init_client(struct lustre_ha_mgr *mgr, int req_portal,
51 int rep_portal, struct ptlrpc_client *cl)
53 memset(cl, 0, sizeof(*cl));
56 llite_ha_conn_manage(mgr, cl);
58 cl->cli_request_portal = req_portal;
59 cl->cli_reply_portal = rep_portal;
60 INIT_LIST_HEAD(&cl->cli_sending_head);
61 INIT_LIST_HEAD(&cl->cli_sent_head);
62 sema_init(&cl->cli_rpc_sem, 32);
65 struct ptlrpc_connection *ptlrpc_connect_client(char *uuid)
67 struct ptlrpc_connection *c;
68 struct lustre_peer peer;
71 err = kportal_uuid_to_peer(uuid, &peer);
73 CERROR("cannot find peer %s!\n", uuid);
77 c = ptlrpc_get_connection(&peer);
84 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *conn)
86 struct ptlrpc_bulk_desc *bulk;
88 OBD_ALLOC(bulk, sizeof(*bulk));
90 bulk->b_connection = ptlrpc_connection_addref(conn);
91 init_waitqueue_head(&bulk->b_waitq);
97 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl,
98 struct ptlrpc_connection *conn,
99 int opcode, int count, int *lengths,
102 struct ptlrpc_request *request;
106 OBD_ALLOC(request, sizeof(*request));
108 CERROR("request allocation out of memory\n");
112 rc = lustre_pack_msg(count, lengths, bufs,
113 &request->rq_reqlen, &request->rq_reqmsg);
115 CERROR("cannot pack request %d\n", rc);
119 request->rq_time = CURRENT_TIME;
120 request->rq_type = PTL_RPC_REQUEST;
121 request->rq_connection = ptlrpc_connection_addref(conn);
123 request->rq_reqmsg->conn = (__u64)(unsigned long)conn;
124 request->rq_reqmsg->token = conn->c_token;
125 request->rq_reqmsg->opc = HTON__u32(opcode);
126 request->rq_reqmsg->type = HTON__u32(request->rq_type);
128 spin_lock(&conn->c_lock);
129 request->rq_reqmsg->xid = HTON__u32(++conn->c_xid_out);
130 spin_unlock(&c->c_lock);
132 request->rq_client = cl;
137 void ptlrpc_free_req(struct ptlrpc_request *request)
142 if (request->rq_repmsg != NULL)
143 OBD_FREE(request->rq_repmsg, request->rq_replen);
145 ptlrpc_put_connection(request->rq_connection);
147 OBD_FREE(request, sizeof(*request));
150 static int ptlrpc_check_reply(struct ptlrpc_request *req)
154 schedule_timeout(3 * HZ); /* 3 second timeout */
155 if (req->rq_repmsg != NULL) {
156 req->rq_flags = PTL_RPC_REPLY;
160 if (CURRENT_TIME - req->rq_time >= 3) {
161 CERROR("-- REQ TIMEOUT --\n");
162 if (req->rq_client && req->rq_client->cli_ha_mgr)
163 llite_ha_conn_fail(req->rq_client);
167 if (sigismember(&(current->pending.signal), SIGKILL) ||
168 sigismember(&(current->pending.signal), SIGTERM) ||
169 sigismember(&(current->pending.signal), SIGINT)) {
170 req->rq_flags = PTL_RPC_INTR;
178 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
183 CERROR("err is %d\n", err);
188 CERROR("req == NULL\n");
192 if (req->rq_repmsg == NULL) {
193 CERROR("req->rq_repmsg == NULL\n");
197 if (req->rq_repmsg->status != 0) {
198 CERROR("req->rq_repmsg->status is %d\n",
199 req->rq_repmsg->status);
200 /* XXX: translate this error from net to host */
201 RETURN(req->rq_repmsg->status);
207 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
209 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
210 request->rq_reqmsg = NULL;
211 request->rq_reqlen = 0;
214 /* Abort this request and cleanup any resources associated with it. */
215 static int ptlrpc_abort(struct ptlrpc_request *request)
217 /* First remove the ME for the reply; in theory, this means
218 * that we can tear down the buffer safely. */
219 PtlMEUnlink(request->rq_reply_me_h);
220 OBD_FREE(request->rq_reply_md.start, request->rq_replen);
221 request->rq_repmsg = NULL;
222 request->rq_replen = 0;
226 int ptlrpc_queue_wait(struct ptlrpc_request *req)
231 init_waitqueue_head(&req->rq_wait_for_rep);
233 rc = ptl_send_rpc(req);
235 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
236 ptlrpc_cleanup_request_buf(req);
237 up(&req->rq_client->cli_rpc_sem);
241 CDEBUG(D_OTHER, "-- sleeping\n");
242 wait_event_interruptible(req->rq_wait_for_rep, ptlrpc_check_reply(req));
243 CDEBUG(D_OTHER, "-- done\n");
244 ptlrpc_cleanup_request_buf(req);
245 up(&req->rq_client->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_repmsg, req->rq_replen);
261 CERROR("unpack_rep failed: %d\n", rc);
264 CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
266 if (req->rq_repmsg->status == 0)
267 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
268 req->rq_replen, req->rq_repmsg->status);