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