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 recovd_obd *recovd,
30 int (*recover)(struct ptlrpc_client *recover),
32 int rep_portal, struct ptlrpc_client *cl)
34 memset(cl, 0, sizeof(*cl));
35 cl->cli_recovd = recovd;
36 cl->cli_recover = recover;
38 recovd_cli_manage(recovd, cl);
40 cl->cli_request_portal = req_portal;
41 cl->cli_reply_portal = rep_portal;
42 INIT_LIST_HEAD(&cl->cli_delayed_head);
43 INIT_LIST_HEAD(&cl->cli_sending_head);
44 INIT_LIST_HEAD(&cl->cli_dying_head);
45 spin_lock_init(&cl->cli_lock);
46 sema_init(&cl->cli_rpc_sem, 32);
49 __u8 *ptlrpc_req_to_uuid(struct ptlrpc_request *req)
51 return req->rq_connection->c_remote_uuid;
54 struct ptlrpc_connection *ptlrpc_uuid_to_connection(char *uuid)
56 struct ptlrpc_connection *c;
57 struct lustre_peer peer;
60 err = kportal_uuid_to_peer(uuid, &peer);
62 CERROR("cannot find peer %s!\n", uuid);
66 c = ptlrpc_get_connection(&peer);
73 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn, char *uuid)
75 struct lustre_peer peer;
78 err = kportal_uuid_to_peer(uuid, &peer);
80 CERROR("cannot find peer %s!\n", uuid);
84 memcpy(&conn->c_peer, &peer, sizeof(peer));
88 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *conn)
90 struct ptlrpc_bulk_desc *bulk;
92 OBD_ALLOC(bulk, sizeof(*bulk));
94 bulk->b_connection = ptlrpc_connection_addref(conn);
95 init_waitqueue_head(&bulk->b_waitq);
101 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk)
109 ptlrpc_put_connection(bulk->b_connection);
111 OBD_FREE(bulk, sizeof(*bulk));
115 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl,
116 struct ptlrpc_connection *conn,
117 int opcode, int count, int *lengths,
120 struct ptlrpc_request *request;
124 OBD_ALLOC(request, sizeof(*request));
126 CERROR("request allocation out of memory\n");
130 rc = lustre_pack_msg(count, lengths, bufs,
131 &request->rq_reqlen, &request->rq_reqmsg);
133 CERROR("cannot pack request %d\n", rc);
137 request->rq_type = PTL_RPC_TYPE_REQUEST;
138 request->rq_connection = ptlrpc_connection_addref(conn);
140 request->rq_reqmsg->conn = (__u64)(unsigned long)conn->c_remote_conn;
141 request->rq_reqmsg->token = conn->c_remote_token;
142 request->rq_reqmsg->opc = HTON__u32(opcode);
143 request->rq_reqmsg->type = HTON__u32(PTL_RPC_MSG_REQUEST);
144 INIT_LIST_HEAD(&request->rq_list);
146 /* this will be dec()d once in req_finished, once in free_committed */
147 atomic_set(&request->rq_refcount, 2);
149 spin_lock(&conn->c_lock);
150 request->rq_reqmsg->xid = HTON__u32(++conn->c_xid_out);
151 request->rq_xid = conn->c_xid_out;
152 spin_unlock(&conn->c_lock);
154 request->rq_client = cl;
159 void ptlrpc_req_finished(struct ptlrpc_request *request)
164 if (request->rq_repmsg != NULL) {
165 OBD_FREE(request->rq_repmsg, request->rq_replen);
166 request->rq_repmsg = NULL;
167 request->rq_reply_md.start = NULL;
170 if (atomic_dec_and_test(&request->rq_refcount))
171 ptlrpc_free_req(request);
174 void ptlrpc_free_req(struct ptlrpc_request *request)
179 if (request->rq_repmsg != NULL)
180 OBD_FREE(request->rq_repmsg, request->rq_replen);
181 if (request->rq_reqmsg != NULL)
182 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
184 if (request->rq_client) {
185 spin_lock(&request->rq_client->cli_lock);
186 list_del_init(&request->rq_list);
187 spin_unlock(&request->rq_client->cli_lock);
190 ptlrpc_put_connection(request->rq_connection);
192 OBD_FREE(request, sizeof(*request));
195 static int ptlrpc_check_reply(struct ptlrpc_request *req)
199 if (req->rq_repmsg != NULL) {
200 req->rq_transno = NTOH__u64(req->rq_repmsg->transno);
201 req->rq_flags |= PTL_RPC_FL_REPLIED;
205 if (req->rq_flags & PTL_RPC_FL_RESEND) {
206 CERROR("-- RESEND --\n");
210 if (req->rq_flags & PTL_RPC_FL_RECOVERY) {
211 CERROR("-- RESTART --\n");
216 if (CURRENT_TIME - req->rq_time >= req->rq_timeout) {
217 CERROR("-- REQ TIMEOUT --\n");
218 /* clear the timeout */
220 req->rq_connection->c_level = LUSTRE_CONN_RECOVD;
221 req->rq_flags |= PTL_RPC_FL_TIMEOUT;
222 if (req->rq_client && req->rq_client->cli_recovd)
223 recovd_cli_fail(req->rq_client);
224 if (req->rq_level < LUSTRE_CONN_FULL)
232 if (req->rq_timeout) {
233 schedule_timeout(req->rq_timeout * HZ);
236 if (sigismember(&(current->pending.signal), SIGKILL) ||
237 sigismember(&(current->pending.signal), SIGTERM) ||
238 sigismember(&(current->pending.signal), SIGINT)) {
239 req->rq_flags |= PTL_RPC_FL_INTR;
247 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
252 CERROR("err is %d\n", err);
257 CERROR("req == NULL\n");
261 if (req->rq_repmsg == NULL) {
262 CERROR("req->rq_repmsg == NULL\n");
266 if (req->rq_repmsg->type == NTOH__u32(PTL_RPC_MSG_ERR)) {
267 CERROR("req->rq_repmsg->type == PTL_RPC_MSG_ERR\n");
271 if (req->rq_repmsg->status != 0) {
272 CERROR("req->rq_repmsg->status is %d\n",
273 req->rq_repmsg->status);
274 /* XXX: translate this error from net to host */
275 RETURN(req->rq_repmsg->status);
281 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
283 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
284 request->rq_reqmsg = NULL;
285 request->rq_reqlen = 0;
288 /* Abort this request and cleanup any resources associated with it. */
289 static int ptlrpc_abort(struct ptlrpc_request *request)
291 /* First remove the ME for the reply; in theory, this means
292 * that we can tear down the buffer safely. */
293 PtlMEUnlink(request->rq_reply_me_h);
294 OBD_FREE(request->rq_reply_md.start, request->rq_replen);
295 request->rq_repmsg = NULL;
296 request->rq_replen = 0;
300 /* caller must lock cli */
301 void ptlrpc_free_committed(struct ptlrpc_client *cli)
303 struct list_head *tmp, *saved;
304 struct ptlrpc_request *req;
306 list_for_each_safe(tmp, saved, &cli->cli_sending_head) {
307 req = list_entry(tmp, struct ptlrpc_request, rq_list);
309 if ( (req->rq_flags & PTL_RPC_FL_REPLAY) ) {
310 CDEBUG(D_INFO, "Retaining request %Ld for replay\n",
315 /* not yet committed */
316 if (!req->rq_transno ||
317 req->rq_transno > cli->cli_last_committed)
320 CDEBUG(D_INFO, "Marking request %Ld as committed ("
321 "transno=%Lu, last_committed=%Lu\n",
322 req->rq_xid, req->rq_transno,
323 cli->cli_last_committed);
324 if (atomic_dec_and_test(&req->rq_refcount)) {
325 /* we do this to prevent free_req deadlock */
326 list_del_init(&req->rq_list);
327 req->rq_client = NULL;
328 ptlrpc_free_req(req);
330 list_del_init(&req->rq_list);
331 list_add(&req->rq_list, &cli->cli_dying_head);
339 void ptlrpc_cleanup_client(struct ptlrpc_client *cli)
341 struct list_head *tmp, *saved;
342 struct ptlrpc_request *req;
345 spin_lock(&cli->cli_lock);
346 list_for_each_safe(tmp, saved, &cli->cli_sending_head) {
347 req = list_entry(tmp, struct ptlrpc_request, rq_list);
348 CDEBUG(D_INFO, "Cleaning req %p from sending list.\n", req);
349 list_del_init(&req->rq_list);
350 req->rq_client = NULL;
351 ptlrpc_free_req(req);
353 list_for_each_safe(tmp, saved, &cli->cli_dying_head) {
354 req = list_entry(tmp, struct ptlrpc_request, rq_list);
355 CERROR("Request %p is on the dying list at cleanup!\n", req);
356 list_del_init(&req->rq_list);
357 req->rq_client = NULL;
358 ptlrpc_free_req(req);
360 spin_unlock(&cli->cli_lock);
366 void ptlrpc_continue_req(struct ptlrpc_request *req)
369 CDEBUG(D_INODE, "continue delayed request %Ld opc %d\n",
370 req->rq_xid, req->rq_reqmsg->opc);
371 wake_up_interruptible(&req->rq_wait_for_rep);
375 void ptlrpc_resend_req(struct ptlrpc_request *req)
378 CDEBUG(D_INODE, "resend request %Ld, opc %d\n",
379 req->rq_xid, req->rq_reqmsg->opc);
380 req->rq_status = -EAGAIN;
381 req->rq_level = LUSTRE_CONN_RECOVD;
382 req->rq_flags |= PTL_RPC_FL_RESEND;
383 req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
384 wake_up_interruptible(&req->rq_wait_for_rep);
388 void ptlrpc_restart_req(struct ptlrpc_request *req)
391 CDEBUG(D_INODE, "restart completed request %Ld, opc %d\n",
392 req->rq_xid, req->rq_reqmsg->opc);
393 req->rq_status = -ERESTARTSYS;
394 req->rq_flags |= PTL_RPC_FL_RECOVERY;
395 req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
396 wake_up_interruptible(&req->rq_wait_for_rep);
400 int ptlrpc_queue_wait(struct ptlrpc_request *req)
403 struct ptlrpc_client *cli = req->rq_client;
406 init_waitqueue_head(&req->rq_wait_for_rep);
407 CERROR("subsys: %s req %Ld opc %d level %d, conn level %d\n",
408 cli->cli_name, req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
409 req->rq_connection->c_level);
411 /* XXX probably both an import and connection level are needed */
412 if (req->rq_level > req->rq_connection->c_level) {
413 CERROR("process %d waiting for recovery\n", current->pid);
414 spin_lock(&cli->cli_lock);
415 list_del_init(&req->rq_list);
416 list_add(&req->rq_list, cli->cli_delayed_head.prev);
417 spin_unlock(&cli->cli_lock);
418 wait_event_interruptible
419 (req->rq_wait_for_rep,
420 req->rq_level <= req->rq_connection->c_level);
421 spin_lock(&cli->cli_lock);
422 list_del_init(&req->rq_list);
423 spin_unlock(&cli->cli_lock);
424 CERROR("process %d resumed\n", current->pid);
427 req->rq_time = CURRENT_TIME;
428 req->rq_timeout = 30;
429 rc = ptl_send_rpc(req);
431 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
434 ptlrpc_cleanup_request_buf(req);
435 up(&cli->cli_rpc_sem);
439 spin_lock(&cli->cli_lock);
440 list_del_init(&req->rq_list);
441 list_add(&req->rq_list, cli->cli_sending_head.prev);
442 spin_unlock(&cli->cli_lock);
444 CDEBUG(D_OTHER, "-- sleeping\n");
445 wait_event_interruptible(req->rq_wait_for_rep,
446 ptlrpc_check_reply(req));
447 CDEBUG(D_OTHER, "-- done\n");
449 if (req->rq_flags & PTL_RPC_FL_RESEND) {
450 req->rq_flags &= ~PTL_RPC_FL_RESEND;
454 up(&cli->cli_rpc_sem);
455 if (req->rq_flags & PTL_RPC_FL_INTR) {
456 /* Clean up the dangling reply buffers */
458 GOTO(out, rc = -EINTR);
461 if (! (req->rq_flags & PTL_RPC_FL_REPLIED)) {
462 GOTO(out, rc = req->rq_status);
465 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
467 CERROR("unpack_rep failed: %d\n", rc);
470 CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
471 if (req->rq_repmsg->status == 0)
472 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
473 req->rq_replen, req->rq_repmsg->status);
475 spin_lock(&cli->cli_lock);
476 cli->cli_last_rcvd = req->rq_repmsg->last_rcvd;
477 cli->cli_last_committed = req->rq_repmsg->last_committed;
478 ptlrpc_free_committed(cli);
479 spin_unlock(&cli->cli_lock);
486 int ptlrpc_replay_req(struct ptlrpc_request *req)
489 struct ptlrpc_client *cli = req->rq_client;
492 init_waitqueue_head(&req->rq_wait_for_rep);
493 CERROR("req %Ld opc %d level %d, conn level %d\n",
494 req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
495 req->rq_connection->c_level);
497 req->rq_time = CURRENT_TIME;
499 rc = ptl_send_rpc(req);
501 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
502 ptlrpc_cleanup_request_buf(req);
503 up(&cli->cli_rpc_sem);
507 CDEBUG(D_OTHER, "-- sleeping\n");
508 wait_event_interruptible(req->rq_wait_for_rep,
509 ptlrpc_check_reply(req));
510 CDEBUG(D_OTHER, "-- done\n");
512 up(&cli->cli_rpc_sem);
514 if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
515 CERROR("Unknown reason for wakeup\n");
516 /* XXX Phil - I end up here when I kill obdctl */
518 GOTO(out, rc = -EINTR);
521 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
523 CERROR("unpack_rep failed: %d\n", rc);
527 CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
528 if (req->rq_repmsg->status == 0)
529 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
530 req->rq_replen, req->rq_repmsg->status);
532 CERROR("recovery failed: ");
533 CERROR("req %Ld opc %d level %d, conn level %d\n",
534 req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
535 req->rq_connection->c_level);