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