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"
56 * Initialize passed in client structure \a cl.
58 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
59 struct ptlrpc_client *cl)
61 cl->cli_request_portal = req_portal;
62 cl->cli_reply_portal = rep_portal;
67 * Return PortalRPC connection for remore uud \a uuid
69 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
71 struct ptlrpc_connection *c;
73 lnet_process_id_t peer;
76 err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
78 CNETERR("cannot find peer %s!\n", uuid->uuid);
82 c = ptlrpc_connection_get(peer, self, uuid);
84 memcpy(c->c_remote_uuid.uuid,
85 uuid->uuid, sizeof(c->c_remote_uuid.uuid));
88 CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
94 * Allocate and initialize new bulk descriptor
95 * Returns pointer to the descriptor or NULL on error.
97 struct ptlrpc_bulk_desc *new_bulk(int npages, int type, int portal)
99 struct ptlrpc_bulk_desc *desc;
101 OBD_ALLOC(desc, offsetof (struct ptlrpc_bulk_desc, bd_iov[npages]));
105 cfs_spin_lock_init(&desc->bd_lock);
106 cfs_waitq_init(&desc->bd_waitq);
107 desc->bd_max_iov = npages;
108 desc->bd_iov_count = 0;
109 LNetInvalidateHandle(&desc->bd_md_h);
110 desc->bd_portal = portal;
111 desc->bd_type = type;
117 * Prepare bulk descriptor for specified outgoing request \a req that
118 * can fit \a npages * pages. \a type is bulk type. \a portal is where
119 * the bulk to be sent. Used on client-side.
120 * Returns pointer to newly allocatrd initialized bulk descriptor or NULL on
123 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp(struct ptlrpc_request *req,
124 int npages, int type, int portal)
126 struct obd_import *imp = req->rq_import;
127 struct ptlrpc_bulk_desc *desc;
130 LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
131 desc = new_bulk(npages, type, portal);
135 desc->bd_import_generation = req->rq_import_generation;
136 desc->bd_import = class_import_get(imp);
139 desc->bd_cbid.cbid_fn = client_bulk_callback;
140 desc->bd_cbid.cbid_arg = desc;
142 /* This makes req own desc, and free it when she frees herself */
149 * Add a page \a page to the bulk descriptor \a desc.
150 * Data to transfer in the page starts at offset \a pageoffset and
151 * amount of data to transfer from the page is \a len
153 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
154 cfs_page_t *page, int pageoffset, int len)
156 LASSERT(desc->bd_iov_count < desc->bd_max_iov);
157 LASSERT(page != NULL);
158 LASSERT(pageoffset >= 0);
160 LASSERT(pageoffset + len <= CFS_PAGE_SIZE);
165 ptlrpc_add_bulk_page(desc, page, pageoffset, len);
169 * Uninitialize and free bulk descriptor \a desc.
170 * Works on bulk descriptors both from server and client side.
172 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
177 LASSERT(desc != NULL);
178 LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
179 LASSERT(!desc->bd_network_rw); /* network hands off or */
180 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
182 sptlrpc_enc_pool_put_pages(desc);
185 class_export_put(desc->bd_export);
187 class_import_put(desc->bd_import);
189 for (i = 0; i < desc->bd_iov_count ; i++)
190 cfs_page_unpin(desc->bd_iov[i].kiov_page);
192 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
193 bd_iov[desc->bd_max_iov]));
198 * Set server timelimit for this req, i.e. how long are we willing to wait
199 * for reply before timing out this request.
201 void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req)
207 LASSERT(req->rq_import);
210 /* non-AT settings */
212 * \a imp_server_timeout means this is reverse import and
213 * we send (currently only) ASTs to the client and cannot afford
214 * to wait too long for the reply, otherwise the other client
215 * (because of which we are sending this request) would
216 * timeout waiting for us
218 req->rq_timeout = req->rq_import->imp_server_timeout ?
219 obd_timeout / 2 : obd_timeout;
221 at = &req->rq_import->imp_at;
222 idx = import_at_get_index(req->rq_import,
223 req->rq_request_portal);
224 serv_est = at_get(&at->iat_service_estimate[idx]);
225 req->rq_timeout = at_est2timeout(serv_est);
227 /* We could get even fancier here, using history to predict increased
230 /* Let the server know what this RPC timeout is by putting it in the
232 lustre_msg_set_timeout(req->rq_reqmsg, req->rq_timeout);
235 /* Adjust max service estimate based on server value */
236 static void ptlrpc_at_adj_service(struct ptlrpc_request *req,
237 unsigned int serv_est)
243 LASSERT(req->rq_import);
244 at = &req->rq_import->imp_at;
246 idx = import_at_get_index(req->rq_import, req->rq_request_portal);
247 /* max service estimates are tracked on the server side,
248 so just keep minimal history here */
249 oldse = at_measured(&at->iat_service_estimate[idx], serv_est);
251 CDEBUG(D_ADAPTTO, "The RPC service estimate for %s ptl %d "
252 "has changed from %d to %d\n",
253 req->rq_import->imp_obd->obd_name,req->rq_request_portal,
254 oldse, at_get(&at->iat_service_estimate[idx]));
257 /* Expected network latency per remote node (secs) */
258 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req)
260 return AT_OFF ? 0 : at_get(&req->rq_import->imp_at.iat_net_latency);
263 /* Adjust expected network latency */
264 static void ptlrpc_at_adj_net_latency(struct ptlrpc_request *req,
265 unsigned int service_time)
267 unsigned int nl, oldnl;
269 time_t now = cfs_time_current_sec();
271 LASSERT(req->rq_import);
272 at = &req->rq_import->imp_at;
274 /* Network latency is total time less server processing time */
275 nl = max_t(int, now - req->rq_sent - service_time, 0) +1/*st rounding*/;
276 if (service_time > now - req->rq_sent + 3 /* bz16408 */)
277 CWARN("Reported service time %u > total measured time "
278 CFS_DURATION_T"\n", service_time,
279 cfs_time_sub(now, req->rq_sent));
281 oldnl = at_measured(&at->iat_net_latency, nl);
283 CDEBUG(D_ADAPTTO, "The network latency for %s (nid %s) "
284 "has changed from %d to %d\n",
285 req->rq_import->imp_obd->obd_name,
287 &req->rq_import->imp_connection->c_remote_uuid),
288 oldnl, at_get(&at->iat_net_latency));
291 static int unpack_reply(struct ptlrpc_request *req)
295 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
296 rc = ptlrpc_unpack_rep_msg(req, req->rq_replen);
298 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
303 rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
305 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
312 * Handle an early reply message, called with the rq_lock held.
313 * If anything goes wrong just ignore it - same as if it never happened
315 static int ptlrpc_at_recv_early_reply(struct ptlrpc_request *req)
317 struct ptlrpc_request *early_req;
323 cfs_spin_unlock(&req->rq_lock);
325 rc = sptlrpc_cli_unwrap_early_reply(req, &early_req);
327 cfs_spin_lock(&req->rq_lock);
331 rc = unpack_reply(early_req);
333 /* Expecting to increase the service time estimate here */
334 ptlrpc_at_adj_service(req,
335 lustre_msg_get_timeout(early_req->rq_repmsg));
336 ptlrpc_at_adj_net_latency(req,
337 lustre_msg_get_service_time(early_req->rq_repmsg));
340 sptlrpc_cli_finish_early_reply(early_req);
342 cfs_spin_lock(&req->rq_lock);
345 /* Adjust the local timeout for this req */
346 ptlrpc_at_set_req_timeout(req);
348 olddl = req->rq_deadline;
349 /* server assumes it now has rq_timeout from when it sent the
350 early reply, so client should give it at least that long. */
351 req->rq_deadline = cfs_time_current_sec() + req->rq_timeout +
352 ptlrpc_at_get_net_latency(req);
354 DEBUG_REQ(D_ADAPTTO, req,
355 "Early reply #%d, new deadline in "CFS_DURATION_T"s "
356 "("CFS_DURATION_T"s)", req->rq_early_count,
357 cfs_time_sub(req->rq_deadline,
358 cfs_time_current_sec()),
359 cfs_time_sub(req->rq_deadline, olddl));
366 * Wind down request pool \a pool.
367 * Frees all requests from the pool too
369 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
372 struct ptlrpc_request *req;
374 LASSERT(pool != NULL);
376 cfs_spin_lock(&pool->prp_lock);
377 cfs_list_for_each_safe(l, tmp, &pool->prp_req_list) {
378 req = cfs_list_entry(l, struct ptlrpc_request, rq_list);
379 cfs_list_del(&req->rq_list);
380 LASSERT(req->rq_reqbuf);
381 LASSERT(req->rq_reqbuf_len == pool->prp_rq_size);
382 OBD_FREE_LARGE(req->rq_reqbuf, pool->prp_rq_size);
383 OBD_FREE(req, sizeof(*req));
385 cfs_spin_unlock(&pool->prp_lock);
386 OBD_FREE(pool, sizeof(*pool));
390 * Allocates, initializes and adds \a num_rq requests to the pool \a pool
392 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
397 while (size < pool->prp_rq_size)
400 LASSERTF(cfs_list_empty(&pool->prp_req_list) ||
401 size == pool->prp_rq_size,
402 "Trying to change pool size with nonempty pool "
403 "from %d to %d bytes\n", pool->prp_rq_size, size);
405 cfs_spin_lock(&pool->prp_lock);
406 pool->prp_rq_size = size;
407 for (i = 0; i < num_rq; i++) {
408 struct ptlrpc_request *req;
409 struct lustre_msg *msg;
411 cfs_spin_unlock(&pool->prp_lock);
412 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
415 OBD_ALLOC_LARGE(msg, size);
417 OBD_FREE(req, sizeof(struct ptlrpc_request));
420 req->rq_reqbuf = msg;
421 req->rq_reqbuf_len = size;
423 cfs_spin_lock(&pool->prp_lock);
424 cfs_list_add_tail(&req->rq_list, &pool->prp_req_list);
426 cfs_spin_unlock(&pool->prp_lock);
431 * Create and initialize new request pool with given attributes:
432 * \a num_rq - initial number of requests to create for the pool
433 * \a msgsize - maximum message size possible for requests in thid pool
434 * \a populate_pool - function to be called when more requests need to be added
436 * Returns pointer to newly created pool or NULL on error.
438 struct ptlrpc_request_pool *
439 ptlrpc_init_rq_pool(int num_rq, int msgsize,
440 void (*populate_pool)(struct ptlrpc_request_pool *, int))
442 struct ptlrpc_request_pool *pool;
444 OBD_ALLOC(pool, sizeof (struct ptlrpc_request_pool));
448 /* Request next power of two for the allocation, because internally
449 kernel would do exactly this */
451 cfs_spin_lock_init(&pool->prp_lock);
452 CFS_INIT_LIST_HEAD(&pool->prp_req_list);
453 pool->prp_rq_size = msgsize + SPTLRPC_MAX_PAYLOAD;
454 pool->prp_populate = populate_pool;
456 populate_pool(pool, num_rq);
458 if (cfs_list_empty(&pool->prp_req_list)) {
459 /* have not allocated a single request for the pool */
460 OBD_FREE(pool, sizeof (struct ptlrpc_request_pool));
467 * Fetches one request from pool \a pool
469 static struct ptlrpc_request *
470 ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
472 struct ptlrpc_request *request;
473 struct lustre_msg *reqbuf;
478 cfs_spin_lock(&pool->prp_lock);
480 /* See if we have anything in a pool, and bail out if nothing,
481 * in writeout path, where this matters, this is safe to do, because
482 * nothing is lost in this case, and when some in-flight requests
483 * complete, this code will be called again. */
484 if (unlikely(cfs_list_empty(&pool->prp_req_list))) {
485 cfs_spin_unlock(&pool->prp_lock);
489 request = cfs_list_entry(pool->prp_req_list.next, struct ptlrpc_request,
491 cfs_list_del_init(&request->rq_list);
492 cfs_spin_unlock(&pool->prp_lock);
494 LASSERT(request->rq_reqbuf);
495 LASSERT(request->rq_pool);
497 reqbuf = request->rq_reqbuf;
498 memset(request, 0, sizeof(*request));
499 request->rq_reqbuf = reqbuf;
500 request->rq_reqbuf_len = pool->prp_rq_size;
501 request->rq_pool = pool;
507 * Returns freed \a request to pool.
509 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
511 struct ptlrpc_request_pool *pool = request->rq_pool;
513 cfs_spin_lock(&pool->prp_lock);
514 LASSERT(cfs_list_empty(&request->rq_list));
515 LASSERT(!request->rq_receiving_reply);
516 cfs_list_add_tail(&request->rq_list, &pool->prp_req_list);
517 cfs_spin_unlock(&pool->prp_lock);
520 static int __ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
521 __u32 version, int opcode,
522 int count, __u32 *lengths, char **bufs,
523 struct ptlrpc_cli_ctx *ctx)
525 struct obd_import *imp = request->rq_import;
530 request->rq_cli_ctx = sptlrpc_cli_ctx_get(ctx);
532 rc = sptlrpc_req_get_ctx(request);
537 sptlrpc_req_set_flavor(request, opcode);
539 rc = lustre_pack_request(request, imp->imp_msg_magic, count,
542 LASSERT(!request->rq_pool);
546 lustre_msg_add_version(request->rq_reqmsg, version);
547 request->rq_send_state = LUSTRE_IMP_FULL;
548 request->rq_type = PTL_RPC_MSG_REQUEST;
549 request->rq_export = NULL;
551 request->rq_req_cbid.cbid_fn = request_out_callback;
552 request->rq_req_cbid.cbid_arg = request;
554 request->rq_reply_cbid.cbid_fn = reply_in_callback;
555 request->rq_reply_cbid.cbid_arg = request;
557 request->rq_reply_deadline = 0;
558 request->rq_phase = RQ_PHASE_NEW;
559 request->rq_next_phase = RQ_PHASE_UNDEFINED;
561 request->rq_request_portal = imp->imp_client->cli_request_portal;
562 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
564 ptlrpc_at_set_req_timeout(request);
566 cfs_spin_lock_init(&request->rq_lock);
567 CFS_INIT_LIST_HEAD(&request->rq_list);
568 CFS_INIT_LIST_HEAD(&request->rq_timed_list);
569 CFS_INIT_LIST_HEAD(&request->rq_replay_list);
570 CFS_INIT_LIST_HEAD(&request->rq_ctx_chain);
571 CFS_INIT_LIST_HEAD(&request->rq_set_chain);
572 CFS_INIT_LIST_HEAD(&request->rq_history_list);
573 CFS_INIT_LIST_HEAD(&request->rq_exp_list);
574 cfs_waitq_init(&request->rq_reply_waitq);
575 cfs_waitq_init(&request->rq_set_waitq);
576 request->rq_xid = ptlrpc_next_xid();
577 cfs_atomic_set(&request->rq_refcount, 1);
579 lustre_msg_set_opc(request->rq_reqmsg, opcode);
583 sptlrpc_cli_ctx_put(request->rq_cli_ctx, 1);
585 class_import_put(imp);
589 int ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
590 __u32 version, int opcode, char **bufs,
591 struct ptlrpc_cli_ctx *ctx)
595 count = req_capsule_filled_sizes(&request->rq_pill, RCL_CLIENT);
596 return __ptlrpc_request_bufs_pack(request, version, opcode, count,
597 request->rq_pill.rc_area[RCL_CLIENT],
600 EXPORT_SYMBOL(ptlrpc_request_bufs_pack);
603 * Pack request buffers for network transfer, performing necessary encryption
604 * steps if necessary.
606 int ptlrpc_request_pack(struct ptlrpc_request *request,
607 __u32 version, int opcode)
609 return ptlrpc_request_bufs_pack(request, version, opcode, NULL, NULL);
613 * Helper function to allocate new request on import \a imp
614 * and possibly using existing request from pool \a pool if provided.
615 * Returns allocated request structure with import field filled or
619 struct ptlrpc_request *__ptlrpc_request_alloc(struct obd_import *imp,
620 struct ptlrpc_request_pool *pool)
622 struct ptlrpc_request *request = NULL;
625 request = ptlrpc_prep_req_from_pool(pool);
628 OBD_ALLOC_PTR(request);
631 LASSERTF((unsigned long)imp > 0x1000, "%p", imp);
632 LASSERT(imp != LP_POISON);
633 LASSERTF((unsigned long)imp->imp_client > 0x1000, "%p",
635 LASSERT(imp->imp_client != LP_POISON);
637 request->rq_import = class_import_get(imp);
639 CERROR("request allocation out of memory\n");
646 * Helper function for creating a request.
647 * Calls __ptlrpc_request_alloc to allocate new request sturcture and inits
648 * buffer structures according to capsule template \a format.
649 * Returns allocated request structure pointer or NULL on error.
651 static struct ptlrpc_request *
652 ptlrpc_request_alloc_internal(struct obd_import *imp,
653 struct ptlrpc_request_pool * pool,
654 const struct req_format *format)
656 struct ptlrpc_request *request;
658 request = __ptlrpc_request_alloc(imp, pool);
662 req_capsule_init(&request->rq_pill, request, RCL_CLIENT);
663 req_capsule_set(&request->rq_pill, format);
668 * Allocate new request structure for import \a imp and initialize its
669 * buffer structure according to capsule template \a format.
671 struct ptlrpc_request *ptlrpc_request_alloc(struct obd_import *imp,
672 const struct req_format *format)
674 return ptlrpc_request_alloc_internal(imp, NULL, format);
678 * Allocate new request structure for import \a imp from pool \a pool and
679 * initialize its buffer structure according to capsule template \a format.
681 struct ptlrpc_request *ptlrpc_request_alloc_pool(struct obd_import *imp,
682 struct ptlrpc_request_pool * pool,
683 const struct req_format *format)
685 return ptlrpc_request_alloc_internal(imp, pool, format);
689 * For requests not from pool, free memory of the request structure.
690 * For requests obtained from a pool earlier, return request back to pool.
692 void ptlrpc_request_free(struct ptlrpc_request *request)
694 if (request->rq_pool)
695 __ptlrpc_free_req_to_pool(request);
697 OBD_FREE_PTR(request);
701 * Allocate new request for operatione \a opcode and immediatelly pack it for
703 * Only used for simple requests like OBD_PING where the only important
704 * part of the request is operation itself.
705 * Returns allocated request or NULL on error.
707 struct ptlrpc_request *ptlrpc_request_alloc_pack(struct obd_import *imp,
708 const struct req_format *format,
709 __u32 version, int opcode)
711 struct ptlrpc_request *req = ptlrpc_request_alloc(imp, format);
715 rc = ptlrpc_request_pack(req, version, opcode);
717 ptlrpc_request_free(req);
725 * Prepare request (fetched from pool \a poolif not NULL) on import \a imp
726 * for operation \a opcode. Request would contain \a count buffers.
727 * Sizes of buffers are described in array \a lengths and buffers themselves
728 * are provided by a pointer \a bufs.
729 * Returns prepared request structure pointer or NULL on error.
731 struct ptlrpc_request *
732 ptlrpc_prep_req_pool(struct obd_import *imp,
733 __u32 version, int opcode,
734 int count, __u32 *lengths, char **bufs,
735 struct ptlrpc_request_pool *pool)
737 struct ptlrpc_request *request;
740 request = __ptlrpc_request_alloc(imp, pool);
744 rc = __ptlrpc_request_bufs_pack(request, version, opcode, count,
745 lengths, bufs, NULL);
747 ptlrpc_request_free(request);
754 * Same as ptlrpc_prep_req_pool, but without pool
756 struct ptlrpc_request *
757 ptlrpc_prep_req(struct obd_import *imp, __u32 version, int opcode, int count,
758 __u32 *lengths, char **bufs)
760 return ptlrpc_prep_req_pool(imp, version, opcode, count, lengths, bufs,
765 * Allocate "fake" request that would not be sent anywhere in the end.
766 * Only used as a hack because we have no other way of performing
767 * async actions in lustre between layers.
768 * Used on MDS to request object preallocations from more than one OST at a
771 struct ptlrpc_request *ptlrpc_prep_fakereq(struct obd_import *imp,
772 unsigned int timeout,
773 ptlrpc_interpterer_t interpreter)
775 struct ptlrpc_request *request = NULL;
778 OBD_ALLOC(request, sizeof(*request));
780 CERROR("request allocation out of memory\n");
784 request->rq_send_state = LUSTRE_IMP_FULL;
785 request->rq_type = PTL_RPC_MSG_REQUEST;
786 request->rq_import = class_import_get(imp);
787 request->rq_export = NULL;
788 request->rq_import_generation = imp->imp_generation;
790 request->rq_timeout = timeout;
791 request->rq_sent = cfs_time_current_sec();
792 request->rq_deadline = request->rq_sent + timeout;
793 request->rq_reply_deadline = request->rq_deadline;
794 request->rq_interpret_reply = interpreter;
795 request->rq_phase = RQ_PHASE_RPC;
796 request->rq_next_phase = RQ_PHASE_INTERPRET;
797 /* don't want reply */
798 request->rq_receiving_reply = 0;
799 request->rq_must_unlink = 0;
800 request->rq_no_delay = request->rq_no_resend = 1;
801 request->rq_fake = 1;
803 cfs_spin_lock_init(&request->rq_lock);
804 CFS_INIT_LIST_HEAD(&request->rq_list);
805 CFS_INIT_LIST_HEAD(&request->rq_replay_list);
806 CFS_INIT_LIST_HEAD(&request->rq_set_chain);
807 CFS_INIT_LIST_HEAD(&request->rq_history_list);
808 CFS_INIT_LIST_HEAD(&request->rq_exp_list);
809 cfs_waitq_init(&request->rq_reply_waitq);
810 cfs_waitq_init(&request->rq_set_waitq);
812 request->rq_xid = ptlrpc_next_xid();
813 cfs_atomic_set(&request->rq_refcount, 1);
819 * Indicate that processing of "fake" request is finished.
821 void ptlrpc_fakereq_finished(struct ptlrpc_request *req)
823 struct ptlrpc_request_set *set = req->rq_set;
826 /* hold ref on the request to prevent others (ptlrpcd) to free it */
827 ptlrpc_request_addref(req);
828 cfs_list_del_init(&req->rq_list);
830 /* if we kill request before timeout - need adjust counter */
831 if (req->rq_phase == RQ_PHASE_RPC && set != NULL &&
832 cfs_atomic_dec_and_test(&set->set_remaining))
835 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
837 /* Only need to call wakeup once when to be empty. */
839 cfs_waitq_signal(&set->set_waitq);
840 ptlrpc_req_finished(req);
844 * Allocate and initialize new request set structure.
845 * Returns a pointer to the newly allocated set structure or NULL on error.
847 struct ptlrpc_request_set *ptlrpc_prep_set(void)
849 struct ptlrpc_request_set *set;
852 OBD_ALLOC(set, sizeof *set);
855 cfs_atomic_set(&set->set_refcount, 1);
856 CFS_INIT_LIST_HEAD(&set->set_requests);
857 cfs_waitq_init(&set->set_waitq);
858 cfs_atomic_set(&set->set_new_count, 0);
859 cfs_atomic_set(&set->set_remaining, 0);
860 cfs_spin_lock_init(&set->set_new_req_lock);
861 CFS_INIT_LIST_HEAD(&set->set_new_requests);
862 CFS_INIT_LIST_HEAD(&set->set_cblist);
868 * Wind down and free request set structure previously allocated with
870 * Ensures that all requests on the set have completed and removes
871 * all requests from the request list in a set.
872 * If any unsent request happen to be on the list, pretends that they got
873 * an error in flight and calls their completion handler.
875 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
883 /* Requests on the set should either all be completed, or all be new */
884 expected_phase = (cfs_atomic_read(&set->set_remaining) == 0) ?
885 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
886 cfs_list_for_each (tmp, &set->set_requests) {
887 struct ptlrpc_request *req =
888 cfs_list_entry(tmp, struct ptlrpc_request,
891 LASSERT(req->rq_phase == expected_phase);
895 LASSERTF(cfs_atomic_read(&set->set_remaining) == 0 ||
896 cfs_atomic_read(&set->set_remaining) == n, "%d / %d\n",
897 cfs_atomic_read(&set->set_remaining), n);
899 cfs_list_for_each_safe(tmp, next, &set->set_requests) {
900 struct ptlrpc_request *req =
901 cfs_list_entry(tmp, struct ptlrpc_request,
903 cfs_list_del_init(&req->rq_set_chain);
905 LASSERT(req->rq_phase == expected_phase);
907 if (req->rq_phase == RQ_PHASE_NEW) {
908 ptlrpc_req_interpret(NULL, req, -EBADR);
909 cfs_atomic_dec(&set->set_remaining);
912 cfs_spin_lock(&req->rq_lock);
914 req->rq_invalid_rqset = 0;
915 cfs_spin_unlock(&req->rq_lock);
917 ptlrpc_req_finished (req);
920 LASSERT(cfs_atomic_read(&set->set_remaining) == 0);
922 ptlrpc_reqset_put(set);
927 * Add a callback function \a fn to the set.
928 * This function would be called when all requests on this set are completed.
929 * The function will be passed \a data argument.
931 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
932 set_interpreter_func fn, void *data)
934 struct ptlrpc_set_cbdata *cbdata;
936 OBD_ALLOC_PTR(cbdata);
940 cbdata->psc_interpret = fn;
941 cbdata->psc_data = data;
942 cfs_list_add_tail(&cbdata->psc_item, &set->set_cblist);
948 * Add a new request to the general purpose request set.
949 * Assumes request reference from the caller.
951 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
952 struct ptlrpc_request *req)
954 LASSERT(cfs_list_empty(&req->rq_set_chain));
956 /* The set takes over the caller's request reference */
957 cfs_list_add_tail(&req->rq_set_chain, &set->set_requests);
959 cfs_atomic_inc(&set->set_remaining);
960 req->rq_queued_time = cfs_time_current();
964 * Add a request to a request with dedicated server thread
965 * and wake the thread to make any necessary processing.
966 * Currently only used for ptlrpcd.
968 void ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
969 struct ptlrpc_request *req)
971 struct ptlrpc_request_set *set = pc->pc_set;
974 LASSERT(req->rq_set == NULL);
975 LASSERT(cfs_test_bit(LIOD_STOP, &pc->pc_flags) == 0);
977 cfs_spin_lock(&set->set_new_req_lock);
979 * The set takes over the caller's request reference.
982 req->rq_queued_time = cfs_time_current();
983 cfs_list_add_tail(&req->rq_set_chain, &set->set_new_requests);
984 count = cfs_atomic_inc_return(&set->set_new_count);
985 cfs_spin_unlock(&set->set_new_req_lock);
987 /* Only need to call wakeup once for the first entry. */
989 cfs_waitq_signal(&set->set_waitq);
991 /* XXX: It maybe unnecessary to wakeup all the partners. But to
992 * guarantee the async RPC can be processed ASAP, we have
993 * no other better choice. It maybe fixed in future. */
994 for (i = 0; i < pc->pc_npartners; i++)
995 cfs_waitq_signal(&pc->pc_partners[i]->pc_set->set_waitq);
1000 * Based on the current state of the import, determine if the request
1001 * can be sent, is an error, or should be delayed.
1003 * Returns true if this request should be delayed. If false, and
1004 * *status is set, then the request can not be sent and *status is the
1005 * error code. If false and status is 0, then request can be sent.
1007 * The imp->imp_lock must be held.
1009 static int ptlrpc_import_delay_req(struct obd_import *imp,
1010 struct ptlrpc_request *req, int *status)
1015 LASSERT (status != NULL);
1018 if (req->rq_ctx_init || req->rq_ctx_fini) {
1019 /* always allow ctx init/fini rpc go through */
1020 } else if (imp->imp_state == LUSTRE_IMP_NEW) {
1021 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
1024 } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
1025 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
1027 } else if (ptlrpc_send_limit_expired(req)) {
1028 /* probably doesn't need to be a D_ERROR after initial testing */
1029 DEBUG_REQ(D_ERROR, req, "send limit expired ");
1031 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
1032 imp->imp_state == LUSTRE_IMP_CONNECTING) {
1033 /* allow CONNECT even if import is invalid */ ;
1034 if (cfs_atomic_read(&imp->imp_inval_count) != 0) {
1035 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1038 } else if (imp->imp_invalid || imp->imp_obd->obd_no_recov) {
1039 if (!imp->imp_deactive)
1040 DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
1041 *status = -ESHUTDOWN; /* bz 12940 */
1042 } else if (req->rq_import_generation != imp->imp_generation) {
1043 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
1045 } else if (req->rq_send_state != imp->imp_state) {
1046 /* invalidate in progress - any requests should be drop */
1047 if (cfs_atomic_read(&imp->imp_inval_count) != 0) {
1048 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1050 } else if (imp->imp_dlm_fake || req->rq_no_delay) {
1051 *status = -EWOULDBLOCK;
1061 * Decide if the eror message regarding provided request \a req
1062 * should be printed to the console or not.
1063 * Makes it's decision on request status and other properties.
1064 * Returns 1 to print error on the system console or 0 if not.
1066 static int ptlrpc_console_allow(struct ptlrpc_request *req)
1071 /* Fake requests include no rq_reqmsg */
1075 LASSERT(req->rq_reqmsg != NULL);
1076 opc = lustre_msg_get_opc(req->rq_reqmsg);
1078 /* Suppress particular reconnect errors which are to be expected. No
1079 * errors are suppressed for the initial connection on an import */
1080 if ((lustre_handle_is_used(&req->rq_import->imp_remote_handle)) &&
1081 (opc == OST_CONNECT || opc == MDS_CONNECT || opc == MGS_CONNECT)) {
1083 /* Suppress timed out reconnect requests */
1084 if (req->rq_timedout)
1087 /* Suppress unavailable/again reconnect requests */
1088 err = lustre_msg_get_status(req->rq_repmsg);
1089 if (err == -ENODEV || err == -EAGAIN)
1097 * Check request processing status.
1098 * Returns the status.
1100 static int ptlrpc_check_status(struct ptlrpc_request *req)
1105 err = lustre_msg_get_status(req->rq_repmsg);
1106 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
1107 struct obd_import *imp = req->rq_import;
1108 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1109 LCONSOLE_ERROR_MSG(0x011,"an error occurred while communicating"
1110 " with %s. The %s operation failed with %d\n",
1111 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1112 ll_opcode2str(opc), err);
1113 RETURN(err < 0 ? err : -EINVAL);
1117 DEBUG_REQ(D_INFO, req, "status is %d", err);
1118 } else if (err > 0) {
1119 /* XXX: translate this error from net to host */
1120 DEBUG_REQ(D_INFO, req, "status is %d", err);
1123 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
1124 struct obd_import *imp = req->rq_import;
1125 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1127 if (ptlrpc_console_allow(req))
1128 LCONSOLE_ERROR_MSG(0x011,"an error occurred while "
1129 "communicating with %s. The %s "
1130 "operation failed with %d\n",
1132 imp->imp_connection->c_peer.nid),
1133 ll_opcode2str(opc), err);
1135 RETURN(err < 0 ? err : -EINVAL);
1142 * save pre-versions of objects into request for replay.
1143 * Versions are obtained from server reply.
1146 static void ptlrpc_save_versions(struct ptlrpc_request *req)
1148 struct lustre_msg *repmsg = req->rq_repmsg;
1149 struct lustre_msg *reqmsg = req->rq_reqmsg;
1150 __u64 *versions = lustre_msg_get_versions(repmsg);
1153 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1157 lustre_msg_set_versions(reqmsg, versions);
1158 CDEBUG(D_INFO, "Client save versions ["LPX64"/"LPX64"]\n",
1159 versions[0], versions[1]);
1165 * Callback function called when client receives RPC reply for \a req.
1166 * Returns 0 on success or error code.
1167 * The return alue would be assigned to req->rq_status by the caller
1168 * as request processing status.
1169 * This function also decides if the request needs to be saved for later replay.
1171 static int after_reply(struct ptlrpc_request *req)
1173 struct obd_import *imp = req->rq_import;
1174 struct obd_device *obd = req->rq_import->imp_obd;
1176 struct timeval work_start;
1180 LASSERT(obd != NULL);
1181 /* repbuf must be unlinked */
1182 LASSERT(!req->rq_receiving_reply && !req->rq_must_unlink);
1184 if (req->rq_reply_truncate) {
1185 if (ptlrpc_no_resend(req)) {
1186 DEBUG_REQ(D_ERROR, req, "reply buffer overflow,"
1187 " expected: %d, actual size: %d",
1188 req->rq_nob_received, req->rq_repbuf_len);
1192 sptlrpc_cli_free_repbuf(req);
1193 /* Pass the required reply buffer size (include
1194 * space for early reply).
1195 * NB: no need to roundup because alloc_repbuf
1196 * will roundup it */
1197 req->rq_replen = req->rq_nob_received;
1198 req->rq_nob_received = 0;
1204 * NB Until this point, the whole of the incoming message,
1205 * including buflens, status etc is in the sender's byte order.
1207 rc = sptlrpc_cli_unwrap_reply(req);
1209 DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
1214 * Security layer unwrap might ask resend this request.
1219 rc = unpack_reply(req);
1223 cfs_gettimeofday(&work_start);
1224 timediff = cfs_timeval_sub(&work_start, &req->rq_arrival_time, NULL);
1225 if (obd->obd_svc_stats != NULL) {
1226 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR,
1228 ptlrpc_lprocfs_rpc_sent(req, timediff);
1231 if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
1232 lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
1233 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
1234 lustre_msg_get_type(req->rq_repmsg));
1238 if (lustre_msg_get_opc(req->rq_reqmsg) != OBD_PING)
1239 CFS_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_PAUSE_REP, cfs_fail_val);
1240 ptlrpc_at_adj_service(req, lustre_msg_get_timeout(req->rq_repmsg));
1241 ptlrpc_at_adj_net_latency(req,
1242 lustre_msg_get_service_time(req->rq_repmsg));
1244 rc = ptlrpc_check_status(req);
1245 imp->imp_connect_error = rc;
1249 * Either we've been evicted, or the server has failed for
1250 * some reason. Try to reconnect, and if that fails, punt to
1253 if (ll_rpc_recoverable_error(rc)) {
1254 if (req->rq_send_state != LUSTRE_IMP_FULL ||
1255 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
1258 ptlrpc_request_handle_notconn(req);
1263 * Let's look if server sent slv. Do it only for RPC with
1266 ldlm_cli_update_pool(req);
1270 * Store transno in reqmsg for replay.
1272 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
1273 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
1274 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
1277 if (imp->imp_replayable) {
1278 cfs_spin_lock(&imp->imp_lock);
1280 * No point in adding already-committed requests to the replay
1281 * list, we will just remove them immediately. b=9829
1283 if (req->rq_transno != 0 &&
1285 lustre_msg_get_last_committed(req->rq_repmsg) ||
1287 /** version recovery */
1288 ptlrpc_save_versions(req);
1289 ptlrpc_retain_replayable_request(req, imp);
1290 } else if (req->rq_commit_cb != NULL) {
1291 cfs_spin_unlock(&imp->imp_lock);
1292 req->rq_commit_cb(req);
1293 cfs_spin_lock(&imp->imp_lock);
1297 * Replay-enabled imports return commit-status information.
1299 if (lustre_msg_get_last_committed(req->rq_repmsg)) {
1300 imp->imp_peer_committed_transno =
1301 lustre_msg_get_last_committed(req->rq_repmsg);
1303 ptlrpc_free_committed(imp);
1305 if (req->rq_transno > imp->imp_peer_committed_transno)
1306 ptlrpc_pinger_commit_expected(imp);
1308 cfs_spin_unlock(&imp->imp_lock);
1315 * Helper function to send request \a req over the network for the first time
1316 * Also adjusts request phase.
1317 * Returns 0 on success or error code.
1319 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
1321 struct obd_import *imp = req->rq_import;
1325 LASSERT(req->rq_phase == RQ_PHASE_NEW);
1326 if (req->rq_sent && (req->rq_sent > cfs_time_current_sec()) &&
1327 (!req->rq_generation_set ||
1328 req->rq_import_generation == imp->imp_generation))
1331 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
1333 cfs_spin_lock(&imp->imp_lock);
1335 if (!req->rq_generation_set)
1336 req->rq_import_generation = imp->imp_generation;
1338 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1339 cfs_spin_lock(&req->rq_lock);
1340 req->rq_waiting = 1;
1341 cfs_spin_unlock(&req->rq_lock);
1343 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
1344 "(%s != %s)", lustre_msg_get_status(req->rq_reqmsg),
1345 ptlrpc_import_state_name(req->rq_send_state),
1346 ptlrpc_import_state_name(imp->imp_state));
1347 LASSERT(cfs_list_empty(&req->rq_list));
1348 cfs_list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1349 cfs_atomic_inc(&req->rq_import->imp_inflight);
1350 cfs_spin_unlock(&imp->imp_lock);
1355 cfs_spin_unlock(&imp->imp_lock);
1356 req->rq_status = rc;
1357 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1361 LASSERT(cfs_list_empty(&req->rq_list));
1362 cfs_list_add_tail(&req->rq_list, &imp->imp_sending_list);
1363 cfs_atomic_inc(&req->rq_import->imp_inflight);
1364 cfs_spin_unlock(&imp->imp_lock);
1366 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
1368 rc = sptlrpc_req_refresh_ctx(req, -1);
1371 req->rq_status = rc;
1374 req->rq_wait_ctx = 1;
1379 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
1380 " %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1381 imp->imp_obd->obd_uuid.uuid,
1382 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1383 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1384 lustre_msg_get_opc(req->rq_reqmsg));
1386 rc = ptl_send_rpc(req, 0);
1388 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
1389 req->rq_net_err = 1;
1396 * this sends any unsent RPCs in \a set and returns 1 if all are sent
1397 * and no more replies are expected.
1398 * (it is possible to get less replies than requests sent e.g. due to timed out
1399 * requests or requests that we had trouble to send out)
1401 int ptlrpc_check_set(const struct lu_env *env, struct ptlrpc_request_set *set)
1404 int force_timer_recalc = 0;
1407 if (cfs_atomic_read(&set->set_remaining) == 0)
1410 cfs_list_for_each(tmp, &set->set_requests) {
1411 struct ptlrpc_request *req =
1412 cfs_list_entry(tmp, struct ptlrpc_request,
1414 struct obd_import *imp = req->rq_import;
1415 int unregistered = 0;
1418 if (req->rq_phase == RQ_PHASE_NEW &&
1419 ptlrpc_send_new_req(req)) {
1420 force_timer_recalc = 1;
1423 /* delayed send - skip */
1424 if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
1427 if (!(req->rq_phase == RQ_PHASE_RPC ||
1428 req->rq_phase == RQ_PHASE_BULK ||
1429 req->rq_phase == RQ_PHASE_INTERPRET ||
1430 req->rq_phase == RQ_PHASE_UNREGISTERING ||
1431 req->rq_phase == RQ_PHASE_COMPLETE)) {
1432 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
1436 if (req->rq_phase == RQ_PHASE_UNREGISTERING) {
1437 LASSERT(req->rq_next_phase != req->rq_phase);
1438 LASSERT(req->rq_next_phase != RQ_PHASE_UNDEFINED);
1441 * Skip processing until reply is unlinked. We
1442 * can't return to pool before that and we can't
1443 * call interpret before that. We need to make
1444 * sure that all rdma transfers finished and will
1445 * not corrupt any data.
1447 if (ptlrpc_client_recv_or_unlink(req) ||
1448 ptlrpc_client_bulk_active(req))
1452 * Turn fail_loc off to prevent it from looping
1455 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
1456 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK,
1459 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK)) {
1460 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK,
1465 * Move to next phase if reply was successfully
1468 ptlrpc_rqphase_move(req, req->rq_next_phase);
1471 if (req->rq_phase == RQ_PHASE_COMPLETE)
1474 if (req->rq_phase == RQ_PHASE_INTERPRET)
1475 GOTO(interpret, req->rq_status);
1478 * Note that this also will start async reply unlink.
1480 if (req->rq_net_err && !req->rq_timedout) {
1481 ptlrpc_expire_one_request(req, 1);
1484 * Check if we still need to wait for unlink.
1486 if (ptlrpc_client_recv_or_unlink(req) ||
1487 ptlrpc_client_bulk_active(req))
1489 /* If there is no need to resend, fail it now. */
1490 if (req->rq_no_resend) {
1491 if (req->rq_status == 0)
1492 req->rq_status = -EIO;
1493 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1494 GOTO(interpret, req->rq_status);
1501 cfs_spin_lock(&req->rq_lock);
1502 req->rq_replied = 0;
1503 cfs_spin_unlock(&req->rq_lock);
1504 if (req->rq_status == 0)
1505 req->rq_status = -EIO;
1506 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1507 GOTO(interpret, req->rq_status);
1510 /* ptlrpc_set_wait->l_wait_event sets lwi_allow_intr
1511 * so it sets rq_intr regardless of individual rpc
1512 * timeouts. The synchronous IO waiting path sets
1513 * rq_intr irrespective of whether ptlrpcd
1514 * has seen a timeout. Our policy is to only interpret
1515 * interrupted rpcs after they have timed out, so we
1516 * need to enforce that here.
1519 if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
1520 req->rq_wait_ctx)) {
1521 req->rq_status = -EINTR;
1522 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1523 GOTO(interpret, req->rq_status);
1526 if (req->rq_phase == RQ_PHASE_RPC) {
1527 if (req->rq_timedout || req->rq_resend ||
1528 req->rq_waiting || req->rq_wait_ctx) {
1531 if (!ptlrpc_unregister_reply(req, 1))
1534 cfs_spin_lock(&imp->imp_lock);
1535 if (ptlrpc_import_delay_req(imp, req, &status)){
1536 /* put on delay list - only if we wait
1537 * recovery finished - before send */
1538 cfs_list_del_init(&req->rq_list);
1539 cfs_list_add_tail(&req->rq_list,
1542 cfs_spin_unlock(&imp->imp_lock);
1547 req->rq_status = status;
1548 ptlrpc_rqphase_move(req,
1549 RQ_PHASE_INTERPRET);
1550 cfs_spin_unlock(&imp->imp_lock);
1551 GOTO(interpret, req->rq_status);
1553 if (ptlrpc_no_resend(req) && !req->rq_wait_ctx) {
1554 req->rq_status = -ENOTCONN;
1555 ptlrpc_rqphase_move(req,
1556 RQ_PHASE_INTERPRET);
1557 cfs_spin_unlock(&imp->imp_lock);
1558 GOTO(interpret, req->rq_status);
1561 cfs_list_del_init(&req->rq_list);
1562 cfs_list_add_tail(&req->rq_list,
1563 &imp->imp_sending_list);
1565 cfs_spin_unlock(&imp->imp_lock);
1567 cfs_spin_lock(&req->rq_lock);
1568 req->rq_waiting = 0;
1569 cfs_spin_unlock(&req->rq_lock);
1571 if (req->rq_timedout || req->rq_resend) {
1572 /* This is re-sending anyways,
1573 * let's mark req as resend. */
1574 cfs_spin_lock(&req->rq_lock);
1576 cfs_spin_unlock(&req->rq_lock);
1580 if (!ptlrpc_unregister_bulk(req, 1))
1583 /* ensure previous bulk fails */
1584 old_xid = req->rq_xid;
1585 req->rq_xid = ptlrpc_next_xid();
1586 CDEBUG(D_HA, "resend bulk "
1589 old_xid, req->rq_xid);
1593 * rq_wait_ctx is only touched by ptlrpcd,
1594 * so no lock is needed here.
1596 status = sptlrpc_req_refresh_ctx(req, -1);
1599 req->rq_status = status;
1600 cfs_spin_lock(&req->rq_lock);
1601 req->rq_wait_ctx = 0;
1602 cfs_spin_unlock(&req->rq_lock);
1603 force_timer_recalc = 1;
1605 cfs_spin_lock(&req->rq_lock);
1606 req->rq_wait_ctx = 1;
1607 cfs_spin_unlock(&req->rq_lock);
1612 cfs_spin_lock(&req->rq_lock);
1613 req->rq_wait_ctx = 0;
1614 cfs_spin_unlock(&req->rq_lock);
1617 rc = ptl_send_rpc(req, 0);
1619 DEBUG_REQ(D_HA, req, "send failed (%d)",
1621 force_timer_recalc = 1;
1622 cfs_spin_lock(&req->rq_lock);
1623 req->rq_net_err = 1;
1624 cfs_spin_unlock(&req->rq_lock);
1626 /* need to reset the timeout */
1627 force_timer_recalc = 1;
1630 cfs_spin_lock(&req->rq_lock);
1632 if (ptlrpc_client_early(req)) {
1633 ptlrpc_at_recv_early_reply(req);
1634 cfs_spin_unlock(&req->rq_lock);
1638 /* Still waiting for a reply? */
1639 if (ptlrpc_client_recv(req)) {
1640 cfs_spin_unlock(&req->rq_lock);
1644 /* Did we actually receive a reply? */
1645 if (!ptlrpc_client_replied(req)) {
1646 cfs_spin_unlock(&req->rq_lock);
1650 cfs_spin_unlock(&req->rq_lock);
1652 /* unlink from net because we are going to
1653 * swab in-place of reply buffer */
1654 unregistered = ptlrpc_unregister_reply(req, 1);
1658 req->rq_status = after_reply(req);
1662 /* If there is no bulk associated with this request,
1663 * then we're done and should let the interpreter
1664 * process the reply. Similarly if the RPC returned
1665 * an error, and therefore the bulk will never arrive.
1667 if (req->rq_bulk == NULL || req->rq_status < 0) {
1668 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1669 GOTO(interpret, req->rq_status);
1672 ptlrpc_rqphase_move(req, RQ_PHASE_BULK);
1675 LASSERT(req->rq_phase == RQ_PHASE_BULK);
1676 if (ptlrpc_client_bulk_active(req))
1679 if (!req->rq_bulk->bd_success) {
1680 /* The RPC reply arrived OK, but the bulk screwed
1681 * up! Dead weird since the server told us the RPC
1682 * was good after getting the REPLY for her GET or
1683 * the ACK for her PUT. */
1684 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1685 req->rq_status = -EIO;
1688 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1691 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
1693 /* This moves to "unregistering" phase we need to wait for
1695 if (!unregistered && !ptlrpc_unregister_reply(req, 1)) {
1696 /* start async bulk unlink too */
1697 ptlrpc_unregister_bulk(req, 1);
1701 if (!ptlrpc_unregister_bulk(req, 1))
1704 /* When calling interpret receiving already should be
1706 LASSERT(!req->rq_receiving_reply);
1708 ptlrpc_req_interpret(env, req, req->rq_status);
1710 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
1712 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
1713 "opc %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1714 imp->imp_obd->obd_uuid.uuid,
1715 req->rq_reqmsg ? lustre_msg_get_status(req->rq_reqmsg):-1,
1717 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1718 req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : -1);
1720 cfs_spin_lock(&imp->imp_lock);
1721 /* Request already may be not on sending or delaying list. This
1722 * may happen in the case of marking it erroneous for the case
1723 * ptlrpc_import_delay_req(req, status) find it impossible to
1724 * allow sending this rpc and returns *status != 0. */
1725 if (!cfs_list_empty(&req->rq_list)) {
1726 cfs_list_del_init(&req->rq_list);
1727 cfs_atomic_dec(&imp->imp_inflight);
1729 cfs_spin_unlock(&imp->imp_lock);
1731 cfs_atomic_dec(&set->set_remaining);
1732 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
1735 /* If we hit an error, we want to recover promptly. */
1736 RETURN(cfs_atomic_read(&set->set_remaining) == 0 || force_timer_recalc);
1740 * Time out request \a req. is \a async_unlink is set, that means do not wait
1741 * until LNet actually confirms network buffer unlinking.
1742 * Return 1 if we should give up further retrying attempts or 0 otherwise.
1744 int ptlrpc_expire_one_request(struct ptlrpc_request *req, int async_unlink)
1746 struct obd_import *imp = req->rq_import;
1750 cfs_spin_lock(&req->rq_lock);
1751 req->rq_timedout = 1;
1752 cfs_spin_unlock(&req->rq_lock);
1754 DEBUG_REQ(req->rq_fake ? D_INFO : D_WARNING, req, "Request "
1755 " sent has %s: [sent "CFS_DURATION_T"/"
1756 "real "CFS_DURATION_T"]",
1757 req->rq_net_err ? "failed due to network error" :
1758 ((req->rq_real_sent == 0 ||
1759 cfs_time_before(req->rq_real_sent, req->rq_sent) ||
1760 cfs_time_aftereq(req->rq_real_sent, req->rq_deadline)) ?
1761 "timed out for sent delay" : "timed out for slow reply"),
1762 req->rq_sent, req->rq_real_sent);
1764 if (imp != NULL && obd_debug_peer_on_timeout)
1765 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1767 ptlrpc_unregister_reply(req, async_unlink);
1768 ptlrpc_unregister_bulk(req, async_unlink);
1770 if (obd_dump_on_timeout)
1771 libcfs_debug_dumplog();
1774 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
1781 cfs_atomic_inc(&imp->imp_timeouts);
1783 /* The DLM server doesn't want recovery run on its imports. */
1784 if (imp->imp_dlm_fake)
1787 /* If this request is for recovery or other primordial tasks,
1788 * then error it out here. */
1789 if (req->rq_ctx_init || req->rq_ctx_fini ||
1790 req->rq_send_state != LUSTRE_IMP_FULL ||
1791 imp->imp_obd->obd_no_recov) {
1792 DEBUG_REQ(D_RPCTRACE, req, "err -110, sent_state=%s (now=%s)",
1793 ptlrpc_import_state_name(req->rq_send_state),
1794 ptlrpc_import_state_name(imp->imp_state));
1795 cfs_spin_lock(&req->rq_lock);
1796 req->rq_status = -ETIMEDOUT;
1798 cfs_spin_unlock(&req->rq_lock);
1802 /* if a request can't be resent we can't wait for an answer after
1804 if (ptlrpc_no_resend(req)) {
1805 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1809 ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1815 * Time out all uncompleted requests in request set pointed by \a data
1816 * Callback used when waiting on sets with l_wait_event.
1819 int ptlrpc_expired_set(void *data)
1821 struct ptlrpc_request_set *set = data;
1823 time_t now = cfs_time_current_sec();
1826 LASSERT(set != NULL);
1829 * A timeout expired. See which reqs it applies to...
1831 cfs_list_for_each (tmp, &set->set_requests) {
1832 struct ptlrpc_request *req =
1833 cfs_list_entry(tmp, struct ptlrpc_request,
1836 /* don't expire request waiting for context */
1837 if (req->rq_wait_ctx)
1840 /* Request in-flight? */
1841 if (!((req->rq_phase == RQ_PHASE_RPC &&
1842 !req->rq_waiting && !req->rq_resend) ||
1843 (req->rq_phase == RQ_PHASE_BULK)))
1846 if (req->rq_timedout || /* already dealt with */
1847 req->rq_deadline > now) /* not expired */
1850 /* Deal with this guy. Do it asynchronously to not block
1851 * ptlrpcd thread. */
1852 ptlrpc_expire_one_request(req, 1);
1856 * When waiting for a whole set, we always break out of the
1857 * sleep so we can recalculate the timeout, or enable interrupts
1858 * if everyone's timed out.
1864 * Sets rq_intr flag in \a req under spinlock.
1866 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1868 cfs_spin_lock(&req->rq_lock);
1870 cfs_spin_unlock(&req->rq_lock);
1874 * Interrupts (sets interrupted flag) all uncompleted requests in
1875 * a set \a data. Callback for l_wait_event for interruptible waits.
1877 void ptlrpc_interrupted_set(void *data)
1879 struct ptlrpc_request_set *set = data;
1882 LASSERT(set != NULL);
1883 CDEBUG(D_RPCTRACE, "INTERRUPTED SET %p\n", set);
1885 cfs_list_for_each(tmp, &set->set_requests) {
1886 struct ptlrpc_request *req =
1887 cfs_list_entry(tmp, struct ptlrpc_request,
1890 if (req->rq_phase != RQ_PHASE_RPC &&
1891 req->rq_phase != RQ_PHASE_UNREGISTERING)
1894 ptlrpc_mark_interrupted(req);
1899 * Get the smallest timeout in the set; this does NOT set a timeout.
1901 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1904 time_t now = cfs_time_current_sec();
1906 struct ptlrpc_request *req;
1910 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1912 cfs_list_for_each(tmp, &set->set_requests) {
1913 req = cfs_list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1916 * Request in-flight?
1918 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
1919 (req->rq_phase == RQ_PHASE_BULK) ||
1920 (req->rq_phase == RQ_PHASE_NEW)))
1924 * Already timed out.
1926 if (req->rq_timedout)
1932 if (req->rq_wait_ctx)
1935 if (req->rq_phase == RQ_PHASE_NEW)
1936 deadline = req->rq_sent;
1938 deadline = req->rq_sent + req->rq_timeout;
1940 if (deadline <= now) /* actually expired already */
1941 timeout = 1; /* ASAP */
1942 else if (timeout == 0 || timeout > deadline - now)
1943 timeout = deadline - now;
1949 * Send all unset request from the set and then wait untill all
1950 * requests in the set complete (either get a reply, timeout, get an
1951 * error or otherwise be interrupted).
1952 * Returns 0 on success or error code otherwise.
1954 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
1957 struct ptlrpc_request *req;
1958 struct l_wait_info lwi;
1962 if (cfs_list_empty(&set->set_requests))
1965 cfs_list_for_each(tmp, &set->set_requests) {
1966 req = cfs_list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1967 if (req->rq_phase == RQ_PHASE_NEW)
1968 (void)ptlrpc_send_new_req(req);
1972 timeout = ptlrpc_set_next_timeout(set);
1974 /* wait until all complete, interrupted, or an in-flight
1976 CDEBUG(D_RPCTRACE, "set %p going to sleep for %d seconds\n",
1979 if (timeout == 0 && !cfs_signal_pending())
1981 * No requests are in-flight (ether timed out
1982 * or delayed), so we can allow interrupts.
1983 * We still want to block for a limited time,
1984 * so we allow interrupts during the timeout.
1986 lwi = LWI_TIMEOUT_INTR_ALL(cfs_time_seconds(1),
1988 ptlrpc_interrupted_set, set);
1991 * At least one request is in flight, so no
1992 * interrupts are allowed. Wait until all
1993 * complete, or an in-flight req times out.
1995 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout? timeout : 1),
1996 ptlrpc_expired_set, set);
1998 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(NULL, set), &lwi);
2000 /* LU-769 - if we ignored the signal because it was already
2001 * pending when we started, we need to handle it now or we risk
2002 * it being ignored forever */
2003 if (rc == -ETIMEDOUT && !lwi.lwi_allow_intr &&
2004 cfs_signal_pending()) {
2005 cfs_sigset_t blocked_sigs =
2006 cfs_block_sigsinv(LUSTRE_FATAL_SIGS);
2008 /* In fact we only interrupt for the "fatal" signals
2009 * like SIGINT or SIGKILL. We still ignore less
2010 * important signals since ptlrpc set is not easily
2011 * reentrant from userspace again */
2012 if (cfs_signal_pending())
2013 ptlrpc_interrupted_set(set);
2014 cfs_block_sigs(blocked_sigs);
2017 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
2019 /* -EINTR => all requests have been flagged rq_intr so next
2021 * -ETIMEDOUT => someone timed out. When all reqs have
2022 * timed out, signals are enabled allowing completion with
2024 * I don't really care if we go once more round the loop in
2025 * the error cases -eeb. */
2026 if (rc == 0 && cfs_atomic_read(&set->set_remaining) == 0) {
2027 cfs_list_for_each(tmp, &set->set_requests) {
2028 req = cfs_list_entry(tmp, struct ptlrpc_request,
2030 cfs_spin_lock(&req->rq_lock);
2031 req->rq_invalid_rqset = 1;
2032 cfs_spin_unlock(&req->rq_lock);
2035 } while (rc != 0 || cfs_atomic_read(&set->set_remaining) != 0);
2037 LASSERT(cfs_atomic_read(&set->set_remaining) == 0);
2040 cfs_list_for_each(tmp, &set->set_requests) {
2041 req = cfs_list_entry(tmp, struct ptlrpc_request, rq_set_chain);
2043 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
2044 if (req->rq_status != 0)
2045 rc = req->rq_status;
2048 if (set->set_interpret != NULL) {
2049 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
2051 rc = interpreter (set, set->set_arg, rc);
2053 struct ptlrpc_set_cbdata *cbdata, *n;
2056 cfs_list_for_each_entry_safe(cbdata, n,
2057 &set->set_cblist, psc_item) {
2058 cfs_list_del_init(&cbdata->psc_item);
2059 err = cbdata->psc_interpret(set, cbdata->psc_data, rc);
2062 OBD_FREE_PTR(cbdata);
2070 * Helper fuction for request freeing.
2071 * Called when request count reached zero and request needs to be freed.
2072 * Removes request from all sorts of sending/replay lists it might be on,
2073 * frees network buffers if any are present.
2074 * If \a locked is set, that means caller is already holding import imp_lock
2075 * and so we no longer need to reobtain it (for certain lists manipulations)
2077 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
2080 if (request == NULL) {
2085 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
2086 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
2087 LASSERTF(cfs_list_empty(&request->rq_list), "req %p\n", request);
2088 LASSERTF(cfs_list_empty(&request->rq_set_chain), "req %p\n", request);
2089 LASSERTF(cfs_list_empty(&request->rq_exp_list), "req %p\n", request);
2090 LASSERTF(!request->rq_replay, "req %p\n", request);
2092 req_capsule_fini(&request->rq_pill);
2094 /* We must take it off the imp_replay_list first. Otherwise, we'll set
2095 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
2096 if (request->rq_import != NULL) {
2098 cfs_spin_lock(&request->rq_import->imp_lock);
2099 cfs_list_del_init(&request->rq_replay_list);
2101 cfs_spin_unlock(&request->rq_import->imp_lock);
2103 LASSERTF(cfs_list_empty(&request->rq_replay_list), "req %p\n", request);
2105 if (cfs_atomic_read(&request->rq_refcount) != 0) {
2106 DEBUG_REQ(D_ERROR, request,
2107 "freeing request with nonzero refcount");
2111 if (request->rq_repbuf != NULL)
2112 sptlrpc_cli_free_repbuf(request);
2113 if (request->rq_export != NULL) {
2114 class_export_put(request->rq_export);
2115 request->rq_export = NULL;
2117 if (request->rq_import != NULL) {
2118 class_import_put(request->rq_import);
2119 request->rq_import = NULL;
2121 if (request->rq_bulk != NULL)
2122 ptlrpc_free_bulk(request->rq_bulk);
2124 if (request->rq_reqbuf != NULL || request->rq_clrbuf != NULL)
2125 sptlrpc_cli_free_reqbuf(request);
2127 if (request->rq_cli_ctx)
2128 sptlrpc_req_put_ctx(request, !locked);
2130 if (request->rq_pool)
2131 __ptlrpc_free_req_to_pool(request);
2133 OBD_FREE(request, sizeof(*request));
2137 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
2139 * Drop one request reference. Must be called with import imp_lock held.
2140 * When reference count drops to zero, reuqest is freed.
2142 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
2144 LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
2145 (void)__ptlrpc_req_finished(request, 1);
2150 * Drops one reference count for request \a request.
2151 * \a locked set indicates that caller holds import imp_lock.
2152 * Frees the request whe reference count reaches zero.
2154 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
2157 if (request == NULL)
2160 if (request == LP_POISON ||
2161 request->rq_reqmsg == LP_POISON) {
2162 CERROR("dereferencing freed request (bug 575)\n");
2167 DEBUG_REQ(D_INFO, request, "refcount now %u",
2168 cfs_atomic_read(&request->rq_refcount) - 1);
2170 if (cfs_atomic_dec_and_test(&request->rq_refcount)) {
2171 __ptlrpc_free_req(request, locked);
2179 * Drops one reference count for a request.
2181 void ptlrpc_req_finished(struct ptlrpc_request *request)
2183 __ptlrpc_req_finished(request, 0);
2187 * Returns xid of a \a request
2189 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
2191 return request->rq_xid;
2193 EXPORT_SYMBOL(ptlrpc_req_xid);
2196 * Disengage the client's reply buffer from the network
2197 * NB does _NOT_ unregister any client-side bulk.
2198 * IDEMPOTENT, but _not_ safe against concurrent callers.
2199 * The request owner (i.e. the thread doing the I/O) must call...
2200 * Returns 0 on success or 1 if unregistering cannot be made.
2202 int ptlrpc_unregister_reply(struct ptlrpc_request *request, int async)
2206 struct l_wait_info lwi;
2211 LASSERT(!cfs_in_interrupt());
2214 * Let's setup deadline for reply unlink.
2216 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
2217 async && request->rq_reply_deadline == 0)
2218 request->rq_reply_deadline = cfs_time_current_sec()+LONG_UNLINK;
2221 * Nothing left to do.
2223 if (!ptlrpc_client_recv_or_unlink(request))
2226 LNetMDUnlink(request->rq_reply_md_h);
2229 * Let's check it once again.
2231 if (!ptlrpc_client_recv_or_unlink(request))
2235 * Move to "Unregistering" phase as reply was not unlinked yet.
2237 ptlrpc_rqphase_move(request, RQ_PHASE_UNREGISTERING);
2240 * Do not wait for unlink to finish.
2246 * We have to l_wait_event() whatever the result, to give liblustre
2247 * a chance to run reply_in_callback(), and to make sure we've
2248 * unlinked before returning a req to the pool.
2250 if (request->rq_set != NULL)
2251 wq = &request->rq_set->set_waitq;
2253 wq = &request->rq_reply_waitq;
2256 /* Network access will complete in finite time but the HUGE
2257 * timeout lets us CWARN for visibility of sluggish NALs */
2258 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
2259 cfs_time_seconds(1), NULL, NULL);
2260 rc = l_wait_event(*wq, !ptlrpc_client_recv_or_unlink(request),
2263 ptlrpc_rqphase_move(request, request->rq_next_phase);
2267 LASSERT(rc == -ETIMEDOUT);
2268 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout "
2269 "rvcng=%d unlnk=%d", request->rq_receiving_reply,
2270 request->rq_must_unlink);
2276 * Iterates through replay_list on import and prunes
2277 * all requests have transno smaller than last_committed for the
2278 * import and don't have rq_replay set.
2279 * Since requests are sorted in transno order, stops when meetign first
2280 * transno bigger than last_committed.
2281 * caller must hold imp->imp_lock
2283 void ptlrpc_free_committed(struct obd_import *imp)
2285 cfs_list_t *tmp, *saved;
2286 struct ptlrpc_request *req;
2287 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
2290 LASSERT(imp != NULL);
2292 LASSERT_SPIN_LOCKED(&imp->imp_lock);
2295 if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
2296 imp->imp_generation == imp->imp_last_generation_checked) {
2297 CDEBUG(D_INFO, "%s: skip recheck: last_committed "LPU64"\n",
2298 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
2302 CDEBUG(D_RPCTRACE, "%s: committing for last_committed "LPU64" gen %d\n",
2303 imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
2304 imp->imp_generation);
2305 imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
2306 imp->imp_last_generation_checked = imp->imp_generation;
2308 cfs_list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
2309 req = cfs_list_entry(tmp, struct ptlrpc_request,
2312 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
2313 LASSERT(req != last_req);
2316 if (req->rq_transno == 0) {
2317 DEBUG_REQ(D_EMERG, req, "zero transno during replay");
2320 if (req->rq_import_generation < imp->imp_generation) {
2321 DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
2325 if (req->rq_replay) {
2326 DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
2330 /* not yet committed */
2331 if (req->rq_transno > imp->imp_peer_committed_transno) {
2332 DEBUG_REQ(D_RPCTRACE, req, "stopping search");
2336 DEBUG_REQ(D_INFO, req, "commit (last_committed "LPU64")",
2337 imp->imp_peer_committed_transno);
2339 cfs_spin_lock(&req->rq_lock);
2341 cfs_spin_unlock(&req->rq_lock);
2342 if (req->rq_commit_cb != NULL)
2343 req->rq_commit_cb(req);
2344 cfs_list_del_init(&req->rq_replay_list);
2345 __ptlrpc_req_finished(req, 1);
2352 void ptlrpc_cleanup_client(struct obd_import *imp)
2360 * Schedule previously sent request for resend.
2361 * For bulk requests we assign new xid (to avoid problems with
2362 * lost replies and therefore several transfers landing into same buffer
2363 * from different sending attempts).
2365 void ptlrpc_resend_req(struct ptlrpc_request *req)
2367 DEBUG_REQ(D_HA, req, "going to resend");
2368 lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
2369 req->rq_status = -EAGAIN;
2371 cfs_spin_lock(&req->rq_lock);
2373 req->rq_net_err = 0;
2374 req->rq_timedout = 0;
2376 __u64 old_xid = req->rq_xid;
2378 /* ensure previous bulk fails */
2379 req->rq_xid = ptlrpc_next_xid();
2380 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
2381 old_xid, req->rq_xid);
2383 ptlrpc_client_wake_req(req);
2384 cfs_spin_unlock(&req->rq_lock);
2387 /* XXX: this function and rq_status are currently unused */
2388 void ptlrpc_restart_req(struct ptlrpc_request *req)
2390 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
2391 req->rq_status = -ERESTARTSYS;
2393 cfs_spin_lock(&req->rq_lock);
2394 req->rq_restart = 1;
2395 req->rq_timedout = 0;
2396 ptlrpc_client_wake_req(req);
2397 cfs_spin_unlock(&req->rq_lock);
2401 * Grab additional reference on a request \a req
2403 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
2406 cfs_atomic_inc(&req->rq_refcount);
2411 * Add a request to import replay_list.
2412 * Must be called under imp_lock
2414 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
2415 struct obd_import *imp)
2419 LASSERT_SPIN_LOCKED(&imp->imp_lock);
2421 if (req->rq_transno == 0) {
2422 DEBUG_REQ(D_EMERG, req, "saving request with zero transno");
2426 /* clear this for new requests that were resent as well
2427 as resent replayed requests. */
2428 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2430 /* don't re-add requests that have been replayed */
2431 if (!cfs_list_empty(&req->rq_replay_list))
2434 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
2436 LASSERT(imp->imp_replayable);
2437 /* Balanced in ptlrpc_free_committed, usually. */
2438 ptlrpc_request_addref(req);
2439 cfs_list_for_each_prev(tmp, &imp->imp_replay_list) {
2440 struct ptlrpc_request *iter =
2441 cfs_list_entry(tmp, struct ptlrpc_request,
2444 /* We may have duplicate transnos if we create and then
2445 * open a file, or for closes retained if to match creating
2446 * opens, so use req->rq_xid as a secondary key.
2447 * (See bugs 684, 685, and 428.)
2448 * XXX no longer needed, but all opens need transnos!
2450 if (iter->rq_transno > req->rq_transno)
2453 if (iter->rq_transno == req->rq_transno) {
2454 LASSERT(iter->rq_xid != req->rq_xid);
2455 if (iter->rq_xid > req->rq_xid)
2459 cfs_list_add(&req->rq_replay_list, &iter->rq_replay_list);
2463 cfs_list_add(&req->rq_replay_list, &imp->imp_replay_list);
2467 * Send request and wait until it completes.
2468 * Returns request processing status.
2470 int ptlrpc_queue_wait(struct ptlrpc_request *req)
2472 struct ptlrpc_request_set *set;
2476 LASSERT(req->rq_set == NULL);
2477 LASSERT(!req->rq_receiving_reply);
2479 set = ptlrpc_prep_set();
2481 CERROR("Unable to allocate ptlrpc set.");
2485 /* for distributed debugging */
2486 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
2488 /* add a ref for the set (see comment in ptlrpc_set_add_req) */
2489 ptlrpc_request_addref(req);
2490 ptlrpc_set_add_req(set, req);
2491 rc = ptlrpc_set_wait(set);
2492 ptlrpc_set_destroy(set);
2497 struct ptlrpc_replay_async_args {
2499 int praa_old_status;
2503 * Callback used for replayed requests reply processing.
2504 * In case of succesful reply calls registeresd request replay callback.
2505 * In case of error restart replay process.
2507 static int ptlrpc_replay_interpret(const struct lu_env *env,
2508 struct ptlrpc_request *req,
2509 void * data, int rc)
2511 struct ptlrpc_replay_async_args *aa = data;
2512 struct obd_import *imp = req->rq_import;
2515 cfs_atomic_dec(&imp->imp_replay_inflight);
2517 if (!ptlrpc_client_replied(req)) {
2518 CERROR("request replay timed out, restarting recovery\n");
2519 GOTO(out, rc = -ETIMEDOUT);
2522 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
2523 (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
2524 lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
2525 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2527 /** VBR: check version failure */
2528 if (lustre_msg_get_status(req->rq_repmsg) == -EOVERFLOW) {
2529 /** replay was failed due to version mismatch */
2530 DEBUG_REQ(D_WARNING, req, "Version mismatch during replay\n");
2531 cfs_spin_lock(&imp->imp_lock);
2532 imp->imp_vbr_failed = 1;
2533 imp->imp_no_lock_replay = 1;
2534 cfs_spin_unlock(&imp->imp_lock);
2535 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2537 /** The transno had better not change over replay. */
2538 LASSERTF(lustre_msg_get_transno(req->rq_reqmsg) ==
2539 lustre_msg_get_transno(req->rq_repmsg) ||
2540 lustre_msg_get_transno(req->rq_repmsg) == 0,
2542 lustre_msg_get_transno(req->rq_reqmsg),
2543 lustre_msg_get_transno(req->rq_repmsg));
2546 cfs_spin_lock(&imp->imp_lock);
2547 /** if replays by version then gap was occur on server, no trust to locks */
2548 if (lustre_msg_get_flags(req->rq_repmsg) & MSG_VERSION_REPLAY)
2549 imp->imp_no_lock_replay = 1;
2550 imp->imp_last_replay_transno = lustre_msg_get_transno(req->rq_reqmsg);
2551 cfs_spin_unlock(&imp->imp_lock);
2552 LASSERT(imp->imp_last_replay_transno);
2554 /* transaction number shouldn't be bigger than the latest replayed */
2555 if (req->rq_transno > lustre_msg_get_transno(req->rq_reqmsg)) {
2556 DEBUG_REQ(D_ERROR, req,
2557 "Reported transno "LPU64" is bigger than the "
2558 "replayed one: "LPU64, req->rq_transno,
2559 lustre_msg_get_transno(req->rq_reqmsg));
2560 GOTO(out, rc = -EINVAL);
2563 DEBUG_REQ(D_HA, req, "got rep");
2565 /* let the callback do fixups, possibly including in the request */
2566 if (req->rq_replay_cb)
2567 req->rq_replay_cb(req);
2569 if (ptlrpc_client_replied(req) &&
2570 lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
2571 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
2572 lustre_msg_get_status(req->rq_repmsg),
2573 aa->praa_old_status);
2575 /* Put it back for re-replay. */
2576 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2580 * Errors while replay can set transno to 0, but
2581 * imp_last_replay_transno shouldn't be set to 0 anyway
2583 if (req->rq_transno == 0)
2584 CERROR("Transno is 0 during replay!\n");
2586 /* continue with recovery */
2587 rc = ptlrpc_import_recovery_state_machine(imp);
2589 req->rq_send_state = aa->praa_old_state;
2592 /* this replay failed, so restart recovery */
2593 ptlrpc_connect_import(imp);
2599 * Prepares and queues request for replay.
2600 * Adds it to ptlrpcd queue for actual sending.
2601 * Returns 0 on success.
2603 int ptlrpc_replay_req(struct ptlrpc_request *req)
2605 struct ptlrpc_replay_async_args *aa;
2608 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
2610 LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
2611 aa = ptlrpc_req_async_args(req);
2612 memset(aa, 0, sizeof *aa);
2614 /* Prepare request to be resent with ptlrpcd */
2615 aa->praa_old_state = req->rq_send_state;
2616 req->rq_send_state = LUSTRE_IMP_REPLAY;
2617 req->rq_phase = RQ_PHASE_NEW;
2618 req->rq_next_phase = RQ_PHASE_UNDEFINED;
2620 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
2622 req->rq_interpret_reply = ptlrpc_replay_interpret;
2623 /* Readjust the timeout for current conditions */
2624 ptlrpc_at_set_req_timeout(req);
2626 /* Tell server the net_latency, so the server can calculate how long
2627 * it should wait for next replay */
2628 lustre_msg_set_service_time(req->rq_reqmsg,
2629 ptlrpc_at_get_net_latency(req));
2630 DEBUG_REQ(D_HA, req, "REPLAY");
2632 cfs_atomic_inc(&req->rq_import->imp_replay_inflight);
2633 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
2635 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2640 * Aborts all in-flight request on import \a imp sending and delayed lists
2642 void ptlrpc_abort_inflight(struct obd_import *imp)
2644 cfs_list_t *tmp, *n;
2647 /* Make sure that no new requests get processed for this import.
2648 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
2649 * this flag and then putting requests on sending_list or delayed_list.
2651 cfs_spin_lock(&imp->imp_lock);
2653 /* XXX locking? Maybe we should remove each request with the list
2654 * locked? Also, how do we know if the requests on the list are
2655 * being freed at this time?
2657 cfs_list_for_each_safe(tmp, n, &imp->imp_sending_list) {
2658 struct ptlrpc_request *req =
2659 cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
2661 DEBUG_REQ(D_RPCTRACE, req, "inflight");
2663 cfs_spin_lock (&req->rq_lock);
2664 if (req->rq_import_generation < imp->imp_generation) {
2666 req->rq_status = -EINTR;
2667 ptlrpc_client_wake_req(req);
2669 cfs_spin_unlock (&req->rq_lock);
2672 cfs_list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
2673 struct ptlrpc_request *req =
2674 cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
2676 DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
2678 cfs_spin_lock (&req->rq_lock);
2679 if (req->rq_import_generation < imp->imp_generation) {
2681 req->rq_status = -EINTR;
2682 ptlrpc_client_wake_req(req);
2684 cfs_spin_unlock (&req->rq_lock);
2687 /* Last chance to free reqs left on the replay list, but we
2688 * will still leak reqs that haven't committed. */
2689 if (imp->imp_replayable)
2690 ptlrpc_free_committed(imp);
2692 cfs_spin_unlock(&imp->imp_lock);
2698 * Abort all uncompleted requests in request set \a set
2700 void ptlrpc_abort_set(struct ptlrpc_request_set *set)
2702 cfs_list_t *tmp, *pos;
2704 LASSERT(set != NULL);
2706 cfs_list_for_each_safe(pos, tmp, &set->set_requests) {
2707 struct ptlrpc_request *req =
2708 cfs_list_entry(pos, struct ptlrpc_request,
2711 cfs_spin_lock(&req->rq_lock);
2712 if (req->rq_phase != RQ_PHASE_RPC) {
2713 cfs_spin_unlock(&req->rq_lock);
2718 req->rq_status = -EINTR;
2719 ptlrpc_client_wake_req(req);
2720 cfs_spin_unlock(&req->rq_lock);
2724 static __u64 ptlrpc_last_xid;
2725 static cfs_spinlock_t ptlrpc_last_xid_lock;
2728 * Initialize the XID for the node. This is common among all requests on
2729 * this node, and only requires the property that it is monotonically
2730 * increasing. It does not need to be sequential. Since this is also used
2731 * as the RDMA match bits, it is important that a single client NOT have
2732 * the same match bits for two different in-flight requests, hence we do
2733 * NOT want to have an XID per target or similar.
2735 * To avoid an unlikely collision between match bits after a client reboot
2736 * (which would deliver old data into the wrong RDMA buffer) initialize
2737 * the XID based on the current time, assuming a maximum RPC rate of 1M RPC/s.
2738 * If the time is clearly incorrect, we instead use a 62-bit random number.
2739 * In the worst case the random number will overflow 1M RPCs per second in
2740 * 9133 years, or permutations thereof.
2742 #define YEAR_2004 (1ULL << 30)
2743 void ptlrpc_init_xid(void)
2745 time_t now = cfs_time_current_sec();
2747 cfs_spin_lock_init(&ptlrpc_last_xid_lock);
2748 if (now < YEAR_2004) {
2749 cfs_get_random_bytes(&ptlrpc_last_xid, sizeof(ptlrpc_last_xid));
2750 ptlrpc_last_xid >>= 2;
2751 ptlrpc_last_xid |= (1ULL << 61);
2753 ptlrpc_last_xid = (__u64)now << 20;
2758 * Increase xid and returns resultng new value to the caller.
2760 __u64 ptlrpc_next_xid(void)
2763 cfs_spin_lock(&ptlrpc_last_xid_lock);
2764 tmp = ++ptlrpc_last_xid;
2765 cfs_spin_unlock(&ptlrpc_last_xid_lock);
2770 * Get a glimpse at what next xid value might have been.
2771 * Returns possible next xid.
2773 __u64 ptlrpc_sample_next_xid(void)
2775 #if BITS_PER_LONG == 32
2776 /* need to avoid possible word tearing on 32-bit systems */
2778 cfs_spin_lock(&ptlrpc_last_xid_lock);
2779 tmp = ptlrpc_last_xid + 1;
2780 cfs_spin_unlock(&ptlrpc_last_xid_lock);
2783 /* No need to lock, since returned value is racy anyways */
2784 return ptlrpc_last_xid + 1;
2787 EXPORT_SYMBOL(ptlrpc_sample_next_xid);
2790 * Functions for operating ptlrpc workers.
2792 * A ptlrpc work is a function which will be running inside ptlrpc context.
2793 * The callback shouldn't sleep otherwise it will block that ptlrpcd thread.
2795 * 1. after a work is created, it can be used many times, that is:
2796 * handler = ptlrpcd_alloc_work();
2797 * ptlrpcd_queue_work();
2799 * queue it again when necessary:
2800 * ptlrpcd_queue_work();
2801 * ptlrpcd_destroy_work();
2802 * 2. ptlrpcd_queue_work() can be called by multiple processes meanwhile, but
2803 * it will only be queued once in any time. Also as its name implies, it may
2804 * have delay before it really runs by ptlrpcd thread.
2806 struct ptlrpc_work_async_args {
2808 int (*cb)(const struct lu_env *, void *);
2812 #define PTLRPC_WORK_MAGIC 0x6655436b676f4f44ULL /* magic code */
2814 static int work_interpreter(const struct lu_env *env,
2815 struct ptlrpc_request *req, void *data, int rc)
2817 struct ptlrpc_work_async_args *arg = data;
2819 LASSERT(arg->magic == PTLRPC_WORK_MAGIC);
2820 LASSERT(arg->cb != NULL);
2822 return arg->cb(env, arg->cbdata);
2826 * Create a work for ptlrpc.
2828 void *ptlrpcd_alloc_work(struct obd_import *imp,
2829 int (*cb)(const struct lu_env *, void *), void *cbdata)
2831 struct ptlrpc_request *req = NULL;
2832 struct ptlrpc_work_async_args *args;
2838 RETURN(ERR_PTR(-EINVAL));
2840 /* copy some code from deprecated fakereq. */
2843 CERROR("ptlrpc: run out of memory!\n");
2844 RETURN(ERR_PTR(-ENOMEM));
2847 req->rq_send_state = LUSTRE_IMP_FULL;
2848 req->rq_type = PTL_RPC_MSG_REQUEST;
2849 req->rq_import = class_import_get(imp);
2850 req->rq_export = NULL;
2851 req->rq_interpret_reply = work_interpreter;
2852 /* don't want reply */
2853 req->rq_receiving_reply = 0;
2854 req->rq_must_unlink = 0;
2855 req->rq_no_delay = req->rq_no_resend = 1;
2857 cfs_spin_lock_init(&req->rq_lock);
2858 CFS_INIT_LIST_HEAD(&req->rq_list);
2859 CFS_INIT_LIST_HEAD(&req->rq_replay_list);
2860 CFS_INIT_LIST_HEAD(&req->rq_set_chain);
2861 CFS_INIT_LIST_HEAD(&req->rq_history_list);
2862 CFS_INIT_LIST_HEAD(&req->rq_exp_list);
2863 cfs_waitq_init(&req->rq_reply_waitq);
2864 cfs_waitq_init(&req->rq_set_waitq);
2865 cfs_atomic_set(&req->rq_refcount, 1);
2867 CLASSERT (sizeof(*args) <= sizeof(req->rq_async_args));
2868 args = ptlrpc_req_async_args(req);
2869 args->magic = PTLRPC_WORK_MAGIC;
2871 args->cbdata = cbdata;
2875 EXPORT_SYMBOL(ptlrpcd_alloc_work);
2877 void ptlrpcd_destroy_work(void *handler)
2879 struct ptlrpc_request *req = handler;
2882 ptlrpc_req_finished(req);
2884 EXPORT_SYMBOL(ptlrpcd_destroy_work);
2886 int ptlrpcd_queue_work(void *handler)
2888 struct ptlrpc_request *req = handler;
2891 * Check if the req is already being queued.
2893 * Here comes a trick: it lacks a way of checking if a req is being
2894 * processed reliably in ptlrpc. Here I have to use refcount of req
2895 * for this purpose. This is okay because the caller should use this
2896 * req as opaque data. - Jinshan
2898 LASSERT(cfs_atomic_read(&req->rq_refcount) > 0);
2899 if (cfs_atomic_read(&req->rq_refcount) > 1)
2902 if (cfs_atomic_inc_return(&req->rq_refcount) > 2) { /* race */
2903 cfs_atomic_dec(&req->rq_refcount);
2907 /* re-initialize the req */
2908 req->rq_timeout = obd_timeout;
2909 req->rq_sent = cfs_time_current_sec();
2910 req->rq_deadline = req->rq_sent + req->rq_timeout;
2911 req->rq_reply_deadline = req->rq_deadline;
2912 req->rq_phase = RQ_PHASE_INTERPRET;
2913 req->rq_next_phase = RQ_PHASE_COMPLETE;
2914 req->rq_xid = ptlrpc_next_xid();
2915 req->rq_import_generation = req->rq_import->imp_generation;
2917 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
2920 EXPORT_SYMBOL(ptlrpcd_queue_work);