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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_RPC
38 #include <obd_support.h>
39 #include <lustre_net.h>
40 #include <lustre_lib.h>
42 #include <obd_class.h>
43 #include "ptlrpc_internal.h"
46 * Helper function. Sends \a len bytes from \a base at offset \a offset
47 * over \a conn connection to portal \a portal.
48 * Returns 0 on success or error code.
50 static int ptl_send_buf (lnet_handle_md_t *mdh, void *base, int len,
51 lnet_ack_req_t ack, struct ptlrpc_cb_id *cbid,
52 struct ptlrpc_connection *conn, int portal, __u64 xid,
59 LASSERT (portal != 0);
60 LASSERT (conn != NULL);
61 CDEBUG (D_INFO, "conn=%p id %s\n", conn, libcfs_id2str(conn->c_peer));
64 md.threshold = (ack == LNET_ACK_REQ) ? 2 : 1;
65 md.options = PTLRPC_MD_OPTIONS;
67 md.eq_handle = ptlrpc_eq_h;
69 if (unlikely(ack == LNET_ACK_REQ &&
70 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_ACK, OBD_FAIL_ONCE))){
71 /* don't ask for the ack to simulate failing client */
75 rc = LNetMDBind (md, LNET_UNLINK, mdh);
76 if (unlikely(rc != 0)) {
77 CERROR ("LNetMDBind failed: %d\n", rc);
78 LASSERT (rc == -ENOMEM);
82 CDEBUG(D_NET, "Sending %d bytes to portal %d, xid "LPD64", offset %u\n",
83 len, portal, xid, offset);
85 rc = LNetPut (conn->c_self, *mdh, ack,
86 conn->c_peer, portal, xid, offset, 0);
87 if (unlikely(rc != 0)) {
89 /* We're going to get an UNLINK event when I unlink below,
90 * which will complete just like any other failed send, so
91 * I fall through and return success here! */
92 CERROR("LNetPut(%s, %d, "LPD64") failed: %d\n",
93 libcfs_id2str(conn->c_peer), portal, xid, rc);
94 rc2 = LNetMDUnlink(*mdh);
95 LASSERTF(rc2 == 0, "rc2 = %d\n", rc2);
101 static void mdunlink_iterate_helper(lnet_handle_md_t *bd_mds, int count)
105 for (i = 0; i < count; i++)
106 LNetMDUnlink(bd_mds[i]);
109 #ifdef HAVE_SERVER_SUPPORT
111 * Prepare bulk descriptor for specified incoming request \a req that
112 * can fit \a npages * pages. \a type is bulk type. \a portal is where
113 * the bulk to be sent. Used on server-side after request was already
115 * Returns pointer to newly allocatrd initialized bulk descriptor or NULL on
118 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
119 unsigned npages, unsigned max_brw,
120 unsigned type, unsigned portal)
122 struct obd_export *exp = req->rq_export;
123 struct ptlrpc_bulk_desc *desc;
126 LASSERT(type == BULK_PUT_SOURCE || type == BULK_GET_SINK);
128 desc = ptlrpc_new_bulk(npages, max_brw, type, portal);
132 desc->bd_export = class_export_get(exp);
135 desc->bd_cbid.cbid_fn = server_bulk_callback;
136 desc->bd_cbid.cbid_arg = desc;
138 /* NB we don't assign rq_bulk here; server-side requests are
139 * re-used, and the handler frees the bulk desc explicitly. */
143 EXPORT_SYMBOL(ptlrpc_prep_bulk_exp);
146 * Starts bulk transfer for descriptor \a desc on the server.
147 * Returns 0 on success or error code.
149 int ptlrpc_start_bulk_transfer(struct ptlrpc_bulk_desc *desc)
151 struct obd_export *exp = desc->bd_export;
152 struct ptlrpc_connection *conn = exp->exp_connection;
160 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_PUT_NET))
163 /* NB no locking required until desc is on the network */
164 LASSERT(desc->bd_md_count == 0);
165 LASSERT(desc->bd_type == BULK_PUT_SOURCE ||
166 desc->bd_type == BULK_GET_SINK);
168 LASSERT(desc->bd_cbid.cbid_fn == server_bulk_callback);
169 LASSERT(desc->bd_cbid.cbid_arg == desc);
171 /* NB total length may be 0 for a read past EOF, so we send 0
172 * length bulks, since the client expects bulk events.
174 * The client may not need all of the bulk XIDs for the RPC. The RPC
175 * used the XID of the highest bulk XID needed, and the server masks
176 * off high bits to get bulk count for this RPC. LU-1431 */
177 xid = desc->bd_req->rq_xid & ~((__u64)desc->bd_md_max_brw - 1);
178 total_md = desc->bd_req->rq_xid - xid + 1;
180 desc->bd_md_count = total_md;
181 desc->bd_failure = 0;
183 md.user_ptr = &desc->bd_cbid;
184 md.eq_handle = ptlrpc_eq_h;
185 md.threshold = 2; /* SENT and ACK/REPLY */
187 for (posted_md = 0; posted_md < total_md; xid++) {
188 md.options = PTLRPC_MD_OPTIONS;
190 /* NB it's assumed that source and sink buffer frags are
191 * page-aligned. Otherwise we'd have to send client bulk
192 * sizes over and split server buffer accordingly */
193 ptlrpc_fill_bulk_md(&md, desc, posted_md);
194 rc = LNetMDBind(md, LNET_UNLINK, &desc->bd_mds[posted_md]);
196 CERROR("%s: LNetMDBind failed for MD %u: rc = %d\n",
197 exp->exp_obd->obd_name, posted_md, rc);
198 LASSERT(rc == -ENOMEM);
199 if (posted_md == 0) {
200 desc->bd_md_count = 0;
205 /* Network is about to get at the memory */
206 if (desc->bd_type == BULK_PUT_SOURCE)
207 rc = LNetPut(conn->c_self, desc->bd_mds[posted_md],
208 LNET_ACK_REQ, conn->c_peer,
209 desc->bd_portal, xid, 0, 0);
211 rc = LNetGet(conn->c_self, desc->bd_mds[posted_md],
212 conn->c_peer, desc->bd_portal, xid, 0);
216 CERROR("%s: failed bulk transfer with %s:%u x"LPU64": "
217 "rc = %d\n", exp->exp_obd->obd_name,
218 libcfs_id2str(conn->c_peer), desc->bd_portal,
225 /* Can't send, so we unlink the MD bound above. The UNLINK
226 * event this creates will signal completion with failure,
227 * so we return SUCCESS here! */
228 spin_lock(&desc->bd_lock);
229 desc->bd_md_count -= total_md - posted_md;
230 spin_unlock(&desc->bd_lock);
231 LASSERT(desc->bd_md_count >= 0);
233 mdunlink_iterate_helper(desc->bd_mds, posted_md);
237 CDEBUG(D_NET, "Transferring %u pages %u bytes via portal %d "
238 "id %s xid "LPX64"-"LPX64"\n", desc->bd_iov_count,
239 desc->bd_nob, desc->bd_portal, libcfs_id2str(conn->c_peer),
240 xid - posted_md, xid - 1);
244 EXPORT_SYMBOL(ptlrpc_start_bulk_transfer);
247 * Server side bulk abort. Idempotent. Not thread-safe (i.e. only
248 * serialises with completion callback)
250 void ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc)
252 struct l_wait_info lwi;
255 LASSERT(!in_interrupt()); /* might sleep */
257 if (!ptlrpc_server_bulk_active(desc)) /* completed or */
258 return; /* never started */
260 /* We used to poison the pages with 0xab here because we did not want to
261 * send any meaningful data over the wire for evicted clients (bug 9297)
262 * However, this is no longer safe now that we use the page cache on the
265 /* The unlink ensures the callback happens ASAP and is the last
266 * one. If it fails, it must be because completion just happened,
267 * but we must still l_wait_event() in this case, to give liblustre
268 * a chance to run server_bulk_callback()*/
269 mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_count);
272 /* Network access will complete in finite time but the HUGE
273 * timeout lets us CWARN for visibility of sluggish NALs */
274 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
275 cfs_time_seconds(1), NULL, NULL);
276 rc = l_wait_event(desc->bd_waitq,
277 !ptlrpc_server_bulk_active(desc), &lwi);
281 LASSERT(rc == -ETIMEDOUT);
282 CWARN("Unexpectedly long timeout: desc %p\n", desc);
285 EXPORT_SYMBOL(ptlrpc_abort_bulk);
286 #endif /* HAVE_SERVER_SUPPORT */
289 * Register bulk at the sender for later transfer.
290 * Returns 0 on success or error code.
292 int ptlrpc_register_bulk(struct ptlrpc_request *req)
294 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
295 lnet_process_id_t peer;
301 lnet_handle_me_t me_h;
305 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_BULK_GET_NET))
308 /* NB no locking required until desc is on the network */
309 LASSERT(desc->bd_nob > 0);
310 LASSERT(desc->bd_md_count == 0);
311 LASSERT(desc->bd_md_max_brw <= PTLRPC_BULK_OPS_COUNT);
312 LASSERT(desc->bd_iov_count <= PTLRPC_MAX_BRW_PAGES);
313 LASSERT(desc->bd_req != NULL);
314 LASSERT(desc->bd_type == BULK_PUT_SINK ||
315 desc->bd_type == BULK_GET_SOURCE);
317 /* cleanup the state of the bulk for it will be reused */
318 if (req->rq_resend || req->rq_send_state == LUSTRE_IMP_REPLAY)
319 desc->bd_nob_transferred = 0;
321 LASSERT(desc->bd_nob_transferred == 0);
323 desc->bd_failure = 0;
325 peer = desc->bd_import->imp_connection->c_peer;
327 LASSERT(desc->bd_cbid.cbid_fn == client_bulk_callback);
328 LASSERT(desc->bd_cbid.cbid_arg == desc);
330 /* An XID is only used for a single request from the client.
331 * For retried bulk transfers, a new XID will be allocated in
332 * in ptlrpc_check_set() if it needs to be resent, so it is not
333 * using the same RDMA match bits after an error.
335 * For multi-bulk RPCs, rq_xid is the last XID needed for bulks. The
336 * first bulk XID is power-of-two aligned before rq_xid. LU-1431 */
337 xid = req->rq_xid & ~((__u64)desc->bd_md_max_brw - 1);
338 LASSERTF(!(desc->bd_registered &&
339 req->rq_send_state != LUSTRE_IMP_REPLAY) ||
340 xid != desc->bd_last_xid,
341 "registered: %d rq_xid: "LPU64" bd_last_xid: "LPU64"\n",
342 desc->bd_registered, xid, desc->bd_last_xid);
344 total_md = (desc->bd_iov_count + LNET_MAX_IOV - 1) / LNET_MAX_IOV;
345 desc->bd_registered = 1;
346 desc->bd_last_xid = xid;
347 desc->bd_md_count = total_md;
348 md.user_ptr = &desc->bd_cbid;
349 md.eq_handle = ptlrpc_eq_h;
350 md.threshold = 1; /* PUT or GET */
352 for (posted_md = 0; posted_md < total_md; posted_md++, xid++) {
353 md.options = PTLRPC_MD_OPTIONS |
354 ((desc->bd_type == BULK_GET_SOURCE) ?
355 LNET_MD_OP_GET : LNET_MD_OP_PUT);
356 ptlrpc_fill_bulk_md(&md, desc, posted_md);
358 rc = LNetMEAttach(desc->bd_portal, peer, xid, 0,
359 LNET_UNLINK, LNET_INS_AFTER, &me_h);
361 CERROR("%s: LNetMEAttach failed x"LPU64"/%d: rc = %d\n",
362 desc->bd_import->imp_obd->obd_name, xid,
367 /* About to let the network at it... */
368 rc = LNetMDAttach(me_h, md, LNET_UNLINK,
369 &desc->bd_mds[posted_md]);
371 CERROR("%s: LNetMDAttach failed x"LPU64"/%d: rc = %d\n",
372 desc->bd_import->imp_obd->obd_name, xid,
374 rc2 = LNetMEUnlink(me_h);
381 LASSERT(rc == -ENOMEM);
382 spin_lock(&desc->bd_lock);
383 desc->bd_md_count -= total_md - posted_md;
384 spin_unlock(&desc->bd_lock);
385 LASSERT(desc->bd_md_count >= 0);
386 mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_max_brw);
387 req->rq_status = -ENOMEM;
391 /* Set rq_xid to matchbits of the final bulk so that server can
392 * infer the number of bulks that were prepared */
394 LASSERTF(desc->bd_last_xid == (req->rq_xid & PTLRPC_BULK_OPS_MASK),
395 "bd_last_xid = x"LPU64", rq_xid = x"LPU64"\n",
396 desc->bd_last_xid, req->rq_xid);
398 spin_lock(&desc->bd_lock);
399 /* Holler if peer manages to touch buffers before he knows the xid */
400 if (desc->bd_md_count != total_md)
401 CWARN("%s: Peer %s touched %d buffers while I registered\n",
402 desc->bd_import->imp_obd->obd_name, libcfs_id2str(peer),
403 total_md - desc->bd_md_count);
404 spin_unlock(&desc->bd_lock);
406 CDEBUG(D_NET, "Setup %u bulk %s buffers: %u pages %u bytes, "
407 "xid x"LPX64"-"LPX64", portal %u\n", desc->bd_md_count,
408 desc->bd_type == BULK_GET_SOURCE ? "get-source" : "put-sink",
409 desc->bd_iov_count, desc->bd_nob,
410 desc->bd_last_xid, req->rq_xid, desc->bd_portal);
414 EXPORT_SYMBOL(ptlrpc_register_bulk);
417 * Disconnect a bulk desc from the network. Idempotent. Not
418 * thread-safe (i.e. only interlocks with completion callback).
419 * Returns 1 on success or 0 if network unregistration failed for whatever
422 int ptlrpc_unregister_bulk(struct ptlrpc_request *req, int async)
424 struct ptlrpc_bulk_desc *desc = req->rq_bulk;
425 struct l_wait_info lwi;
429 LASSERT(!in_interrupt()); /* might sleep */
431 /* Let's setup deadline for reply unlink. */
432 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK) &&
433 async && req->rq_bulk_deadline == 0)
434 req->rq_bulk_deadline = cfs_time_current_sec() + LONG_UNLINK;
436 if (ptlrpc_client_bulk_active(req) == 0) /* completed or */
437 RETURN(1); /* never registered */
439 LASSERT(desc->bd_req == req); /* bd_req NULL until registered */
441 /* the unlink ensures the callback happens ASAP and is the last
442 * one. If it fails, it must be because completion just happened,
443 * but we must still l_wait_event() in this case to give liblustre
444 * a chance to run client_bulk_callback() */
445 mdunlink_iterate_helper(desc->bd_mds, desc->bd_md_max_brw);
447 if (ptlrpc_client_bulk_active(req) == 0) /* completed or */
448 RETURN(1); /* never registered */
450 /* Move to "Unregistering" phase as bulk was not unlinked yet. */
451 ptlrpc_rqphase_move(req, RQ_PHASE_UNREGISTERING);
453 /* Do not wait for unlink to finish. */
458 /* The wq argument is ignored by user-space wait_event macros */
459 wait_queue_head_t *wq = (req->rq_set != NULL) ?
460 &req->rq_set->set_waitq :
461 &req->rq_reply_waitq;
462 /* Network access will complete in finite time but the HUGE
463 * timeout lets us CWARN for visibility of sluggish NALs */
464 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
465 cfs_time_seconds(1), NULL, NULL);
466 rc = l_wait_event(*wq, !ptlrpc_client_bulk_active(req), &lwi);
468 ptlrpc_rqphase_move(req, req->rq_next_phase);
472 LASSERT(rc == -ETIMEDOUT);
473 DEBUG_REQ(D_WARNING, req, "Unexpectedly long timeout: desc %p",
478 EXPORT_SYMBOL(ptlrpc_unregister_bulk);
480 static void ptlrpc_at_set_reply(struct ptlrpc_request *req, int flags)
482 struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
483 struct ptlrpc_service *svc = svcpt->scp_service;
484 int service_time = max_t(int, cfs_time_current_sec() -
485 req->rq_arrival_time.tv_sec, 1);
487 if (!(flags & PTLRPC_REPLY_EARLY) &&
488 (req->rq_type != PTL_RPC_MSG_ERR) &&
489 (req->rq_reqmsg != NULL) &&
490 !(lustre_msg_get_flags(req->rq_reqmsg) &
491 (MSG_RESENT | MSG_REPLAY |
492 MSG_REQ_REPLAY_DONE | MSG_LOCK_REPLAY_DONE))) {
493 /* early replies, errors and recovery requests don't count
494 * toward our service time estimate */
495 int oldse = at_measured(&svcpt->scp_at_estimate, service_time);
498 DEBUG_REQ(D_ADAPTTO, req,
499 "svc %s changed estimate from %d to %d",
500 svc->srv_name, oldse,
501 at_get(&svcpt->scp_at_estimate));
504 /* Report actual service time for client latency calc */
505 lustre_msg_set_service_time(req->rq_repmsg, service_time);
506 /* Report service time estimate for future client reqs, but report 0
507 * (to be ignored by client) if it's a error reply during recovery.
509 if (req->rq_type == PTL_RPC_MSG_ERR &&
510 (req->rq_export == NULL || req->rq_export->exp_obd->obd_recovering))
511 lustre_msg_set_timeout(req->rq_repmsg, 0);
513 lustre_msg_set_timeout(req->rq_repmsg,
514 at_get(&svcpt->scp_at_estimate));
516 if (req->rq_reqmsg &&
517 !(lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)) {
518 CDEBUG(D_ADAPTTO, "No early reply support: flags=%#x "
519 "req_flags=%#x magic=%d:%x/%x len=%d\n",
520 flags, lustre_msg_get_flags(req->rq_reqmsg),
521 lustre_msg_is_v1(req->rq_reqmsg),
522 lustre_msg_get_magic(req->rq_reqmsg),
523 lustre_msg_get_magic(req->rq_repmsg), req->rq_replen);
528 * Send request reply from request \a req reply buffer.
529 * \a flags defines reply types
530 * Returns 0 on sucess or error code
532 int ptlrpc_send_reply(struct ptlrpc_request *req, int flags)
534 struct ptlrpc_reply_state *rs = req->rq_reply_state;
535 struct ptlrpc_connection *conn;
538 /* We must already have a reply buffer (only ptlrpc_error() may be
539 * called without one). The reply generated by sptlrpc layer (e.g.
540 * error notify, etc.) might have NULL rq->reqmsg; Otherwise we must
541 * have a request buffer which is either the actual (swabbed) incoming
542 * request, or a saved copy if this is a req saved in
543 * target_queue_final_reply().
545 LASSERT (req->rq_no_reply == 0);
546 LASSERT (req->rq_reqbuf != NULL);
547 LASSERT (rs != NULL);
548 LASSERT ((flags & PTLRPC_REPLY_MAYBE_DIFFICULT) || !rs->rs_difficult);
549 LASSERT (req->rq_repmsg != NULL);
550 LASSERT (req->rq_repmsg == rs->rs_msg);
551 LASSERT (rs->rs_cb_id.cbid_fn == reply_out_callback);
552 LASSERT (rs->rs_cb_id.cbid_arg == rs);
554 /* There may be no rq_export during failover */
556 if (unlikely(req->rq_export && req->rq_export->exp_obd &&
557 req->rq_export->exp_obd->obd_fail)) {
558 /* Failed obd's only send ENODEV */
559 req->rq_type = PTL_RPC_MSG_ERR;
560 req->rq_status = -ENODEV;
561 CDEBUG(D_HA, "sending ENODEV from failed obd %d\n",
562 req->rq_export->exp_obd->obd_minor);
565 /* In order to keep interoprability with the client (< 2.3) which
566 * doesn't have pb_jobid in ptlrpc_body, We have to shrink the
567 * ptlrpc_body in reply buffer to ptlrpc_body_v2, otherwise, the
568 * reply buffer on client will be overflow.
570 * XXX Remove this whenver we drop the interoprability with such client.
572 req->rq_replen = lustre_shrink_msg(req->rq_repmsg, 0,
573 sizeof(struct ptlrpc_body_v2), 1);
575 if (req->rq_type != PTL_RPC_MSG_ERR)
576 req->rq_type = PTL_RPC_MSG_REPLY;
578 lustre_msg_set_type(req->rq_repmsg, req->rq_type);
579 lustre_msg_set_status(req->rq_repmsg,
580 ptlrpc_status_hton(req->rq_status));
581 lustre_msg_set_opc(req->rq_repmsg,
582 req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : 0);
584 target_pack_pool_reply(req);
586 ptlrpc_at_set_reply(req, flags);
588 if (req->rq_export == NULL || req->rq_export->exp_connection == NULL)
589 conn = ptlrpc_connection_get(req->rq_peer, req->rq_self, NULL);
591 conn = ptlrpc_connection_addref(req->rq_export->exp_connection);
593 if (unlikely(conn == NULL)) {
594 CERROR("not replying on NULL connection\n"); /* bug 9635 */
597 ptlrpc_rs_addref(rs); /* +1 ref for the network */
599 rc = sptlrpc_svc_wrap_reply(req);
603 req->rq_sent = cfs_time_current_sec();
605 rc = ptl_send_buf (&rs->rs_md_h, rs->rs_repbuf, rs->rs_repdata_len,
606 (rs->rs_difficult && !rs->rs_no_ack) ?
607 LNET_ACK_REQ : LNET_NOACK_REQ,
609 ptlrpc_req2svc(req)->srv_rep_portal,
610 req->rq_xid, req->rq_reply_off);
612 if (unlikely(rc != 0))
613 ptlrpc_req_drop_rs(req);
614 ptlrpc_connection_put(conn);
617 EXPORT_SYMBOL(ptlrpc_send_reply);
619 int ptlrpc_reply (struct ptlrpc_request *req)
621 if (req->rq_no_reply)
624 return (ptlrpc_send_reply(req, 0));
626 EXPORT_SYMBOL(ptlrpc_reply);
629 * For request \a req send an error reply back. Create empty
630 * reply buffers if necessary.
632 int ptlrpc_send_error(struct ptlrpc_request *req, int may_be_difficult)
637 if (req->rq_no_reply)
640 if (!req->rq_repmsg) {
641 rc = lustre_pack_reply(req, 1, NULL, NULL);
646 if (req->rq_status != -ENOSPC && req->rq_status != -EACCES &&
647 req->rq_status != -EPERM && req->rq_status != -ENOENT &&
648 req->rq_status != -EINPROGRESS && req->rq_status != -EDQUOT)
649 req->rq_type = PTL_RPC_MSG_ERR;
651 rc = ptlrpc_send_reply(req, may_be_difficult);
654 EXPORT_SYMBOL(ptlrpc_send_error);
656 int ptlrpc_error(struct ptlrpc_request *req)
658 return ptlrpc_send_error(req, 0);
660 EXPORT_SYMBOL(ptlrpc_error);
663 * Send request \a request.
664 * if \a noreply is set, don't expect any reply back and don't set up
666 * Returns 0 on success or error code.
668 int ptl_send_rpc(struct ptlrpc_request *request, int noreply)
673 struct ptlrpc_connection *connection;
674 lnet_handle_me_t reply_me_h;
676 struct obd_device *obd = request->rq_import->imp_obd;
679 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_DROP_RPC))
682 LASSERT(request->rq_type == PTL_RPC_MSG_REQUEST);
683 LASSERT(request->rq_wait_ctx == 0);
685 /* If this is a re-transmit, we're required to have disengaged
686 * cleanly from the previous attempt */
687 LASSERT(!request->rq_receiving_reply);
688 LASSERT(!((lustre_msg_get_flags(request->rq_reqmsg) & MSG_REPLAY) &&
689 (request->rq_import->imp_state == LUSTRE_IMP_FULL)));
691 if (unlikely(obd != NULL && obd->obd_fail)) {
692 CDEBUG(D_HA, "muting rpc for failed imp obd %s\n",
694 /* this prevents us from waiting in ptlrpc_queue_wait */
695 spin_lock(&request->rq_lock);
697 spin_unlock(&request->rq_lock);
698 request->rq_status = -ENODEV;
702 connection = request->rq_import->imp_connection;
704 lustre_msg_set_handle(request->rq_reqmsg,
705 &request->rq_import->imp_remote_handle);
706 lustre_msg_set_type(request->rq_reqmsg, PTL_RPC_MSG_REQUEST);
707 lustre_msg_set_conn_cnt(request->rq_reqmsg,
708 request->rq_import->imp_conn_cnt);
709 lustre_msghdr_set_flags(request->rq_reqmsg,
710 request->rq_import->imp_msghdr_flags);
712 if (request->rq_resend) {
713 lustre_msg_add_flags(request->rq_reqmsg, MSG_RESENT);
715 if (request->rq_resend_cb != NULL)
716 request->rq_resend_cb(request, &request->rq_async_args);
718 if (request->rq_memalloc)
719 mpflag = cfs_memory_pressure_get_and_set();
721 rc = sptlrpc_cli_wrap_request(request);
725 /* bulk register should be done after wrap_request() */
726 if (request->rq_bulk != NULL) {
727 rc = ptlrpc_register_bulk (request);
733 LASSERT (request->rq_replen != 0);
734 if (request->rq_repbuf == NULL) {
735 LASSERT(request->rq_repdata == NULL);
736 LASSERT(request->rq_repmsg == NULL);
737 rc = sptlrpc_cli_alloc_repbuf(request,
740 /* this prevents us from looping in
741 * ptlrpc_queue_wait */
742 spin_lock(&request->rq_lock);
744 spin_unlock(&request->rq_lock);
745 request->rq_status = rc;
746 GOTO(cleanup_bulk, rc);
749 request->rq_repdata = NULL;
750 request->rq_repmsg = NULL;
753 rc = LNetMEAttach(request->rq_reply_portal,/*XXX FIXME bug 249*/
754 connection->c_peer, request->rq_xid, 0,
755 LNET_UNLINK, LNET_INS_AFTER, &reply_me_h);
757 CERROR("LNetMEAttach failed: %d\n", rc);
758 LASSERT (rc == -ENOMEM);
759 GOTO(cleanup_bulk, rc = -ENOMEM);
763 spin_lock(&request->rq_lock);
764 /* If the MD attach succeeds, there _will_ be a reply_in callback */
765 request->rq_receiving_reply = !noreply;
766 request->rq_req_unlink = 1;
767 /* We are responsible for unlinking the reply buffer */
768 request->rq_reply_unlink = !noreply;
769 /* Clear any flags that may be present from previous sends. */
770 request->rq_replied = 0;
772 request->rq_timedout = 0;
773 request->rq_net_err = 0;
774 request->rq_resend = 0;
775 request->rq_restart = 0;
776 request->rq_reply_truncate = 0;
777 spin_unlock(&request->rq_lock);
780 reply_md.start = request->rq_repbuf;
781 reply_md.length = request->rq_repbuf_len;
782 /* Allow multiple early replies */
783 reply_md.threshold = LNET_MD_THRESH_INF;
784 /* Manage remote for early replies */
785 reply_md.options = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT |
786 LNET_MD_MANAGE_REMOTE |
787 LNET_MD_TRUNCATE; /* allow to make EOVERFLOW error */;
788 reply_md.user_ptr = &request->rq_reply_cbid;
789 reply_md.eq_handle = ptlrpc_eq_h;
791 /* We must see the unlink callback to unset rq_reply_unlink,
792 so we can't auto-unlink */
793 rc = LNetMDAttach(reply_me_h, reply_md, LNET_RETAIN,
794 &request->rq_reply_md_h);
796 CERROR("LNetMDAttach failed: %d\n", rc);
797 LASSERT (rc == -ENOMEM);
798 spin_lock(&request->rq_lock);
799 /* ...but the MD attach didn't succeed... */
800 request->rq_receiving_reply = 0;
801 spin_unlock(&request->rq_lock);
802 GOTO(cleanup_me, rc = -ENOMEM);
805 CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid "LPU64
807 request->rq_repbuf_len, request->rq_xid,
808 request->rq_reply_portal);
811 /* add references on request for request_out_callback */
812 ptlrpc_request_addref(request);
813 if (obd != NULL && obd->obd_svc_stats != NULL)
814 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQACTIVE_CNTR,
815 atomic_read(&request->rq_import->imp_inflight));
817 OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_DELAY_SEND, request->rq_timeout + 5);
819 do_gettimeofday(&request->rq_arrival_time);
820 request->rq_sent = cfs_time_current_sec();
821 /* We give the server rq_timeout secs to process the req, and
822 add the network latency for our local timeout. */
823 request->rq_deadline = request->rq_sent + request->rq_timeout +
824 ptlrpc_at_get_net_latency(request);
826 ptlrpc_pinger_sending_on_import(request->rq_import);
828 DEBUG_REQ(D_INFO, request, "send flg=%x",
829 lustre_msg_get_flags(request->rq_reqmsg));
830 rc = ptl_send_buf(&request->rq_req_md_h,
831 request->rq_reqbuf, request->rq_reqdata_len,
832 LNET_NOACK_REQ, &request->rq_req_cbid,
834 request->rq_request_portal,
839 ptlrpc_req_finished(request);
844 /* MEUnlink is safe; the PUT didn't even get off the ground, and
845 * nobody apart from the PUT's target has the right nid+XID to
846 * access the reply buffer. */
847 rc2 = LNetMEUnlink(reply_me_h);
849 /* UNLINKED callback called synchronously */
850 LASSERT(!request->rq_receiving_reply);
853 /* We do sync unlink here as there was no real transfer here so
854 * the chance to have long unlink to sluggish net is smaller here. */
855 ptlrpc_unregister_bulk(request, 0);
857 if (request->rq_memalloc)
858 cfs_memory_pressure_restore(mpflag);
861 EXPORT_SYMBOL(ptl_send_rpc);
864 * Register request buffer descriptor for request receiving.
866 int ptlrpc_register_rqbd(struct ptlrpc_request_buffer_desc *rqbd)
868 struct ptlrpc_service *service = rqbd->rqbd_svcpt->scp_service;
869 static lnet_process_id_t match_id = {LNET_NID_ANY, LNET_PID_ANY};
872 lnet_handle_me_t me_h;
874 CDEBUG(D_NET, "LNetMEAttach: portal %d\n",
875 service->srv_req_portal);
877 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_RQBD))
880 /* NB: CPT affinity service should use new LNet flag LNET_INS_LOCAL,
881 * which means buffer can only be attached on local CPT, and LND
882 * threads can find it by grabbing a local lock */
883 rc = LNetMEAttach(service->srv_req_portal,
884 match_id, 0, ~0, LNET_UNLINK,
885 rqbd->rqbd_svcpt->scp_cpt >= 0 ?
886 LNET_INS_LOCAL : LNET_INS_AFTER, &me_h);
888 CERROR("LNetMEAttach failed: %d\n", rc);
892 LASSERT(rqbd->rqbd_refcount == 0);
893 rqbd->rqbd_refcount = 1;
895 md.start = rqbd->rqbd_buffer;
896 md.length = service->srv_buf_size;
897 md.max_size = service->srv_max_req_size;
898 md.threshold = LNET_MD_THRESH_INF;
899 md.options = PTLRPC_MD_OPTIONS | LNET_MD_OP_PUT | LNET_MD_MAX_SIZE;
900 md.user_ptr = &rqbd->rqbd_cbid;
901 md.eq_handle = ptlrpc_eq_h;
903 rc = LNetMDAttach(me_h, md, LNET_UNLINK, &rqbd->rqbd_md_h);
907 CERROR("LNetMDAttach failed: %d; \n", rc);
908 LASSERT (rc == -ENOMEM);
909 rc = LNetMEUnlink (me_h);
911 rqbd->rqbd_refcount = 0;