Whamcloud - gitweb
branch: b1_6
[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                          unsigned int offset)
40 {
41         int              rc;
42         lnet_md_t         md;
43         ENTRY;
44
45         LASSERT (portal != 0);
46         LASSERT (conn != NULL);
47         CDEBUG (D_INFO, "conn=%p id %s\n", conn, libcfs_id2str(conn->c_peer));
48         md.start     = base;
49         md.length    = len;
50         md.threshold = (ack == LNET_ACK_REQ) ? 2 : 1;
51         md.options   = PTLRPC_MD_OPTIONS;
52         md.user_ptr  = cbid;
53         md.eq_handle = ptlrpc_eq_h;
54
55         if (ack == LNET_ACK_REQ &&
56             OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_ACK | OBD_FAIL_ONCE)) {
57                 /* don't ask for the ack to simulate failing client */
58                 ack = LNET_NOACK_REQ;
59                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
60         }
61
62         rc = LNetMDBind (md, LNET_UNLINK, mdh);
63         if (rc != 0) {
64                 CERROR ("LNetMDBind failed: %d\n", rc);
65                 LASSERT (rc == -ENOMEM);
66                 RETURN (-ENOMEM);
67         }
68
69         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid "LPD64", offset %u\n",
70                len, portal, xid, offset);
71
72         rc = LNetPut (conn->c_self, *mdh, ack, 
73                       conn->c_peer, portal, xid, offset, 0);
74         if (rc != 0) {
75                 int rc2;
76                 /* We're going to get an UNLINK event when I unlink below,
77                  * which will complete just like any other failed send, so
78                  * I fall through and return success here! */
79                 CERROR("LNetPut(%s, %d, "LPD64") failed: %d\n",
80                        libcfs_id2str(conn->c_peer), portal, xid, rc);
81                 rc2 = LNetMDUnlink(*mdh);
82                 LASSERTF(rc2 == 0, "rc2 = %d\n", rc2);
83         }
84
85         RETURN (0);
86 }
87
88 int ptlrpc_start_bulk_transfer (struct ptlrpc_bulk_desc *desc)
89 {
90         struct ptlrpc_connection *conn = desc->bd_export->exp_connection;
91         int                       rc;
92         int                       rc2;
93         lnet_md_t                 md;
94         __u64                     xid;
95         ENTRY;
96
97         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_PTLRPC_BULK_PUT_NET)) 
98                 RETURN(0);
99
100         /* NB no locking required until desc is on the network */
101         LASSERT (!desc->bd_network_rw);
102         LASSERT (desc->bd_type == BULK_PUT_SOURCE ||
103                  desc->bd_type == BULK_GET_SINK);
104         desc->bd_success = 0;
105         desc->bd_sender = LNET_NID_ANY;
106
107         md.user_ptr = &desc->bd_cbid;
108         md.eq_handle = ptlrpc_eq_h;
109         md.threshold = 2; /* SENT and ACK/REPLY */
110         md.options = PTLRPC_MD_OPTIONS;
111         ptlrpc_fill_bulk_md(&md, desc);
112
113         LASSERT (desc->bd_cbid.cbid_fn == server_bulk_callback);
114         LASSERT (desc->bd_cbid.cbid_arg == desc);
115
116         /* NB total length may be 0 for a read past EOF, so we send a 0
117          * length bulk, since the client expects a bulk event. */
118
119         rc = LNetMDBind(md, LNET_UNLINK, &desc->bd_md_h);
120         if (rc != 0) {
121                 CERROR("LNetMDBind failed: %d\n", rc);
122                 LASSERT (rc == -ENOMEM);
123                 RETURN(-ENOMEM);
124         }
125
126         /* Client's bulk and reply matchbits are the same */
127         xid = desc->bd_req->rq_xid;
128         CDEBUG(D_NET, "Transferring %u pages %u bytes via portal %d "
129                "id %s xid "LPX64"\n", desc->bd_iov_count,
130                desc->bd_nob, desc->bd_portal, 
131                libcfs_id2str(conn->c_peer), xid);
132
133         /* Network is about to get at the memory */
134         desc->bd_network_rw = 1;
135
136         if (desc->bd_type == BULK_PUT_SOURCE)
137                 rc = LNetPut (conn->c_self, desc->bd_md_h, LNET_ACK_REQ, 
138                               conn->c_peer, desc->bd_portal, xid, 0, 0);
139         else
140                 rc = LNetGet (conn->c_self, desc->bd_md_h, 
141                               conn->c_peer, desc->bd_portal, xid, 0);
142
143         if (rc != 0) {
144                 /* Can't send, so we unlink the MD bound above.  The UNLINK
145                  * event this creates will signal completion with failure,
146                  * so we return SUCCESS here! */
147                 CERROR("Transfer(%s, %d, "LPX64") failed: %d\n",
148                        libcfs_id2str(conn->c_peer), desc->bd_portal, xid, rc);
149                 rc2 = LNetMDUnlink(desc->bd_md_h);
150                 LASSERT (rc2 == 0);
151         }
152
153         RETURN(0);
154 }
155
156 void ptlrpc_abort_bulk (struct ptlrpc_bulk_desc *desc)
157 {
158         /* Server side bulk abort. Idempotent. Not thread-safe (i.e. only
159          * serialises with completion callback) */
160         struct l_wait_info lwi;
161         int                rc;
162
163         LASSERT (!in_interrupt ());             /* might sleep */
164
165         if (!ptlrpc_bulk_active(desc))          /* completed or */
166                 return;                         /* never started */
167         
168         /* Do not send any meaningful data over the wire for evicted clients */
169         if (desc->bd_export && desc->bd_export->exp_failed)
170                 ptl_rpc_wipe_bulk_pages(desc);
171
172         /* The unlink ensures the callback happens ASAP and is the last
173          * one.  If it fails, it must be because completion just happened,
174          * but we must still l_wait_event() in this case, to give liblustre
175          * a chance to run server_bulk_callback()*/
176
177         LNetMDUnlink (desc->bd_md_h);
178
179         for (;;) {
180                 /* Network access will complete in finite time but the HUGE
181                  * timeout lets us CWARN for visibility of sluggish NALs */
182                 lwi = LWI_TIMEOUT (cfs_time_seconds(300), NULL, NULL);
183                 rc = l_wait_event(desc->bd_waitq, 
184                                   !ptlrpc_bulk_active(desc), &lwi);
185                 if (rc == 0)
186                         return;
187
188                 LASSERT(rc == -ETIMEDOUT);
189                 CWARN("Unexpectedly long timeout: desc %p\n", desc);
190         }
191 }
192
193 int ptlrpc_register_bulk (struct ptlrpc_request *req)
194 {
195         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
196         lnet_process_id_t peer;
197         int rc;
198         int rc2;
199         lnet_handle_me_t  me_h;
200         lnet_md_t         md;
201         ENTRY;
202
203         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_PTLRPC_BULK_GET_NET)) 
204                 RETURN(0);
205
206         /* NB no locking required until desc is on the network */
207         LASSERT (desc->bd_nob > 0);
208         LASSERT (!desc->bd_network_rw);
209         LASSERT (desc->bd_iov_count <= PTLRPC_MAX_BRW_PAGES);
210         LASSERT (desc->bd_req != NULL);
211         LASSERT (desc->bd_type == BULK_PUT_SINK ||
212                  desc->bd_type == BULK_GET_SOURCE);
213
214         desc->bd_success = 0;
215         desc->bd_sender = LNET_NID_ANY;
216
217         peer = desc->bd_import->imp_connection->c_peer;
218
219         md.user_ptr = &desc->bd_cbid;
220         md.eq_handle = ptlrpc_eq_h;
221         md.threshold = 1;                       /* PUT or GET */
222         md.options = PTLRPC_MD_OPTIONS | 
223                      ((desc->bd_type == BULK_GET_SOURCE) ? 
224                       LNET_MD_OP_GET : LNET_MD_OP_PUT);
225         ptlrpc_fill_bulk_md(&md, desc);
226
227         LASSERT (desc->bd_cbid.cbid_fn == client_bulk_callback);
228         LASSERT (desc->bd_cbid.cbid_arg == desc);
229
230         /* XXX Registering the same xid on retried bulk makes my head
231          * explode trying to understand how the original request's bulk
232          * might interfere with the retried request -eeb */
233         LASSERTF (!desc->bd_registered || req->rq_xid != desc->bd_last_xid,
234                   "registered: %d  rq_xid: "LPU64" bd_last_xid: "LPU64"\n",
235                   desc->bd_registered, req->rq_xid, desc->bd_last_xid);
236         desc->bd_registered = 1;
237         desc->bd_last_xid = req->rq_xid;
238
239         rc = LNetMEAttach(desc->bd_portal, peer,
240                          req->rq_xid, 0, LNET_UNLINK, LNET_INS_AFTER, &me_h);
241         if (rc != 0) {
242                 CERROR("LNetMEAttach failed: %d\n", rc);
243                 LASSERT (rc == -ENOMEM);
244                 RETURN (-ENOMEM);
245         }
246
247         /* About to let the network at it... */
248         desc->bd_network_rw = 1;
249         rc = LNetMDAttach(me_h, md, LNET_UNLINK, &desc->bd_md_h);
250         if (rc != 0) {
251                 CERROR("LNetMDAttach failed: %d\n", rc);
252                 LASSERT (rc == -ENOMEM);
253                 desc->bd_network_rw = 0;
254                 rc2 = LNetMEUnlink (me_h);
255                 LASSERT (rc2 == 0);
256                 RETURN (-ENOMEM);
257         }
258
259         CDEBUG(D_NET, "Setup bulk %s buffers: %u pages %u bytes, xid "LPX64", "
260                "portal %u\n",
261                desc->bd_type == BULK_GET_SOURCE ? "get-source" : "put-sink",
262                desc->bd_iov_count, desc->bd_nob,
263                req->rq_xid, desc->bd_portal);
264         RETURN(0);
265 }
266
267 void ptlrpc_unregister_bulk (struct ptlrpc_request *req)
268 {
269         /* Disconnect a bulk desc from the network. Idempotent. Not
270          * thread-safe (i.e. only interlocks with completion callback). */
271         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
272         cfs_waitq_t             *wq;
273         struct l_wait_info       lwi;
274         int                      rc;
275
276         LASSERT (!in_interrupt ());     /* might sleep */
277
278         if (!ptlrpc_bulk_active(desc))  /* completed or */
279                 return;                 /* never registered */
280
281         LASSERT (desc->bd_req == req);  /* bd_req NULL until registered */
282
283         /* the unlink ensures the callback happens ASAP and is the last
284          * one.  If it fails, it must be because completion just happened,
285          * but we must still l_wait_event() in this case to give liblustre
286          * a chance to run client_bulk_callback() */
287
288         LNetMDUnlink (desc->bd_md_h);
289         
290         if (req->rq_set != NULL)
291                 wq = &req->rq_set->set_waitq;
292         else
293                 wq = &req->rq_reply_waitq;
294
295         for (;;) {
296                 /* Network access will complete in finite time but the HUGE
297                  * timeout lets us CWARN for visibility of sluggish NALs */
298                 lwi = LWI_TIMEOUT (cfs_time_seconds(300), NULL, NULL);
299                 rc = l_wait_event(*wq, !ptlrpc_bulk_active(desc), &lwi);
300                 if (rc == 0)
301                         return;
302
303                 LASSERT (rc == -ETIMEDOUT);
304                 DEBUG_REQ(D_WARNING,req,"Unexpectedly long timeout: desc %p",
305                           desc);
306         }
307 }
308
309 int ptlrpc_send_reply (struct ptlrpc_request *req, int flags)
310 {
311         struct ptlrpc_service     *svc = req->rq_rqbd->rqbd_service;
312         struct ptlrpc_reply_state *rs = req->rq_reply_state;
313         struct ptlrpc_connection  *conn;
314         int                        service_time;
315         unsigned int               offset = 0;
316         int                        rc;
317
318         /* We must already have a reply buffer (only ptlrpc_error() may be
319          * called without one).  We must also have a request buffer which
320          * is either the actual (swabbed) incoming request, or a saved copy
321          * if this is a req saved in target_queue_final_reply(). */
322         LASSERT (req->rq_reqmsg != NULL);
323         LASSERT (req->rq_repmsg != NULL);
324         LASSERT (rs != NULL);
325         LASSERT (req->rq_repmsg == rs->rs_msg);
326         LASSERT ((flags & PTLRPC_REPLY_MAYBE_DIFFICULT) || !rs->rs_difficult);
327         LASSERT (rs->rs_cb_id.cbid_fn == reply_out_callback);
328         LASSERT (rs->rs_cb_id.cbid_arg == rs);
329
330         /* There may be no rq_export during failover */
331
332         if (req->rq_export && req->rq_export->exp_obd &&
333             req->rq_export->exp_obd->obd_fail) {
334                 /* Failed obd's only send ENODEV */
335                 req->rq_type = PTL_RPC_MSG_ERR;
336                 req->rq_status = -ENODEV;
337                 CDEBUG(D_HA, "sending ENODEV from failed obd %d\n",
338                        req->rq_export->exp_obd->obd_minor);
339         }
340
341         if (req->rq_type != PTL_RPC_MSG_ERR)
342                 req->rq_type = PTL_RPC_MSG_REPLY;
343
344         lustre_msg_set_type(req->rq_repmsg, req->rq_type);
345         lustre_msg_set_status(req->rq_repmsg, req->rq_status);
346         lustre_msg_set_opc(req->rq_repmsg, lustre_msg_get_opc(req->rq_reqmsg));
347         
348         service_time = max_t(int, cfs_time_current_sec() -
349                              req->rq_arrival_time.tv_sec, 1);
350         if (!(flags & PTLRPC_REPLY_EARLY) && 
351             (req->rq_type != PTL_RPC_MSG_ERR)) {
352                 /* early replies and errors don't count toward our service
353                    time estimate */
354                 int oldse = at_add(&svc->srv_at_estimate, service_time);
355                 if (oldse != 0)
356                         DEBUG_REQ(D_ADAPTTO, req,
357                                   "svc %s changed estimate from %d to %d",
358                                   svc->srv_name, oldse, 
359                                   at_get(&svc->srv_at_estimate));
360         }
361         /* Report actual service time for client latency calc */
362         lustre_msg_set_service_time(req->rq_repmsg, service_time);
363         /* Report service time estimate for future client reqs */
364         lustre_msg_set_timeout(req->rq_repmsg, at_get(&svc->srv_at_estimate));
365
366         if (req->rq_export && req->rq_export->exp_obd)
367                 target_pack_pool_reply(req);
368
369         if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT) {
370                 /* early replies go to offset 0, regular replies go after that*/
371                 if (flags & PTLRPC_REPLY_EARLY) {
372                         offset = 0;
373                         /* Include a checksum on early replies - must be done
374                            after all other lustre_msg_set_* */
375                         lustre_msg_set_cksum(req->rq_repmsg, 
376                                          lustre_msg_calc_cksum(req->rq_repmsg));
377                 } else {
378                         offset = lustre_msg_early_size();
379                 }
380         } else {
381                 CDEBUG(D_ADAPTTO, "No early reply support: flags=%#x "
382                        "req_flags=%#x magic=%d:%x/%x len=%d\n", 
383                       flags, lustre_msg_get_flags(req->rq_reqmsg),
384                       lustre_msg_is_v1(req->rq_reqmsg),
385                       lustre_msg_get_magic(req->rq_reqmsg),
386                       lustre_msg_get_magic(req->rq_repmsg), req->rq_replen);
387         }
388
389         if (req->rq_export == NULL || req->rq_export->exp_connection == NULL)
390                 conn = ptlrpc_get_connection(req->rq_peer, req->rq_self, NULL);
391         else
392                 conn = ptlrpc_connection_addref(req->rq_export->exp_connection);
393
394         if (conn == NULL) {
395                 CERROR("not replying on NULL connection\n"); /* bug 9635 */
396                 return -ENOTCONN;
397         }
398         
399         atomic_inc (&svc->srv_outstanding_replies);
400         ptlrpc_rs_addref(rs);                   /* +1 ref for the network */
401         req->rq_sent = cfs_time_current_sec();
402         
403         rc = ptl_send_buf (&rs->rs_md_h, req->rq_repmsg, req->rq_replen,
404                            rs->rs_difficult ? LNET_ACK_REQ : LNET_NOACK_REQ,
405                            &rs->rs_cb_id, conn, svc->srv_rep_portal,
406                            req->rq_xid, offset);
407         if (rc != 0) {
408                 atomic_dec (&svc->srv_outstanding_replies);
409                 ptlrpc_req_drop_rs(req);
410         }
411         ptlrpc_put_connection(conn);
412         return rc;
413 }
414
415 int ptlrpc_reply (struct ptlrpc_request *req)
416 {
417         return (ptlrpc_send_reply (req, 0));
418 }
419
420 int ptlrpc_error(struct ptlrpc_request *req)
421 {
422         int rc;
423         ENTRY;
424
425         if (!req->rq_repmsg) {
426                 rc = lustre_pack_reply(req, 1, NULL, NULL);
427                 if (rc)
428                         RETURN(rc);
429         }
430
431         req->rq_type = PTL_RPC_MSG_ERR;
432
433         rc = ptlrpc_send_reply(req, 0);
434         RETURN(rc);
435 }
436
437 int ptl_send_rpc(struct ptlrpc_request *request, int noreply)
438 {
439         int rc;
440         int rc2;
441         struct ptlrpc_connection *connection;
442         lnet_handle_me_t  reply_me_h;
443         lnet_md_t         reply_md;
444         ENTRY;
445
446         OBD_FAIL_RETURN(OBD_FAIL_PTLRPC_DROP_RPC, 0); 
447
448         LASSERT (request->rq_type == PTL_RPC_MSG_REQUEST);
449
450         /* If this is a re-transmit, we're required to have disengaged
451          * cleanly from the previous attempt */
452         LASSERT (!request->rq_receiving_reply);
453
454         if (request->rq_import->imp_obd &&
455             request->rq_import->imp_obd->obd_fail) {
456                 CDEBUG(D_HA, "muting rpc for failed imp obd %s\n",
457                        request->rq_import->imp_obd->obd_name);
458                 /* this prevents us from waiting in ptlrpc_queue_wait */
459                 request->rq_err = 1;
460                 RETURN(-ENODEV);
461         }
462         
463         connection = request->rq_import->imp_connection;
464
465         if (request->rq_bulk != NULL) {
466                 rc = ptlrpc_register_bulk (request);
467                 if (rc != 0)
468                         RETURN(rc);
469         }
470
471         lustre_msg_set_handle(request->rq_reqmsg,
472                               &request->rq_import->imp_remote_handle);
473         lustre_msg_set_type(request->rq_reqmsg, PTL_RPC_MSG_REQUEST);
474         lustre_msg_set_conn_cnt(request->rq_reqmsg,
475                                 request->rq_import->imp_conn_cnt);
476
477         if (!noreply) {
478                 LASSERT (request->rq_replen != 0);
479                 if (request->rq_repbuf == NULL)
480                         OBD_ALLOC(request->rq_repbuf, request->rq_replen);
481                 if (request->rq_repbuf == NULL)
482                         GOTO(cleanup_bulk, rc = -ENOMEM);
483                 request->rq_repmsg = NULL;
484
485                 rc = LNetMEAttach(request->rq_reply_portal,/*XXX FIXME bug 249*/
486                                   connection->c_peer, request->rq_xid, 0,
487                                   LNET_UNLINK, LNET_INS_AFTER, &reply_me_h);
488                 if (rc != 0) {
489                         CERROR("LNetMEAttach failed: %d\n", rc);
490                         LASSERT (rc == -ENOMEM);
491                         GOTO(cleanup_repmsg, rc = -ENOMEM);
492                 }
493         }
494
495         spin_lock(&request->rq_lock);
496         /* If the MD attach succeeds, there _will_ be a reply_in callback */
497         request->rq_receiving_reply = !noreply;
498         /* We are responsible for unlinking the reply buffer */
499         request->rq_must_unlink = !noreply;
500         /* Clear any flags that may be present from previous sends. */
501         request->rq_replied = 0;
502         request->rq_err = 0;
503         request->rq_timedout = 0;
504         request->rq_net_err = 0;
505         request->rq_resend = 0;
506         request->rq_restart = 0;
507         spin_unlock(&request->rq_lock);
508
509         if (!noreply) {
510                 reply_md.start     = request->rq_repbuf;
511                 reply_md.length    = request->rq_replen;
512                 /* Allow multiple early replies */
513                 reply_md.threshold = LNET_MD_THRESH_INF;
514                 /* Manage remote for early replies */
515                 reply_md.options   = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT |
516                         LNET_MD_MANAGE_REMOTE;
517                 reply_md.user_ptr  = &request->rq_reply_cbid;
518                 reply_md.eq_handle = ptlrpc_eq_h;
519
520                 /* We must see the unlink callback to unset rq_must_unlink,
521                    so we can't auto-unlink */
522                 rc = LNetMDAttach(reply_me_h, reply_md, LNET_RETAIN, 
523                                  &request->rq_reply_md_h);
524                 if (rc != 0) {
525                         CERROR("LNetMDAttach failed: %d\n", rc);
526                         LASSERT (rc == -ENOMEM);
527                         spin_lock(&request->rq_lock);
528                         /* ...but the MD attach didn't succeed... */
529                         request->rq_receiving_reply = 0;
530                         spin_unlock(&request->rq_lock);
531                         GOTO(cleanup_me, rc -ENOMEM);
532                 }
533
534                 CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid "LPU64
535                        ", portal %u\n",
536                        request->rq_replen, request->rq_xid,
537                        request->rq_reply_portal);
538         }
539
540         /* add references on request and import for request_out_callback */
541         ptlrpc_request_addref(request);
542         atomic_inc(&request->rq_import->imp_inflight);
543
544         OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_SEND, request->rq_timeout + 5);
545
546         request->rq_sent = cfs_time_current_sec();
547         /* We give the server rq_timeout secs to process the req, and 
548            add the network latency for our local timeout. */
549         request->rq_deadline = request->rq_sent + request->rq_timeout + 
550                     ptlrpc_at_get_net_latency(request);
551
552         ptlrpc_pinger_sending_on_import(request->rq_import);
553         
554         DEBUG_REQ(D_INFO, request, "send flg=%x",
555                   lustre_msg_get_flags(request->rq_reqmsg));
556         rc = ptl_send_buf(&request->rq_req_md_h,
557                           request->rq_reqmsg, request->rq_reqlen,
558                           LNET_NOACK_REQ, &request->rq_req_cbid,
559                           connection,
560                           request->rq_request_portal,
561                           request->rq_xid, 0);
562         if (rc == 0) {
563                 ptlrpc_lprocfs_rpc_sent(request);
564                 RETURN(rc);
565         }
566
567          /* drop request_out_callback refs, we couldn't start the send */
568         atomic_dec(&request->rq_import->imp_inflight);
569         ptlrpc_req_finished(request);
570
571         if (noreply)
572                 RETURN(rc);
573
574  cleanup_me:
575         /* MEUnlink is safe; the PUT didn't even get off the ground, and
576          * nobody apart from the PUT's target has the right nid+XID to
577          * access the reply buffer. */
578         rc2 = LNetMEUnlink(reply_me_h);
579         LASSERT (rc2 == 0);
580         /* UNLINKED callback called synchronously */
581         LASSERT (!request->rq_receiving_reply);
582
583  cleanup_repmsg:
584         OBD_FREE(request->rq_repbuf, request->rq_replen);
585         request->rq_repbuf = NULL;
586         request->rq_repmsg = NULL; //remove
587
588  cleanup_bulk:
589         if (request->rq_bulk != NULL)
590                 ptlrpc_unregister_bulk(request);
591
592         return rc;
593 }
594
595 int ptlrpc_register_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
596 {
597         struct ptlrpc_service   *service = rqbd->rqbd_service;
598         static lnet_process_id_t  match_id = {LNET_NID_ANY, LNET_PID_ANY};
599         int                      rc;
600         lnet_md_t                 md;
601         lnet_handle_me_t          me_h;
602
603         CDEBUG(D_NET, "LNetMEAttach: portal %d\n",
604                service->srv_req_portal);
605
606         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_PTLRPC_RQBD))
607                 return (-ENOMEM);
608
609         rc = LNetMEAttach(service->srv_req_portal,
610                           match_id, 0, ~0, LNET_UNLINK, LNET_INS_AFTER, &me_h);
611         if (rc != 0) {
612                 CERROR("LNetMEAttach failed: %d\n", rc);
613                 return (-ENOMEM);
614         }
615
616         LASSERT(rqbd->rqbd_refcount == 0);
617         rqbd->rqbd_refcount = 1;
618
619         md.start     = rqbd->rqbd_buffer;
620         md.length    = service->srv_buf_size;
621         md.max_size  = service->srv_max_req_size;
622         md.threshold = LNET_MD_THRESH_INF;
623         md.options   = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT | LNET_MD_MAX_SIZE;
624         md.user_ptr  = &rqbd->rqbd_cbid;
625         md.eq_handle = ptlrpc_eq_h;
626         
627         rc = LNetMDAttach(me_h, md, LNET_UNLINK, &rqbd->rqbd_md_h);
628         if (rc == 0)
629                 return (0);
630
631         CERROR("LNetMDAttach failed: %d; \n", rc);
632         LASSERT (rc == -ENOMEM);
633         rc = LNetMEUnlink (me_h);
634         LASSERT (rc == 0);
635         rqbd->rqbd_refcount = 0;
636         
637         return (-ENOMEM);
638 }