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, Intel Corporation.
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
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <lustre_lib.h>
44 #include <lustre_ha.h>
45 #include <lustre_import.h>
46 #include <lustre_req_layout.h>
48 #include "ptlrpc_internal.h"
50 static int ptlrpc_send_new_req(struct ptlrpc_request *req);
53 * Initialize passed in client structure \a cl.
55 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
56 struct ptlrpc_client *cl)
58 cl->cli_request_portal = req_portal;
59 cl->cli_reply_portal = rep_portal;
62 EXPORT_SYMBOL(ptlrpc_init_client);
65 * Return PortalRPC connection for remore uud \a uuid
67 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
69 struct ptlrpc_connection *c;
71 lnet_process_id_t peer;
74 /* ptlrpc_uuid_to_peer() initializes its 2nd parameter
75 * before accessing its values. */
76 /* coverity[uninit_use_in_call] */
77 err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
79 CNETERR("cannot find peer %s!\n", uuid->uuid);
83 c = ptlrpc_connection_get(peer, self, uuid);
85 memcpy(c->c_remote_uuid.uuid,
86 uuid->uuid, sizeof(c->c_remote_uuid.uuid));
89 CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
93 EXPORT_SYMBOL(ptlrpc_uuid_to_connection);
96 * Allocate and initialize new bulk descriptor on the sender.
97 * Returns pointer to the descriptor or NULL on error.
99 struct ptlrpc_bulk_desc *ptlrpc_new_bulk(unsigned npages, unsigned max_brw,
100 unsigned type, unsigned portal)
102 struct ptlrpc_bulk_desc *desc;
105 OBD_ALLOC(desc, offsetof(struct ptlrpc_bulk_desc, bd_iov[npages]));
109 spin_lock_init(&desc->bd_lock);
110 init_waitqueue_head(&desc->bd_waitq);
111 desc->bd_max_iov = npages;
112 desc->bd_iov_count = 0;
113 desc->bd_portal = portal;
114 desc->bd_type = type;
115 desc->bd_md_count = 0;
116 LASSERT(max_brw > 0);
117 desc->bd_md_max_brw = min(max_brw, PTLRPC_BULK_OPS_COUNT);
118 /* PTLRPC_BULK_OPS_COUNT is the compile-time transfer limit for this
119 * node. Negotiated ocd_brw_size will always be <= this number. */
120 for (i = 0; i < PTLRPC_BULK_OPS_COUNT; i++)
121 LNetInvalidateHandle(&desc->bd_mds[i]);
127 * Prepare bulk descriptor for specified outgoing request \a req that
128 * can fit \a npages * pages. \a type is bulk type. \a portal is where
129 * the bulk to be sent. Used on client-side.
130 * Returns pointer to newly allocatrd initialized bulk descriptor or NULL on
133 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp(struct ptlrpc_request *req,
134 unsigned npages, unsigned max_brw,
135 unsigned type, unsigned portal)
137 struct obd_import *imp = req->rq_import;
138 struct ptlrpc_bulk_desc *desc;
140 LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
141 desc = ptlrpc_new_bulk(npages, max_brw, type, portal);
145 desc->bd_import_generation = req->rq_import_generation;
146 desc->bd_import = class_import_get(imp);
149 desc->bd_cbid.cbid_fn = client_bulk_callback;
150 desc->bd_cbid.cbid_arg = desc;
152 /* This makes req own desc, and free it when she frees herself */
157 EXPORT_SYMBOL(ptlrpc_prep_bulk_imp);
160 * Add a page \a page to the bulk descriptor \a desc.
161 * Data to transfer in the page starts at offset \a pageoffset and
162 * amount of data to transfer from the page is \a len
164 void __ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
165 struct page *page, int pageoffset, int len, int pin)
167 LASSERT(desc->bd_iov_count < desc->bd_max_iov);
168 LASSERT(page != NULL);
169 LASSERT(pageoffset >= 0);
171 LASSERT(pageoffset + len <= PAGE_CACHE_SIZE);
176 page_cache_get(page);
178 ptlrpc_add_bulk_page(desc, page, pageoffset, len);
180 EXPORT_SYMBOL(__ptlrpc_prep_bulk_page);
183 * Uninitialize and free bulk descriptor \a desc.
184 * Works on bulk descriptors both from server and client side.
186 void __ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc, int unpin)
190 LASSERT(desc != NULL);
191 LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
192 LASSERT(desc->bd_md_count == 0); /* network hands off */
193 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
195 sptlrpc_enc_pool_put_pages(desc);
198 class_export_put(desc->bd_export);
200 class_import_put(desc->bd_import);
203 for (i = 0; i < desc->bd_iov_count ; i++)
204 page_cache_release(desc->bd_iov[i].kiov_page);
207 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
208 bd_iov[desc->bd_max_iov]));
210 EXPORT_SYMBOL(__ptlrpc_free_bulk);
213 * Set server timelimit for this req, i.e. how long are we willing to wait
214 * for reply before timing out this request.
216 void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req)
222 LASSERT(req->rq_import);
225 /* non-AT settings */
227 * \a imp_server_timeout means this is reverse import and
228 * we send (currently only) ASTs to the client and cannot afford
229 * to wait too long for the reply, otherwise the other client
230 * (because of which we are sending this request) would
231 * timeout waiting for us
233 req->rq_timeout = req->rq_import->imp_server_timeout ?
234 obd_timeout / 2 : obd_timeout;
236 at = &req->rq_import->imp_at;
237 idx = import_at_get_index(req->rq_import,
238 req->rq_request_portal);
239 serv_est = at_get(&at->iat_service_estimate[idx]);
240 req->rq_timeout = at_est2timeout(serv_est);
242 /* We could get even fancier here, using history to predict increased
245 /* Let the server know what this RPC timeout is by putting it in the
247 lustre_msg_set_timeout(req->rq_reqmsg, req->rq_timeout);
249 EXPORT_SYMBOL(ptlrpc_at_set_req_timeout);
251 /* Adjust max service estimate based on server value */
252 static void ptlrpc_at_adj_service(struct ptlrpc_request *req,
253 unsigned int serv_est)
259 LASSERT(req->rq_import);
260 at = &req->rq_import->imp_at;
262 idx = import_at_get_index(req->rq_import, req->rq_request_portal);
263 /* max service estimates are tracked on the server side,
264 so just keep minimal history here */
265 oldse = at_measured(&at->iat_service_estimate[idx], serv_est);
267 CDEBUG(D_ADAPTTO, "The RPC service estimate for %s ptl %d "
268 "has changed from %d to %d\n",
269 req->rq_import->imp_obd->obd_name,req->rq_request_portal,
270 oldse, at_get(&at->iat_service_estimate[idx]));
273 /* Expected network latency per remote node (secs) */
274 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req)
276 return AT_OFF ? 0 : at_get(&req->rq_import->imp_at.iat_net_latency);
279 /* Adjust expected network latency */
280 static void ptlrpc_at_adj_net_latency(struct ptlrpc_request *req,
281 unsigned int service_time)
283 unsigned int nl, oldnl;
285 time_t now = cfs_time_current_sec();
287 LASSERT(req->rq_import);
288 at = &req->rq_import->imp_at;
290 /* Network latency is total time less server processing time */
291 nl = max_t(int, now - req->rq_sent - service_time, 0) +1/*st rounding*/;
292 if (service_time > now - req->rq_sent + 3 /* bz16408 */)
293 CWARN("Reported service time %u > total measured time "
294 CFS_DURATION_T"\n", service_time,
295 cfs_time_sub(now, req->rq_sent));
297 oldnl = at_measured(&at->iat_net_latency, nl);
299 CDEBUG(D_ADAPTTO, "The network latency for %s (nid %s) "
300 "has changed from %d to %d\n",
301 req->rq_import->imp_obd->obd_name,
303 &req->rq_import->imp_connection->c_remote_uuid),
304 oldnl, at_get(&at->iat_net_latency));
307 static int unpack_reply(struct ptlrpc_request *req)
311 if (SPTLRPC_FLVR_POLICY(req->rq_flvr.sf_rpc) != SPTLRPC_POLICY_NULL) {
312 rc = ptlrpc_unpack_rep_msg(req, req->rq_replen);
314 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
319 rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
321 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
328 * Handle an early reply message, called with the rq_lock held.
329 * If anything goes wrong just ignore it - same as if it never happened
331 static int ptlrpc_at_recv_early_reply(struct ptlrpc_request *req)
333 struct ptlrpc_request *early_req;
338 spin_unlock(&req->rq_lock);
340 rc = sptlrpc_cli_unwrap_early_reply(req, &early_req);
342 spin_lock(&req->rq_lock);
346 rc = unpack_reply(early_req);
348 /* Expecting to increase the service time estimate here */
349 ptlrpc_at_adj_service(req,
350 lustre_msg_get_timeout(early_req->rq_repmsg));
351 ptlrpc_at_adj_net_latency(req,
352 lustre_msg_get_service_time(early_req->rq_repmsg));
355 sptlrpc_cli_finish_early_reply(early_req);
358 spin_lock(&req->rq_lock);
362 /* Adjust the local timeout for this req */
363 ptlrpc_at_set_req_timeout(req);
365 spin_lock(&req->rq_lock);
366 olddl = req->rq_deadline;
367 /* server assumes it now has rq_timeout from when it sent the
368 * early reply, so client should give it at least that long. */
369 req->rq_deadline = cfs_time_current_sec() + req->rq_timeout +
370 ptlrpc_at_get_net_latency(req);
372 DEBUG_REQ(D_ADAPTTO, req,
373 "Early reply #%d, new deadline in "CFS_DURATION_T"s "
374 "("CFS_DURATION_T"s)", req->rq_early_count,
375 cfs_time_sub(req->rq_deadline, cfs_time_current_sec()),
376 cfs_time_sub(req->rq_deadline, olddl));
382 * Wind down request pool \a pool.
383 * Frees all requests from the pool too
385 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
387 struct list_head *l, *tmp;
388 struct ptlrpc_request *req;
390 LASSERT(pool != NULL);
392 spin_lock(&pool->prp_lock);
393 list_for_each_safe(l, tmp, &pool->prp_req_list) {
394 req = list_entry(l, struct ptlrpc_request, rq_list);
395 list_del(&req->rq_list);
396 LASSERT(req->rq_reqbuf);
397 LASSERT(req->rq_reqbuf_len == pool->prp_rq_size);
398 OBD_FREE_LARGE(req->rq_reqbuf, pool->prp_rq_size);
399 OBD_FREE(req, sizeof(*req));
401 spin_unlock(&pool->prp_lock);
402 OBD_FREE(pool, sizeof(*pool));
404 EXPORT_SYMBOL(ptlrpc_free_rq_pool);
407 * Allocates, initializes and adds \a num_rq requests to the pool \a pool
409 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
414 while (size < pool->prp_rq_size)
417 LASSERTF(list_empty(&pool->prp_req_list) ||
418 size == pool->prp_rq_size,
419 "Trying to change pool size with nonempty pool "
420 "from %d to %d bytes\n", pool->prp_rq_size, size);
422 spin_lock(&pool->prp_lock);
423 pool->prp_rq_size = size;
424 for (i = 0; i < num_rq; i++) {
425 struct ptlrpc_request *req;
426 struct lustre_msg *msg;
428 spin_unlock(&pool->prp_lock);
429 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
432 OBD_ALLOC_LARGE(msg, size);
434 OBD_FREE(req, sizeof(struct ptlrpc_request));
437 req->rq_reqbuf = msg;
438 req->rq_reqbuf_len = size;
440 spin_lock(&pool->prp_lock);
441 list_add_tail(&req->rq_list, &pool->prp_req_list);
443 spin_unlock(&pool->prp_lock);
446 EXPORT_SYMBOL(ptlrpc_add_rqs_to_pool);
449 * Create and initialize new request pool with given attributes:
450 * \a num_rq - initial number of requests to create for the pool
451 * \a msgsize - maximum message size possible for requests in thid pool
452 * \a populate_pool - function to be called when more requests need to be added
454 * Returns pointer to newly created pool or NULL on error.
456 struct ptlrpc_request_pool *
457 ptlrpc_init_rq_pool(int num_rq, int msgsize,
458 void (*populate_pool)(struct ptlrpc_request_pool *, int))
460 struct ptlrpc_request_pool *pool;
462 OBD_ALLOC(pool, sizeof (struct ptlrpc_request_pool));
466 /* Request next power of two for the allocation, because internally
467 kernel would do exactly this */
469 spin_lock_init(&pool->prp_lock);
470 INIT_LIST_HEAD(&pool->prp_req_list);
471 pool->prp_rq_size = msgsize + SPTLRPC_MAX_PAYLOAD;
472 pool->prp_populate = populate_pool;
474 populate_pool(pool, num_rq);
476 if (list_empty(&pool->prp_req_list)) {
477 /* have not allocated a single request for the pool */
478 OBD_FREE(pool, sizeof (struct ptlrpc_request_pool));
483 EXPORT_SYMBOL(ptlrpc_init_rq_pool);
486 * Fetches one request from pool \a pool
488 static struct ptlrpc_request *
489 ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
491 struct ptlrpc_request *request;
492 struct lustre_msg *reqbuf;
497 spin_lock(&pool->prp_lock);
499 /* See if we have anything in a pool, and bail out if nothing,
500 * in writeout path, where this matters, this is safe to do, because
501 * nothing is lost in this case, and when some in-flight requests
502 * complete, this code will be called again. */
503 if (unlikely(list_empty(&pool->prp_req_list))) {
504 spin_unlock(&pool->prp_lock);
508 request = list_entry(pool->prp_req_list.next, struct ptlrpc_request,
510 list_del_init(&request->rq_list);
511 spin_unlock(&pool->prp_lock);
513 LASSERT(request->rq_reqbuf);
514 LASSERT(request->rq_pool);
516 reqbuf = request->rq_reqbuf;
517 memset(request, 0, sizeof(*request));
518 request->rq_reqbuf = reqbuf;
519 request->rq_reqbuf_len = pool->prp_rq_size;
520 request->rq_pool = pool;
526 * Returns freed \a request to pool.
528 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
530 struct ptlrpc_request_pool *pool = request->rq_pool;
532 spin_lock(&pool->prp_lock);
533 LASSERT(list_empty(&request->rq_list));
534 LASSERT(!request->rq_receiving_reply);
535 list_add_tail(&request->rq_list, &pool->prp_req_list);
536 spin_unlock(&pool->prp_lock);
539 static int __ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
540 __u32 version, int opcode,
541 int count, __u32 *lengths, char **bufs,
542 struct ptlrpc_cli_ctx *ctx)
544 struct obd_import *imp = request->rq_import;
548 request->rq_cli_ctx = sptlrpc_cli_ctx_get(ctx);
550 rc = sptlrpc_req_get_ctx(request);
555 sptlrpc_req_set_flavor(request, opcode);
557 rc = lustre_pack_request(request, imp->imp_msg_magic, count,
560 LASSERT(!request->rq_pool);
564 lustre_msg_add_version(request->rq_reqmsg, version);
565 request->rq_send_state = LUSTRE_IMP_FULL;
566 request->rq_type = PTL_RPC_MSG_REQUEST;
567 request->rq_export = NULL;
569 request->rq_req_cbid.cbid_fn = request_out_callback;
570 request->rq_req_cbid.cbid_arg = request;
572 request->rq_reply_cbid.cbid_fn = reply_in_callback;
573 request->rq_reply_cbid.cbid_arg = request;
575 request->rq_reply_deadline = 0;
576 request->rq_phase = RQ_PHASE_NEW;
577 request->rq_next_phase = RQ_PHASE_UNDEFINED;
579 request->rq_request_portal = imp->imp_client->cli_request_portal;
580 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
582 ptlrpc_at_set_req_timeout(request);
584 spin_lock_init(&request->rq_lock);
585 INIT_LIST_HEAD(&request->rq_list);
586 INIT_LIST_HEAD(&request->rq_timed_list);
587 INIT_LIST_HEAD(&request->rq_replay_list);
588 INIT_LIST_HEAD(&request->rq_ctx_chain);
589 INIT_LIST_HEAD(&request->rq_set_chain);
590 INIT_LIST_HEAD(&request->rq_history_list);
591 INIT_LIST_HEAD(&request->rq_exp_list);
592 init_waitqueue_head(&request->rq_reply_waitq);
593 init_waitqueue_head(&request->rq_set_waitq);
594 request->rq_xid = ptlrpc_next_xid();
595 atomic_set(&request->rq_refcount, 1);
597 lustre_msg_set_opc(request->rq_reqmsg, opcode);
601 sptlrpc_cli_ctx_put(request->rq_cli_ctx, 1);
603 class_import_put(imp);
607 int ptlrpc_request_bufs_pack(struct ptlrpc_request *request,
608 __u32 version, int opcode, char **bufs,
609 struct ptlrpc_cli_ctx *ctx)
613 count = req_capsule_filled_sizes(&request->rq_pill, RCL_CLIENT);
614 return __ptlrpc_request_bufs_pack(request, version, opcode, count,
615 request->rq_pill.rc_area[RCL_CLIENT],
618 EXPORT_SYMBOL(ptlrpc_request_bufs_pack);
621 * Pack request buffers for network transfer, performing necessary encryption
622 * steps if necessary.
624 int ptlrpc_request_pack(struct ptlrpc_request *request,
625 __u32 version, int opcode)
628 rc = ptlrpc_request_bufs_pack(request, version, opcode, NULL, NULL);
632 /* For some old 1.8 clients (< 1.8.7), they will LASSERT the size of
633 * ptlrpc_body sent from server equal to local ptlrpc_body size, so we
634 * have to send old ptlrpc_body to keep interoprability with these
637 * Only three kinds of server->client RPCs so far:
642 * XXX This should be removed whenever we drop the interoprability with
643 * the these old clients.
645 if (opcode == LDLM_BL_CALLBACK || opcode == LDLM_CP_CALLBACK ||
646 opcode == LDLM_GL_CALLBACK)
647 req_capsule_shrink(&request->rq_pill, &RMF_PTLRPC_BODY,
648 sizeof(struct ptlrpc_body_v2), RCL_CLIENT);
652 EXPORT_SYMBOL(ptlrpc_request_pack);
655 * Helper function to allocate new request on import \a imp
656 * and possibly using existing request from pool \a pool if provided.
657 * Returns allocated request structure with import field filled or
661 struct ptlrpc_request *__ptlrpc_request_alloc(struct obd_import *imp,
662 struct ptlrpc_request_pool *pool)
664 struct ptlrpc_request *request = NULL;
667 request = ptlrpc_prep_req_from_pool(pool);
670 OBD_ALLOC_PTR(request);
673 LASSERTF((unsigned long)imp > 0x1000, "%p", imp);
674 LASSERT(imp != LP_POISON);
675 LASSERTF((unsigned long)imp->imp_client > 0x1000, "%p",
677 LASSERT(imp->imp_client != LP_POISON);
679 request->rq_import = class_import_get(imp);
681 CERROR("request allocation out of memory\n");
688 * Helper function for creating a request.
689 * Calls __ptlrpc_request_alloc to allocate new request sturcture and inits
690 * buffer structures according to capsule template \a format.
691 * Returns allocated request structure pointer or NULL on error.
693 static struct ptlrpc_request *
694 ptlrpc_request_alloc_internal(struct obd_import *imp,
695 struct ptlrpc_request_pool * pool,
696 const struct req_format *format)
698 struct ptlrpc_request *request;
700 request = __ptlrpc_request_alloc(imp, pool);
704 req_capsule_init(&request->rq_pill, request, RCL_CLIENT);
705 req_capsule_set(&request->rq_pill, format);
710 * Allocate new request structure for import \a imp and initialize its
711 * buffer structure according to capsule template \a format.
713 struct ptlrpc_request *ptlrpc_request_alloc(struct obd_import *imp,
714 const struct req_format *format)
716 return ptlrpc_request_alloc_internal(imp, NULL, format);
718 EXPORT_SYMBOL(ptlrpc_request_alloc);
721 * Allocate new request structure for import \a imp from pool \a pool and
722 * initialize its buffer structure according to capsule template \a format.
724 struct ptlrpc_request *ptlrpc_request_alloc_pool(struct obd_import *imp,
725 struct ptlrpc_request_pool * pool,
726 const struct req_format *format)
728 return ptlrpc_request_alloc_internal(imp, pool, format);
730 EXPORT_SYMBOL(ptlrpc_request_alloc_pool);
733 * For requests not from pool, free memory of the request structure.
734 * For requests obtained from a pool earlier, return request back to pool.
736 void ptlrpc_request_free(struct ptlrpc_request *request)
738 if (request->rq_pool)
739 __ptlrpc_free_req_to_pool(request);
741 OBD_FREE_PTR(request);
743 EXPORT_SYMBOL(ptlrpc_request_free);
746 * Allocate new request for operatione \a opcode and immediatelly pack it for
748 * Only used for simple requests like OBD_PING where the only important
749 * part of the request is operation itself.
750 * Returns allocated request or NULL on error.
752 struct ptlrpc_request *ptlrpc_request_alloc_pack(struct obd_import *imp,
753 const struct req_format *format,
754 __u32 version, int opcode)
756 struct ptlrpc_request *req = ptlrpc_request_alloc(imp, format);
760 rc = ptlrpc_request_pack(req, version, opcode);
762 ptlrpc_request_free(req);
768 EXPORT_SYMBOL(ptlrpc_request_alloc_pack);
771 * Prepare request (fetched from pool \a poolif not NULL) on import \a imp
772 * for operation \a opcode. Request would contain \a count buffers.
773 * Sizes of buffers are described in array \a lengths and buffers themselves
774 * are provided by a pointer \a bufs.
775 * Returns prepared request structure pointer or NULL on error.
777 struct ptlrpc_request *
778 ptlrpc_prep_req_pool(struct obd_import *imp,
779 __u32 version, int opcode,
780 int count, __u32 *lengths, char **bufs,
781 struct ptlrpc_request_pool *pool)
783 struct ptlrpc_request *request;
786 request = __ptlrpc_request_alloc(imp, pool);
790 rc = __ptlrpc_request_bufs_pack(request, version, opcode, count,
791 lengths, bufs, NULL);
793 ptlrpc_request_free(request);
798 EXPORT_SYMBOL(ptlrpc_prep_req_pool);
801 * Same as ptlrpc_prep_req_pool, but without pool
803 struct ptlrpc_request *
804 ptlrpc_prep_req(struct obd_import *imp, __u32 version, int opcode, int count,
805 __u32 *lengths, char **bufs)
807 return ptlrpc_prep_req_pool(imp, version, opcode, count, lengths, bufs,
810 EXPORT_SYMBOL(ptlrpc_prep_req);
813 * Allocate and initialize new request set structure.
814 * Returns a pointer to the newly allocated set structure or NULL on error.
816 struct ptlrpc_request_set *ptlrpc_prep_set(void)
818 struct ptlrpc_request_set *set;
820 OBD_ALLOC(set, sizeof(*set));
823 atomic_set(&set->set_refcount, 1);
824 INIT_LIST_HEAD(&set->set_requests);
825 init_waitqueue_head(&set->set_waitq);
826 atomic_set(&set->set_new_count, 0);
827 atomic_set(&set->set_remaining, 0);
828 spin_lock_init(&set->set_new_req_lock);
829 INIT_LIST_HEAD(&set->set_new_requests);
830 INIT_LIST_HEAD(&set->set_cblist);
831 set->set_max_inflight = UINT_MAX;
832 set->set_producer = NULL;
833 set->set_producer_arg = NULL;
838 EXPORT_SYMBOL(ptlrpc_prep_set);
841 * Allocate and initialize new request set structure with flow control
842 * extension. This extension allows to control the number of requests in-flight
843 * for the whole set. A callback function to generate requests must be provided
844 * and the request set will keep the number of requests sent over the wire to
846 * Returns a pointer to the newly allocated set structure or NULL on error.
848 struct ptlrpc_request_set *ptlrpc_prep_fcset(int max, set_producer_func func,
852 struct ptlrpc_request_set *set;
854 set = ptlrpc_prep_set();
858 set->set_max_inflight = max;
859 set->set_producer = func;
860 set->set_producer_arg = arg;
864 EXPORT_SYMBOL(ptlrpc_prep_fcset);
867 * Wind down and free request set structure previously allocated with
869 * Ensures that all requests on the set have completed and removes
870 * all requests from the request list in a set.
871 * If any unsent request happen to be on the list, pretends that they got
872 * an error in flight and calls their completion handler.
874 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
876 struct list_head *tmp;
877 struct list_head *next;
881 /* Requests on the set should either all be completed, or all be new */
882 expected_phase = (atomic_read(&set->set_remaining) == 0) ?
883 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
884 list_for_each (tmp, &set->set_requests) {
885 struct ptlrpc_request *req =
886 list_entry(tmp, struct ptlrpc_request,
889 LASSERT(req->rq_phase == expected_phase);
893 LASSERTF(atomic_read(&set->set_remaining) == 0 ||
894 atomic_read(&set->set_remaining) == n, "%d / %d\n",
895 atomic_read(&set->set_remaining), n);
897 list_for_each_safe(tmp, next, &set->set_requests) {
898 struct ptlrpc_request *req =
899 list_entry(tmp, struct ptlrpc_request,
901 list_del_init(&req->rq_set_chain);
903 LASSERT(req->rq_phase == expected_phase);
905 if (req->rq_phase == RQ_PHASE_NEW) {
906 ptlrpc_req_interpret(NULL, req, -EBADR);
907 atomic_dec(&set->set_remaining);
910 spin_lock(&req->rq_lock);
912 req->rq_invalid_rqset = 0;
913 spin_unlock(&req->rq_lock);
915 ptlrpc_req_finished (req);
918 LASSERT(atomic_read(&set->set_remaining) == 0);
920 ptlrpc_reqset_put(set);
922 EXPORT_SYMBOL(ptlrpc_set_destroy);
925 * Add a callback function \a fn to the set.
926 * This function would be called when all requests on this set are completed.
927 * The function will be passed \a data argument.
929 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
930 set_interpreter_func fn, void *data)
932 struct ptlrpc_set_cbdata *cbdata;
934 OBD_ALLOC_PTR(cbdata);
938 cbdata->psc_interpret = fn;
939 cbdata->psc_data = data;
940 list_add_tail(&cbdata->psc_item, &set->set_cblist);
944 EXPORT_SYMBOL(ptlrpc_set_add_cb);
947 * Add a new request to the general purpose request set.
948 * Assumes request reference from the caller.
950 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
951 struct ptlrpc_request *req)
953 LASSERT(list_empty(&req->rq_set_chain));
955 /* The set takes over the caller's request reference */
956 list_add_tail(&req->rq_set_chain, &set->set_requests);
958 atomic_inc(&set->set_remaining);
959 req->rq_queued_time = cfs_time_current();
961 if (req->rq_reqmsg != NULL)
962 lustre_msg_set_jobid(req->rq_reqmsg, NULL);
964 if (set->set_producer != NULL)
965 /* If the request set has a producer callback, the RPC must be
966 * sent straight away */
967 ptlrpc_send_new_req(req);
969 EXPORT_SYMBOL(ptlrpc_set_add_req);
972 * Add a request to a request with dedicated server thread
973 * and wake the thread to make any necessary processing.
974 * Currently only used for ptlrpcd.
976 void ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
977 struct ptlrpc_request *req)
979 struct ptlrpc_request_set *set = pc->pc_set;
982 LASSERT(req->rq_set == NULL);
983 LASSERT(test_bit(LIOD_STOP, &pc->pc_flags) == 0);
985 spin_lock(&set->set_new_req_lock);
987 * The set takes over the caller's request reference.
990 req->rq_queued_time = cfs_time_current();
991 list_add_tail(&req->rq_set_chain, &set->set_new_requests);
992 count = atomic_inc_return(&set->set_new_count);
993 spin_unlock(&set->set_new_req_lock);
995 /* Only need to call wakeup once for the first entry. */
997 wake_up(&set->set_waitq);
999 /* XXX: It maybe unnecessary to wakeup all the partners. But to
1000 * guarantee the async RPC can be processed ASAP, we have
1001 * no other better choice. It maybe fixed in future. */
1002 for (i = 0; i < pc->pc_npartners; i++)
1003 wake_up(&pc->pc_partners[i]->pc_set->set_waitq);
1006 EXPORT_SYMBOL(ptlrpc_set_add_new_req);
1009 * Based on the current state of the import, determine if the request
1010 * can be sent, is an error, or should be delayed.
1012 * Returns true if this request should be delayed. If false, and
1013 * *status is set, then the request can not be sent and *status is the
1014 * error code. If false and status is 0, then request can be sent.
1016 * The imp->imp_lock must be held.
1018 static int ptlrpc_import_delay_req(struct obd_import *imp,
1019 struct ptlrpc_request *req, int *status)
1023 LASSERT (status != NULL);
1026 if (req->rq_ctx_init || req->rq_ctx_fini) {
1027 /* always allow ctx init/fini rpc go through */
1028 } else if (imp->imp_state == LUSTRE_IMP_NEW) {
1029 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
1031 } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
1032 /* pings may safely race with umount */
1033 DEBUG_REQ(lustre_msg_get_opc(req->rq_reqmsg) == OBD_PING ?
1034 D_HA : D_ERROR, req, "IMP_CLOSED ");
1036 } else if (ptlrpc_send_limit_expired(req)) {
1037 /* probably doesn't need to be a D_ERROR after initial testing */
1038 DEBUG_REQ(D_ERROR, req, "send limit expired ");
1040 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
1041 imp->imp_state == LUSTRE_IMP_CONNECTING) {
1042 /* allow CONNECT even if import is invalid */ ;
1043 if (atomic_read(&imp->imp_inval_count) != 0) {
1044 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1047 } else if (imp->imp_invalid || imp->imp_obd->obd_no_recov) {
1048 if (!imp->imp_deactive)
1049 DEBUG_REQ(D_NET, req, "IMP_INVALID");
1050 *status = -ESHUTDOWN; /* bz 12940 */
1051 } else if (req->rq_import_generation != imp->imp_generation) {
1052 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
1054 } else if (req->rq_send_state != imp->imp_state) {
1055 /* invalidate in progress - any requests should be drop */
1056 if (atomic_read(&imp->imp_inval_count) != 0) {
1057 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
1059 } else if (imp->imp_dlm_fake || req->rq_no_delay) {
1060 *status = -EWOULDBLOCK;
1061 } else if (req->rq_allow_replay &&
1062 (imp->imp_state == LUSTRE_IMP_REPLAY ||
1063 imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS ||
1064 imp->imp_state == LUSTRE_IMP_REPLAY_WAIT ||
1065 imp->imp_state == LUSTRE_IMP_RECOVER)) {
1066 DEBUG_REQ(D_HA, req, "allow during recovery.\n");
1076 * Decide if the eror message regarding provided request \a req
1077 * should be printed to the console or not.
1078 * Makes it's decision on request status and other properties.
1079 * Returns 1 to print error on the system console or 0 if not.
1081 static int ptlrpc_console_allow(struct ptlrpc_request *req)
1086 LASSERT(req->rq_reqmsg != NULL);
1087 opc = lustre_msg_get_opc(req->rq_reqmsg);
1089 /* Suppress particular reconnect errors which are to be expected. No
1090 * errors are suppressed for the initial connection on an import */
1091 if ((lustre_handle_is_used(&req->rq_import->imp_remote_handle)) &&
1092 (opc == OST_CONNECT || opc == MDS_CONNECT || opc == MGS_CONNECT)) {
1094 /* Suppress timed out reconnect requests */
1095 if (req->rq_timedout)
1098 /* Suppress unavailable/again reconnect requests */
1099 err = lustre_msg_get_status(req->rq_repmsg);
1100 if (err == -ENODEV || err == -EAGAIN)
1108 * Check request processing status.
1109 * Returns the status.
1111 static int ptlrpc_check_status(struct ptlrpc_request *req)
1115 err = lustre_msg_get_status(req->rq_repmsg);
1116 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
1117 struct obd_import *imp = req->rq_import;
1118 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
1119 if (ptlrpc_console_allow(req))
1120 LCONSOLE_ERROR_MSG(0x011, "%s: Communicating with %s,"
1121 " operation %s failed with %d.\n",
1122 imp->imp_obd->obd_name,
1124 imp->imp_connection->c_peer.nid),
1125 ll_opcode2str(opc), err);
1126 return err < 0 ? err : -EINVAL;
1130 DEBUG_REQ(D_INFO, req, "status is %d", err);
1131 } else if (err > 0) {
1132 /* XXX: translate this error from net to host */
1133 DEBUG_REQ(D_INFO, req, "status is %d", err);
1140 * save pre-versions of objects into request for replay.
1141 * Versions are obtained from server reply.
1144 static void ptlrpc_save_versions(struct ptlrpc_request *req)
1146 struct lustre_msg *repmsg = req->rq_repmsg;
1147 struct lustre_msg *reqmsg = req->rq_reqmsg;
1148 __u64 *versions = lustre_msg_get_versions(repmsg);
1150 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1154 lustre_msg_set_versions(reqmsg, versions);
1155 CDEBUG(D_INFO, "Client save versions ["LPX64"/"LPX64"]\n",
1156 versions[0], versions[1]);
1160 * Callback function called when client receives RPC reply for \a req.
1161 * Returns 0 on success or error code.
1162 * The return alue would be assigned to req->rq_status by the caller
1163 * as request processing status.
1164 * This function also decides if the request needs to be saved for later replay.
1166 static int after_reply(struct ptlrpc_request *req)
1168 struct obd_import *imp = req->rq_import;
1169 struct obd_device *obd = req->rq_import->imp_obd;
1171 struct timeval work_start;
1174 LASSERT(obd != NULL);
1175 /* repbuf must be unlinked */
1176 LASSERT(!req->rq_receiving_reply && !req->rq_must_unlink);
1178 if (req->rq_reply_truncate) {
1179 if (ptlrpc_no_resend(req)) {
1180 DEBUG_REQ(D_ERROR, req, "reply buffer overflow,"
1181 " expected: %d, actual size: %d",
1182 req->rq_nob_received, req->rq_repbuf_len);
1186 sptlrpc_cli_free_repbuf(req);
1187 /* Pass the required reply buffer size (include
1188 * space for early reply).
1189 * NB: no need to roundup because alloc_repbuf
1190 * will roundup it */
1191 req->rq_replen = req->rq_nob_received;
1192 req->rq_nob_received = 0;
1198 * NB Until this point, the whole of the incoming message,
1199 * including buflens, status etc is in the sender's byte order.
1201 rc = sptlrpc_cli_unwrap_reply(req);
1203 DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
1208 * Security layer unwrap might ask resend this request.
1213 rc = unpack_reply(req);
1217 /* retry indefinitely on EINPROGRESS */
1218 if (lustre_msg_get_status(req->rq_repmsg) == -EINPROGRESS &&
1219 ptlrpc_no_resend(req) == 0 && !req->rq_no_retry_einprogress) {
1220 time_t now = cfs_time_current_sec();
1222 DEBUG_REQ(D_RPCTRACE, req, "Resending request on EINPROGRESS");
1224 req->rq_nr_resend++;
1226 /* allocate new xid to avoid reply reconstruction */
1227 if (!req->rq_bulk) {
1228 /* new xid is already allocated for bulk in
1229 * ptlrpc_check_set() */
1230 req->rq_xid = ptlrpc_next_xid();
1231 DEBUG_REQ(D_RPCTRACE, req, "Allocating new xid for "
1232 "resend on EINPROGRESS");
1235 /* Readjust the timeout for current conditions */
1236 ptlrpc_at_set_req_timeout(req);
1237 /* delay resend to give a chance to the server to get ready.
1238 * The delay is increased by 1s on every resend and is capped to
1239 * the current request timeout (i.e. obd_timeout if AT is off,
1240 * or AT service time x 125% + 5s, see at_est2timeout) */
1241 if (req->rq_nr_resend > req->rq_timeout)
1242 req->rq_sent = now + req->rq_timeout;
1244 req->rq_sent = now + req->rq_nr_resend;
1249 do_gettimeofday(&work_start);
1250 timediff = cfs_timeval_sub(&work_start, &req->rq_arrival_time, NULL);
1251 if (obd->obd_svc_stats != NULL) {
1252 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR,
1254 ptlrpc_lprocfs_rpc_sent(req, timediff);
1257 if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
1258 lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
1259 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
1260 lustre_msg_get_type(req->rq_repmsg));
1264 if (lustre_msg_get_opc(req->rq_reqmsg) != OBD_PING)
1265 CFS_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_PAUSE_REP, cfs_fail_val);
1266 ptlrpc_at_adj_service(req, lustre_msg_get_timeout(req->rq_repmsg));
1267 ptlrpc_at_adj_net_latency(req,
1268 lustre_msg_get_service_time(req->rq_repmsg));
1270 rc = ptlrpc_check_status(req);
1271 imp->imp_connect_error = rc;
1275 * Either we've been evicted, or the server has failed for
1276 * some reason. Try to reconnect, and if that fails, punt to
1279 if (ll_rpc_recoverable_error(rc)) {
1280 if (req->rq_send_state != LUSTRE_IMP_FULL ||
1281 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
1284 ptlrpc_request_handle_notconn(req);
1289 * Let's look if server sent slv. Do it only for RPC with
1292 ldlm_cli_update_pool(req);
1296 * Store transno in reqmsg for replay.
1298 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
1299 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
1300 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
1303 if (imp->imp_replayable) {
1304 spin_lock(&imp->imp_lock);
1306 * No point in adding already-committed requests to the replay
1307 * list, we will just remove them immediately. b=9829
1309 if (req->rq_transno != 0 &&
1311 lustre_msg_get_last_committed(req->rq_repmsg) ||
1313 /** version recovery */
1314 ptlrpc_save_versions(req);
1315 ptlrpc_retain_replayable_request(req, imp);
1316 } else if (req->rq_commit_cb != NULL) {
1317 spin_unlock(&imp->imp_lock);
1318 req->rq_commit_cb(req);
1319 spin_lock(&imp->imp_lock);
1323 * Replay-enabled imports return commit-status information.
1325 if (lustre_msg_get_last_committed(req->rq_repmsg)) {
1326 imp->imp_peer_committed_transno =
1327 lustre_msg_get_last_committed(req->rq_repmsg);
1330 ptlrpc_free_committed(imp);
1332 if (!list_empty(&imp->imp_replay_list)) {
1333 struct ptlrpc_request *last;
1335 last = list_entry(imp->imp_replay_list.prev,
1336 struct ptlrpc_request,
1339 * Requests with rq_replay stay on the list even if no
1340 * commit is expected.
1342 if (last->rq_transno > imp->imp_peer_committed_transno)
1343 ptlrpc_pinger_commit_expected(imp);
1346 spin_unlock(&imp->imp_lock);
1353 * Helper function to send request \a req over the network for the first time
1354 * Also adjusts request phase.
1355 * Returns 0 on success or error code.
1357 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
1359 struct obd_import *imp = req->rq_import;
1362 LASSERT(req->rq_phase == RQ_PHASE_NEW);
1363 if (req->rq_sent && (req->rq_sent > cfs_time_current_sec()) &&
1364 (!req->rq_generation_set ||
1365 req->rq_import_generation == imp->imp_generation))
1368 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
1370 spin_lock(&imp->imp_lock);
1372 if (!req->rq_generation_set)
1373 req->rq_import_generation = imp->imp_generation;
1375 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1376 spin_lock(&req->rq_lock);
1377 req->rq_waiting = 1;
1378 spin_unlock(&req->rq_lock);
1380 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
1381 "(%s != %s)", lustre_msg_get_status(req->rq_reqmsg),
1382 ptlrpc_import_state_name(req->rq_send_state),
1383 ptlrpc_import_state_name(imp->imp_state));
1384 LASSERT(list_empty(&req->rq_list));
1385 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1386 atomic_inc(&req->rq_import->imp_inflight);
1387 spin_unlock(&imp->imp_lock);
1392 spin_unlock(&imp->imp_lock);
1393 req->rq_status = rc;
1394 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1398 LASSERT(list_empty(&req->rq_list));
1399 list_add_tail(&req->rq_list, &imp->imp_sending_list);
1400 atomic_inc(&req->rq_import->imp_inflight);
1401 spin_unlock(&imp->imp_lock);
1403 lustre_msg_set_status(req->rq_reqmsg, current_pid());
1405 rc = sptlrpc_req_refresh_ctx(req, -1);
1408 req->rq_status = rc;
1411 req->rq_wait_ctx = 1;
1416 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
1417 " %s:%s:%d:"LPU64":%s:%d\n", current_comm(),
1418 imp->imp_obd->obd_uuid.uuid,
1419 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1420 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1421 lustre_msg_get_opc(req->rq_reqmsg));
1423 rc = ptl_send_rpc(req, 0);
1425 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
1426 req->rq_net_err = 1;
1432 static inline int ptlrpc_set_producer(struct ptlrpc_request_set *set)
1436 LASSERT(set->set_producer != NULL);
1438 remaining = atomic_read(&set->set_remaining);
1440 /* populate the ->set_requests list with requests until we
1441 * reach the maximum number of RPCs in flight for this set */
1442 while (atomic_read(&set->set_remaining) < set->set_max_inflight) {
1443 rc = set->set_producer(set, set->set_producer_arg);
1444 if (rc == -ENOENT) {
1445 /* no more RPC to produce */
1446 set->set_producer = NULL;
1447 set->set_producer_arg = NULL;
1452 return (atomic_read(&set->set_remaining) - remaining);
1456 * this sends any unsent RPCs in \a set and returns 1 if all are sent
1457 * and no more replies are expected.
1458 * (it is possible to get less replies than requests sent e.g. due to timed out
1459 * requests or requests that we had trouble to send out)
1461 int ptlrpc_check_set(const struct lu_env *env, struct ptlrpc_request_set *set)
1463 struct list_head *tmp, *next;
1464 int force_timer_recalc = 0;
1466 if (atomic_read(&set->set_remaining) == 0)
1469 list_for_each_safe(tmp, next, &set->set_requests) {
1470 struct ptlrpc_request *req =
1471 list_entry(tmp, struct ptlrpc_request,
1473 struct obd_import *imp = req->rq_import;
1474 int unregistered = 0;
1477 if (req->rq_phase == RQ_PHASE_NEW &&
1478 ptlrpc_send_new_req(req)) {
1479 force_timer_recalc = 1;
1482 /* delayed send - skip */
1483 if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
1486 /* delayed resend - skip */
1487 if (req->rq_phase == RQ_PHASE_RPC && req->rq_resend &&
1488 req->rq_sent > cfs_time_current_sec())
1491 if (!(req->rq_phase == RQ_PHASE_RPC ||
1492 req->rq_phase == RQ_PHASE_BULK ||
1493 req->rq_phase == RQ_PHASE_INTERPRET ||
1494 req->rq_phase == RQ_PHASE_UNREGISTERING ||
1495 req->rq_phase == RQ_PHASE_COMPLETE)) {
1496 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
1500 if (req->rq_phase == RQ_PHASE_UNREGISTERING) {
1501 LASSERT(req->rq_next_phase != req->rq_phase);
1502 LASSERT(req->rq_next_phase != RQ_PHASE_UNDEFINED);
1505 * Skip processing until reply is unlinked. We
1506 * can't return to pool before that and we can't
1507 * call interpret before that. We need to make
1508 * sure that all rdma transfers finished and will
1509 * not corrupt any data.
1511 if (ptlrpc_client_recv_or_unlink(req) ||
1512 ptlrpc_client_bulk_active(req))
1516 * Turn fail_loc off to prevent it from looping
1519 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
1520 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK,
1523 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK)) {
1524 OBD_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK,
1529 * Move to next phase if reply was successfully
1532 ptlrpc_rqphase_move(req, req->rq_next_phase);
1535 if (req->rq_phase == RQ_PHASE_COMPLETE)
1538 if (req->rq_phase == RQ_PHASE_INTERPRET)
1539 GOTO(interpret, req->rq_status);
1542 * Note that this also will start async reply unlink.
1544 if (req->rq_net_err && !req->rq_timedout) {
1545 ptlrpc_expire_one_request(req, 1);
1548 * Check if we still need to wait for unlink.
1550 if (ptlrpc_client_recv_or_unlink(req) ||
1551 ptlrpc_client_bulk_active(req))
1553 /* If there is no need to resend, fail it now. */
1554 if (req->rq_no_resend) {
1555 if (req->rq_status == 0)
1556 req->rq_status = -EIO;
1557 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1558 GOTO(interpret, req->rq_status);
1565 spin_lock(&req->rq_lock);
1566 req->rq_replied = 0;
1567 spin_unlock(&req->rq_lock);
1568 if (req->rq_status == 0)
1569 req->rq_status = -EIO;
1570 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1571 GOTO(interpret, req->rq_status);
1574 /* ptlrpc_set_wait->l_wait_event sets lwi_allow_intr
1575 * so it sets rq_intr regardless of individual rpc
1576 * timeouts. The synchronous IO waiting path sets
1577 * rq_intr irrespective of whether ptlrpcd
1578 * has seen a timeout. Our policy is to only interpret
1579 * interrupted rpcs after they have timed out, so we
1580 * need to enforce that here.
1583 if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
1584 req->rq_wait_ctx)) {
1585 req->rq_status = -EINTR;
1586 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1587 GOTO(interpret, req->rq_status);
1590 if (req->rq_phase == RQ_PHASE_RPC) {
1591 if (req->rq_timedout || req->rq_resend ||
1592 req->rq_waiting || req->rq_wait_ctx) {
1595 if (!ptlrpc_unregister_reply(req, 1))
1598 spin_lock(&imp->imp_lock);
1599 if (ptlrpc_import_delay_req(imp, req, &status)){
1600 /* put on delay list - only if we wait
1601 * recovery finished - before send */
1602 list_del_init(&req->rq_list);
1603 list_add_tail(&req->rq_list,
1606 spin_unlock(&imp->imp_lock);
1611 req->rq_status = status;
1612 ptlrpc_rqphase_move(req,
1613 RQ_PHASE_INTERPRET);
1614 spin_unlock(&imp->imp_lock);
1615 GOTO(interpret, req->rq_status);
1617 if (ptlrpc_no_resend(req) &&
1618 !req->rq_wait_ctx) {
1619 req->rq_status = -ENOTCONN;
1620 ptlrpc_rqphase_move(req,
1621 RQ_PHASE_INTERPRET);
1622 spin_unlock(&imp->imp_lock);
1623 GOTO(interpret, req->rq_status);
1626 list_del_init(&req->rq_list);
1627 list_add_tail(&req->rq_list,
1628 &imp->imp_sending_list);
1630 spin_unlock(&imp->imp_lock);
1632 spin_lock(&req->rq_lock);
1633 req->rq_waiting = 0;
1634 spin_unlock(&req->rq_lock);
1636 if (req->rq_timedout || req->rq_resend) {
1637 /* This is re-sending anyways,
1638 * let's mark req as resend. */
1639 spin_lock(&req->rq_lock);
1641 spin_unlock(&req->rq_lock);
1645 if (!ptlrpc_unregister_bulk(req, 1))
1648 /* ensure previous bulk fails */
1649 old_xid = req->rq_xid;
1650 req->rq_xid = ptlrpc_next_xid();
1651 CDEBUG(D_HA, "resend bulk "
1654 old_xid, req->rq_xid);
1658 * rq_wait_ctx is only touched by ptlrpcd,
1659 * so no lock is needed here.
1661 status = sptlrpc_req_refresh_ctx(req, -1);
1664 req->rq_status = status;
1665 spin_lock(&req->rq_lock);
1666 req->rq_wait_ctx = 0;
1667 spin_unlock(&req->rq_lock);
1668 force_timer_recalc = 1;
1670 spin_lock(&req->rq_lock);
1671 req->rq_wait_ctx = 1;
1672 spin_unlock(&req->rq_lock);
1677 spin_lock(&req->rq_lock);
1678 req->rq_wait_ctx = 0;
1679 spin_unlock(&req->rq_lock);
1682 rc = ptl_send_rpc(req, 0);
1684 DEBUG_REQ(D_HA, req,
1685 "send failed: rc = %d", rc);
1686 force_timer_recalc = 1;
1687 spin_lock(&req->rq_lock);
1688 req->rq_net_err = 1;
1689 spin_unlock(&req->rq_lock);
1691 /* need to reset the timeout */
1692 force_timer_recalc = 1;
1695 spin_lock(&req->rq_lock);
1697 if (ptlrpc_client_early(req)) {
1698 ptlrpc_at_recv_early_reply(req);
1699 spin_unlock(&req->rq_lock);
1703 /* Still waiting for a reply? */
1704 if (ptlrpc_client_recv(req)) {
1705 spin_unlock(&req->rq_lock);
1709 /* Did we actually receive a reply? */
1710 if (!ptlrpc_client_replied(req)) {
1711 spin_unlock(&req->rq_lock);
1715 spin_unlock(&req->rq_lock);
1717 /* unlink from net because we are going to
1718 * swab in-place of reply buffer */
1719 unregistered = ptlrpc_unregister_reply(req, 1);
1723 req->rq_status = after_reply(req);
1727 /* If there is no bulk associated with this request,
1728 * then we're done and should let the interpreter
1729 * process the reply. Similarly if the RPC returned
1730 * an error, and therefore the bulk will never arrive.
1732 if (req->rq_bulk == NULL || req->rq_status < 0) {
1733 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1734 GOTO(interpret, req->rq_status);
1737 ptlrpc_rqphase_move(req, RQ_PHASE_BULK);
1740 LASSERT(req->rq_phase == RQ_PHASE_BULK);
1741 if (ptlrpc_client_bulk_active(req))
1744 if (req->rq_bulk->bd_failure) {
1745 /* The RPC reply arrived OK, but the bulk screwed
1746 * up! Dead weird since the server told us the RPC
1747 * was good after getting the REPLY for her GET or
1748 * the ACK for her PUT. */
1749 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1750 req->rq_status = -EIO;
1753 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1756 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
1758 /* This moves to "unregistering" phase we need to wait for
1760 if (!unregistered && !ptlrpc_unregister_reply(req, 1)) {
1761 /* start async bulk unlink too */
1762 ptlrpc_unregister_bulk(req, 1);
1766 if (!ptlrpc_unregister_bulk(req, 1))
1769 /* When calling interpret receiving already should be
1771 LASSERT(!req->rq_receiving_reply);
1773 ptlrpc_req_interpret(env, req, req->rq_status);
1775 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
1777 CDEBUG(req->rq_reqmsg != NULL ? D_RPCTRACE : 0,
1778 "Completed RPC pname:cluuid:pid:xid:nid:"
1779 "opc %s:%s:%d:"LPU64":%s:%d\n",
1780 current_comm(), imp->imp_obd->obd_uuid.uuid,
1781 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1782 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1783 lustre_msg_get_opc(req->rq_reqmsg));
1785 spin_lock(&imp->imp_lock);
1786 /* Request already may be not on sending or delaying list. This
1787 * may happen in the case of marking it erroneous for the case
1788 * ptlrpc_import_delay_req(req, status) find it impossible to
1789 * allow sending this rpc and returns *status != 0. */
1790 if (!list_empty(&req->rq_list)) {
1791 list_del_init(&req->rq_list);
1792 atomic_dec(&imp->imp_inflight);
1794 spin_unlock(&imp->imp_lock);
1796 atomic_dec(&set->set_remaining);
1797 wake_up_all(&imp->imp_recovery_waitq);
1799 if (set->set_producer) {
1800 /* produce a new request if possible */
1801 if (ptlrpc_set_producer(set) > 0)
1802 force_timer_recalc = 1;
1804 /* free the request that has just been completed
1805 * in order not to pollute set->set_requests */
1806 list_del_init(&req->rq_set_chain);
1807 spin_lock(&req->rq_lock);
1809 req->rq_invalid_rqset = 0;
1810 spin_unlock(&req->rq_lock);
1812 /* record rq_status to compute the final status later */
1813 if (req->rq_status != 0)
1814 set->set_rc = req->rq_status;
1815 ptlrpc_req_finished(req);
1819 /* If we hit an error, we want to recover promptly. */
1820 return atomic_read(&set->set_remaining) == 0 || force_timer_recalc;
1822 EXPORT_SYMBOL(ptlrpc_check_set);
1825 * Time out request \a req. is \a async_unlink is set, that means do not wait
1826 * until LNet actually confirms network buffer unlinking.
1827 * Return 1 if we should give up further retrying attempts or 0 otherwise.
1829 int ptlrpc_expire_one_request(struct ptlrpc_request *req, int async_unlink)
1831 struct obd_import *imp = req->rq_import;
1834 spin_lock(&req->rq_lock);
1835 req->rq_timedout = 1;
1836 spin_unlock(&req->rq_lock);
1838 DEBUG_REQ(D_WARNING, req, "Request sent has %s: [sent "CFS_DURATION_T
1839 "/real "CFS_DURATION_T"]",
1840 req->rq_net_err ? "failed due to network error" :
1841 ((req->rq_real_sent == 0 ||
1842 cfs_time_before(req->rq_real_sent, req->rq_sent) ||
1843 cfs_time_aftereq(req->rq_real_sent, req->rq_deadline)) ?
1844 "timed out for sent delay" : "timed out for slow reply"),
1845 req->rq_sent, req->rq_real_sent);
1847 if (imp != NULL && obd_debug_peer_on_timeout)
1848 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1850 ptlrpc_unregister_reply(req, async_unlink);
1851 ptlrpc_unregister_bulk(req, async_unlink);
1853 if (obd_dump_on_timeout)
1854 libcfs_debug_dumplog();
1857 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
1861 atomic_inc(&imp->imp_timeouts);
1863 /* The DLM server doesn't want recovery run on its imports. */
1864 if (imp->imp_dlm_fake)
1867 /* If this request is for recovery or other primordial tasks,
1868 * then error it out here. */
1869 if (req->rq_ctx_init || req->rq_ctx_fini ||
1870 req->rq_send_state != LUSTRE_IMP_FULL ||
1871 imp->imp_obd->obd_no_recov) {
1872 DEBUG_REQ(D_RPCTRACE, req, "err -110, sent_state=%s (now=%s)",
1873 ptlrpc_import_state_name(req->rq_send_state),
1874 ptlrpc_import_state_name(imp->imp_state));
1875 spin_lock(&req->rq_lock);
1876 req->rq_status = -ETIMEDOUT;
1878 spin_unlock(&req->rq_lock);
1882 /* if a request can't be resent we can't wait for an answer after
1884 if (ptlrpc_no_resend(req)) {
1885 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1889 ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1895 * Time out all uncompleted requests in request set pointed by \a data
1896 * Callback used when waiting on sets with l_wait_event.
1899 int ptlrpc_expired_set(void *data)
1901 struct ptlrpc_request_set *set = data;
1902 struct list_head *tmp;
1903 time_t now = cfs_time_current_sec();
1905 LASSERT(set != NULL);
1908 * A timeout expired. See which reqs it applies to...
1910 list_for_each (tmp, &set->set_requests) {
1911 struct ptlrpc_request *req =
1912 list_entry(tmp, struct ptlrpc_request,
1915 /* don't expire request waiting for context */
1916 if (req->rq_wait_ctx)
1919 /* Request in-flight? */
1920 if (!((req->rq_phase == RQ_PHASE_RPC &&
1921 !req->rq_waiting && !req->rq_resend) ||
1922 (req->rq_phase == RQ_PHASE_BULK)))
1925 if (req->rq_timedout || /* already dealt with */
1926 req->rq_deadline > now) /* not expired */
1929 /* Deal with this guy. Do it asynchronously to not block
1930 * ptlrpcd thread. */
1931 ptlrpc_expire_one_request(req, 1);
1935 * When waiting for a whole set, we always break out of the
1936 * sleep so we can recalculate the timeout, or enable interrupts
1937 * if everyone's timed out.
1941 EXPORT_SYMBOL(ptlrpc_expired_set);
1944 * Sets rq_intr flag in \a req under spinlock.
1946 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1948 spin_lock(&req->rq_lock);
1950 spin_unlock(&req->rq_lock);
1952 EXPORT_SYMBOL(ptlrpc_mark_interrupted);
1955 * Interrupts (sets interrupted flag) all uncompleted requests in
1956 * a set \a data. Callback for l_wait_event for interruptible waits.
1958 void ptlrpc_interrupted_set(void *data)
1960 struct ptlrpc_request_set *set = data;
1961 struct list_head *tmp;
1963 LASSERT(set != NULL);
1964 CDEBUG(D_RPCTRACE, "INTERRUPTED SET %p\n", set);
1966 list_for_each(tmp, &set->set_requests) {
1967 struct ptlrpc_request *req =
1968 list_entry(tmp, struct ptlrpc_request,
1971 if (req->rq_phase != RQ_PHASE_RPC &&
1972 req->rq_phase != RQ_PHASE_UNREGISTERING)
1975 ptlrpc_mark_interrupted(req);
1978 EXPORT_SYMBOL(ptlrpc_interrupted_set);
1981 * Get the smallest timeout in the set; this does NOT set a timeout.
1983 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1985 struct list_head *tmp;
1986 time_t now = cfs_time_current_sec();
1988 struct ptlrpc_request *req;
1991 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1993 list_for_each(tmp, &set->set_requests) {
1994 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1997 * Request in-flight?
1999 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
2000 (req->rq_phase == RQ_PHASE_BULK) ||
2001 (req->rq_phase == RQ_PHASE_NEW)))
2005 * Already timed out.
2007 if (req->rq_timedout)
2013 if (req->rq_wait_ctx)
2016 if (req->rq_phase == RQ_PHASE_NEW)
2017 deadline = req->rq_sent;
2018 else if (req->rq_phase == RQ_PHASE_RPC && req->rq_resend)
2019 deadline = req->rq_sent;
2021 deadline = req->rq_sent + req->rq_timeout;
2023 if (deadline <= now) /* actually expired already */
2024 timeout = 1; /* ASAP */
2025 else if (timeout == 0 || timeout > deadline - now)
2026 timeout = deadline - now;
2030 EXPORT_SYMBOL(ptlrpc_set_next_timeout);
2033 * Send all unset request from the set and then wait untill all
2034 * requests in the set complete (either get a reply, timeout, get an
2035 * error or otherwise be interrupted).
2036 * Returns 0 on success or error code otherwise.
2038 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
2040 struct list_head *tmp;
2041 struct ptlrpc_request *req;
2042 struct l_wait_info lwi;
2045 if (set->set_producer)
2046 (void)ptlrpc_set_producer(set);
2048 list_for_each(tmp, &set->set_requests) {
2049 req = list_entry(tmp, struct ptlrpc_request,
2051 if (req->rq_phase == RQ_PHASE_NEW)
2052 (void)ptlrpc_send_new_req(req);
2055 if (list_empty(&set->set_requests))
2059 timeout = ptlrpc_set_next_timeout(set);
2061 /* wait until all complete, interrupted, or an in-flight
2063 CDEBUG(D_RPCTRACE, "set %p going to sleep for %d seconds\n",
2066 if (timeout == 0 && !cfs_signal_pending())
2068 * No requests are in-flight (ether timed out
2069 * or delayed), so we can allow interrupts.
2070 * We still want to block for a limited time,
2071 * so we allow interrupts during the timeout.
2073 lwi = LWI_TIMEOUT_INTR_ALL(cfs_time_seconds(1),
2075 ptlrpc_interrupted_set, set);
2078 * At least one request is in flight, so no
2079 * interrupts are allowed. Wait until all
2080 * complete, or an in-flight req times out.
2082 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout? timeout : 1),
2083 ptlrpc_expired_set, set);
2085 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(NULL, set), &lwi);
2087 /* LU-769 - if we ignored the signal because it was already
2088 * pending when we started, we need to handle it now or we risk
2089 * it being ignored forever */
2090 if (rc == -ETIMEDOUT && !lwi.lwi_allow_intr &&
2091 cfs_signal_pending()) {
2092 sigset_t blocked_sigs =
2093 cfs_block_sigsinv(LUSTRE_FATAL_SIGS);
2095 /* In fact we only interrupt for the "fatal" signals
2096 * like SIGINT or SIGKILL. We still ignore less
2097 * important signals since ptlrpc set is not easily
2098 * reentrant from userspace again */
2099 if (cfs_signal_pending())
2100 ptlrpc_interrupted_set(set);
2101 cfs_restore_sigs(blocked_sigs);
2104 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
2106 /* -EINTR => all requests have been flagged rq_intr so next
2108 * -ETIMEDOUT => someone timed out. When all reqs have
2109 * timed out, signals are enabled allowing completion with
2111 * I don't really care if we go once more round the loop in
2112 * the error cases -eeb. */
2113 if (rc == 0 && atomic_read(&set->set_remaining) == 0) {
2114 list_for_each(tmp, &set->set_requests) {
2115 req = list_entry(tmp, struct ptlrpc_request,
2117 spin_lock(&req->rq_lock);
2118 req->rq_invalid_rqset = 1;
2119 spin_unlock(&req->rq_lock);
2122 } while (rc != 0 || atomic_read(&set->set_remaining) != 0);
2124 LASSERT(atomic_read(&set->set_remaining) == 0);
2126 rc = set->set_rc; /* rq_status of already freed requests if any */
2127 list_for_each(tmp, &set->set_requests) {
2128 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
2130 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
2131 if (req->rq_status != 0)
2132 rc = req->rq_status;
2135 if (set->set_interpret != NULL) {
2136 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
2138 rc = interpreter (set, set->set_arg, rc);
2140 struct ptlrpc_set_cbdata *cbdata, *n;
2143 list_for_each_entry_safe(cbdata, n,
2144 &set->set_cblist, psc_item) {
2145 list_del_init(&cbdata->psc_item);
2146 err = cbdata->psc_interpret(set, cbdata->psc_data, rc);
2149 OBD_FREE_PTR(cbdata);
2155 EXPORT_SYMBOL(ptlrpc_set_wait);
2158 * Helper fuction for request freeing.
2159 * Called when request count reached zero and request needs to be freed.
2160 * Removes request from all sorts of sending/replay lists it might be on,
2161 * frees network buffers if any are present.
2162 * If \a locked is set, that means caller is already holding import imp_lock
2163 * and so we no longer need to reobtain it (for certain lists manipulations)
2165 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
2167 if (request == NULL) {
2171 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
2172 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
2173 LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
2174 LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
2175 LASSERTF(list_empty(&request->rq_exp_list), "req %p\n", request);
2176 LASSERTF(!request->rq_replay, "req %p\n", request);
2178 req_capsule_fini(&request->rq_pill);
2180 /* We must take it off the imp_replay_list first. Otherwise, we'll set
2181 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
2182 if (request->rq_import != NULL) {
2184 spin_lock(&request->rq_import->imp_lock);
2185 list_del_init(&request->rq_replay_list);
2187 spin_unlock(&request->rq_import->imp_lock);
2189 LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
2191 if (atomic_read(&request->rq_refcount) != 0) {
2192 DEBUG_REQ(D_ERROR, request,
2193 "freeing request with nonzero refcount");
2197 if (request->rq_repbuf != NULL)
2198 sptlrpc_cli_free_repbuf(request);
2199 if (request->rq_export != NULL) {
2200 class_export_put(request->rq_export);
2201 request->rq_export = NULL;
2203 if (request->rq_import != NULL) {
2204 class_import_put(request->rq_import);
2205 request->rq_import = NULL;
2207 if (request->rq_bulk != NULL)
2208 ptlrpc_free_bulk_pin(request->rq_bulk);
2210 if (request->rq_reqbuf != NULL || request->rq_clrbuf != NULL)
2211 sptlrpc_cli_free_reqbuf(request);
2213 if (request->rq_cli_ctx)
2214 sptlrpc_req_put_ctx(request, !locked);
2216 if (request->rq_pool)
2217 __ptlrpc_free_req_to_pool(request);
2219 OBD_FREE(request, sizeof(*request));
2222 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
2224 * Drop one request reference. Must be called with import imp_lock held.
2225 * When reference count drops to zero, reuqest is freed.
2227 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
2229 LASSERT(spin_is_locked(&request->rq_import->imp_lock));
2230 (void)__ptlrpc_req_finished(request, 1);
2232 EXPORT_SYMBOL(ptlrpc_req_finished_with_imp_lock);
2236 * Drops one reference count for request \a request.
2237 * \a locked set indicates that caller holds import imp_lock.
2238 * Frees the request whe reference count reaches zero.
2240 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
2242 if (request == NULL)
2245 if (request == LP_POISON ||
2246 request->rq_reqmsg == LP_POISON) {
2247 CERROR("dereferencing freed request (bug 575)\n");
2252 DEBUG_REQ(D_INFO, request, "refcount now %u",
2253 atomic_read(&request->rq_refcount) - 1);
2255 if (atomic_dec_and_test(&request->rq_refcount)) {
2256 __ptlrpc_free_req(request, locked);
2264 * Drops one reference count for a request.
2266 void ptlrpc_req_finished(struct ptlrpc_request *request)
2268 __ptlrpc_req_finished(request, 0);
2270 EXPORT_SYMBOL(ptlrpc_req_finished);
2273 * Returns xid of a \a request
2275 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
2277 return request->rq_xid;
2279 EXPORT_SYMBOL(ptlrpc_req_xid);
2282 * Disengage the client's reply buffer from the network
2283 * NB does _NOT_ unregister any client-side bulk.
2284 * IDEMPOTENT, but _not_ safe against concurrent callers.
2285 * The request owner (i.e. the thread doing the I/O) must call...
2286 * Returns 0 on success or 1 if unregistering cannot be made.
2288 int ptlrpc_unregister_reply(struct ptlrpc_request *request, int async)
2291 wait_queue_head_t *wq;
2292 struct l_wait_info lwi;
2297 LASSERT(!in_interrupt());
2300 * Let's setup deadline for reply unlink.
2302 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
2303 async && request->rq_reply_deadline == 0)
2304 request->rq_reply_deadline = cfs_time_current_sec()+LONG_UNLINK;
2307 * Nothing left to do.
2309 if (!ptlrpc_client_recv_or_unlink(request))
2312 LNetMDUnlink(request->rq_reply_md_h);
2315 * Let's check it once again.
2317 if (!ptlrpc_client_recv_or_unlink(request))
2321 * Move to "Unregistering" phase as reply was not unlinked yet.
2323 ptlrpc_rqphase_move(request, RQ_PHASE_UNREGISTERING);
2326 * Do not wait for unlink to finish.
2332 * We have to l_wait_event() whatever the result, to give liblustre
2333 * a chance to run reply_in_callback(), and to make sure we've
2334 * unlinked before returning a req to the pool.
2336 if (request->rq_set != NULL)
2337 wq = &request->rq_set->set_waitq;
2339 wq = &request->rq_reply_waitq;
2342 /* Network access will complete in finite time but the HUGE
2343 * timeout lets us CWARN for visibility of sluggish NALs */
2344 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
2345 cfs_time_seconds(1), NULL, NULL);
2346 rc = l_wait_event(*wq, !ptlrpc_client_recv_or_unlink(request),
2349 ptlrpc_rqphase_move(request, request->rq_next_phase);
2353 LASSERT(rc == -ETIMEDOUT);
2354 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout "
2355 "rvcng=%d unlnk=%d", request->rq_receiving_reply,
2356 request->rq_must_unlink);
2360 EXPORT_SYMBOL(ptlrpc_unregister_reply);
2363 * Iterates through replay_list on import and prunes
2364 * all requests have transno smaller than last_committed for the
2365 * import and don't have rq_replay set.
2366 * Since requests are sorted in transno order, stops when meetign first
2367 * transno bigger than last_committed.
2368 * caller must hold imp->imp_lock
2370 void ptlrpc_free_committed(struct obd_import *imp)
2372 struct list_head *tmp, *saved;
2373 struct ptlrpc_request *req;
2374 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
2376 LASSERT(imp != NULL);
2378 LASSERT(spin_is_locked(&imp->imp_lock));
2381 if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
2382 imp->imp_generation == imp->imp_last_generation_checked) {
2383 CDEBUG(D_INFO, "%s: skip recheck: last_committed "LPU64"\n",
2384 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
2387 CDEBUG(D_RPCTRACE, "%s: committing for last_committed "LPU64" gen %d\n",
2388 imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
2389 imp->imp_generation);
2390 imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
2391 imp->imp_last_generation_checked = imp->imp_generation;
2393 list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
2394 req = list_entry(tmp, struct ptlrpc_request,
2397 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
2398 LASSERT(req != last_req);
2401 if (req->rq_transno == 0) {
2402 DEBUG_REQ(D_EMERG, req, "zero transno during replay");
2405 if (req->rq_import_generation < imp->imp_generation) {
2406 DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
2410 if (req->rq_replay) {
2411 DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
2415 /* not yet committed */
2416 if (req->rq_transno > imp->imp_peer_committed_transno) {
2417 DEBUG_REQ(D_RPCTRACE, req, "stopping search");
2421 DEBUG_REQ(D_INFO, req, "commit (last_committed "LPU64")",
2422 imp->imp_peer_committed_transno);
2424 spin_lock(&req->rq_lock);
2426 spin_unlock(&req->rq_lock);
2427 if (req->rq_commit_cb != NULL)
2428 req->rq_commit_cb(req);
2429 list_del_init(&req->rq_replay_list);
2430 __ptlrpc_req_finished(req, 1);
2434 void ptlrpc_cleanup_client(struct obd_import *imp)
2437 EXPORT_SYMBOL(ptlrpc_cleanup_client);
2440 * Schedule previously sent request for resend.
2441 * For bulk requests we assign new xid (to avoid problems with
2442 * lost replies and therefore several transfers landing into same buffer
2443 * from different sending attempts).
2445 void ptlrpc_resend_req(struct ptlrpc_request *req)
2447 DEBUG_REQ(D_HA, req, "going to resend");
2448 lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
2449 req->rq_status = -EAGAIN;
2451 spin_lock(&req->rq_lock);
2453 req->rq_net_err = 0;
2454 req->rq_timedout = 0;
2456 __u64 old_xid = req->rq_xid;
2458 /* ensure previous bulk fails */
2459 req->rq_xid = ptlrpc_next_xid();
2460 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
2461 old_xid, req->rq_xid);
2463 ptlrpc_client_wake_req(req);
2464 spin_unlock(&req->rq_lock);
2466 EXPORT_SYMBOL(ptlrpc_resend_req);
2468 /* XXX: this function and rq_status are currently unused */
2469 void ptlrpc_restart_req(struct ptlrpc_request *req)
2471 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
2472 req->rq_status = -ERESTARTSYS;
2474 spin_lock(&req->rq_lock);
2475 req->rq_restart = 1;
2476 req->rq_timedout = 0;
2477 ptlrpc_client_wake_req(req);
2478 spin_unlock(&req->rq_lock);
2480 EXPORT_SYMBOL(ptlrpc_restart_req);
2483 * Grab additional reference on a request \a req
2485 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
2487 atomic_inc(&req->rq_refcount);
2490 EXPORT_SYMBOL(ptlrpc_request_addref);
2493 * Add a request to import replay_list.
2494 * Must be called under imp_lock
2496 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
2497 struct obd_import *imp)
2499 struct list_head *tmp;
2501 LASSERT(spin_is_locked(&imp->imp_lock));
2503 if (req->rq_transno == 0) {
2504 DEBUG_REQ(D_EMERG, req, "saving request with zero transno");
2508 /* clear this for new requests that were resent as well
2509 as resent replayed requests. */
2510 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
2512 /* don't re-add requests that have been replayed */
2513 if (!list_empty(&req->rq_replay_list))
2516 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
2518 LASSERT(imp->imp_replayable);
2519 /* Balanced in ptlrpc_free_committed, usually. */
2520 ptlrpc_request_addref(req);
2521 list_for_each_prev(tmp, &imp->imp_replay_list) {
2522 struct ptlrpc_request *iter =
2523 list_entry(tmp, struct ptlrpc_request,
2526 /* We may have duplicate transnos if we create and then
2527 * open a file, or for closes retained if to match creating
2528 * opens, so use req->rq_xid as a secondary key.
2529 * (See bugs 684, 685, and 428.)
2530 * XXX no longer needed, but all opens need transnos!
2532 if (iter->rq_transno > req->rq_transno)
2535 if (iter->rq_transno == req->rq_transno) {
2536 LASSERT(iter->rq_xid != req->rq_xid);
2537 if (iter->rq_xid > req->rq_xid)
2541 list_add(&req->rq_replay_list, &iter->rq_replay_list);
2545 list_add(&req->rq_replay_list, &imp->imp_replay_list);
2547 EXPORT_SYMBOL(ptlrpc_retain_replayable_request);
2550 * Send request and wait until it completes.
2551 * Returns request processing status.
2553 int ptlrpc_queue_wait(struct ptlrpc_request *req)
2555 struct ptlrpc_request_set *set;
2558 LASSERT(req->rq_set == NULL);
2559 LASSERT(!req->rq_receiving_reply);
2561 set = ptlrpc_prep_set();
2563 CERROR("Unable to allocate ptlrpc set.");
2567 /* for distributed debugging */
2568 lustre_msg_set_status(req->rq_reqmsg, current_pid());
2570 /* add a ref for the set (see comment in ptlrpc_set_add_req) */
2571 ptlrpc_request_addref(req);
2572 ptlrpc_set_add_req(set, req);
2573 rc = ptlrpc_set_wait(set);
2574 ptlrpc_set_destroy(set);
2578 EXPORT_SYMBOL(ptlrpc_queue_wait);
2580 struct ptlrpc_replay_async_args {
2582 int praa_old_status;
2586 * Callback used for replayed requests reply processing.
2587 * In case of succesful reply calls registeresd request replay callback.
2588 * In case of error restart replay process.
2590 static int ptlrpc_replay_interpret(const struct lu_env *env,
2591 struct ptlrpc_request *req,
2592 void * data, int rc)
2594 struct ptlrpc_replay_async_args *aa = data;
2595 struct obd_import *imp = req->rq_import;
2597 atomic_dec(&imp->imp_replay_inflight);
2599 if (!ptlrpc_client_replied(req)) {
2600 CERROR("request replay timed out, restarting recovery\n");
2601 GOTO(out, rc = -ETIMEDOUT);
2604 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
2605 (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
2606 lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
2607 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2609 /** VBR: check version failure */
2610 if (lustre_msg_get_status(req->rq_repmsg) == -EOVERFLOW) {
2611 /** replay was failed due to version mismatch */
2612 DEBUG_REQ(D_WARNING, req, "Version mismatch during replay\n");
2613 spin_lock(&imp->imp_lock);
2614 imp->imp_vbr_failed = 1;
2615 imp->imp_no_lock_replay = 1;
2616 spin_unlock(&imp->imp_lock);
2617 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2619 /** The transno had better not change over replay. */
2620 LASSERTF(lustre_msg_get_transno(req->rq_reqmsg) ==
2621 lustre_msg_get_transno(req->rq_repmsg) ||
2622 lustre_msg_get_transno(req->rq_repmsg) == 0,
2624 lustre_msg_get_transno(req->rq_reqmsg),
2625 lustre_msg_get_transno(req->rq_repmsg));
2628 spin_lock(&imp->imp_lock);
2629 /** if replays by version then gap occur on server, no trust to locks */
2630 if (lustre_msg_get_flags(req->rq_repmsg) & MSG_VERSION_REPLAY)
2631 imp->imp_no_lock_replay = 1;
2632 imp->imp_last_replay_transno = lustre_msg_get_transno(req->rq_reqmsg);
2633 spin_unlock(&imp->imp_lock);
2634 LASSERT(imp->imp_last_replay_transno);
2636 /* transaction number shouldn't be bigger than the latest replayed */
2637 if (req->rq_transno > lustre_msg_get_transno(req->rq_reqmsg)) {
2638 DEBUG_REQ(D_ERROR, req,
2639 "Reported transno "LPU64" is bigger than the "
2640 "replayed one: "LPU64, req->rq_transno,
2641 lustre_msg_get_transno(req->rq_reqmsg));
2642 GOTO(out, rc = -EINVAL);
2645 DEBUG_REQ(D_HA, req, "got rep");
2647 /* let the callback do fixups, possibly including in the request */
2648 if (req->rq_replay_cb)
2649 req->rq_replay_cb(req);
2651 if (ptlrpc_client_replied(req) &&
2652 lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
2653 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
2654 lustre_msg_get_status(req->rq_repmsg),
2655 aa->praa_old_status);
2657 /* Put it back for re-replay. */
2658 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2662 * Errors while replay can set transno to 0, but
2663 * imp_last_replay_transno shouldn't be set to 0 anyway
2665 if (req->rq_transno == 0)
2666 CERROR("Transno is 0 during replay!\n");
2668 /* continue with recovery */
2669 rc = ptlrpc_import_recovery_state_machine(imp);
2671 req->rq_send_state = aa->praa_old_state;
2674 /* this replay failed, so restart recovery */
2675 ptlrpc_connect_import(imp);
2681 * Prepares and queues request for replay.
2682 * Adds it to ptlrpcd queue for actual sending.
2683 * Returns 0 on success.
2685 int ptlrpc_replay_req(struct ptlrpc_request *req)
2687 struct ptlrpc_replay_async_args *aa;
2689 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
2691 LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
2692 aa = ptlrpc_req_async_args(req);
2693 memset(aa, 0, sizeof(*aa));
2695 /* Prepare request to be resent with ptlrpcd */
2696 aa->praa_old_state = req->rq_send_state;
2697 req->rq_send_state = LUSTRE_IMP_REPLAY;
2698 req->rq_phase = RQ_PHASE_NEW;
2699 req->rq_next_phase = RQ_PHASE_UNDEFINED;
2701 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
2703 req->rq_interpret_reply = ptlrpc_replay_interpret;
2704 /* Readjust the timeout for current conditions */
2705 ptlrpc_at_set_req_timeout(req);
2707 /* Tell server the net_latency, so the server can calculate how long
2708 * it should wait for next replay */
2709 lustre_msg_set_service_time(req->rq_reqmsg,
2710 ptlrpc_at_get_net_latency(req));
2711 DEBUG_REQ(D_HA, req, "REPLAY");
2713 atomic_inc(&req->rq_import->imp_replay_inflight);
2714 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
2716 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2719 EXPORT_SYMBOL(ptlrpc_replay_req);
2722 * Aborts all in-flight request on import \a imp sending and delayed lists
2724 void ptlrpc_abort_inflight(struct obd_import *imp)
2726 struct list_head *tmp, *n;
2728 /* Make sure that no new requests get processed for this import.
2729 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
2730 * this flag and then putting requests on sending_list or delayed_list.
2732 spin_lock(&imp->imp_lock);
2734 /* XXX locking? Maybe we should remove each request with the list
2735 * locked? Also, how do we know if the requests on the list are
2736 * being freed at this time?
2738 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
2739 struct ptlrpc_request *req =
2740 list_entry(tmp, struct ptlrpc_request, rq_list);
2742 DEBUG_REQ(D_RPCTRACE, req, "inflight");
2744 spin_lock(&req->rq_lock);
2745 if (req->rq_import_generation < imp->imp_generation) {
2747 req->rq_status = -EIO;
2748 ptlrpc_client_wake_req(req);
2750 spin_unlock(&req->rq_lock);
2753 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
2754 struct ptlrpc_request *req =
2755 list_entry(tmp, struct ptlrpc_request, rq_list);
2757 DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
2759 spin_lock(&req->rq_lock);
2760 if (req->rq_import_generation < imp->imp_generation) {
2762 req->rq_status = -EIO;
2763 ptlrpc_client_wake_req(req);
2765 spin_unlock(&req->rq_lock);
2768 /* Last chance to free reqs left on the replay list, but we
2769 * will still leak reqs that haven't committed. */
2770 if (imp->imp_replayable)
2771 ptlrpc_free_committed(imp);
2773 spin_unlock(&imp->imp_lock);
2775 EXPORT_SYMBOL(ptlrpc_abort_inflight);
2778 * Abort all uncompleted requests in request set \a set
2780 void ptlrpc_abort_set(struct ptlrpc_request_set *set)
2782 struct list_head *tmp, *pos;
2784 LASSERT(set != NULL);
2786 list_for_each_safe(pos, tmp, &set->set_requests) {
2787 struct ptlrpc_request *req =
2788 list_entry(pos, struct ptlrpc_request,
2791 spin_lock(&req->rq_lock);
2792 if (req->rq_phase != RQ_PHASE_RPC) {
2793 spin_unlock(&req->rq_lock);
2798 req->rq_status = -EINTR;
2799 ptlrpc_client_wake_req(req);
2800 spin_unlock(&req->rq_lock);
2804 static __u64 ptlrpc_last_xid;
2805 static spinlock_t ptlrpc_last_xid_lock;
2808 * Initialize the XID for the node. This is common among all requests on
2809 * this node, and only requires the property that it is monotonically
2810 * increasing. It does not need to be sequential. Since this is also used
2811 * as the RDMA match bits, it is important that a single client NOT have
2812 * the same match bits for two different in-flight requests, hence we do
2813 * NOT want to have an XID per target or similar.
2815 * To avoid an unlikely collision between match bits after a client reboot
2816 * (which would deliver old data into the wrong RDMA buffer) initialize
2817 * the XID based on the current time, assuming a maximum RPC rate of 1M RPC/s.
2818 * If the time is clearly incorrect, we instead use a 62-bit random number.
2819 * In the worst case the random number will overflow 1M RPCs per second in
2820 * 9133 years, or permutations thereof.
2822 #define YEAR_2004 (1ULL << 30)
2823 void ptlrpc_init_xid(void)
2825 time_t now = cfs_time_current_sec();
2827 spin_lock_init(&ptlrpc_last_xid_lock);
2828 if (now < YEAR_2004) {
2829 cfs_get_random_bytes(&ptlrpc_last_xid, sizeof(ptlrpc_last_xid));
2830 ptlrpc_last_xid >>= 2;
2831 ptlrpc_last_xid |= (1ULL << 61);
2833 ptlrpc_last_xid = (__u64)now << 20;
2836 /* Need to always be aligned to a power-of-two for mutli-bulk BRW */
2837 CLASSERT((PTLRPC_BULK_OPS_COUNT & (PTLRPC_BULK_OPS_COUNT - 1)) == 0);
2838 ptlrpc_last_xid &= PTLRPC_BULK_OPS_MASK;
2842 * Increase xid and returns resulting new value to the caller.
2844 * Multi-bulk BRW RPCs consume multiple XIDs for each bulk transfer, starting
2845 * at the returned xid, up to xid + PTLRPC_BULK_OPS_COUNT - 1. The BRW RPC
2846 * itself uses the last bulk xid needed, so the server can determine the
2847 * the number of bulk transfers from the RPC XID and a bitmask. The starting
2848 * xid must align to a power-of-two value.
2850 * This is assumed to be true due to the initial ptlrpc_last_xid
2851 * value also being initialized to a power-of-two value. LU-1431
2853 __u64 ptlrpc_next_xid(void)
2857 spin_lock(&ptlrpc_last_xid_lock);
2858 next = ptlrpc_last_xid + PTLRPC_BULK_OPS_COUNT;
2859 ptlrpc_last_xid = next;
2860 spin_unlock(&ptlrpc_last_xid_lock);
2864 EXPORT_SYMBOL(ptlrpc_next_xid);
2867 * Get a glimpse at what next xid value might have been.
2868 * Returns possible next xid.
2870 __u64 ptlrpc_sample_next_xid(void)
2872 #if BITS_PER_LONG == 32
2873 /* need to avoid possible word tearing on 32-bit systems */
2876 spin_lock(&ptlrpc_last_xid_lock);
2877 next = ptlrpc_last_xid + PTLRPC_BULK_OPS_COUNT;
2878 spin_unlock(&ptlrpc_last_xid_lock);
2882 /* No need to lock, since returned value is racy anyways */
2883 return ptlrpc_last_xid + PTLRPC_BULK_OPS_COUNT;
2886 EXPORT_SYMBOL(ptlrpc_sample_next_xid);
2889 * Functions for operating ptlrpc workers.
2891 * A ptlrpc work is a function which will be running inside ptlrpc context.
2892 * The callback shouldn't sleep otherwise it will block that ptlrpcd thread.
2894 * 1. after a work is created, it can be used many times, that is:
2895 * handler = ptlrpcd_alloc_work();
2896 * ptlrpcd_queue_work();
2898 * queue it again when necessary:
2899 * ptlrpcd_queue_work();
2900 * ptlrpcd_destroy_work();
2901 * 2. ptlrpcd_queue_work() can be called by multiple processes meanwhile, but
2902 * it will only be queued once in any time. Also as its name implies, it may
2903 * have delay before it really runs by ptlrpcd thread.
2905 struct ptlrpc_work_async_args {
2907 int (*cb)(const struct lu_env *, void *);
2911 #define PTLRPC_WORK_MAGIC 0x6655436b676f4f44ULL /* magic code */
2913 static int work_interpreter(const struct lu_env *env,
2914 struct ptlrpc_request *req, void *data, int rc)
2916 struct ptlrpc_work_async_args *arg = data;
2918 LASSERT(arg->magic == PTLRPC_WORK_MAGIC);
2919 LASSERT(arg->cb != NULL);
2921 return arg->cb(env, arg->cbdata);
2925 * Create a work for ptlrpc.
2927 void *ptlrpcd_alloc_work(struct obd_import *imp,
2928 int (*cb)(const struct lu_env *, void *), void *cbdata)
2930 struct ptlrpc_request *req = NULL;
2931 struct ptlrpc_work_async_args *args;
2936 return ERR_PTR(-EINVAL);
2938 /* copy some code from deprecated fakereq. */
2941 CERROR("ptlrpc: run out of memory!\n");
2942 return ERR_PTR(-ENOMEM);
2945 req->rq_send_state = LUSTRE_IMP_FULL;
2946 req->rq_type = PTL_RPC_MSG_REQUEST;
2947 req->rq_import = class_import_get(imp);
2948 req->rq_export = NULL;
2949 req->rq_interpret_reply = work_interpreter;
2950 /* don't want reply */
2951 req->rq_receiving_reply = 0;
2952 req->rq_must_unlink = 0;
2953 req->rq_no_delay = req->rq_no_resend = 1;
2955 spin_lock_init(&req->rq_lock);
2956 INIT_LIST_HEAD(&req->rq_list);
2957 INIT_LIST_HEAD(&req->rq_replay_list);
2958 INIT_LIST_HEAD(&req->rq_set_chain);
2959 INIT_LIST_HEAD(&req->rq_history_list);
2960 INIT_LIST_HEAD(&req->rq_exp_list);
2961 init_waitqueue_head(&req->rq_reply_waitq);
2962 init_waitqueue_head(&req->rq_set_waitq);
2963 atomic_set(&req->rq_refcount, 1);
2965 CLASSERT (sizeof(*args) <= sizeof(req->rq_async_args));
2966 args = ptlrpc_req_async_args(req);
2967 args->magic = PTLRPC_WORK_MAGIC;
2969 args->cbdata = cbdata;
2973 EXPORT_SYMBOL(ptlrpcd_alloc_work);
2975 void ptlrpcd_destroy_work(void *handler)
2977 struct ptlrpc_request *req = handler;
2980 ptlrpc_req_finished(req);
2982 EXPORT_SYMBOL(ptlrpcd_destroy_work);
2984 int ptlrpcd_queue_work(void *handler)
2986 struct ptlrpc_request *req = handler;
2989 * Check if the req is already being queued.
2991 * Here comes a trick: it lacks a way of checking if a req is being
2992 * processed reliably in ptlrpc. Here I have to use refcount of req
2993 * for this purpose. This is okay because the caller should use this
2994 * req as opaque data. - Jinshan
2996 LASSERT(atomic_read(&req->rq_refcount) > 0);
2997 if (atomic_read(&req->rq_refcount) > 1)
3000 if (atomic_inc_return(&req->rq_refcount) > 2) { /* race */
3001 atomic_dec(&req->rq_refcount);
3005 /* re-initialize the req */
3006 req->rq_timeout = obd_timeout;
3007 req->rq_sent = cfs_time_current_sec();
3008 req->rq_deadline = req->rq_sent + req->rq_timeout;
3009 req->rq_reply_deadline = req->rq_deadline;
3010 req->rq_phase = RQ_PHASE_INTERPRET;
3011 req->rq_next_phase = RQ_PHASE_COMPLETE;
3012 req->rq_xid = ptlrpc_next_xid();
3013 req->rq_import_generation = req->rq_import->imp_generation;
3015 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
3018 EXPORT_SYMBOL(ptlrpcd_queue_work);