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_ha.h>
29 void ptlrpc_init_client(struct connmgr_obd *mgr, int req_portal,
30 int rep_portal, struct ptlrpc_client *cl)
32 memset(cl, 0, sizeof(*cl));
35 connmgr_cli_manage(mgr, cl);
37 cl->cli_request_portal = req_portal;
38 cl->cli_reply_portal = rep_portal;
39 INIT_LIST_HEAD(&cl->cli_sending_head);
40 INIT_LIST_HEAD(&cl->cli_sent_head);
41 spin_lock_init(&cl->cli_lock);
42 sema_init(&cl->cli_rpc_sem, 32);
45 struct ptlrpc_connection *ptlrpc_uuid_to_connection(char *uuid)
47 struct ptlrpc_connection *c;
48 struct lustre_peer peer;
51 err = kportal_uuid_to_peer(uuid, &peer);
53 CERROR("cannot find peer %s!\n", uuid);
57 c = ptlrpc_get_connection(&peer);
64 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *conn)
66 struct ptlrpc_bulk_desc *bulk;
68 OBD_ALLOC(bulk, sizeof(*bulk));
70 bulk->b_connection = ptlrpc_connection_addref(conn);
71 init_waitqueue_head(&bulk->b_waitq);
77 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk)
82 ptlrpc_put_connection(bulk->b_connection);
84 OBD_FREE(bulk, sizeof(*bulk));
87 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl,
88 struct ptlrpc_connection *conn,
89 int opcode, int count, int *lengths,
92 struct ptlrpc_request *request;
96 OBD_ALLOC(request, sizeof(*request));
98 CERROR("request allocation out of memory\n");
102 rc = lustre_pack_msg(count, lengths, bufs,
103 &request->rq_reqlen, &request->rq_reqmsg);
105 CERROR("cannot pack request %d\n", rc);
109 request->rq_time = CURRENT_TIME;
110 request->rq_type = PTL_RPC_REQUEST;
111 request->rq_connection = ptlrpc_connection_addref(conn);
113 request->rq_reqmsg->conn = (__u64)(unsigned long)conn->c_remote_conn;
114 request->rq_reqmsg->token = conn->c_remote_token;
115 request->rq_reqmsg->opc = HTON__u32(opcode);
116 request->rq_reqmsg->type = HTON__u32(request->rq_type);
117 INIT_LIST_HEAD(&request->rq_list);
119 spin_lock(&conn->c_lock);
120 request->rq_reqmsg->xid = HTON__u32(++conn->c_xid_out);
121 spin_unlock(&conn->c_lock);
123 request->rq_client = cl;
128 void ptlrpc_free_req(struct ptlrpc_request *request)
133 if (request->rq_repmsg != NULL)
134 OBD_FREE(request->rq_repmsg, request->rq_replen);
136 if (request->rq_client) {
137 spin_lock(&request->rq_client->cli_lock);
138 list_del(&request->rq_list);
139 spin_unlock(&request->rq_client->cli_lock);
142 ptlrpc_put_connection(request->rq_connection);
144 OBD_FREE(request, sizeof(*request));
147 static int ptlrpc_check_reply(struct ptlrpc_request *req)
151 schedule_timeout(3 * HZ); /* 3 second timeout */
152 if (req->rq_repmsg != NULL) {
153 req->rq_flags = PTL_RPC_REPLY;
157 if (CURRENT_TIME - req->rq_time >= 3) {
158 CERROR("-- REQ TIMEOUT --\n");
159 if (req->rq_client && req->rq_client->cli_ha_mgr)
160 connmgr_cli_fail(req->rq_client);
164 if (sigismember(&(current->pending.signal), SIGKILL) ||
165 sigismember(&(current->pending.signal), SIGTERM) ||
166 sigismember(&(current->pending.signal), SIGINT)) {
167 req->rq_flags = PTL_RPC_INTR;
175 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
180 CERROR("err is %d\n", err);
185 CERROR("req == NULL\n");
189 if (req->rq_repmsg == NULL) {
190 CERROR("req->rq_repmsg == NULL\n");
194 if (req->rq_repmsg->status != 0) {
195 CERROR("req->rq_repmsg->status is %d\n",
196 req->rq_repmsg->status);
197 /* XXX: translate this error from net to host */
198 RETURN(req->rq_repmsg->status);
204 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
206 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
207 request->rq_reqmsg = NULL;
208 request->rq_reqlen = 0;
211 /* Abort this request and cleanup any resources associated with it. */
212 static int ptlrpc_abort(struct ptlrpc_request *request)
214 /* First remove the ME for the reply; in theory, this means
215 * that we can tear down the buffer safely. */
216 PtlMEUnlink(request->rq_reply_me_h);
217 OBD_FREE(request->rq_reply_md.start, request->rq_replen);
218 request->rq_repmsg = NULL;
219 request->rq_replen = 0;
223 int ptlrpc_queue_wait(struct ptlrpc_request *req)
228 init_waitqueue_head(&req->rq_wait_for_rep);
230 rc = ptl_send_rpc(req);
232 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
233 ptlrpc_cleanup_request_buf(req);
234 up(&req->rq_client->cli_rpc_sem);
238 CDEBUG(D_OTHER, "-- sleeping\n");
239 wait_event_interruptible(req->rq_wait_for_rep, ptlrpc_check_reply(req));
240 CDEBUG(D_OTHER, "-- done\n");
241 ptlrpc_cleanup_request_buf(req);
242 up(&req->rq_client->cli_rpc_sem);
243 if (req->rq_flags == PTL_RPC_INTR) {
244 /* Clean up the dangling reply buffers */
246 GOTO(out, rc = -EINTR);
249 if (req->rq_flags != PTL_RPC_REPLY) {
250 CERROR("Unknown reason for wakeup\n");
251 /* XXX Phil - I end up here when I kill obdctl */
253 GOTO(out, rc = -EINTR);
256 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
258 CERROR("unpack_rep failed: %d\n", rc);
261 CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
263 if (req->rq_repmsg->status == 0)
264 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
265 req->rq_replen, req->rq_repmsg->status);