4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_RPC
34 #include <obd_support.h>
35 #include <lustre_net.h>
36 #include <lustre_lib.h>
38 #include <obd_class.h>
39 #include "ptlrpc_internal.h"
40 #include <lnet/lib-lnet.h> /* for CFS_FAIL_PTLRPC_OST_BULK_CB2 */
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.
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)
57 LASSERT (portal != 0);
58 CDEBUG (D_INFO, "peer_id %s\n", libcfs_id2str(peer_id));
61 md.threshold = (ack == LNET_ACK_REQ) ? 2 : 1;
62 md.options = PTLRPC_MD_OPTIONS;
64 md.eq_handle = ptlrpc_eq_h;
65 LNetInvalidateMDHandle(&md.bulk_handle);
68 md.bulk_handle = *bulk_cookie;
69 md.options |= LNET_MD_BULK_HANDLE;
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 */
78 rc = LNetMDBind (md, LNET_UNLINK, mdh);
79 if (unlikely(rc != 0)) {
80 CERROR ("LNetMDBind failed: %d\n", rc);
81 LASSERT (rc == -ENOMEM);
85 CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %lld, offset %u\n",
86 len, portal, xid, offset);
88 rc = LNetPut(self, *mdh, ack,
89 peer_id, portal, xid, offset, 0);
90 if (unlikely(rc != 0)) {
92 /* We're going to get an UNLINK event when I unlink below,
93 * which will complete just like any other failed send, so
94 * I fall through and return success here! */
95 CERROR("LNetPut(%s, %d, %lld) failed: %d\n",
96 libcfs_id2str(peer_id), portal, xid, rc);
97 rc2 = LNetMDUnlink(*mdh);
98 LASSERTF(rc2 == 0, "rc2 = %d\n", rc2);
104 static void mdunlink_iterate_helper(struct lnet_handle_md *bd_mds, int count)
108 for (i = 0; i < count; i++)
109 LNetMDUnlink(bd_mds[i]);
112 #ifdef HAVE_SERVER_SUPPORT
114 * Prepare bulk descriptor for specified incoming request \a req that
115 * can fit \a nfrags * pages. \a type is bulk type. \a portal is where
116 * the bulk to be sent. Used on server-side after request was already
118 * Returns pointer to newly allocatrd initialized bulk descriptor or NULL on
121 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
122 unsigned nfrags, unsigned max_brw,
125 const struct ptlrpc_bulk_frag_ops
128 struct obd_export *exp = req->rq_export;
129 struct ptlrpc_bulk_desc *desc;
132 LASSERT(ptlrpc_is_bulk_op_active(type));
134 desc = ptlrpc_new_bulk(nfrags, max_brw, type, portal, ops);
138 desc->bd_export = class_export_get(exp);
141 desc->bd_cbid.cbid_fn = server_bulk_callback;
142 desc->bd_cbid.cbid_arg = desc;
144 /* NB we don't assign rq_bulk here; server-side requests are
145 * re-used, and the handler frees the bulk desc explicitly. */
149 EXPORT_SYMBOL(ptlrpc_prep_bulk_exp);
152 * Starts bulk transfer for descriptor \a desc on the server.
153 * Returns 0 on success or error code.
155 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc)
157 struct obd_export *exp = desc->bd_export;
159 struct lnet_process_id peer_id;
167 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_PUT_NET))
170 /* NB no locking required until desc is on the network */
171 LASSERT(desc->bd_md_count == 0);
172 LASSERT(ptlrpc_is_bulk_op_active(desc->bd_type));
174 LASSERT(desc->bd_cbid.cbid_fn == server_bulk_callback);
175 LASSERT(desc->bd_cbid.cbid_arg == desc);
178 * Multi-Rail: get the preferred self and peer NIDs from the
179 * request, so they are based on the route taken by the
182 self_nid = desc->bd_req->rq_self;
183 peer_id = desc->bd_req->rq_source;
185 /* NB total length may be 0 for a read past EOF, so we send 0
186 * length bulks, since the client expects bulk events.
188 * The client may not need all of the bulk mbits for the RPC. The RPC
189 * used the mbits of the highest bulk mbits needed, and the server masks
190 * off high bits to get bulk count for this RPC. LU-1431 */
191 mbits = desc->bd_req->rq_mbits & ~((__u64)desc->bd_md_max_brw - 1);
192 total_md = desc->bd_req->rq_mbits - mbits + 1;
194 desc->bd_md_count = total_md;
195 desc->bd_failure = 0;
197 md.user_ptr = &desc->bd_cbid;
198 md.eq_handle = ptlrpc_eq_h;
199 md.threshold = 2; /* SENT and ACK/REPLY */
201 for (posted_md = 0; posted_md < total_md; mbits++) {
202 md.options = PTLRPC_MD_OPTIONS;
204 /* NB it's assumed that source and sink buffer frags are
205 * page-aligned. Otherwise we'd have to send client bulk
206 * sizes over and split server buffer accordingly */
207 ptlrpc_fill_bulk_md(&md, desc, posted_md);
208 rc = LNetMDBind(md, LNET_UNLINK, &desc->bd_mds[posted_md]);
210 CERROR("%s: LNetMDBind failed for MD %u: rc = %d\n",
211 exp->exp_obd->obd_name, posted_md, rc);
212 LASSERT(rc == -ENOMEM);
213 if (posted_md == 0) {
214 desc->bd_md_count = 0;
220 /* sanity.sh 224c: lets skip last md */
221 if (posted_md == desc->bd_md_max_brw - 1)
222 OBD_FAIL_CHECK_RESET(OBD_FAIL_PTLRPC_CLIENT_BULK_CB3,
223 CFS_FAIL_PTLRPC_OST_BULK_CB2);
225 /* Network is about to get at the memory */
226 if (ptlrpc_is_bulk_put_source(desc->bd_type))
227 rc = LNetPut(self_nid, desc->bd_mds[posted_md],
228 LNET_ACK_REQ, peer_id,
229 desc->bd_portal, mbits, 0, 0);
231 rc = LNetGet(self_nid, desc->bd_mds[posted_md],
232 peer_id, desc->bd_portal, mbits, 0, false);
236 CERROR("%s: failed bulk transfer with %s:%u x%llu: "
237 "rc = %d\n", exp->exp_obd->obd_name,
238 libcfs_id2str(peer_id), desc->bd_portal,
245 /* Can't send, so we unlink the MD bound above. The UNLINK
246 * event this creates will signal completion with failure,
247 * so we return SUCCESS here! */
248 spin_lock(&desc->bd_lock);
249 desc->bd_md_count -= total_md - posted_md;
250 spin_unlock(&desc->bd_lock);
251 LASSERT(desc->bd_md_count >= 0);
253 mdunlink_iterate_helper(desc->bd_mds, posted_md);
257 CDEBUG(D_NET, "Transferring %u pages %u bytes via portal %d "
258 "id %s mbits %#llx-%#llx\n", desc->bd_iov_count,
259 desc->bd_nob, desc->bd_portal, libcfs_id2str(peer_id),
260 mbits - posted_md, mbits - 1);
266 * Server side bulk abort. Idempotent. Not thread-safe (i.e. only
267 * serialises with completion callback)
269 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc)
271 struct l_wait_info lwi;
274 LASSERT(!in_interrupt()); /* might sleep */
276 if (!ptlrpc_server_bulk_active(desc)) /* completed or */
277 return; /* never started */
279 /* We used to poison the pages with 0xab here because we did not want to
280 * send any meaningful data over the wire for evicted clients (bug 9297)
281 * However, this is no longer safe now that we use the page cache on the
284 /* The unlink ensures the callback happens ASAP and is the last
285 * one. If it fails, it must be because completion just happened,
286 * but we must still l_wait_event() in this case, to give liblustre
287 * a chance to run server_bulk_callback()*/
288 mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_max_brw);
291 /* Network access will complete in finite time but the HUGE
292 * timeout lets us CWARN for visibility of sluggish NALs */
293 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
294 cfs_time_seconds(1), NULL, NULL);
295 rc = l_wait_event(desc->bd_waitq,
296 !ptlrpc_server_bulk_active(desc), &lwi);
300 LASSERT(rc == -ETIMEDOUT);
301 CWARN("Unexpectedly long timeout: desc %p\n", desc);
304 #endif /* HAVE_SERVER_SUPPORT */
307 * Register bulk at the sender for later transfer.
308 * Returns 0 on success or error code.
310 int ptlrpc_register_bulk(struct ptlrpc_request *req)
312 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
313 struct lnet_process_id peer;
319 struct lnet_handle_me me_h;
323 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_GET_NET))
326 /* NB no locking required until desc is on the network */
327 LASSERT(desc->bd_nob > 0);
328 LASSERT(desc->bd_md_count == 0);
329 LASSERT(desc->bd_md_max_brw <= PTLRPC_BULK_OPS_COUNT);
330 LASSERT(desc->bd_iov_count <= PTLRPC_MAX_BRW_PAGES);
331 LASSERT(desc->bd_req != NULL);
332 LASSERT(ptlrpc_is_bulk_op_passive(desc->bd_type));
334 /* cleanup the state of the bulk for it will be reused */
335 if (req->rq_resend || req->rq_send_state == LUSTRE_IMP_REPLAY)
336 desc->bd_nob_transferred = 0;
337 else if (desc->bd_nob_transferred != 0)
338 /* If the network failed after an RPC was sent, this condition
339 * could happen. Rather than assert (was here before), return
343 desc->bd_failure = 0;
345 peer = desc->bd_import->imp_connection->c_peer;
347 LASSERT(desc->bd_cbid.cbid_fn == client_bulk_callback);
348 LASSERT(desc->bd_cbid.cbid_arg == desc);
350 total_md = (desc->bd_iov_count + LNET_MAX_IOV - 1) / LNET_MAX_IOV;
351 /* rq_mbits is matchbits of the final bulk */
352 mbits = req->rq_mbits - total_md + 1;
354 LASSERTF(mbits == (req->rq_mbits & PTLRPC_BULK_OPS_MASK),
355 "first mbits = x%llu, last mbits = x%llu\n",
356 mbits, req->rq_mbits);
357 LASSERTF(!(desc->bd_registered &&
358 req->rq_send_state != LUSTRE_IMP_REPLAY) ||
359 mbits != desc->bd_last_mbits,
360 "registered: %d rq_mbits: %llu bd_last_mbits: %llu\n",
361 desc->bd_registered, mbits, desc->bd_last_mbits);
363 desc->bd_registered = 1;
364 desc->bd_last_mbits = mbits;
365 desc->bd_md_count = total_md;
366 md.user_ptr = &desc->bd_cbid;
367 md.eq_handle = ptlrpc_eq_h;
368 md.threshold = 1; /* PUT or GET */
370 for (posted_md = 0; posted_md < total_md; posted_md++, mbits++) {
371 md.options = PTLRPC_MD_OPTIONS |
372 (ptlrpc_is_bulk_op_get(desc->bd_type) ?
373 LNET_MD_OP_GET : LNET_MD_OP_PUT);
374 ptlrpc_fill_bulk_md(&md, desc, posted_md);
376 if (posted_md > 0 && posted_md + 1 == total_md &&
377 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_ATTACH)) {
380 rc = LNetMEAttach(desc->bd_portal, peer, mbits, 0,
381 LNET_UNLINK, LNET_INS_AFTER, &me_h);
384 CERROR("%s: LNetMEAttach failed x%llu/%d: rc = %d\n",
385 desc->bd_import->imp_obd->obd_name, mbits,
390 /* About to let the network at it... */
391 rc = LNetMDAttach(me_h, md, LNET_UNLINK,
392 &desc->bd_mds[posted_md]);
394 CERROR("%s: LNetMDAttach failed x%llu/%d: rc = %d\n",
395 desc->bd_import->imp_obd->obd_name, mbits,
397 rc2 = LNetMEUnlink(me_h);
404 LASSERT(rc == -ENOMEM);
405 spin_lock(&desc->bd_lock);
406 desc->bd_md_count -= total_md - posted_md;
407 spin_unlock(&desc->bd_lock);
408 LASSERT(desc->bd_md_count >= 0);
409 mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_max_brw);
410 req->rq_status = -ENOMEM;
411 desc->bd_registered = 0;
415 spin_lock(&desc->bd_lock);
416 /* Holler if peer manages to touch buffers before he knows the mbits */
417 if (desc->bd_md_count != total_md)
418 CWARN("%s: Peer %s touched %d buffers while I registered\n",
419 desc->bd_import->imp_obd->obd_name, libcfs_id2str(peer),
420 total_md - desc->bd_md_count);
421 spin_unlock(&desc->bd_lock);
423 CDEBUG(D_NET, "Setup %u bulk %s buffers: %u pages %u bytes, "
424 "mbits x%#llx-%#llx, portal %u\n", desc->bd_md_count,
425 ptlrpc_is_bulk_op_get(desc->bd_type) ? "get-source" : "put-sink",
426 desc->bd_iov_count, desc->bd_nob,
427 desc->bd_last_mbits, req->rq_mbits, desc->bd_portal);
433 * Disconnect a bulk desc from the network. Idempotent. Not
434 * thread-safe (i.e. only interlocks with completion callback).
435 * Returns 1 on success or 0 if network unregistration failed for whatever
438 int ptlrpc_unregister_bulk(struct ptlrpc_request *req, int async)
440 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
441 struct l_wait_info lwi;
445 LASSERT(!in_interrupt()); /* might sleep */
447 /* Let's setup deadline for reply unlink. */
448 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK) &&
449 async && req->rq_bulk_deadline == 0 && cfs_fail_val == 0)
450 req->rq_bulk_deadline = ktime_get_real_seconds() + LONG_UNLINK;
452 if (ptlrpc_client_bulk_active(req) == 0) /* completed or */
453 RETURN(1); /* never registered */
455 LASSERT(desc->bd_req == req); /* bd_req NULL until registered */
457 /* the unlink ensures the callback happens ASAP and is the last
458 * one. If it fails, it must be because completion just happened,
459 * but we must still l_wait_event() in this case to give liblustre
460 * a chance to run client_bulk_callback() */
461 mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_max_brw);
463 if (ptlrpc_client_bulk_active(req) == 0) /* completed or */
464 RETURN(1); /* never registered */
466 /* Move to "Unregistering" phase as bulk was not unlinked yet. */
467 ptlrpc_rqphase_move(req, RQ_PHASE_UNREG_BULK);
469 /* Do not wait for unlink to finish. */
474 /* The wq argument is ignored by user-space wait_event macros */
475 wait_queue_head_t *wq = (req->rq_set != NULL) ?
476 &req->rq_set->set_waitq :
477 &req->rq_reply_waitq;
479 * Network access will complete in finite time but the HUGE
480 * timeout lets us CWARN for visibility of sluggish NALs.
482 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
483 cfs_time_seconds(1), NULL, NULL);
484 rc = l_wait_event(*wq, !ptlrpc_client_bulk_active(req), &lwi);
486 ptlrpc_rqphase_move(req, req->rq_next_phase);
490 LASSERT(rc == -ETIMEDOUT);
491 DEBUG_REQ(D_WARNING, req, "Unexpectedly long timeout: desc %p",
497 static void ptlrpc_at_set_reply(struct ptlrpc_request *req, int flags)
499 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
500 struct ptlrpc_service *svc = svcpt->scp_service;
501 int service_time = max_t(int, ktime_get_real_seconds() -
502 req->rq_arrival_time.tv_sec, 1);
504 if (!(flags & PTLRPC_REPLY_EARLY) &&
505 (req->rq_type != PTL_RPC_MSG_ERR) &&
506 (req->rq_reqmsg != NULL) &&
507 !(lustre_msg_get_flags(req->rq_reqmsg) &
508 (MSG_RESENT | MSG_REPLAY |
509 MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE))) {
510 /* early replies, errors and recovery requests don't count
511 * toward our service time estimate */
512 int oldse = at_measured(&svcpt->scp_at_estimate, service_time);
515 DEBUG_REQ(D_ADAPTTO, req,
516 "svc %s changed estimate from %d to %d",
517 svc->srv_name, oldse,
518 at_get(&svcpt->scp_at_estimate));
521 /* Report actual service time for client latency calc */
522 lustre_msg_set_service_time(req->rq_repmsg, service_time);
523 /* Report service time estimate for future client reqs, but report 0
524 * (to be ignored by client) if it's an error reply during recovery.
527 if (req->rq_type == PTL_RPC_MSG_ERR &&
528 (req->rq_export == NULL ||
529 req->rq_export->exp_obd->obd_recovering)) {
530 lustre_msg_set_timeout(req->rq_repmsg, 0);
534 if (req->rq_export && req->rq_reqmsg != NULL &&
535 (flags & PTLRPC_REPLY_EARLY) &&
536 lustre_msg_get_flags(req->rq_reqmsg) &
537 (MSG_REPLAY | MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE)) {
538 struct obd_device *exp_obd = req->rq_export->exp_obd;
540 timeout = ktime_get_real_seconds() -
541 req->rq_arrival_time.tv_sec +
542 min_t(time64_t, at_extra,
543 exp_obd->obd_recovery_timeout / 4);
545 timeout = at_get(&svcpt->scp_at_estimate);
547 lustre_msg_set_timeout(req->rq_repmsg, timeout);
550 if (req->rq_reqmsg &&
551 !(lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
552 CDEBUG(D_ADAPTTO, "No early reply support: flags=%#x "
553 "req_flags=%#x magic=%x/%x len=%d\n",
554 flags, lustre_msg_get_flags(req->rq_reqmsg),
555 lustre_msg_get_magic(req->rq_reqmsg),
556 lustre_msg_get_magic(req->rq_repmsg), req->rq_replen);
561 * Send request reply from request \a req reply buffer.
562 * \a flags defines reply types
563 * Returns 0 on success or error code
565 int ptlrpc_send_reply(struct ptlrpc_request *req, int flags)
567 struct ptlrpc_reply_state *rs = req->rq_reply_state;
568 struct ptlrpc_connection *conn;
571 /* We must already have a reply buffer (only ptlrpc_error() may be
572 * called without one). The reply generated by sptlrpc layer (e.g.
573 * error notify, etc.) might have NULL rq->reqmsg; Otherwise we must
574 * have a request buffer which is either the actual (swabbed) incoming
575 * request, or a saved copy if this is a req saved in
576 * target_queue_final_reply().
578 LASSERT (req->rq_no_reply == 0);
579 LASSERT (req->rq_reqbuf != NULL);
580 LASSERT (rs != NULL);
581 LASSERT ((flags & PTLRPC_REPLY_MAYBE_DIFFICULT) || !rs->rs_difficult);
582 LASSERT (req->rq_repmsg != NULL);
583 LASSERT (req->rq_repmsg == rs->rs_msg);
584 LASSERT (rs->rs_cb_id.cbid_fn == reply_out_callback);
585 LASSERT (rs->rs_cb_id.cbid_arg == rs);
587 /* There may be no rq_export during failover */
589 if (unlikely(req->rq_export && req->rq_export->exp_obd &&
590 req->rq_export->exp_obd->obd_fail)) {
591 /* Failed obd's only send ENODEV */
592 req->rq_type = PTL_RPC_MSG_ERR;
593 req->rq_status = -ENODEV;
594 CDEBUG(D_HA, "sending ENODEV from failed obd %d\n",
595 req->rq_export->exp_obd->obd_minor);
598 if (req->rq_type != PTL_RPC_MSG_ERR)
599 req->rq_type = PTL_RPC_MSG_REPLY;
601 lustre_msg_set_type(req->rq_repmsg, req->rq_type);
602 lustre_msg_set_status(req->rq_repmsg,
603 ptlrpc_status_hton(req->rq_status));
604 lustre_msg_set_opc(req->rq_repmsg,
605 req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : 0);
607 target_pack_pool_reply(req);
609 ptlrpc_at_set_reply(req, flags);
611 if (req->rq_export == NULL || req->rq_export->exp_connection == NULL)
612 conn = ptlrpc_connection_get(req->rq_peer, req->rq_self, NULL);
614 conn = ptlrpc_connection_addref(req->rq_export->exp_connection);
616 if (unlikely(conn == NULL)) {
617 CERROR("not replying on NULL connection\n"); /* bug 9635 */
620 ptlrpc_rs_addref(rs); /* +1 ref for the network */
622 rc = sptlrpc_svc_wrap_reply(req);
626 req->rq_sent = ktime_get_real_seconds();
628 rc = ptl_send_buf(&rs->rs_md_h, rs->rs_repbuf, rs->rs_repdata_len,
629 (rs->rs_difficult && !rs->rs_no_ack) ?
630 LNET_ACK_REQ : LNET_NOACK_REQ,
631 &rs->rs_cb_id, req->rq_self, req->rq_source,
632 ptlrpc_req2svc(req)->srv_rep_portal,
633 req->rq_xid, req->rq_reply_off, NULL);
635 if (unlikely(rc != 0))
636 ptlrpc_req_drop_rs(req);
637 ptlrpc_connection_put(conn);
641 int ptlrpc_reply (struct ptlrpc_request *req)
643 if (req->rq_no_reply)
646 return (ptlrpc_send_reply(req, 0));
650 * For request \a req send an error reply back. Create empty
651 * reply buffers if necessary.
653 int ptlrpc_send_error(struct ptlrpc_request *req, int may_be_difficult)
658 if (req->rq_no_reply)
661 if (!req->rq_repmsg) {
662 rc = lustre_pack_reply(req, 1, NULL, NULL);
667 if (req->rq_status != -ENOSPC && req->rq_status != -EACCES &&
668 req->rq_status != -EPERM && req->rq_status != -ENOENT &&
669 req->rq_status != -EINPROGRESS && req->rq_status != -EDQUOT)
670 req->rq_type = PTL_RPC_MSG_ERR;
672 rc = ptlrpc_send_reply(req, may_be_difficult);
676 int ptlrpc_error(struct ptlrpc_request *req)
678 return ptlrpc_send_error(req, 0);
682 * Send request \a request.
683 * if \a noreply is set, don't expect any reply back and don't set up
685 * Returns 0 on success or error code.
687 int ptl_send_rpc(struct ptlrpc_request *request, int noreply)
692 struct lnet_handle_md bulk_cookie;
693 struct ptlrpc_connection *connection;
694 struct lnet_handle_me reply_me_h;
695 struct lnet_md reply_md;
696 struct obd_import *imp = request->rq_import;
697 struct obd_device *obd = imp->imp_obd;
700 LNetInvalidateMDHandle(&bulk_cookie);
702 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_RPC))
705 LASSERT(request->rq_type == PTL_RPC_MSG_REQUEST);
706 LASSERT(request->rq_wait_ctx == 0);
708 /* If this is a re-transmit, we're required to have disengaged
709 * cleanly from the previous attempt */
710 LASSERT(!request->rq_receiving_reply);
711 LASSERT(!((lustre_msg_get_flags(request->rq_reqmsg) & MSG_REPLAY) &&
712 (imp->imp_state == LUSTRE_IMP_FULL)));
714 if (unlikely(obd != NULL && obd->obd_fail)) {
715 CDEBUG(D_HA, "muting rpc for failed imp obd %s\n",
717 /* this prevents us from waiting in ptlrpc_queue_wait */
718 spin_lock(&request->rq_lock);
720 spin_unlock(&request->rq_lock);
721 request->rq_status = -ENODEV;
725 connection = imp->imp_connection;
727 lustre_msg_set_handle(request->rq_reqmsg,
728 &imp->imp_remote_handle);
729 lustre_msg_set_type(request->rq_reqmsg, PTL_RPC_MSG_REQUEST);
730 lustre_msg_set_conn_cnt(request->rq_reqmsg,
732 lustre_msghdr_set_flags(request->rq_reqmsg,
733 imp->imp_msghdr_flags);
735 /* If it's the first time to resend the request for EINPROGRESS,
736 * we need to allocate a new XID (see after_reply()), it's different
737 * from the resend for reply timeout. */
738 if (request->rq_nr_resend != 0 &&
739 list_empty(&request->rq_unreplied_list)) {
741 /* resend for EINPROGRESS, allocate new xid to avoid reply
743 spin_lock(&imp->imp_lock);
744 ptlrpc_assign_next_xid_nolock(request);
745 min_xid = ptlrpc_known_replied_xid(imp);
746 spin_unlock(&imp->imp_lock);
748 lustre_msg_set_last_xid(request->rq_reqmsg, min_xid);
749 DEBUG_REQ(D_RPCTRACE, request,
750 "Allocating new XID for resend on EINPROGRESS");
753 if (request->rq_bulk != NULL) {
754 ptlrpc_set_bulk_mbits(request);
755 lustre_msg_set_mbits(request->rq_reqmsg, request->rq_mbits);
758 if (list_empty(&request->rq_unreplied_list) ||
759 request->rq_xid <= imp->imp_known_replied_xid) {
760 DEBUG_REQ(D_ERROR, request,
761 "xid=%llu, replied=%llu, list_empty=%d",
762 request->rq_xid, imp->imp_known_replied_xid,
763 list_empty(&request->rq_unreplied_list));
767 /** For enabled AT all request should have AT_SUPPORT in the
768 * FULL import state when OBD_CONNECT_AT is set */
769 LASSERT(AT_OFF || imp->imp_state != LUSTRE_IMP_FULL ||
770 (imp->imp_msghdr_flags & MSGHDR_AT_SUPPORT) ||
771 !(imp->imp_connect_data.ocd_connect_flags &
774 if (request->rq_resend) {
775 lustre_msg_add_flags(request->rq_reqmsg, MSG_RESENT);
776 if (request->rq_resend_cb != NULL)
777 request->rq_resend_cb(request, &request->rq_async_args);
779 if (request->rq_memalloc)
780 mpflag = cfs_memory_pressure_get_and_set();
782 rc = sptlrpc_cli_wrap_request(request);
786 /* bulk register should be done after wrap_request() */
787 if (request->rq_bulk != NULL) {
788 rc = ptlrpc_register_bulk (request);
790 GOTO(cleanup_bulk, rc);
792 * All the mds in the request will have the same cpt
793 * encoded in the cookie. So we can just get the first
796 bulk_cookie = request->rq_bulk->bd_mds[0];
800 LASSERT (request->rq_replen != 0);
801 if (request->rq_repbuf == NULL) {
802 LASSERT(request->rq_repdata == NULL);
803 LASSERT(request->rq_repmsg == NULL);
804 rc = sptlrpc_cli_alloc_repbuf(request,
807 /* this prevents us from looping in
808 * ptlrpc_queue_wait */
809 spin_lock(&request->rq_lock);
811 spin_unlock(&request->rq_lock);
812 request->rq_status = rc;
813 GOTO(cleanup_bulk, rc);
816 request->rq_repdata = NULL;
817 request->rq_repmsg = NULL;
820 rc = LNetMEAttach(request->rq_reply_portal,/*XXX FIXME bug 249*/
821 connection->c_peer, request->rq_xid, 0,
822 LNET_UNLINK, LNET_INS_AFTER, &reply_me_h);
824 CERROR("LNetMEAttach failed: %d\n", rc);
825 LASSERT (rc == -ENOMEM);
826 GOTO(cleanup_bulk, rc = -ENOMEM);
830 spin_lock(&request->rq_lock);
831 /* We are responsible for unlinking the reply buffer */
832 request->rq_reply_unlinked = noreply;
833 request->rq_receiving_reply = !noreply;
834 /* Clear any flags that may be present from previous sends. */
835 request->rq_req_unlinked = 0;
836 request->rq_replied = 0;
838 request->rq_timedout = 0;
839 request->rq_net_err = 0;
840 request->rq_resend = 0;
841 request->rq_restart = 0;
842 request->rq_reply_truncated = 0;
843 spin_unlock(&request->rq_lock);
846 reply_md.start = request->rq_repbuf;
847 reply_md.length = request->rq_repbuf_len;
848 /* Allow multiple early replies */
849 reply_md.threshold = LNET_MD_THRESH_INF;
850 /* Manage remote for early replies */
851 reply_md.options = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT |
852 LNET_MD_MANAGE_REMOTE |
853 LNET_MD_TRUNCATE; /* allow to make EOVERFLOW error */;
854 reply_md.user_ptr = &request->rq_reply_cbid;
855 reply_md.eq_handle = ptlrpc_eq_h;
857 /* We must see the unlink callback to set rq_reply_unlinked,
858 * so we can't auto-unlink */
859 rc = LNetMDAttach(reply_me_h, reply_md, LNET_RETAIN,
860 &request->rq_reply_md_h);
862 CERROR("LNetMDAttach failed: %d\n", rc);
863 LASSERT (rc == -ENOMEM);
864 spin_lock(&request->rq_lock);
865 /* ...but the MD attach didn't succeed... */
866 request->rq_receiving_reply = 0;
867 spin_unlock(&request->rq_lock);
868 GOTO(cleanup_me, rc = -ENOMEM);
871 CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid %llu"
873 request->rq_repbuf_len, request->rq_xid,
874 request->rq_reply_portal);
877 /* add references on request for request_out_callback */
878 ptlrpc_request_addref(request);
879 if (obd != NULL && obd->obd_svc_stats != NULL)
880 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQACTIVE_CNTR,
881 atomic_read(&imp->imp_inflight));
883 OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_SEND, request->rq_timeout + 5);
885 request->rq_sent_ns = ktime_get_real();
886 request->rq_sent = ktime_get_real_seconds();
887 /* We give the server rq_timeout secs to process the req, and
888 add the network latency for our local timeout. */
889 request->rq_deadline = request->rq_sent + request->rq_timeout +
890 ptlrpc_at_get_net_latency(request);
892 ptlrpc_pinger_sending_on_import(imp);
894 DEBUG_REQ(D_INFO, request, "send flags=%x",
895 lustre_msg_get_flags(request->rq_reqmsg));
896 rc = ptl_send_buf(&request->rq_req_md_h,
897 request->rq_reqbuf, request->rq_reqdata_len,
898 LNET_NOACK_REQ, &request->rq_req_cbid,
899 LNET_NID_ANY, connection->c_peer,
900 request->rq_request_portal,
901 request->rq_xid, 0, &bulk_cookie);
905 request->rq_req_unlinked = 1;
906 ptlrpc_req_finished(request);
911 /* MEUnlink is safe; the PUT didn't even get off the ground, and
912 * nobody apart from the PUT's target has the right nid+XID to
913 * access the reply buffer. */
914 rc2 = LNetMEUnlink(reply_me_h);
916 /* UNLINKED callback called synchronously */
917 LASSERT(!request->rq_receiving_reply);
920 /* We do sync unlink here as there was no real transfer here so
921 * the chance to have long unlink to sluggish net is smaller here. */
922 ptlrpc_unregister_bulk(request, 0);
923 if (request->rq_bulk != NULL)
924 request->rq_bulk->bd_registered = 0;
927 /* set rq_sent so that this request is treated
928 * as a delayed send in the upper layers */
929 request->rq_sent = ktime_get_real_seconds();
932 if (request->rq_memalloc)
933 cfs_memory_pressure_restore(mpflag);
937 EXPORT_SYMBOL(ptl_send_rpc);
940 * Register request buffer descriptor for request receiving.
942 int ptlrpc_register_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
944 struct ptlrpc_service *service = rqbd->rqbd_svcpt->scp_service;
945 static struct lnet_process_id match_id = {
951 struct lnet_handle_me me_h;
953 CDEBUG(D_NET, "LNetMEAttach: portal %d\n",
954 service->srv_req_portal);
956 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_RQBD))
959 /* NB: CPT affinity service should use new LNet flag LNET_INS_LOCAL,
960 * which means buffer can only be attached on local CPT, and LND
961 * threads can find it by grabbing a local lock */
962 rc = LNetMEAttach(service->srv_req_portal,
963 match_id, 0, ~0, LNET_UNLINK,
964 rqbd->rqbd_svcpt->scp_cpt >= 0 ?
965 LNET_INS_LOCAL : LNET_INS_AFTER, &me_h);
967 CERROR("LNetMEAttach failed: %d\n", rc);
971 LASSERT(rqbd->rqbd_refcount == 0);
972 rqbd->rqbd_refcount = 1;
974 md.start = rqbd->rqbd_buffer;
975 md.length = service->srv_buf_size;
976 md.max_size = service->srv_max_req_size;
977 md.threshold = LNET_MD_THRESH_INF;
978 md.options = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT | LNET_MD_MAX_SIZE;
979 md.user_ptr = &rqbd->rqbd_cbid;
980 md.eq_handle = ptlrpc_eq_h;
982 rc = LNetMDAttach(me_h, md, LNET_UNLINK, &rqbd->rqbd_md_h);
986 CERROR("LNetMDAttach failed: %d; \n", rc);
987 LASSERT (rc == -ENOMEM);
988 rc = LNetMEUnlink (me_h);
990 rqbd->rqbd_refcount = 0;