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;
67 EXPORT_SYMBOL(ptlrpc_init_client);
70 * Return PortalRPC connection for remore uud \a uuid
72 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
74 struct ptlrpc_connection *c;
76 lnet_process_id_t peer;
79 err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
81 CNETERR("cannot find peer %s!\n", uuid->uuid);
85 c = ptlrpc_connection_get(peer, self, uuid);
87 memcpy(c->c_remote_uuid.uuid,
88 uuid->uuid, sizeof(c->c_remote_uuid.uuid));
91 CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
95 EXPORT_SYMBOL(ptlrpc_uuid_to_connection);
98 * Allocate and initialize new bulk descriptor
99 * Returns pointer to the descriptor or NULL on error.
101 struct ptlrpc_bulk_desc *new_bulk(int npages, int type, int portal)
103 struct ptlrpc_bulk_desc *desc;
105 OBD_ALLOC(desc, offsetof (struct ptlrpc_bulk_desc, bd_iov[npages]));
109 cfs_spin_lock_init(&desc->bd_lock);
110 cfs_waitq_init(&desc->bd_waitq);
111 desc->bd_max_iov = npages;
112 desc->bd_iov_count = 0;
113 LNetInvalidateHandle(&desc->bd_md_h);
114 desc->bd_portal = portal;
115 desc->bd_type = type;
121 * Prepare bulk descriptor for specified outgoing request \a req that
122 * can fit \a npages * pages. \a type is bulk type. \a portal is where
123 * the bulk to be sent. Used on client-side.
124 * Returns pointer to newly allocatrd initialized bulk descriptor or NULL on
127 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp(struct ptlrpc_request *req,
128 int npages, int type, int portal)
130 struct obd_import *imp = req->rq_import;
131 struct ptlrpc_bulk_desc *desc;
134 LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
135 desc = new_bulk(npages, type, portal);
139 desc->bd_import_generation = req->rq_import_generation;
140 desc->bd_import = class_import_get(imp);
143 desc->bd_cbid.cbid_fn = client_bulk_callback;
144 desc->bd_cbid.cbid_arg = desc;
146 /* This makes req own desc, and free it when she frees herself */
151 EXPORT_SYMBOL(ptlrpc_prep_bulk_imp);
154 * Add a page \a page to the bulk descriptor \a desc.
155 * Data to transfer in the page starts at offset \a pageoffset and
156 * amount of data to transfer from the page is \a len
158 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
159 cfs_page_t *page, int pageoffset, int len)
161 LASSERT(desc->bd_iov_count < desc->bd_max_iov);
162 LASSERT(page != NULL);
163 LASSERT(pageoffset >= 0);
165 LASSERT(pageoffset + len <= CFS_PAGE_SIZE);
170 ptlrpc_add_bulk_page(desc, page, pageoffset, len);
172 EXPORT_SYMBOL(ptlrpc_prep_bulk_page);
175 * Uninitialize and free bulk descriptor \a desc.
176 * Works on bulk descriptors both from server and client side.
178 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
183 LASSERT(desc != NULL);
184 LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
185 LASSERT(!desc->bd_network_rw); /* network hands off or */
186 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
188 sptlrpc_enc_pool_put_pages(desc);
191 class_export_put(desc->bd_export);
193 class_import_put(desc->bd_import);
195 for (i = 0; i < desc->bd_iov_count ; i++)
196 cfs_page_unpin(desc->bd_iov[i].kiov_page);
198 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
199 bd_iov[desc->bd_max_iov]));
202 EXPORT_SYMBOL(ptlrpc_free_bulk);
205 * Set server timelimit for this req, i.e. how long are we willing to wait
206 * for reply before timing out this request.
208 void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req)
214 LASSERT(req->rq_import);
217 /* non-AT settings */
219 * \a imp_server_timeout means this is reverse import and
220 * we send (currently only) ASTs to the client and cannot afford
221 * to wait too long for the reply, otherwise the other client
222 * (because of which we are sending this request) would
223 * timeout waiting for us
225 req->rq_timeout = req->rq_import->imp_server_timeout ?
226 obd_timeout / 2 : obd_timeout;
228 at = &req->rq_import->imp_at;
229 idx = import_at_get_index(req->rq_import,
230 req->rq_request_portal);
231 serv_est = at_get(&at->iat_service_estimate[idx]);
232 req->rq_timeout = at_est2timeout(serv_est);
234 /* We could get even fancier here, using history to predict increased
237 /* Let the server know what this RPC timeout is by putting it in the
239 lustre_msg_set_timeout(req->rq_reqmsg, req->rq_timeout);
241 EXPORT_SYMBOL(ptlrpc_at_set_req_timeout);
243 /* Adjust max service estimate based on server value */
244 static void ptlrpc_at_adj_service(struct ptlrpc_request *req,
245 unsigned int serv_est)
251 LASSERT(req->rq_import);
252 at = &req->rq_import->imp_at;
254 idx = import_at_get_index(req->rq_import, req->rq_request_portal);
255 /* max service estimates are tracked on the server side,
256 so just keep minimal history here */
257 oldse = at_measured(&at->iat_service_estimate[idx], serv_est);
259 CDEBUG(D_ADAPTTO, "The RPC service estimate for %s ptl %d "
260 "has changed from %d to %d\n",
261 req->rq_import->imp_obd->obd_name,req->rq_request_portal,
262 oldse, at_get(&at->iat_service_estimate[idx]));
265 /* Expected network latency per remote node (secs) */
266 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req)
268 return AT_OFF ? 0 : at_get(&req->rq_import->imp_at.iat_net_latency);
271 /* Adjust expected network latency */
272 static void ptlrpc_at_adj_net_latency(struct ptlrpc_request *req,
273 unsigned int service_time)
275 unsigned int nl, oldnl;
277 time_t now = cfs_time_current_sec();
279 LASSERT(req->rq_import);
280 at = &req->rq_import->imp_at;
282 /* Network latency is total time less server processing time */
283 nl = max_t(int, now - req->rq_sent - service_time, 0) +1/*st rounding*/;
284 if (service_time > now - req->rq_sent + 3 /* bz16408 */)
285 CWARN("Reported service time %u > total measured time "
286 CFS_DURATION_T"\n", service_time,
287 cfs_time_sub(now, req->rq_sent));
289 oldnl = at_measured(&at->iat_net_latency, nl);
291 CDEBUG(D_ADAPTTO, "The network latency for %s (nid %s) "
292 "has changed from %d to %d\n",
293 req->rq_import->imp_obd->obd_name,
295 &req->rq_import->imp_connection->c_remote_uuid),
296 oldnl, at_get(&at->iat_net_latency));
299 static int unpack_reply(struct ptlrpc_request *req)
303 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
304 rc = ptlrpc_unpack_rep_msg(req, req->rq_replen);
306 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
311 rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
313 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
320 * Handle an early reply message, called with the rq_lock held.
321 * If anything goes wrong just ignore it - same as if it never happened
323 static int ptlrpc_at_recv_early_reply(struct ptlrpc_request *req)
325 struct ptlrpc_request *early_req;
331 cfs_spin_unlock(&req->rq_lock);
333 rc = sptlrpc_cli_unwrap_early_reply(req, &early_req);
335 cfs_spin_lock(&req->rq_lock);
339 rc = unpack_reply(early_req);
341 /* Expecting to increase the service time estimate here */
342 ptlrpc_at_adj_service(req,
343 lustre_msg_get_timeout(early_req->rq_repmsg));
344 ptlrpc_at_adj_net_latency(req,
345 lustre_msg_get_service_time(early_req->rq_repmsg));
348 sptlrpc_cli_finish_early_reply(early_req);
350 cfs_spin_lock(&req->rq_lock);
353 /* Adjust the local timeout for this req */
354 ptlrpc_at_set_req_timeout(req);
356 olddl = req->rq_deadline;
357 /* server assumes it now has rq_timeout from when it sent the
358 early reply, so client should give it at least that long. */
359 req->rq_deadline = cfs_time_current_sec() + req->rq_timeout +
360 ptlrpc_at_get_net_latency(req);
362 DEBUG_REQ(D_ADAPTTO, req,
363 "Early reply #%d, new deadline in "CFS_DURATION_T"s "
364 "("CFS_DURATION_T"s)", req->rq_early_count,
365 cfs_time_sub(req->rq_deadline,
366 cfs_time_current_sec()),
367 cfs_time_sub(req->rq_deadline, olddl));
374 * Wind down request pool \a pool.
375 * Frees all requests from the pool too
377 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
380 struct ptlrpc_request *req;
382 LASSERT(pool != NULL);
384 cfs_spin_lock(&pool->prp_lock);
385 cfs_list_for_each_safe(l, tmp, &pool->prp_req_list) {
386 req = cfs_list_entry(l, struct ptlrpc_request, rq_list);
387 cfs_list_del(&req->rq_list);
388 LASSERT(req->rq_reqbuf);
389 LASSERT(req->rq_reqbuf_len == pool->prp_rq_size);
390 OBD_FREE_LARGE(req->rq_reqbuf, pool->prp_rq_size);
391 OBD_FREE(req, sizeof(*req));
393 cfs_spin_unlock(&pool->prp_lock);
394 OBD_FREE(pool, sizeof(*pool));
396 EXPORT_SYMBOL(ptlrpc_free_rq_pool);
399 * Allocates, initializes and adds \a num_rq requests to the pool \a pool
401 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
406 while (size < pool->prp_rq_size)
409 LASSERTF(cfs_list_empty(&pool->prp_req_list) ||
410 size == pool->prp_rq_size,
411 "Trying to change pool size with nonempty pool "
412 "from %d to %d bytes\n", pool->prp_rq_size, size);
414 cfs_spin_lock(&pool->prp_lock);
415 pool->prp_rq_size = size;
416 for (i = 0; i < num_rq; i++) {
417 struct ptlrpc_request *req;
418 struct lustre_msg *msg;
420 cfs_spin_unlock(&pool->prp_lock);
421 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
424 OBD_ALLOC_LARGE(msg, size);
426 OBD_FREE(req, sizeof(struct ptlrpc_request));
429 req->rq_reqbuf = msg;
430 req->rq_reqbuf_len = size;
432 cfs_spin_lock(&pool->prp_lock);
433 cfs_list_add_tail(&req->rq_list, &pool->prp_req_list);
435 cfs_spin_unlock(&pool->prp_lock);
438 EXPORT_SYMBOL(ptlrpc_add_rqs_to_pool);
441 * Create and initialize new request pool with given attributes:
442 * \a num_rq - initial number of requests to create for the pool
443 * \a msgsize - maximum message size possible for requests in thid pool
444 * \a populate_pool - function to be called when more requests need to be added
446 * Returns pointer to newly created pool or NULL on error.
448 struct ptlrpc_request_pool *
449 ptlrpc_init_rq_pool(int num_rq, int msgsize,
450 void (*populate_pool)(struct ptlrpc_request_pool *, int))
452 struct ptlrpc_request_pool *pool;
454 OBD_ALLOC(pool, sizeof (struct ptlrpc_request_pool));
458 /* Request next power of two for the allocation, because internally
459 kernel would do exactly this */
461 cfs_spin_lock_init(&pool->prp_lock);
462 CFS_INIT_LIST_HEAD(&pool->prp_req_list);
463 pool->prp_rq_size = msgsize + SPTLRPC_MAX_PAYLOAD;
464 pool->prp_populate = populate_pool;
466 populate_pool(pool, num_rq);
468 if (cfs_list_empty(&pool->prp_req_list)) {
469 /* have not allocated a single request for the pool */
470 OBD_FREE(pool, sizeof (struct ptlrpc_request_pool));
475 EXPORT_SYMBOL(ptlrpc_init_rq_pool);
478 * Fetches one request from pool \a pool
480 static struct ptlrpc_request *
481 ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
483 struct ptlrpc_request *request;
484 struct lustre_msg *reqbuf;
489 cfs_spin_lock(&pool->prp_lock);
491 /* See if we have anything in a pool, and bail out if nothing,
492 * in writeout path, where this matters, this is safe to do, because
493 * nothing is lost in this case, and when some in-flight requests
494 * complete, this code will be called again. */
495 if (unlikely(cfs_list_empty(&pool->prp_req_list))) {
496 cfs_spin_unlock(&pool->prp_lock);
500 request = cfs_list_entry(pool->prp_req_list.next, struct ptlrpc_request,
502 cfs_list_del_init(&request->rq_list);
503 cfs_spin_unlock(&pool->prp_lock);
505 LASSERT(request->rq_reqbuf);
506 LASSERT(request->rq_pool);
508 reqbuf = request->rq_reqbuf;
509 memset(request, 0, sizeof(*request));
510 request->rq_reqbuf = reqbuf;
511 request->rq_reqbuf_len = pool->prp_rq_size;
512 request->rq_pool = pool;
518 * Returns freed \a request to pool.
520 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
522 struct ptlrpc_request_pool *pool = request->rq_pool;
524 cfs_spin_lock(&pool->prp_lock);
525 LASSERT(cfs_list_empty(&request->rq_list));
526 LASSERT(!request->rq_receiving_reply);
527 cfs_list_add_tail(&request->rq_list, &pool->prp_req_list);
528 cfs_spin_unlock(&pool->prp_lock);
531 static int __ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
532 __u32 version, int opcode,
533 int count, __u32 *lengths, char **bufs,
534 struct ptlrpc_cli_ctx *ctx)
536 struct obd_import *imp = request->rq_import;
541 request->rq_cli_ctx = sptlrpc_cli_ctx_get(ctx);
543 rc = sptlrpc_req_get_ctx(request);
548 sptlrpc_req_set_flavor(request, opcode);
550 rc = lustre_pack_request(request, imp->imp_msg_magic, count,
553 LASSERT(!request->rq_pool);
557 lustre_msg_add_version(request->rq_reqmsg, version);
558 request->rq_send_state = LUSTRE_IMP_FULL;
559 request->rq_type = PTL_RPC_MSG_REQUEST;
560 request->rq_export = NULL;
562 request->rq_req_cbid.cbid_fn = request_out_callback;
563 request->rq_req_cbid.cbid_arg = request;
565 request->rq_reply_cbid.cbid_fn = reply_in_callback;
566 request->rq_reply_cbid.cbid_arg = request;
568 request->rq_reply_deadline = 0;
569 request->rq_phase = RQ_PHASE_NEW;
570 request->rq_next_phase = RQ_PHASE_UNDEFINED;
572 request->rq_request_portal = imp->imp_client->cli_request_portal;
573 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
575 ptlrpc_at_set_req_timeout(request);
577 cfs_spin_lock_init(&request->rq_lock);
578 CFS_INIT_LIST_HEAD(&request->rq_list);
579 CFS_INIT_LIST_HEAD(&request->rq_timed_list);
580 CFS_INIT_LIST_HEAD(&request->rq_replay_list);
581 CFS_INIT_LIST_HEAD(&request->rq_ctx_chain);
582 CFS_INIT_LIST_HEAD(&request->rq_set_chain);
583 CFS_INIT_LIST_HEAD(&request->rq_history_list);
584 CFS_INIT_LIST_HEAD(&request->rq_exp_list);
585 cfs_waitq_init(&request->rq_reply_waitq);
586 cfs_waitq_init(&request->rq_set_waitq);
587 request->rq_xid = ptlrpc_next_xid();
588 cfs_atomic_set(&request->rq_refcount, 1);
590 lustre_msg_set_opc(request->rq_reqmsg, opcode);
594 sptlrpc_cli_ctx_put(request->rq_cli_ctx, 1);
596 class_import_put(imp);
600 int ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
601 __u32 version, int opcode, char **bufs,
602 struct ptlrpc_cli_ctx *ctx)
606 count = req_capsule_filled_sizes(&request->rq_pill, RCL_CLIENT);
607 return __ptlrpc_request_bufs_pack(request, version, opcode, count,
608 request->rq_pill.rc_area[RCL_CLIENT],
611 EXPORT_SYMBOL(ptlrpc_request_bufs_pack);
614 * Pack request buffers for network transfer, performing necessary encryption
615 * steps if necessary.
617 int ptlrpc_request_pack(struct ptlrpc_request *request,
618 __u32 version, int opcode)
621 rc = ptlrpc_request_bufs_pack(request, version, opcode, NULL, NULL);
625 /* For some old 1.8 clients (< 1.8.7), they will LASSERT the size of
626 * ptlrpc_body sent from server equal to local ptlrpc_body size, so we
627 * have to send old ptlrpc_body to keep interoprability with these
630 * Only three kinds of server->client RPCs so far:
635 * XXX This should be removed whenever we drop the interoprability with
636 * the these old clients.
638 if (opcode == LDLM_BL_CALLBACK || opcode == LDLM_CP_CALLBACK ||
639 opcode == LDLM_GL_CALLBACK)
640 req_capsule_shrink(&request->rq_pill, &RMF_PTLRPC_BODY,
641 sizeof(struct ptlrpc_body_v2), RCL_CLIENT);
645 EXPORT_SYMBOL(ptlrpc_request_pack);
648 * Helper function to allocate new request on import \a imp
649 * and possibly using existing request from pool \a pool if provided.
650 * Returns allocated request structure with import field filled or
654 struct ptlrpc_request *__ptlrpc_request_alloc(struct obd_import *imp,
655 struct ptlrpc_request_pool *pool)
657 struct ptlrpc_request *request = NULL;
660 request = ptlrpc_prep_req_from_pool(pool);
663 OBD_ALLOC_PTR(request);
666 LASSERTF((unsigned long)imp > 0x1000, "%p", imp);
667 LASSERT(imp != LP_POISON);
668 LASSERTF((unsigned long)imp->imp_client > 0x1000, "%p",
670 LASSERT(imp->imp_client != LP_POISON);
672 request->rq_import = class_import_get(imp);
674 CERROR("request allocation out of memory\n");
681 * Helper function for creating a request.
682 * Calls __ptlrpc_request_alloc to allocate new request sturcture and inits
683 * buffer structures according to capsule template \a format.
684 * Returns allocated request structure pointer or NULL on error.
686 static struct ptlrpc_request *
687 ptlrpc_request_alloc_internal(struct obd_import *imp,
688 struct ptlrpc_request_pool * pool,
689 const struct req_format *format)
691 struct ptlrpc_request *request;
693 request = __ptlrpc_request_alloc(imp, pool);
697 req_capsule_init(&request->rq_pill, request, RCL_CLIENT);
698 req_capsule_set(&request->rq_pill, format);
703 * Allocate new request structure for import \a imp and initialize its
704 * buffer structure according to capsule template \a format.
706 struct ptlrpc_request *ptlrpc_request_alloc(struct obd_import *imp,
707 const struct req_format *format)
709 return ptlrpc_request_alloc_internal(imp, NULL, format);
711 EXPORT_SYMBOL(ptlrpc_request_alloc);
714 * Allocate new request structure for import \a imp from pool \a pool and
715 * initialize its buffer structure according to capsule template \a format.
717 struct ptlrpc_request *ptlrpc_request_alloc_pool(struct obd_import *imp,
718 struct ptlrpc_request_pool * pool,
719 const struct req_format *format)
721 return ptlrpc_request_alloc_internal(imp, pool, format);
723 EXPORT_SYMBOL(ptlrpc_request_alloc_pool);
726 * For requests not from pool, free memory of the request structure.
727 * For requests obtained from a pool earlier, return request back to pool.
729 void ptlrpc_request_free(struct ptlrpc_request *request)
731 if (request->rq_pool)
732 __ptlrpc_free_req_to_pool(request);
734 OBD_FREE_PTR(request);
736 EXPORT_SYMBOL(ptlrpc_request_free);
739 * Allocate new request for operatione \a opcode and immediatelly pack it for
741 * Only used for simple requests like OBD_PING where the only important
742 * part of the request is operation itself.
743 * Returns allocated request or NULL on error.
745 struct ptlrpc_request *ptlrpc_request_alloc_pack(struct obd_import *imp,
746 const struct req_format *format,
747 __u32 version, int opcode)
749 struct ptlrpc_request *req = ptlrpc_request_alloc(imp, format);
753 rc = ptlrpc_request_pack(req, version, opcode);
755 ptlrpc_request_free(req);
761 EXPORT_SYMBOL(ptlrpc_request_alloc_pack);
764 * Prepare request (fetched from pool \a poolif not NULL) on import \a imp
765 * for operation \a opcode. Request would contain \a count buffers.
766 * Sizes of buffers are described in array \a lengths and buffers themselves
767 * are provided by a pointer \a bufs.
768 * Returns prepared request structure pointer or NULL on error.
770 struct ptlrpc_request *
771 ptlrpc_prep_req_pool(struct obd_import *imp,
772 __u32 version, int opcode,
773 int count, __u32 *lengths, char **bufs,
774 struct ptlrpc_request_pool *pool)
776 struct ptlrpc_request *request;
779 request = __ptlrpc_request_alloc(imp, pool);
783 rc = __ptlrpc_request_bufs_pack(request, version, opcode, count,
784 lengths, bufs, NULL);
786 ptlrpc_request_free(request);
791 EXPORT_SYMBOL(ptlrpc_prep_req_pool);
794 * Same as ptlrpc_prep_req_pool, but without pool
796 struct ptlrpc_request *
797 ptlrpc_prep_req(struct obd_import *imp, __u32 version, int opcode, int count,
798 __u32 *lengths, char **bufs)
800 return ptlrpc_prep_req_pool(imp, version, opcode, count, lengths, bufs,
803 EXPORT_SYMBOL(ptlrpc_prep_req);
806 * Allocate "fake" request that would not be sent anywhere in the end.
807 * Only used as a hack because we have no other way of performing
808 * async actions in lustre between layers.
809 * Used on MDS to request object preallocations from more than one OST at a
812 struct ptlrpc_request *ptlrpc_prep_fakereq(struct obd_import *imp,
813 unsigned int timeout,
814 ptlrpc_interpterer_t interpreter)
816 struct ptlrpc_request *request = NULL;
819 OBD_ALLOC(request, sizeof(*request));
821 CERROR("request allocation out of memory\n");
825 request->rq_send_state = LUSTRE_IMP_FULL;
826 request->rq_type = PTL_RPC_MSG_REQUEST;
827 request->rq_import = class_import_get(imp);
828 request->rq_export = NULL;
829 request->rq_import_generation = imp->imp_generation;
831 request->rq_timeout = timeout;
832 request->rq_sent = cfs_time_current_sec();
833 request->rq_deadline = request->rq_sent + timeout;
834 request->rq_reply_deadline = request->rq_deadline;
835 request->rq_interpret_reply = interpreter;
836 request->rq_phase = RQ_PHASE_RPC;
837 request->rq_next_phase = RQ_PHASE_INTERPRET;
838 /* don't want reply */
839 request->rq_receiving_reply = 0;
840 request->rq_must_unlink = 0;
841 request->rq_no_delay = request->rq_no_resend = 1;
842 request->rq_fake = 1;
844 cfs_spin_lock_init(&request->rq_lock);
845 CFS_INIT_LIST_HEAD(&request->rq_list);
846 CFS_INIT_LIST_HEAD(&request->rq_replay_list);
847 CFS_INIT_LIST_HEAD(&request->rq_set_chain);
848 CFS_INIT_LIST_HEAD(&request->rq_history_list);
849 CFS_INIT_LIST_HEAD(&request->rq_exp_list);
850 cfs_waitq_init(&request->rq_reply_waitq);
851 cfs_waitq_init(&request->rq_set_waitq);
853 request->rq_xid = ptlrpc_next_xid();
854 cfs_atomic_set(&request->rq_refcount, 1);
858 EXPORT_SYMBOL(ptlrpc_prep_fakereq);
861 * Indicate that processing of "fake" request is finished.
863 void ptlrpc_fakereq_finished(struct ptlrpc_request *req)
865 struct ptlrpc_request_set *set = req->rq_set;
868 /* hold ref on the request to prevent others (ptlrpcd) to free it */
869 ptlrpc_request_addref(req);
870 cfs_list_del_init(&req->rq_list);
872 /* if we kill request before timeout - need adjust counter */
873 if (req->rq_phase == RQ_PHASE_RPC && set != NULL &&
874 cfs_atomic_dec_and_test(&set->set_remaining))
877 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
879 /* Only need to call wakeup once when to be empty. */
881 cfs_waitq_signal(&set->set_waitq);
882 ptlrpc_req_finished(req);
884 EXPORT_SYMBOL(ptlrpc_fakereq_finished);
887 * Allocate and initialize new request set structure.
888 * Returns a pointer to the newly allocated set structure or NULL on error.
890 struct ptlrpc_request_set *ptlrpc_prep_set(void)
892 struct ptlrpc_request_set *set;
895 OBD_ALLOC(set, sizeof *set);
898 cfs_atomic_set(&set->set_refcount, 1);
899 CFS_INIT_LIST_HEAD(&set->set_requests);
900 cfs_waitq_init(&set->set_waitq);
901 cfs_atomic_set(&set->set_new_count, 0);
902 cfs_atomic_set(&set->set_remaining, 0);
903 cfs_spin_lock_init(&set->set_new_req_lock);
904 CFS_INIT_LIST_HEAD(&set->set_new_requests);
905 CFS_INIT_LIST_HEAD(&set->set_cblist);
906 set->set_max_inflight = UINT_MAX;
907 set->set_producer = NULL;
908 set->set_producer_arg = NULL;
913 EXPORT_SYMBOL(ptlrpc_prep_set);
916 * Allocate and initialize new request set structure with flow control
917 * extension. This extension allows to control the number of requests in-flight
918 * for the whole set. A callback function to generate requests must be provided
919 * and the request set will keep the number of requests sent over the wire to
921 * Returns a pointer to the newly allocated set structure or NULL on error.
923 struct ptlrpc_request_set *ptlrpc_prep_fcset(int max, set_producer_func func,
927 struct ptlrpc_request_set *set;
929 set = ptlrpc_prep_set();
933 set->set_max_inflight = max;
934 set->set_producer = func;
935 set->set_producer_arg = arg;
939 EXPORT_SYMBOL(ptlrpc_prep_fcset);
942 * Wind down and free request set structure previously allocated with
944 * Ensures that all requests on the set have completed and removes
945 * all requests from the request list in a set.
946 * If any unsent request happen to be on the list, pretends that they got
947 * an error in flight and calls their completion handler.
949 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
957 /* Requests on the set should either all be completed, or all be new */
958 expected_phase = (cfs_atomic_read(&set->set_remaining) == 0) ?
959 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
960 cfs_list_for_each (tmp, &set->set_requests) {
961 struct ptlrpc_request *req =
962 cfs_list_entry(tmp, struct ptlrpc_request,
965 LASSERT(req->rq_phase == expected_phase);
969 LASSERTF(cfs_atomic_read(&set->set_remaining) == 0 ||
970 cfs_atomic_read(&set->set_remaining) == n, "%d / %d\n",
971 cfs_atomic_read(&set->set_remaining), n);
973 cfs_list_for_each_safe(tmp, next, &set->set_requests) {
974 struct ptlrpc_request *req =
975 cfs_list_entry(tmp, struct ptlrpc_request,
977 cfs_list_del_init(&req->rq_set_chain);
979 LASSERT(req->rq_phase == expected_phase);
981 if (req->rq_phase == RQ_PHASE_NEW) {
982 ptlrpc_req_interpret(NULL, req, -EBADR);
983 cfs_atomic_dec(&set->set_remaining);
986 cfs_spin_lock(&req->rq_lock);
988 req->rq_invalid_rqset = 0;
989 cfs_spin_unlock(&req->rq_lock);
991 ptlrpc_req_finished (req);
994 LASSERT(cfs_atomic_read(&set->set_remaining) == 0);
996 ptlrpc_reqset_put(set);
999 EXPORT_SYMBOL(ptlrpc_set_destroy);
1002 * Add a callback function \a fn to the set.
1003 * This function would be called when all requests on this set are completed.
1004 * The function will be passed \a data argument.
1006 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
1007 set_interpreter_func fn, void *data)
1009 struct ptlrpc_set_cbdata *cbdata;
1011 OBD_ALLOC_PTR(cbdata);
1015 cbdata->psc_interpret = fn;
1016 cbdata->psc_data = data;
1017 cfs_list_add_tail(&cbdata->psc_item, &set->set_cblist);
1021 EXPORT_SYMBOL(ptlrpc_set_add_cb);
1024 * Add a new request to the general purpose request set.
1025 * Assumes request reference from the caller.
1027 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
1028 struct ptlrpc_request *req)
1030 char jobid[JOBSTATS_JOBID_SIZE];
1031 LASSERT(cfs_list_empty(&req->rq_set_chain));
1033 /* The set takes over the caller's request reference */
1034 cfs_list_add_tail(&req->rq_set_chain, &set->set_requests);
1036 cfs_atomic_inc(&set->set_remaining);
1037 req->rq_queued_time = cfs_time_current();
1039 if (req->rq_reqmsg) {
1040 lustre_get_jobid(jobid);
1041 lustre_msg_set_jobid(req->rq_reqmsg, jobid);
1044 if (set->set_producer != NULL)
1045 /* If the request set has a producer callback, the RPC must be
1046 * sent straight away */
1047 ptlrpc_send_new_req(req);
1049 EXPORT_SYMBOL(ptlrpc_set_add_req);
1052 * Add a request to a request with dedicated server thread
1053 * and wake the thread to make any necessary processing.
1054 * Currently only used for ptlrpcd.
1056 void ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
1057 struct ptlrpc_request *req)
1059 struct ptlrpc_request_set *set = pc->pc_set;
1062 LASSERT(req->rq_set == NULL);
1063 LASSERT(cfs_test_bit(LIOD_STOP, &pc->pc_flags) == 0);
1065 cfs_spin_lock(&set->set_new_req_lock);
1067 * The set takes over the caller's request reference.
1070 req->rq_queued_time = cfs_time_current();
1071 cfs_list_add_tail(&req->rq_set_chain, &set->set_new_requests);
1072 count = cfs_atomic_inc_return(&set->set_new_count);
1073 cfs_spin_unlock(&set->set_new_req_lock);
1075 /* Only need to call wakeup once for the first entry. */
1077 cfs_waitq_signal(&set->set_waitq);
1079 /* XXX: It maybe unnecessary to wakeup all the partners. But to
1080 * guarantee the async RPC can be processed ASAP, we have
1081 * no other better choice. It maybe fixed in future. */
1082 for (i = 0; i < pc->pc_npartners; i++)
1083 cfs_waitq_signal(&pc->pc_partners[i]->pc_set->set_waitq);
1086 EXPORT_SYMBOL(ptlrpc_set_add_new_req);
1089 * Based on the current state of the import, determine if the request
1090 * can be sent, is an error, or should be delayed.
1092 * Returns true if this request should be delayed. If false, and
1093 * *status is set, then the request can not be sent and *status is the
1094 * error code. If false and status is 0, then request can be sent.
1096 * The imp->imp_lock must be held.
1098 static int ptlrpc_import_delay_req(struct obd_import *imp,
1099 struct ptlrpc_request *req, int *status)
1104 LASSERT (status != NULL);
1107 if (req->rq_ctx_init || req->rq_ctx_fini) {
1108 /* always allow ctx init/fini rpc go through */
1109 } else if (imp->imp_state == LUSTRE_IMP_NEW) {
1110 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
1112 } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
1113 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
1115 } else if (ptlrpc_send_limit_expired(req)) {
1116 /* probably doesn't need to be a D_ERROR after initial testing */
1117 DEBUG_REQ(D_ERROR, req, "send limit expired ");
1119 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
1120 imp->imp_state == LUSTRE_IMP_CONNECTING) {
1121 /* allow CONNECT even if import is invalid */ ;
1122 if (cfs_atomic_read(&imp->imp_inval_count) != 0) {
1123 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1126 } else if (imp->imp_invalid || imp->imp_obd->obd_no_recov) {
1127 if (!imp->imp_deactive)
1128 DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
1129 *status = -ESHUTDOWN; /* bz 12940 */
1130 } else if (req->rq_import_generation != imp->imp_generation) {
1131 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
1133 } else if (req->rq_send_state != imp->imp_state) {
1134 /* invalidate in progress - any requests should be drop */
1135 if (cfs_atomic_read(&imp->imp_inval_count) != 0) {
1136 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1138 } else if (imp->imp_dlm_fake || req->rq_no_delay) {
1139 *status = -EWOULDBLOCK;
1149 * Decide if the eror message regarding provided request \a req
1150 * should be printed to the console or not.
1151 * Makes it's decision on request status and other properties.
1152 * Returns 1 to print error on the system console or 0 if not.
1154 static int ptlrpc_console_allow(struct ptlrpc_request *req)
1159 /* Fake requests include no rq_reqmsg */
1163 LASSERT(req->rq_reqmsg != NULL);
1164 opc = lustre_msg_get_opc(req->rq_reqmsg);
1166 /* Suppress particular reconnect errors which are to be expected. No
1167 * errors are suppressed for the initial connection on an import */
1168 if ((lustre_handle_is_used(&req->rq_import->imp_remote_handle)) &&
1169 (opc == OST_CONNECT || opc == MDS_CONNECT || opc == MGS_CONNECT)) {
1171 /* Suppress timed out reconnect requests */
1172 if (req->rq_timedout)
1175 /* Suppress unavailable/again reconnect requests */
1176 err = lustre_msg_get_status(req->rq_repmsg);
1177 if (err == -ENODEV || err == -EAGAIN)
1185 * Check request processing status.
1186 * Returns the status.
1188 static int ptlrpc_check_status(struct ptlrpc_request *req)
1193 err = lustre_msg_get_status(req->rq_repmsg);
1194 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
1195 struct obd_import *imp = req->rq_import;
1196 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1197 LCONSOLE_ERROR_MSG(0x011,"an error occurred while communicating"
1198 " with %s. The %s operation failed with %d\n",
1199 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1200 ll_opcode2str(opc), err);
1201 RETURN(err < 0 ? err : -EINVAL);
1205 DEBUG_REQ(D_INFO, req, "status is %d", err);
1206 } else if (err > 0) {
1207 /* XXX: translate this error from net to host */
1208 DEBUG_REQ(D_INFO, req, "status is %d", err);
1211 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
1212 struct obd_import *imp = req->rq_import;
1213 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1215 if (ptlrpc_console_allow(req))
1216 LCONSOLE_ERROR_MSG(0x011,"an error occurred while "
1217 "communicating with %s. The %s "
1218 "operation failed with %d\n",
1220 imp->imp_connection->c_peer.nid),
1221 ll_opcode2str(opc), err);
1223 RETURN(err < 0 ? err : -EINVAL);
1230 * save pre-versions of objects into request for replay.
1231 * Versions are obtained from server reply.
1234 static void ptlrpc_save_versions(struct ptlrpc_request *req)
1236 struct lustre_msg *repmsg = req->rq_repmsg;
1237 struct lustre_msg *reqmsg = req->rq_reqmsg;
1238 __u64 *versions = lustre_msg_get_versions(repmsg);
1241 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1245 lustre_msg_set_versions(reqmsg, versions);
1246 CDEBUG(D_INFO, "Client save versions ["LPX64"/"LPX64"]\n",
1247 versions[0], versions[1]);
1253 * Callback function called when client receives RPC reply for \a req.
1254 * Returns 0 on success or error code.
1255 * The return alue would be assigned to req->rq_status by the caller
1256 * as request processing status.
1257 * This function also decides if the request needs to be saved for later replay.
1259 static int after_reply(struct ptlrpc_request *req)
1261 struct obd_import *imp = req->rq_import;
1262 struct obd_device *obd = req->rq_import->imp_obd;
1264 struct timeval work_start;
1268 LASSERT(obd != NULL);
1269 /* repbuf must be unlinked */
1270 LASSERT(!req->rq_receiving_reply && !req->rq_must_unlink);
1272 if (req->rq_reply_truncate) {
1273 if (ptlrpc_no_resend(req)) {
1274 DEBUG_REQ(D_ERROR, req, "reply buffer overflow,"
1275 " expected: %d, actual size: %d",
1276 req->rq_nob_received, req->rq_repbuf_len);
1280 sptlrpc_cli_free_repbuf(req);
1281 /* Pass the required reply buffer size (include
1282 * space for early reply).
1283 * NB: no need to roundup because alloc_repbuf
1284 * will roundup it */
1285 req->rq_replen = req->rq_nob_received;
1286 req->rq_nob_received = 0;
1292 * NB Until this point, the whole of the incoming message,
1293 * including buflens, status etc is in the sender's byte order.
1295 rc = sptlrpc_cli_unwrap_reply(req);
1297 DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
1301 /* retry indefinitely on EINPROGRESS */
1302 if (lustre_msg_get_status(req->rq_repmsg) == -EINPROGRESS &&
1303 ptlrpc_no_resend(req) == 0 && !req->rq_no_retry_einprogress) {
1304 time_t now = cfs_time_current_sec();
1306 DEBUG_REQ(D_RPCTRACE, req, "Resending request on EINPROGRESS");
1308 req->rq_nr_resend++;
1310 /* Readjust the timeout for current conditions */
1311 ptlrpc_at_set_req_timeout(req);
1312 /* delay resend to give a chance to the server to get ready.
1313 * The delay is increased by 1s on every resend and is capped to
1314 * the current request timeout (i.e. obd_timeout if AT is off,
1315 * or AT service time x 125% + 5s, see at_est2timeout) */
1316 if (req->rq_nr_resend > req->rq_timeout)
1317 req->rq_sent = now + req->rq_timeout;
1319 req->rq_sent = now + req->rq_nr_resend;
1323 * Security layer unwrap might ask resend this request.
1328 rc = unpack_reply(req);
1332 cfs_gettimeofday(&work_start);
1333 timediff = cfs_timeval_sub(&work_start, &req->rq_arrival_time, NULL);
1334 if (obd->obd_svc_stats != NULL) {
1335 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR,
1337 ptlrpc_lprocfs_rpc_sent(req, timediff);
1340 if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
1341 lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
1342 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
1343 lustre_msg_get_type(req->rq_repmsg));
1347 if (lustre_msg_get_opc(req->rq_reqmsg) != OBD_PING)
1348 CFS_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_PAUSE_REP, cfs_fail_val);
1349 ptlrpc_at_adj_service(req, lustre_msg_get_timeout(req->rq_repmsg));
1350 ptlrpc_at_adj_net_latency(req,
1351 lustre_msg_get_service_time(req->rq_repmsg));
1353 rc = ptlrpc_check_status(req);
1354 imp->imp_connect_error = rc;
1358 * Either we've been evicted, or the server has failed for
1359 * some reason. Try to reconnect, and if that fails, punt to
1362 if (ll_rpc_recoverable_error(rc)) {
1363 if (req->rq_send_state != LUSTRE_IMP_FULL ||
1364 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
1367 ptlrpc_request_handle_notconn(req);
1372 * Let's look if server sent slv. Do it only for RPC with
1375 ldlm_cli_update_pool(req);
1379 * Store transno in reqmsg for replay.
1381 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
1382 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
1383 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
1386 if (imp->imp_replayable) {
1387 cfs_spin_lock(&imp->imp_lock);
1389 * No point in adding already-committed requests to the replay
1390 * list, we will just remove them immediately. b=9829
1392 if (req->rq_transno != 0 &&
1394 lustre_msg_get_last_committed(req->rq_repmsg) ||
1396 /** version recovery */
1397 ptlrpc_save_versions(req);
1398 ptlrpc_retain_replayable_request(req, imp);
1399 } else if (req->rq_commit_cb != NULL) {
1400 cfs_spin_unlock(&imp->imp_lock);
1401 req->rq_commit_cb(req);
1402 cfs_spin_lock(&imp->imp_lock);
1406 * Replay-enabled imports return commit-status information.
1408 if (lustre_msg_get_last_committed(req->rq_repmsg)) {
1409 imp->imp_peer_committed_transno =
1410 lustre_msg_get_last_committed(req->rq_repmsg);
1412 ptlrpc_free_committed(imp);
1414 if (req->rq_transno > imp->imp_peer_committed_transno)
1415 ptlrpc_pinger_commit_expected(imp);
1417 cfs_spin_unlock(&imp->imp_lock);
1424 * Helper function to send request \a req over the network for the first time
1425 * Also adjusts request phase.
1426 * Returns 0 on success or error code.
1428 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
1430 struct obd_import *imp = req->rq_import;
1434 LASSERT(req->rq_phase == RQ_PHASE_NEW);
1435 if (req->rq_sent && (req->rq_sent > cfs_time_current_sec()) &&
1436 (!req->rq_generation_set ||
1437 req->rq_import_generation == imp->imp_generation))
1440 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
1442 cfs_spin_lock(&imp->imp_lock);
1444 if (!req->rq_generation_set)
1445 req->rq_import_generation = imp->imp_generation;
1447 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1448 cfs_spin_lock(&req->rq_lock);
1449 req->rq_waiting = 1;
1450 cfs_spin_unlock(&req->rq_lock);
1452 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
1453 "(%s != %s)", lustre_msg_get_status(req->rq_reqmsg),
1454 ptlrpc_import_state_name(req->rq_send_state),
1455 ptlrpc_import_state_name(imp->imp_state));
1456 LASSERT(cfs_list_empty(&req->rq_list));
1457 cfs_list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1458 cfs_atomic_inc(&req->rq_import->imp_inflight);
1459 cfs_spin_unlock(&imp->imp_lock);
1464 cfs_spin_unlock(&imp->imp_lock);
1465 req->rq_status = rc;
1466 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1470 LASSERT(cfs_list_empty(&req->rq_list));
1471 cfs_list_add_tail(&req->rq_list, &imp->imp_sending_list);
1472 cfs_atomic_inc(&req->rq_import->imp_inflight);
1473 cfs_spin_unlock(&imp->imp_lock);
1475 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
1477 rc = sptlrpc_req_refresh_ctx(req, -1);
1480 req->rq_status = rc;
1483 req->rq_wait_ctx = 1;
1488 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
1489 " %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1490 imp->imp_obd->obd_uuid.uuid,
1491 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1492 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1493 lustre_msg_get_opc(req->rq_reqmsg));
1495 rc = ptl_send_rpc(req, 0);
1497 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
1498 req->rq_net_err = 1;
1504 static inline int ptlrpc_set_producer(struct ptlrpc_request_set *set)
1509 LASSERT(set->set_producer != NULL);
1511 remaining = cfs_atomic_read(&set->set_remaining);
1513 /* populate the ->set_requests list with requests until we
1514 * reach the maximum number of RPCs in flight for this set */
1515 while (cfs_atomic_read(&set->set_remaining) < set->set_max_inflight) {
1516 rc = set->set_producer(set, set->set_producer_arg);
1517 if (rc == -ENOENT) {
1518 /* no more RPC to produce */
1519 set->set_producer = NULL;
1520 set->set_producer_arg = NULL;
1525 RETURN((cfs_atomic_read(&set->set_remaining) - remaining));
1529 * this sends any unsent RPCs in \a set and returns 1 if all are sent
1530 * and no more replies are expected.
1531 * (it is possible to get less replies than requests sent e.g. due to timed out
1532 * requests or requests that we had trouble to send out)
1534 int ptlrpc_check_set(const struct lu_env *env, struct ptlrpc_request_set *set)
1536 cfs_list_t *tmp, *next;
1537 int force_timer_recalc = 0;
1540 if (cfs_atomic_read(&set->set_remaining) == 0)
1543 cfs_list_for_each_safe(tmp, next, &set->set_requests) {
1544 struct ptlrpc_request *req =
1545 cfs_list_entry(tmp, struct ptlrpc_request,
1547 struct obd_import *imp = req->rq_import;
1548 int unregistered = 0;
1551 if (req->rq_phase == RQ_PHASE_NEW &&
1552 ptlrpc_send_new_req(req)) {
1553 force_timer_recalc = 1;
1556 /* delayed send - skip */
1557 if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
1560 /* delayed resend - skip */
1561 if (req->rq_phase == RQ_PHASE_RPC && req->rq_resend &&
1562 req->rq_sent > cfs_time_current_sec())
1565 if (!(req->rq_phase == RQ_PHASE_RPC ||
1566 req->rq_phase == RQ_PHASE_BULK ||
1567 req->rq_phase == RQ_PHASE_INTERPRET ||
1568 req->rq_phase == RQ_PHASE_UNREGISTERING ||
1569 req->rq_phase == RQ_PHASE_COMPLETE)) {
1570 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
1574 if (req->rq_phase == RQ_PHASE_UNREGISTERING) {
1575 LASSERT(req->rq_next_phase != req->rq_phase);
1576 LASSERT(req->rq_next_phase != RQ_PHASE_UNDEFINED);
1579 * Skip processing until reply is unlinked. We
1580 * can't return to pool before that and we can't
1581 * call interpret before that. We need to make
1582 * sure that all rdma transfers finished and will
1583 * not corrupt any data.
1585 if (ptlrpc_client_recv_or_unlink(req) ||
1586 ptlrpc_client_bulk_active(req))
1590 * Turn fail_loc off to prevent it from looping
1593 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
1594 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK,
1597 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK)) {
1598 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK,
1603 * Move to next phase if reply was successfully
1606 ptlrpc_rqphase_move(req, req->rq_next_phase);
1609 if (req->rq_phase == RQ_PHASE_COMPLETE)
1612 if (req->rq_phase == RQ_PHASE_INTERPRET)
1613 GOTO(interpret, req->rq_status);
1616 * Note that this also will start async reply unlink.
1618 if (req->rq_net_err && !req->rq_timedout) {
1619 ptlrpc_expire_one_request(req, 1);
1622 * Check if we still need to wait for unlink.
1624 if (ptlrpc_client_recv_or_unlink(req) ||
1625 ptlrpc_client_bulk_active(req))
1627 /* If there is no need to resend, fail it now. */
1628 if (req->rq_no_resend) {
1629 if (req->rq_status == 0)
1630 req->rq_status = -EIO;
1631 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1632 GOTO(interpret, req->rq_status);
1639 cfs_spin_lock(&req->rq_lock);
1640 req->rq_replied = 0;
1641 cfs_spin_unlock(&req->rq_lock);
1642 if (req->rq_status == 0)
1643 req->rq_status = -EIO;
1644 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1645 GOTO(interpret, req->rq_status);
1648 /* ptlrpc_set_wait->l_wait_event sets lwi_allow_intr
1649 * so it sets rq_intr regardless of individual rpc
1650 * timeouts. The synchronous IO waiting path sets
1651 * rq_intr irrespective of whether ptlrpcd
1652 * has seen a timeout. Our policy is to only interpret
1653 * interrupted rpcs after they have timed out, so we
1654 * need to enforce that here.
1657 if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
1658 req->rq_wait_ctx)) {
1659 req->rq_status = -EINTR;
1660 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1661 GOTO(interpret, req->rq_status);
1664 if (req->rq_phase == RQ_PHASE_RPC) {
1665 if (req->rq_timedout || req->rq_resend ||
1666 req->rq_waiting || req->rq_wait_ctx) {
1669 if (!ptlrpc_unregister_reply(req, 1))
1672 cfs_spin_lock(&imp->imp_lock);
1673 if (ptlrpc_import_delay_req(imp, req, &status)){
1674 /* put on delay list - only if we wait
1675 * recovery finished - before send */
1676 cfs_list_del_init(&req->rq_list);
1677 cfs_list_add_tail(&req->rq_list,
1680 cfs_spin_unlock(&imp->imp_lock);
1685 req->rq_status = status;
1686 ptlrpc_rqphase_move(req,
1687 RQ_PHASE_INTERPRET);
1688 cfs_spin_unlock(&imp->imp_lock);
1689 GOTO(interpret, req->rq_status);
1691 if (ptlrpc_no_resend(req) && !req->rq_wait_ctx) {
1692 req->rq_status = -ENOTCONN;
1693 ptlrpc_rqphase_move(req,
1694 RQ_PHASE_INTERPRET);
1695 cfs_spin_unlock(&imp->imp_lock);
1696 GOTO(interpret, req->rq_status);
1699 cfs_list_del_init(&req->rq_list);
1700 cfs_list_add_tail(&req->rq_list,
1701 &imp->imp_sending_list);
1703 cfs_spin_unlock(&imp->imp_lock);
1705 cfs_spin_lock(&req->rq_lock);
1706 req->rq_waiting = 0;
1707 cfs_spin_unlock(&req->rq_lock);
1709 if (req->rq_timedout || req->rq_resend) {
1710 /* This is re-sending anyways,
1711 * let's mark req as resend. */
1712 cfs_spin_lock(&req->rq_lock);
1714 cfs_spin_unlock(&req->rq_lock);
1718 if (!ptlrpc_unregister_bulk(req, 1))
1721 /* ensure previous bulk fails */
1722 old_xid = req->rq_xid;
1723 req->rq_xid = ptlrpc_next_xid();
1724 CDEBUG(D_HA, "resend bulk "
1727 old_xid, req->rq_xid);
1731 * rq_wait_ctx is only touched by ptlrpcd,
1732 * so no lock is needed here.
1734 status = sptlrpc_req_refresh_ctx(req, -1);
1737 req->rq_status = status;
1738 cfs_spin_lock(&req->rq_lock);
1739 req->rq_wait_ctx = 0;
1740 cfs_spin_unlock(&req->rq_lock);
1741 force_timer_recalc = 1;
1743 cfs_spin_lock(&req->rq_lock);
1744 req->rq_wait_ctx = 1;
1745 cfs_spin_unlock(&req->rq_lock);
1750 cfs_spin_lock(&req->rq_lock);
1751 req->rq_wait_ctx = 0;
1752 cfs_spin_unlock(&req->rq_lock);
1755 rc = ptl_send_rpc(req, 0);
1757 DEBUG_REQ(D_HA, req, "send failed (%d)",
1759 force_timer_recalc = 1;
1760 cfs_spin_lock(&req->rq_lock);
1761 req->rq_net_err = 1;
1762 cfs_spin_unlock(&req->rq_lock);
1764 /* need to reset the timeout */
1765 force_timer_recalc = 1;
1768 cfs_spin_lock(&req->rq_lock);
1770 if (ptlrpc_client_early(req)) {
1771 ptlrpc_at_recv_early_reply(req);
1772 cfs_spin_unlock(&req->rq_lock);
1776 /* Still waiting for a reply? */
1777 if (ptlrpc_client_recv(req)) {
1778 cfs_spin_unlock(&req->rq_lock);
1782 /* Did we actually receive a reply? */
1783 if (!ptlrpc_client_replied(req)) {
1784 cfs_spin_unlock(&req->rq_lock);
1788 cfs_spin_unlock(&req->rq_lock);
1790 /* unlink from net because we are going to
1791 * swab in-place of reply buffer */
1792 unregistered = ptlrpc_unregister_reply(req, 1);
1796 req->rq_status = after_reply(req);
1800 /* If there is no bulk associated with this request,
1801 * then we're done and should let the interpreter
1802 * process the reply. Similarly if the RPC returned
1803 * an error, and therefore the bulk will never arrive.
1805 if (req->rq_bulk == NULL || req->rq_status < 0) {
1806 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1807 GOTO(interpret, req->rq_status);
1810 ptlrpc_rqphase_move(req, RQ_PHASE_BULK);
1813 LASSERT(req->rq_phase == RQ_PHASE_BULK);
1814 if (ptlrpc_client_bulk_active(req))
1817 if (!req->rq_bulk->bd_success) {
1818 /* The RPC reply arrived OK, but the bulk screwed
1819 * up! Dead weird since the server told us the RPC
1820 * was good after getting the REPLY for her GET or
1821 * the ACK for her PUT. */
1822 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1823 req->rq_status = -EIO;
1826 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1829 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
1831 /* This moves to "unregistering" phase we need to wait for
1833 if (!unregistered && !ptlrpc_unregister_reply(req, 1)) {
1834 /* start async bulk unlink too */
1835 ptlrpc_unregister_bulk(req, 1);
1839 if (!ptlrpc_unregister_bulk(req, 1))
1842 /* When calling interpret receiving already should be
1844 LASSERT(!req->rq_receiving_reply);
1846 ptlrpc_req_interpret(env, req, req->rq_status);
1848 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
1850 CDEBUG(req->rq_reqmsg != NULL ? D_RPCTRACE : 0,
1851 "Completed RPC pname:cluuid:pid:xid:nid:"
1852 "opc %s:%s:%d:"LPU64":%s:%d\n",
1853 cfs_curproc_comm(), imp->imp_obd->obd_uuid.uuid,
1854 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1855 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1856 lustre_msg_get_opc(req->rq_reqmsg));
1858 cfs_spin_lock(&imp->imp_lock);
1859 /* Request already may be not on sending or delaying list. This
1860 * may happen in the case of marking it erroneous for the case
1861 * ptlrpc_import_delay_req(req, status) find it impossible to
1862 * allow sending this rpc and returns *status != 0. */
1863 if (!cfs_list_empty(&req->rq_list)) {
1864 cfs_list_del_init(&req->rq_list);
1865 cfs_atomic_dec(&imp->imp_inflight);
1867 cfs_spin_unlock(&imp->imp_lock);
1869 cfs_atomic_dec(&set->set_remaining);
1870 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
1872 if (set->set_producer) {
1873 /* produce a new request if possible */
1874 if (ptlrpc_set_producer(set) > 0)
1875 force_timer_recalc = 1;
1877 /* free the request that has just been completed
1878 * in order not to pollute set->set_requests */
1879 cfs_list_del_init(&req->rq_set_chain);
1880 cfs_spin_lock(&req->rq_lock);
1882 req->rq_invalid_rqset = 0;
1883 cfs_spin_unlock(&req->rq_lock);
1885 /* record rq_status to compute the final status later */
1886 if (req->rq_status != 0)
1887 set->set_rc = req->rq_status;
1888 ptlrpc_req_finished(req);
1892 /* If we hit an error, we want to recover promptly. */
1893 RETURN(cfs_atomic_read(&set->set_remaining) == 0 || force_timer_recalc);
1895 EXPORT_SYMBOL(ptlrpc_check_set);
1898 * Time out request \a req. is \a async_unlink is set, that means do not wait
1899 * until LNet actually confirms network buffer unlinking.
1900 * Return 1 if we should give up further retrying attempts or 0 otherwise.
1902 int ptlrpc_expire_one_request(struct ptlrpc_request *req, int async_unlink)
1904 struct obd_import *imp = req->rq_import;
1908 cfs_spin_lock(&req->rq_lock);
1909 req->rq_timedout = 1;
1910 cfs_spin_unlock(&req->rq_lock);
1912 DEBUG_REQ(req->rq_fake ? D_INFO : D_WARNING, req, "Request "
1913 " sent has %s: [sent "CFS_DURATION_T"/"
1914 "real "CFS_DURATION_T"]",
1915 req->rq_net_err ? "failed due to network error" :
1916 ((req->rq_real_sent == 0 ||
1917 cfs_time_before(req->rq_real_sent, req->rq_sent) ||
1918 cfs_time_aftereq(req->rq_real_sent, req->rq_deadline)) ?
1919 "timed out for sent delay" : "timed out for slow reply"),
1920 req->rq_sent, req->rq_real_sent);
1922 if (imp != NULL && obd_debug_peer_on_timeout)
1923 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1925 ptlrpc_unregister_reply(req, async_unlink);
1926 ptlrpc_unregister_bulk(req, async_unlink);
1928 if (obd_dump_on_timeout)
1929 libcfs_debug_dumplog();
1932 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
1939 cfs_atomic_inc(&imp->imp_timeouts);
1941 /* The DLM server doesn't want recovery run on its imports. */
1942 if (imp->imp_dlm_fake)
1945 /* If this request is for recovery or other primordial tasks,
1946 * then error it out here. */
1947 if (req->rq_ctx_init || req->rq_ctx_fini ||
1948 req->rq_send_state != LUSTRE_IMP_FULL ||
1949 imp->imp_obd->obd_no_recov) {
1950 DEBUG_REQ(D_RPCTRACE, req, "err -110, sent_state=%s (now=%s)",
1951 ptlrpc_import_state_name(req->rq_send_state),
1952 ptlrpc_import_state_name(imp->imp_state));
1953 cfs_spin_lock(&req->rq_lock);
1954 req->rq_status = -ETIMEDOUT;
1956 cfs_spin_unlock(&req->rq_lock);
1960 /* if a request can't be resent we can't wait for an answer after
1962 if (ptlrpc_no_resend(req)) {
1963 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1967 ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1973 * Time out all uncompleted requests in request set pointed by \a data
1974 * Callback used when waiting on sets with l_wait_event.
1977 int ptlrpc_expired_set(void *data)
1979 struct ptlrpc_request_set *set = data;
1981 time_t now = cfs_time_current_sec();
1984 LASSERT(set != NULL);
1987 * A timeout expired. See which reqs it applies to...
1989 cfs_list_for_each (tmp, &set->set_requests) {
1990 struct ptlrpc_request *req =
1991 cfs_list_entry(tmp, struct ptlrpc_request,
1994 /* don't expire request waiting for context */
1995 if (req->rq_wait_ctx)
1998 /* Request in-flight? */
1999 if (!((req->rq_phase == RQ_PHASE_RPC &&
2000 !req->rq_waiting && !req->rq_resend) ||
2001 (req->rq_phase == RQ_PHASE_BULK)))
2004 if (req->rq_timedout || /* already dealt with */
2005 req->rq_deadline > now) /* not expired */
2008 /* Deal with this guy. Do it asynchronously to not block
2009 * ptlrpcd thread. */
2010 ptlrpc_expire_one_request(req, 1);
2014 * When waiting for a whole set, we always break out of the
2015 * sleep so we can recalculate the timeout, or enable interrupts
2016 * if everyone's timed out.
2020 EXPORT_SYMBOL(ptlrpc_expired_set);
2023 * Sets rq_intr flag in \a req under spinlock.
2025 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
2027 cfs_spin_lock(&req->rq_lock);
2029 cfs_spin_unlock(&req->rq_lock);
2031 EXPORT_SYMBOL(ptlrpc_mark_interrupted);
2034 * Interrupts (sets interrupted flag) all uncompleted requests in
2035 * a set \a data. Callback for l_wait_event for interruptible waits.
2037 void ptlrpc_interrupted_set(void *data)
2039 struct ptlrpc_request_set *set = data;
2042 LASSERT(set != NULL);
2043 CDEBUG(D_RPCTRACE, "INTERRUPTED SET %p\n", set);
2045 cfs_list_for_each(tmp, &set->set_requests) {
2046 struct ptlrpc_request *req =
2047 cfs_list_entry(tmp, struct ptlrpc_request,
2050 if (req->rq_phase != RQ_PHASE_RPC &&
2051 req->rq_phase != RQ_PHASE_UNREGISTERING)
2054 ptlrpc_mark_interrupted(req);
2057 EXPORT_SYMBOL(ptlrpc_interrupted_set);
2060 * Get the smallest timeout in the set; this does NOT set a timeout.
2062 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
2065 time_t now = cfs_time_current_sec();
2067 struct ptlrpc_request *req;
2071 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
2073 cfs_list_for_each(tmp, &set->set_requests) {
2074 req = cfs_list_entry(tmp, struct ptlrpc_request, rq_set_chain);
2077 * Request in-flight?
2079 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
2080 (req->rq_phase == RQ_PHASE_BULK) ||
2081 (req->rq_phase == RQ_PHASE_NEW)))
2085 * Already timed out.
2087 if (req->rq_timedout)
2093 if (req->rq_wait_ctx)
2096 if (req->rq_phase == RQ_PHASE_NEW)
2097 deadline = req->rq_sent;
2098 else if (req->rq_phase == RQ_PHASE_RPC && req->rq_resend)
2099 deadline = req->rq_sent;
2101 deadline = req->rq_sent + req->rq_timeout;
2103 if (deadline <= now) /* actually expired already */
2104 timeout = 1; /* ASAP */
2105 else if (timeout == 0 || timeout > deadline - now)
2106 timeout = deadline - now;
2110 EXPORT_SYMBOL(ptlrpc_set_next_timeout);
2113 * Send all unset request from the set and then wait untill all
2114 * requests in the set complete (either get a reply, timeout, get an
2115 * error or otherwise be interrupted).
2116 * Returns 0 on success or error code otherwise.
2118 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
2121 struct ptlrpc_request *req;
2122 struct l_wait_info lwi;
2126 if (set->set_producer)
2127 (void)ptlrpc_set_producer(set);
2129 cfs_list_for_each(tmp, &set->set_requests) {
2130 req = cfs_list_entry(tmp, struct ptlrpc_request,
2132 if (req->rq_phase == RQ_PHASE_NEW)
2133 (void)ptlrpc_send_new_req(req);
2136 if (cfs_list_empty(&set->set_requests))
2140 timeout = ptlrpc_set_next_timeout(set);
2142 /* wait until all complete, interrupted, or an in-flight
2144 CDEBUG(D_RPCTRACE, "set %p going to sleep for %d seconds\n",
2147 if (timeout == 0 && !cfs_signal_pending())
2149 * No requests are in-flight (ether timed out
2150 * or delayed), so we can allow interrupts.
2151 * We still want to block for a limited time,
2152 * so we allow interrupts during the timeout.
2154 lwi = LWI_TIMEOUT_INTR_ALL(cfs_time_seconds(1),
2156 ptlrpc_interrupted_set, set);
2159 * At least one request is in flight, so no
2160 * interrupts are allowed. Wait until all
2161 * complete, or an in-flight req times out.
2163 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout? timeout : 1),
2164 ptlrpc_expired_set, set);
2166 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(NULL, set), &lwi);
2168 /* LU-769 - if we ignored the signal because it was already
2169 * pending when we started, we need to handle it now or we risk
2170 * it being ignored forever */
2171 if (rc == -ETIMEDOUT && !lwi.lwi_allow_intr &&
2172 cfs_signal_pending()) {
2173 cfs_sigset_t blocked_sigs =
2174 cfs_block_sigsinv(LUSTRE_FATAL_SIGS);
2176 /* In fact we only interrupt for the "fatal" signals
2177 * like SIGINT or SIGKILL. We still ignore less
2178 * important signals since ptlrpc set is not easily
2179 * reentrant from userspace again */
2180 if (cfs_signal_pending())
2181 ptlrpc_interrupted_set(set);
2182 cfs_restore_sigs(blocked_sigs);
2185 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
2187 /* -EINTR => all requests have been flagged rq_intr so next
2189 * -ETIMEDOUT => someone timed out. When all reqs have
2190 * timed out, signals are enabled allowing completion with
2192 * I don't really care if we go once more round the loop in
2193 * the error cases -eeb. */
2194 if (rc == 0 && cfs_atomic_read(&set->set_remaining) == 0) {
2195 cfs_list_for_each(tmp, &set->set_requests) {
2196 req = cfs_list_entry(tmp, struct ptlrpc_request,
2198 cfs_spin_lock(&req->rq_lock);
2199 req->rq_invalid_rqset = 1;
2200 cfs_spin_unlock(&req->rq_lock);
2203 } while (rc != 0 || cfs_atomic_read(&set->set_remaining) != 0);
2205 LASSERT(cfs_atomic_read(&set->set_remaining) == 0);
2207 rc = set->set_rc; /* rq_status of already freed requests if any */
2208 cfs_list_for_each(tmp, &set->set_requests) {
2209 req = cfs_list_entry(tmp, struct ptlrpc_request, rq_set_chain);
2211 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
2212 if (req->rq_status != 0)
2213 rc = req->rq_status;
2216 if (set->set_interpret != NULL) {
2217 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
2219 rc = interpreter (set, set->set_arg, rc);
2221 struct ptlrpc_set_cbdata *cbdata, *n;
2224 cfs_list_for_each_entry_safe(cbdata, n,
2225 &set->set_cblist, psc_item) {
2226 cfs_list_del_init(&cbdata->psc_item);
2227 err = cbdata->psc_interpret(set, cbdata->psc_data, rc);
2230 OBD_FREE_PTR(cbdata);
2236 EXPORT_SYMBOL(ptlrpc_set_wait);
2239 * Helper fuction for request freeing.
2240 * Called when request count reached zero and request needs to be freed.
2241 * Removes request from all sorts of sending/replay lists it might be on,
2242 * frees network buffers if any are present.
2243 * If \a locked is set, that means caller is already holding import imp_lock
2244 * and so we no longer need to reobtain it (for certain lists manipulations)
2246 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
2249 if (request == NULL) {
2254 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
2255 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
2256 LASSERTF(cfs_list_empty(&request->rq_list), "req %p\n", request);
2257 LASSERTF(cfs_list_empty(&request->rq_set_chain), "req %p\n", request);
2258 LASSERTF(cfs_list_empty(&request->rq_exp_list), "req %p\n", request);
2259 LASSERTF(!request->rq_replay, "req %p\n", request);
2261 req_capsule_fini(&request->rq_pill);
2263 /* We must take it off the imp_replay_list first. Otherwise, we'll set
2264 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
2265 if (request->rq_import != NULL) {
2267 cfs_spin_lock(&request->rq_import->imp_lock);
2268 cfs_list_del_init(&request->rq_replay_list);
2270 cfs_spin_unlock(&request->rq_import->imp_lock);
2272 LASSERTF(cfs_list_empty(&request->rq_replay_list), "req %p\n", request);
2274 if (cfs_atomic_read(&request->rq_refcount) != 0) {
2275 DEBUG_REQ(D_ERROR, request,
2276 "freeing request with nonzero refcount");
2280 if (request->rq_repbuf != NULL)
2281 sptlrpc_cli_free_repbuf(request);
2282 if (request->rq_export != NULL) {
2283 class_export_put(request->rq_export);
2284 request->rq_export = NULL;
2286 if (request->rq_import != NULL) {
2287 class_import_put(request->rq_import);
2288 request->rq_import = NULL;
2290 if (request->rq_bulk != NULL)
2291 ptlrpc_free_bulk(request->rq_bulk);
2293 if (request->rq_reqbuf != NULL || request->rq_clrbuf != NULL)
2294 sptlrpc_cli_free_reqbuf(request);
2296 if (request->rq_cli_ctx)
2297 sptlrpc_req_put_ctx(request, !locked);
2299 if (request->rq_pool)
2300 __ptlrpc_free_req_to_pool(request);
2302 OBD_FREE(request, sizeof(*request));
2306 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
2308 * Drop one request reference. Must be called with import imp_lock held.
2309 * When reference count drops to zero, reuqest is freed.
2311 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
2313 LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
2314 (void)__ptlrpc_req_finished(request, 1);
2316 EXPORT_SYMBOL(ptlrpc_req_finished_with_imp_lock);
2320 * Drops one reference count for request \a request.
2321 * \a locked set indicates that caller holds import imp_lock.
2322 * Frees the request whe reference count reaches zero.
2324 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
2327 if (request == NULL)
2330 if (request == LP_POISON ||
2331 request->rq_reqmsg == LP_POISON) {
2332 CERROR("dereferencing freed request (bug 575)\n");
2337 DEBUG_REQ(D_INFO, request, "refcount now %u",
2338 cfs_atomic_read(&request->rq_refcount) - 1);
2340 if (cfs_atomic_dec_and_test(&request->rq_refcount)) {
2341 __ptlrpc_free_req(request, locked);
2349 * Drops one reference count for a request.
2351 void ptlrpc_req_finished(struct ptlrpc_request *request)
2353 __ptlrpc_req_finished(request, 0);
2355 EXPORT_SYMBOL(ptlrpc_req_finished);
2358 * Returns xid of a \a request
2360 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
2362 return request->rq_xid;
2364 EXPORT_SYMBOL(ptlrpc_req_xid);
2367 * Disengage the client's reply buffer from the network
2368 * NB does _NOT_ unregister any client-side bulk.
2369 * IDEMPOTENT, but _not_ safe against concurrent callers.
2370 * The request owner (i.e. the thread doing the I/O) must call...
2371 * Returns 0 on success or 1 if unregistering cannot be made.
2373 int ptlrpc_unregister_reply(struct ptlrpc_request *request, int async)
2377 struct l_wait_info lwi;
2382 LASSERT(!cfs_in_interrupt());
2385 * Let's setup deadline for reply unlink.
2387 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
2388 async && request->rq_reply_deadline == 0)
2389 request->rq_reply_deadline = cfs_time_current_sec()+LONG_UNLINK;
2392 * Nothing left to do.
2394 if (!ptlrpc_client_recv_or_unlink(request))
2397 LNetMDUnlink(request->rq_reply_md_h);
2400 * Let's check it once again.
2402 if (!ptlrpc_client_recv_or_unlink(request))
2406 * Move to "Unregistering" phase as reply was not unlinked yet.
2408 ptlrpc_rqphase_move(request, RQ_PHASE_UNREGISTERING);
2411 * Do not wait for unlink to finish.
2417 * We have to l_wait_event() whatever the result, to give liblustre
2418 * a chance to run reply_in_callback(), and to make sure we've
2419 * unlinked before returning a req to the pool.
2421 if (request->rq_set != NULL)
2422 wq = &request->rq_set->set_waitq;
2424 wq = &request->rq_reply_waitq;
2427 /* Network access will complete in finite time but the HUGE
2428 * timeout lets us CWARN for visibility of sluggish NALs */
2429 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
2430 cfs_time_seconds(1), NULL, NULL);
2431 rc = l_wait_event(*wq, !ptlrpc_client_recv_or_unlink(request),
2434 ptlrpc_rqphase_move(request, request->rq_next_phase);
2438 LASSERT(rc == -ETIMEDOUT);
2439 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout "
2440 "rvcng=%d unlnk=%d", request->rq_receiving_reply,
2441 request->rq_must_unlink);
2445 EXPORT_SYMBOL(ptlrpc_unregister_reply);
2448 * Iterates through replay_list on import and prunes
2449 * all requests have transno smaller than last_committed for the
2450 * import and don't have rq_replay set.
2451 * Since requests are sorted in transno order, stops when meetign first
2452 * transno bigger than last_committed.
2453 * caller must hold imp->imp_lock
2455 void ptlrpc_free_committed(struct obd_import *imp)
2457 cfs_list_t *tmp, *saved;
2458 struct ptlrpc_request *req;
2459 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
2462 LASSERT(imp != NULL);
2464 LASSERT_SPIN_LOCKED(&imp->imp_lock);
2467 if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
2468 imp->imp_generation == imp->imp_last_generation_checked) {
2469 CDEBUG(D_INFO, "%s: skip recheck: last_committed "LPU64"\n",
2470 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
2474 CDEBUG(D_RPCTRACE, "%s: committing for last_committed "LPU64" gen %d\n",
2475 imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
2476 imp->imp_generation);
2477 imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
2478 imp->imp_last_generation_checked = imp->imp_generation;
2480 cfs_list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
2481 req = cfs_list_entry(tmp, struct ptlrpc_request,
2484 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
2485 LASSERT(req != last_req);
2488 if (req->rq_transno == 0) {
2489 DEBUG_REQ(D_EMERG, req, "zero transno during replay");
2492 if (req->rq_import_generation < imp->imp_generation) {
2493 DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
2497 if (req->rq_replay) {
2498 DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
2502 /* not yet committed */
2503 if (req->rq_transno > imp->imp_peer_committed_transno) {
2504 DEBUG_REQ(D_RPCTRACE, req, "stopping search");
2508 DEBUG_REQ(D_INFO, req, "commit (last_committed "LPU64")",
2509 imp->imp_peer_committed_transno);
2511 cfs_spin_lock(&req->rq_lock);
2513 cfs_spin_unlock(&req->rq_lock);
2514 if (req->rq_commit_cb != NULL)
2515 req->rq_commit_cb(req);
2516 cfs_list_del_init(&req->rq_replay_list);
2517 __ptlrpc_req_finished(req, 1);
2524 void ptlrpc_cleanup_client(struct obd_import *imp)
2530 EXPORT_SYMBOL(ptlrpc_cleanup_client);
2533 * Schedule previously sent request for resend.
2534 * For bulk requests we assign new xid (to avoid problems with
2535 * lost replies and therefore several transfers landing into same buffer
2536 * from different sending attempts).
2538 void ptlrpc_resend_req(struct ptlrpc_request *req)
2540 DEBUG_REQ(D_HA, req, "going to resend");
2541 lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
2542 req->rq_status = -EAGAIN;
2544 cfs_spin_lock(&req->rq_lock);
2546 req->rq_net_err = 0;
2547 req->rq_timedout = 0;
2549 __u64 old_xid = req->rq_xid;
2551 /* ensure previous bulk fails */
2552 req->rq_xid = ptlrpc_next_xid();
2553 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
2554 old_xid, req->rq_xid);
2556 ptlrpc_client_wake_req(req);
2557 cfs_spin_unlock(&req->rq_lock);
2559 EXPORT_SYMBOL(ptlrpc_resend_req);
2561 /* XXX: this function and rq_status are currently unused */
2562 void ptlrpc_restart_req(struct ptlrpc_request *req)
2564 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
2565 req->rq_status = -ERESTARTSYS;
2567 cfs_spin_lock(&req->rq_lock);
2568 req->rq_restart = 1;
2569 req->rq_timedout = 0;
2570 ptlrpc_client_wake_req(req);
2571 cfs_spin_unlock(&req->rq_lock);
2573 EXPORT_SYMBOL(ptlrpc_restart_req);
2576 * Grab additional reference on a request \a req
2578 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
2581 cfs_atomic_inc(&req->rq_refcount);
2584 EXPORT_SYMBOL(ptlrpc_request_addref);
2587 * Add a request to import replay_list.
2588 * Must be called under imp_lock
2590 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
2591 struct obd_import *imp)
2595 LASSERT_SPIN_LOCKED(&imp->imp_lock);
2597 if (req->rq_transno == 0) {
2598 DEBUG_REQ(D_EMERG, req, "saving request with zero transno");
2602 /* clear this for new requests that were resent as well
2603 as resent replayed requests. */
2604 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2606 /* don't re-add requests that have been replayed */
2607 if (!cfs_list_empty(&req->rq_replay_list))
2610 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
2612 LASSERT(imp->imp_replayable);
2613 /* Balanced in ptlrpc_free_committed, usually. */
2614 ptlrpc_request_addref(req);
2615 cfs_list_for_each_prev(tmp, &imp->imp_replay_list) {
2616 struct ptlrpc_request *iter =
2617 cfs_list_entry(tmp, struct ptlrpc_request,
2620 /* We may have duplicate transnos if we create and then
2621 * open a file, or for closes retained if to match creating
2622 * opens, so use req->rq_xid as a secondary key.
2623 * (See bugs 684, 685, and 428.)
2624 * XXX no longer needed, but all opens need transnos!
2626 if (iter->rq_transno > req->rq_transno)
2629 if (iter->rq_transno == req->rq_transno) {
2630 LASSERT(iter->rq_xid != req->rq_xid);
2631 if (iter->rq_xid > req->rq_xid)
2635 cfs_list_add(&req->rq_replay_list, &iter->rq_replay_list);
2639 cfs_list_add(&req->rq_replay_list, &imp->imp_replay_list);
2641 EXPORT_SYMBOL(ptlrpc_retain_replayable_request);
2644 * Send request and wait until it completes.
2645 * Returns request processing status.
2647 int ptlrpc_queue_wait(struct ptlrpc_request *req)
2649 struct ptlrpc_request_set *set;
2653 LASSERT(req->rq_set == NULL);
2654 LASSERT(!req->rq_receiving_reply);
2656 set = ptlrpc_prep_set();
2658 CERROR("Unable to allocate ptlrpc set.");
2662 /* for distributed debugging */
2663 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
2665 /* add a ref for the set (see comment in ptlrpc_set_add_req) */
2666 ptlrpc_request_addref(req);
2667 ptlrpc_set_add_req(set, req);
2668 rc = ptlrpc_set_wait(set);
2669 ptlrpc_set_destroy(set);
2673 EXPORT_SYMBOL(ptlrpc_queue_wait);
2675 struct ptlrpc_replay_async_args {
2677 int praa_old_status;
2681 * Callback used for replayed requests reply processing.
2682 * In case of succesful reply calls registeresd request replay callback.
2683 * In case of error restart replay process.
2685 static int ptlrpc_replay_interpret(const struct lu_env *env,
2686 struct ptlrpc_request *req,
2687 void * data, int rc)
2689 struct ptlrpc_replay_async_args *aa = data;
2690 struct obd_import *imp = req->rq_import;
2693 cfs_atomic_dec(&imp->imp_replay_inflight);
2695 if (!ptlrpc_client_replied(req)) {
2696 CERROR("request replay timed out, restarting recovery\n");
2697 GOTO(out, rc = -ETIMEDOUT);
2700 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
2701 (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
2702 lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
2703 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2705 /** VBR: check version failure */
2706 if (lustre_msg_get_status(req->rq_repmsg) == -EOVERFLOW) {
2707 /** replay was failed due to version mismatch */
2708 DEBUG_REQ(D_WARNING, req, "Version mismatch during replay\n");
2709 cfs_spin_lock(&imp->imp_lock);
2710 imp->imp_vbr_failed = 1;
2711 imp->imp_no_lock_replay = 1;
2712 cfs_spin_unlock(&imp->imp_lock);
2713 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2715 /** The transno had better not change over replay. */
2716 LASSERTF(lustre_msg_get_transno(req->rq_reqmsg) ==
2717 lustre_msg_get_transno(req->rq_repmsg) ||
2718 lustre_msg_get_transno(req->rq_repmsg) == 0,
2720 lustre_msg_get_transno(req->rq_reqmsg),
2721 lustre_msg_get_transno(req->rq_repmsg));
2724 cfs_spin_lock(&imp->imp_lock);
2725 /** if replays by version then gap was occur on server, no trust to locks */
2726 if (lustre_msg_get_flags(req->rq_repmsg) & MSG_VERSION_REPLAY)
2727 imp->imp_no_lock_replay = 1;
2728 imp->imp_last_replay_transno = lustre_msg_get_transno(req->rq_reqmsg);
2729 cfs_spin_unlock(&imp->imp_lock);
2730 LASSERT(imp->imp_last_replay_transno);
2732 /* transaction number shouldn't be bigger than the latest replayed */
2733 if (req->rq_transno > lustre_msg_get_transno(req->rq_reqmsg)) {
2734 DEBUG_REQ(D_ERROR, req,
2735 "Reported transno "LPU64" is bigger than the "
2736 "replayed one: "LPU64, req->rq_transno,
2737 lustre_msg_get_transno(req->rq_reqmsg));
2738 GOTO(out, rc = -EINVAL);
2741 DEBUG_REQ(D_HA, req, "got rep");
2743 /* let the callback do fixups, possibly including in the request */
2744 if (req->rq_replay_cb)
2745 req->rq_replay_cb(req);
2747 if (ptlrpc_client_replied(req) &&
2748 lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
2749 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
2750 lustre_msg_get_status(req->rq_repmsg),
2751 aa->praa_old_status);
2753 /* Put it back for re-replay. */
2754 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2758 * Errors while replay can set transno to 0, but
2759 * imp_last_replay_transno shouldn't be set to 0 anyway
2761 if (req->rq_transno == 0)
2762 CERROR("Transno is 0 during replay!\n");
2764 /* continue with recovery */
2765 rc = ptlrpc_import_recovery_state_machine(imp);
2767 req->rq_send_state = aa->praa_old_state;
2770 /* this replay failed, so restart recovery */
2771 ptlrpc_connect_import(imp);
2777 * Prepares and queues request for replay.
2778 * Adds it to ptlrpcd queue for actual sending.
2779 * Returns 0 on success.
2781 int ptlrpc_replay_req(struct ptlrpc_request *req)
2783 struct ptlrpc_replay_async_args *aa;
2786 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
2788 LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
2789 aa = ptlrpc_req_async_args(req);
2790 memset(aa, 0, sizeof *aa);
2792 /* Prepare request to be resent with ptlrpcd */
2793 aa->praa_old_state = req->rq_send_state;
2794 req->rq_send_state = LUSTRE_IMP_REPLAY;
2795 req->rq_phase = RQ_PHASE_NEW;
2796 req->rq_next_phase = RQ_PHASE_UNDEFINED;
2798 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
2800 req->rq_interpret_reply = ptlrpc_replay_interpret;
2801 /* Readjust the timeout for current conditions */
2802 ptlrpc_at_set_req_timeout(req);
2804 /* Tell server the net_latency, so the server can calculate how long
2805 * it should wait for next replay */
2806 lustre_msg_set_service_time(req->rq_reqmsg,
2807 ptlrpc_at_get_net_latency(req));
2808 DEBUG_REQ(D_HA, req, "REPLAY");
2810 cfs_atomic_inc(&req->rq_import->imp_replay_inflight);
2811 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
2813 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2816 EXPORT_SYMBOL(ptlrpc_replay_req);
2819 * Aborts all in-flight request on import \a imp sending and delayed lists
2821 void ptlrpc_abort_inflight(struct obd_import *imp)
2823 cfs_list_t *tmp, *n;
2826 /* Make sure that no new requests get processed for this import.
2827 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
2828 * this flag and then putting requests on sending_list or delayed_list.
2830 cfs_spin_lock(&imp->imp_lock);
2832 /* XXX locking? Maybe we should remove each request with the list
2833 * locked? Also, how do we know if the requests on the list are
2834 * being freed at this time?
2836 cfs_list_for_each_safe(tmp, n, &imp->imp_sending_list) {
2837 struct ptlrpc_request *req =
2838 cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
2840 DEBUG_REQ(D_RPCTRACE, req, "inflight");
2842 cfs_spin_lock (&req->rq_lock);
2843 if (req->rq_import_generation < imp->imp_generation) {
2845 req->rq_status = -EIO;
2846 ptlrpc_client_wake_req(req);
2848 cfs_spin_unlock (&req->rq_lock);
2851 cfs_list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
2852 struct ptlrpc_request *req =
2853 cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
2855 DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
2857 cfs_spin_lock (&req->rq_lock);
2858 if (req->rq_import_generation < imp->imp_generation) {
2860 req->rq_status = -EIO;
2861 ptlrpc_client_wake_req(req);
2863 cfs_spin_unlock (&req->rq_lock);
2866 /* Last chance to free reqs left on the replay list, but we
2867 * will still leak reqs that haven't committed. */
2868 if (imp->imp_replayable)
2869 ptlrpc_free_committed(imp);
2871 cfs_spin_unlock(&imp->imp_lock);
2875 EXPORT_SYMBOL(ptlrpc_abort_inflight);
2878 * Abort all uncompleted requests in request set \a set
2880 void ptlrpc_abort_set(struct ptlrpc_request_set *set)
2882 cfs_list_t *tmp, *pos;
2884 LASSERT(set != NULL);
2886 cfs_list_for_each_safe(pos, tmp, &set->set_requests) {
2887 struct ptlrpc_request *req =
2888 cfs_list_entry(pos, struct ptlrpc_request,
2891 cfs_spin_lock(&req->rq_lock);
2892 if (req->rq_phase != RQ_PHASE_RPC) {
2893 cfs_spin_unlock(&req->rq_lock);
2898 req->rq_status = -EINTR;
2899 ptlrpc_client_wake_req(req);
2900 cfs_spin_unlock(&req->rq_lock);
2904 static __u64 ptlrpc_last_xid;
2905 static cfs_spinlock_t ptlrpc_last_xid_lock;
2908 * Initialize the XID for the node. This is common among all requests on
2909 * this node, and only requires the property that it is monotonically
2910 * increasing. It does not need to be sequential. Since this is also used
2911 * as the RDMA match bits, it is important that a single client NOT have
2912 * the same match bits for two different in-flight requests, hence we do
2913 * NOT want to have an XID per target or similar.
2915 * To avoid an unlikely collision between match bits after a client reboot
2916 * (which would deliver old data into the wrong RDMA buffer) initialize
2917 * the XID based on the current time, assuming a maximum RPC rate of 1M RPC/s.
2918 * If the time is clearly incorrect, we instead use a 62-bit random number.
2919 * In the worst case the random number will overflow 1M RPCs per second in
2920 * 9133 years, or permutations thereof.
2922 #define YEAR_2004 (1ULL << 30)
2923 void ptlrpc_init_xid(void)
2925 time_t now = cfs_time_current_sec();
2927 cfs_spin_lock_init(&ptlrpc_last_xid_lock);
2928 if (now < YEAR_2004) {
2929 cfs_get_random_bytes(&ptlrpc_last_xid, sizeof(ptlrpc_last_xid));
2930 ptlrpc_last_xid >>= 2;
2931 ptlrpc_last_xid |= (1ULL << 61);
2933 ptlrpc_last_xid = (__u64)now << 20;
2938 * Increase xid and returns resultng new value to the caller.
2940 __u64 ptlrpc_next_xid(void)
2943 cfs_spin_lock(&ptlrpc_last_xid_lock);
2944 tmp = ++ptlrpc_last_xid;
2945 cfs_spin_unlock(&ptlrpc_last_xid_lock);
2948 EXPORT_SYMBOL(ptlrpc_next_xid);
2951 * Get a glimpse at what next xid value might have been.
2952 * Returns possible next xid.
2954 __u64 ptlrpc_sample_next_xid(void)
2956 #if BITS_PER_LONG == 32
2957 /* need to avoid possible word tearing on 32-bit systems */
2959 cfs_spin_lock(&ptlrpc_last_xid_lock);
2960 tmp = ptlrpc_last_xid + 1;
2961 cfs_spin_unlock(&ptlrpc_last_xid_lock);
2964 /* No need to lock, since returned value is racy anyways */
2965 return ptlrpc_last_xid + 1;
2968 EXPORT_SYMBOL(ptlrpc_sample_next_xid);
2971 * Functions for operating ptlrpc workers.
2973 * A ptlrpc work is a function which will be running inside ptlrpc context.
2974 * The callback shouldn't sleep otherwise it will block that ptlrpcd thread.
2976 * 1. after a work is created, it can be used many times, that is:
2977 * handler = ptlrpcd_alloc_work();
2978 * ptlrpcd_queue_work();
2980 * queue it again when necessary:
2981 * ptlrpcd_queue_work();
2982 * ptlrpcd_destroy_work();
2983 * 2. ptlrpcd_queue_work() can be called by multiple processes meanwhile, but
2984 * it will only be queued once in any time. Also as its name implies, it may
2985 * have delay before it really runs by ptlrpcd thread.
2987 struct ptlrpc_work_async_args {
2989 int (*cb)(const struct lu_env *, void *);
2993 #define PTLRPC_WORK_MAGIC 0x6655436b676f4f44ULL /* magic code */
2995 static int work_interpreter(const struct lu_env *env,
2996 struct ptlrpc_request *req, void *data, int rc)
2998 struct ptlrpc_work_async_args *arg = data;
3000 LASSERT(arg->magic == PTLRPC_WORK_MAGIC);
3001 LASSERT(arg->cb != NULL);
3003 return arg->cb(env, arg->cbdata);
3007 * Create a work for ptlrpc.
3009 void *ptlrpcd_alloc_work(struct obd_import *imp,
3010 int (*cb)(const struct lu_env *, void *), void *cbdata)
3012 struct ptlrpc_request *req = NULL;
3013 struct ptlrpc_work_async_args *args;
3019 RETURN(ERR_PTR(-EINVAL));
3021 /* copy some code from deprecated fakereq. */
3024 CERROR("ptlrpc: run out of memory!\n");
3025 RETURN(ERR_PTR(-ENOMEM));
3028 req->rq_send_state = LUSTRE_IMP_FULL;
3029 req->rq_type = PTL_RPC_MSG_REQUEST;
3030 req->rq_import = class_import_get(imp);
3031 req->rq_export = NULL;
3032 req->rq_interpret_reply = work_interpreter;
3033 /* don't want reply */
3034 req->rq_receiving_reply = 0;
3035 req->rq_must_unlink = 0;
3036 req->rq_no_delay = req->rq_no_resend = 1;
3038 cfs_spin_lock_init(&req->rq_lock);
3039 CFS_INIT_LIST_HEAD(&req->rq_list);
3040 CFS_INIT_LIST_HEAD(&req->rq_replay_list);
3041 CFS_INIT_LIST_HEAD(&req->rq_set_chain);
3042 CFS_INIT_LIST_HEAD(&req->rq_history_list);
3043 CFS_INIT_LIST_HEAD(&req->rq_exp_list);
3044 cfs_waitq_init(&req->rq_reply_waitq);
3045 cfs_waitq_init(&req->rq_set_waitq);
3046 cfs_atomic_set(&req->rq_refcount, 1);
3048 CLASSERT (sizeof(*args) <= sizeof(req->rq_async_args));
3049 args = ptlrpc_req_async_args(req);
3050 args->magic = PTLRPC_WORK_MAGIC;
3052 args->cbdata = cbdata;
3056 EXPORT_SYMBOL(ptlrpcd_alloc_work);
3058 void ptlrpcd_destroy_work(void *handler)
3060 struct ptlrpc_request *req = handler;
3063 ptlrpc_req_finished(req);
3065 EXPORT_SYMBOL(ptlrpcd_destroy_work);
3067 int ptlrpcd_queue_work(void *handler)
3069 struct ptlrpc_request *req = handler;
3072 * Check if the req is already being queued.
3074 * Here comes a trick: it lacks a way of checking if a req is being
3075 * processed reliably in ptlrpc. Here I have to use refcount of req
3076 * for this purpose. This is okay because the caller should use this
3077 * req as opaque data. - Jinshan
3079 LASSERT(cfs_atomic_read(&req->rq_refcount) > 0);
3080 if (cfs_atomic_read(&req->rq_refcount) > 1)
3083 if (cfs_atomic_inc_return(&req->rq_refcount) > 2) { /* race */
3084 cfs_atomic_dec(&req->rq_refcount);
3088 /* re-initialize the req */
3089 req->rq_timeout = obd_timeout;
3090 req->rq_sent = cfs_time_current_sec();
3091 req->rq_deadline = req->rq_sent + req->rq_timeout;
3092 req->rq_reply_deadline = req->rq_deadline;
3093 req->rq_phase = RQ_PHASE_INTERPRET;
3094 req->rq_next_phase = RQ_PHASE_COMPLETE;
3095 req->rq_xid = ptlrpc_next_xid();
3096 req->rq_import_generation = req->rq_import->imp_generation;
3098 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
3101 EXPORT_SYMBOL(ptlrpcd_queue_work);