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