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