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