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