Whamcloud - gitweb
Send the correct (I think) client-LDLM export address when reconnecting.
[fs/lustre-release.git] / lustre / ptlrpc / niobuf.c
index ddb2cec..46aa30f 100644 (file)
  *
  */
 
-#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>
+#include <linux/lustre_lib.h>
+#include <linux/obd.h>
 
-extern ptl_handle_eq_t bulk_source_eq, sent_pkt_eq, rcvd_rep_eq, bulk_sink_eq;
-static ptl_process_id_t local_id = {PTL_ADDR_GID, PTL_ID_ANY, PTL_ID_ANY};
-
-
-int ptlrpc_check_bulk_sent(struct ptlrpc_bulk_desc *bulk)
-{
-        if (bulk->b_flags == PTL_BULK_SENT) {
-                EXIT;
-                return 1;
-        }
-
-        if (sigismember(&(current->pending.signal), SIGKILL) ||
-            sigismember(&(current->pending.signal), SIGINT)) {
-                bulk->b_flags = PTL_RPC_INTR;
-                EXIT;
-                return 1;
-        }
-
-        CDEBUG(D_NET, "no event yet\n");
-        return 0;
-}
+extern ptl_handle_eq_t request_out_eq, reply_in_eq, reply_out_eq,
+        bulk_source_eq, bulk_sink_eq;
 
-int ptl_send_buf(struct ptlrpc_request *request, struct lustre_peer *peer,
-                 int portal)
+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;
+
+        LASSERT(conn);
+
+        request->rq_req_md.user_ptr = request;
 
         switch (request->rq_type) {
-        case PTL_RPC_BULK:
-                request->rq_req_md.start = request->rq_bulkbuf;
-                request->rq_req_md.length = request->rq_bulklen;
-                request->rq_req_md.eventq = bulk_source_eq;
-                request->rq_req_md.threshold = 2; /* SENT and ACK events */
-                ack = PTL_ACK_REQ;
-                break;
-        case PTL_RPC_REQUEST:
-                request->rq_req_md.start = request->rq_reqbuf;
+        case PTL_RPC_MSG_REQUEST:
+                request->rq_reqmsg->type = HTON__u32(request->rq_type);
+                request->rq_req_md.start = request->rq_reqmsg;
                 request->rq_req_md.length = request->rq_reqlen;
-                request->rq_req_md.eventq = sent_pkt_eq;
-                request->rq_req_md.threshold = 1;
-                ack = PTL_NOACK_REQ;
+                request->rq_req_md.eventq = request_out_eq;
                 break;
-        case PTL_RPC_REPLY:
-                request->rq_req_md.start = request->rq_repbuf;
+        case PTL_RPC_MSG_ERR:
+        case PTL_RPC_MSG_REPLY:
+                request->rq_repmsg->type = HTON__u32(request->rq_type);
+                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.threshold = 1;
-                ack = PTL_NOACK_REQ;
+                request->rq_req_md.eventq = reply_out_eq;
                 break;
         default:
-                BUG();
+                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);
-        //CERROR("MDBind (outgoing req/rep/bulk): %Lu\n", (__u64)md_h);
+        rc = PtlMDBind(conn->c_peer.peer_ni, request->rq_req_md, &md_h);
         if (rc != 0) {
                 CERROR("PtlMDBind failed: %d\n", rc);
-                BUG();
+                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;
 
-        CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %d\n",
+        CDEBUG(D_NET, "Sending %d bytes to portal %d, xid "LPD64"\n",
                request->rq_req_md.length, portal, request->rq_xid);
 
-        rc = PtlPut(md_h, ack, remote_id, portal, 0, request->rq_xid, 0, 0);
+        if (!portal)
+                LBUG();
+        rc = PtlPut(md_h, PTL_NOACK_REQ, remote_id, portal, 0, request->rq_xid,
+                    0, 0);
         if (rc != PTL_OK) {
-                CERROR("PtlPut(%d, %d, %d) failed: %d\n", remote_id.nid,
-                       portal, request->rq_xid, rc);
+                CERROR("PtlPut("LPU64", %d, "LPD64") failed: %d\n",
+                       remote_id.nid, portal, request->rq_xid, rc);
                 PtlMDUnlink(md_h);
         }
 
         return rc;
 }
 
-int ptlrpc_send_bulk(struct ptlrpc_bulk_desc *bulk, int portal)
+static inline struct iovec *
+ptlrpc_get_bulk_iov (struct ptlrpc_bulk_desc *desc)
+{
+        struct iovec *iov;
+
+        if (desc->bd_page_count <= sizeof (desc->bd_iov)/sizeof (struct iovec))
+                return (desc->bd_iov);
+
+        OBD_ALLOC (iov, desc->bd_page_count * sizeof (struct iovec));
+        if (iov == NULL)
+                LBUG();
+
+        return (iov);
+}
+
+static inline void
+ptlrpc_put_bulk_iov (struct ptlrpc_bulk_desc *desc, struct iovec *iov)
+{
+        if (desc->bd_page_count <= sizeof (desc->bd_iov)/sizeof (struct iovec))
+                return;
+
+        OBD_FREE (iov, desc->bd_page_count * sizeof (struct iovec));
+}
+
+int ptlrpc_send_bulk(struct ptlrpc_bulk_desc *desc)
 {
         int rc;
+        struct list_head *tmp, *next;
         ptl_process_id_t remote_id;
-        ptl_handle_md_t md_h;
+        __u32 xid = 0;
+        struct iovec *iov;
+        ENTRY;
 
-        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;
+        iov = ptlrpc_get_bulk_iov (desc);
+        if (iov == NULL)
+                RETURN (-ENOMEM);
 
-        rc = PtlMDBind(bulk->b_peer.peer_ni, bulk->b_md, &md_h);
-        if (rc != 0) {
+        desc->bd_md.start = iov;
+        desc->bd_md.niov = 0;
+        desc->bd_md.length = 0;
+        desc->bd_md.eventq = bulk_source_eq;
+        desc->bd_md.threshold = 2; /* SENT and ACK */
+        desc->bd_md.options = PTL_MD_OP_PUT | PTL_MD_IOV;
+        desc->bd_md.user_ptr = desc;
+
+        atomic_set (&desc->bd_source_callback_count, 2);
+
+        list_for_each_safe(tmp, next, &desc->bd_page_list) {
+                struct ptlrpc_bulk_page *bulk;
+                bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
+
+                LASSERT (desc->bd_md.niov < desc->bd_page_count);
+
+                if (desc->bd_md.niov == 0)
+                        xid = bulk->bp_xid;
+                LASSERT (xid == bulk->bp_xid);   /* should all be the same */
+
+                iov[desc->bd_md.niov].iov_base = bulk->bp_buf;
+                iov[desc->bd_md.niov].iov_len = bulk->bp_buflen;
+                desc->bd_md.niov++;
+                desc->bd_md.length += bulk->bp_buflen;
+        }
+
+        LASSERT (desc->bd_md.niov == desc->bd_page_count);
+        LASSERT (desc->bd_md.niov != 0);
+
+        rc = PtlMDBind(desc->bd_connection->c_peer.peer_ni, desc->bd_md,
+                       &desc->bd_md_h);
+
+        ptlrpc_put_bulk_iov (desc, iov); /*move down to reduce latency to send*/
+
+        if (rc != PTL_OK) {
                 CERROR("PtlMDBind failed: %d\n", rc);
-                BUG();
-                return rc;
+                LBUG();
+                RETURN(rc);
         }
 
-        remote_id.addr_kind = PTL_ADDR_NID;
-        remote_id.nid = bulk->b_peer.peer_nid;
+        remote_id.nid = desc->bd_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);
+        CDEBUG(D_NET, "Sending %u pages %u bytes to portal %d nid "LPX64" pid "
+               "%d xid %d\n", desc->bd_md.niov, desc->bd_md.length,
+               desc->bd_portal, remote_id.nid, remote_id.pid, xid);
 
-        rc = PtlPut(md_h, PTL_ACK_REQ, remote_id, portal, 0, bulk->b_xid, 0, 0);
+        rc = PtlPut(desc->bd_md_h, PTL_ACK_REQ, remote_id,
+                    desc->bd_portal, 0, xid, 0, 0);
         if (rc != PTL_OK) {
-                CERROR("PtlPut(%d, %d, %d) failed: %d\n", remote_id.nid,
-                       portal, bulk->b_xid, rc);
-                PtlMDUnlink(md_h);
-                BUG();
+                CERROR("PtlPut("LPU64", %d, %d) failed: %d\n",
+                       remote_id.nid, desc->bd_portal, xid, rc);
+                PtlMDUnlink(desc->bd_md_h);
+                LBUG();
+                RETURN(rc);
         }
 
-        return rc;
+        RETURN(0);
 }
 
-int ptlrpc_register_bulk(struct ptlrpc_bulk_desc *bulk)
+int ptlrpc_register_bulk(struct ptlrpc_bulk_desc *desc)
 {
+        struct list_head *tmp, *next;
         int rc;
-
+        __u32 xid = 0;
+        struct iovec *iov;
+        ptl_process_id_t source_id;
         ENTRY;
 
-        rc = PtlMEPrepend(bulk->b_peer.peer_ni, bulk->b_portal, local_id,
-                          bulk->b_xid, 0, PTL_UNLINK, &bulk->b_me_h);
+        if (desc->bd_page_count > PTL_MD_MAX_IOV) {
+                CERROR("iov longer than %d pages not supported (count=%d)\n",
+                       PTL_MD_MAX_IOV, desc->bd_page_count);
+                RETURN(-EINVAL);
+        }
+
+        iov = ptlrpc_get_bulk_iov (desc);
+        if (iov == NULL)
+                return (-ENOMEM);
+
+        desc->bd_md.start = iov;
+        desc->bd_md.niov = 0;
+        desc->bd_md.length = 0;
+        desc->bd_md.threshold = 1;
+        desc->bd_md.options = PTL_MD_OP_PUT | PTL_MD_IOV;
+        desc->bd_md.user_ptr = desc;
+        desc->bd_md.eventq = bulk_sink_eq;
+
+        list_for_each_safe(tmp, next, &desc->bd_page_list) {
+                struct ptlrpc_bulk_page *bulk;
+                bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
+
+                LASSERT (desc->bd_md.niov < desc->bd_page_count);
+
+                if (desc->bd_md.niov == 0)
+                        xid = bulk->bp_xid;
+                LASSERT (xid == bulk->bp_xid);   /* should all be the same */
+
+                iov[desc->bd_md.niov].iov_base = bulk->bp_buf;
+                iov[desc->bd_md.niov].iov_len = bulk->bp_buflen;
+                desc->bd_md.niov++;
+                desc->bd_md.length += bulk->bp_buflen;
+        }
+
+        LASSERT (desc->bd_md.niov == desc->bd_page_count);
+        LASSERT (desc->bd_md.niov != 0);
+
+        source_id.nid = desc->bd_connection->c_peer.peer_nid;
+        source_id.pid = PTL_PID_ANY;
+
+        rc = PtlMEAttach(desc->bd_connection->c_peer.peer_ni,
+                         desc->bd_portal, source_id, xid, 0,
+                         PTL_UNLINK, PTL_INS_AFTER, &desc->bd_me_h);
+
         if (rc != PTL_OK) {
                 CERROR("PtlMEAttach failed: %d\n", rc);
-                BUG();
-                EXIT;
-                goto cleanup1;
+                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);
+        rc = PtlMDAttach(desc->bd_me_h, desc->bd_md, PTL_UNLINK,
+                         &desc->bd_md_h);
         if (rc != PTL_OK) {
                 CERROR("PtlMDAttach failed: %d\n", rc);
-                BUG();
-                EXIT;
-                goto cleanup2;
+                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);
-        EXIT;
-        return 0;
+        ptlrpc_put_bulk_iov (desc, iov);
+
+        CDEBUG(D_NET, "Setup bulk sink buffers: %u pages %u bytes, xid %u, "
+               "portal %u\n", desc->bd_md.niov, desc->bd_md.length,
+               xid, desc->bd_portal);
+
+        RETURN(0);
+
+ cleanup:
+        ptlrpc_put_bulk_iov (desc, iov);
+        ptlrpc_abort_bulk(desc);
 
-        // XXX Confirm that this is safe!
- cleanup2:
-        PtlMDUnlink(bulk->b_md_h);
- cleanup1:
-        PtlMEUnlink(bulk->b_me_h);
         return rc;
 }
 
-int ptlrpc_reply(struct obd_device *obddev, struct ptlrpc_service *svc,
-                 struct ptlrpc_request *req)
+int ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc)
 {
-       struct ptlrpc_request *clnt_req = req->rq_reply_handle;
-       ENTRY;
-
-       if (req->rq_reply_handle == NULL) {
-               /* This is a request that came from the network via portals. */
-
-               /* FIXME: we need to increment the count of handled events */
-                req->rq_type = PTL_RPC_REPLY;
-                req->rq_reqhdr->xid = req->rq_reqhdr->xid;
-               ptl_send_buf(req, &req->rq_peer, svc->srv_rep_portal);
-       } else {
-               /* This is a local request that came from another thread. */
-
-               /* move the reply to the client */ 
-               clnt_req->rq_replen = req->rq_replen;
-               clnt_req->rq_repbuf = req->rq_repbuf;
-               req->rq_repbuf = NULL;
-               req->rq_replen = 0;
-
-               /* free the request buffer */
-               OBD_FREE(req->rq_reqbuf, req->rq_reqlen);
-               req->rq_reqbuf = NULL;
-
-               /* wake up the client */ 
-               wake_up_interruptible(&clnt_req->rq_wait_for_rep); 
-       }
-
-       EXIT;
-       return 0;
+        /* This should be safe: these handles are initialized to be
+         * invalid in ptlrpc_prep_bulk() */
+        PtlMDUnlink(desc->bd_md_h);
+        PtlMEUnlink(desc->bd_me_h);
+
+        return 0;
 }
 
-int ptlrpc_error(struct obd_device *obddev, struct ptlrpc_service *svc,
-                 struct ptlrpc_request *req)
+int ptlrpc_reply(struct ptlrpc_service *svc, struct ptlrpc_request *req)
 {
-       struct ptlrep_hdr *hdr;
-
-       ENTRY;
-
-       OBD_ALLOC(hdr, sizeof(*hdr));
-       if (!hdr) { 
-               EXIT;
-               return -ENOMEM;
-       }
+        if (req->rq_repmsg == NULL) {
+                CERROR("bad: someone called ptlrpc_reply when they meant "
+                       "ptlrpc_error\n");
+                return -EINVAL;
+        }
 
-       memset(hdr, 0, sizeof(*hdr));
+        /* FIXME: we need to increment the count of handled events */
+        if (req->rq_type != PTL_RPC_MSG_ERR)
+                req->rq_type = PTL_RPC_MSG_REPLY;
+        //req->rq_repmsg->conn = req->rq_connection->c_remote_conn;
+        //req->rq_repmsg->token = req->rq_connection->c_remote_token;
+        req->rq_repmsg->status = HTON__u32(req->rq_status);
+        return ptl_send_buf(req, req->rq_connection, svc->srv_rep_portal);
+}
 
-       hdr->xid = req->rq_reqhdr->xid;
-       hdr->status = req->rq_status; 
-       hdr->type = OST_TYPE_ERR;
+int ptlrpc_error(struct ptlrpc_service *svc, struct ptlrpc_request *req)
+{
+        int rc;
+        ENTRY;
 
-        if (req->rq_repbuf) { 
-                CERROR("req has repbuf\n");
-                BUG();
+        if (req->rq_repmsg) {
+                CERROR("req already has repmsg\n");
+                LBUG();
         }
 
-       req->rq_repbuf = (char *)hdr;
-       req->rq_replen = sizeof(*hdr); 
+        rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
+        if (rc)
+                RETURN(rc);
+
+        req->rq_type = PTL_RPC_MSG_ERR;
 
-       EXIT;
-       return ptlrpc_reply(obddev, svc, req);
+        rc = ptlrpc_reply(svc, req);
+        RETURN(rc);
 }
 
-int ptl_send_rpc(struct ptlrpc_request *request, struct lustre_peer *peer)
+int ptl_send_rpc(struct ptlrpc_request *request)
 {
-        ptl_process_id_t local_id;
-       struct ptlreq_hdr *hdr;
         int rc;
         char *repbuf;
+        ptl_process_id_t source_id;
 
         ENTRY;
 
-        hdr = (struct ptlreq_hdr *)request->rq_reqbuf;
-        if (NTOH__u32(hdr->type) != OST_TYPE_REQ) {
-                CERROR("lustre_ost: wrong packet type sent %d\n",
-                       NTOH__u32(hdr->type));
-                BUG();
-        }
-        if (request->rq_replen == 0) {
-                CERROR("request->rq_replen is 0!\n");
-                EXIT;
-                return -EINVAL;
+        if (request->rq_type != PTL_RPC_MSG_REQUEST) {
+                CERROR("wrong packet type sent %d\n",
+                       NTOH__u32(request->rq_reqmsg->type));
+                LBUG();
+                RETURN(EINVAL);
         }
 
-        /* request->rq_repbuf is set only when the reply comes in, in
-         * client_packet_callback() */
-        OBD_ALLOC(repbuf, request->rq_replen);
-        if (!repbuf) { 
-                EXIT;
-                return -ENOMEM;
-        }
+        source_id.nid = request->rq_connection->c_peer.peer_nid;
+        source_id.pid = PTL_PID_ANY;
 
-        local_id.addr_kind = PTL_ADDR_GID;
-        local_id.gid = PTL_ID_ANY;
-        local_id.rid = PTL_ID_ANY;
+        if (request->rq_replen != 0) {
 
-        //CERROR("sending req %d\n", request->rq_xid);
-        rc = PtlMEPrepend(peer->peer_ni, request->rq_reply_portal, local_id,
-                          request->rq_xid, 0, PTL_UNLINK,
-                          &request->rq_reply_me_h);
-        if (rc != PTL_OK) {
-                CERROR("PtlMEAttach failed: %d\n", rc);
-                BUG();
-                EXIT;
-                goto cleanup;
-        }
+                /* request->rq_repmsg is set only when the reply comes in, in
+                 * client_packet_callback() */
+                if (request->rq_reply_md.start)
+                        OBD_FREE(request->rq_reply_md.start,
+                                 request->rq_replen);
 
-        request->rq_type = PTL_RPC_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;
-
-        rc = PtlMDAttach(request->rq_reply_me_h, request->rq_reply_md,
-                         PTL_UNLINK, &request->rq_reply_md_h);
-        //CERROR("MDAttach (send RPC): %Lu\n", (__u64)request->rq_reply_md_h);
-        if (rc != PTL_OK) {
-                CERROR("PtlMDAttach failed: %d\n", rc);
-                BUG();
-                EXIT;
-                goto cleanup2;
-        }
+                OBD_ALLOC(repbuf, request->rq_replen);
+                if (!repbuf) {
+                        LBUG();
+                        RETURN(ENOMEM);
+                }
 
-        CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid %u, portal %u\n",
-               request->rq_replen, request->rq_xid, request->rq_reply_portal);
+                rc = PtlMEAttach(request->rq_connection->c_peer.peer_ni,
+                               request->rq_import->imp_client->cli_reply_portal,
+                                 source_id, request->rq_xid, 0, PTL_UNLINK,
+                                 PTL_INS_AFTER, &request->rq_reply_me_h);
+                if (rc != PTL_OK) {
+                        CERROR("PtlMEAttach failed: %d\n", rc);
+                        LBUG();
+                        GOTO(cleanup, rc);
+                }
 
-        return ptl_send_buf(request, peer, request->rq_req_portal);
+                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 = reply_in_eq;
+
+                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);
+                        LBUG();
+                        GOTO(cleanup2, rc);
+                }
+
+                CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid "LPU64
+                       ", portal %u\n",
+                       request->rq_replen, request->rq_xid,
+                       request->rq_import->imp_client->cli_reply_portal);
+        }
+
+        rc = ptl_send_buf(request, request->rq_connection,
+                          request->rq_import->imp_client->cli_request_portal);
+        RETURN(rc);
 
  cleanup2:
         PtlMEUnlink(request->rq_reply_me_h);
  cleanup:
         OBD_FREE(repbuf, request->rq_replen);
+        // up(&request->rq_client->cli_rpc_sem);
 
         return rc;
 }
 
-/* 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_handled_rpc(struct ptlrpc_service *service, void *start) 
+void ptlrpc_link_svc_me(struct ptlrpc_request_buffer_desc *rqbd)
 {
-        int rc, index = 0;
+        struct ptlrpc_service *service = rqbd->rqbd_service;
+        static ptl_process_id_t match_id = {PTL_NID_ANY, PTL_PID_ANY};
+        int rc;
+        ptl_md_t dummy;
+        ptl_handle_md_t md_h;
 
-        spin_lock(&service->srv_lock);
+        LASSERT (atomic_read (&rqbd->rqbd_refcount) == 0);
 
-        while (index < service->srv_ring_length) {
-                if ( service->srv_md[index].start == start) 
-                        break;
-                index++;
+        /* Attach the leading ME on which we build the ring */
+        rc = PtlMEAttach(service->srv_self.peer_ni, service->srv_req_portal,
+                         match_id, 0, ~0,
+                         PTL_UNLINK, PTL_INS_AFTER, &rqbd->rqbd_me_h);
+        if (rc != PTL_OK) {
+                CERROR("PtlMEAttach failed: %d\n", rc);
+                LBUG();
         }
-        if (index == service->srv_ring_length)
-                BUG();
-
-        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)
-                BUG();
-        
-        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]));
-                if (rc != PTL_OK) {
-                        CERROR("PtlMEInsert failed: %d\n", rc);
-                        BUG();
-                        spin_unlock(&service->srv_lock);
-                        return rc;
-                }
-                CDEBUG(D_NET, "Inserting new ME and MD in ring, rc %d\n", rc);
-
-                service->srv_me_tail = index;
 
-                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;
+        dummy.start      = rqbd->rqbd_buffer;
+        dummy.length     = service->srv_buf_size;
+        dummy.max_size   = service->srv_max_req_size;
+        dummy.threshold  = PTL_MD_THRESH_INF;
+        dummy.options    = PTL_MD_OP_PUT | PTL_MD_MAX_SIZE | PTL_MD_AUTO_UNLINK;
+        dummy.user_ptr   = rqbd;
+        dummy.eventq     = service->srv_eq_h;
 
-                rc = PtlMDAttach(service->srv_me_h[index],
-                                 service->srv_md[index],
-                                 PTL_RETAIN, &(service->srv_md_h[index]));
-                //CERROR("MDAttach (request MDs): %Lu\n",
-                //(__u64)(service->srv_md_h[index]));
+        atomic_inc (&service->srv_nrqbds_receiving);
+        atomic_set (&rqbd->rqbd_refcount, 1);   /* 1 ref for portals */
 
-                CDEBUG(D_INFO, "Attach MD in ring, rc %d\n", rc);
-                if (rc != PTL_OK) {
-                        /* XXX cleanup */
-                        CERROR("PtlMDAttach failed: %d\n", rc);
-                        BUG();
-                        spin_unlock(&service->srv_lock);
-                        return rc;
-                }
-        } 
-        
-        spin_unlock(&service->srv_lock);
-        return 0;
+        rc = PtlMDAttach(rqbd->rqbd_me_h, dummy, PTL_UNLINK, &md_h);
+        if (rc != PTL_OK) {
+                CERROR("PtlMDAttach failed: %d\n", rc);
+                LBUG();
+#warning proper cleanup required
+                PtlMEUnlink (rqbd->rqbd_me_h);
+                atomic_set (&rqbd->rqbd_refcount, 0);
+                atomic_dec (&service->srv_nrqbds_receiving);
+        }
 }