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