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