Whamcloud - gitweb
- documentation update for MDS recovery
[fs/lustre-release.git] / lustre / ptlrpc / niobuf.c
index f019560..caf94a4 100644 (file)
@@ -1,4 +1,3 @@
-
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
 
 #define EXPORT_SYMTAB
 
-#include <linux/config.h>
-#include <linux/module.h>
-#include <linux/kernel.h>
-
 #define DEBUG_SUBSYSTEM S_RPC
 
-#include <linux/obd_support.h>
-#include <linux/obd_class.h>
 #include <linux/lustre_net.h>
 
-extern ptl_handle_eq_t bulk_source_eq, sent_pkt_eq, rcvd_rep_eq, bulk_sink_eq;
+extern ptl_handle_eq_t request_out_eq, 
+        reply_in_eq, 
+        reply_out_eq,
+        bulk_source_eq, 
+        bulk_sink_eq;
+static ptl_process_id_t local_id = {PTL_ID_ANY, PTL_ID_ANY};
 
-int ptl_send_buf(struct ptlrpc_request *request, struct lustre_peer *peer,
-                 int portal, int is_request)
+
+int ptlrpc_check_bulk_sent(struct ptlrpc_bulk_desc *bulk)
+{
+        ENTRY;
+
+        if (bulk->b_flags & PTL_BULK_FL_SENT)
+                RETURN(1);
+
+        if (sigismember(&(current->pending.signal), SIGKILL) ||
+            sigismember(&(current->pending.signal), SIGINT)) {
+                bulk->b_flags |= PTL_RPC_FL_INTR;
+                RETURN(1);
+        }
+
+        CDEBUG(D_NET, "no event yet\n");
+        RETURN(0);
+}
+
+static int ptl_send_buf(struct ptlrpc_request *request,
+                        struct ptlrpc_connection *conn, int portal)
 {
         int rc;
         ptl_process_id_t remote_id;
         ptl_handle_md_t md_h;
+        ptl_ack_req_t ack;
+
+        request->rq_req_md.user_ptr = request;
 
-        /* FIXME: This is bad. */
-        if (request->rq_bulklen) {
-                request->rq_req_md.start = request->rq_bulkbuf;
-                request->rq_req_md.length = request->rq_bulklen;
-                request->rq_req_md.eventq = bulk_source_eq;
-        } else if (is_request) {
-                request->rq_req_md.start = request->rq_reqbuf;
+        switch (request->rq_type) {
+        case PTL_RPC_TYPE_REQUEST:
+                request->rq_req_md.start = request->rq_reqmsg;
                 request->rq_req_md.length = request->rq_reqlen;
-                request->rq_req_md.eventq = sent_pkt_eq;
-        } else {
-                request->rq_req_md.start = request->rq_repbuf;
+                request->rq_req_md.eventq = request_out_eq;
+                request->rq_req_md.threshold = 1;
+                ack = PTL_NOACK_REQ;
+                break;
+        case PTL_RPC_TYPE_REPLY:
+                request->rq_req_md.start = request->rq_repmsg;
                 request->rq_req_md.length = request->rq_replen;
-                request->rq_req_md.eventq = sent_pkt_eq;
+                request->rq_req_md.eventq = reply_out_eq;
+                request->rq_req_md.threshold = 1;
+                ack = PTL_NOACK_REQ;
+                break;
+        default:
+                LBUG();
+                return -1; /* notreached */
         }
-        request->rq_req_md.threshold = 1;
         request->rq_req_md.options = PTL_MD_OP_PUT;
         request->rq_req_md.user_ptr = request;
 
-        rc = PtlMDBind(peer->peer_ni, request->rq_req_md, &md_h);
+        rc = PtlMDBind(conn->c_peer.peer_ni, request->rq_req_md, &md_h);
+        //CERROR("MDBind (outgoing req/rep/bulk): %Lu\n", (__u64)md_h);
         if (rc != 0) {
-                BUG();
                 CERROR("PtlMDBind failed: %d\n", rc);
+                LBUG();
                 return rc;
         }
 
-        remote_id.addr_kind = PTL_ADDR_NID;
-        remote_id.nid = peer->peer_nid;
+        remote_id.nid = conn->c_peer.peer_nid;
         remote_id.pid = 0;
 
-        if (request->rq_bulklen) {
-                rc = PtlPut(md_h, PTL_ACK_REQ, remote_id, portal, 0,
-                            request->rq_xid, 0, 0);
-        } else {
-                rc = PtlPut(md_h, PTL_NOACK_REQ, remote_id, portal, 0,
-                            request->rq_xid, 0, 0);
+        CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %d\n",
+               request->rq_req_md.length, portal, request->rq_reqmsg->xid);
+
+        rc = PtlPut(md_h, ack, remote_id, portal, 0, request->rq_reqmsg->xid,
+                    0, 0);
+        if (rc != PTL_OK) {
+                CERROR("PtlPut(%d, %d, %d) failed: %d\n", remote_id.nid,
+                       portal, request->rq_reqmsg->xid, rc);
+                PtlMDUnlink(md_h);
         }
+
+        return rc;
+}
+
+int ptlrpc_send_bulk(struct ptlrpc_bulk_desc *bulk, int portal)
+{
+        int rc;
+        ptl_process_id_t remote_id;
+
+        bulk->b_md.start = bulk->b_buf;
+        bulk->b_md.length = bulk->b_buflen;
+        bulk->b_md.eventq = bulk_source_eq;
+        bulk->b_md.threshold = 2; /* SENT and ACK events */
+        bulk->b_md.options = PTL_MD_OP_PUT;
+        bulk->b_md.user_ptr = bulk;
+
+        rc = PtlMDBind(bulk->b_connection->c_peer.peer_ni, bulk->b_md,
+                       &bulk->b_md_h);
+        if (rc != 0) {
+                CERROR("PtlMDBind failed: %d\n", rc);
+                LBUG();
+                return rc;
+        }
+
+        remote_id.nid = bulk->b_connection->c_peer.peer_nid;
+        remote_id.pid = 0;
+
+        CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %d\n",
+               bulk->b_md.length, portal, bulk->b_xid);
+
+        rc = PtlPut(bulk->b_md_h, PTL_ACK_REQ, remote_id, portal, 0,
+                    bulk->b_xid, 0, 0);
         if (rc != PTL_OK) {
-                BUG();
                 CERROR("PtlPut(%d, %d, %d) failed: %d\n", remote_id.nid,
-                       portal, request->rq_xid, rc);
-                /* FIXME: tear down md */
+                       portal, bulk->b_xid, rc);
+                PtlMDUnlink(bulk->b_md_h);
+                LBUG();
         }
 
         return rc;
 }
 
-int ptl_send_rpc(struct ptlrpc_request *request, struct lustre_peer *peer)
+int ptlrpc_register_bulk(struct ptlrpc_bulk_desc *bulk)
+{
+        int rc;
+        ENTRY;
+
+        rc = PtlMEAttach(bulk->b_connection->c_peer.peer_ni, bulk->b_portal,
+                         local_id, bulk->b_xid, 0, PTL_UNLINK, PTL_INS_AFTER,
+                         &bulk->b_me_h);
+        if (rc != PTL_OK) {
+                CERROR("PtlMEAttach failed: %d\n", rc);
+                LBUG();
+                GOTO(cleanup, rc);
+        }
+
+        bulk->b_md.start = bulk->b_buf;
+        bulk->b_md.length = bulk->b_buflen;
+        bulk->b_md.threshold = 1;
+        bulk->b_md.options = PTL_MD_OP_PUT;
+        bulk->b_md.user_ptr = bulk;
+        bulk->b_md.eventq = bulk_sink_eq;
+
+        rc = PtlMDAttach(bulk->b_me_h, bulk->b_md, PTL_UNLINK, &bulk->b_md_h);
+        //CERROR("MDAttach (bulk sink): %Lu\n", (__u64)bulk->b_md_h);
+        if (rc != PTL_OK) {
+                CERROR("PtlMDAttach failed: %d\n", rc);
+                LBUG();
+                GOTO(cleanup, rc);
+        }
+
+        CDEBUG(D_NET, "Setup bulk sink buffer: %u bytes, xid %u, portal %u\n",
+               bulk->b_buflen, bulk->b_xid, bulk->b_portal);
+        RETURN(0);
+
+        // XXX Confirm that this is safe!
+ cleanup:
+        PtlMEUnlink(bulk->b_me_h);
+        return rc;
+}
+
+int ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *bulk)
+{
+        int rc;
+
+        rc = PtlMEUnlink(bulk->b_me_h);
+        if (rc != PTL_OK)
+                CERROR("PtlMEUnlink failed: %d\n", rc);
+
+        return rc;
+}
+
+int ptlrpc_reply(struct ptlrpc_service *svc, struct ptlrpc_request *req)
+{
+        /* FIXME: we need to increment the count of handled events */
+        req->rq_type = PTL_RPC_TYPE_REPLY;
+        req->rq_repmsg->conn = req->rq_connection->c_remote_conn;
+        req->rq_repmsg->token = req->rq_connection->c_remote_token;
+        req->rq_repmsg->xid = HTON__u32(req->rq_reqmsg->xid);
+        req->rq_repmsg->status = HTON__u32(req->rq_status);
+        req->rq_reqmsg->type = HTON__u32(req->rq_type);
+        return ptl_send_buf(req, req->rq_connection, svc->srv_rep_portal);
+}
+
+int ptlrpc_error(struct ptlrpc_service *svc, struct ptlrpc_request *req)
+{
+        int rc;
+        ENTRY;
+
+        if (req->rq_repmsg) {
+                CERROR("req already has repmsg\n");
+                LBUG();
+        }
+
+        rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
+        if (rc)
+                RETURN(rc);
+
+        req->rq_repmsg->type = HTON__u32(PTL_RPC_MSG_ERR);
+
+        rc = ptlrpc_reply(svc, req);
+        RETURN(rc);
+}
+
+void ptlrpc_resend_req(struct ptlrpc_request *req)
+{
+        ENTRY;
+        req->rq_flags |= PTL_RPC_FL_RESEND;
+        req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
+        wake_up_interruptible(&req->rq_wait_for_rep);
+        EXIT;
+        return; 
+}
+
+int ptl_send_rpc(struct ptlrpc_request *request)
 {
-        ptl_handle_me_t me_h, bulk_me_h;
         ptl_process_id_t local_id;
         int rc;
         char *repbuf;
 
         ENTRY;
 
+        if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
+                CERROR("wrong packet type sent %d\n",
+                       NTOH__u32(request->rq_reqmsg->type));
+                LBUG();
+                RETURN(-EINVAL);
+        }
         if (request->rq_replen == 0) {
                 CERROR("request->rq_replen is 0!\n");
-                EXIT;
-                return -EINVAL;
+                RETURN(-EINVAL);
         }
 
-        /* request->rq_repbuf is set only when the reply comes in, in
+        /* request->rq_repmsg is set only when the reply comes in, in
          * client_packet_callback() */
         OBD_ALLOC(repbuf, request->rq_replen);
-        if (!repbuf) { 
-                EXIT;
-                return -ENOMEM;
-        }
+        if (!repbuf)
+                RETURN(-ENOMEM);
 
-        local_id.addr_kind = PTL_ADDR_GID;
-        local_id.gid = PTL_ID_ANY;
-        local_id.rid = PTL_ID_ANY;
+        local_id.nid = PTL_ID_ANY;
+        local_id.pid = PTL_ID_ANY;
 
-        //CERROR("sending req %d\n", request->rq_xid);
-        rc = PtlMEAttach(peer->peer_ni, request->rq_reply_portal, local_id,
-                         request->rq_xid, 0, PTL_UNLINK, &me_h);
+        down(&request->rq_client->cli_rpc_sem);
+
+        rc = PtlMEAttach(request->rq_connection->c_peer.peer_ni,
+                         request->rq_client->cli_reply_portal,
+                         local_id, request->rq_reqmsg->xid, 0, PTL_UNLINK,
+                         PTL_INS_AFTER, &request->rq_reply_me_h);
         if (rc != PTL_OK) {
                 CERROR("PtlMEAttach failed: %d\n", rc);
-                BUG();
-                EXIT;
-                goto cleanup;
+                LBUG();
+                GOTO(cleanup, rc);
         }
 
+        request->rq_type = PTL_RPC_TYPE_REQUEST;
         request->rq_reply_md.start = repbuf;
         request->rq_reply_md.length = request->rq_replen;
         request->rq_reply_md.threshold = 1;
         request->rq_reply_md.options = PTL_MD_OP_PUT;
         request->rq_reply_md.user_ptr = request;
-        request->rq_reply_md.eventq = rcvd_rep_eq;
+        request->rq_reply_md.eventq = reply_in_eq;
 
-        rc = PtlMDAttach(me_h, request->rq_reply_md, PTL_UNLINK,
-                         &request->rq_reply_md_h);
+        rc = PtlMDAttach(request->rq_reply_me_h, request->rq_reply_md,
+                         PTL_UNLINK, &request->rq_reply_md_h);
         if (rc != PTL_OK) {
                 CERROR("PtlMDAttach failed: %d\n", rc);
-                BUG();
-                EXIT;
-                goto cleanup2;
-        }
-
-        if (request->rq_bulklen != 0) {
-                rc = PtlMEAttach(peer->peer_ni, request->rq_bulk_portal,
-                                 local_id, request->rq_xid, 0, PTL_UNLINK,
-                                 &bulk_me_h);
-                if (rc != PTL_OK) {
-                        CERROR("PtlMEAttach failed: %d\n", rc);
-                        BUG();
-                        EXIT;
-                        goto cleanup3;
-                }
-
-                request->rq_bulk_md.start = request->rq_bulkbuf;
-                request->rq_bulk_md.length = request->rq_bulklen;
-                request->rq_bulk_md.threshold = 1;
-                request->rq_bulk_md.options = PTL_MD_OP_PUT;
-                request->rq_bulk_md.user_ptr = request;
-                request->rq_bulk_md.eventq = bulk_sink_eq;
-
-                rc = PtlMDAttach(bulk_me_h, request->rq_bulk_md, PTL_UNLINK,
-                                 &request->rq_bulk_md_h);
-                if (rc != PTL_OK) {
-                        CERROR("PtlMDAttach failed: %d\n", rc);
-                        BUG();
-                        EXIT;
-                        goto cleanup4;
-                }
-        }
-
-        return ptl_send_buf(request, peer, request->rq_req_portal, 1);
-
- cleanup4:
-        PtlMEUnlink(bulk_me_h);
- cleanup3:
-        PtlMDUnlink(request->rq_reply_md_h);
+                LBUG();
+                GOTO(cleanup2, rc);
+        }
+
+        CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid %u, portal %u\n",
+               request->rq_replen, request->rq_reqmsg->xid,
+               request->rq_client->cli_reply_portal);
+
+        spin_lock(&request->rq_client->cli_lock);
+        list_add(&request->rq_list, &request->rq_client->cli_sending_head);
+        spin_unlock(&request->rq_client->cli_lock);
+
+        rc = ptl_send_buf(request, request->rq_connection,
+                          request->rq_client->cli_request_portal);
+        RETURN(rc);
+
  cleanup2:
-        PtlMEUnlink(me_h);
+        PtlMEUnlink(request->rq_reply_me_h);
  cleanup:
         OBD_FREE(repbuf, request->rq_replen);
+        up(&request->rq_client->cli_rpc_sem);
 
         return rc;
 }
 
-/* ptl_received_rpc() should be called by the sleeping process once
+void ptlrpc_link_svc_me(struct ptlrpc_service *service, int i)
+{
+        int rc;
+        ptl_md_t dummy;
+        ptl_handle_md_t md_h;
+
+        /* Attach the leading ME on which we build the ring */
+        rc = PtlMEAttach(service->srv_self.peer_ni, service->srv_req_portal,
+                         service->srv_id, 0, ~0, PTL_RETAIN, PTL_INS_BEFORE,
+                         &(service->srv_me_h[i]));
+        if (rc != PTL_OK) {
+                CERROR("PtlMEAttach failed: %d\n", rc);
+                LBUG();
+        }
+        
+        if (service->srv_ref_count[i])
+                LBUG();
+
+        dummy.start         = service->srv_buf[i];
+        dummy.length        = service->srv_buf_size;
+        dummy.max_offset    = service->srv_buf_size;
+        dummy.threshold     = PTL_MD_THRESH_INF;
+        dummy.options       = PTL_MD_OP_PUT | PTL_MD_AUTO_UNLINK;
+        dummy.user_ptr      = service;
+        dummy.eventq        = service->srv_eq_h;
+        dummy.max_offset    = service->srv_buf_size;
+        
+        rc = PtlMDAttach(service->srv_me_h[i], dummy, PTL_UNLINK, &md_h);
+        if (rc != PTL_OK) {
+                /* cleanup */
+                CERROR("PtlMDAttach failed: %d\n", rc);
+                LBUG();
+        }
+}        
+
+/* ptl_handled_rpc() should be called by the sleeping process once
  * it finishes processing an event.  This ensures the ref count is
  * decremented and that the rpc ring buffer cycles properly.
  */ 
-int ptl_received_rpc(struct ptlrpc_service *service) {
-        int rc, index;
+int ptl_handled_rpc(struct ptlrpc_service *service, void *start) 
+{
+        int index;
+
+        spin_lock(&service->srv_lock);
+
+        for (index = 0; index < service->srv_ring_length; index++)
+                if (service->srv_buf[index] == start) 
+                        break;
+
+        if (index == service->srv_ring_length)
+                LBUG();
 
-        index = service->srv_md_active;
         CDEBUG(D_INFO, "MD index=%d Ref Count=%d\n", index,
                service->srv_ref_count[index]);
         service->srv_ref_count[index]--;
 
-        if ((service->srv_ref_count[index] <= 0) &&
-            (service->srv_me_h[index] == 0)) {
-
-                /* Replace the unlinked ME and MD */
-                rc = PtlMEInsert(service->srv_me_h[service->srv_me_tail],
-                                 service->srv_id, 0, ~0, PTL_RETAIN,
-                                 PTL_INS_AFTER, &(service->srv_me_h[index]));
-                CERROR("Inserting new ME and MD in ring, rc %d\n", rc);
-                service->srv_me_tail = index;
-                service->srv_ref_count[index] = 0;
-                
-                if (rc != PTL_OK) {
-                        CERROR("PtlMEInsert failed: %d\n", rc);
-                        BUG();
-                        return rc;
-                }
-
-                service->srv_md[index].start        = service->srv_buf[index];
-                service->srv_md[index].length       = service->srv_buf_size;
-                service->srv_md[index].threshold    = PTL_MD_THRESH_INF;
-                service->srv_md[index].options      = PTL_MD_OP_PUT;
-                service->srv_md[index].user_ptr     = service;
-                service->srv_md[index].eventq       = service->srv_eq_h;
-
-                rc = PtlMDAttach(service->srv_me_h[index],
-                                 service->srv_md[index],
-                                 PTL_RETAIN, &(service->srv_md_h[index]));
-
-                CDEBUG(D_INFO, "Attach MD in ring, rc %d\n", rc);
-                if (rc != PTL_OK) {
-                        /* XXX cleanup */
-                        BUG();
-                        CERROR("PtlMDAttach failed: %d\n", rc);
-                        return rc;
-                }
-
-                service->srv_md_active =
-                        NEXT_INDEX(index, service->srv_ring_length);
-        } 
+        if (service->srv_ref_count[index] < 0)
+                LBUG();
+
+        if (service->srv_ref_count[index] == 0 &&
+            !ptl_is_valid_handle(&(service->srv_me_h[index]))) {
+                CDEBUG(D_NET, "relinking %d\n", index); 
+                ptlrpc_link_svc_me(service, index); 
+        }
         
+        spin_unlock(&service->srv_lock);
         return 0;
 }