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.
23 #define DEBUG_SUBSYSTEM S_RPC
25 #include <linux/obd_support.h>
26 #include <linux/obd_class.h>
27 #include <linux/lustre_lib.h>
28 #include <linux/lustre_ha.h>
29 #include <linux/lustre_import.h>
31 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
32 struct ptlrpc_client *cl)
34 cl->cli_request_portal = req_portal;
35 cl->cli_reply_portal = rep_portal;
39 __u8 *ptlrpc_req_to_uuid(struct ptlrpc_request *req)
41 return req->rq_connection->c_remote_uuid;
44 struct ptlrpc_connection *ptlrpc_uuid_to_connection(obd_uuid_t uuid)
46 struct ptlrpc_connection *c;
47 struct lustre_peer peer;
50 err = kportal_uuid_to_peer(uuid, &peer);
52 CERROR("cannot find peer %s!\n", uuid);
56 c = ptlrpc_get_connection(&peer, uuid);
58 memcpy(c->c_remote_uuid, uuid, sizeof(c->c_remote_uuid));
62 CDEBUG(D_INFO, "%s -> %p\n", uuid, c);
67 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn,obd_uuid_t uuid)
69 struct lustre_peer peer;
72 err = kportal_uuid_to_peer(uuid, &peer);
74 CERROR("cannot find peer %s!\n", uuid);
78 memcpy(&conn->c_peer, &peer, sizeof(peer));
82 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *conn)
84 struct ptlrpc_bulk_desc *desc;
86 OBD_ALLOC(desc, sizeof(*desc));
88 desc->bd_connection = ptlrpc_connection_addref(conn);
89 atomic_set(&desc->bd_refcount, 1);
90 init_waitqueue_head(&desc->bd_waitq);
91 INIT_LIST_HEAD(&desc->bd_page_list);
92 ptl_set_inv_handle(&desc->bd_md_h);
93 ptl_set_inv_handle(&desc->bd_me_h);
99 struct ptlrpc_bulk_page *ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc)
101 struct ptlrpc_bulk_page *bulk;
103 OBD_ALLOC(bulk, sizeof(*bulk));
105 bulk->bp_desc = desc;
106 list_add_tail(&bulk->bp_link, &desc->bd_page_list);
107 desc->bd_page_count++;
112 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
114 struct list_head *tmp, *next;
121 list_for_each_safe(tmp, next, &desc->bd_page_list) {
122 struct ptlrpc_bulk_page *bulk;
123 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
124 ptlrpc_free_bulk_page(bulk);
127 ptlrpc_put_connection(desc->bd_connection);
129 OBD_FREE(desc, sizeof(*desc));
133 void ptlrpc_free_bulk_page(struct ptlrpc_bulk_page *bulk)
141 list_del(&bulk->bp_link);
142 bulk->bp_desc->bd_page_count--;
143 OBD_FREE(bulk, sizeof(*bulk));
147 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, int opcode,
148 int count, int *lengths, char **bufs)
150 struct ptlrpc_connection *conn = imp->imp_connection;
151 struct ptlrpc_request *request;
155 OBD_ALLOC(request, sizeof(*request));
157 CERROR("request allocation out of memory\n");
161 rc = lustre_pack_msg(count, lengths, bufs,
162 &request->rq_reqlen, &request->rq_reqmsg);
164 CERROR("cannot pack request %d\n", rc);
165 OBD_FREE(request, sizeof(*request));
169 request->rq_level = LUSTRE_CONN_FULL;
170 request->rq_type = PTL_RPC_MSG_REQUEST;
171 request->rq_import = imp;
173 /* XXX FIXME bug 625069 */
174 request->rq_request_portal = imp->imp_client->cli_request_portal;
175 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
177 request->rq_connection = ptlrpc_connection_addref(conn);
179 INIT_LIST_HEAD(&request->rq_list);
181 * This will be reduced once when the sender is finished (waiting for
182 * reply, f.e.), and once when the request has been committed and is
183 * removed from the to-be-committed list.
185 * Also, the refcount will be increased in ptl_send_rpc immediately
186 * before we hand it off to portals, and there will be a corresponding
187 * decrease in request_out_cb (which is called to indicate that portals
188 * is finished with the request, and it can be safely freed).
190 * (Except in the DLM server case, where it will be dropped twice
191 * by the sender, and then the last time by request_out_callback.)
193 atomic_set(&request->rq_refcount, 2);
195 spin_lock(&conn->c_lock);
196 request->rq_xid = HTON__u32(++conn->c_xid_out);
197 spin_unlock(&conn->c_lock);
199 request->rq_reqmsg->magic = PTLRPC_MSG_MAGIC;
200 request->rq_reqmsg->version = PTLRPC_MSG_VERSION;
201 request->rq_reqmsg->opc = HTON__u32(opcode);
203 ptlrpc_hdl2req(request, &imp->imp_handle);
207 void ptlrpc_req_finished(struct ptlrpc_request *request)
212 if (atomic_dec_and_test(&request->rq_refcount))
213 ptlrpc_free_req(request);
216 void ptlrpc_free_req(struct ptlrpc_request *request)
219 if (request == NULL) {
224 if (atomic_read(&request->rq_refcount) != 0) {
225 CERROR("freeing request %p (%d->%s:%d) with refcount %d\n",
226 request, request->rq_reqmsg->opc,
227 request->rq_connection->c_remote_uuid,
228 request->rq_import->imp_client->cli_request_portal,
229 request->rq_refcount);
233 if (request->rq_repmsg != NULL) {
234 OBD_FREE(request->rq_repmsg, request->rq_replen);
235 request->rq_repmsg = NULL;
236 request->rq_reply_md.start = NULL;
238 if (request->rq_reqmsg != NULL) {
239 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
240 request->rq_reqmsg = NULL;
243 if (request->rq_connection) {
244 spin_lock(&request->rq_connection->c_lock);
245 list_del_init(&request->rq_list);
246 spin_unlock(&request->rq_connection->c_lock);
249 ptlrpc_put_connection(request->rq_connection);
250 OBD_FREE(request, sizeof(*request));
254 static int ptlrpc_check_reply(struct ptlrpc_request *req)
258 if (req->rq_repmsg != NULL) {
259 struct ptlrpc_connection *conn = req->rq_import->imp_connection;
260 spin_lock(&conn->c_lock);
261 if (req->rq_level > conn->c_level) {
263 "rep to xid "LPD64" op %d to %s:%d: "
264 "recovery started, ignoring (%d > %d)\n",
265 (unsigned long long)req->rq_xid,
266 req->rq_reqmsg->opc, conn->c_remote_uuid,
267 req->rq_import->imp_client->cli_request_portal,
268 req->rq_level, conn->c_level);
269 req->rq_repmsg = NULL;
270 spin_unlock(&conn->c_lock);
273 spin_unlock(&conn->c_lock);
274 req->rq_transno = NTOH__u64(req->rq_repmsg->transno);
275 req->rq_flags |= PTL_RPC_FL_REPLIED;
279 if (req->rq_flags & PTL_RPC_FL_RESEND) {
280 CERROR("-- RESTART --\n");
284 if (req->rq_flags & PTL_RPC_FL_ERR) {
285 CERROR("-- ABORTED --\n");
290 CDEBUG(D_NET, "req = %p, rc = %d\n", req, rc);
294 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
299 CERROR("err is %d\n", err);
304 CERROR("req == NULL\n");
308 if (req->rq_repmsg == NULL) {
309 CERROR("req->rq_repmsg == NULL\n");
313 err = req->rq_repmsg->status;
314 if (req->rq_repmsg->type == NTOH__u32(PTL_RPC_MSG_ERR)) {
315 CERROR("req->rq_repmsg->type == PTL_RPC_MSG_ERR\n");
316 RETURN(err ? err : -EINVAL);
321 CERROR("req->rq_repmsg->status is %d\n", err);
323 CDEBUG(D_INFO, "req->rq_repmsg->status is %d\n", err);
324 /* XXX: translate this error from net to host */
331 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
333 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
334 request->rq_reqmsg = NULL;
335 request->rq_reqlen = 0;
338 /* Abort this request and cleanup any resources associated with it. */
339 static int ptlrpc_abort(struct ptlrpc_request *request)
341 /* First remove the ME for the reply; in theory, this means
342 * that we can tear down the buffer safely. */
343 PtlMEUnlink(request->rq_reply_me_h);
344 OBD_FREE(request->rq_reply_md.start, request->rq_replen);
345 request->rq_repmsg = NULL;
346 request->rq_replen = 0;
350 /* caller must hold conn->c_lock */
351 void ptlrpc_free_committed(struct ptlrpc_connection *conn)
353 struct list_head *tmp, *saved;
354 struct ptlrpc_request *req;
357 list_for_each_safe(tmp, saved, &conn->c_sending_head) {
358 req = list_entry(tmp, struct ptlrpc_request, rq_list);
360 if (req->rq_flags & PTL_RPC_FL_REPLAY) {
361 CDEBUG(D_INFO, "Keeping req %p xid "LPD64" for replay\n",
366 if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
367 CDEBUG(D_INFO, "Keeping in-flight req %p xid "LPD64
368 " for replay\n", req, req->rq_xid);
372 /* not yet committed */
373 if (req->rq_transno > conn->c_last_committed)
376 CDEBUG(D_INFO, "Marking request %p xid %Ld as committed "
377 "transno=%Lu, last_committed=%Lu\n", req,
378 (long long)req->rq_xid, (long long)req->rq_transno,
379 (long long)conn->c_last_committed);
380 if (atomic_dec_and_test(&req->rq_refcount)) {
381 /* We do this to prevent free_req deadlock.
382 * Restarting after each removal is not so bad, as we are
383 * almost always deleting the first item in the list.
385 * If we use a recursive lock here, we can skip the
386 * unlock/lock/restart sequence.
388 spin_unlock(&conn->c_lock);
389 ptlrpc_free_req(req);
390 spin_lock(&conn->c_lock);
393 list_del(&req->rq_list);
394 list_add(&req->rq_list, &conn->c_dying_head);
402 void ptlrpc_cleanup_client(struct obd_import *imp)
404 struct list_head *tmp, *saved;
405 struct ptlrpc_request *req;
406 struct ptlrpc_connection *conn = imp->imp_connection;
412 spin_lock(&conn->c_lock);
413 list_for_each_safe(tmp, saved, &conn->c_sending_head) {
414 req = list_entry(tmp, struct ptlrpc_request, rq_list);
415 if (req->rq_import != imp)
417 /* XXX we should make sure that nobody's sleeping on these! */
418 CDEBUG(D_INFO, "Cleaning req %p from sending list.\n", req);
419 list_del_init(&req->rq_list);
420 req->rq_import = NULL;
421 spin_unlock(&conn->c_lock);
422 ptlrpc_req_finished(req);
426 list_for_each_safe(tmp, saved, &conn->c_dying_head) {
427 req = list_entry(tmp, struct ptlrpc_request, rq_list);
428 if (req->rq_import != imp)
430 CERROR("Request %p is on the dying list at cleanup!\n", req);
431 list_del_init(&req->rq_list);
432 req->rq_import = NULL;
433 spin_unlock(&conn->c_lock);
434 ptlrpc_req_finished(req);
435 spin_lock(&conn->c_lock);
438 spin_unlock(&conn->c_lock);
444 void ptlrpc_continue_req(struct ptlrpc_request *req)
447 CDEBUG(D_HA, "continue delayed request "LPD64" opc %d\n",
448 req->rq_xid, req->rq_reqmsg->opc);
449 req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
450 req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
451 wake_up(&req->rq_wait_for_rep);
455 void ptlrpc_resend_req(struct ptlrpc_request *req)
458 CDEBUG(D_HA, "resend request "LPD64", opc %d\n",
459 req->rq_xid, req->rq_reqmsg->opc);
460 req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
461 req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
462 req->rq_status = -EAGAIN;
463 req->rq_level = LUSTRE_CONN_RECOVD;
464 req->rq_flags |= PTL_RPC_FL_RESEND;
465 req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
466 wake_up(&req->rq_wait_for_rep);
470 void ptlrpc_restart_req(struct ptlrpc_request *req)
473 CDEBUG(D_HA, "restart completed request "LPD64", opc %d\n",
474 req->rq_xid, req->rq_reqmsg->opc);
475 req->rq_status = -ERESTARTSYS;
476 req->rq_flags |= PTL_RPC_FL_RECOVERY;
477 req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
478 wake_up(&req->rq_wait_for_rep);
482 static int expired_request(void *data)
484 struct ptlrpc_request *req = data;
487 CERROR("req xid "LPD64" op %d: timeout on conn to %s:%d\n",
488 (unsigned long long)req->rq_xid, req->rq_reqmsg->opc,
489 req->rq_connection->c_remote_uuid,
490 req->rq_import->imp_client->cli_request_portal);
491 req->rq_flags |= PTL_RPC_FL_TIMEOUT;
492 if (!req->rq_import->imp_connection->c_recovd_data.rd_recovd)
496 req->rq_connection->c_level = LUSTRE_CONN_RECOVD;
497 recovd_conn_fail(req->rq_import->imp_connection);
499 /* If this request is for recovery or other primordial tasks,
500 * don't go back to sleep.
502 if (req->rq_level < LUSTRE_CONN_FULL)
507 static int interrupted_request(void *data)
509 struct ptlrpc_request *req = data;
511 req->rq_flags |= PTL_RPC_FL_INTR;
512 RETURN(1); /* ignored, as of this writing */
515 /* If we're being torn down by umount -f, or the import has been
516 * invalidated (such as by an OST failure), the request must fail with
519 * Must be called with conn->c_lock held, will drop it if it returns -EIO.
521 * XXX this should just be testing the import, and umount_begin shouldn't touch
522 * XXX the connection.
524 #define EIO_IF_INVALID(conn, req) \
525 if ((conn->c_flags & CONN_INVALID) || \
526 (req->rq_import->imp_flags & IMP_INVALID)) { \
527 CERROR("req xid "LPD64" op %d to %s:%d: %s_INVALID\n", \
528 (unsigned long long)req->rq_xid, req->rq_reqmsg->opc, \
529 req->rq_connection->c_remote_uuid, \
530 req->rq_import->imp_client->cli_request_portal, \
531 (conn->c_flags & CONN_INVALID) ? "CONN_" : "IMP_"); \
532 spin_unlock(&conn->c_lock); \
536 int ptlrpc_queue_wait(struct ptlrpc_request *req)
539 struct l_wait_info lwi;
540 struct ptlrpc_client *cli = req->rq_import->imp_client;
541 struct ptlrpc_connection *conn = req->rq_import->imp_connection;
544 init_waitqueue_head(&req->rq_wait_for_rep);
545 CDEBUG(D_NET, "subsys: %s req "LPD64" opc %d level %d, conn level %d\n",
546 cli->cli_name, req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
547 req->rq_connection->c_level);
549 /* XXX probably both an import and connection level are needed */
550 if (req->rq_level > conn->c_level) {
551 spin_lock(&conn->c_lock);
552 EIO_IF_INVALID(conn, req);
553 list_del(&req->rq_list);
554 list_add_tail(&req->rq_list, &conn->c_delayed_head);
555 spin_unlock(&conn->c_lock);
557 CDEBUG(D_HA, "req xid "LPD64" op %d to %s:%d: waiting for "
558 "recovery (%d < %d)\n",
559 (unsigned long long)req->rq_xid, req->rq_reqmsg->opc,
560 req->rq_connection->c_remote_uuid,
561 req->rq_import->imp_client->cli_request_portal,
562 req->rq_level, conn->c_level);
564 lwi = LWI_INTR(NULL, NULL);
565 rc = l_wait_event(req->rq_wait_for_rep,
566 (req->rq_level <= conn->c_level) ||
567 (req->rq_flags & PTL_RPC_FL_ERR), &lwi);
569 spin_lock(&conn->c_lock);
570 list_del_init(&req->rq_list);
571 spin_unlock(&conn->c_lock);
573 if (req->rq_flags & PTL_RPC_FL_ERR)
579 CERROR("process %d resumed\n", current->pid);
582 req->rq_timeout = obd_timeout;
583 spin_lock(&conn->c_lock);
584 EIO_IF_INVALID(conn, req);
586 list_del(&req->rq_list);
587 list_add_tail(&req->rq_list, &conn->c_sending_head);
588 spin_unlock(&conn->c_lock);
589 rc = ptl_send_rpc(req);
591 CDEBUG(D_HA, "error %d, opcode %d, need recovery\n", rc,
592 req->rq_reqmsg->opc);
593 /* the sleep below will time out, triggering recovery */
596 CDEBUG(D_NET, "-- sleeping on req xid "LPD64" op %d to %s:%d\n",
597 (unsigned long long)req->rq_xid, req->rq_reqmsg->opc,
598 req->rq_connection->c_remote_uuid,
599 req->rq_import->imp_client->cli_request_portal);
600 lwi = LWI_TIMEOUT_INTR(req->rq_timeout * HZ, expired_request,
601 interrupted_request,req);
602 l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
603 CDEBUG(D_NET, "-- done sleeping on req xid "LPD64" op %d to %s:%d\n",
604 (unsigned long long)req->rq_xid, req->rq_reqmsg->opc,
605 req->rq_connection->c_remote_uuid,
606 req->rq_import->imp_client->cli_request_portal);
608 if (req->rq_flags & PTL_RPC_FL_ERR) {
610 GOTO(out, rc = -EIO);
613 /* Don't resend if we were interrupted. */
614 if ((req->rq_flags & (PTL_RPC_FL_RESEND | PTL_RPC_FL_INTR)) ==
616 req->rq_flags &= ~PTL_RPC_FL_RESEND;
617 CDEBUG(D_HA, "resending req xid "LPD64" op %d to %s:%d\n",
618 (unsigned long long)req->rq_xid, req->rq_reqmsg->opc,
619 req->rq_connection->c_remote_uuid,
620 req->rq_import->imp_client->cli_request_portal);
624 // up(&cli->cli_rpc_sem);
625 if (req->rq_flags & PTL_RPC_FL_INTR) {
626 if (!(req->rq_flags & PTL_RPC_FL_TIMEOUT))
627 LBUG(); /* should only be interrupted if we timed out */
628 /* Clean up the dangling reply buffers */
630 GOTO(out, rc = -EINTR);
633 if (req->rq_flags & PTL_RPC_FL_TIMEOUT)
634 GOTO(out, rc = -ETIMEDOUT);
636 if (!(req->rq_flags & PTL_RPC_FL_REPLIED))
637 GOTO(out, rc = req->rq_status);
639 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
641 CERROR("unpack_rep failed: %d\n", rc);
645 /* FIXME: Enable when BlueArc makes new release */
646 if (req->rq_repmsg->type != PTL_RPC_MSG_REPLY &&
647 req->rq_repmsg->type != PTL_RPC_MSG_ERR) {
648 CERROR("invalid packet type received (type=%u)\n",
649 req->rq_repmsg->type);
651 GOTO(out, rc = -EINVAL);
654 CDEBUG(D_NET, "got rep "LPD64"\n", req->rq_xid);
655 if (req->rq_repmsg->status == 0)
656 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
657 req->rq_replen, req->rq_repmsg->status);
659 spin_lock(&conn->c_lock);
660 conn->c_last_xid = req->rq_repmsg->last_xid;
661 conn->c_last_committed = req->rq_repmsg->last_committed;
662 ptlrpc_free_committed(conn);
663 spin_unlock(&conn->c_lock);
670 #undef EIO_IF_INVALID
672 int ptlrpc_replay_req(struct ptlrpc_request *req)
674 int rc = 0, old_level;
675 // struct ptlrpc_client *cli = req->rq_import->imp_client;
676 struct l_wait_info lwi;
679 init_waitqueue_head(&req->rq_wait_for_rep);
680 CDEBUG(D_NET, "req "LPD64" opc %d level %d, conn level %d\n",
681 req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
682 req->rq_connection->c_level);
684 req->rq_timeout = obd_timeout;
685 req->rq_reqmsg->addr = req->rq_import->imp_handle.addr;
686 req->rq_reqmsg->cookie = req->rq_import->imp_handle.cookie;
688 /* temporarily set request to RECOVD level (reset at out:) */
689 old_level = req->rq_level;
690 req->rq_level = LUSTRE_CONN_RECOVD;
691 rc = ptl_send_rpc(req);
693 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
694 ptlrpc_cleanup_request_buf(req);
695 // up(&cli->cli_rpc_sem);
699 CDEBUG(D_OTHER, "-- sleeping\n");
700 lwi = LWI_INTR(NULL, NULL); /* XXX needs timeout, nested recovery */
701 l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
702 CDEBUG(D_OTHER, "-- done\n");
704 // up(&cli->cli_rpc_sem);
706 if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
707 CERROR("Unknown reason for wakeup\n");
708 /* XXX Phil - I end up here when I kill obdctl */
710 GOTO(out, rc = -EINTR);
713 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
715 CERROR("unpack_rep failed: %d\n", rc);
719 CDEBUG(D_NET, "got rep "LPD64"\n", req->rq_xid);
721 /* let the callback do fixups, possibly including in the request */
722 if (req->rq_replay_cb)
723 req->rq_replay_cb(req, req->rq_replay_cb_data);
725 if (req->rq_repmsg->status == 0) {
726 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
727 req->rq_replen, req->rq_repmsg->status);
729 CERROR("recovery failed: ");
730 CERROR("req "LPD64" opc %d level %d, conn level %d\n",
731 req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
732 req->rq_connection->c_level);
737 req->rq_level = old_level;