Whamcloud - gitweb
LU-8197 ptlrpc: do not reduce replay request deadline
[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  * Copyright (c) 2012, 2015, Intel Corporation.
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 #include <obd_support.h>
39 #include <lustre_net.h>
40 #include <lustre_lib.h>
41 #include <obd.h>
42 #include <obd_class.h>
43 #include "ptlrpc_internal.h"
44
45 /**
46  * Helper function. Sends \a len bytes from \a base at offset \a offset
47  * over \a conn connection to portal \a portal.
48  * Returns 0 on success or error code.
49  */
50 static int ptl_send_buf (lnet_handle_md_t *mdh, void *base, int len,
51                          lnet_ack_req_t ack, struct ptlrpc_cb_id *cbid,
52                          struct ptlrpc_connection *conn, int portal, __u64 xid,
53                          unsigned int offset)
54 {
55         int              rc;
56         lnet_md_t         md;
57         ENTRY;
58
59         LASSERT (portal != 0);
60         LASSERT (conn != NULL);
61         CDEBUG (D_INFO, "conn=%p id %s\n", conn, libcfs_id2str(conn->c_peer));
62         md.start     = base;
63         md.length    = len;
64         md.threshold = (ack == LNET_ACK_REQ) ? 2 : 1;
65         md.options   = PTLRPC_MD_OPTIONS;
66         md.user_ptr  = cbid;
67         md.eq_handle = ptlrpc_eq_h;
68
69         if (unlikely(ack == LNET_ACK_REQ &&
70                      OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_ACK, OBD_FAIL_ONCE))){
71                 /* don't ask for the ack to simulate failing client */
72                 ack = LNET_NOACK_REQ;
73         }
74
75         rc = LNetMDBind (md, LNET_UNLINK, mdh);
76         if (unlikely(rc != 0)) {
77                 CERROR ("LNetMDBind failed: %d\n", rc);
78                 LASSERT (rc == -ENOMEM);
79                 RETURN (-ENOMEM);
80         }
81
82         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid "LPD64", offset %u\n",
83                len, portal, xid, offset);
84
85         rc = LNetPut (conn->c_self, *mdh, ack,
86                       conn->c_peer, portal, xid, offset, 0);
87         if (unlikely(rc != 0)) {
88                 int rc2;
89                 /* We're going to get an UNLINK event when I unlink below,
90                  * which will complete just like any other failed send, so
91                  * I fall through and return success here! */
92                 CERROR("LNetPut(%s, %d, "LPD64") failed: %d\n",
93                        libcfs_id2str(conn->c_peer), portal, xid, rc);
94                 rc2 = LNetMDUnlink(*mdh);
95                 LASSERTF(rc2 == 0, "rc2 = %d\n", rc2);
96         }
97
98         RETURN (0);
99 }
100
101 static void mdunlink_iterate_helper(lnet_handle_md_t *bd_mds, int count)
102 {
103         int i;
104
105         for (i = 0; i < count; i++)
106                 LNetMDUnlink(bd_mds[i]);
107 }
108
109 #ifdef HAVE_SERVER_SUPPORT
110 /**
111  * Prepare bulk descriptor for specified incoming request \a req that
112  * can fit \a nfrags * pages. \a type is bulk type. \a portal is where
113  * the bulk to be sent. Used on server-side after request was already
114  * received.
115  * Returns pointer to newly allocatrd initialized bulk descriptor or NULL on
116  * error.
117  */
118 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
119                                               unsigned nfrags, unsigned max_brw,
120                                               unsigned int type,
121                                               unsigned portal,
122                                               const struct ptlrpc_bulk_frag_ops
123                                                 *ops)
124 {
125         struct obd_export *exp = req->rq_export;
126         struct ptlrpc_bulk_desc *desc;
127
128         ENTRY;
129         LASSERT(ptlrpc_is_bulk_op_active(type));
130
131         desc = ptlrpc_new_bulk(nfrags, max_brw, type, portal, ops);
132         if (desc == NULL)
133                 RETURN(NULL);
134
135         desc->bd_export = class_export_get(exp);
136         desc->bd_req = req;
137
138         desc->bd_cbid.cbid_fn  = server_bulk_callback;
139         desc->bd_cbid.cbid_arg = desc;
140
141         /* NB we don't assign rq_bulk here; server-side requests are
142          * re-used, and the handler frees the bulk desc explicitly. */
143
144         return desc;
145 }
146 EXPORT_SYMBOL(ptlrpc_prep_bulk_exp);
147
148 /**
149  * Starts bulk transfer for descriptor \a desc on the server.
150  * Returns 0 on success or error code.
151  */
152 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc)
153 {
154         struct obd_export        *exp = desc->bd_export;
155         struct ptlrpc_connection *conn = exp->exp_connection;
156         int                       rc = 0;
157         __u64                     mbits;
158         int                       posted_md;
159         int                       total_md;
160         lnet_md_t                 md;
161         ENTRY;
162
163         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_PUT_NET))
164                 RETURN(0);
165
166         /* NB no locking required until desc is on the network */
167         LASSERT(desc->bd_md_count == 0);
168         LASSERT(ptlrpc_is_bulk_op_active(desc->bd_type));
169
170         LASSERT(desc->bd_cbid.cbid_fn == server_bulk_callback);
171         LASSERT(desc->bd_cbid.cbid_arg == desc);
172
173         /* NB total length may be 0 for a read past EOF, so we send 0
174          * length bulks, since the client expects bulk events.
175          *
176          * The client may not need all of the bulk mbits for the RPC. The RPC
177          * used the mbits of the highest bulk mbits needed, and the server masks
178          * off high bits to get bulk count for this RPC. LU-1431 */
179         mbits = desc->bd_req->rq_mbits & ~((__u64)desc->bd_md_max_brw - 1);
180         total_md = desc->bd_req->rq_mbits - mbits + 1;
181
182         desc->bd_md_count = total_md;
183         desc->bd_failure = 0;
184
185         md.user_ptr = &desc->bd_cbid;
186         md.eq_handle = ptlrpc_eq_h;
187         md.threshold = 2; /* SENT and ACK/REPLY */
188
189         for (posted_md = 0; posted_md < total_md; mbits++) {
190                 md.options = PTLRPC_MD_OPTIONS;
191
192                 /* NB it's assumed that source and sink buffer frags are
193                  * page-aligned. Otherwise we'd have to send client bulk
194                  * sizes over and split server buffer accordingly */
195                 ptlrpc_fill_bulk_md(&md, desc, posted_md);
196                 rc = LNetMDBind(md, LNET_UNLINK, &desc->bd_mds[posted_md]);
197                 if (rc != 0) {
198                         CERROR("%s: LNetMDBind failed for MD %u: rc = %d\n",
199                                exp->exp_obd->obd_name, posted_md, rc);
200                         LASSERT(rc == -ENOMEM);
201                         if (posted_md == 0) {
202                                 desc->bd_md_count = 0;
203                                 RETURN(-ENOMEM);
204                         }
205                         break;
206                 }
207
208                 /* LU-6441: last md is not sent and desc->bd_md_count == 1 */
209                 if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_CLIENT_BULK_CB3,
210                                          CFS_FAIL_ONCE) &&
211                     posted_md == desc->bd_md_max_brw - 1) {
212                         posted_md++;
213                         continue;
214                 }
215
216                 /* Network is about to get at the memory */
217                 if (ptlrpc_is_bulk_put_source(desc->bd_type))
218                         rc = LNetPut(conn->c_self, desc->bd_mds[posted_md],
219                                      LNET_ACK_REQ, conn->c_peer,
220                                      desc->bd_portal, mbits, 0, 0);
221                 else
222                         rc = LNetGet(conn->c_self, desc->bd_mds[posted_md],
223                                      conn->c_peer, desc->bd_portal, mbits, 0);
224
225                 posted_md++;
226                 if (rc != 0) {
227                         CERROR("%s: failed bulk transfer with %s:%u x"LPU64": "
228                                "rc = %d\n", exp->exp_obd->obd_name,
229                                libcfs_id2str(conn->c_peer), desc->bd_portal,
230                                mbits, rc);
231                         break;
232                 }
233         }
234
235         if (rc != 0) {
236                 /* Can't send, so we unlink the MD bound above.  The UNLINK
237                  * event this creates will signal completion with failure,
238                  * so we return SUCCESS here! */
239                 spin_lock(&desc->bd_lock);
240                 desc->bd_md_count -= total_md - posted_md;
241                 spin_unlock(&desc->bd_lock);
242                 LASSERT(desc->bd_md_count >= 0);
243
244                 mdunlink_iterate_helper(desc->bd_mds, posted_md);
245                 RETURN(0);
246         }
247
248         CDEBUG(D_NET, "Transferring %u pages %u bytes via portal %d "
249                "id %s mbits "LPX64"-"LPX64"\n", desc->bd_iov_count,
250                desc->bd_nob, desc->bd_portal, libcfs_id2str(conn->c_peer),
251                mbits - posted_md, mbits - 1);
252
253         RETURN(0);
254 }
255
256 /**
257  * Server side bulk abort. Idempotent. Not thread-safe (i.e. only
258  * serialises with completion callback)
259  */
260 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc)
261 {
262         struct l_wait_info       lwi;
263         int                      rc;
264
265         LASSERT(!in_interrupt());           /* might sleep */
266
267         if (!ptlrpc_server_bulk_active(desc))   /* completed or */
268                 return;                         /* never started */
269
270         /* We used to poison the pages with 0xab here because we did not want to
271          * send any meaningful data over the wire for evicted clients (bug 9297)
272          * However, this is no longer safe now that we use the page cache on the
273          * OSS (bug 20560) */
274
275         /* The unlink ensures the callback happens ASAP and is the last
276          * one.  If it fails, it must be because completion just happened,
277          * but we must still l_wait_event() in this case, to give liblustre
278          * a chance to run server_bulk_callback()*/
279         mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_max_brw);
280
281         for (;;) {
282                 /* Network access will complete in finite time but the HUGE
283                  * timeout lets us CWARN for visibility of sluggish NALs */
284                 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
285                                            cfs_time_seconds(1), NULL, NULL);
286                 rc = l_wait_event(desc->bd_waitq,
287                                   !ptlrpc_server_bulk_active(desc), &lwi);
288                 if (rc == 0)
289                         return;
290
291                 LASSERT(rc == -ETIMEDOUT);
292                 CWARN("Unexpectedly long timeout: desc %p\n", desc);
293         }
294 }
295 #endif /* HAVE_SERVER_SUPPORT */
296
297 /**
298  * Register bulk at the sender for later transfer.
299  * Returns 0 on success or error code.
300  */
301 int ptlrpc_register_bulk(struct ptlrpc_request *req)
302 {
303         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
304         lnet_process_id_t peer;
305         int rc = 0;
306         int rc2;
307         int posted_md;
308         int total_md;
309         __u64 mbits;
310         lnet_handle_me_t  me_h;
311         lnet_md_t         md;
312         ENTRY;
313
314         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_GET_NET))
315                 RETURN(0);
316
317         /* NB no locking required until desc is on the network */
318         LASSERT(desc->bd_nob > 0);
319         LASSERT(desc->bd_md_count == 0);
320         LASSERT(desc->bd_md_max_brw <= PTLRPC_BULK_OPS_COUNT);
321         LASSERT(desc->bd_iov_count <= PTLRPC_MAX_BRW_PAGES);
322         LASSERT(desc->bd_req != NULL);
323         LASSERT(ptlrpc_is_bulk_op_passive(desc->bd_type));
324
325         /* cleanup the state of the bulk for it will be reused */
326         if (req->rq_resend || req->rq_send_state == LUSTRE_IMP_REPLAY)
327                 desc->bd_nob_transferred = 0;
328         else
329                 LASSERT(desc->bd_nob_transferred == 0);
330
331         desc->bd_failure = 0;
332
333         peer = desc->bd_import->imp_connection->c_peer;
334
335         LASSERT(desc->bd_cbid.cbid_fn == client_bulk_callback);
336         LASSERT(desc->bd_cbid.cbid_arg == desc);
337
338         total_md = (desc->bd_iov_count + LNET_MAX_IOV - 1) / LNET_MAX_IOV;
339         /* rq_mbits is matchbits of the final bulk */
340         mbits = req->rq_mbits - total_md + 1;
341
342         LASSERTF(mbits == (req->rq_mbits & PTLRPC_BULK_OPS_MASK),
343                  "first mbits = x"LPU64", last mbits = x"LPU64"\n",
344                  mbits, req->rq_mbits);
345         LASSERTF(!(desc->bd_registered &&
346                    req->rq_send_state != LUSTRE_IMP_REPLAY) ||
347                  mbits != desc->bd_last_mbits,
348                  "registered: %d  rq_mbits: "LPU64" bd_last_mbits: "LPU64"\n",
349                  desc->bd_registered, mbits, desc->bd_last_mbits);
350
351         desc->bd_registered = 1;
352         desc->bd_last_mbits = mbits;
353         desc->bd_md_count = total_md;
354         md.user_ptr = &desc->bd_cbid;
355         md.eq_handle = ptlrpc_eq_h;
356         md.threshold = 1;                       /* PUT or GET */
357
358         for (posted_md = 0; posted_md < total_md; posted_md++, mbits++) {
359                 md.options = PTLRPC_MD_OPTIONS |
360                              (ptlrpc_is_bulk_op_get(desc->bd_type) ?
361                               LNET_MD_OP_GET : LNET_MD_OP_PUT);
362                 ptlrpc_fill_bulk_md(&md, desc, posted_md);
363
364                 rc = LNetMEAttach(desc->bd_portal, peer, mbits, 0,
365                                   LNET_UNLINK, LNET_INS_AFTER, &me_h);
366                 if (rc != 0) {
367                         CERROR("%s: LNetMEAttach failed x"LPU64"/%d: rc = %d\n",
368                                desc->bd_import->imp_obd->obd_name, mbits,
369                                posted_md, rc);
370                         break;
371                 }
372
373                 /* About to let the network at it... */
374                 rc = LNetMDAttach(me_h, md, LNET_UNLINK,
375                                   &desc->bd_mds[posted_md]);
376                 if (rc != 0) {
377                         CERROR("%s: LNetMDAttach failed x"LPU64"/%d: rc = %d\n",
378                                desc->bd_import->imp_obd->obd_name, mbits,
379                                posted_md, rc);
380                         rc2 = LNetMEUnlink(me_h);
381                         LASSERT(rc2 == 0);
382                         break;
383                 }
384         }
385
386         if (rc != 0) {
387                 LASSERT(rc == -ENOMEM);
388                 spin_lock(&desc->bd_lock);
389                 desc->bd_md_count -= total_md - posted_md;
390                 spin_unlock(&desc->bd_lock);
391                 LASSERT(desc->bd_md_count >= 0);
392                 mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_max_brw);
393                 req->rq_status = -ENOMEM;
394                 RETURN(-ENOMEM);
395         }
396
397         spin_lock(&desc->bd_lock);
398         /* Holler if peer manages to touch buffers before he knows the mbits */
399         if (desc->bd_md_count != total_md)
400                 CWARN("%s: Peer %s touched %d buffers while I registered\n",
401                       desc->bd_import->imp_obd->obd_name, libcfs_id2str(peer),
402                       total_md - desc->bd_md_count);
403         spin_unlock(&desc->bd_lock);
404
405         CDEBUG(D_NET, "Setup %u bulk %s buffers: %u pages %u bytes, "
406                "mbits x"LPX64"-"LPX64", portal %u\n", desc->bd_md_count,
407                ptlrpc_is_bulk_op_get(desc->bd_type) ? "get-source" : "put-sink",
408                desc->bd_iov_count, desc->bd_nob,
409                desc->bd_last_mbits, req->rq_mbits, desc->bd_portal);
410
411         RETURN(0);
412 }
413
414 /**
415  * Disconnect a bulk desc from the network. Idempotent. Not
416  * thread-safe (i.e. only interlocks with completion callback).
417  * Returns 1 on success or 0 if network unregistration failed for whatever
418  * reason.
419  */
420 int ptlrpc_unregister_bulk(struct ptlrpc_request *req, int async)
421 {
422         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
423         struct l_wait_info       lwi;
424         int                      rc;
425         ENTRY;
426
427         LASSERT(!in_interrupt());     /* might sleep */
428
429         /* Let's setup deadline for reply unlink. */
430         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK) &&
431             async && req->rq_bulk_deadline == 0 && cfs_fail_val == 0)
432                 req->rq_bulk_deadline = cfs_time_current_sec() + LONG_UNLINK;
433
434         if (ptlrpc_client_bulk_active(req) == 0)        /* completed or */
435                 RETURN(1);                              /* never registered */
436
437         LASSERT(desc->bd_req == req);  /* bd_req NULL until registered */
438
439         /* the unlink ensures the callback happens ASAP and is the last
440          * one.  If it fails, it must be because completion just happened,
441          * but we must still l_wait_event() in this case to give liblustre
442          * a chance to run client_bulk_callback() */
443         mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_max_brw);
444
445         if (ptlrpc_client_bulk_active(req) == 0)        /* completed or */
446                 RETURN(1);                              /* never registered */
447
448         /* Move to "Unregistering" phase as bulk was not unlinked yet. */
449         ptlrpc_rqphase_move(req, RQ_PHASE_UNREG_BULK);
450
451         /* Do not wait for unlink to finish. */
452         if (async)
453                 RETURN(0);
454
455         for (;;) {
456                 /* The wq argument is ignored by user-space wait_event macros */
457                 wait_queue_head_t *wq = (req->rq_set != NULL) ?
458                                         &req->rq_set->set_waitq :
459                                         &req->rq_reply_waitq;
460                 /* Network access will complete in finite time but the HUGE
461                  * timeout lets us CWARN for visibility of sluggish NALs */
462                 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
463                                            cfs_time_seconds(1), NULL, NULL);
464                 rc = l_wait_event(*wq, !ptlrpc_client_bulk_active(req), &lwi);
465                 if (rc == 0) {
466                         ptlrpc_rqphase_move(req, req->rq_next_phase);
467                         RETURN(1);
468                 }
469
470                 LASSERT(rc == -ETIMEDOUT);
471                 DEBUG_REQ(D_WARNING, req, "Unexpectedly long timeout: desc %p",
472                           desc);
473         }
474         RETURN(0);
475 }
476
477 static void ptlrpc_at_set_reply(struct ptlrpc_request *req, int flags)
478 {
479         struct ptlrpc_service_part      *svcpt = req->rq_rqbd->rqbd_svcpt;
480         struct ptlrpc_service           *svc = svcpt->scp_service;
481         int service_time = max_t(int, cfs_time_current_sec() -
482                                  req->rq_arrival_time.tv_sec, 1);
483
484         if (!(flags & PTLRPC_REPLY_EARLY) &&
485             (req->rq_type != PTL_RPC_MSG_ERR) &&
486             (req->rq_reqmsg != NULL) &&
487             !(lustre_msg_get_flags(req->rq_reqmsg) &
488               (MSG_RESENT | MSG_REPLAY |
489                MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE))) {
490                 /* early replies, errors and recovery requests don't count
491                  * toward our service time estimate */
492                 int oldse = at_measured(&svcpt->scp_at_estimate, service_time);
493
494                 if (oldse != 0) {
495                         DEBUG_REQ(D_ADAPTTO, req,
496                                   "svc %s changed estimate from %d to %d",
497                                   svc->srv_name, oldse,
498                                   at_get(&svcpt->scp_at_estimate));
499                 }
500         }
501         /* Report actual service time for client latency calc */
502         lustre_msg_set_service_time(req->rq_repmsg, service_time);
503         /* Report service time estimate for future client reqs, but report 0
504          * (to be ignored by client) if it's an error reply during recovery.
505          * (bz15815) */
506         if (req->rq_type == PTL_RPC_MSG_ERR &&
507             (req->rq_export == NULL ||
508              req->rq_export->exp_obd->obd_recovering)) {
509                 lustre_msg_set_timeout(req->rq_repmsg, 0);
510         } else {
511                 __u32 timeout;
512
513                 if (req->rq_export && req->rq_reqmsg != NULL &&
514                     lustre_msg_get_flags(req->rq_reqmsg) &
515                     (MSG_REPLAY | MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE))
516                         timeout = cfs_time_current_sec() -
517                                 req->rq_arrival_time.tv_sec +
518                                 min(at_extra,
519                                     req->rq_export->exp_obd->
520                                     obd_recovery_timeout / 4);
521                 else
522                         timeout = at_get(&svcpt->scp_at_estimate);
523                 lustre_msg_set_timeout(req->rq_repmsg, timeout);
524         }
525
526         if (req->rq_reqmsg &&
527             !(lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
528                 CDEBUG(D_ADAPTTO, "No early reply support: flags=%#x "
529                        "req_flags=%#x magic=%x/%x len=%d\n",
530                        flags, lustre_msg_get_flags(req->rq_reqmsg),
531                        lustre_msg_get_magic(req->rq_reqmsg),
532                        lustre_msg_get_magic(req->rq_repmsg), req->rq_replen);
533         }
534 }
535
536 /**
537  * Send request reply from request \a req reply buffer.
538  * \a flags defines reply types
539  * Returns 0 on success or error code
540  */
541 int ptlrpc_send_reply(struct ptlrpc_request *req, int flags)
542 {
543         struct ptlrpc_reply_state *rs = req->rq_reply_state;
544         struct ptlrpc_connection  *conn;
545         int                        rc;
546
547         /* We must already have a reply buffer (only ptlrpc_error() may be
548          * called without one). The reply generated by sptlrpc layer (e.g.
549          * error notify, etc.) might have NULL rq->reqmsg; Otherwise we must
550          * have a request buffer which is either the actual (swabbed) incoming
551          * request, or a saved copy if this is a req saved in
552          * target_queue_final_reply().
553          */
554         LASSERT (req->rq_no_reply == 0);
555         LASSERT (req->rq_reqbuf != NULL);
556         LASSERT (rs != NULL);
557         LASSERT ((flags & PTLRPC_REPLY_MAYBE_DIFFICULT) || !rs->rs_difficult);
558         LASSERT (req->rq_repmsg != NULL);
559         LASSERT (req->rq_repmsg == rs->rs_msg);
560         LASSERT (rs->rs_cb_id.cbid_fn == reply_out_callback);
561         LASSERT (rs->rs_cb_id.cbid_arg == rs);
562
563         /* There may be no rq_export during failover */
564
565         if (unlikely(req->rq_export && req->rq_export->exp_obd &&
566                      req->rq_export->exp_obd->obd_fail)) {
567                 /* Failed obd's only send ENODEV */
568                 req->rq_type = PTL_RPC_MSG_ERR;
569                 req->rq_status = -ENODEV;
570                 CDEBUG(D_HA, "sending ENODEV from failed obd %d\n",
571                        req->rq_export->exp_obd->obd_minor);
572         }
573
574         /* In order to keep interoprability with the client (< 2.3) which
575          * doesn't have pb_jobid in ptlrpc_body, We have to shrink the
576          * ptlrpc_body in reply buffer to ptlrpc_body_v2, otherwise, the
577          * reply buffer on client will be overflow.
578          *
579          * XXX Remove this whenver we drop the interoprability with such client.
580          */
581         req->rq_replen = lustre_shrink_msg(req->rq_repmsg, 0,
582                                            sizeof(struct ptlrpc_body_v2), 1);
583
584         if (req->rq_type != PTL_RPC_MSG_ERR)
585                 req->rq_type = PTL_RPC_MSG_REPLY;
586
587         lustre_msg_set_type(req->rq_repmsg, req->rq_type);
588         lustre_msg_set_status(req->rq_repmsg,
589                               ptlrpc_status_hton(req->rq_status));
590         lustre_msg_set_opc(req->rq_repmsg,
591                 req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : 0);
592
593         target_pack_pool_reply(req);
594
595         ptlrpc_at_set_reply(req, flags);
596
597         if (req->rq_export == NULL || req->rq_export->exp_connection == NULL)
598                 conn = ptlrpc_connection_get(req->rq_peer, req->rq_self, NULL);
599         else
600                 conn = ptlrpc_connection_addref(req->rq_export->exp_connection);
601
602         if (unlikely(conn == NULL)) {
603                 CERROR("not replying on NULL connection\n"); /* bug 9635 */
604                 return -ENOTCONN;
605         }
606         ptlrpc_rs_addref(rs);                   /* +1 ref for the network */
607
608         rc = sptlrpc_svc_wrap_reply(req);
609         if (unlikely(rc))
610                 goto out;
611
612         req->rq_sent = cfs_time_current_sec();
613
614         rc = ptl_send_buf (&rs->rs_md_h, rs->rs_repbuf, rs->rs_repdata_len,
615                            (rs->rs_difficult && !rs->rs_no_ack) ?
616                            LNET_ACK_REQ : LNET_NOACK_REQ,
617                            &rs->rs_cb_id, conn,
618                            ptlrpc_req2svc(req)->srv_rep_portal,
619                            req->rq_xid, req->rq_reply_off);
620 out:
621         if (unlikely(rc != 0))
622                 ptlrpc_req_drop_rs(req);
623         ptlrpc_connection_put(conn);
624         return rc;
625 }
626
627 int ptlrpc_reply (struct ptlrpc_request *req)
628 {
629         if (req->rq_no_reply)
630                 return 0;
631         else
632                 return (ptlrpc_send_reply(req, 0));
633 }
634
635 /**
636  * For request \a req send an error reply back. Create empty
637  * reply buffers if necessary.
638  */
639 int ptlrpc_send_error(struct ptlrpc_request *req, int may_be_difficult)
640 {
641         int rc;
642         ENTRY;
643
644         if (req->rq_no_reply)
645                 RETURN(0);
646
647         if (!req->rq_repmsg) {
648                 rc = lustre_pack_reply(req, 1, NULL, NULL);
649                 if (rc)
650                         RETURN(rc);
651         }
652
653         if (req->rq_status != -ENOSPC && req->rq_status != -EACCES &&
654             req->rq_status != -EPERM && req->rq_status != -ENOENT &&
655             req->rq_status != -EINPROGRESS && req->rq_status != -EDQUOT)
656                 req->rq_type = PTL_RPC_MSG_ERR;
657
658         rc = ptlrpc_send_reply(req, may_be_difficult);
659         RETURN(rc);
660 }
661
662 int ptlrpc_error(struct ptlrpc_request *req)
663 {
664         return ptlrpc_send_error(req, 0);
665 }
666
667 /**
668  * Send request \a request.
669  * if \a noreply is set, don't expect any reply back and don't set up
670  * reply buffers.
671  * Returns 0 on success or error code.
672  */
673 int ptl_send_rpc(struct ptlrpc_request *request, int noreply)
674 {
675         int rc;
676         int rc2;
677         int mpflag = 0;
678         struct ptlrpc_connection *connection;
679         lnet_handle_me_t  reply_me_h;
680         lnet_md_t         reply_md;
681         struct obd_import *imp = request->rq_import;
682         struct obd_device *obd = imp->imp_obd;
683         ENTRY;
684
685         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_RPC))
686                 RETURN(0);
687
688         LASSERT(request->rq_type == PTL_RPC_MSG_REQUEST);
689         LASSERT(request->rq_wait_ctx == 0);
690
691         /* If this is a re-transmit, we're required to have disengaged
692          * cleanly from the previous attempt */
693         LASSERT(!request->rq_receiving_reply);
694         LASSERT(!((lustre_msg_get_flags(request->rq_reqmsg) & MSG_REPLAY) &&
695                 (imp->imp_state == LUSTRE_IMP_FULL)));
696
697         if (unlikely(obd != NULL && obd->obd_fail)) {
698                 CDEBUG(D_HA, "muting rpc for failed imp obd %s\n",
699                         obd->obd_name);
700                 /* this prevents us from waiting in ptlrpc_queue_wait */
701                 spin_lock(&request->rq_lock);
702                 request->rq_err = 1;
703                 spin_unlock(&request->rq_lock);
704                 request->rq_status = -ENODEV;
705                 RETURN(-ENODEV);
706         }
707
708         connection = imp->imp_connection;
709
710         lustre_msg_set_handle(request->rq_reqmsg,
711                               &imp->imp_remote_handle);
712         lustre_msg_set_type(request->rq_reqmsg, PTL_RPC_MSG_REQUEST);
713         lustre_msg_set_conn_cnt(request->rq_reqmsg,
714                                 imp->imp_conn_cnt);
715         lustre_msghdr_set_flags(request->rq_reqmsg,
716                                 imp->imp_msghdr_flags);
717
718         /* If it's the first time to resend the request for EINPROGRESS,
719          * we need to allocate a new XID (see after_reply()), it's different
720          * from the resend for reply timeout. */
721         if (request->rq_nr_resend != 0 &&
722             list_empty(&request->rq_unreplied_list)) {
723                 __u64 min_xid = 0;
724                 /* resend for EINPROGRESS, allocate new xid to avoid reply
725                  * reconstruction */
726                 spin_lock(&imp->imp_lock);
727                 ptlrpc_assign_next_xid_nolock(request);
728                 min_xid = ptlrpc_known_replied_xid(imp);
729                 spin_unlock(&imp->imp_lock);
730
731                 lustre_msg_set_last_xid(request->rq_reqmsg, min_xid);
732                 DEBUG_REQ(D_RPCTRACE, request, "Allocating new xid for "
733                           "resend on EINPROGRESS");
734         }
735
736         if (request->rq_bulk != NULL) {
737                 ptlrpc_set_bulk_mbits(request);
738                 lustre_msg_set_mbits(request->rq_reqmsg, request->rq_mbits);
739         }
740
741         if (list_empty(&request->rq_unreplied_list) ||
742             request->rq_xid <= imp->imp_known_replied_xid) {
743                 DEBUG_REQ(D_ERROR, request, "xid: "LPU64", replied: "LPU64", "
744                           "list_empty:%d\n", request->rq_xid,
745                           imp->imp_known_replied_xid,
746                           list_empty(&request->rq_unreplied_list));
747                 LBUG();
748         }
749
750         /** For enabled AT all request should have AT_SUPPORT in the
751          * FULL import state when OBD_CONNECT_AT is set */
752         LASSERT(AT_OFF || imp->imp_state != LUSTRE_IMP_FULL ||
753                 (imp->imp_msghdr_flags & MSGHDR_AT_SUPPORT) ||
754                 !(imp->imp_connect_data.ocd_connect_flags &
755                 OBD_CONNECT_AT));
756
757         if (request->rq_resend) {
758                 lustre_msg_add_flags(request->rq_reqmsg, MSG_RESENT);
759                 if (request->rq_resend_cb != NULL)
760                         request->rq_resend_cb(request, &request->rq_async_args);
761         }
762         if (request->rq_memalloc)
763                 mpflag = cfs_memory_pressure_get_and_set();
764
765         rc = sptlrpc_cli_wrap_request(request);
766         if (rc == -ENOMEM)
767                 /* set rq_sent so that this request is treated
768                  * as a delayed send in the upper layers */
769                 request->rq_sent = cfs_time_current_sec();
770         if (rc)
771                 GOTO(out, rc);
772
773         /* bulk register should be done after wrap_request() */
774         if (request->rq_bulk != NULL) {
775                 rc = ptlrpc_register_bulk (request);
776                 if (rc != 0)
777                         GOTO(out, rc);
778         }
779
780         if (!noreply) {
781                 LASSERT (request->rq_replen != 0);
782                 if (request->rq_repbuf == NULL) {
783                         LASSERT(request->rq_repdata == NULL);
784                         LASSERT(request->rq_repmsg == NULL);
785                         rc = sptlrpc_cli_alloc_repbuf(request,
786                                                       request->rq_replen);
787                         if (rc) {
788                                 /* this prevents us from looping in
789                                  * ptlrpc_queue_wait */
790                                 spin_lock(&request->rq_lock);
791                                 request->rq_err = 1;
792                                 spin_unlock(&request->rq_lock);
793                                 request->rq_status = rc;
794                                 GOTO(cleanup_bulk, rc);
795                         }
796                 } else {
797                         request->rq_repdata = NULL;
798                         request->rq_repmsg = NULL;
799                 }
800
801                 rc = LNetMEAttach(request->rq_reply_portal,/*XXX FIXME bug 249*/
802                                   connection->c_peer, request->rq_xid, 0,
803                                   LNET_UNLINK, LNET_INS_AFTER, &reply_me_h);
804                 if (rc != 0) {
805                         CERROR("LNetMEAttach failed: %d\n", rc);
806                         LASSERT (rc == -ENOMEM);
807                         GOTO(cleanup_bulk, rc = -ENOMEM);
808                 }
809         }
810
811         spin_lock(&request->rq_lock);
812         /* We are responsible for unlinking the reply buffer */
813         request->rq_reply_unlinked = noreply;
814         request->rq_receiving_reply = !noreply;
815         /* Clear any flags that may be present from previous sends. */
816         request->rq_req_unlinked = 0;
817         request->rq_replied = 0;
818         request->rq_err = 0;
819         request->rq_timedout = 0;
820         request->rq_net_err = 0;
821         request->rq_resend = 0;
822         request->rq_restart = 0;
823         request->rq_reply_truncated = 0;
824         spin_unlock(&request->rq_lock);
825
826         if (!noreply) {
827                 reply_md.start     = request->rq_repbuf;
828                 reply_md.length    = request->rq_repbuf_len;
829                 /* Allow multiple early replies */
830                 reply_md.threshold = LNET_MD_THRESH_INF;
831                 /* Manage remote for early replies */
832                 reply_md.options   = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT |
833                         LNET_MD_MANAGE_REMOTE |
834                         LNET_MD_TRUNCATE; /* allow to make EOVERFLOW error */;
835                 reply_md.user_ptr  = &request->rq_reply_cbid;
836                 reply_md.eq_handle = ptlrpc_eq_h;
837
838                 /* We must see the unlink callback to set rq_reply_unlinked,
839                  * so we can't auto-unlink */
840                 rc = LNetMDAttach(reply_me_h, reply_md, LNET_RETAIN,
841                                   &request->rq_reply_md_h);
842                 if (rc != 0) {
843                         CERROR("LNetMDAttach failed: %d\n", rc);
844                         LASSERT (rc == -ENOMEM);
845                         spin_lock(&request->rq_lock);
846                         /* ...but the MD attach didn't succeed... */
847                         request->rq_receiving_reply = 0;
848                         spin_unlock(&request->rq_lock);
849                         GOTO(cleanup_me, rc = -ENOMEM);
850                 }
851
852                 CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid "LPU64
853                        ", portal %u\n",
854                        request->rq_repbuf_len, request->rq_xid,
855                        request->rq_reply_portal);
856         }
857
858         /* add references on request for request_out_callback */
859         ptlrpc_request_addref(request);
860         if (obd != NULL && obd->obd_svc_stats != NULL)
861                 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQACTIVE_CNTR,
862                         atomic_read(&imp->imp_inflight));
863
864         OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_SEND, request->rq_timeout + 5);
865
866         do_gettimeofday(&request->rq_sent_tv);
867         request->rq_sent = cfs_time_current_sec();
868         /* We give the server rq_timeout secs to process the req, and
869            add the network latency for our local timeout. */
870         request->rq_deadline = request->rq_sent + request->rq_timeout +
871                 ptlrpc_at_get_net_latency(request);
872
873         ptlrpc_pinger_sending_on_import(imp);
874
875         DEBUG_REQ(D_INFO, request, "send flg=%x",
876                   lustre_msg_get_flags(request->rq_reqmsg));
877         rc = ptl_send_buf(&request->rq_req_md_h,
878                           request->rq_reqbuf, request->rq_reqdata_len,
879                           LNET_NOACK_REQ, &request->rq_req_cbid,
880                           connection,
881                           request->rq_request_portal,
882                           request->rq_xid, 0);
883         if (likely(rc == 0))
884                 GOTO(out, rc);
885
886         request->rq_req_unlinked = 1;
887         ptlrpc_req_finished(request);
888         if (noreply)
889                 GOTO(out, rc);
890
891  cleanup_me:
892         /* MEUnlink is safe; the PUT didn't even get off the ground, and
893          * nobody apart from the PUT's target has the right nid+XID to
894          * access the reply buffer. */
895         rc2 = LNetMEUnlink(reply_me_h);
896         LASSERT (rc2 == 0);
897         /* UNLINKED callback called synchronously */
898         LASSERT(!request->rq_receiving_reply);
899
900  cleanup_bulk:
901         /* We do sync unlink here as there was no real transfer here so
902          * the chance to have long unlink to sluggish net is smaller here. */
903         ptlrpc_unregister_bulk(request, 0);
904  out:
905         if (request->rq_memalloc)
906                 cfs_memory_pressure_restore(mpflag);
907         return rc;
908 }
909 EXPORT_SYMBOL(ptl_send_rpc);
910
911 /**
912  * Register request buffer descriptor for request receiving.
913  */
914 int ptlrpc_register_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
915 {
916         struct ptlrpc_service     *service = rqbd->rqbd_svcpt->scp_service;
917         static lnet_process_id_t  match_id = {LNET_NID_ANY, LNET_PID_ANY};
918         int                       rc;
919         lnet_md_t                 md;
920         lnet_handle_me_t          me_h;
921
922         CDEBUG(D_NET, "LNetMEAttach: portal %d\n",
923                service->srv_req_portal);
924
925         if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_RQBD))
926                 return (-ENOMEM);
927
928         /* NB: CPT affinity service should use new LNet flag LNET_INS_LOCAL,
929          * which means buffer can only be attached on local CPT, and LND
930          * threads can find it by grabbing a local lock */
931         rc = LNetMEAttach(service->srv_req_portal,
932                           match_id, 0, ~0, LNET_UNLINK,
933                           rqbd->rqbd_svcpt->scp_cpt >= 0 ?
934                           LNET_INS_LOCAL : LNET_INS_AFTER, &me_h);
935         if (rc != 0) {
936                 CERROR("LNetMEAttach failed: %d\n", rc);
937                 return (-ENOMEM);
938         }
939
940         LASSERT(rqbd->rqbd_refcount == 0);
941         rqbd->rqbd_refcount = 1;
942
943         md.start     = rqbd->rqbd_buffer;
944         md.length    = service->srv_buf_size;
945         md.max_size  = service->srv_max_req_size;
946         md.threshold = LNET_MD_THRESH_INF;
947         md.options   = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT | LNET_MD_MAX_SIZE;
948         md.user_ptr  = &rqbd->rqbd_cbid;
949         md.eq_handle = ptlrpc_eq_h;
950
951         rc = LNetMDAttach(me_h, md, LNET_UNLINK, &rqbd->rqbd_md_h);
952         if (rc == 0)
953                 return (0);
954
955         CERROR("LNetMDAttach failed: %d; \n", rc);
956         LASSERT (rc == -ENOMEM);
957         rc = LNetMEUnlink (me_h);
958         LASSERT (rc == 0);
959         rqbd->rqbd_refcount = 0;
960
961         return (-ENOMEM);
962 }