Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / ptlrpc / niobuf.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  *
24  */
25
26 #define DEBUG_SUBSYSTEM S_RPC
27 #ifndef __KERNEL__
28 #include <liblustre.h>
29 #endif
30 #include <obd_support.h>
31 #include <lustre_net.h>
32 #include <lustre_lib.h>
33 #include <obd.h>
34 #include "ptlrpc_internal.h"
35
36 static int ptl_send_buf (lnet_handle_md_t *mdh, void *base, int len,
37                          lnet_ack_req_t ack, struct ptlrpc_cb_id *cbid,
38                          struct ptlrpc_connection *conn, int portal, __u64 xid)
39 {
40         int              rc;
41         lnet_md_t         md;
42         ENTRY;
43
44         LASSERT (portal != 0);
45         LASSERT (conn != NULL);
46         CDEBUG (D_INFO, "conn=%p id %s\n", conn, libcfs_id2str(conn->c_peer));
47         md.start     = base;
48         md.length    = len;
49         md.threshold = (ack == LNET_ACK_REQ) ? 2 : 1;
50         md.options   = PTLRPC_MD_OPTIONS;
51         md.user_ptr  = cbid;
52         md.eq_handle = ptlrpc_eq_h;
53
54         if (unlikely(ack == LNET_ACK_REQ &&
55                      OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_ACK, OBD_FAIL_ONCE))){
56                 /* don't ask for the ack to simulate failing client */
57                 ack = LNET_NOACK_REQ;
58         }
59
60         rc = LNetMDBind (md, LNET_UNLINK, mdh);
61         if (unlikely(rc != 0)) {
62                 CERROR ("LNetMDBind failed: %d\n", rc);
63                 LASSERT (rc == -ENOMEM);
64                 RETURN (-ENOMEM);
65         }
66
67         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid "LPD64"\n",
68                len, portal, xid);
69
70         rc = LNetPut (conn->c_self, *mdh, ack,
71                       conn->c_peer, portal, xid, 0, 0);
72         if (unlikely(rc != 0)) {
73                 int rc2;
74                 /* We're going to get an UNLINK event when I unlink below,
75                  * which will complete just like any other failed send, so
76                  * I fall through and return success here! */
77                 CERROR("LNetPut(%s, %d, "LPD64") failed: %d\n",
78                        libcfs_id2str(conn->c_peer), portal, xid, rc);
79                 rc2 = LNetMDUnlink(*mdh);
80                 LASSERTF(rc2 == 0, "rc2 = %d\n", rc2);
81         }
82
83         RETURN (0);
84 }
85
86 int ptlrpc_start_bulk_transfer (struct ptlrpc_bulk_desc *desc)
87 {
88         struct ptlrpc_connection *conn = desc->bd_export->exp_connection;
89         int                       rc;
90         int                       rc2;
91         lnet_md_t                 md;
92         __u64                     xid;
93         ENTRY;
94
95         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_PUT_NET))
96                 RETURN(0);
97
98         /* NB no locking required until desc is on the network */
99         LASSERT (!desc->bd_network_rw);
100         LASSERT (desc->bd_type == BULK_PUT_SOURCE ||
101                  desc->bd_type == BULK_GET_SINK);
102         desc->bd_success = 0;
103
104         md.user_ptr = &desc->bd_cbid;
105         md.eq_handle = ptlrpc_eq_h;
106         md.threshold = 2; /* SENT and ACK/REPLY */
107         md.options = PTLRPC_MD_OPTIONS;
108         ptlrpc_fill_bulk_md(&md, desc);
109
110         LASSERT (desc->bd_cbid.cbid_fn == server_bulk_callback);
111         LASSERT (desc->bd_cbid.cbid_arg == desc);
112
113         /* NB total length may be 0 for a read past EOF, so we send a 0
114          * length bulk, since the client expects a bulk event. */
115
116         rc = LNetMDBind(md, LNET_UNLINK, &desc->bd_md_h);
117         if (rc != 0) {
118                 CERROR("LNetMDBind failed: %d\n", rc);
119                 LASSERT (rc == -ENOMEM);
120                 RETURN(-ENOMEM);
121         }
122
123         /* Client's bulk and reply matchbits are the same */
124         xid = desc->bd_req->rq_xid;
125         CDEBUG(D_NET, "Transferring %u pages %u bytes via portal %d "
126                "id %s xid "LPX64"\n", desc->bd_iov_count,
127                desc->bd_nob, desc->bd_portal,
128                libcfs_id2str(conn->c_peer), xid);
129
130         /* Network is about to get at the memory */
131         desc->bd_network_rw = 1;
132
133         if (desc->bd_type == BULK_PUT_SOURCE)
134                 rc = LNetPut (conn->c_self, desc->bd_md_h, LNET_ACK_REQ,
135                               conn->c_peer, desc->bd_portal, xid, 0, 0);
136         else
137                 rc = LNetGet (conn->c_self, desc->bd_md_h,
138                               conn->c_peer, desc->bd_portal, xid, 0);
139
140         if (rc != 0) {
141                 /* Can't send, so we unlink the MD bound above.  The UNLINK
142                  * event this creates will signal completion with failure,
143                  * so we return SUCCESS here! */
144                 CERROR("Transfer(%s, %d, "LPX64") failed: %d\n",
145                        libcfs_id2str(conn->c_peer), desc->bd_portal, xid, rc);
146                 rc2 = LNetMDUnlink(desc->bd_md_h);
147                 LASSERT (rc2 == 0);
148         }
149
150         RETURN(0);
151 }
152
153 void ptlrpc_abort_bulk (struct ptlrpc_bulk_desc *desc)
154 {
155         /* Server side bulk abort. Idempotent. Not thread-safe (i.e. only
156          * serialises with completion callback) */
157         struct l_wait_info lwi;
158         int                rc;
159
160         LASSERT (!in_interrupt ());             /* might sleep */
161
162         if (!ptlrpc_bulk_active(desc))          /* completed or */
163                 return;                         /* never started */
164
165         /* Do not send any meaningful data over the wire for evicted clients */
166         if (desc->bd_export && desc->bd_export->exp_failed)
167                 ptl_rpc_wipe_bulk_pages(desc);
168
169         /* The unlink ensures the callback happens ASAP and is the last
170          * one.  If it fails, it must be because completion just happened,
171          * but we must still l_wait_event() in this case, to give liblustre
172          * a chance to run server_bulk_callback()*/
173
174         LNetMDUnlink (desc->bd_md_h);
175
176         for (;;) {
177                 /* Network access will complete in finite time but the HUGE
178                  * timeout lets us CWARN for visibility of sluggish NALs */
179                 lwi = LWI_TIMEOUT (cfs_time_seconds(300), NULL, NULL);
180                 rc = l_wait_event(desc->bd_waitq,
181                                   !ptlrpc_bulk_active(desc), &lwi);
182                 if (rc == 0)
183                         return;
184
185                 LASSERT(rc == -ETIMEDOUT);
186                 CWARN("Unexpectedly long timeout: desc %p\n", desc);
187         }
188 }
189
190 int ptlrpc_register_bulk (struct ptlrpc_request *req)
191 {
192         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
193         lnet_process_id_t peer;
194         int rc;
195         int rc2;
196         lnet_handle_me_t  me_h;
197         lnet_md_t         md;
198         ENTRY;
199
200         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_GET_NET))
201                 RETURN(0);
202
203         /* NB no locking required until desc is on the network */
204         LASSERT (desc->bd_nob > 0);
205         LASSERT (!desc->bd_network_rw);
206         LASSERT (desc->bd_iov_count <= PTLRPC_MAX_BRW_PAGES);
207         LASSERT (desc->bd_req != NULL);
208         LASSERT (desc->bd_type == BULK_PUT_SINK ||
209                  desc->bd_type == BULK_GET_SOURCE);
210
211         desc->bd_success = 0;
212
213         peer = desc->bd_import->imp_connection->c_peer;
214
215         md.user_ptr = &desc->bd_cbid;
216         md.eq_handle = ptlrpc_eq_h;
217         md.threshold = 1;                       /* PUT or GET */
218         md.options = PTLRPC_MD_OPTIONS |
219                      ((desc->bd_type == BULK_GET_SOURCE) ?
220                       LNET_MD_OP_GET : LNET_MD_OP_PUT);
221         ptlrpc_fill_bulk_md(&md, desc);
222
223         LASSERT (desc->bd_cbid.cbid_fn == client_bulk_callback);
224         LASSERT (desc->bd_cbid.cbid_arg == desc);
225
226         /* XXX Registering the same xid on retried bulk makes my head
227          * explode trying to understand how the original request's bulk
228          * might interfere with the retried request -eeb */
229         LASSERTF (!desc->bd_registered || req->rq_xid != desc->bd_last_xid,
230                   "registered: %d  rq_xid: "LPU64" bd_last_xid: "LPU64"\n",
231                   desc->bd_registered, req->rq_xid, desc->bd_last_xid);
232         desc->bd_registered = 1;
233         desc->bd_last_xid = req->rq_xid;
234
235         rc = LNetMEAttach(desc->bd_portal, peer,
236                          req->rq_xid, 0, LNET_UNLINK, LNET_INS_AFTER, &me_h);
237         if (rc != 0) {
238                 CERROR("LNetMEAttach failed: %d\n", rc);
239                 LASSERT (rc == -ENOMEM);
240                 RETURN (-ENOMEM);
241         }
242
243         /* About to let the network at it... */
244         desc->bd_network_rw = 1;
245         rc = LNetMDAttach(me_h, md, LNET_UNLINK, &desc->bd_md_h);
246         if (rc != 0) {
247                 CERROR("LNetMDAttach failed: %d\n", rc);
248                 LASSERT (rc == -ENOMEM);
249                 desc->bd_network_rw = 0;
250                 rc2 = LNetMEUnlink (me_h);
251                 LASSERT (rc2 == 0);
252                 RETURN (-ENOMEM);
253         }
254
255         CDEBUG(D_NET, "Setup bulk %s buffers: %u pages %u bytes, xid "LPX64", "
256                "portal %u\n",
257                desc->bd_type == BULK_GET_SOURCE ? "get-source" : "put-sink",
258                desc->bd_iov_count, desc->bd_nob,
259                req->rq_xid, desc->bd_portal);
260         RETURN(0);
261 }
262
263 void ptlrpc_unregister_bulk (struct ptlrpc_request *req)
264 {
265         /* Disconnect a bulk desc from the network. Idempotent. Not
266          * thread-safe (i.e. only interlocks with completion callback). */
267         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
268         cfs_waitq_t             *wq;
269         struct l_wait_info       lwi;
270         int                      rc;
271
272         LASSERT (!in_interrupt ());     /* might sleep */
273
274         if (!ptlrpc_bulk_active(desc))  /* completed or */
275                 return;                 /* never registered */
276
277         LASSERT (desc->bd_req == req);  /* bd_req NULL until registered */
278
279         /* the unlink ensures the callback happens ASAP and is the last
280          * one.  If it fails, it must be because completion just happened,
281          * but we must still l_wait_event() in this case to give liblustre
282          * a chance to run client_bulk_callback() */
283
284         LNetMDUnlink (desc->bd_md_h);
285
286         if (req->rq_set != NULL)
287                 wq = &req->rq_set->set_waitq;
288         else
289                 wq = &req->rq_reply_waitq;
290
291         for (;;) {
292                 /* Network access will complete in finite time but the HUGE
293                  * timeout lets us CWARN for visibility of sluggish NALs */
294                 lwi = LWI_TIMEOUT (cfs_time_seconds(300), NULL, NULL);
295                 rc = l_wait_event(*wq, !ptlrpc_bulk_active(desc), &lwi);
296                 if (rc == 0)
297                         return;
298
299                 LASSERT (rc == -ETIMEDOUT);
300                 DEBUG_REQ(D_WARNING,req,"Unexpectedly long timeout: desc %p",
301                           desc);
302         }
303 }
304
305 int ptlrpc_send_reply (struct ptlrpc_request *req, int may_be_difficult)
306 {
307         struct ptlrpc_service     *svc = req->rq_rqbd->rqbd_service;
308         struct ptlrpc_reply_state *rs = req->rq_reply_state;
309         struct ptlrpc_connection  *conn;
310         int                        rc;
311
312         /* We must already have a reply buffer (only ptlrpc_error() may be
313          * called without one). The reply generated by sptlrpc layer (e.g.
314          * error notify, etc.) might have NULL rq->reqmsg; Otherwise we must
315          * have a request buffer which is either the actual (swabbed) incoming
316          * request, or a saved copy if this is a req saved in
317          * target_queue_final_reply().
318          */
319         LASSERT (req->rq_no_reply == 0);
320         LASSERT (req->rq_reqbuf != NULL);
321         LASSERT (rs != NULL);
322         LASSERT (may_be_difficult || !rs->rs_difficult);
323         LASSERT (req->rq_repmsg != NULL);
324         LASSERT (req->rq_repmsg == rs->rs_msg);
325         LASSERT (rs->rs_cb_id.cbid_fn == reply_out_callback);
326         LASSERT (rs->rs_cb_id.cbid_arg == rs);
327
328         if (unlikely(req->rq_export && req->rq_export->exp_obd &&
329                      req->rq_export->exp_obd->obd_fail)) {
330                 /* Failed obd's only send ENODEV */
331                 req->rq_type = PTL_RPC_MSG_ERR;
332                 req->rq_status = -ENODEV;
333                 CDEBUG(D_HA, "sending ENODEV from failed obd %d\n",
334                        req->rq_export->exp_obd->obd_minor);
335         }
336
337         if (req->rq_type != PTL_RPC_MSG_ERR)
338                 req->rq_type = PTL_RPC_MSG_REPLY;
339
340         lustre_msg_set_type(req->rq_repmsg, req->rq_type);
341         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
342         lustre_msg_set_opc(req->rq_repmsg,
343                 req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : 0);
344
345         target_pack_pool_reply(req);
346
347         if (req->rq_export == NULL || req->rq_export->exp_connection == NULL)
348                 conn = ptlrpc_get_connection(req->rq_peer, req->rq_self, NULL);
349         else
350                 conn = ptlrpc_connection_addref(req->rq_export->exp_connection);
351
352         if (unlikely(conn == NULL)) {
353                 CERROR("not replying on NULL connection\n"); /* bug 9635 */
354                 return -ENOTCONN;
355         }
356         atomic_inc (&svc->srv_outstanding_replies);
357         ptlrpc_rs_addref(rs);                   /* +1 ref for the network */
358
359         rc = sptlrpc_svc_wrap_reply(req);
360         if (unlikely(rc))
361                 goto out;
362
363         rc = ptl_send_buf (&rs->rs_md_h, rs->rs_repbuf, rs->rs_repdata_len,
364                            rs->rs_difficult ? LNET_ACK_REQ : LNET_NOACK_REQ,
365                            &rs->rs_cb_id, conn,
366                            svc->srv_rep_portal, req->rq_xid);
367 out:
368         if (unlikely(rc != 0)) {
369                 atomic_dec (&svc->srv_outstanding_replies);
370                 ptlrpc_rs_decref(rs);
371         }
372         ptlrpc_put_connection(conn);
373         return rc;
374 }
375
376 int ptlrpc_reply (struct ptlrpc_request *req)
377 {
378         if (req->rq_no_reply)
379                 return 0;
380         else
381                 return (ptlrpc_send_reply(req, 0));
382 }
383
384 int ptlrpc_send_error(struct ptlrpc_request *req, int may_be_difficult)
385 {
386         int rc;
387         ENTRY;
388
389         if (req->rq_no_reply)
390                 RETURN(0);
391
392         if (!req->rq_repmsg) {
393                 rc = lustre_pack_reply(req, 1, NULL, NULL);
394                 if (rc)
395                         RETURN(rc);
396         }
397
398         req->rq_type = PTL_RPC_MSG_ERR;
399
400         rc = ptlrpc_send_reply(req, may_be_difficult);
401         RETURN(rc);
402 }
403
404 int ptlrpc_error(struct ptlrpc_request *req)
405 {
406         return ptlrpc_send_error(req, 0);
407 }
408
409 int ptl_send_rpc(struct ptlrpc_request *request, int noreply)
410 {
411         int rc;
412         int rc2;
413         struct ptlrpc_connection *connection;
414         lnet_handle_me_t  reply_me_h;
415         lnet_md_t         reply_md;
416         struct obd_device *obd = request->rq_import->imp_obd;
417         ENTRY;
418
419         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_RPC))
420                 RETURN(0);
421
422         LASSERT (request->rq_type == PTL_RPC_MSG_REQUEST);
423
424         /* If this is a re-transmit, we're required to have disengaged
425          * cleanly from the previous attempt */
426         LASSERT (!request->rq_receiving_reply);
427
428         if (request->rq_import->imp_obd &&
429             request->rq_import->imp_obd->obd_fail) {
430                 CDEBUG(D_HA, "muting rpc for failed imp obd %s\n",
431                        request->rq_import->imp_obd->obd_name);
432                 /* this prevents us from waiting in ptlrpc_queue_wait */
433                 request->rq_err = 1;
434                 RETURN(-ENODEV);
435         }
436
437         connection = request->rq_import->imp_connection;
438
439         lustre_msg_set_handle(request->rq_reqmsg,
440                               &request->rq_import->imp_remote_handle);
441         lustre_msg_set_type(request->rq_reqmsg, PTL_RPC_MSG_REQUEST);
442         lustre_msg_set_conn_cnt(request->rq_reqmsg,
443                                 request->rq_import->imp_conn_cnt);
444
445         rc = sptlrpc_cli_wrap_request(request);
446         if (rc)
447                 RETURN(rc);
448
449         /* bulk register should be done after wrap_request() */
450         if (request->rq_bulk != NULL) {
451                 rc = ptlrpc_register_bulk (request);
452                 if (rc != 0)
453                         RETURN(rc);
454         }
455
456         if (!noreply) {
457                 LASSERT (request->rq_replen != 0);
458                 if (request->rq_repbuf == NULL) {
459                         rc = sptlrpc_cli_alloc_repbuf(request,
460                                                       request->rq_replen);
461                         if (rc)
462                                 GOTO(cleanup_bulk, rc);
463                 }
464
465                 rc = LNetMEAttach(request->rq_reply_portal,/*XXX FIXME bug 249*/
466                                   connection->c_peer, request->rq_xid, 0,
467                                   LNET_UNLINK, LNET_INS_AFTER, &reply_me_h);
468                 if (rc != 0) {
469                         CERROR("LNetMEAttach failed: %d\n", rc);
470                         LASSERT (rc == -ENOMEM);
471                         GOTO(cleanup_bulk, rc = -ENOMEM);
472                 }
473         }
474
475         spin_lock(&request->rq_lock);
476         /* If the MD attach succeeds, there _will_ be a reply_in callback */
477         request->rq_receiving_reply = !noreply;
478         /* Clear any flags that may be present from previous sends. */
479         request->rq_replied = 0;
480         request->rq_err = 0;
481         request->rq_timedout = 0;
482         request->rq_net_err = 0;
483         request->rq_resend = 0;
484         request->rq_restart = 0;
485         spin_unlock(&request->rq_lock);
486
487         if (!noreply) {
488                 reply_md.start     = request->rq_repbuf;
489                 reply_md.length    = request->rq_repbuf_len;
490                 reply_md.threshold = 1;
491                 reply_md.options   = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT;
492                 reply_md.user_ptr  = &request->rq_reply_cbid;
493                 reply_md.eq_handle = ptlrpc_eq_h;
494
495                 rc = LNetMDAttach(reply_me_h, reply_md, LNET_UNLINK,
496                                  &request->rq_reply_md_h);
497                 if (rc != 0) {
498                         CERROR("LNetMDAttach failed: %d\n", rc);
499                         LASSERT (rc == -ENOMEM);
500                         spin_lock(&request->rq_lock);
501                         /* ...but the MD attach didn't succeed... */
502                         request->rq_receiving_reply = 0;
503                         spin_unlock(&request->rq_lock);
504                         GOTO(cleanup_me, rc -ENOMEM);
505                 }
506
507                 CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid "LPU64
508                        ", portal %u\n",
509                        request->rq_repbuf_len, request->rq_xid,
510                        request->rq_reply_portal);
511         }
512
513         /* add references on request for request_out_callback */
514         ptlrpc_request_addref(request);
515         if (obd->obd_svc_stats != NULL)
516                 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQACTIVE_CNTR,
517                                     request->rq_import->imp_inflight.counter);
518
519         OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_SEND, request->rq_timeout + 5);
520
521         request->rq_sent = cfs_time_current_sec();
522         do_gettimeofday(&request->rq_arrival_time);
523         ptlrpc_pinger_sending_on_import(request->rq_import);
524         rc = ptl_send_buf(&request->rq_req_md_h,
525                           request->rq_reqbuf, request->rq_reqdata_len,
526                           LNET_NOACK_REQ, &request->rq_req_cbid,
527                           connection,
528                           request->rq_request_portal,
529                           request->rq_xid);
530         if (rc == 0) {
531                 ptlrpc_lprocfs_rpc_sent(request);
532                 RETURN(rc);
533         }
534
535         ptlrpc_req_finished(request);
536         if (noreply)
537                 RETURN(rc);
538         else
539                 GOTO(cleanup_me, rc);
540  cleanup_me:
541         /* MEUnlink is safe; the PUT didn't even get off the ground, and
542          * nobody apart from the PUT's target has the right nid+XID to
543          * access the reply buffer. */
544         rc2 = LNetMEUnlink(reply_me_h);
545         LASSERT (rc2 == 0);
546         /* UNLINKED callback called synchronously */
547         LASSERT (!request->rq_receiving_reply);
548
549  cleanup_bulk:
550         if (request->rq_bulk != NULL)
551                 ptlrpc_unregister_bulk(request);
552
553         return rc;
554 }
555
556 int ptlrpc_register_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
557 {
558         struct ptlrpc_service   *service = rqbd->rqbd_service;
559         static lnet_process_id_t  match_id = {LNET_NID_ANY, LNET_PID_ANY};
560         int                      rc;
561         lnet_md_t                 md;
562         lnet_handle_me_t          me_h;
563
564         CDEBUG(D_NET, "LNetMEAttach: portal %d\n",
565                service->srv_req_portal);
566
567         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_RQBD))
568                 return (-ENOMEM);
569
570         rc = LNetMEAttach(service->srv_req_portal,
571                           match_id, 0, ~0, LNET_UNLINK, LNET_INS_AFTER, &me_h);
572         if (rc != 0) {
573                 CERROR("LNetMEAttach failed: %d\n", rc);
574                 return (-ENOMEM);
575         }
576
577         LASSERT(rqbd->rqbd_refcount == 0);
578         rqbd->rqbd_refcount = 1;
579
580         md.start     = rqbd->rqbd_buffer;
581         md.length    = service->srv_buf_size;
582         md.max_size  = service->srv_max_req_size;
583         md.threshold = LNET_MD_THRESH_INF;
584         md.options   = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT | LNET_MD_MAX_SIZE;
585         md.user_ptr  = &rqbd->rqbd_cbid;
586         md.eq_handle = ptlrpc_eq_h;
587
588         rc = LNetMDAttach(me_h, md, LNET_UNLINK, &rqbd->rqbd_md_h);
589         if (rc == 0)
590                 return (0);
591
592         CERROR("LNetMDAttach failed: %d; \n", rc);
593         LASSERT (rc == -ENOMEM);
594         rc = LNetMEUnlink (me_h);
595         LASSERT (rc == 0);
596         rqbd->rqbd_refcount = 0;
597
598         return (-ENOMEM);
599 }