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