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