4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 /** Implementation of client-side PortalRPC interfaces */
39 #define DEBUG_SUBSYSTEM S_RPC
43 #include <liblustre.h>
46 #include <obd_support.h>
47 #include <obd_class.h>
48 #include <lustre_lib.h>
49 #include <lustre_ha.h>
50 #include <lustre_import.h>
51 #include <lustre_req_layout.h>
53 #include "ptlrpc_internal.h"
55 static int ptlrpc_send_new_req(struct ptlrpc_request *req);
58 * Initialize passed in client structure \a cl.
60 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
61 struct ptlrpc_client *cl)
63 cl->cli_request_portal = req_portal;
64 cl->cli_reply_portal = rep_portal;
69 * Return PortalRPC connection for remore uud \a uuid
71 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
73 struct ptlrpc_connection *c;
75 lnet_process_id_t peer;
78 err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
80 CNETERR("cannot find peer %s!\n", uuid->uuid);
84 c = ptlrpc_connection_get(peer, self, uuid);
86 memcpy(c->c_remote_uuid.uuid,
87 uuid->uuid, sizeof(c->c_remote_uuid.uuid));
90 CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
96 * Allocate and initialize new bulk descriptor
97 * Returns pointer to the descriptor or NULL on error.
99 struct ptlrpc_bulk_desc *new_bulk(int npages, int type, int portal)
101 struct ptlrpc_bulk_desc *desc;
103 OBD_ALLOC(desc, offsetof (struct ptlrpc_bulk_desc, bd_iov[npages]));
107 cfs_spin_lock_init(&desc->bd_lock);
108 cfs_waitq_init(&desc->bd_waitq);
109 desc->bd_max_iov = npages;
110 desc->bd_iov_count = 0;
111 LNetInvalidateHandle(&desc->bd_md_h);
112 desc->bd_portal = portal;
113 desc->bd_type = type;
119 * Prepare bulk descriptor for specified outgoing request \a req that
120 * can fit \a npages * pages. \a type is bulk type. \a portal is where
121 * the bulk to be sent. Used on client-side.
122 * Returns pointer to newly allocatrd initialized bulk descriptor or NULL on
125 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp(struct ptlrpc_request *req,
126 int npages, int type, int portal)
128 struct obd_import *imp = req->rq_import;
129 struct ptlrpc_bulk_desc *desc;
132 LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
133 desc = new_bulk(npages, type, portal);
137 desc->bd_import_generation = req->rq_import_generation;
138 desc->bd_import = class_import_get(imp);
141 desc->bd_cbid.cbid_fn = client_bulk_callback;
142 desc->bd_cbid.cbid_arg = desc;
144 /* This makes req own desc, and free it when she frees herself */
151 * Add a page \a page to the bulk descriptor \a desc.
152 * Data to transfer in the page starts at offset \a pageoffset and
153 * amount of data to transfer from the page is \a len
155 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
156 cfs_page_t *page, int pageoffset, int len)
158 LASSERT(desc->bd_iov_count < desc->bd_max_iov);
159 LASSERT(page != NULL);
160 LASSERT(pageoffset >= 0);
162 LASSERT(pageoffset + len <= CFS_PAGE_SIZE);
167 ptlrpc_add_bulk_page(desc, page, pageoffset, len);
171 * Uninitialize and free bulk descriptor \a desc.
172 * Works on bulk descriptors both from server and client side.
174 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
179 LASSERT(desc != NULL);
180 LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
181 LASSERT(!desc->bd_network_rw); /* network hands off or */
182 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
184 sptlrpc_enc_pool_put_pages(desc);
187 class_export_put(desc->bd_export);
189 class_import_put(desc->bd_import);
191 for (i = 0; i < desc->bd_iov_count ; i++)
192 cfs_page_unpin(desc->bd_iov[i].kiov_page);
194 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
195 bd_iov[desc->bd_max_iov]));
200 * Set server timelimit for this req, i.e. how long are we willing to wait
201 * for reply before timing out this request.
203 void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req)
209 LASSERT(req->rq_import);
212 /* non-AT settings */
214 * \a imp_server_timeout means this is reverse import and
215 * we send (currently only) ASTs to the client and cannot afford
216 * to wait too long for the reply, otherwise the other client
217 * (because of which we are sending this request) would
218 * timeout waiting for us
220 req->rq_timeout = req->rq_import->imp_server_timeout ?
221 obd_timeout / 2 : obd_timeout;
223 at = &req->rq_import->imp_at;
224 idx = import_at_get_index(req->rq_import,
225 req->rq_request_portal);
226 serv_est = at_get(&at->iat_service_estimate[idx]);
227 req->rq_timeout = at_est2timeout(serv_est);
229 /* We could get even fancier here, using history to predict increased
232 /* Let the server know what this RPC timeout is by putting it in the
234 lustre_msg_set_timeout(req->rq_reqmsg, req->rq_timeout);
237 /* Adjust max service estimate based on server value */
238 static void ptlrpc_at_adj_service(struct ptlrpc_request *req,
239 unsigned int serv_est)
245 LASSERT(req->rq_import);
246 at = &req->rq_import->imp_at;
248 idx = import_at_get_index(req->rq_import, req->rq_request_portal);
249 /* max service estimates are tracked on the server side,
250 so just keep minimal history here */
251 oldse = at_measured(&at->iat_service_estimate[idx], serv_est);
253 CDEBUG(D_ADAPTTO, "The RPC service estimate for %s ptl %d "
254 "has changed from %d to %d\n",
255 req->rq_import->imp_obd->obd_name,req->rq_request_portal,
256 oldse, at_get(&at->iat_service_estimate[idx]));
259 /* Expected network latency per remote node (secs) */
260 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req)
262 return AT_OFF ? 0 : at_get(&req->rq_import->imp_at.iat_net_latency);
265 /* Adjust expected network latency */
266 static void ptlrpc_at_adj_net_latency(struct ptlrpc_request *req,
267 unsigned int service_time)
269 unsigned int nl, oldnl;
271 time_t now = cfs_time_current_sec();
273 LASSERT(req->rq_import);
274 at = &req->rq_import->imp_at;
276 /* Network latency is total time less server processing time */
277 nl = max_t(int, now - req->rq_sent - service_time, 0) +1/*st rounding*/;
278 if (service_time > now - req->rq_sent + 3 /* bz16408 */)
279 CWARN("Reported service time %u > total measured time "
280 CFS_DURATION_T"\n", service_time,
281 cfs_time_sub(now, req->rq_sent));
283 oldnl = at_measured(&at->iat_net_latency, nl);
285 CDEBUG(D_ADAPTTO, "The network latency for %s (nid %s) "
286 "has changed from %d to %d\n",
287 req->rq_import->imp_obd->obd_name,
289 &req->rq_import->imp_connection->c_remote_uuid),
290 oldnl, at_get(&at->iat_net_latency));
293 static int unpack_reply(struct ptlrpc_request *req)
297 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
298 rc = ptlrpc_unpack_rep_msg(req, req->rq_replen);
300 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
305 rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
307 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
314 * Handle an early reply message, called with the rq_lock held.
315 * If anything goes wrong just ignore it - same as if it never happened
317 static int ptlrpc_at_recv_early_reply(struct ptlrpc_request *req)
319 struct ptlrpc_request *early_req;
325 cfs_spin_unlock(&req->rq_lock);
327 rc = sptlrpc_cli_unwrap_early_reply(req, &early_req);
329 cfs_spin_lock(&req->rq_lock);
333 rc = unpack_reply(early_req);
335 /* Expecting to increase the service time estimate here */
336 ptlrpc_at_adj_service(req,
337 lustre_msg_get_timeout(early_req->rq_repmsg));
338 ptlrpc_at_adj_net_latency(req,
339 lustre_msg_get_service_time(early_req->rq_repmsg));
342 sptlrpc_cli_finish_early_reply(early_req);
344 cfs_spin_lock(&req->rq_lock);
347 /* Adjust the local timeout for this req */
348 ptlrpc_at_set_req_timeout(req);
350 olddl = req->rq_deadline;
351 /* server assumes it now has rq_timeout from when it sent the
352 early reply, so client should give it at least that long. */
353 req->rq_deadline = cfs_time_current_sec() + req->rq_timeout +
354 ptlrpc_at_get_net_latency(req);
356 DEBUG_REQ(D_ADAPTTO, req,
357 "Early reply #%d, new deadline in "CFS_DURATION_T"s "
358 "("CFS_DURATION_T"s)", req->rq_early_count,
359 cfs_time_sub(req->rq_deadline,
360 cfs_time_current_sec()),
361 cfs_time_sub(req->rq_deadline, olddl));
368 * Wind down request pool \a pool.
369 * Frees all requests from the pool too
371 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
374 struct ptlrpc_request *req;
376 LASSERT(pool != NULL);
378 cfs_spin_lock(&pool->prp_lock);
379 cfs_list_for_each_safe(l, tmp, &pool->prp_req_list) {
380 req = cfs_list_entry(l, struct ptlrpc_request, rq_list);
381 cfs_list_del(&req->rq_list);
382 LASSERT(req->rq_reqbuf);
383 LASSERT(req->rq_reqbuf_len == pool->prp_rq_size);
384 OBD_FREE_LARGE(req->rq_reqbuf, pool->prp_rq_size);
385 OBD_FREE(req, sizeof(*req));
387 cfs_spin_unlock(&pool->prp_lock);
388 OBD_FREE(pool, sizeof(*pool));
392 * Allocates, initializes and adds \a num_rq requests to the pool \a pool
394 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
399 while (size < pool->prp_rq_size)
402 LASSERTF(cfs_list_empty(&pool->prp_req_list) ||
403 size == pool->prp_rq_size,
404 "Trying to change pool size with nonempty pool "
405 "from %d to %d bytes\n", pool->prp_rq_size, size);
407 cfs_spin_lock(&pool->prp_lock);
408 pool->prp_rq_size = size;
409 for (i = 0; i < num_rq; i++) {
410 struct ptlrpc_request *req;
411 struct lustre_msg *msg;
413 cfs_spin_unlock(&pool->prp_lock);
414 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
417 OBD_ALLOC_LARGE(msg, size);
419 OBD_FREE(req, sizeof(struct ptlrpc_request));
422 req->rq_reqbuf = msg;
423 req->rq_reqbuf_len = size;
425 cfs_spin_lock(&pool->prp_lock);
426 cfs_list_add_tail(&req->rq_list, &pool->prp_req_list);
428 cfs_spin_unlock(&pool->prp_lock);
433 * Create and initialize new request pool with given attributes:
434 * \a num_rq - initial number of requests to create for the pool
435 * \a msgsize - maximum message size possible for requests in thid pool
436 * \a populate_pool - function to be called when more requests need to be added
438 * Returns pointer to newly created pool or NULL on error.
440 struct ptlrpc_request_pool *
441 ptlrpc_init_rq_pool(int num_rq, int msgsize,
442 void (*populate_pool)(struct ptlrpc_request_pool *, int))
444 struct ptlrpc_request_pool *pool;
446 OBD_ALLOC(pool, sizeof (struct ptlrpc_request_pool));
450 /* Request next power of two for the allocation, because internally
451 kernel would do exactly this */
453 cfs_spin_lock_init(&pool->prp_lock);
454 CFS_INIT_LIST_HEAD(&pool->prp_req_list);
455 pool->prp_rq_size = msgsize + SPTLRPC_MAX_PAYLOAD;
456 pool->prp_populate = populate_pool;
458 populate_pool(pool, num_rq);
460 if (cfs_list_empty(&pool->prp_req_list)) {
461 /* have not allocated a single request for the pool */
462 OBD_FREE(pool, sizeof (struct ptlrpc_request_pool));
469 * Fetches one request from pool \a pool
471 static struct ptlrpc_request *
472 ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
474 struct ptlrpc_request *request;
475 struct lustre_msg *reqbuf;
480 cfs_spin_lock(&pool->prp_lock);
482 /* See if we have anything in a pool, and bail out if nothing,
483 * in writeout path, where this matters, this is safe to do, because
484 * nothing is lost in this case, and when some in-flight requests
485 * complete, this code will be called again. */
486 if (unlikely(cfs_list_empty(&pool->prp_req_list))) {
487 cfs_spin_unlock(&pool->prp_lock);
491 request = cfs_list_entry(pool->prp_req_list.next, struct ptlrpc_request,
493 cfs_list_del_init(&request->rq_list);
494 cfs_spin_unlock(&pool->prp_lock);
496 LASSERT(request->rq_reqbuf);
497 LASSERT(request->rq_pool);
499 reqbuf = request->rq_reqbuf;
500 memset(request, 0, sizeof(*request));
501 request->rq_reqbuf = reqbuf;
502 request->rq_reqbuf_len = pool->prp_rq_size;
503 request->rq_pool = pool;
509 * Returns freed \a request to pool.
511 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
513 struct ptlrpc_request_pool *pool = request->rq_pool;
515 cfs_spin_lock(&pool->prp_lock);
516 LASSERT(cfs_list_empty(&request->rq_list));
517 LASSERT(!request->rq_receiving_reply);
518 cfs_list_add_tail(&request->rq_list, &pool->prp_req_list);
519 cfs_spin_unlock(&pool->prp_lock);
522 static int __ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
523 __u32 version, int opcode,
524 int count, __u32 *lengths, char **bufs,
525 struct ptlrpc_cli_ctx *ctx)
527 struct obd_import *imp = request->rq_import;
532 request->rq_cli_ctx = sptlrpc_cli_ctx_get(ctx);
534 rc = sptlrpc_req_get_ctx(request);
539 sptlrpc_req_set_flavor(request, opcode);
541 rc = lustre_pack_request(request, imp->imp_msg_magic, count,
544 LASSERT(!request->rq_pool);
548 lustre_msg_add_version(request->rq_reqmsg, version);
549 request->rq_send_state = LUSTRE_IMP_FULL;
550 request->rq_type = PTL_RPC_MSG_REQUEST;
551 request->rq_export = NULL;
553 request->rq_req_cbid.cbid_fn = request_out_callback;
554 request->rq_req_cbid.cbid_arg = request;
556 request->rq_reply_cbid.cbid_fn = reply_in_callback;
557 request->rq_reply_cbid.cbid_arg = request;
559 request->rq_reply_deadline = 0;
560 request->rq_phase = RQ_PHASE_NEW;
561 request->rq_next_phase = RQ_PHASE_UNDEFINED;
563 request->rq_request_portal = imp->imp_client->cli_request_portal;
564 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
566 ptlrpc_at_set_req_timeout(request);
568 cfs_spin_lock_init(&request->rq_lock);
569 CFS_INIT_LIST_HEAD(&request->rq_list);
570 CFS_INIT_LIST_HEAD(&request->rq_timed_list);
571 CFS_INIT_LIST_HEAD(&request->rq_replay_list);
572 CFS_INIT_LIST_HEAD(&request->rq_ctx_chain);
573 CFS_INIT_LIST_HEAD(&request->rq_set_chain);
574 CFS_INIT_LIST_HEAD(&request->rq_history_list);
575 CFS_INIT_LIST_HEAD(&request->rq_exp_list);
576 cfs_waitq_init(&request->rq_reply_waitq);
577 cfs_waitq_init(&request->rq_set_waitq);
578 request->rq_xid = ptlrpc_next_xid();
579 cfs_atomic_set(&request->rq_refcount, 1);
581 lustre_msg_set_opc(request->rq_reqmsg, opcode);
585 sptlrpc_cli_ctx_put(request->rq_cli_ctx, 1);
587 class_import_put(imp);
591 int ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
592 __u32 version, int opcode, char **bufs,
593 struct ptlrpc_cli_ctx *ctx)
597 count = req_capsule_filled_sizes(&request->rq_pill, RCL_CLIENT);
598 return __ptlrpc_request_bufs_pack(request, version, opcode, count,
599 request->rq_pill.rc_area[RCL_CLIENT],
602 EXPORT_SYMBOL(ptlrpc_request_bufs_pack);
605 * Pack request buffers for network transfer, performing necessary encryption
606 * steps if necessary.
608 int ptlrpc_request_pack(struct ptlrpc_request *request,
609 __u32 version, int opcode)
612 rc = ptlrpc_request_bufs_pack(request, version, opcode, NULL, NULL);
616 /* For some old 1.8 clients (< 1.8.7), they will LASSERT the size of
617 * ptlrpc_body sent from server equal to local ptlrpc_body size, so we
618 * have to send old ptlrpc_body to keep interoprability with these
621 * Only three kinds of server->client RPCs so far:
626 * XXX This should be removed whenever we drop the interoprability with
627 * the these old clients.
629 if (opcode == LDLM_BL_CALLBACK || opcode == LDLM_CP_CALLBACK ||
630 opcode == LDLM_GL_CALLBACK)
631 req_capsule_shrink(&request->rq_pill, &RMF_PTLRPC_BODY,
632 sizeof(struct ptlrpc_body_v2), RCL_CLIENT);
638 * Helper function to allocate new request on import \a imp
639 * and possibly using existing request from pool \a pool if provided.
640 * Returns allocated request structure with import field filled or
644 struct ptlrpc_request *__ptlrpc_request_alloc(struct obd_import *imp,
645 struct ptlrpc_request_pool *pool)
647 struct ptlrpc_request *request = NULL;
650 request = ptlrpc_prep_req_from_pool(pool);
653 OBD_ALLOC_PTR(request);
656 LASSERTF((unsigned long)imp > 0x1000, "%p", imp);
657 LASSERT(imp != LP_POISON);
658 LASSERTF((unsigned long)imp->imp_client > 0x1000, "%p",
660 LASSERT(imp->imp_client != LP_POISON);
662 request->rq_import = class_import_get(imp);
664 CERROR("request allocation out of memory\n");
671 * Helper function for creating a request.
672 * Calls __ptlrpc_request_alloc to allocate new request sturcture and inits
673 * buffer structures according to capsule template \a format.
674 * Returns allocated request structure pointer or NULL on error.
676 static struct ptlrpc_request *
677 ptlrpc_request_alloc_internal(struct obd_import *imp,
678 struct ptlrpc_request_pool * pool,
679 const struct req_format *format)
681 struct ptlrpc_request *request;
683 request = __ptlrpc_request_alloc(imp, pool);
687 req_capsule_init(&request->rq_pill, request, RCL_CLIENT);
688 req_capsule_set(&request->rq_pill, format);
693 * Allocate new request structure for import \a imp and initialize its
694 * buffer structure according to capsule template \a format.
696 struct ptlrpc_request *ptlrpc_request_alloc(struct obd_import *imp,
697 const struct req_format *format)
699 return ptlrpc_request_alloc_internal(imp, NULL, format);
703 * Allocate new request structure for import \a imp from pool \a pool and
704 * initialize its buffer structure according to capsule template \a format.
706 struct ptlrpc_request *ptlrpc_request_alloc_pool(struct obd_import *imp,
707 struct ptlrpc_request_pool * pool,
708 const struct req_format *format)
710 return ptlrpc_request_alloc_internal(imp, pool, format);
714 * For requests not from pool, free memory of the request structure.
715 * For requests obtained from a pool earlier, return request back to pool.
717 void ptlrpc_request_free(struct ptlrpc_request *request)
719 if (request->rq_pool)
720 __ptlrpc_free_req_to_pool(request);
722 OBD_FREE_PTR(request);
726 * Allocate new request for operatione \a opcode and immediatelly pack it for
728 * Only used for simple requests like OBD_PING where the only important
729 * part of the request is operation itself.
730 * Returns allocated request or NULL on error.
732 struct ptlrpc_request *ptlrpc_request_alloc_pack(struct obd_import *imp,
733 const struct req_format *format,
734 __u32 version, int opcode)
736 struct ptlrpc_request *req = ptlrpc_request_alloc(imp, format);
740 rc = ptlrpc_request_pack(req, version, opcode);
742 ptlrpc_request_free(req);
750 * Prepare request (fetched from pool \a poolif not NULL) on import \a imp
751 * for operation \a opcode. Request would contain \a count buffers.
752 * Sizes of buffers are described in array \a lengths and buffers themselves
753 * are provided by a pointer \a bufs.
754 * Returns prepared request structure pointer or NULL on error.
756 struct ptlrpc_request *
757 ptlrpc_prep_req_pool(struct obd_import *imp,
758 __u32 version, int opcode,
759 int count, __u32 *lengths, char **bufs,
760 struct ptlrpc_request_pool *pool)
762 struct ptlrpc_request *request;
765 request = __ptlrpc_request_alloc(imp, pool);
769 rc = __ptlrpc_request_bufs_pack(request, version, opcode, count,
770 lengths, bufs, NULL);
772 ptlrpc_request_free(request);
779 * Same as ptlrpc_prep_req_pool, but without pool
781 struct ptlrpc_request *
782 ptlrpc_prep_req(struct obd_import *imp, __u32 version, int opcode, int count,
783 __u32 *lengths, char **bufs)
785 return ptlrpc_prep_req_pool(imp, version, opcode, count, lengths, bufs,
790 * Allocate "fake" request that would not be sent anywhere in the end.
791 * Only used as a hack because we have no other way of performing
792 * async actions in lustre between layers.
793 * Used on MDS to request object preallocations from more than one OST at a
796 struct ptlrpc_request *ptlrpc_prep_fakereq(struct obd_import *imp,
797 unsigned int timeout,
798 ptlrpc_interpterer_t interpreter)
800 struct ptlrpc_request *request = NULL;
803 OBD_ALLOC(request, sizeof(*request));
805 CERROR("request allocation out of memory\n");
809 request->rq_send_state = LUSTRE_IMP_FULL;
810 request->rq_type = PTL_RPC_MSG_REQUEST;
811 request->rq_import = class_import_get(imp);
812 request->rq_export = NULL;
813 request->rq_import_generation = imp->imp_generation;
815 request->rq_timeout = timeout;
816 request->rq_sent = cfs_time_current_sec();
817 request->rq_deadline = request->rq_sent + timeout;
818 request->rq_reply_deadline = request->rq_deadline;
819 request->rq_interpret_reply = interpreter;
820 request->rq_phase = RQ_PHASE_RPC;
821 request->rq_next_phase = RQ_PHASE_INTERPRET;
822 /* don't want reply */
823 request->rq_receiving_reply = 0;
824 request->rq_must_unlink = 0;
825 request->rq_no_delay = request->rq_no_resend = 1;
826 request->rq_fake = 1;
828 cfs_spin_lock_init(&request->rq_lock);
829 CFS_INIT_LIST_HEAD(&request->rq_list);
830 CFS_INIT_LIST_HEAD(&request->rq_replay_list);
831 CFS_INIT_LIST_HEAD(&request->rq_set_chain);
832 CFS_INIT_LIST_HEAD(&request->rq_history_list);
833 CFS_INIT_LIST_HEAD(&request->rq_exp_list);
834 cfs_waitq_init(&request->rq_reply_waitq);
835 cfs_waitq_init(&request->rq_set_waitq);
837 request->rq_xid = ptlrpc_next_xid();
838 cfs_atomic_set(&request->rq_refcount, 1);
844 * Indicate that processing of "fake" request is finished.
846 void ptlrpc_fakereq_finished(struct ptlrpc_request *req)
848 struct ptlrpc_request_set *set = req->rq_set;
851 /* hold ref on the request to prevent others (ptlrpcd) to free it */
852 ptlrpc_request_addref(req);
853 cfs_list_del_init(&req->rq_list);
855 /* if we kill request before timeout - need adjust counter */
856 if (req->rq_phase == RQ_PHASE_RPC && set != NULL &&
857 cfs_atomic_dec_and_test(&set->set_remaining))
860 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
862 /* Only need to call wakeup once when to be empty. */
864 cfs_waitq_signal(&set->set_waitq);
865 ptlrpc_req_finished(req);
869 * Allocate and initialize new request set structure.
870 * Returns a pointer to the newly allocated set structure or NULL on error.
872 struct ptlrpc_request_set *ptlrpc_prep_set(void)
874 struct ptlrpc_request_set *set;
877 OBD_ALLOC(set, sizeof *set);
880 cfs_atomic_set(&set->set_refcount, 1);
881 CFS_INIT_LIST_HEAD(&set->set_requests);
882 cfs_waitq_init(&set->set_waitq);
883 cfs_atomic_set(&set->set_new_count, 0);
884 cfs_atomic_set(&set->set_remaining, 0);
885 cfs_spin_lock_init(&set->set_new_req_lock);
886 CFS_INIT_LIST_HEAD(&set->set_new_requests);
887 CFS_INIT_LIST_HEAD(&set->set_cblist);
888 set->set_max_inflight = UINT_MAX;
889 set->set_producer = NULL;
890 set->set_producer_arg = NULL;
897 * Allocate and initialize new request set structure with flow control
898 * extension. This extension allows to control the number of requests in-flight
899 * for the whole set. A callback function to generate requests must be provided
900 * and the request set will keep the number of requests sent over the wire to
902 * Returns a pointer to the newly allocated set structure or NULL on error.
904 struct ptlrpc_request_set *ptlrpc_prep_fcset(int max, set_producer_func func,
908 struct ptlrpc_request_set *set;
910 set = ptlrpc_prep_set();
914 set->set_max_inflight = max;
915 set->set_producer = func;
916 set->set_producer_arg = arg;
920 EXPORT_SYMBOL(ptlrpc_prep_fcset);
923 * Wind down and free request set structure previously allocated with
925 * Ensures that all requests on the set have completed and removes
926 * all requests from the request list in a set.
927 * If any unsent request happen to be on the list, pretends that they got
928 * an error in flight and calls their completion handler.
930 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
938 /* Requests on the set should either all be completed, or all be new */
939 expected_phase = (cfs_atomic_read(&set->set_remaining) == 0) ?
940 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
941 cfs_list_for_each (tmp, &set->set_requests) {
942 struct ptlrpc_request *req =
943 cfs_list_entry(tmp, struct ptlrpc_request,
946 LASSERT(req->rq_phase == expected_phase);
950 LASSERTF(cfs_atomic_read(&set->set_remaining) == 0 ||
951 cfs_atomic_read(&set->set_remaining) == n, "%d / %d\n",
952 cfs_atomic_read(&set->set_remaining), n);
954 cfs_list_for_each_safe(tmp, next, &set->set_requests) {
955 struct ptlrpc_request *req =
956 cfs_list_entry(tmp, struct ptlrpc_request,
958 cfs_list_del_init(&req->rq_set_chain);
960 LASSERT(req->rq_phase == expected_phase);
962 if (req->rq_phase == RQ_PHASE_NEW) {
963 ptlrpc_req_interpret(NULL, req, -EBADR);
964 cfs_atomic_dec(&set->set_remaining);
967 cfs_spin_lock(&req->rq_lock);
969 req->rq_invalid_rqset = 0;
970 cfs_spin_unlock(&req->rq_lock);
972 ptlrpc_req_finished (req);
975 LASSERT(cfs_atomic_read(&set->set_remaining) == 0);
977 ptlrpc_reqset_put(set);
982 * Add a callback function \a fn to the set.
983 * This function would be called when all requests on this set are completed.
984 * The function will be passed \a data argument.
986 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
987 set_interpreter_func fn, void *data)
989 struct ptlrpc_set_cbdata *cbdata;
991 OBD_ALLOC_PTR(cbdata);
995 cbdata->psc_interpret = fn;
996 cbdata->psc_data = data;
997 cfs_list_add_tail(&cbdata->psc_item, &set->set_cblist);
1003 * Add a new request to the general purpose request set.
1004 * Assumes request reference from the caller.
1006 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
1007 struct ptlrpc_request *req)
1009 char jobid[JOBSTATS_JOBID_SIZE];
1010 LASSERT(cfs_list_empty(&req->rq_set_chain));
1012 /* The set takes over the caller's request reference */
1013 cfs_list_add_tail(&req->rq_set_chain, &set->set_requests);
1015 cfs_atomic_inc(&set->set_remaining);
1016 req->rq_queued_time = cfs_time_current();
1018 if (req->rq_reqmsg) {
1019 lustre_get_jobid(jobid);
1020 lustre_msg_set_jobid(req->rq_reqmsg, jobid);
1023 if (set->set_producer != NULL)
1024 /* If the request set has a producer callback, the RPC must be
1025 * sent straight away */
1026 ptlrpc_send_new_req(req);
1030 * Add a request to a request with dedicated server thread
1031 * and wake the thread to make any necessary processing.
1032 * Currently only used for ptlrpcd.
1034 void ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
1035 struct ptlrpc_request *req)
1037 struct ptlrpc_request_set *set = pc->pc_set;
1040 LASSERT(req->rq_set == NULL);
1041 LASSERT(cfs_test_bit(LIOD_STOP, &pc->pc_flags) == 0);
1043 cfs_spin_lock(&set->set_new_req_lock);
1045 * The set takes over the caller's request reference.
1048 req->rq_queued_time = cfs_time_current();
1049 cfs_list_add_tail(&req->rq_set_chain, &set->set_new_requests);
1050 count = cfs_atomic_inc_return(&set->set_new_count);
1051 cfs_spin_unlock(&set->set_new_req_lock);
1053 /* Only need to call wakeup once for the first entry. */
1055 cfs_waitq_signal(&set->set_waitq);
1057 /* XXX: It maybe unnecessary to wakeup all the partners. But to
1058 * guarantee the async RPC can be processed ASAP, we have
1059 * no other better choice. It maybe fixed in future. */
1060 for (i = 0; i < pc->pc_npartners; i++)
1061 cfs_waitq_signal(&pc->pc_partners[i]->pc_set->set_waitq);
1066 * Based on the current state of the import, determine if the request
1067 * can be sent, is an error, or should be delayed.
1069 * Returns true if this request should be delayed. If false, and
1070 * *status is set, then the request can not be sent and *status is the
1071 * error code. If false and status is 0, then request can be sent.
1073 * The imp->imp_lock must be held.
1075 static int ptlrpc_import_delay_req(struct obd_import *imp,
1076 struct ptlrpc_request *req, int *status)
1081 LASSERT (status != NULL);
1084 if (req->rq_ctx_init || req->rq_ctx_fini) {
1085 /* always allow ctx init/fini rpc go through */
1086 } else if (imp->imp_state == LUSTRE_IMP_NEW) {
1087 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
1089 } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
1090 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
1092 } else if (ptlrpc_send_limit_expired(req)) {
1093 /* probably doesn't need to be a D_ERROR after initial testing */
1094 DEBUG_REQ(D_ERROR, req, "send limit expired ");
1096 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
1097 imp->imp_state == LUSTRE_IMP_CONNECTING) {
1098 /* allow CONNECT even if import is invalid */ ;
1099 if (cfs_atomic_read(&imp->imp_inval_count) != 0) {
1100 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1103 } else if (imp->imp_invalid || imp->imp_obd->obd_no_recov) {
1104 if (!imp->imp_deactive)
1105 DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
1106 *status = -ESHUTDOWN; /* bz 12940 */
1107 } else if (req->rq_import_generation != imp->imp_generation) {
1108 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
1110 } else if (req->rq_send_state != imp->imp_state) {
1111 /* invalidate in progress - any requests should be drop */
1112 if (cfs_atomic_read(&imp->imp_inval_count) != 0) {
1113 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1115 } else if (imp->imp_dlm_fake || req->rq_no_delay) {
1116 *status = -EWOULDBLOCK;
1126 * Decide if the eror message regarding provided request \a req
1127 * should be printed to the console or not.
1128 * Makes it's decision on request status and other properties.
1129 * Returns 1 to print error on the system console or 0 if not.
1131 static int ptlrpc_console_allow(struct ptlrpc_request *req)
1136 /* Fake requests include no rq_reqmsg */
1140 LASSERT(req->rq_reqmsg != NULL);
1141 opc = lustre_msg_get_opc(req->rq_reqmsg);
1143 /* Suppress particular reconnect errors which are to be expected. No
1144 * errors are suppressed for the initial connection on an import */
1145 if ((lustre_handle_is_used(&req->rq_import->imp_remote_handle)) &&
1146 (opc == OST_CONNECT || opc == MDS_CONNECT || opc == MGS_CONNECT)) {
1148 /* Suppress timed out reconnect requests */
1149 if (req->rq_timedout)
1152 /* Suppress unavailable/again reconnect requests */
1153 err = lustre_msg_get_status(req->rq_repmsg);
1154 if (err == -ENODEV || err == -EAGAIN)
1162 * Check request processing status.
1163 * Returns the status.
1165 static int ptlrpc_check_status(struct ptlrpc_request *req)
1170 err = lustre_msg_get_status(req->rq_repmsg);
1171 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
1172 struct obd_import *imp = req->rq_import;
1173 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1174 LCONSOLE_ERROR_MSG(0x011,"an error occurred while communicating"
1175 " with %s. The %s operation failed with %d\n",
1176 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1177 ll_opcode2str(opc), err);
1178 RETURN(err < 0 ? err : -EINVAL);
1182 DEBUG_REQ(D_INFO, req, "status is %d", err);
1183 } else if (err > 0) {
1184 /* XXX: translate this error from net to host */
1185 DEBUG_REQ(D_INFO, req, "status is %d", err);
1188 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
1189 struct obd_import *imp = req->rq_import;
1190 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1192 if (ptlrpc_console_allow(req))
1193 LCONSOLE_ERROR_MSG(0x011,"an error occurred while "
1194 "communicating with %s. The %s "
1195 "operation failed with %d\n",
1197 imp->imp_connection->c_peer.nid),
1198 ll_opcode2str(opc), err);
1200 RETURN(err < 0 ? err : -EINVAL);
1207 * save pre-versions of objects into request for replay.
1208 * Versions are obtained from server reply.
1211 static void ptlrpc_save_versions(struct ptlrpc_request *req)
1213 struct lustre_msg *repmsg = req->rq_repmsg;
1214 struct lustre_msg *reqmsg = req->rq_reqmsg;
1215 __u64 *versions = lustre_msg_get_versions(repmsg);
1218 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1222 lustre_msg_set_versions(reqmsg, versions);
1223 CDEBUG(D_INFO, "Client save versions ["LPX64"/"LPX64"]\n",
1224 versions[0], versions[1]);
1230 * Callback function called when client receives RPC reply for \a req.
1231 * Returns 0 on success or error code.
1232 * The return alue would be assigned to req->rq_status by the caller
1233 * as request processing status.
1234 * This function also decides if the request needs to be saved for later replay.
1236 static int after_reply(struct ptlrpc_request *req)
1238 struct obd_import *imp = req->rq_import;
1239 struct obd_device *obd = req->rq_import->imp_obd;
1241 struct timeval work_start;
1245 LASSERT(obd != NULL);
1246 /* repbuf must be unlinked */
1247 LASSERT(!req->rq_receiving_reply && !req->rq_must_unlink);
1249 if (req->rq_reply_truncate) {
1250 if (ptlrpc_no_resend(req)) {
1251 DEBUG_REQ(D_ERROR, req, "reply buffer overflow,"
1252 " expected: %d, actual size: %d",
1253 req->rq_nob_received, req->rq_repbuf_len);
1257 sptlrpc_cli_free_repbuf(req);
1258 /* Pass the required reply buffer size (include
1259 * space for early reply).
1260 * NB: no need to roundup because alloc_repbuf
1261 * will roundup it */
1262 req->rq_replen = req->rq_nob_received;
1263 req->rq_nob_received = 0;
1269 * NB Until this point, the whole of the incoming message,
1270 * including buflens, status etc is in the sender's byte order.
1272 rc = sptlrpc_cli_unwrap_reply(req);
1274 DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
1279 * Security layer unwrap might ask resend this request.
1284 rc = unpack_reply(req);
1288 cfs_gettimeofday(&work_start);
1289 timediff = cfs_timeval_sub(&work_start, &req->rq_arrival_time, NULL);
1290 if (obd->obd_svc_stats != NULL) {
1291 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR,
1293 ptlrpc_lprocfs_rpc_sent(req, timediff);
1296 if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
1297 lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
1298 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
1299 lustre_msg_get_type(req->rq_repmsg));
1303 if (lustre_msg_get_opc(req->rq_reqmsg) != OBD_PING)
1304 CFS_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_PAUSE_REP, cfs_fail_val);
1305 ptlrpc_at_adj_service(req, lustre_msg_get_timeout(req->rq_repmsg));
1306 ptlrpc_at_adj_net_latency(req,
1307 lustre_msg_get_service_time(req->rq_repmsg));
1309 rc = ptlrpc_check_status(req);
1310 imp->imp_connect_error = rc;
1314 * Either we've been evicted, or the server has failed for
1315 * some reason. Try to reconnect, and if that fails, punt to
1318 if (ll_rpc_recoverable_error(rc)) {
1319 if (req->rq_send_state != LUSTRE_IMP_FULL ||
1320 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
1323 ptlrpc_request_handle_notconn(req);
1328 * Let's look if server sent slv. Do it only for RPC with
1331 ldlm_cli_update_pool(req);
1335 * Store transno in reqmsg for replay.
1337 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
1338 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
1339 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
1342 if (imp->imp_replayable) {
1343 cfs_spin_lock(&imp->imp_lock);
1345 * No point in adding already-committed requests to the replay
1346 * list, we will just remove them immediately. b=9829
1348 if (req->rq_transno != 0 &&
1350 lustre_msg_get_last_committed(req->rq_repmsg) ||
1352 /** version recovery */
1353 ptlrpc_save_versions(req);
1354 ptlrpc_retain_replayable_request(req, imp);
1355 } else if (req->rq_commit_cb != NULL) {
1356 cfs_spin_unlock(&imp->imp_lock);
1357 req->rq_commit_cb(req);
1358 cfs_spin_lock(&imp->imp_lock);
1362 * Replay-enabled imports return commit-status information.
1364 if (lustre_msg_get_last_committed(req->rq_repmsg)) {
1365 imp->imp_peer_committed_transno =
1366 lustre_msg_get_last_committed(req->rq_repmsg);
1368 ptlrpc_free_committed(imp);
1370 if (req->rq_transno > imp->imp_peer_committed_transno)
1371 ptlrpc_pinger_commit_expected(imp);
1373 cfs_spin_unlock(&imp->imp_lock);
1380 * Helper function to send request \a req over the network for the first time
1381 * Also adjusts request phase.
1382 * Returns 0 on success or error code.
1384 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
1386 struct obd_import *imp = req->rq_import;
1390 LASSERT(req->rq_phase == RQ_PHASE_NEW);
1391 if (req->rq_sent && (req->rq_sent > cfs_time_current_sec()) &&
1392 (!req->rq_generation_set ||
1393 req->rq_import_generation == imp->imp_generation))
1396 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
1398 cfs_spin_lock(&imp->imp_lock);
1400 if (!req->rq_generation_set)
1401 req->rq_import_generation = imp->imp_generation;
1403 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1404 cfs_spin_lock(&req->rq_lock);
1405 req->rq_waiting = 1;
1406 cfs_spin_unlock(&req->rq_lock);
1408 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
1409 "(%s != %s)", lustre_msg_get_status(req->rq_reqmsg),
1410 ptlrpc_import_state_name(req->rq_send_state),
1411 ptlrpc_import_state_name(imp->imp_state));
1412 LASSERT(cfs_list_empty(&req->rq_list));
1413 cfs_list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1414 cfs_atomic_inc(&req->rq_import->imp_inflight);
1415 cfs_spin_unlock(&imp->imp_lock);
1420 cfs_spin_unlock(&imp->imp_lock);
1421 req->rq_status = rc;
1422 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1426 LASSERT(cfs_list_empty(&req->rq_list));
1427 cfs_list_add_tail(&req->rq_list, &imp->imp_sending_list);
1428 cfs_atomic_inc(&req->rq_import->imp_inflight);
1429 cfs_spin_unlock(&imp->imp_lock);
1431 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
1433 rc = sptlrpc_req_refresh_ctx(req, -1);
1436 req->rq_status = rc;
1439 req->rq_wait_ctx = 1;
1444 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
1445 " %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1446 imp->imp_obd->obd_uuid.uuid,
1447 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1448 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1449 lustre_msg_get_opc(req->rq_reqmsg));
1451 rc = ptl_send_rpc(req, 0);
1453 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
1454 req->rq_net_err = 1;
1460 static inline int ptlrpc_set_producer(struct ptlrpc_request_set *set)
1465 LASSERT(set->set_producer != NULL);
1467 remaining = cfs_atomic_read(&set->set_remaining);
1469 /* populate the ->set_requests list with requests until we
1470 * reach the maximum number of RPCs in flight for this set */
1471 while (cfs_atomic_read(&set->set_remaining) < set->set_max_inflight) {
1472 rc = set->set_producer(set, set->set_producer_arg);
1473 if (rc == -ENOENT) {
1474 /* no more RPC to produce */
1475 set->set_producer = NULL;
1476 set->set_producer_arg = NULL;
1481 RETURN((cfs_atomic_read(&set->set_remaining) - remaining));
1485 * this sends any unsent RPCs in \a set and returns 1 if all are sent
1486 * and no more replies are expected.
1487 * (it is possible to get less replies than requests sent e.g. due to timed out
1488 * requests or requests that we had trouble to send out)
1490 int ptlrpc_check_set(const struct lu_env *env, struct ptlrpc_request_set *set)
1492 cfs_list_t *tmp, *next;
1493 int force_timer_recalc = 0;
1496 if (cfs_atomic_read(&set->set_remaining) == 0)
1499 cfs_list_for_each_safe(tmp, next, &set->set_requests) {
1500 struct ptlrpc_request *req =
1501 cfs_list_entry(tmp, struct ptlrpc_request,
1503 struct obd_import *imp = req->rq_import;
1504 int unregistered = 0;
1507 if (req->rq_phase == RQ_PHASE_NEW &&
1508 ptlrpc_send_new_req(req)) {
1509 force_timer_recalc = 1;
1512 /* delayed send - skip */
1513 if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
1516 if (!(req->rq_phase == RQ_PHASE_RPC ||
1517 req->rq_phase == RQ_PHASE_BULK ||
1518 req->rq_phase == RQ_PHASE_INTERPRET ||
1519 req->rq_phase == RQ_PHASE_UNREGISTERING ||
1520 req->rq_phase == RQ_PHASE_COMPLETE)) {
1521 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
1525 if (req->rq_phase == RQ_PHASE_UNREGISTERING) {
1526 LASSERT(req->rq_next_phase != req->rq_phase);
1527 LASSERT(req->rq_next_phase != RQ_PHASE_UNDEFINED);
1530 * Skip processing until reply is unlinked. We
1531 * can't return to pool before that and we can't
1532 * call interpret before that. We need to make
1533 * sure that all rdma transfers finished and will
1534 * not corrupt any data.
1536 if (ptlrpc_client_recv_or_unlink(req) ||
1537 ptlrpc_client_bulk_active(req))
1541 * Turn fail_loc off to prevent it from looping
1544 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
1545 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK,
1548 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK)) {
1549 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK,
1554 * Move to next phase if reply was successfully
1557 ptlrpc_rqphase_move(req, req->rq_next_phase);
1560 if (req->rq_phase == RQ_PHASE_COMPLETE)
1563 if (req->rq_phase == RQ_PHASE_INTERPRET)
1564 GOTO(interpret, req->rq_status);
1567 * Note that this also will start async reply unlink.
1569 if (req->rq_net_err && !req->rq_timedout) {
1570 ptlrpc_expire_one_request(req, 1);
1573 * Check if we still need to wait for unlink.
1575 if (ptlrpc_client_recv_or_unlink(req) ||
1576 ptlrpc_client_bulk_active(req))
1578 /* If there is no need to resend, fail it now. */
1579 if (req->rq_no_resend) {
1580 if (req->rq_status == 0)
1581 req->rq_status = -EIO;
1582 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1583 GOTO(interpret, req->rq_status);
1590 cfs_spin_lock(&req->rq_lock);
1591 req->rq_replied = 0;
1592 cfs_spin_unlock(&req->rq_lock);
1593 if (req->rq_status == 0)
1594 req->rq_status = -EIO;
1595 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1596 GOTO(interpret, req->rq_status);
1599 /* ptlrpc_set_wait->l_wait_event sets lwi_allow_intr
1600 * so it sets rq_intr regardless of individual rpc
1601 * timeouts. The synchronous IO waiting path sets
1602 * rq_intr irrespective of whether ptlrpcd
1603 * has seen a timeout. Our policy is to only interpret
1604 * interrupted rpcs after they have timed out, so we
1605 * need to enforce that here.
1608 if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
1609 req->rq_wait_ctx)) {
1610 req->rq_status = -EINTR;
1611 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1612 GOTO(interpret, req->rq_status);
1615 if (req->rq_phase == RQ_PHASE_RPC) {
1616 if (req->rq_timedout || req->rq_resend ||
1617 req->rq_waiting || req->rq_wait_ctx) {
1620 if (!ptlrpc_unregister_reply(req, 1))
1623 cfs_spin_lock(&imp->imp_lock);
1624 if (ptlrpc_import_delay_req(imp, req, &status)){
1625 /* put on delay list - only if we wait
1626 * recovery finished - before send */
1627 cfs_list_del_init(&req->rq_list);
1628 cfs_list_add_tail(&req->rq_list,
1631 cfs_spin_unlock(&imp->imp_lock);
1636 req->rq_status = status;
1637 ptlrpc_rqphase_move(req,
1638 RQ_PHASE_INTERPRET);
1639 cfs_spin_unlock(&imp->imp_lock);
1640 GOTO(interpret, req->rq_status);
1642 if (ptlrpc_no_resend(req) && !req->rq_wait_ctx) {
1643 req->rq_status = -ENOTCONN;
1644 ptlrpc_rqphase_move(req,
1645 RQ_PHASE_INTERPRET);
1646 cfs_spin_unlock(&imp->imp_lock);
1647 GOTO(interpret, req->rq_status);
1650 cfs_list_del_init(&req->rq_list);
1651 cfs_list_add_tail(&req->rq_list,
1652 &imp->imp_sending_list);
1654 cfs_spin_unlock(&imp->imp_lock);
1656 cfs_spin_lock(&req->rq_lock);
1657 req->rq_waiting = 0;
1658 cfs_spin_unlock(&req->rq_lock);
1660 if (req->rq_timedout || req->rq_resend) {
1661 /* This is re-sending anyways,
1662 * let's mark req as resend. */
1663 cfs_spin_lock(&req->rq_lock);
1665 cfs_spin_unlock(&req->rq_lock);
1669 if (!ptlrpc_unregister_bulk(req, 1))
1672 /* ensure previous bulk fails */
1673 old_xid = req->rq_xid;
1674 req->rq_xid = ptlrpc_next_xid();
1675 CDEBUG(D_HA, "resend bulk "
1678 old_xid, req->rq_xid);
1682 * rq_wait_ctx is only touched by ptlrpcd,
1683 * so no lock is needed here.
1685 status = sptlrpc_req_refresh_ctx(req, -1);
1688 req->rq_status = status;
1689 cfs_spin_lock(&req->rq_lock);
1690 req->rq_wait_ctx = 0;
1691 cfs_spin_unlock(&req->rq_lock);
1692 force_timer_recalc = 1;
1694 cfs_spin_lock(&req->rq_lock);
1695 req->rq_wait_ctx = 1;
1696 cfs_spin_unlock(&req->rq_lock);
1701 cfs_spin_lock(&req->rq_lock);
1702 req->rq_wait_ctx = 0;
1703 cfs_spin_unlock(&req->rq_lock);
1706 rc = ptl_send_rpc(req, 0);
1708 DEBUG_REQ(D_HA, req, "send failed (%d)",
1710 force_timer_recalc = 1;
1711 cfs_spin_lock(&req->rq_lock);
1712 req->rq_net_err = 1;
1713 cfs_spin_unlock(&req->rq_lock);
1715 /* need to reset the timeout */
1716 force_timer_recalc = 1;
1719 cfs_spin_lock(&req->rq_lock);
1721 if (ptlrpc_client_early(req)) {
1722 ptlrpc_at_recv_early_reply(req);
1723 cfs_spin_unlock(&req->rq_lock);
1727 /* Still waiting for a reply? */
1728 if (ptlrpc_client_recv(req)) {
1729 cfs_spin_unlock(&req->rq_lock);
1733 /* Did we actually receive a reply? */
1734 if (!ptlrpc_client_replied(req)) {
1735 cfs_spin_unlock(&req->rq_lock);
1739 cfs_spin_unlock(&req->rq_lock);
1741 /* unlink from net because we are going to
1742 * swab in-place of reply buffer */
1743 unregistered = ptlrpc_unregister_reply(req, 1);
1747 req->rq_status = after_reply(req);
1751 /* If there is no bulk associated with this request,
1752 * then we're done and should let the interpreter
1753 * process the reply. Similarly if the RPC returned
1754 * an error, and therefore the bulk will never arrive.
1756 if (req->rq_bulk == NULL || req->rq_status < 0) {
1757 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1758 GOTO(interpret, req->rq_status);
1761 ptlrpc_rqphase_move(req, RQ_PHASE_BULK);
1764 LASSERT(req->rq_phase == RQ_PHASE_BULK);
1765 if (ptlrpc_client_bulk_active(req))
1768 if (!req->rq_bulk->bd_success) {
1769 /* The RPC reply arrived OK, but the bulk screwed
1770 * up! Dead weird since the server told us the RPC
1771 * was good after getting the REPLY for her GET or
1772 * the ACK for her PUT. */
1773 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1774 req->rq_status = -EIO;
1777 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1780 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
1782 /* This moves to "unregistering" phase we need to wait for
1784 if (!unregistered && !ptlrpc_unregister_reply(req, 1)) {
1785 /* start async bulk unlink too */
1786 ptlrpc_unregister_bulk(req, 1);
1790 if (!ptlrpc_unregister_bulk(req, 1))
1793 /* When calling interpret receiving already should be
1795 LASSERT(!req->rq_receiving_reply);
1797 ptlrpc_req_interpret(env, req, req->rq_status);
1799 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
1801 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
1802 "opc %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1803 imp->imp_obd->obd_uuid.uuid,
1804 req->rq_reqmsg ? lustre_msg_get_status(req->rq_reqmsg):-1,
1806 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1807 req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : -1);
1809 cfs_spin_lock(&imp->imp_lock);
1810 /* Request already may be not on sending or delaying list. This
1811 * may happen in the case of marking it erroneous for the case
1812 * ptlrpc_import_delay_req(req, status) find it impossible to
1813 * allow sending this rpc and returns *status != 0. */
1814 if (!cfs_list_empty(&req->rq_list)) {
1815 cfs_list_del_init(&req->rq_list);
1816 cfs_atomic_dec(&imp->imp_inflight);
1818 cfs_spin_unlock(&imp->imp_lock);
1820 cfs_atomic_dec(&set->set_remaining);
1821 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
1823 if (set->set_producer) {
1824 /* produce a new request if possible */
1825 if (ptlrpc_set_producer(set) > 0)
1826 force_timer_recalc = 1;
1828 /* free the request that has just been completed
1829 * in order not to pollute set->set_requests */
1830 cfs_list_del_init(&req->rq_set_chain);
1831 cfs_spin_lock(&req->rq_lock);
1833 req->rq_invalid_rqset = 0;
1834 cfs_spin_unlock(&req->rq_lock);
1836 /* record rq_status to compute the final status later */
1837 if (req->rq_status != 0)
1838 set->set_rc = req->rq_status;
1839 ptlrpc_req_finished(req);
1843 /* If we hit an error, we want to recover promptly. */
1844 RETURN(cfs_atomic_read(&set->set_remaining) == 0 || force_timer_recalc);
1848 * Time out request \a req. is \a async_unlink is set, that means do not wait
1849 * until LNet actually confirms network buffer unlinking.
1850 * Return 1 if we should give up further retrying attempts or 0 otherwise.
1852 int ptlrpc_expire_one_request(struct ptlrpc_request *req, int async_unlink)
1854 struct obd_import *imp = req->rq_import;
1858 cfs_spin_lock(&req->rq_lock);
1859 req->rq_timedout = 1;
1860 cfs_spin_unlock(&req->rq_lock);
1862 DEBUG_REQ(req->rq_fake ? D_INFO : D_WARNING, req, "Request "
1863 " sent has %s: [sent "CFS_DURATION_T"/"
1864 "real "CFS_DURATION_T"]",
1865 req->rq_net_err ? "failed due to network error" :
1866 ((req->rq_real_sent == 0 ||
1867 cfs_time_before(req->rq_real_sent, req->rq_sent) ||
1868 cfs_time_aftereq(req->rq_real_sent, req->rq_deadline)) ?
1869 "timed out for sent delay" : "timed out for slow reply"),
1870 req->rq_sent, req->rq_real_sent);
1872 if (imp != NULL && obd_debug_peer_on_timeout)
1873 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1875 ptlrpc_unregister_reply(req, async_unlink);
1876 ptlrpc_unregister_bulk(req, async_unlink);
1878 if (obd_dump_on_timeout)
1879 libcfs_debug_dumplog();
1882 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
1889 cfs_atomic_inc(&imp->imp_timeouts);
1891 /* The DLM server doesn't want recovery run on its imports. */
1892 if (imp->imp_dlm_fake)
1895 /* If this request is for recovery or other primordial tasks,
1896 * then error it out here. */
1897 if (req->rq_ctx_init || req->rq_ctx_fini ||
1898 req->rq_send_state != LUSTRE_IMP_FULL ||
1899 imp->imp_obd->obd_no_recov) {
1900 DEBUG_REQ(D_RPCTRACE, req, "err -110, sent_state=%s (now=%s)",
1901 ptlrpc_import_state_name(req->rq_send_state),
1902 ptlrpc_import_state_name(imp->imp_state));
1903 cfs_spin_lock(&req->rq_lock);
1904 req->rq_status = -ETIMEDOUT;
1906 cfs_spin_unlock(&req->rq_lock);
1910 /* if a request can't be resent we can't wait for an answer after
1912 if (ptlrpc_no_resend(req)) {
1913 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1917 ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1923 * Time out all uncompleted requests in request set pointed by \a data
1924 * Callback used when waiting on sets with l_wait_event.
1927 int ptlrpc_expired_set(void *data)
1929 struct ptlrpc_request_set *set = data;
1931 time_t now = cfs_time_current_sec();
1934 LASSERT(set != NULL);
1937 * A timeout expired. See which reqs it applies to...
1939 cfs_list_for_each (tmp, &set->set_requests) {
1940 struct ptlrpc_request *req =
1941 cfs_list_entry(tmp, struct ptlrpc_request,
1944 /* don't expire request waiting for context */
1945 if (req->rq_wait_ctx)
1948 /* Request in-flight? */
1949 if (!((req->rq_phase == RQ_PHASE_RPC &&
1950 !req->rq_waiting && !req->rq_resend) ||
1951 (req->rq_phase == RQ_PHASE_BULK)))
1954 if (req->rq_timedout || /* already dealt with */
1955 req->rq_deadline > now) /* not expired */
1958 /* Deal with this guy. Do it asynchronously to not block
1959 * ptlrpcd thread. */
1960 ptlrpc_expire_one_request(req, 1);
1964 * When waiting for a whole set, we always break out of the
1965 * sleep so we can recalculate the timeout, or enable interrupts
1966 * if everyone's timed out.
1972 * Sets rq_intr flag in \a req under spinlock.
1974 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1976 cfs_spin_lock(&req->rq_lock);
1978 cfs_spin_unlock(&req->rq_lock);
1982 * Interrupts (sets interrupted flag) all uncompleted requests in
1983 * a set \a data. Callback for l_wait_event for interruptible waits.
1985 void ptlrpc_interrupted_set(void *data)
1987 struct ptlrpc_request_set *set = data;
1990 LASSERT(set != NULL);
1991 CDEBUG(D_RPCTRACE, "INTERRUPTED SET %p\n", set);
1993 cfs_list_for_each(tmp, &set->set_requests) {
1994 struct ptlrpc_request *req =
1995 cfs_list_entry(tmp, struct ptlrpc_request,
1998 if (req->rq_phase != RQ_PHASE_RPC &&
1999 req->rq_phase != RQ_PHASE_UNREGISTERING)
2002 ptlrpc_mark_interrupted(req);
2007 * Get the smallest timeout in the set; this does NOT set a timeout.
2009 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
2012 time_t now = cfs_time_current_sec();
2014 struct ptlrpc_request *req;
2018 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
2020 cfs_list_for_each(tmp, &set->set_requests) {
2021 req = cfs_list_entry(tmp, struct ptlrpc_request, rq_set_chain);
2024 * Request in-flight?
2026 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
2027 (req->rq_phase == RQ_PHASE_BULK) ||
2028 (req->rq_phase == RQ_PHASE_NEW)))
2032 * Already timed out.
2034 if (req->rq_timedout)
2040 if (req->rq_wait_ctx)
2043 if (req->rq_phase == RQ_PHASE_NEW)
2044 deadline = req->rq_sent;
2046 deadline = req->rq_sent + req->rq_timeout;
2048 if (deadline <= now) /* actually expired already */
2049 timeout = 1; /* ASAP */
2050 else if (timeout == 0 || timeout > deadline - now)
2051 timeout = deadline - now;
2057 * Send all unset request from the set and then wait untill all
2058 * requests in the set complete (either get a reply, timeout, get an
2059 * error or otherwise be interrupted).
2060 * Returns 0 on success or error code otherwise.
2062 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
2065 struct ptlrpc_request *req;
2066 struct l_wait_info lwi;
2070 if (set->set_producer)
2071 (void)ptlrpc_set_producer(set);
2073 cfs_list_for_each(tmp, &set->set_requests) {
2074 req = cfs_list_entry(tmp, struct ptlrpc_request,
2076 if (req->rq_phase == RQ_PHASE_NEW)
2077 (void)ptlrpc_send_new_req(req);
2080 if (cfs_list_empty(&set->set_requests))
2084 timeout = ptlrpc_set_next_timeout(set);
2086 /* wait until all complete, interrupted, or an in-flight
2088 CDEBUG(D_RPCTRACE, "set %p going to sleep for %d seconds\n",
2091 if (timeout == 0 && !cfs_signal_pending())
2093 * No requests are in-flight (ether timed out
2094 * or delayed), so we can allow interrupts.
2095 * We still want to block for a limited time,
2096 * so we allow interrupts during the timeout.
2098 lwi = LWI_TIMEOUT_INTR_ALL(cfs_time_seconds(1),
2100 ptlrpc_interrupted_set, set);
2103 * At least one request is in flight, so no
2104 * interrupts are allowed. Wait until all
2105 * complete, or an in-flight req times out.
2107 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout? timeout : 1),
2108 ptlrpc_expired_set, set);
2110 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(NULL, set), &lwi);
2112 /* LU-769 - if we ignored the signal because it was already
2113 * pending when we started, we need to handle it now or we risk
2114 * it being ignored forever */
2115 if (rc == -ETIMEDOUT && !lwi.lwi_allow_intr &&
2116 cfs_signal_pending()) {
2117 cfs_sigset_t blocked_sigs =
2118 cfs_block_sigsinv(LUSTRE_FATAL_SIGS);
2120 /* In fact we only interrupt for the "fatal" signals
2121 * like SIGINT or SIGKILL. We still ignore less
2122 * important signals since ptlrpc set is not easily
2123 * reentrant from userspace again */
2124 if (cfs_signal_pending())
2125 ptlrpc_interrupted_set(set);
2126 cfs_restore_sigs(blocked_sigs);
2129 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
2131 /* -EINTR => all requests have been flagged rq_intr so next
2133 * -ETIMEDOUT => someone timed out. When all reqs have
2134 * timed out, signals are enabled allowing completion with
2136 * I don't really care if we go once more round the loop in
2137 * the error cases -eeb. */
2138 if (rc == 0 && cfs_atomic_read(&set->set_remaining) == 0) {
2139 cfs_list_for_each(tmp, &set->set_requests) {
2140 req = cfs_list_entry(tmp, struct ptlrpc_request,
2142 cfs_spin_lock(&req->rq_lock);
2143 req->rq_invalid_rqset = 1;
2144 cfs_spin_unlock(&req->rq_lock);
2147 } while (rc != 0 || cfs_atomic_read(&set->set_remaining) != 0);
2149 LASSERT(cfs_atomic_read(&set->set_remaining) == 0);
2151 rc = set->set_rc; /* rq_status of already freed requests if any */
2152 cfs_list_for_each(tmp, &set->set_requests) {
2153 req = cfs_list_entry(tmp, struct ptlrpc_request, rq_set_chain);
2155 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
2156 if (req->rq_status != 0)
2157 rc = req->rq_status;
2160 if (set->set_interpret != NULL) {
2161 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
2163 rc = interpreter (set, set->set_arg, rc);
2165 struct ptlrpc_set_cbdata *cbdata, *n;
2168 cfs_list_for_each_entry_safe(cbdata, n,
2169 &set->set_cblist, psc_item) {
2170 cfs_list_del_init(&cbdata->psc_item);
2171 err = cbdata->psc_interpret(set, cbdata->psc_data, rc);
2174 OBD_FREE_PTR(cbdata);
2182 * Helper fuction for request freeing.
2183 * Called when request count reached zero and request needs to be freed.
2184 * Removes request from all sorts of sending/replay lists it might be on,
2185 * frees network buffers if any are present.
2186 * If \a locked is set, that means caller is already holding import imp_lock
2187 * and so we no longer need to reobtain it (for certain lists manipulations)
2189 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
2192 if (request == NULL) {
2197 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
2198 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
2199 LASSERTF(cfs_list_empty(&request->rq_list), "req %p\n", request);
2200 LASSERTF(cfs_list_empty(&request->rq_set_chain), "req %p\n", request);
2201 LASSERTF(cfs_list_empty(&request->rq_exp_list), "req %p\n", request);
2202 LASSERTF(!request->rq_replay, "req %p\n", request);
2204 req_capsule_fini(&request->rq_pill);
2206 /* We must take it off the imp_replay_list first. Otherwise, we'll set
2207 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
2208 if (request->rq_import != NULL) {
2210 cfs_spin_lock(&request->rq_import->imp_lock);
2211 cfs_list_del_init(&request->rq_replay_list);
2213 cfs_spin_unlock(&request->rq_import->imp_lock);
2215 LASSERTF(cfs_list_empty(&request->rq_replay_list), "req %p\n", request);
2217 if (cfs_atomic_read(&request->rq_refcount) != 0) {
2218 DEBUG_REQ(D_ERROR, request,
2219 "freeing request with nonzero refcount");
2223 if (request->rq_repbuf != NULL)
2224 sptlrpc_cli_free_repbuf(request);
2225 if (request->rq_export != NULL) {
2226 class_export_put(request->rq_export);
2227 request->rq_export = NULL;
2229 if (request->rq_import != NULL) {
2230 class_import_put(request->rq_import);
2231 request->rq_import = NULL;
2233 if (request->rq_bulk != NULL)
2234 ptlrpc_free_bulk(request->rq_bulk);
2236 if (request->rq_reqbuf != NULL || request->rq_clrbuf != NULL)
2237 sptlrpc_cli_free_reqbuf(request);
2239 if (request->rq_cli_ctx)
2240 sptlrpc_req_put_ctx(request, !locked);
2242 if (request->rq_pool)
2243 __ptlrpc_free_req_to_pool(request);
2245 OBD_FREE(request, sizeof(*request));
2249 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
2251 * Drop one request reference. Must be called with import imp_lock held.
2252 * When reference count drops to zero, reuqest is freed.
2254 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
2256 LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
2257 (void)__ptlrpc_req_finished(request, 1);
2262 * Drops one reference count for request \a request.
2263 * \a locked set indicates that caller holds import imp_lock.
2264 * Frees the request whe reference count reaches zero.
2266 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
2269 if (request == NULL)
2272 if (request == LP_POISON ||
2273 request->rq_reqmsg == LP_POISON) {
2274 CERROR("dereferencing freed request (bug 575)\n");
2279 DEBUG_REQ(D_INFO, request, "refcount now %u",
2280 cfs_atomic_read(&request->rq_refcount) - 1);
2282 if (cfs_atomic_dec_and_test(&request->rq_refcount)) {
2283 __ptlrpc_free_req(request, locked);
2291 * Drops one reference count for a request.
2293 void ptlrpc_req_finished(struct ptlrpc_request *request)
2295 __ptlrpc_req_finished(request, 0);
2299 * Returns xid of a \a request
2301 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
2303 return request->rq_xid;
2305 EXPORT_SYMBOL(ptlrpc_req_xid);
2308 * Disengage the client's reply buffer from the network
2309 * NB does _NOT_ unregister any client-side bulk.
2310 * IDEMPOTENT, but _not_ safe against concurrent callers.
2311 * The request owner (i.e. the thread doing the I/O) must call...
2312 * Returns 0 on success or 1 if unregistering cannot be made.
2314 int ptlrpc_unregister_reply(struct ptlrpc_request *request, int async)
2318 struct l_wait_info lwi;
2323 LASSERT(!cfs_in_interrupt());
2326 * Let's setup deadline for reply unlink.
2328 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
2329 async && request->rq_reply_deadline == 0)
2330 request->rq_reply_deadline = cfs_time_current_sec()+LONG_UNLINK;
2333 * Nothing left to do.
2335 if (!ptlrpc_client_recv_or_unlink(request))
2338 LNetMDUnlink(request->rq_reply_md_h);
2341 * Let's check it once again.
2343 if (!ptlrpc_client_recv_or_unlink(request))
2347 * Move to "Unregistering" phase as reply was not unlinked yet.
2349 ptlrpc_rqphase_move(request, RQ_PHASE_UNREGISTERING);
2352 * Do not wait for unlink to finish.
2358 * We have to l_wait_event() whatever the result, to give liblustre
2359 * a chance to run reply_in_callback(), and to make sure we've
2360 * unlinked before returning a req to the pool.
2362 if (request->rq_set != NULL)
2363 wq = &request->rq_set->set_waitq;
2365 wq = &request->rq_reply_waitq;
2368 /* Network access will complete in finite time but the HUGE
2369 * timeout lets us CWARN for visibility of sluggish NALs */
2370 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
2371 cfs_time_seconds(1), NULL, NULL);
2372 rc = l_wait_event(*wq, !ptlrpc_client_recv_or_unlink(request),
2375 ptlrpc_rqphase_move(request, request->rq_next_phase);
2379 LASSERT(rc == -ETIMEDOUT);
2380 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout "
2381 "rvcng=%d unlnk=%d", request->rq_receiving_reply,
2382 request->rq_must_unlink);
2388 * Iterates through replay_list on import and prunes
2389 * all requests have transno smaller than last_committed for the
2390 * import and don't have rq_replay set.
2391 * Since requests are sorted in transno order, stops when meetign first
2392 * transno bigger than last_committed.
2393 * caller must hold imp->imp_lock
2395 void ptlrpc_free_committed(struct obd_import *imp)
2397 cfs_list_t *tmp, *saved;
2398 struct ptlrpc_request *req;
2399 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
2402 LASSERT(imp != NULL);
2404 LASSERT_SPIN_LOCKED(&imp->imp_lock);
2407 if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
2408 imp->imp_generation == imp->imp_last_generation_checked) {
2409 CDEBUG(D_INFO, "%s: skip recheck: last_committed "LPU64"\n",
2410 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
2414 CDEBUG(D_RPCTRACE, "%s: committing for last_committed "LPU64" gen %d\n",
2415 imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
2416 imp->imp_generation);
2417 imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
2418 imp->imp_last_generation_checked = imp->imp_generation;
2420 cfs_list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
2421 req = cfs_list_entry(tmp, struct ptlrpc_request,
2424 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
2425 LASSERT(req != last_req);
2428 if (req->rq_transno == 0) {
2429 DEBUG_REQ(D_EMERG, req, "zero transno during replay");
2432 if (req->rq_import_generation < imp->imp_generation) {
2433 DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
2437 if (req->rq_replay) {
2438 DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
2442 /* not yet committed */
2443 if (req->rq_transno > imp->imp_peer_committed_transno) {
2444 DEBUG_REQ(D_RPCTRACE, req, "stopping search");
2448 DEBUG_REQ(D_INFO, req, "commit (last_committed "LPU64")",
2449 imp->imp_peer_committed_transno);
2451 cfs_spin_lock(&req->rq_lock);
2453 cfs_spin_unlock(&req->rq_lock);
2454 if (req->rq_commit_cb != NULL)
2455 req->rq_commit_cb(req);
2456 cfs_list_del_init(&req->rq_replay_list);
2457 __ptlrpc_req_finished(req, 1);
2464 void ptlrpc_cleanup_client(struct obd_import *imp)
2472 * Schedule previously sent request for resend.
2473 * For bulk requests we assign new xid (to avoid problems with
2474 * lost replies and therefore several transfers landing into same buffer
2475 * from different sending attempts).
2477 void ptlrpc_resend_req(struct ptlrpc_request *req)
2479 DEBUG_REQ(D_HA, req, "going to resend");
2480 lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
2481 req->rq_status = -EAGAIN;
2483 cfs_spin_lock(&req->rq_lock);
2485 req->rq_net_err = 0;
2486 req->rq_timedout = 0;
2488 __u64 old_xid = req->rq_xid;
2490 /* ensure previous bulk fails */
2491 req->rq_xid = ptlrpc_next_xid();
2492 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
2493 old_xid, req->rq_xid);
2495 ptlrpc_client_wake_req(req);
2496 cfs_spin_unlock(&req->rq_lock);
2499 /* XXX: this function and rq_status are currently unused */
2500 void ptlrpc_restart_req(struct ptlrpc_request *req)
2502 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
2503 req->rq_status = -ERESTARTSYS;
2505 cfs_spin_lock(&req->rq_lock);
2506 req->rq_restart = 1;
2507 req->rq_timedout = 0;
2508 ptlrpc_client_wake_req(req);
2509 cfs_spin_unlock(&req->rq_lock);
2513 * Grab additional reference on a request \a req
2515 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
2518 cfs_atomic_inc(&req->rq_refcount);
2523 * Add a request to import replay_list.
2524 * Must be called under imp_lock
2526 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
2527 struct obd_import *imp)
2531 LASSERT_SPIN_LOCKED(&imp->imp_lock);
2533 if (req->rq_transno == 0) {
2534 DEBUG_REQ(D_EMERG, req, "saving request with zero transno");
2538 /* clear this for new requests that were resent as well
2539 as resent replayed requests. */
2540 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2542 /* don't re-add requests that have been replayed */
2543 if (!cfs_list_empty(&req->rq_replay_list))
2546 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
2548 LASSERT(imp->imp_replayable);
2549 /* Balanced in ptlrpc_free_committed, usually. */
2550 ptlrpc_request_addref(req);
2551 cfs_list_for_each_prev(tmp, &imp->imp_replay_list) {
2552 struct ptlrpc_request *iter =
2553 cfs_list_entry(tmp, struct ptlrpc_request,
2556 /* We may have duplicate transnos if we create and then
2557 * open a file, or for closes retained if to match creating
2558 * opens, so use req->rq_xid as a secondary key.
2559 * (See bugs 684, 685, and 428.)
2560 * XXX no longer needed, but all opens need transnos!
2562 if (iter->rq_transno > req->rq_transno)
2565 if (iter->rq_transno == req->rq_transno) {
2566 LASSERT(iter->rq_xid != req->rq_xid);
2567 if (iter->rq_xid > req->rq_xid)
2571 cfs_list_add(&req->rq_replay_list, &iter->rq_replay_list);
2575 cfs_list_add(&req->rq_replay_list, &imp->imp_replay_list);
2579 * Send request and wait until it completes.
2580 * Returns request processing status.
2582 int ptlrpc_queue_wait(struct ptlrpc_request *req)
2584 struct ptlrpc_request_set *set;
2588 LASSERT(req->rq_set == NULL);
2589 LASSERT(!req->rq_receiving_reply);
2591 set = ptlrpc_prep_set();
2593 CERROR("Unable to allocate ptlrpc set.");
2597 /* for distributed debugging */
2598 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
2600 /* add a ref for the set (see comment in ptlrpc_set_add_req) */
2601 ptlrpc_request_addref(req);
2602 ptlrpc_set_add_req(set, req);
2603 rc = ptlrpc_set_wait(set);
2604 ptlrpc_set_destroy(set);
2609 struct ptlrpc_replay_async_args {
2611 int praa_old_status;
2615 * Callback used for replayed requests reply processing.
2616 * In case of succesful reply calls registeresd request replay callback.
2617 * In case of error restart replay process.
2619 static int ptlrpc_replay_interpret(const struct lu_env *env,
2620 struct ptlrpc_request *req,
2621 void * data, int rc)
2623 struct ptlrpc_replay_async_args *aa = data;
2624 struct obd_import *imp = req->rq_import;
2627 cfs_atomic_dec(&imp->imp_replay_inflight);
2629 if (!ptlrpc_client_replied(req)) {
2630 CERROR("request replay timed out, restarting recovery\n");
2631 GOTO(out, rc = -ETIMEDOUT);
2634 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
2635 (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
2636 lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
2637 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2639 /** VBR: check version failure */
2640 if (lustre_msg_get_status(req->rq_repmsg) == -EOVERFLOW) {
2641 /** replay was failed due to version mismatch */
2642 DEBUG_REQ(D_WARNING, req, "Version mismatch during replay\n");
2643 cfs_spin_lock(&imp->imp_lock);
2644 imp->imp_vbr_failed = 1;
2645 imp->imp_no_lock_replay = 1;
2646 cfs_spin_unlock(&imp->imp_lock);
2647 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2649 /** The transno had better not change over replay. */
2650 LASSERTF(lustre_msg_get_transno(req->rq_reqmsg) ==
2651 lustre_msg_get_transno(req->rq_repmsg) ||
2652 lustre_msg_get_transno(req->rq_repmsg) == 0,
2654 lustre_msg_get_transno(req->rq_reqmsg),
2655 lustre_msg_get_transno(req->rq_repmsg));
2658 cfs_spin_lock(&imp->imp_lock);
2659 /** if replays by version then gap was occur on server, no trust to locks */
2660 if (lustre_msg_get_flags(req->rq_repmsg) & MSG_VERSION_REPLAY)
2661 imp->imp_no_lock_replay = 1;
2662 imp->imp_last_replay_transno = lustre_msg_get_transno(req->rq_reqmsg);
2663 cfs_spin_unlock(&imp->imp_lock);
2664 LASSERT(imp->imp_last_replay_transno);
2666 /* transaction number shouldn't be bigger than the latest replayed */
2667 if (req->rq_transno > lustre_msg_get_transno(req->rq_reqmsg)) {
2668 DEBUG_REQ(D_ERROR, req,
2669 "Reported transno "LPU64" is bigger than the "
2670 "replayed one: "LPU64, req->rq_transno,
2671 lustre_msg_get_transno(req->rq_reqmsg));
2672 GOTO(out, rc = -EINVAL);
2675 DEBUG_REQ(D_HA, req, "got rep");
2677 /* let the callback do fixups, possibly including in the request */
2678 if (req->rq_replay_cb)
2679 req->rq_replay_cb(req);
2681 if (ptlrpc_client_replied(req) &&
2682 lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
2683 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
2684 lustre_msg_get_status(req->rq_repmsg),
2685 aa->praa_old_status);
2687 /* Put it back for re-replay. */
2688 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2692 * Errors while replay can set transno to 0, but
2693 * imp_last_replay_transno shouldn't be set to 0 anyway
2695 if (req->rq_transno == 0)
2696 CERROR("Transno is 0 during replay!\n");
2698 /* continue with recovery */
2699 rc = ptlrpc_import_recovery_state_machine(imp);
2701 req->rq_send_state = aa->praa_old_state;
2704 /* this replay failed, so restart recovery */
2705 ptlrpc_connect_import(imp);
2711 * Prepares and queues request for replay.
2712 * Adds it to ptlrpcd queue for actual sending.
2713 * Returns 0 on success.
2715 int ptlrpc_replay_req(struct ptlrpc_request *req)
2717 struct ptlrpc_replay_async_args *aa;
2720 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
2722 LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
2723 aa = ptlrpc_req_async_args(req);
2724 memset(aa, 0, sizeof *aa);
2726 /* Prepare request to be resent with ptlrpcd */
2727 aa->praa_old_state = req->rq_send_state;
2728 req->rq_send_state = LUSTRE_IMP_REPLAY;
2729 req->rq_phase = RQ_PHASE_NEW;
2730 req->rq_next_phase = RQ_PHASE_UNDEFINED;
2732 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
2734 req->rq_interpret_reply = ptlrpc_replay_interpret;
2735 /* Readjust the timeout for current conditions */
2736 ptlrpc_at_set_req_timeout(req);
2738 /* Tell server the net_latency, so the server can calculate how long
2739 * it should wait for next replay */
2740 lustre_msg_set_service_time(req->rq_reqmsg,
2741 ptlrpc_at_get_net_latency(req));
2742 DEBUG_REQ(D_HA, req, "REPLAY");
2744 cfs_atomic_inc(&req->rq_import->imp_replay_inflight);
2745 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
2747 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2752 * Aborts all in-flight request on import \a imp sending and delayed lists
2754 void ptlrpc_abort_inflight(struct obd_import *imp)
2756 cfs_list_t *tmp, *n;
2759 /* Make sure that no new requests get processed for this import.
2760 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
2761 * this flag and then putting requests on sending_list or delayed_list.
2763 cfs_spin_lock(&imp->imp_lock);
2765 /* XXX locking? Maybe we should remove each request with the list
2766 * locked? Also, how do we know if the requests on the list are
2767 * being freed at this time?
2769 cfs_list_for_each_safe(tmp, n, &imp->imp_sending_list) {
2770 struct ptlrpc_request *req =
2771 cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
2773 DEBUG_REQ(D_RPCTRACE, req, "inflight");
2775 cfs_spin_lock (&req->rq_lock);
2776 if (req->rq_import_generation < imp->imp_generation) {
2778 req->rq_status = -EIO;
2779 ptlrpc_client_wake_req(req);
2781 cfs_spin_unlock (&req->rq_lock);
2784 cfs_list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
2785 struct ptlrpc_request *req =
2786 cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
2788 DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
2790 cfs_spin_lock (&req->rq_lock);
2791 if (req->rq_import_generation < imp->imp_generation) {
2793 req->rq_status = -EIO;
2794 ptlrpc_client_wake_req(req);
2796 cfs_spin_unlock (&req->rq_lock);
2799 /* Last chance to free reqs left on the replay list, but we
2800 * will still leak reqs that haven't committed. */
2801 if (imp->imp_replayable)
2802 ptlrpc_free_committed(imp);
2804 cfs_spin_unlock(&imp->imp_lock);
2810 * Abort all uncompleted requests in request set \a set
2812 void ptlrpc_abort_set(struct ptlrpc_request_set *set)
2814 cfs_list_t *tmp, *pos;
2816 LASSERT(set != NULL);
2818 cfs_list_for_each_safe(pos, tmp, &set->set_requests) {
2819 struct ptlrpc_request *req =
2820 cfs_list_entry(pos, struct ptlrpc_request,
2823 cfs_spin_lock(&req->rq_lock);
2824 if (req->rq_phase != RQ_PHASE_RPC) {
2825 cfs_spin_unlock(&req->rq_lock);
2830 req->rq_status = -EINTR;
2831 ptlrpc_client_wake_req(req);
2832 cfs_spin_unlock(&req->rq_lock);
2836 static __u64 ptlrpc_last_xid;
2837 static cfs_spinlock_t ptlrpc_last_xid_lock;
2840 * Initialize the XID for the node. This is common among all requests on
2841 * this node, and only requires the property that it is monotonically
2842 * increasing. It does not need to be sequential. Since this is also used
2843 * as the RDMA match bits, it is important that a single client NOT have
2844 * the same match bits for two different in-flight requests, hence we do
2845 * NOT want to have an XID per target or similar.
2847 * To avoid an unlikely collision between match bits after a client reboot
2848 * (which would deliver old data into the wrong RDMA buffer) initialize
2849 * the XID based on the current time, assuming a maximum RPC rate of 1M RPC/s.
2850 * If the time is clearly incorrect, we instead use a 62-bit random number.
2851 * In the worst case the random number will overflow 1M RPCs per second in
2852 * 9133 years, or permutations thereof.
2854 #define YEAR_2004 (1ULL << 30)
2855 void ptlrpc_init_xid(void)
2857 time_t now = cfs_time_current_sec();
2859 cfs_spin_lock_init(&ptlrpc_last_xid_lock);
2860 if (now < YEAR_2004) {
2861 cfs_get_random_bytes(&ptlrpc_last_xid, sizeof(ptlrpc_last_xid));
2862 ptlrpc_last_xid >>= 2;
2863 ptlrpc_last_xid |= (1ULL << 61);
2865 ptlrpc_last_xid = (__u64)now << 20;
2870 * Increase xid and returns resultng new value to the caller.
2872 __u64 ptlrpc_next_xid(void)
2875 cfs_spin_lock(&ptlrpc_last_xid_lock);
2876 tmp = ++ptlrpc_last_xid;
2877 cfs_spin_unlock(&ptlrpc_last_xid_lock);
2882 * Get a glimpse at what next xid value might have been.
2883 * Returns possible next xid.
2885 __u64 ptlrpc_sample_next_xid(void)
2887 #if BITS_PER_LONG == 32
2888 /* need to avoid possible word tearing on 32-bit systems */
2890 cfs_spin_lock(&ptlrpc_last_xid_lock);
2891 tmp = ptlrpc_last_xid + 1;
2892 cfs_spin_unlock(&ptlrpc_last_xid_lock);
2895 /* No need to lock, since returned value is racy anyways */
2896 return ptlrpc_last_xid + 1;
2899 EXPORT_SYMBOL(ptlrpc_sample_next_xid);
2902 * Functions for operating ptlrpc workers.
2904 * A ptlrpc work is a function which will be running inside ptlrpc context.
2905 * The callback shouldn't sleep otherwise it will block that ptlrpcd thread.
2907 * 1. after a work is created, it can be used many times, that is:
2908 * handler = ptlrpcd_alloc_work();
2909 * ptlrpcd_queue_work();
2911 * queue it again when necessary:
2912 * ptlrpcd_queue_work();
2913 * ptlrpcd_destroy_work();
2914 * 2. ptlrpcd_queue_work() can be called by multiple processes meanwhile, but
2915 * it will only be queued once in any time. Also as its name implies, it may
2916 * have delay before it really runs by ptlrpcd thread.
2918 struct ptlrpc_work_async_args {
2920 int (*cb)(const struct lu_env *, void *);
2924 #define PTLRPC_WORK_MAGIC 0x6655436b676f4f44ULL /* magic code */
2926 static int work_interpreter(const struct lu_env *env,
2927 struct ptlrpc_request *req, void *data, int rc)
2929 struct ptlrpc_work_async_args *arg = data;
2931 LASSERT(arg->magic == PTLRPC_WORK_MAGIC);
2932 LASSERT(arg->cb != NULL);
2934 return arg->cb(env, arg->cbdata);
2938 * Create a work for ptlrpc.
2940 void *ptlrpcd_alloc_work(struct obd_import *imp,
2941 int (*cb)(const struct lu_env *, void *), void *cbdata)
2943 struct ptlrpc_request *req = NULL;
2944 struct ptlrpc_work_async_args *args;
2950 RETURN(ERR_PTR(-EINVAL));
2952 /* copy some code from deprecated fakereq. */
2955 CERROR("ptlrpc: run out of memory!\n");
2956 RETURN(ERR_PTR(-ENOMEM));
2959 req->rq_send_state = LUSTRE_IMP_FULL;
2960 req->rq_type = PTL_RPC_MSG_REQUEST;
2961 req->rq_import = class_import_get(imp);
2962 req->rq_export = NULL;
2963 req->rq_interpret_reply = work_interpreter;
2964 /* don't want reply */
2965 req->rq_receiving_reply = 0;
2966 req->rq_must_unlink = 0;
2967 req->rq_no_delay = req->rq_no_resend = 1;
2969 cfs_spin_lock_init(&req->rq_lock);
2970 CFS_INIT_LIST_HEAD(&req->rq_list);
2971 CFS_INIT_LIST_HEAD(&req->rq_replay_list);
2972 CFS_INIT_LIST_HEAD(&req->rq_set_chain);
2973 CFS_INIT_LIST_HEAD(&req->rq_history_list);
2974 CFS_INIT_LIST_HEAD(&req->rq_exp_list);
2975 cfs_waitq_init(&req->rq_reply_waitq);
2976 cfs_waitq_init(&req->rq_set_waitq);
2977 cfs_atomic_set(&req->rq_refcount, 1);
2979 CLASSERT (sizeof(*args) <= sizeof(req->rq_async_args));
2980 args = ptlrpc_req_async_args(req);
2981 args->magic = PTLRPC_WORK_MAGIC;
2983 args->cbdata = cbdata;
2987 EXPORT_SYMBOL(ptlrpcd_alloc_work);
2989 void ptlrpcd_destroy_work(void *handler)
2991 struct ptlrpc_request *req = handler;
2994 ptlrpc_req_finished(req);
2996 EXPORT_SYMBOL(ptlrpcd_destroy_work);
2998 int ptlrpcd_queue_work(void *handler)
3000 struct ptlrpc_request *req = handler;
3003 * Check if the req is already being queued.
3005 * Here comes a trick: it lacks a way of checking if a req is being
3006 * processed reliably in ptlrpc. Here I have to use refcount of req
3007 * for this purpose. This is okay because the caller should use this
3008 * req as opaque data. - Jinshan
3010 LASSERT(cfs_atomic_read(&req->rq_refcount) > 0);
3011 if (cfs_atomic_read(&req->rq_refcount) > 1)
3014 if (cfs_atomic_inc_return(&req->rq_refcount) > 2) { /* race */
3015 cfs_atomic_dec(&req->rq_refcount);
3019 /* re-initialize the req */
3020 req->rq_timeout = obd_timeout;
3021 req->rq_sent = cfs_time_current_sec();
3022 req->rq_deadline = req->rq_sent + req->rq_timeout;
3023 req->rq_reply_deadline = req->rq_deadline;
3024 req->rq_phase = RQ_PHASE_INTERPRET;
3025 req->rq_next_phase = RQ_PHASE_COMPLETE;
3026 req->rq_xid = ptlrpc_next_xid();
3027 req->rq_import_generation = req->rq_import->imp_generation;
3029 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
3032 EXPORT_SYMBOL(ptlrpcd_queue_work);