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) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 /** Implementation of client-side PortalRPC interfaces */
35 #define DEBUG_SUBSYSTEM S_RPC
37 #include <linux/delay.h>
38 #include <obd_support.h>
39 #include <obd_class.h>
40 #include <lustre_lib.h>
41 #include <lustre_ha.h>
42 #include <lustre_import.h>
43 #include <lustre_req_layout.h>
45 #include "ptlrpc_internal.h"
47 const struct ptlrpc_bulk_frag_ops ptlrpc_bulk_kiov_pin_ops = {
48 .add_kiov_frag = ptlrpc_prep_bulk_page_pin,
49 .release_frags = ptlrpc_release_bulk_page_pin,
51 EXPORT_SYMBOL(ptlrpc_bulk_kiov_pin_ops);
53 const struct ptlrpc_bulk_frag_ops ptlrpc_bulk_kiov_nopin_ops = {
54 .add_kiov_frag = ptlrpc_prep_bulk_page_nopin,
55 .release_frags = ptlrpc_release_bulk_noop,
57 EXPORT_SYMBOL(ptlrpc_bulk_kiov_nopin_ops);
59 const struct ptlrpc_bulk_frag_ops ptlrpc_bulk_kvec_ops = {
60 .add_iov_frag = ptlrpc_prep_bulk_frag,
62 EXPORT_SYMBOL(ptlrpc_bulk_kvec_ops);
64 static int ptlrpc_send_new_req(struct ptlrpc_request *req);
65 static int ptlrpcd_check_work(struct ptlrpc_request *req);
66 static int ptlrpc_unregister_reply(struct ptlrpc_request *request, int async);
69 * Initialize passed in client structure \a cl.
71 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
72 struct ptlrpc_client *cl)
74 cl->cli_request_portal = req_portal;
75 cl->cli_reply_portal = rep_portal;
78 EXPORT_SYMBOL(ptlrpc_init_client);
81 * Return PortalRPC connection for remore uud \a uuid
83 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid,
84 lnet_nid_t nid4refnet)
86 struct ptlrpc_connection *c;
88 struct lnet_process_id peer;
92 * ptlrpc_uuid_to_peer() initializes its 2nd parameter
93 * before accessing its values.
95 /* coverity[uninit_use_in_call] */
96 peer.nid = nid4refnet;
97 err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
99 CNETERR("cannot find peer %s!\n", uuid->uuid);
103 c = ptlrpc_connection_get(peer, self, uuid);
105 memcpy(c->c_remote_uuid.uuid,
106 uuid->uuid, sizeof(c->c_remote_uuid.uuid));
109 CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
115 * Allocate and initialize new bulk descriptor on the sender.
116 * Returns pointer to the descriptor or NULL on error.
118 struct ptlrpc_bulk_desc *ptlrpc_new_bulk(unsigned int nfrags,
119 unsigned int max_brw,
120 enum ptlrpc_bulk_op_type type,
122 const struct ptlrpc_bulk_frag_ops *ops)
124 struct ptlrpc_bulk_desc *desc;
127 /* ensure that only one of KIOV or IOVEC is set but not both */
128 LASSERT((ptlrpc_is_bulk_desc_kiov(type) &&
129 ops->add_kiov_frag != NULL) ||
130 (ptlrpc_is_bulk_desc_kvec(type) &&
131 ops->add_iov_frag != NULL));
136 if (type & PTLRPC_BULK_BUF_KIOV) {
137 OBD_ALLOC_LARGE(GET_KIOV(desc),
138 nfrags * sizeof(*GET_KIOV(desc)));
142 OBD_ALLOC_LARGE(GET_KVEC(desc),
143 nfrags * sizeof(*GET_KVEC(desc)));
148 spin_lock_init(&desc->bd_lock);
149 init_waitqueue_head(&desc->bd_waitq);
150 desc->bd_max_iov = nfrags;
151 desc->bd_iov_count = 0;
152 desc->bd_portal = portal;
153 desc->bd_type = type;
154 desc->bd_md_count = 0;
155 desc->bd_frag_ops = (struct ptlrpc_bulk_frag_ops *)ops;
156 LASSERT(max_brw > 0);
157 desc->bd_md_max_brw = min(max_brw, PTLRPC_BULK_OPS_COUNT);
159 * PTLRPC_BULK_OPS_COUNT is the compile-time transfer limit for this
160 * node. Negotiated ocd_brw_size will always be <= this number.
162 for (i = 0; i < PTLRPC_BULK_OPS_COUNT; i++)
163 LNetInvalidateMDHandle(&desc->bd_mds[i]);
172 * Prepare bulk descriptor for specified outgoing request \a req that
173 * can fit \a nfrags * pages. \a type is bulk type. \a portal is where
174 * the bulk to be sent. Used on client-side.
175 * Returns pointer to newly allocatrd initialized bulk descriptor or NULL on
178 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp(struct ptlrpc_request *req,
180 unsigned int max_brw,
183 const struct ptlrpc_bulk_frag_ops
186 struct obd_import *imp = req->rq_import;
187 struct ptlrpc_bulk_desc *desc;
190 LASSERT(ptlrpc_is_bulk_op_passive(type));
192 desc = ptlrpc_new_bulk(nfrags, max_brw, type, portal, ops);
196 desc->bd_import_generation = req->rq_import_generation;
197 desc->bd_import = class_import_get(imp);
200 desc->bd_cbid.cbid_fn = client_bulk_callback;
201 desc->bd_cbid.cbid_arg = desc;
203 /* This makes req own desc, and free it when she frees herself */
208 EXPORT_SYMBOL(ptlrpc_prep_bulk_imp);
210 void __ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
211 struct page *page, int pageoffset, int len,
216 LASSERT(desc->bd_iov_count < desc->bd_max_iov);
217 LASSERT(page != NULL);
218 LASSERT(pageoffset >= 0);
220 LASSERT(pageoffset + len <= PAGE_SIZE);
221 LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
223 kiov = &BD_GET_KIOV(desc, desc->bd_iov_count);
230 kiov->kiov_page = page;
231 kiov->kiov_offset = pageoffset;
232 kiov->kiov_len = len;
234 desc->bd_iov_count++;
236 EXPORT_SYMBOL(__ptlrpc_prep_bulk_page);
238 int ptlrpc_prep_bulk_frag(struct ptlrpc_bulk_desc *desc,
245 LASSERT(desc->bd_iov_count < desc->bd_max_iov);
246 LASSERT(frag != NULL);
248 LASSERT(ptlrpc_is_bulk_desc_kvec(desc->bd_type));
250 iovec = &BD_GET_KVEC(desc, desc->bd_iov_count);
254 iovec->iov_base = frag;
255 iovec->iov_len = len;
257 desc->bd_iov_count++;
259 RETURN(desc->bd_nob);
261 EXPORT_SYMBOL(ptlrpc_prep_bulk_frag);
263 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
267 LASSERT(desc != NULL);
268 LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
269 LASSERT(desc->bd_md_count == 0); /* network hands off */
270 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
271 LASSERT(desc->bd_frag_ops != NULL);
273 if (ptlrpc_is_bulk_desc_kiov(desc->bd_type))
274 sptlrpc_enc_pool_put_pages(desc);
277 class_export_put(desc->bd_export);
279 class_import_put(desc->bd_import);
281 if (desc->bd_frag_ops->release_frags != NULL)
282 desc->bd_frag_ops->release_frags(desc);
284 if (ptlrpc_is_bulk_desc_kiov(desc->bd_type))
285 OBD_FREE_LARGE(GET_KIOV(desc),
286 desc->bd_max_iov * sizeof(*GET_KIOV(desc)));
288 OBD_FREE_LARGE(GET_KVEC(desc),
289 desc->bd_max_iov * sizeof(*GET_KVEC(desc)));
293 EXPORT_SYMBOL(ptlrpc_free_bulk);
296 * Set server timelimit for this req, i.e. how long are we willing to wait
297 * for reply before timing out this request.
299 void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req)
305 LASSERT(req->rq_import);
308 /* non-AT settings */
310 * \a imp_server_timeout means this is reverse import and
311 * we send (currently only) ASTs to the client and cannot afford
312 * to wait too long for the reply, otherwise the other client
313 * (because of which we are sending this request) would
314 * timeout waiting for us
316 req->rq_timeout = req->rq_import->imp_server_timeout ?
317 obd_timeout / 2 : obd_timeout;
319 at = &req->rq_import->imp_at;
320 idx = import_at_get_index(req->rq_import,
321 req->rq_request_portal);
322 serv_est = at_get(&at->iat_service_estimate[idx]);
323 req->rq_timeout = at_est2timeout(serv_est);
326 * We could get even fancier here, using history to predict increased
331 * Let the server know what this RPC timeout is by putting it in the
334 lustre_msg_set_timeout(req->rq_reqmsg, req->rq_timeout);
336 EXPORT_SYMBOL(ptlrpc_at_set_req_timeout);
338 /* Adjust max service estimate based on server value */
339 static void ptlrpc_at_adj_service(struct ptlrpc_request *req,
340 unsigned int serv_est)
346 LASSERT(req->rq_import);
347 at = &req->rq_import->imp_at;
349 idx = import_at_get_index(req->rq_import, req->rq_request_portal);
351 * max service estimates are tracked on the server side,
352 * so just keep minimal history here
354 oldse = at_measured(&at->iat_service_estimate[idx], serv_est);
357 "The RPC service estimate for %s ptl %d has changed from %d to %d\n",
358 req->rq_import->imp_obd->obd_name,
359 req->rq_request_portal,
360 oldse, at_get(&at->iat_service_estimate[idx]));
363 /* Expected network latency per remote node (secs) */
364 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req)
366 return AT_OFF ? 0 : at_get(&req->rq_import->imp_at.iat_net_latency);
369 /* Adjust expected network latency */
370 void ptlrpc_at_adj_net_latency(struct ptlrpc_request *req,
371 unsigned int service_time)
373 unsigned int nl, oldnl;
375 time64_t now = ktime_get_real_seconds();
377 LASSERT(req->rq_import);
379 if (service_time > now - req->rq_sent + 3) {
381 * b=16408, however, this can also happen if early reply
382 * is lost and client RPC is expired and resent, early reply
383 * or reply of original RPC can still be fit in reply buffer
384 * of resent RPC, now client is measuring time from the
385 * resent time, but server sent back service time of original
388 CDEBUG((lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT) ?
389 D_ADAPTTO : D_WARNING,
390 "Reported service time %u > total measured time %lld\n",
391 service_time, now - req->rq_sent);
395 /* Network latency is total time less server processing time */
396 nl = max_t(int, now - req->rq_sent -
397 service_time, 0) + 1; /* st rounding */
398 at = &req->rq_import->imp_at;
400 oldnl = at_measured(&at->iat_net_latency, nl);
403 "The network latency for %s (nid %s) has changed from %d to %d\n",
404 req->rq_import->imp_obd->obd_name,
405 obd_uuid2str(&req->rq_import->imp_connection->c_remote_uuid),
406 oldnl, at_get(&at->iat_net_latency));
409 static int unpack_reply(struct ptlrpc_request *req)
413 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
414 rc = ptlrpc_unpack_rep_msg(req, req->rq_replen);
416 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
421 rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
423 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
430 * Handle an early reply message, called with the rq_lock held.
431 * If anything goes wrong just ignore it - same as if it never happened
433 static int ptlrpc_at_recv_early_reply(struct ptlrpc_request *req)
434 __must_hold(&req->rq_lock)
436 struct ptlrpc_request *early_req;
442 spin_unlock(&req->rq_lock);
444 rc = sptlrpc_cli_unwrap_early_reply(req, &early_req);
446 spin_lock(&req->rq_lock);
450 rc = unpack_reply(early_req);
452 sptlrpc_cli_finish_early_reply(early_req);
453 spin_lock(&req->rq_lock);
458 * Use new timeout value just to adjust the local value for this
459 * request, don't include it into at_history. It is unclear yet why
460 * service time increased and should it be counted or skipped, e.g.
461 * that can be recovery case or some error or server, the real reply
462 * will add all new data if it is worth to add.
464 req->rq_timeout = lustre_msg_get_timeout(early_req->rq_repmsg);
465 lustre_msg_set_timeout(req->rq_reqmsg, req->rq_timeout);
467 /* Network latency can be adjusted, it is pure network delays */
468 ptlrpc_at_adj_net_latency(req,
469 lustre_msg_get_service_time(early_req->rq_repmsg));
471 sptlrpc_cli_finish_early_reply(early_req);
473 spin_lock(&req->rq_lock);
474 olddl = req->rq_deadline;
476 * server assumes it now has rq_timeout from when the request
477 * arrived, so the client should give it at least that long.
478 * since we don't know the arrival time we'll use the original
481 req->rq_deadline = req->rq_sent + req->rq_timeout +
482 ptlrpc_at_get_net_latency(req);
484 DEBUG_REQ(D_ADAPTTO, req,
485 "Early reply #%d, new deadline in %llds (%llds)",
487 req->rq_deadline - ktime_get_real_seconds(),
488 req->rq_deadline - olddl);
493 static struct kmem_cache *request_cache;
495 int ptlrpc_request_cache_init(void)
497 request_cache = kmem_cache_create("ptlrpc_cache",
498 sizeof(struct ptlrpc_request),
499 0, SLAB_HWCACHE_ALIGN, NULL);
500 return request_cache ? 0 : -ENOMEM;
503 void ptlrpc_request_cache_fini(void)
505 kmem_cache_destroy(request_cache);
508 struct ptlrpc_request *ptlrpc_request_cache_alloc(gfp_t flags)
510 struct ptlrpc_request *req;
512 OBD_SLAB_ALLOC_PTR_GFP(req, request_cache, flags);
516 void ptlrpc_request_cache_free(struct ptlrpc_request *req)
518 OBD_SLAB_FREE_PTR(req, request_cache);
522 * Wind down request pool \a pool.
523 * Frees all requests from the pool too
525 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
527 struct list_head *l, *tmp;
528 struct ptlrpc_request *req;
530 LASSERT(pool != NULL);
532 spin_lock(&pool->prp_lock);
533 list_for_each_safe(l, tmp, &pool->prp_req_list) {
534 req = list_entry(l, struct ptlrpc_request, rq_list);
535 list_del(&req->rq_list);
536 LASSERT(req->rq_reqbuf);
537 LASSERT(req->rq_reqbuf_len == pool->prp_rq_size);
538 OBD_FREE_LARGE(req->rq_reqbuf, pool->prp_rq_size);
539 ptlrpc_request_cache_free(req);
541 spin_unlock(&pool->prp_lock);
542 OBD_FREE(pool, sizeof(*pool));
544 EXPORT_SYMBOL(ptlrpc_free_rq_pool);
547 * Allocates, initializes and adds \a num_rq requests to the pool \a pool
549 int ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
554 while (size < pool->prp_rq_size)
557 LASSERTF(list_empty(&pool->prp_req_list) ||
558 size == pool->prp_rq_size,
559 "Trying to change pool size with nonempty pool from %d to %d bytes\n",
560 pool->prp_rq_size, size);
562 spin_lock(&pool->prp_lock);
563 pool->prp_rq_size = size;
564 for (i = 0; i < num_rq; i++) {
565 struct ptlrpc_request *req;
566 struct lustre_msg *msg;
568 spin_unlock(&pool->prp_lock);
569 req = ptlrpc_request_cache_alloc(GFP_NOFS);
572 OBD_ALLOC_LARGE(msg, size);
574 ptlrpc_request_cache_free(req);
577 req->rq_reqbuf = msg;
578 req->rq_reqbuf_len = size;
580 spin_lock(&pool->prp_lock);
581 list_add_tail(&req->rq_list, &pool->prp_req_list);
583 spin_unlock(&pool->prp_lock);
586 EXPORT_SYMBOL(ptlrpc_add_rqs_to_pool);
589 * Create and initialize new request pool with given attributes:
590 * \a num_rq - initial number of requests to create for the pool
591 * \a msgsize - maximum message size possible for requests in thid pool
592 * \a populate_pool - function to be called when more requests need to be added
594 * Returns pointer to newly created pool or NULL on error.
596 struct ptlrpc_request_pool *
597 ptlrpc_init_rq_pool(int num_rq, int msgsize,
598 int (*populate_pool)(struct ptlrpc_request_pool *, int))
600 struct ptlrpc_request_pool *pool;
602 OBD_ALLOC(pool, sizeof(struct ptlrpc_request_pool));
607 * Request next power of two for the allocation, because internally
608 * kernel would do exactly this
610 spin_lock_init(&pool->prp_lock);
611 INIT_LIST_HEAD(&pool->prp_req_list);
612 pool->prp_rq_size = msgsize + SPTLRPC_MAX_PAYLOAD;
613 pool->prp_populate = populate_pool;
615 populate_pool(pool, num_rq);
619 EXPORT_SYMBOL(ptlrpc_init_rq_pool);
622 * Fetches one request from pool \a pool
624 static struct ptlrpc_request *
625 ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
627 struct ptlrpc_request *request;
628 struct lustre_msg *reqbuf;
633 spin_lock(&pool->prp_lock);
636 * See if we have anything in a pool, and bail out if nothing,
637 * in writeout path, where this matters, this is safe to do, because
638 * nothing is lost in this case, and when some in-flight requests
639 * complete, this code will be called again.
641 if (unlikely(list_empty(&pool->prp_req_list))) {
642 spin_unlock(&pool->prp_lock);
646 request = list_entry(pool->prp_req_list.next, struct ptlrpc_request,
648 list_del_init(&request->rq_list);
649 spin_unlock(&pool->prp_lock);
651 LASSERT(request->rq_reqbuf);
652 LASSERT(request->rq_pool);
654 reqbuf = request->rq_reqbuf;
655 memset(request, 0, sizeof(*request));
656 request->rq_reqbuf = reqbuf;
657 request->rq_reqbuf_len = pool->prp_rq_size;
658 request->rq_pool = pool;
664 * Returns freed \a request to pool.
666 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
668 struct ptlrpc_request_pool *pool = request->rq_pool;
670 spin_lock(&pool->prp_lock);
671 LASSERT(list_empty(&request->rq_list));
672 LASSERT(!request->rq_receiving_reply);
673 list_add_tail(&request->rq_list, &pool->prp_req_list);
674 spin_unlock(&pool->prp_lock);
677 void ptlrpc_add_unreplied(struct ptlrpc_request *req)
679 struct obd_import *imp = req->rq_import;
680 struct list_head *tmp;
681 struct ptlrpc_request *iter;
683 assert_spin_locked(&imp->imp_lock);
684 LASSERT(list_empty(&req->rq_unreplied_list));
686 /* unreplied list is sorted by xid in ascending order */
687 list_for_each_prev(tmp, &imp->imp_unreplied_list) {
688 iter = list_entry(tmp, struct ptlrpc_request,
691 LASSERT(req->rq_xid != iter->rq_xid);
692 if (req->rq_xid < iter->rq_xid)
694 list_add(&req->rq_unreplied_list, &iter->rq_unreplied_list);
697 list_add(&req->rq_unreplied_list, &imp->imp_unreplied_list);
700 void ptlrpc_assign_next_xid_nolock(struct ptlrpc_request *req)
702 req->rq_xid = ptlrpc_next_xid();
703 ptlrpc_add_unreplied(req);
706 static inline void ptlrpc_assign_next_xid(struct ptlrpc_request *req)
708 spin_lock(&req->rq_import->imp_lock);
709 ptlrpc_assign_next_xid_nolock(req);
710 spin_unlock(&req->rq_import->imp_lock);
713 static __u64 ptlrpc_last_xid;
714 static spinlock_t ptlrpc_last_xid_lock;
716 int ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
717 __u32 version, int opcode, char **bufs,
718 struct ptlrpc_cli_ctx *ctx)
721 struct obd_import *imp;
727 count = req_capsule_filled_sizes(&request->rq_pill, RCL_CLIENT);
728 imp = request->rq_import;
729 lengths = request->rq_pill.rc_area[RCL_CLIENT];
732 request->rq_cli_ctx = sptlrpc_cli_ctx_get(ctx);
734 rc = sptlrpc_req_get_ctx(request);
738 sptlrpc_req_set_flavor(request, opcode);
740 rc = lustre_pack_request(request, imp->imp_msg_magic, count,
745 lustre_msg_add_version(request->rq_reqmsg, version);
746 request->rq_send_state = LUSTRE_IMP_FULL;
747 request->rq_type = PTL_RPC_MSG_REQUEST;
749 request->rq_req_cbid.cbid_fn = request_out_callback;
750 request->rq_req_cbid.cbid_arg = request;
752 request->rq_reply_cbid.cbid_fn = reply_in_callback;
753 request->rq_reply_cbid.cbid_arg = request;
755 request->rq_reply_deadline = 0;
756 request->rq_bulk_deadline = 0;
757 request->rq_req_deadline = 0;
758 request->rq_phase = RQ_PHASE_NEW;
759 request->rq_next_phase = RQ_PHASE_UNDEFINED;
761 request->rq_request_portal = imp->imp_client->cli_request_portal;
762 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
764 ptlrpc_at_set_req_timeout(request);
766 lustre_msg_set_opc(request->rq_reqmsg, opcode);
768 /* Let's setup deadline for req/reply/bulk unlink for opcode. */
769 if (cfs_fail_val == opcode) {
770 time64_t *fail_t = NULL, *fail2_t = NULL;
772 if (CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK)) {
773 fail_t = &request->rq_bulk_deadline;
774 } else if (CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
775 fail_t = &request->rq_reply_deadline;
776 } else if (CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REQ_UNLINK)) {
777 fail_t = &request->rq_req_deadline;
778 } else if (CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BOTH_UNLINK)) {
779 fail_t = &request->rq_reply_deadline;
780 fail2_t = &request->rq_bulk_deadline;
781 } else if (CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_ROUND_XID)) {
782 time64_t now = ktime_get_real_seconds();
783 spin_lock(&ptlrpc_last_xid_lock);
784 ptlrpc_last_xid = ((__u64)now >> 4) << 24;
785 spin_unlock(&ptlrpc_last_xid_lock);
789 *fail_t = ktime_get_real_seconds() + LONG_UNLINK;
792 *fail2_t = ktime_get_real_seconds() +
796 * The RPC is infected, let the test to change the
799 msleep(4 * MSEC_PER_SEC);
802 ptlrpc_assign_next_xid(request);
807 LASSERT(!request->rq_pool);
808 sptlrpc_cli_ctx_put(request->rq_cli_ctx, 1);
810 class_import_put(imp);
814 EXPORT_SYMBOL(ptlrpc_request_bufs_pack);
817 * Pack request buffers for network transfer, performing necessary encryption
818 * steps if necessary.
820 int ptlrpc_request_pack(struct ptlrpc_request *request,
821 __u32 version, int opcode)
825 rc = ptlrpc_request_bufs_pack(request, version, opcode, NULL, NULL);
830 * For some old 1.8 clients (< 1.8.7), they will LASSERT the size of
831 * ptlrpc_body sent from server equal to local ptlrpc_body size, so we
832 * have to send old ptlrpc_body to keep interoprability with these
835 * Only three kinds of server->client RPCs so far:
840 * XXX This should be removed whenever we drop the interoprability with
841 * the these old clients.
843 if (opcode == LDLM_BL_CALLBACK || opcode == LDLM_CP_CALLBACK ||
844 opcode == LDLM_GL_CALLBACK)
845 req_capsule_shrink(&request->rq_pill, &RMF_PTLRPC_BODY,
846 sizeof(struct ptlrpc_body_v2), RCL_CLIENT);
850 EXPORT_SYMBOL(ptlrpc_request_pack);
853 * Helper function to allocate new request on import \a imp
854 * and possibly using existing request from pool \a pool if provided.
855 * Returns allocated request structure with import field filled or
859 struct ptlrpc_request *__ptlrpc_request_alloc(struct obd_import *imp,
860 struct ptlrpc_request_pool *pool)
862 struct ptlrpc_request *request = NULL;
864 request = ptlrpc_request_cache_alloc(GFP_NOFS);
866 if (!request && pool)
867 request = ptlrpc_prep_req_from_pool(pool);
870 ptlrpc_cli_req_init(request);
872 LASSERTF((unsigned long)imp > 0x1000, "%p", imp);
873 LASSERT(imp != LP_POISON);
874 LASSERTF((unsigned long)imp->imp_client > 0x1000, "%p\n",
876 LASSERT(imp->imp_client != LP_POISON);
878 request->rq_import = class_import_get(imp);
880 CERROR("request allocation out of memory\n");
887 * Helper function for creating a request.
888 * Calls __ptlrpc_request_alloc to allocate new request sturcture and inits
889 * buffer structures according to capsule template \a format.
890 * Returns allocated request structure pointer or NULL on error.
892 static struct ptlrpc_request *
893 ptlrpc_request_alloc_internal(struct obd_import *imp,
894 struct ptlrpc_request_pool *pool,
895 const struct req_format *format)
897 struct ptlrpc_request *request;
900 request = __ptlrpc_request_alloc(imp, pool);
905 * initiate connection if needed when the import has been
906 * referenced by the new request to avoid races with disconnect
908 if (unlikely(imp->imp_state == LUSTRE_IMP_IDLE)) {
911 CDEBUG_LIMIT(imp->imp_idle_debug,
912 "%s: reconnect after %llds idle\n",
913 imp->imp_obd->obd_name, ktime_get_real_seconds() -
914 imp->imp_last_reply_time);
915 spin_lock(&imp->imp_lock);
916 if (imp->imp_state == LUSTRE_IMP_IDLE) {
917 imp->imp_generation++;
918 imp->imp_initiated_at = imp->imp_generation;
919 imp->imp_state = LUSTRE_IMP_NEW;
922 spin_unlock(&imp->imp_lock);
924 rc = ptlrpc_connect_import(imp);
926 ptlrpc_request_free(request);
929 ptlrpc_pinger_add_import(imp);
933 req_capsule_init(&request->rq_pill, request, RCL_CLIENT);
934 req_capsule_set(&request->rq_pill, format);
939 * Allocate new request structure for import \a imp and initialize its
940 * buffer structure according to capsule template \a format.
942 struct ptlrpc_request *ptlrpc_request_alloc(struct obd_import *imp,
943 const struct req_format *format)
945 return ptlrpc_request_alloc_internal(imp, NULL, format);
947 EXPORT_SYMBOL(ptlrpc_request_alloc);
950 * Allocate new request structure for import \a imp from pool \a pool and
951 * initialize its buffer structure according to capsule template \a format.
953 struct ptlrpc_request *
954 ptlrpc_request_alloc_pool(struct obd_import *imp,
955 struct ptlrpc_request_pool *pool,
956 const struct req_format *format)
958 return ptlrpc_request_alloc_internal(imp, pool, format);
960 EXPORT_SYMBOL(ptlrpc_request_alloc_pool);
963 * For requests not from pool, free memory of the request structure.
964 * For requests obtained from a pool earlier, return request back to pool.
966 void ptlrpc_request_free(struct ptlrpc_request *request)
968 if (request->rq_pool)
969 __ptlrpc_free_req_to_pool(request);
971 ptlrpc_request_cache_free(request);
973 EXPORT_SYMBOL(ptlrpc_request_free);
976 * Allocate new request for operatione \a opcode and immediatelly pack it for
978 * Only used for simple requests like OBD_PING where the only important
979 * part of the request is operation itself.
980 * Returns allocated request or NULL on error.
982 struct ptlrpc_request *ptlrpc_request_alloc_pack(struct obd_import *imp,
983 const struct req_format *format,
984 __u32 version, int opcode)
986 struct ptlrpc_request *req = ptlrpc_request_alloc(imp, format);
990 rc = ptlrpc_request_pack(req, version, opcode);
992 ptlrpc_request_free(req);
998 EXPORT_SYMBOL(ptlrpc_request_alloc_pack);
1001 * Allocate and initialize new request set structure on the current CPT.
1002 * Returns a pointer to the newly allocated set structure or NULL on error.
1004 struct ptlrpc_request_set *ptlrpc_prep_set(void)
1006 struct ptlrpc_request_set *set;
1010 cpt = cfs_cpt_current(cfs_cpt_table, 0);
1011 OBD_CPT_ALLOC(set, cfs_cpt_table, cpt, sizeof(*set));
1014 atomic_set(&set->set_refcount, 1);
1015 INIT_LIST_HEAD(&set->set_requests);
1016 init_waitqueue_head(&set->set_waitq);
1017 atomic_set(&set->set_new_count, 0);
1018 atomic_set(&set->set_remaining, 0);
1019 spin_lock_init(&set->set_new_req_lock);
1020 INIT_LIST_HEAD(&set->set_new_requests);
1021 set->set_max_inflight = UINT_MAX;
1022 set->set_producer = NULL;
1023 set->set_producer_arg = NULL;
1028 EXPORT_SYMBOL(ptlrpc_prep_set);
1031 * Allocate and initialize new request set structure with flow control
1032 * extension. This extension allows to control the number of requests in-flight
1033 * for the whole set. A callback function to generate requests must be provided
1034 * and the request set will keep the number of requests sent over the wire to
1036 * Returns a pointer to the newly allocated set structure or NULL on error.
1038 struct ptlrpc_request_set *ptlrpc_prep_fcset(int max, set_producer_func func,
1042 struct ptlrpc_request_set *set;
1044 set = ptlrpc_prep_set();
1048 set->set_max_inflight = max;
1049 set->set_producer = func;
1050 set->set_producer_arg = arg;
1056 * Wind down and free request set structure previously allocated with
1058 * Ensures that all requests on the set have completed and removes
1059 * all requests from the request list in a set.
1060 * If any unsent request happen to be on the list, pretends that they got
1061 * an error in flight and calls their completion handler.
1063 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
1065 struct list_head *tmp;
1066 struct list_head *next;
1072 /* Requests on the set should either all be completed, or all be new */
1073 expected_phase = (atomic_read(&set->set_remaining) == 0) ?
1074 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
1075 list_for_each(tmp, &set->set_requests) {
1076 struct ptlrpc_request *req =
1077 list_entry(tmp, struct ptlrpc_request,
1080 LASSERT(req->rq_phase == expected_phase);
1084 LASSERTF(atomic_read(&set->set_remaining) == 0 ||
1085 atomic_read(&set->set_remaining) == n, "%d / %d\n",
1086 atomic_read(&set->set_remaining), n);
1088 list_for_each_safe(tmp, next, &set->set_requests) {
1089 struct ptlrpc_request *req =
1090 list_entry(tmp, struct ptlrpc_request,
1092 list_del_init(&req->rq_set_chain);
1094 LASSERT(req->rq_phase == expected_phase);
1096 if (req->rq_phase == RQ_PHASE_NEW) {
1097 ptlrpc_req_interpret(NULL, req, -EBADR);
1098 atomic_dec(&set->set_remaining);
1101 spin_lock(&req->rq_lock);
1103 req->rq_invalid_rqset = 0;
1104 spin_unlock(&req->rq_lock);
1106 ptlrpc_req_finished(req);
1109 LASSERT(atomic_read(&set->set_remaining) == 0);
1111 ptlrpc_reqset_put(set);
1114 EXPORT_SYMBOL(ptlrpc_set_destroy);
1117 * Add a new request to the general purpose request set.
1118 * Assumes request reference from the caller.
1120 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
1121 struct ptlrpc_request *req)
1123 LASSERT(req->rq_import->imp_state != LUSTRE_IMP_IDLE);
1124 LASSERT(list_empty(&req->rq_set_chain));
1126 if (req->rq_allow_intr)
1127 set->set_allow_intr = 1;
1129 /* The set takes over the caller's request reference */
1130 list_add_tail(&req->rq_set_chain, &set->set_requests);
1132 atomic_inc(&set->set_remaining);
1133 req->rq_queued_time = ktime_get_seconds();
1136 lustre_msg_set_jobid(req->rq_reqmsg, NULL);
1138 if (set->set_producer)
1140 * If the request set has a producer callback, the RPC must be
1141 * sent straight away
1143 ptlrpc_send_new_req(req);
1145 EXPORT_SYMBOL(ptlrpc_set_add_req);
1148 * Add a request to a request with dedicated server thread
1149 * and wake the thread to make any necessary processing.
1150 * Currently only used for ptlrpcd.
1152 void ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
1153 struct ptlrpc_request *req)
1155 struct ptlrpc_request_set *set = pc->pc_set;
1158 LASSERT(req->rq_set == NULL);
1159 LASSERT(test_bit(LIOD_STOP, &pc->pc_flags) == 0);
1161 spin_lock(&set->set_new_req_lock);
1163 * The set takes over the caller's request reference.
1166 req->rq_queued_time = ktime_get_seconds();
1167 list_add_tail(&req->rq_set_chain, &set->set_new_requests);
1168 count = atomic_inc_return(&set->set_new_count);
1169 spin_unlock(&set->set_new_req_lock);
1171 /* Only need to call wakeup once for the first entry. */
1173 wake_up(&set->set_waitq);
1176 * XXX: It maybe unnecessary to wakeup all the partners. But to
1177 * guarantee the async RPC can be processed ASAP, we have
1178 * no other better choice. It maybe fixed in future.
1180 for (i = 0; i < pc->pc_npartners; i++)
1181 wake_up(&pc->pc_partners[i]->pc_set->set_waitq);
1186 * Based on the current state of the import, determine if the request
1187 * can be sent, is an error, or should be delayed.
1189 * Returns true if this request should be delayed. If false, and
1190 * *status is set, then the request can not be sent and *status is the
1191 * error code. If false and status is 0, then request can be sent.
1193 * The imp->imp_lock must be held.
1195 static int ptlrpc_import_delay_req(struct obd_import *imp,
1196 struct ptlrpc_request *req, int *status)
1204 if (req->rq_ctx_init || req->rq_ctx_fini) {
1205 /* always allow ctx init/fini rpc go through */
1206 } else if (imp->imp_state == LUSTRE_IMP_NEW) {
1207 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
1209 } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
1210 unsigned int opc = lustre_msg_get_opc(req->rq_reqmsg);
1213 * pings or MDS-equivalent STATFS may safely
1216 DEBUG_REQ((opc == OBD_PING || opc == OST_STATFS) ?
1217 D_HA : D_ERROR, req, "IMP_CLOSED ");
1219 } else if (ptlrpc_send_limit_expired(req)) {
1220 /* probably doesn't need to be a D_ERROR afterinitial testing */
1221 DEBUG_REQ(D_HA, req, "send limit expired ");
1222 *status = -ETIMEDOUT;
1223 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
1224 imp->imp_state == LUSTRE_IMP_CONNECTING) {
1225 ;/* allow CONNECT even if import is invalid */
1226 if (atomic_read(&imp->imp_inval_count) != 0) {
1227 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1230 } else if (imp->imp_invalid || imp->imp_obd->obd_no_recov) {
1231 if (!imp->imp_deactive)
1232 DEBUG_REQ(D_NET, req, "IMP_INVALID");
1233 *status = -ESHUTDOWN; /* b=12940 */
1234 } else if (req->rq_import_generation != imp->imp_generation) {
1235 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
1237 } else if (req->rq_send_state != imp->imp_state) {
1238 /* invalidate in progress - any requests should be drop */
1239 if (atomic_read(&imp->imp_inval_count) != 0) {
1240 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1242 } else if (req->rq_no_delay &&
1243 imp->imp_generation != imp->imp_initiated_at) {
1244 /* ignore nodelay for requests initiating connections */
1245 *status = -EWOULDBLOCK;
1246 } else if (req->rq_allow_replay &&
1247 (imp->imp_state == LUSTRE_IMP_REPLAY ||
1248 imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS ||
1249 imp->imp_state == LUSTRE_IMP_REPLAY_WAIT ||
1250 imp->imp_state == LUSTRE_IMP_RECOVER)) {
1251 DEBUG_REQ(D_HA, req, "allow during recovery.\n");
1261 * Decide if the error message should be printed to the console or not.
1262 * Makes its decision based on request type, status, and failure frequency.
1264 * \param[in] req request that failed and may need a console message
1266 * \retval false if no message should be printed
1267 * \retval true if console message should be printed
1269 static bool ptlrpc_console_allow(struct ptlrpc_request *req, __u32 opc, int err)
1271 LASSERT(req->rq_reqmsg != NULL);
1273 /* Suppress particular reconnect errors which are to be expected. */
1274 if (opc == OST_CONNECT || opc == MDS_CONNECT || opc == MGS_CONNECT) {
1275 /* Suppress timed out reconnect requests */
1276 if (lustre_handle_is_used(&req->rq_import->imp_remote_handle) ||
1281 * Suppress most unavailable/again reconnect requests, but
1282 * print occasionally so it is clear client is trying to
1283 * connect to a server where no target is running.
1285 if ((err == -ENODEV || err == -EAGAIN) &&
1286 req->rq_import->imp_conn_cnt % 30 != 20)
1290 if (opc == LDLM_ENQUEUE && err == -EAGAIN)
1291 /* -EAGAIN is normal when using POSIX flocks */
1294 if (opc == OBD_PING && (err == -ENODEV || err == -ENOTCONN) &&
1295 (req->rq_xid & 0xf) != 10)
1296 /* Suppress most ping requests, they may fail occasionally */
1303 * Check request processing status.
1304 * Returns the status.
1306 static int ptlrpc_check_status(struct ptlrpc_request *req)
1311 err = lustre_msg_get_status(req->rq_repmsg);
1312 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
1313 struct obd_import *imp = req->rq_import;
1314 lnet_nid_t nid = imp->imp_connection->c_peer.nid;
1315 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1317 if (ptlrpc_console_allow(req, opc, err))
1318 LCONSOLE_ERROR_MSG(0x11,
1319 "%s: operation %s to node %s failed: rc = %d\n",
1320 imp->imp_obd->obd_name,
1322 libcfs_nid2str(nid), err);
1323 RETURN(err < 0 ? err : -EINVAL);
1327 DEBUG_REQ(D_INFO, req, "status is %d", err);
1328 } else if (err > 0) {
1329 /* XXX: translate this error from net to host */
1330 DEBUG_REQ(D_INFO, req, "status is %d", err);
1337 * save pre-versions of objects into request for replay.
1338 * Versions are obtained from server reply.
1341 static void ptlrpc_save_versions(struct ptlrpc_request *req)
1343 struct lustre_msg *repmsg = req->rq_repmsg;
1344 struct lustre_msg *reqmsg = req->rq_reqmsg;
1345 __u64 *versions = lustre_msg_get_versions(repmsg);
1348 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1352 lustre_msg_set_versions(reqmsg, versions);
1353 CDEBUG(D_INFO, "Client save versions [%#llx/%#llx]\n",
1354 versions[0], versions[1]);
1359 __u64 ptlrpc_known_replied_xid(struct obd_import *imp)
1361 struct ptlrpc_request *req;
1363 assert_spin_locked(&imp->imp_lock);
1364 if (list_empty(&imp->imp_unreplied_list))
1367 req = list_entry(imp->imp_unreplied_list.next, struct ptlrpc_request,
1369 LASSERTF(req->rq_xid >= 1, "XID:%llu\n", req->rq_xid);
1371 if (imp->imp_known_replied_xid < req->rq_xid - 1)
1372 imp->imp_known_replied_xid = req->rq_xid - 1;
1374 return req->rq_xid - 1;
1378 * Callback function called when client receives RPC reply for \a req.
1379 * Returns 0 on success or error code.
1380 * The return alue would be assigned to req->rq_status by the caller
1381 * as request processing status.
1382 * This function also decides if the request needs to be saved for later replay.
1384 static int after_reply(struct ptlrpc_request *req)
1386 struct obd_import *imp = req->rq_import;
1387 struct obd_device *obd = req->rq_import->imp_obd;
1394 LASSERT(obd != NULL);
1395 /* repbuf must be unlinked */
1396 LASSERT(!req->rq_receiving_reply && req->rq_reply_unlinked);
1398 if (req->rq_reply_truncated) {
1399 if (ptlrpc_no_resend(req)) {
1400 DEBUG_REQ(D_ERROR, req,
1401 "reply buffer overflow, expected: %d, actual size: %d",
1402 req->rq_nob_received, req->rq_repbuf_len);
1406 sptlrpc_cli_free_repbuf(req);
1408 * Pass the required reply buffer size (include
1409 * space for early reply).
1410 * NB: no need to roundup because alloc_repbuf
1413 req->rq_replen = req->rq_nob_received;
1414 req->rq_nob_received = 0;
1415 spin_lock(&req->rq_lock);
1417 spin_unlock(&req->rq_lock);
1421 work_start = ktime_get_real();
1422 timediff = ktime_us_delta(work_start, req->rq_sent_ns);
1425 * NB Until this point, the whole of the incoming message,
1426 * including buflens, status etc is in the sender's byte order.
1428 rc = sptlrpc_cli_unwrap_reply(req);
1430 DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
1435 * Security layer unwrap might ask resend this request.
1440 rc = unpack_reply(req);
1444 /* retry indefinitely on EINPROGRESS */
1445 if (lustre_msg_get_status(req->rq_repmsg) == -EINPROGRESS &&
1446 ptlrpc_no_resend(req) == 0 && !req->rq_no_retry_einprogress) {
1447 time64_t now = ktime_get_real_seconds();
1449 DEBUG_REQ(D_RPCTRACE, req, "Resending request on EINPROGRESS");
1450 spin_lock(&req->rq_lock);
1452 spin_unlock(&req->rq_lock);
1453 req->rq_nr_resend++;
1455 /* Readjust the timeout for current conditions */
1456 ptlrpc_at_set_req_timeout(req);
1458 * delay resend to give a chance to the server to get ready.
1459 * The delay is increased by 1s on every resend and is capped to
1460 * the current request timeout (i.e. obd_timeout if AT is off,
1461 * or AT service time x 125% + 5s, see at_est2timeout)
1463 if (req->rq_nr_resend > req->rq_timeout)
1464 req->rq_sent = now + req->rq_timeout;
1466 req->rq_sent = now + req->rq_nr_resend;
1468 /* Resend for EINPROGRESS will use a new XID */
1469 spin_lock(&imp->imp_lock);
1470 list_del_init(&req->rq_unreplied_list);
1471 spin_unlock(&imp->imp_lock);
1476 if (obd->obd_svc_stats) {
1477 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR,
1479 ptlrpc_lprocfs_rpc_sent(req, timediff);
1482 if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
1483 lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
1484 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
1485 lustre_msg_get_type(req->rq_repmsg));
1489 if (lustre_msg_get_opc(req->rq_reqmsg) != OBD_PING)
1490 CFS_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_PAUSE_REP, cfs_fail_val);
1491 ptlrpc_at_adj_service(req, lustre_msg_get_timeout(req->rq_repmsg));
1492 ptlrpc_at_adj_net_latency(req,
1493 lustre_msg_get_service_time(req->rq_repmsg));
1495 rc = ptlrpc_check_status(req);
1499 * Either we've been evicted, or the server has failed for
1500 * some reason. Try to reconnect, and if that fails, punt to
1503 if (ptlrpc_recoverable_error(rc)) {
1504 if (req->rq_send_state != LUSTRE_IMP_FULL ||
1505 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
1508 ptlrpc_request_handle_notconn(req);
1513 * Let's look if server sent slv. Do it only for RPC with
1516 ldlm_cli_update_pool(req);
1520 * Store transno in reqmsg for replay.
1522 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
1523 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
1524 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
1527 if (imp->imp_replayable) {
1528 spin_lock(&imp->imp_lock);
1530 * No point in adding already-committed requests to the replay
1531 * list, we will just remove them immediately. b=9829
1533 if (req->rq_transno != 0 &&
1535 lustre_msg_get_last_committed(req->rq_repmsg) ||
1537 /** version recovery */
1538 ptlrpc_save_versions(req);
1539 ptlrpc_retain_replayable_request(req, imp);
1540 } else if (req->rq_commit_cb &&
1541 list_empty(&req->rq_replay_list)) {
1543 * NB: don't call rq_commit_cb if it's already on
1544 * rq_replay_list, ptlrpc_free_committed() will call
1545 * it later, see LU-3618 for details
1547 spin_unlock(&imp->imp_lock);
1548 req->rq_commit_cb(req);
1549 spin_lock(&imp->imp_lock);
1553 * Replay-enabled imports return commit-status information.
1555 committed = lustre_msg_get_last_committed(req->rq_repmsg);
1556 if (likely(committed > imp->imp_peer_committed_transno))
1557 imp->imp_peer_committed_transno = committed;
1559 ptlrpc_free_committed(imp);
1561 if (!list_empty(&imp->imp_replay_list)) {
1562 struct ptlrpc_request *last;
1564 last = list_entry(imp->imp_replay_list.prev,
1565 struct ptlrpc_request,
1568 * Requests with rq_replay stay on the list even if no
1569 * commit is expected.
1571 if (last->rq_transno > imp->imp_peer_committed_transno)
1572 ptlrpc_pinger_commit_expected(imp);
1575 spin_unlock(&imp->imp_lock);
1582 * Helper function to send request \a req over the network for the first time
1583 * Also adjusts request phase.
1584 * Returns 0 on success or error code.
1586 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
1588 struct obd_import *imp = req->rq_import;
1593 LASSERT(req->rq_phase == RQ_PHASE_NEW);
1595 /* do not try to go further if there is not enough memory in enc_pool */
1596 if (req->rq_sent && req->rq_bulk)
1597 if (req->rq_bulk->bd_iov_count > get_free_pages_in_pool() &&
1598 pool_is_at_full_capacity())
1601 if (req->rq_sent && (req->rq_sent > ktime_get_real_seconds()) &&
1602 (!req->rq_generation_set ||
1603 req->rq_import_generation == imp->imp_generation))
1606 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
1608 spin_lock(&imp->imp_lock);
1610 LASSERT(req->rq_xid != 0);
1611 LASSERT(!list_empty(&req->rq_unreplied_list));
1613 if (!req->rq_generation_set)
1614 req->rq_import_generation = imp->imp_generation;
1616 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1617 spin_lock(&req->rq_lock);
1618 req->rq_waiting = 1;
1619 spin_unlock(&req->rq_lock);
1621 DEBUG_REQ(D_HA, req, "req waiting for recovery: (%s != %s)",
1622 ptlrpc_import_state_name(req->rq_send_state),
1623 ptlrpc_import_state_name(imp->imp_state));
1624 LASSERT(list_empty(&req->rq_list));
1625 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1626 atomic_inc(&req->rq_import->imp_inflight);
1627 spin_unlock(&imp->imp_lock);
1632 spin_unlock(&imp->imp_lock);
1633 req->rq_status = rc;
1634 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1638 LASSERT(list_empty(&req->rq_list));
1639 list_add_tail(&req->rq_list, &imp->imp_sending_list);
1640 atomic_inc(&req->rq_import->imp_inflight);
1643 * find the known replied XID from the unreplied list, CONNECT
1644 * and DISCONNECT requests are skipped to make the sanity check
1645 * on server side happy. see process_req_last_xid().
1647 * For CONNECT: Because replay requests have lower XID, it'll
1648 * break the sanity check if CONNECT bump the exp_last_xid on
1651 * For DISCONNECT: Since client will abort inflight RPC before
1652 * sending DISCONNECT, DISCONNECT may carry an XID which higher
1653 * than the inflight RPC.
1655 if (!ptlrpc_req_is_connect(req) && !ptlrpc_req_is_disconnect(req))
1656 min_xid = ptlrpc_known_replied_xid(imp);
1657 spin_unlock(&imp->imp_lock);
1659 lustre_msg_set_last_xid(req->rq_reqmsg, min_xid);
1661 lustre_msg_set_status(req->rq_reqmsg, current_pid());
1663 rc = sptlrpc_req_refresh_ctx(req, -1);
1666 req->rq_status = rc;
1669 spin_lock(&req->rq_lock);
1670 req->rq_wait_ctx = 1;
1671 spin_unlock(&req->rq_lock);
1677 "Sending RPC pname:cluuid:pid:xid:nid:opc %s:%s:%d:%llu:%s:%d\n",
1679 imp->imp_obd->obd_uuid.uuid,
1680 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1681 obd_import_nid2str(imp), lustre_msg_get_opc(req->rq_reqmsg));
1683 rc = ptl_send_rpc(req, 0);
1684 if (rc == -ENOMEM) {
1685 spin_lock(&imp->imp_lock);
1686 if (!list_empty(&req->rq_list)) {
1687 list_del_init(&req->rq_list);
1688 atomic_dec(&req->rq_import->imp_inflight);
1690 spin_unlock(&imp->imp_lock);
1691 ptlrpc_rqphase_move(req, RQ_PHASE_NEW);
1695 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
1696 spin_lock(&req->rq_lock);
1697 req->rq_net_err = 1;
1698 spin_unlock(&req->rq_lock);
1704 static inline int ptlrpc_set_producer(struct ptlrpc_request_set *set)
1709 LASSERT(set->set_producer != NULL);
1711 remaining = atomic_read(&set->set_remaining);
1714 * populate the ->set_requests list with requests until we
1715 * reach the maximum number of RPCs in flight for this set
1717 while (atomic_read(&set->set_remaining) < set->set_max_inflight) {
1718 rc = set->set_producer(set, set->set_producer_arg);
1719 if (rc == -ENOENT) {
1720 /* no more RPC to produce */
1721 set->set_producer = NULL;
1722 set->set_producer_arg = NULL;
1727 RETURN((atomic_read(&set->set_remaining) - remaining));
1731 * this sends any unsent RPCs in \a set and returns 1 if all are sent
1732 * and no more replies are expected.
1733 * (it is possible to get less replies than requests sent e.g. due to timed out
1734 * requests or requests that we had trouble to send out)
1736 * NOTE: This function contains a potential schedule point (cond_resched()).
1738 int ptlrpc_check_set(const struct lu_env *env, struct ptlrpc_request_set *set)
1740 struct list_head *tmp, *next;
1741 struct list_head comp_reqs;
1742 int force_timer_recalc = 0;
1745 if (atomic_read(&set->set_remaining) == 0)
1748 INIT_LIST_HEAD(&comp_reqs);
1749 list_for_each_safe(tmp, next, &set->set_requests) {
1750 struct ptlrpc_request *req =
1751 list_entry(tmp, struct ptlrpc_request,
1753 struct obd_import *imp = req->rq_import;
1754 int unregistered = 0;
1758 if (req->rq_phase == RQ_PHASE_COMPLETE) {
1759 list_move_tail(&req->rq_set_chain, &comp_reqs);
1764 * This schedule point is mainly for the ptlrpcd caller of this
1765 * function. Most ptlrpc sets are not long-lived and unbounded
1766 * in length, but at the least the set used by the ptlrpcd is.
1767 * Since the processing time is unbounded, we need to insert an
1768 * explicit schedule point to make the thread well-behaved.
1773 * If the caller requires to allow to be interpreted by force
1774 * and it has really been interpreted, then move the request
1775 * to RQ_PHASE_INTERPRET phase in spite of what the current
1778 if (unlikely(req->rq_allow_intr && req->rq_intr)) {
1779 req->rq_status = -EINTR;
1780 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1783 * Since it is interpreted and we have to wait for
1784 * the reply to be unlinked, then use sync mode.
1788 GOTO(interpret, req->rq_status);
1791 if (req->rq_phase == RQ_PHASE_NEW && ptlrpc_send_new_req(req))
1792 force_timer_recalc = 1;
1794 /* delayed send - skip */
1795 if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
1798 /* delayed resend - skip */
1799 if (req->rq_phase == RQ_PHASE_RPC && req->rq_resend &&
1800 req->rq_sent > ktime_get_real_seconds())
1803 if (!(req->rq_phase == RQ_PHASE_RPC ||
1804 req->rq_phase == RQ_PHASE_BULK ||
1805 req->rq_phase == RQ_PHASE_INTERPRET ||
1806 req->rq_phase == RQ_PHASE_UNREG_RPC ||
1807 req->rq_phase == RQ_PHASE_UNREG_BULK)) {
1808 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
1812 if (req->rq_phase == RQ_PHASE_UNREG_RPC ||
1813 req->rq_phase == RQ_PHASE_UNREG_BULK) {
1814 LASSERT(req->rq_next_phase != req->rq_phase);
1815 LASSERT(req->rq_next_phase != RQ_PHASE_UNDEFINED);
1817 if (req->rq_req_deadline &&
1818 !OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REQ_UNLINK))
1819 req->rq_req_deadline = 0;
1820 if (req->rq_reply_deadline &&
1821 !OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK))
1822 req->rq_reply_deadline = 0;
1823 if (req->rq_bulk_deadline &&
1824 !OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK))
1825 req->rq_bulk_deadline = 0;
1828 * Skip processing until reply is unlinked. We
1829 * can't return to pool before that and we can't
1830 * call interpret before that. We need to make
1831 * sure that all rdma transfers finished and will
1832 * not corrupt any data.
1834 if (req->rq_phase == RQ_PHASE_UNREG_RPC &&
1835 ptlrpc_client_recv_or_unlink(req))
1837 if (req->rq_phase == RQ_PHASE_UNREG_BULK &&
1838 ptlrpc_client_bulk_active(req))
1842 * Turn fail_loc off to prevent it from looping
1845 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
1846 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK,
1849 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK)) {
1850 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK,
1855 * Move to next phase if reply was successfully
1858 ptlrpc_rqphase_move(req, req->rq_next_phase);
1861 if (req->rq_phase == RQ_PHASE_INTERPRET)
1862 GOTO(interpret, req->rq_status);
1865 * Note that this also will start async reply unlink.
1867 if (req->rq_net_err && !req->rq_timedout) {
1868 ptlrpc_expire_one_request(req, 1);
1871 * Check if we still need to wait for unlink.
1873 if (ptlrpc_client_recv_or_unlink(req) ||
1874 ptlrpc_client_bulk_active(req))
1876 /* If there is no need to resend, fail it now. */
1877 if (req->rq_no_resend) {
1878 if (req->rq_status == 0)
1879 req->rq_status = -EIO;
1880 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1881 GOTO(interpret, req->rq_status);
1888 spin_lock(&req->rq_lock);
1889 req->rq_replied = 0;
1890 spin_unlock(&req->rq_lock);
1891 if (req->rq_status == 0)
1892 req->rq_status = -EIO;
1893 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1894 GOTO(interpret, req->rq_status);
1898 * ptlrpc_set_wait->l_wait_event sets lwi_allow_intr
1899 * so it sets rq_intr regardless of individual rpc
1900 * timeouts. The synchronous IO waiting path sets
1901 * rq_intr irrespective of whether ptlrpcd
1902 * has seen a timeout. Our policy is to only interpret
1903 * interrupted rpcs after they have timed out, so we
1904 * need to enforce that here.
1907 if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
1908 req->rq_wait_ctx)) {
1909 req->rq_status = -EINTR;
1910 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1911 GOTO(interpret, req->rq_status);
1914 if (req->rq_phase == RQ_PHASE_RPC) {
1915 if (req->rq_timedout || req->rq_resend ||
1916 req->rq_waiting || req->rq_wait_ctx) {
1919 if (!ptlrpc_unregister_reply(req, 1)) {
1920 ptlrpc_unregister_bulk(req, 1);
1924 spin_lock(&imp->imp_lock);
1925 if (ptlrpc_import_delay_req(imp, req,
1928 * put on delay list - only if we wait
1929 * recovery finished - before send
1931 list_del_init(&req->rq_list);
1932 list_add_tail(&req->rq_list,
1933 &imp->imp_delayed_list);
1934 spin_unlock(&imp->imp_lock);
1939 req->rq_status = status;
1940 ptlrpc_rqphase_move(req,
1941 RQ_PHASE_INTERPRET);
1942 spin_unlock(&imp->imp_lock);
1943 GOTO(interpret, req->rq_status);
1945 /* ignore on just initiated connections */
1946 if (ptlrpc_no_resend(req) &&
1947 !req->rq_wait_ctx &&
1948 imp->imp_generation !=
1949 imp->imp_initiated_at) {
1950 req->rq_status = -ENOTCONN;
1951 ptlrpc_rqphase_move(req,
1952 RQ_PHASE_INTERPRET);
1953 spin_unlock(&imp->imp_lock);
1954 GOTO(interpret, req->rq_status);
1957 list_del_init(&req->rq_list);
1958 list_add_tail(&req->rq_list,
1959 &imp->imp_sending_list);
1961 spin_unlock(&imp->imp_lock);
1963 spin_lock(&req->rq_lock);
1964 req->rq_waiting = 0;
1965 spin_unlock(&req->rq_lock);
1967 if (req->rq_timedout || req->rq_resend) {
1969 * This is re-sending anyways,
1970 * let's mark req as resend.
1972 spin_lock(&req->rq_lock);
1974 spin_unlock(&req->rq_lock);
1977 * rq_wait_ctx is only touched by ptlrpcd,
1978 * so no lock is needed here.
1980 status = sptlrpc_req_refresh_ctx(req, -1);
1983 req->rq_status = status;
1984 spin_lock(&req->rq_lock);
1985 req->rq_wait_ctx = 0;
1986 spin_unlock(&req->rq_lock);
1987 force_timer_recalc = 1;
1989 spin_lock(&req->rq_lock);
1990 req->rq_wait_ctx = 1;
1991 spin_unlock(&req->rq_lock);
1996 spin_lock(&req->rq_lock);
1997 req->rq_wait_ctx = 0;
1998 spin_unlock(&req->rq_lock);
2002 * In any case, the previous bulk should be
2003 * cleaned up to prepare for the new sending
2006 !ptlrpc_unregister_bulk(req, 1))
2009 rc = ptl_send_rpc(req, 0);
2010 if (rc == -ENOMEM) {
2011 spin_lock(&imp->imp_lock);
2012 if (!list_empty(&req->rq_list))
2013 list_del_init(&req->rq_list);
2014 spin_unlock(&imp->imp_lock);
2015 ptlrpc_rqphase_move(req, RQ_PHASE_NEW);
2019 DEBUG_REQ(D_HA, req,
2020 "send failed: rc = %d", rc);
2021 force_timer_recalc = 1;
2022 spin_lock(&req->rq_lock);
2023 req->rq_net_err = 1;
2024 spin_unlock(&req->rq_lock);
2027 /* need to reset the timeout */
2028 force_timer_recalc = 1;
2031 spin_lock(&req->rq_lock);
2033 if (ptlrpc_client_early(req)) {
2034 ptlrpc_at_recv_early_reply(req);
2035 spin_unlock(&req->rq_lock);
2039 /* Still waiting for a reply? */
2040 if (ptlrpc_client_recv(req)) {
2041 spin_unlock(&req->rq_lock);
2045 /* Did we actually receive a reply? */
2046 if (!ptlrpc_client_replied(req)) {
2047 spin_unlock(&req->rq_lock);
2051 spin_unlock(&req->rq_lock);
2054 * unlink from net because we are going to
2055 * swab in-place of reply buffer
2057 unregistered = ptlrpc_unregister_reply(req, 1);
2061 req->rq_status = after_reply(req);
2066 * If there is no bulk associated with this request,
2067 * then we're done and should let the interpreter
2068 * process the reply. Similarly if the RPC returned
2069 * an error, and therefore the bulk will never arrive.
2071 if (!req->rq_bulk || req->rq_status < 0) {
2072 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
2073 GOTO(interpret, req->rq_status);
2076 ptlrpc_rqphase_move(req, RQ_PHASE_BULK);
2079 LASSERT(req->rq_phase == RQ_PHASE_BULK);
2080 if (ptlrpc_client_bulk_active(req))
2083 if (req->rq_bulk->bd_failure) {
2085 * The RPC reply arrived OK, but the bulk screwed
2086 * up! Dead weird since the server told us the RPC
2087 * was good after getting the REPLY for her GET or
2088 * the ACK for her PUT.
2090 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
2091 req->rq_status = -EIO;
2094 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
2097 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
2100 * This moves to "unregistering" phase we need to wait for
2103 if (!unregistered && !ptlrpc_unregister_reply(req, async)) {
2104 /* start async bulk unlink too */
2105 ptlrpc_unregister_bulk(req, 1);
2109 if (!ptlrpc_unregister_bulk(req, async))
2113 * When calling interpret receiving already should be
2116 LASSERT(!req->rq_receiving_reply);
2118 ptlrpc_req_interpret(env, req, req->rq_status);
2120 if (ptlrpcd_check_work(req)) {
2121 atomic_dec(&set->set_remaining);
2124 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
2128 "Completed RPC pname:cluuid:pid:xid:nid:opc %s:%s:%d:%llu:%s:%d\n",
2130 imp->imp_obd->obd_uuid.uuid,
2131 lustre_msg_get_status(req->rq_reqmsg),
2133 obd_import_nid2str(imp),
2134 lustre_msg_get_opc(req->rq_reqmsg));
2136 spin_lock(&imp->imp_lock);
2138 * Request already may be not on sending or delaying list. This
2139 * may happen in the case of marking it erroneous for the case
2140 * ptlrpc_import_delay_req(req, status) find it impossible to
2141 * allow sending this rpc and returns *status != 0.
2143 if (!list_empty(&req->rq_list)) {
2144 list_del_init(&req->rq_list);
2145 atomic_dec(&imp->imp_inflight);
2147 list_del_init(&req->rq_unreplied_list);
2148 spin_unlock(&imp->imp_lock);
2150 atomic_dec(&set->set_remaining);
2151 wake_up_all(&imp->imp_recovery_waitq);
2153 if (set->set_producer) {
2154 /* produce a new request if possible */
2155 if (ptlrpc_set_producer(set) > 0)
2156 force_timer_recalc = 1;
2159 * free the request that has just been completed
2160 * in order not to pollute set->set_requests
2162 list_del_init(&req->rq_set_chain);
2163 spin_lock(&req->rq_lock);
2165 req->rq_invalid_rqset = 0;
2166 spin_unlock(&req->rq_lock);
2168 /* record rq_status to compute the final status later */
2169 if (req->rq_status != 0)
2170 set->set_rc = req->rq_status;
2171 ptlrpc_req_finished(req);
2173 list_move_tail(&req->rq_set_chain, &comp_reqs);
2178 * move completed request at the head of list so it's easier for
2179 * caller to find them
2181 list_splice(&comp_reqs, &set->set_requests);
2183 /* If we hit an error, we want to recover promptly. */
2184 RETURN(atomic_read(&set->set_remaining) == 0 || force_timer_recalc);
2186 EXPORT_SYMBOL(ptlrpc_check_set);
2189 * Time out request \a req. is \a async_unlink is set, that means do not wait
2190 * until LNet actually confirms network buffer unlinking.
2191 * Return 1 if we should give up further retrying attempts or 0 otherwise.
2193 int ptlrpc_expire_one_request(struct ptlrpc_request *req, int async_unlink)
2195 struct obd_import *imp = req->rq_import;
2196 unsigned int debug_mask = D_RPCTRACE;
2200 spin_lock(&req->rq_lock);
2201 req->rq_timedout = 1;
2202 spin_unlock(&req->rq_lock);
2204 if (ptlrpc_console_allow(req, lustre_msg_get_opc(req->rq_reqmsg),
2205 lustre_msg_get_status(req->rq_reqmsg)))
2206 debug_mask = D_WARNING;
2207 DEBUG_REQ(debug_mask, req, "Request sent has %s: [sent %lld/real %lld]",
2208 req->rq_net_err ? "failed due to network error" :
2209 ((req->rq_real_sent == 0 ||
2210 req->rq_real_sent < req->rq_sent ||
2211 req->rq_real_sent >= req->rq_deadline) ?
2212 "timed out for sent delay" : "timed out for slow reply"),
2213 (s64)req->rq_sent, (s64)req->rq_real_sent);
2215 if (imp && obd_debug_peer_on_timeout)
2216 LNetDebugPeer(imp->imp_connection->c_peer);
2218 ptlrpc_unregister_reply(req, async_unlink);
2219 ptlrpc_unregister_bulk(req, async_unlink);
2221 if (obd_dump_on_timeout)
2222 libcfs_debug_dumplog();
2225 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
2229 atomic_inc(&imp->imp_timeouts);
2231 /* The DLM server doesn't want recovery run on its imports. */
2232 if (imp->imp_dlm_fake)
2236 * If this request is for recovery or other primordial tasks,
2237 * then error it out here.
2239 if (req->rq_ctx_init || req->rq_ctx_fini ||
2240 req->rq_send_state != LUSTRE_IMP_FULL ||
2241 imp->imp_obd->obd_no_recov) {
2242 DEBUG_REQ(D_RPCTRACE, req, "err -110, sent_state=%s (now=%s)",
2243 ptlrpc_import_state_name(req->rq_send_state),
2244 ptlrpc_import_state_name(imp->imp_state));
2245 spin_lock(&req->rq_lock);
2246 req->rq_status = -ETIMEDOUT;
2248 spin_unlock(&req->rq_lock);
2253 * if a request can't be resent we can't wait for an answer after
2256 if (ptlrpc_no_resend(req)) {
2257 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
2261 ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
2267 * Time out all uncompleted requests in request set pointed by \a data
2268 * Callback used when waiting on sets with l_wait_event.
2271 int ptlrpc_expired_set(void *data)
2273 struct ptlrpc_request_set *set = data;
2274 struct list_head *tmp;
2275 time64_t now = ktime_get_real_seconds();
2278 LASSERT(set != NULL);
2281 * A timeout expired. See which reqs it applies to...
2283 list_for_each(tmp, &set->set_requests) {
2284 struct ptlrpc_request *req =
2285 list_entry(tmp, struct ptlrpc_request,
2288 /* don't expire request waiting for context */
2289 if (req->rq_wait_ctx)
2292 /* Request in-flight? */
2293 if (!((req->rq_phase == RQ_PHASE_RPC &&
2294 !req->rq_waiting && !req->rq_resend) ||
2295 (req->rq_phase == RQ_PHASE_BULK)))
2298 if (req->rq_timedout || /* already dealt with */
2299 req->rq_deadline > now) /* not expired */
2303 * Deal with this guy. Do it asynchronously to not block
2306 ptlrpc_expire_one_request(req, 1);
2310 * When waiting for a whole set, we always break out of the
2311 * sleep so we can recalculate the timeout, or enable interrupts
2312 * if everyone's timed out.
2318 * Sets rq_intr flag in \a req under spinlock.
2320 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
2322 spin_lock(&req->rq_lock);
2324 spin_unlock(&req->rq_lock);
2326 EXPORT_SYMBOL(ptlrpc_mark_interrupted);
2329 * Interrupts (sets interrupted flag) all uncompleted requests in
2330 * a set \a data. Callback for l_wait_event for interruptible waits.
2332 static void ptlrpc_interrupted_set(void *data)
2334 struct ptlrpc_request_set *set = data;
2335 struct list_head *tmp;
2337 LASSERT(set != NULL);
2338 CDEBUG(D_RPCTRACE, "INTERRUPTED SET %p\n", set);
2340 list_for_each(tmp, &set->set_requests) {
2341 struct ptlrpc_request *req =
2342 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
2347 if (req->rq_phase != RQ_PHASE_RPC &&
2348 req->rq_phase != RQ_PHASE_UNREG_RPC &&
2349 !req->rq_allow_intr)
2352 ptlrpc_mark_interrupted(req);
2357 * Get the smallest timeout in the set; this does NOT set a timeout.
2359 time64_t ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
2361 struct list_head *tmp;
2362 time64_t now = ktime_get_real_seconds();
2364 struct ptlrpc_request *req;
2368 list_for_each(tmp, &set->set_requests) {
2369 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
2371 /* Request in-flight? */
2372 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
2373 (req->rq_phase == RQ_PHASE_BULK) ||
2374 (req->rq_phase == RQ_PHASE_NEW)))
2377 /* Already timed out. */
2378 if (req->rq_timedout)
2381 /* Waiting for ctx. */
2382 if (req->rq_wait_ctx)
2385 if (req->rq_phase == RQ_PHASE_NEW)
2386 deadline = req->rq_sent;
2387 else if (req->rq_phase == RQ_PHASE_RPC && req->rq_resend)
2388 deadline = req->rq_sent;
2390 deadline = req->rq_sent + req->rq_timeout;
2392 if (deadline <= now) /* actually expired already */
2393 timeout = 1; /* ASAP */
2394 else if (timeout == 0 || timeout > deadline - now)
2395 timeout = deadline - now;
2401 * Send all unset request from the set and then wait untill all
2402 * requests in the set complete (either get a reply, timeout, get an
2403 * error or otherwise be interrupted).
2404 * Returns 0 on success or error code otherwise.
2406 int ptlrpc_set_wait(const struct lu_env *env, struct ptlrpc_request_set *set)
2408 struct list_head *tmp;
2409 struct ptlrpc_request *req;
2410 struct l_wait_info lwi;
2415 if (set->set_producer)
2416 (void)ptlrpc_set_producer(set);
2418 list_for_each(tmp, &set->set_requests) {
2419 req = list_entry(tmp, struct ptlrpc_request,
2421 if (req->rq_phase == RQ_PHASE_NEW)
2422 (void)ptlrpc_send_new_req(req);
2425 if (list_empty(&set->set_requests))
2429 timeout = ptlrpc_set_next_timeout(set);
2432 * wait until all complete, interrupted, or an in-flight
2435 CDEBUG(D_RPCTRACE, "set %p going to sleep for %lld seconds\n",
2438 if ((timeout == 0 && !signal_pending(current)) ||
2439 set->set_allow_intr)
2441 * No requests are in-flight (ether timed out
2442 * or delayed), so we can allow interrupts.
2443 * We still want to block for a limited time,
2444 * so we allow interrupts during the timeout.
2446 lwi = LWI_TIMEOUT_INTR_ALL(
2447 cfs_time_seconds(timeout ? timeout : 1),
2449 ptlrpc_interrupted_set, set);
2452 * At least one request is in flight, so no
2453 * interrupts are allowed. Wait until all
2454 * complete, or an in-flight req times out.
2456 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout ? timeout : 1),
2457 ptlrpc_expired_set, set);
2459 rc = l_wait_event(set->set_waitq,
2460 ptlrpc_check_set(NULL, set), &lwi);
2463 * LU-769 - if we ignored the signal because it was already
2464 * pending when we started, we need to handle it now or we risk
2465 * it being ignored forever
2467 if (rc == -ETIMEDOUT &&
2468 (!lwi.lwi_allow_intr || set->set_allow_intr) &&
2469 signal_pending(current)) {
2470 sigset_t blocked_sigs =
2471 cfs_block_sigsinv(LUSTRE_FATAL_SIGS);
2474 * In fact we only interrupt for the "fatal" signals
2475 * like SIGINT or SIGKILL. We still ignore less
2476 * important signals since ptlrpc set is not easily
2477 * reentrant from userspace again
2479 if (signal_pending(current))
2480 ptlrpc_interrupted_set(set);
2481 cfs_restore_sigs(blocked_sigs);
2484 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
2487 * -EINTR => all requests have been flagged rq_intr so next
2489 * -ETIMEDOUT => someone timed out. When all reqs have
2490 * timed out, signals are enabled allowing completion with
2492 * I don't really care if we go once more round the loop in
2493 * the error cases -eeb.
2495 if (rc == 0 && atomic_read(&set->set_remaining) == 0) {
2496 list_for_each(tmp, &set->set_requests) {
2497 req = list_entry(tmp, struct ptlrpc_request,
2499 spin_lock(&req->rq_lock);
2500 req->rq_invalid_rqset = 1;
2501 spin_unlock(&req->rq_lock);
2504 } while (rc != 0 || atomic_read(&set->set_remaining) != 0);
2506 LASSERT(atomic_read(&set->set_remaining) == 0);
2508 rc = set->set_rc; /* rq_status of already freed requests if any */
2509 list_for_each(tmp, &set->set_requests) {
2510 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
2512 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
2513 if (req->rq_status != 0)
2514 rc = req->rq_status;
2519 EXPORT_SYMBOL(ptlrpc_set_wait);
2522 * Helper fuction for request freeing.
2523 * Called when request count reached zero and request needs to be freed.
2524 * Removes request from all sorts of sending/replay lists it might be on,
2525 * frees network buffers if any are present.
2526 * If \a locked is set, that means caller is already holding import imp_lock
2527 * and so we no longer need to reobtain it (for certain lists manipulations)
2529 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
2536 LASSERT(!request->rq_srv_req);
2537 LASSERT(request->rq_export == NULL);
2538 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
2539 LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
2540 LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
2541 LASSERTF(!request->rq_replay, "req %p\n", request);
2543 req_capsule_fini(&request->rq_pill);
2546 * We must take it off the imp_replay_list first. Otherwise, we'll set
2547 * request->rq_reqmsg to NULL while osc_close is dereferencing it.
2549 if (request->rq_import) {
2551 spin_lock(&request->rq_import->imp_lock);
2552 list_del_init(&request->rq_replay_list);
2553 list_del_init(&request->rq_unreplied_list);
2555 spin_unlock(&request->rq_import->imp_lock);
2557 LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
2559 if (atomic_read(&request->rq_refcount) != 0) {
2560 DEBUG_REQ(D_ERROR, request,
2561 "freeing request with nonzero refcount");
2565 if (request->rq_repbuf)
2566 sptlrpc_cli_free_repbuf(request);
2568 if (request->rq_import) {
2569 class_import_put(request->rq_import);
2570 request->rq_import = NULL;
2572 if (request->rq_bulk)
2573 ptlrpc_free_bulk(request->rq_bulk);
2575 if (request->rq_reqbuf || request->rq_clrbuf)
2576 sptlrpc_cli_free_reqbuf(request);
2578 if (request->rq_cli_ctx)
2579 sptlrpc_req_put_ctx(request, !locked);
2581 if (request->rq_pool)
2582 __ptlrpc_free_req_to_pool(request);
2584 ptlrpc_request_cache_free(request);
2588 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
2590 * Drop one request reference. Must be called with import imp_lock held.
2591 * When reference count drops to zero, request is freed.
2593 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
2595 assert_spin_locked(&request->rq_import->imp_lock);
2596 (void)__ptlrpc_req_finished(request, 1);
2601 * Drops one reference count for request \a request.
2602 * \a locked set indicates that caller holds import imp_lock.
2603 * Frees the request whe reference count reaches zero.
2605 * \retval 1 the request is freed
2606 * \retval 0 some others still hold references on the request
2608 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
2616 LASSERT(request != LP_POISON);
2617 LASSERT(request->rq_reqmsg != LP_POISON);
2619 DEBUG_REQ(D_INFO, request, "refcount now %u",
2620 atomic_read(&request->rq_refcount) - 1);
2622 spin_lock(&request->rq_lock);
2623 count = atomic_dec_return(&request->rq_refcount);
2624 LASSERTF(count >= 0, "Invalid ref count %d\n", count);
2627 * For open RPC, the client does not know the EA size (LOV, ACL, and
2628 * so on) before replied, then the client has to reserve very large
2629 * reply buffer. Such buffer will not be released until the RPC freed.
2630 * Since The open RPC is replayable, we need to keep it in the replay
2631 * list until close. If there are a lot of files opened concurrently,
2632 * then the client may be OOM.
2634 * If fact, it is unnecessary to keep reply buffer for open replay,
2635 * related EAs have already been saved via mdc_save_lovea() before
2636 * coming here. So it is safe to free the reply buffer some earlier
2637 * before releasing the RPC to avoid client OOM. LU-9514
2639 if (count == 1 && request->rq_early_free_repbuf && request->rq_repbuf) {
2640 spin_lock(&request->rq_early_free_lock);
2641 sptlrpc_cli_free_repbuf(request);
2642 request->rq_repbuf = NULL;
2643 request->rq_repbuf_len = 0;
2644 request->rq_repdata = NULL;
2645 request->rq_reqdata_len = 0;
2646 spin_unlock(&request->rq_early_free_lock);
2648 spin_unlock(&request->rq_lock);
2651 __ptlrpc_free_req(request, locked);
2657 * Drops one reference count for a request.
2659 void ptlrpc_req_finished(struct ptlrpc_request *request)
2661 __ptlrpc_req_finished(request, 0);
2663 EXPORT_SYMBOL(ptlrpc_req_finished);
2666 * Returns xid of a \a request
2668 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
2670 return request->rq_xid;
2672 EXPORT_SYMBOL(ptlrpc_req_xid);
2675 * Disengage the client's reply buffer from the network
2676 * NB does _NOT_ unregister any client-side bulk.
2677 * IDEMPOTENT, but _not_ safe against concurrent callers.
2678 * The request owner (i.e. the thread doing the I/O) must call...
2679 * Returns 0 on success or 1 if unregistering cannot be made.
2681 static int ptlrpc_unregister_reply(struct ptlrpc_request *request, int async)
2684 struct l_wait_info lwi;
2689 LASSERT(!in_interrupt());
2691 /* Let's setup deadline for reply unlink. */
2692 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
2693 async && request->rq_reply_deadline == 0 && cfs_fail_val == 0)
2694 request->rq_reply_deadline = ktime_get_real_seconds() +
2698 * Nothing left to do.
2700 if (!ptlrpc_client_recv_or_unlink(request))
2703 LNetMDUnlink(request->rq_reply_md_h);
2706 * Let's check it once again.
2708 if (!ptlrpc_client_recv_or_unlink(request))
2711 /* Move to "Unregistering" phase as reply was not unlinked yet. */
2712 ptlrpc_rqphase_move(request, RQ_PHASE_UNREG_RPC);
2715 * Do not wait for unlink to finish.
2721 * We have to l_wait_event() whatever the result, to give liblustre
2722 * a chance to run reply_in_callback(), and to make sure we've
2723 * unlinked before returning a req to the pool.
2726 /* The wq argument is ignored by user-space wait_event macros */
2727 wait_queue_head_t *wq = (request->rq_set) ?
2728 &request->rq_set->set_waitq :
2729 &request->rq_reply_waitq;
2731 * Network access will complete in finite time but the HUGE
2732 * timeout lets us CWARN for visibility of sluggish NALs
2734 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
2735 cfs_time_seconds(1), NULL, NULL);
2736 rc = l_wait_event(*wq, !ptlrpc_client_recv_or_unlink(request),
2739 ptlrpc_rqphase_move(request, request->rq_next_phase);
2743 LASSERT(rc == -ETIMEDOUT);
2744 DEBUG_REQ(D_WARNING, request,
2745 "Unexpectedly long timeout receiving_reply=%d req_ulinked=%d reply_unlinked=%d",
2746 request->rq_receiving_reply,
2747 request->rq_req_unlinked,
2748 request->rq_reply_unlinked);
2753 static void ptlrpc_free_request(struct ptlrpc_request *req)
2755 spin_lock(&req->rq_lock);
2757 spin_unlock(&req->rq_lock);
2759 if (req->rq_commit_cb)
2760 req->rq_commit_cb(req);
2761 list_del_init(&req->rq_replay_list);
2763 __ptlrpc_req_finished(req, 1);
2767 * the request is committed and dropped from the replay list of its import
2769 void ptlrpc_request_committed(struct ptlrpc_request *req, int force)
2771 struct obd_import *imp = req->rq_import;
2773 spin_lock(&imp->imp_lock);
2774 if (list_empty(&req->rq_replay_list)) {
2775 spin_unlock(&imp->imp_lock);
2779 if (force || req->rq_transno <= imp->imp_peer_committed_transno) {
2780 if (imp->imp_replay_cursor == &req->rq_replay_list)
2781 imp->imp_replay_cursor = req->rq_replay_list.next;
2782 ptlrpc_free_request(req);
2785 spin_unlock(&imp->imp_lock);
2787 EXPORT_SYMBOL(ptlrpc_request_committed);
2790 * Iterates through replay_list on import and prunes
2791 * all requests have transno smaller than last_committed for the
2792 * import and don't have rq_replay set.
2793 * Since requests are sorted in transno order, stops when meetign first
2794 * transno bigger than last_committed.
2795 * caller must hold imp->imp_lock
2797 void ptlrpc_free_committed(struct obd_import *imp)
2799 struct ptlrpc_request *req, *saved;
2800 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
2801 bool skip_committed_list = true;
2804 LASSERT(imp != NULL);
2805 assert_spin_locked(&imp->imp_lock);
2807 if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
2808 imp->imp_generation == imp->imp_last_generation_checked) {
2809 CDEBUG(D_INFO, "%s: skip recheck: last_committed %llu\n",
2810 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
2813 CDEBUG(D_RPCTRACE, "%s: committing for last_committed %llu gen %d\n",
2814 imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
2815 imp->imp_generation);
2817 if (imp->imp_generation != imp->imp_last_generation_checked ||
2818 imp->imp_last_transno_checked == 0)
2819 skip_committed_list = false;
2821 imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
2822 imp->imp_last_generation_checked = imp->imp_generation;
2824 list_for_each_entry_safe(req, saved, &imp->imp_replay_list,
2826 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
2827 LASSERT(req != last_req);
2830 if (req->rq_transno == 0) {
2831 DEBUG_REQ(D_EMERG, req, "zero transno during replay");
2834 if (req->rq_import_generation < imp->imp_generation) {
2835 DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
2839 /* not yet committed */
2840 if (req->rq_transno > imp->imp_peer_committed_transno) {
2841 DEBUG_REQ(D_RPCTRACE, req, "stopping search");
2845 if (req->rq_replay) {
2846 DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
2847 list_move_tail(&req->rq_replay_list,
2848 &imp->imp_committed_list);
2852 DEBUG_REQ(D_INFO, req, "commit (last_committed %llu)",
2853 imp->imp_peer_committed_transno);
2855 ptlrpc_free_request(req);
2858 if (skip_committed_list)
2861 list_for_each_entry_safe(req, saved, &imp->imp_committed_list,
2863 LASSERT(req->rq_transno != 0);
2864 if (req->rq_import_generation < imp->imp_generation ||
2866 DEBUG_REQ(D_RPCTRACE, req, "free %s open request",
2867 req->rq_import_generation <
2868 imp->imp_generation ? "stale" : "closed");
2870 if (imp->imp_replay_cursor == &req->rq_replay_list)
2871 imp->imp_replay_cursor =
2872 req->rq_replay_list.next;
2874 ptlrpc_free_request(req);
2881 void ptlrpc_cleanup_client(struct obd_import *imp)
2888 * Schedule previously sent request for resend.
2889 * For bulk requests we assign new xid (to avoid problems with
2890 * lost replies and therefore several transfers landing into same buffer
2891 * from different sending attempts).
2893 void ptlrpc_resend_req(struct ptlrpc_request *req)
2895 DEBUG_REQ(D_HA, req, "going to resend");
2896 spin_lock(&req->rq_lock);
2899 * Request got reply but linked to the import list still.
2900 * Let ptlrpc_check_set() process it.
2902 if (ptlrpc_client_replied(req)) {
2903 spin_unlock(&req->rq_lock);
2904 DEBUG_REQ(D_HA, req, "it has reply, so skip it");
2908 req->rq_status = -EAGAIN;
2911 req->rq_net_err = 0;
2912 req->rq_timedout = 0;
2914 ptlrpc_client_wake_req(req);
2915 spin_unlock(&req->rq_lock);
2918 /* XXX: this function and rq_status are currently unused */
2919 void ptlrpc_restart_req(struct ptlrpc_request *req)
2921 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
2922 req->rq_status = -ERESTARTSYS;
2924 spin_lock(&req->rq_lock);
2925 req->rq_restart = 1;
2926 req->rq_timedout = 0;
2927 ptlrpc_client_wake_req(req);
2928 spin_unlock(&req->rq_lock);
2932 * Grab additional reference on a request \a req
2934 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
2937 atomic_inc(&req->rq_refcount);
2940 EXPORT_SYMBOL(ptlrpc_request_addref);
2943 * Add a request to import replay_list.
2944 * Must be called under imp_lock
2946 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
2947 struct obd_import *imp)
2949 struct list_head *tmp;
2951 assert_spin_locked(&imp->imp_lock);
2953 if (req->rq_transno == 0) {
2954 DEBUG_REQ(D_EMERG, req, "saving request with zero transno");
2959 * clear this for new requests that were resent as well
2960 * as resent replayed requests.
2962 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2964 /* don't re-add requests that have been replayed */
2965 if (!list_empty(&req->rq_replay_list))
2968 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
2970 spin_lock(&req->rq_lock);
2972 spin_unlock(&req->rq_lock);
2974 LASSERT(imp->imp_replayable);
2975 /* Balanced in ptlrpc_free_committed, usually. */
2976 ptlrpc_request_addref(req);
2977 list_for_each_prev(tmp, &imp->imp_replay_list) {
2978 struct ptlrpc_request *iter = list_entry(tmp,
2979 struct ptlrpc_request,
2983 * We may have duplicate transnos if we create and then
2984 * open a file, or for closes retained if to match creating
2985 * opens, so use req->rq_xid as a secondary key.
2986 * (See bugs 684, 685, and 428.)
2987 * XXX no longer needed, but all opens need transnos!
2989 if (iter->rq_transno > req->rq_transno)
2992 if (iter->rq_transno == req->rq_transno) {
2993 LASSERT(iter->rq_xid != req->rq_xid);
2994 if (iter->rq_xid > req->rq_xid)
2998 list_add(&req->rq_replay_list, &iter->rq_replay_list);
3002 list_add(&req->rq_replay_list, &imp->imp_replay_list);
3006 * Send request and wait until it completes.
3007 * Returns request processing status.
3009 int ptlrpc_queue_wait(struct ptlrpc_request *req)
3011 struct ptlrpc_request_set *set;
3015 LASSERT(req->rq_set == NULL);
3016 LASSERT(!req->rq_receiving_reply);
3018 set = ptlrpc_prep_set();
3020 CERROR("cannot allocate ptlrpc set: rc = %d\n", -ENOMEM);
3024 /* for distributed debugging */
3025 lustre_msg_set_status(req->rq_reqmsg, current_pid());
3027 /* add a ref for the set (see comment in ptlrpc_set_add_req) */
3028 ptlrpc_request_addref(req);
3029 ptlrpc_set_add_req(set, req);
3030 rc = ptlrpc_set_wait(NULL, set);
3031 ptlrpc_set_destroy(set);
3035 EXPORT_SYMBOL(ptlrpc_queue_wait);
3038 * Callback used for replayed requests reply processing.
3039 * In case of successful reply calls registered request replay callback.
3040 * In case of error restart replay process.
3042 static int ptlrpc_replay_interpret(const struct lu_env *env,
3043 struct ptlrpc_request *req,
3046 struct ptlrpc_replay_async_args *aa = args;
3047 struct obd_import *imp = req->rq_import;
3050 atomic_dec(&imp->imp_replay_inflight);
3053 * Note: if it is bulk replay (MDS-MDS replay), then even if
3054 * server got the request, but bulk transfer timeout, let's
3055 * replay the bulk req again
3057 if (!ptlrpc_client_replied(req) ||
3059 lustre_msg_get_status(req->rq_repmsg) == -ETIMEDOUT)) {
3060 DEBUG_REQ(D_ERROR, req, "request replay timed out.\n");
3061 GOTO(out, rc = -ETIMEDOUT);
3064 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
3065 (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
3066 lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
3067 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
3069 /** VBR: check version failure */
3070 if (lustre_msg_get_status(req->rq_repmsg) == -EOVERFLOW) {
3071 /** replay was failed due to version mismatch */
3072 DEBUG_REQ(D_WARNING, req, "Version mismatch during replay\n");
3073 spin_lock(&imp->imp_lock);
3074 imp->imp_vbr_failed = 1;
3075 spin_unlock(&imp->imp_lock);
3076 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
3078 /** The transno had better not change over replay. */
3079 LASSERTF(lustre_msg_get_transno(req->rq_reqmsg) ==
3080 lustre_msg_get_transno(req->rq_repmsg) ||
3081 lustre_msg_get_transno(req->rq_repmsg) == 0,
3083 lustre_msg_get_transno(req->rq_reqmsg),
3084 lustre_msg_get_transno(req->rq_repmsg));
3087 spin_lock(&imp->imp_lock);
3088 imp->imp_last_replay_transno = lustre_msg_get_transno(req->rq_reqmsg);
3089 spin_unlock(&imp->imp_lock);
3090 LASSERT(imp->imp_last_replay_transno);
3092 /* transaction number shouldn't be bigger than the latest replayed */
3093 if (req->rq_transno > lustre_msg_get_transno(req->rq_reqmsg)) {
3094 DEBUG_REQ(D_ERROR, req,
3095 "Reported transno %llu is bigger than the replayed one: %llu",
3097 lustre_msg_get_transno(req->rq_reqmsg));
3098 GOTO(out, rc = -EINVAL);
3101 DEBUG_REQ(D_HA, req, "got rep");
3103 /* let the callback do fixups, possibly including in the request */
3104 if (req->rq_replay_cb)
3105 req->rq_replay_cb(req);
3107 if (ptlrpc_client_replied(req) &&
3108 lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
3109 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
3110 lustre_msg_get_status(req->rq_repmsg),
3111 aa->praa_old_status);
3114 * Note: If the replay fails for MDT-MDT recovery, let's
3115 * abort all of the following requests in the replay
3116 * and sending list, because MDT-MDT update requests
3117 * are dependent on each other, see LU-7039
3119 if (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) {
3120 struct ptlrpc_request *free_req;
3121 struct ptlrpc_request *tmp;
3123 spin_lock(&imp->imp_lock);
3124 list_for_each_entry_safe(free_req, tmp,
3125 &imp->imp_replay_list,
3127 ptlrpc_free_request(free_req);
3130 list_for_each_entry_safe(free_req, tmp,
3131 &imp->imp_committed_list,
3133 ptlrpc_free_request(free_req);
3136 list_for_each_entry_safe(free_req, tmp,
3137 &imp->imp_delayed_list,
3139 spin_lock(&free_req->rq_lock);
3140 free_req->rq_err = 1;
3141 free_req->rq_status = -EIO;
3142 ptlrpc_client_wake_req(free_req);
3143 spin_unlock(&free_req->rq_lock);
3146 list_for_each_entry_safe(free_req, tmp,
3147 &imp->imp_sending_list,
3149 spin_lock(&free_req->rq_lock);
3150 free_req->rq_err = 1;
3151 free_req->rq_status = -EIO;
3152 ptlrpc_client_wake_req(free_req);
3153 spin_unlock(&free_req->rq_lock);
3155 spin_unlock(&imp->imp_lock);
3158 /* Put it back for re-replay. */
3159 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
3163 * Errors while replay can set transno to 0, but
3164 * imp_last_replay_transno shouldn't be set to 0 anyway
3166 if (req->rq_transno == 0)
3167 CERROR("Transno is 0 during replay!\n");
3169 /* continue with recovery */
3170 rc = ptlrpc_import_recovery_state_machine(imp);
3172 req->rq_send_state = aa->praa_old_state;
3175 /* this replay failed, so restart recovery */
3176 ptlrpc_connect_import(imp);
3182 * Prepares and queues request for replay.
3183 * Adds it to ptlrpcd queue for actual sending.
3184 * Returns 0 on success.
3186 int ptlrpc_replay_req(struct ptlrpc_request *req)
3188 struct ptlrpc_replay_async_args *aa;
3192 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
3194 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
3195 aa = ptlrpc_req_async_args(req);
3196 memset(aa, 0, sizeof(*aa));
3198 /* Prepare request to be resent with ptlrpcd */
3199 aa->praa_old_state = req->rq_send_state;
3200 req->rq_send_state = LUSTRE_IMP_REPLAY;
3201 req->rq_phase = RQ_PHASE_NEW;
3202 req->rq_next_phase = RQ_PHASE_UNDEFINED;
3204 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
3206 req->rq_interpret_reply = ptlrpc_replay_interpret;
3207 /* Readjust the timeout for current conditions */
3208 ptlrpc_at_set_req_timeout(req);
3210 /* Tell server net_latency to calculate how long to wait for reply. */
3211 lustre_msg_set_service_time(req->rq_reqmsg,
3212 ptlrpc_at_get_net_latency(req));
3213 DEBUG_REQ(D_HA, req, "REPLAY");
3215 atomic_inc(&req->rq_import->imp_replay_inflight);
3216 spin_lock(&req->rq_lock);
3217 req->rq_early_free_repbuf = 0;
3218 spin_unlock(&req->rq_lock);
3219 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
3221 ptlrpcd_add_req(req);
3226 * Aborts all in-flight request on import \a imp sending and delayed lists
3228 void ptlrpc_abort_inflight(struct obd_import *imp)
3230 struct list_head *tmp, *n;
3234 * Make sure that no new requests get processed for this import.
3235 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
3236 * this flag and then putting requests on sending_list or delayed_list.
3238 spin_lock(&imp->imp_lock);
3241 * XXX locking? Maybe we should remove each request with the list
3242 * locked? Also, how do we know if the requests on the list are
3243 * being freed at this time?
3245 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
3246 struct ptlrpc_request *req = list_entry(tmp,
3247 struct ptlrpc_request,
3250 DEBUG_REQ(D_RPCTRACE, req, "inflight");
3252 spin_lock(&req->rq_lock);
3253 if (req->rq_import_generation < imp->imp_generation) {
3255 req->rq_status = -EIO;
3256 ptlrpc_client_wake_req(req);
3258 spin_unlock(&req->rq_lock);
3261 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
3262 struct ptlrpc_request *req =
3263 list_entry(tmp, struct ptlrpc_request, rq_list);
3265 DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
3267 spin_lock(&req->rq_lock);
3268 if (req->rq_import_generation < imp->imp_generation) {
3270 req->rq_status = -EIO;
3271 ptlrpc_client_wake_req(req);
3273 spin_unlock(&req->rq_lock);
3277 * Last chance to free reqs left on the replay list, but we
3278 * will still leak reqs that haven't committed.
3280 if (imp->imp_replayable)
3281 ptlrpc_free_committed(imp);
3283 spin_unlock(&imp->imp_lock);
3289 * Abort all uncompleted requests in request set \a set
3291 void ptlrpc_abort_set(struct ptlrpc_request_set *set)
3293 struct list_head *tmp, *pos;
3295 LASSERT(set != NULL);
3297 list_for_each_safe(pos, tmp, &set->set_requests) {
3298 struct ptlrpc_request *req =
3299 list_entry(pos, struct ptlrpc_request,
3302 spin_lock(&req->rq_lock);
3303 if (req->rq_phase != RQ_PHASE_RPC) {
3304 spin_unlock(&req->rq_lock);
3309 req->rq_status = -EINTR;
3310 ptlrpc_client_wake_req(req);
3311 spin_unlock(&req->rq_lock);
3316 * Initialize the XID for the node. This is common among all requests on
3317 * this node, and only requires the property that it is monotonically
3318 * increasing. It does not need to be sequential. Since this is also used
3319 * as the RDMA match bits, it is important that a single client NOT have
3320 * the same match bits for two different in-flight requests, hence we do
3321 * NOT want to have an XID per target or similar.
3323 * To avoid an unlikely collision between match bits after a client reboot
3324 * (which would deliver old data into the wrong RDMA buffer) initialize
3325 * the XID based on the current time, assuming a maximum RPC rate of 1M RPC/s.
3326 * If the time is clearly incorrect, we instead use a 62-bit random number.
3327 * In the worst case the random number will overflow 1M RPCs per second in
3328 * 9133 years, or permutations thereof.
3330 #define YEAR_2004 (1ULL << 30)
3331 void ptlrpc_init_xid(void)
3333 time64_t now = ktime_get_real_seconds();
3335 spin_lock_init(&ptlrpc_last_xid_lock);
3336 if (now < YEAR_2004) {
3337 cfs_get_random_bytes(&ptlrpc_last_xid, sizeof(ptlrpc_last_xid));
3338 ptlrpc_last_xid >>= 2;
3339 ptlrpc_last_xid |= (1ULL << 61);
3341 ptlrpc_last_xid = (__u64)now << 20;
3344 /* Need to always be aligned to a power-of-two for mutli-bulk BRW */
3345 CLASSERT((PTLRPC_BULK_OPS_COUNT & (PTLRPC_BULK_OPS_COUNT - 1)) == 0);
3346 ptlrpc_last_xid &= PTLRPC_BULK_OPS_MASK;
3350 * Increase xid and returns resulting new value to the caller.
3352 * Multi-bulk BRW RPCs consume multiple XIDs for each bulk transfer, starting
3353 * at the returned xid, up to xid + PTLRPC_BULK_OPS_COUNT - 1. The BRW RPC
3354 * itself uses the last bulk xid needed, so the server can determine the
3355 * the number of bulk transfers from the RPC XID and a bitmask. The starting
3356 * xid must align to a power-of-two value.
3358 * This is assumed to be true due to the initial ptlrpc_last_xid
3359 * value also being initialized to a power-of-two value. LU-1431
3361 __u64 ptlrpc_next_xid(void)
3365 spin_lock(&ptlrpc_last_xid_lock);
3366 next = ptlrpc_last_xid + PTLRPC_BULK_OPS_COUNT;
3367 ptlrpc_last_xid = next;
3368 spin_unlock(&ptlrpc_last_xid_lock);
3374 * If request has a new allocated XID (new request or EINPROGRESS resend),
3375 * use this XID as matchbits of bulk, otherwise allocate a new matchbits for
3376 * request to ensure previous bulk fails and avoid problems with lost replies
3377 * and therefore several transfers landing into the same buffer from different
3380 void ptlrpc_set_bulk_mbits(struct ptlrpc_request *req)
3382 struct ptlrpc_bulk_desc *bd = req->rq_bulk;
3384 LASSERT(bd != NULL);
3387 * Generate new matchbits for all resend requests, including
3390 if (req->rq_resend) {
3391 __u64 old_mbits = req->rq_mbits;
3394 * First time resend on -EINPROGRESS will generate new xid,
3395 * so we can actually use the rq_xid as rq_mbits in such case,
3396 * however, it's bit hard to distinguish such resend with a
3397 * 'resend for the -EINPROGRESS resend'. To make it simple,
3398 * we opt to generate mbits for all resend cases.
3400 if (OCD_HAS_FLAG(&bd->bd_import->imp_connect_data,
3402 req->rq_mbits = ptlrpc_next_xid();
3405 * Old version transfers rq_xid to peer as
3408 spin_lock(&req->rq_import->imp_lock);
3409 list_del_init(&req->rq_unreplied_list);
3410 ptlrpc_assign_next_xid_nolock(req);
3411 spin_unlock(&req->rq_import->imp_lock);
3412 req->rq_mbits = req->rq_xid;
3414 CDEBUG(D_HA, "resend bulk old x%llu new x%llu\n",
3415 old_mbits, req->rq_mbits);
3416 } else if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
3417 /* Request being sent first time, use xid as matchbits. */
3418 if (OCD_HAS_FLAG(&bd->bd_import->imp_connect_data, BULK_MBITS)
3419 || req->rq_mbits == 0) {
3420 req->rq_mbits = req->rq_xid;
3422 int total_md = (bd->bd_iov_count + LNET_MAX_IOV - 1) /
3424 req->rq_mbits -= total_md - 1;
3428 * Replay request, xid and matchbits have already been
3429 * correctly assigned.
3435 * For multi-bulk RPCs, rq_mbits is the last mbits needed for bulks so
3436 * that server can infer the number of bulks that were prepared,
3439 req->rq_mbits += ((bd->bd_iov_count + LNET_MAX_IOV - 1) /
3443 * Set rq_xid as rq_mbits to indicate the final bulk for the old
3444 * server which does not support OBD_CONNECT_BULK_MBITS. LU-6808.
3446 * It's ok to directly set the rq_xid here, since this xid bump
3447 * won't affect the request position in unreplied list.
3449 if (!OCD_HAS_FLAG(&bd->bd_import->imp_connect_data, BULK_MBITS))
3450 req->rq_xid = req->rq_mbits;
3454 * Get a glimpse at what next xid value might have been.
3455 * Returns possible next xid.
3457 __u64 ptlrpc_sample_next_xid(void)
3459 #if BITS_PER_LONG == 32
3460 /* need to avoid possible word tearing on 32-bit systems */
3463 spin_lock(&ptlrpc_last_xid_lock);
3464 next = ptlrpc_last_xid + PTLRPC_BULK_OPS_COUNT;
3465 spin_unlock(&ptlrpc_last_xid_lock);
3469 /* No need to lock, since returned value is racy anyways */
3470 return ptlrpc_last_xid + PTLRPC_BULK_OPS_COUNT;
3473 EXPORT_SYMBOL(ptlrpc_sample_next_xid);
3476 * Functions for operating ptlrpc workers.
3478 * A ptlrpc work is a function which will be running inside ptlrpc context.
3479 * The callback shouldn't sleep otherwise it will block that ptlrpcd thread.
3481 * 1. after a work is created, it can be used many times, that is:
3482 * handler = ptlrpcd_alloc_work();
3483 * ptlrpcd_queue_work();
3485 * queue it again when necessary:
3486 * ptlrpcd_queue_work();
3487 * ptlrpcd_destroy_work();
3488 * 2. ptlrpcd_queue_work() can be called by multiple processes meanwhile, but
3489 * it will only be queued once in any time. Also as its name implies, it may
3490 * have delay before it really runs by ptlrpcd thread.
3492 struct ptlrpc_work_async_args {
3493 int (*cb)(const struct lu_env *, void *);
3497 static void ptlrpcd_add_work_req(struct ptlrpc_request *req)
3499 /* re-initialize the req */
3500 req->rq_timeout = obd_timeout;
3501 req->rq_sent = ktime_get_real_seconds();
3502 req->rq_deadline = req->rq_sent + req->rq_timeout;
3503 req->rq_phase = RQ_PHASE_INTERPRET;
3504 req->rq_next_phase = RQ_PHASE_COMPLETE;
3505 req->rq_xid = ptlrpc_next_xid();
3506 req->rq_import_generation = req->rq_import->imp_generation;
3508 ptlrpcd_add_req(req);
3511 static int work_interpreter(const struct lu_env *env,
3512 struct ptlrpc_request *req, void *args, int rc)
3514 struct ptlrpc_work_async_args *arg = args;
3516 LASSERT(ptlrpcd_check_work(req));
3517 LASSERT(arg->cb != NULL);
3519 rc = arg->cb(env, arg->cbdata);
3521 list_del_init(&req->rq_set_chain);
3524 if (atomic_dec_return(&req->rq_refcount) > 1) {
3525 atomic_set(&req->rq_refcount, 2);
3526 ptlrpcd_add_work_req(req);
3531 static int worker_format;
3533 static int ptlrpcd_check_work(struct ptlrpc_request *req)
3535 return req->rq_pill.rc_fmt == (void *)&worker_format;
3539 * Create a work for ptlrpc.
3541 void *ptlrpcd_alloc_work(struct obd_import *imp,
3542 int (*cb)(const struct lu_env *, void *), void *cbdata)
3544 struct ptlrpc_request *req = NULL;
3545 struct ptlrpc_work_async_args *args;
3551 RETURN(ERR_PTR(-EINVAL));
3553 /* copy some code from deprecated fakereq. */
3554 req = ptlrpc_request_cache_alloc(GFP_NOFS);
3556 CERROR("ptlrpc: run out of memory!\n");
3557 RETURN(ERR_PTR(-ENOMEM));
3560 ptlrpc_cli_req_init(req);
3562 req->rq_send_state = LUSTRE_IMP_FULL;
3563 req->rq_type = PTL_RPC_MSG_REQUEST;
3564 req->rq_import = class_import_get(imp);
3565 req->rq_interpret_reply = work_interpreter;
3566 /* don't want reply */
3567 req->rq_no_delay = req->rq_no_resend = 1;
3568 req->rq_pill.rc_fmt = (void *)&worker_format;
3570 CLASSERT(sizeof(*args) <= sizeof(req->rq_async_args));
3571 args = ptlrpc_req_async_args(req);
3573 args->cbdata = cbdata;
3577 EXPORT_SYMBOL(ptlrpcd_alloc_work);
3579 void ptlrpcd_destroy_work(void *handler)
3581 struct ptlrpc_request *req = handler;
3584 ptlrpc_req_finished(req);
3586 EXPORT_SYMBOL(ptlrpcd_destroy_work);
3588 int ptlrpcd_queue_work(void *handler)
3590 struct ptlrpc_request *req = handler;
3593 * Check if the req is already being queued.
3595 * Here comes a trick: it lacks a way of checking if a req is being
3596 * processed reliably in ptlrpc. Here I have to use refcount of req
3597 * for this purpose. This is okay because the caller should use this
3598 * req as opaque data. - Jinshan
3600 LASSERT(atomic_read(&req->rq_refcount) > 0);
3601 if (atomic_inc_return(&req->rq_refcount) == 2)
3602 ptlrpcd_add_work_req(req);
3605 EXPORT_SYMBOL(ptlrpcd_queue_work);