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