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