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