1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_RPC
41 #include <liblustre.h>
44 #include <obd_support.h>
45 #include <obd_class.h>
46 #include <lustre_lib.h>
47 #include <lustre_ha.h>
48 #include <lustre_import.h>
50 #include "ptlrpc_internal.h"
52 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
53 struct ptlrpc_client *cl)
55 cl->cli_request_portal = req_portal;
56 cl->cli_reply_portal = rep_portal;
60 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
62 struct ptlrpc_connection *c;
64 lnet_process_id_t peer;
67 err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
69 CERROR("cannot find peer %s!\n", uuid->uuid);
73 c = ptlrpc_connection_get(peer, self, uuid);
75 memcpy(c->c_remote_uuid.uuid,
76 uuid->uuid, sizeof(c->c_remote_uuid.uuid));
79 CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
84 static inline struct ptlrpc_bulk_desc *new_bulk(int npages, int type, int portal)
86 struct ptlrpc_bulk_desc *desc;
88 OBD_ALLOC(desc, offsetof (struct ptlrpc_bulk_desc, bd_iov[npages]));
92 spin_lock_init(&desc->bd_lock);
93 cfs_waitq_init(&desc->bd_waitq);
94 desc->bd_max_iov = npages;
95 desc->bd_iov_count = 0;
96 desc->bd_md_h = LNET_INVALID_HANDLE;
97 desc->bd_portal = portal;
103 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp(struct ptlrpc_request *req,
104 int npages, int type, int portal)
106 struct obd_import *imp = req->rq_import;
107 struct ptlrpc_bulk_desc *desc;
110 LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
111 desc = new_bulk(npages, type, portal);
115 desc->bd_import_generation = req->rq_import_generation;
116 desc->bd_import = class_import_get(imp);
119 desc->bd_cbid.cbid_fn = client_bulk_callback;
120 desc->bd_cbid.cbid_arg = desc;
122 /* This makes req own desc, and free it when she frees herself */
128 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp(struct ptlrpc_request *req,
129 int npages, int type, int portal)
131 struct obd_export *exp = req->rq_export;
132 struct ptlrpc_bulk_desc *desc;
135 LASSERT(type == BULK_PUT_SOURCE || type == BULK_GET_SINK);
137 desc = new_bulk(npages, type, portal);
141 desc->bd_export = class_export_get(exp);
144 desc->bd_cbid.cbid_fn = server_bulk_callback;
145 desc->bd_cbid.cbid_arg = desc;
147 /* NB we don't assign rq_bulk here; server-side requests are
148 * re-used, and the handler frees the bulk desc explicitly. */
153 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
154 cfs_page_t *page, int pageoffset, int len)
156 LASSERT(desc->bd_iov_count < desc->bd_max_iov);
157 LASSERT(page != NULL);
158 LASSERT(pageoffset >= 0);
160 LASSERT(pageoffset + len <= CFS_PAGE_SIZE);
165 ptlrpc_add_bulk_page(desc, page, pageoffset, len);
168 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
173 LASSERT(desc != NULL);
174 LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
175 LASSERT(!desc->bd_network_rw); /* network hands off or */
176 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
178 class_export_put(desc->bd_export);
180 class_import_put(desc->bd_import);
182 for (i = 0; i < desc->bd_iov_count ; i++)
183 cfs_page_unpin(desc->bd_iov[i].kiov_page);
185 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
186 bd_iov[desc->bd_max_iov]));
190 /* Set server timelimit for this req */
191 void ptlrpc_at_set_req_timeout(struct ptlrpc_request *req)
197 LASSERT(req->rq_import);
200 /* non-AT settings */
201 req->rq_timeout = req->rq_import->imp_server_timeout ?
202 obd_timeout / 2 : obd_timeout;
204 at = &req->rq_import->imp_at;
205 idx = import_at_get_index(req->rq_import,
206 req->rq_request_portal);
207 serv_est = at_get(&at->iat_service_estimate[idx]);
208 req->rq_timeout = at_est2timeout(serv_est);
211 /* We could get even fancier here, using history to predict increased
214 /* Let the server know what this RPC timeout is by putting it in the
216 lustre_msg_set_timeout(req->rq_reqmsg, req->rq_timeout);
219 /* Adjust max service estimate based on server value */
220 static void ptlrpc_at_adj_service(struct ptlrpc_request *req,
221 unsigned int serv_est)
227 LASSERT(req->rq_import);
228 at = &req->rq_import->imp_at;
230 idx = import_at_get_index(req->rq_import, req->rq_request_portal);
231 /* max service estimates are tracked on the server side,
232 so just keep minimal history here */
233 oldse = at_add(&at->iat_service_estimate[idx], serv_est);
235 CDEBUG(D_ADAPTTO, "The RPC service estimate for %s ptl %d "
236 "has changed from %d to %d\n",
237 req->rq_import->imp_obd->obd_name,req->rq_request_portal,
238 oldse, at_get(&at->iat_service_estimate[idx]));
241 /* Expected network latency per remote node (secs) */
242 int ptlrpc_at_get_net_latency(struct ptlrpc_request *req)
244 return AT_OFF ? 0 : at_get(&req->rq_import->imp_at.iat_net_latency);
247 /* Adjust expected network latency */
248 static void ptlrpc_at_adj_net_latency(struct ptlrpc_request *req,
249 unsigned int service_time)
251 unsigned int nl, oldnl;
253 time_t now = cfs_time_current_sec();
255 LASSERT(req->rq_import);
256 at = &req->rq_import->imp_at;
258 /* Network latency is total time less server processing time */
259 nl = max_t(int, now - req->rq_sent - service_time, 0) + 1/*st rounding*/;
260 if (service_time > now - req->rq_sent + 3 /* bz16408 */)
261 CWARN("Reported service time %u > total measured time %ld\n",
262 service_time, now - req->rq_sent);
264 oldnl = at_add(&at->iat_net_latency, nl);
266 CDEBUG(D_ADAPTTO, "The network latency for %s (nid %s) "
267 "has changed from %d to %d\n",
268 req->rq_import->imp_obd->obd_name,
270 &req->rq_import->imp_connection->c_remote_uuid),
271 oldnl, at_get(&at->iat_net_latency));
274 static int unpack_reply_common(struct ptlrpc_request *req)
278 req->rq_rep_swab_mask = 0;
279 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_nob_received);
281 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
286 lustre_set_rep_swabbed(req, MSG_PTLRPC_HEADER_OFF);
291 static int unpack_reply(struct ptlrpc_request *req)
295 rc = unpack_reply_common(req);
299 rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
301 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
307 static inline void unpack_reply_free_msg(struct lustre_msg *msg, int len)
312 static int unpack_reply_copy_msg(struct ptlrpc_request *req,
313 struct lustre_msg **msg, int *len)
315 struct lustre_msg *msgcpy;
316 __u32 csum_calc, csum_get;
320 LASSERT_SPIN_LOCKED(&req->rq_lock);
324 /* Swabbing required when rc == 1 */
325 rc = unpack_reply_common(req);
329 lencpy = req->rq_replen;
330 spin_unlock(&req->rq_lock);
332 OBD_ALLOC(msgcpy, lencpy);
334 spin_lock(&req->rq_lock);
337 spin_lock(&req->rq_lock);
339 /* Checksum must be calculated before being unswabbed. If the magic
340 * in the copy is unswabbed discard like the checksum failure case */
341 memcpy(msgcpy, req->rq_repmsg, lencpy);
342 if (lustre_msg_need_swab(msgcpy)) {
343 DEBUG_REQ(D_NET, req, "incorrect message magic: %08x\n",
345 GOTO(err, rc = -EINVAL);
348 csum_calc = lustre_msg_calc_cksum(msgcpy);
350 /* Unpack the copy the original rq_repmsg is untouched */
351 rc = lustre_unpack_msg_ptlrpc_body(msgcpy, MSG_PTLRPC_BODY_OFF, rc);
353 DEBUG_REQ(D_ERROR, req, "unpack msg copy failed: %d", rc);
354 GOTO(err, rc = -EPROTO);
357 /* For early replies the LND may update repmsg outside req->rq_lock
358 * resulting in a checksum failure which is non-harmful */
359 csum_get = lustre_msg_get_cksum(msgcpy);
360 if (csum_calc != csum_get) {
361 DEBUG_REQ(D_NET, req, "checksum mismatch: %x != %x\n",
362 csum_calc, csum_get);
363 GOTO(err, rc = -EINVAL);
370 unpack_reply_free_msg(msgcpy, lencpy);
374 /* Handle an early reply message. To prevent a real reply from arriving
375 * and changing req->rq_repmsg this func is called under the rq_lock */
376 static int ptlrpc_at_recv_early_reply(struct ptlrpc_request *req) {
377 struct lustre_msg *msg;
382 LASSERT_SPIN_LOCKED(&req->rq_lock);
385 /* All early replys for this request use a single repbuf which can
386 * be updated outside the req->rq_lock. To prevent racing we create
387 * a copy of the repmsg and verify its checksum before it is used. */
388 rc = unpack_reply_copy_msg(req, &msg, &len);
390 /* Let's just ignore it - same as if it never got here */
391 CDEBUG(D_ADAPTTO, "Discarding racing early reply: %d\n", rc);
395 /* Expecting to increase the service time estimate here */
396 ptlrpc_at_adj_service(req, lustre_msg_get_timeout(msg));
397 ptlrpc_at_adj_net_latency(req, lustre_msg_get_service_time(msg));
398 /* Adjust the local timeout for this req */
399 ptlrpc_at_set_req_timeout(req);
401 olddl = req->rq_deadline;
402 /* Server assumes it now has rq_timeout from when it sent the
403 early reply, so client should give it at least that long. */
404 req->rq_deadline = cfs_time_current_sec() + req->rq_timeout +
405 ptlrpc_at_get_net_latency(req);
407 DEBUG_REQ(D_ADAPTTO, req,
408 "Early reply #%d, new deadline in %lds (%+lds)",
409 req->rq_early_count, req->rq_deadline -
410 cfs_time_current_sec(), req->rq_deadline - olddl);
412 unpack_reply_free_msg(msg, len);
416 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
418 struct list_head *l, *tmp;
419 struct ptlrpc_request *req;
421 LASSERT(pool != NULL);
423 spin_lock(&pool->prp_lock);
424 list_for_each_safe(l, tmp, &pool->prp_req_list) {
425 req = list_entry(l, struct ptlrpc_request, rq_list);
426 list_del(&req->rq_list);
427 LASSERT(req->rq_reqmsg);
428 OBD_FREE(req->rq_reqmsg, pool->prp_rq_size);
429 OBD_FREE(req, sizeof(*req));
431 spin_unlock(&pool->prp_lock);
432 OBD_FREE(pool, sizeof(*pool));
435 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
440 while (size < pool->prp_rq_size)
443 LASSERTF(list_empty(&pool->prp_req_list) || size == pool->prp_rq_size,
444 "Trying to change pool size with nonempty pool "
445 "from %d to %d bytes\n", pool->prp_rq_size, size);
447 spin_lock(&pool->prp_lock);
448 pool->prp_rq_size = size;
449 for (i = 0; i < num_rq; i++) {
450 struct ptlrpc_request *req;
451 struct lustre_msg *msg;
453 spin_unlock(&pool->prp_lock);
454 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
457 OBD_ALLOC_GFP(msg, size, CFS_ALLOC_STD);
459 OBD_FREE(req, sizeof(struct ptlrpc_request));
462 req->rq_reqmsg = msg;
464 spin_lock(&pool->prp_lock);
465 list_add_tail(&req->rq_list, &pool->prp_req_list);
467 spin_unlock(&pool->prp_lock);
471 struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int num_rq, int msgsize,
472 void (*populate_pool)(struct ptlrpc_request_pool *, int))
474 struct ptlrpc_request_pool *pool;
476 OBD_ALLOC(pool, sizeof (struct ptlrpc_request_pool));
480 /* Request next power of two for the allocation, because internally
481 kernel would do exactly this */
483 spin_lock_init(&pool->prp_lock);
484 CFS_INIT_LIST_HEAD(&pool->prp_req_list);
485 pool->prp_rq_size = msgsize;
486 pool->prp_populate = populate_pool;
488 populate_pool(pool, num_rq);
490 if (list_empty(&pool->prp_req_list)) {
491 /* have not allocated a single request for the pool */
492 OBD_FREE(pool, sizeof (struct ptlrpc_request_pool));
498 static struct ptlrpc_request *ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
500 struct ptlrpc_request *request;
501 struct lustre_msg *reqmsg;
506 spin_lock(&pool->prp_lock);
508 /* See if we have anything in a pool, and bail out if nothing,
509 * in writeout path, where this matters, this is safe to do, because
510 * nothing is lost in this case, and when some in-flight requests
511 * complete, this code will be called again. */
512 if (unlikely(list_empty(&pool->prp_req_list))) {
513 spin_unlock(&pool->prp_lock);
517 request = list_entry(pool->prp_req_list.next, struct ptlrpc_request,
519 list_del_init(&request->rq_list);
520 spin_unlock(&pool->prp_lock);
522 LASSERT(request->rq_reqmsg);
523 LASSERT(request->rq_pool);
525 reqmsg = request->rq_reqmsg;
526 memset(request, 0, sizeof(*request));
527 request->rq_reqmsg = reqmsg;
528 request->rq_pool = pool;
529 request->rq_reqlen = pool->prp_rq_size;
533 struct ptlrpc_request *
534 ptlrpc_prep_req_pool(struct obd_import *imp, __u32 version, int opcode,
535 int count, __u32 *lengths, char **bufs,
536 struct ptlrpc_request_pool *pool)
538 struct ptlrpc_request *request = NULL;
542 /* The obd disconnected */
546 LASSERT(imp != LP_POISON);
547 LASSERT((unsigned long)imp->imp_client > 0x1000);
548 LASSERT(imp->imp_client != LP_POISON);
551 request = ptlrpc_prep_req_from_pool(pool);
554 OBD_ALLOC(request, sizeof(*request));
557 CERROR("request allocation out of memory\n");
561 rc = lustre_pack_request(request, imp->imp_msg_magic, count, lengths,
564 LASSERT(!request->rq_pool);
565 OBD_FREE(request, sizeof(*request));
569 lustre_msg_add_version(request->rq_reqmsg, version);
570 request->rq_send_state = LUSTRE_IMP_FULL;
571 request->rq_type = PTL_RPC_MSG_REQUEST;
572 request->rq_import = class_import_get(imp);
573 request->rq_export = NULL;
575 request->rq_req_cbid.cbid_fn = request_out_callback;
576 request->rq_req_cbid.cbid_arg = request;
578 request->rq_reply_cbid.cbid_fn = reply_in_callback;
579 request->rq_reply_cbid.cbid_arg = request;
581 request->rq_reply_deadline = 0;
582 request->rq_phase = RQ_PHASE_NEW;
583 request->rq_next_phase = RQ_PHASE_UNDEFINED;
585 request->rq_request_portal = imp->imp_client->cli_request_portal;
586 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
588 ptlrpc_at_set_req_timeout(request);
590 spin_lock_init(&request->rq_lock);
591 CFS_INIT_LIST_HEAD(&request->rq_list);
592 CFS_INIT_LIST_HEAD(&request->rq_replay_list);
593 CFS_INIT_LIST_HEAD(&request->rq_set_chain);
594 CFS_INIT_LIST_HEAD(&request->rq_history_list);
595 CFS_INIT_LIST_HEAD(&request->rq_exp_list);
596 cfs_waitq_init(&request->rq_reply_waitq);
597 request->rq_xid = ptlrpc_next_xid();
598 atomic_set(&request->rq_refcount, 1);
600 lustre_msg_set_opc(request->rq_reqmsg, opcode);
605 struct ptlrpc_request *
606 ptlrpc_prep_req(struct obd_import *imp, __u32 version, int opcode, int count,
607 __u32 *lengths, char **bufs)
609 return ptlrpc_prep_req_pool(imp, version, opcode, count, lengths, bufs,
613 struct ptlrpc_request *ptlrpc_prep_fakereq(struct obd_import *imp,
614 unsigned int timeout,
615 int (*interpreter)(struct ptlrpc_request *,
618 struct ptlrpc_request *request = NULL;
621 OBD_ALLOC(request, sizeof(*request));
623 CERROR("request allocation out of memory\n");
627 request->rq_send_state = LUSTRE_IMP_FULL;
628 request->rq_type = PTL_RPC_MSG_REQUEST;
629 request->rq_import = class_import_get(imp);
630 request->rq_export = NULL;
631 request->rq_import_generation = imp->imp_generation;
633 request->rq_timeout = timeout;
634 request->rq_sent = cfs_time_current_sec();
635 request->rq_deadline = request->rq_sent + timeout;
636 request->rq_reply_deadline = request->rq_deadline;
637 request->rq_interpret_reply = interpreter;
638 request->rq_phase = RQ_PHASE_RPC;
639 request->rq_next_phase = RQ_PHASE_INTERPRET;
640 /* don't want reply */
641 request->rq_receiving_reply = 0;
642 request->rq_must_unlink = 0;
643 request->rq_no_delay = request->rq_no_resend = 1;
644 request->rq_fake = 1;
646 spin_lock_init(&request->rq_lock);
647 CFS_INIT_LIST_HEAD(&request->rq_list);
648 CFS_INIT_LIST_HEAD(&request->rq_replay_list);
649 CFS_INIT_LIST_HEAD(&request->rq_set_chain);
650 CFS_INIT_LIST_HEAD(&request->rq_history_list);
651 CFS_INIT_LIST_HEAD(&request->rq_exp_list);
652 cfs_waitq_init(&request->rq_reply_waitq);
654 request->rq_xid = ptlrpc_next_xid();
655 atomic_set(&request->rq_refcount, 1);
660 void ptlrpc_fakereq_finished(struct ptlrpc_request *req)
662 /* if we kill request before timeout - need adjust counter */
663 if (req->rq_phase == RQ_PHASE_RPC) {
664 struct ptlrpc_request_set *set = req->rq_set;
667 set->set_remaining --;
670 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
671 list_del_init(&req->rq_list);
675 struct ptlrpc_request_set *ptlrpc_prep_set(void)
677 struct ptlrpc_request_set *set;
680 OBD_ALLOC(set, sizeof *set);
683 CFS_INIT_LIST_HEAD(&set->set_requests);
684 cfs_waitq_init(&set->set_waitq);
685 set->set_remaining = 0;
686 spin_lock_init(&set->set_new_req_lock);
687 CFS_INIT_LIST_HEAD(&set->set_new_requests);
688 CFS_INIT_LIST_HEAD(&set->set_cblist);
693 /* Finish with this set; opposite of prep_set. */
694 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
696 struct list_head *tmp;
697 struct list_head *next;
702 /* Requests on the set should either all be completed, or all be new */
703 expected_phase = (set->set_remaining == 0) ?
704 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
705 list_for_each (tmp, &set->set_requests) {
706 struct ptlrpc_request *req =
707 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
709 LASSERT(req->rq_phase == expected_phase);
713 LASSERTF(set->set_remaining == 0 || set->set_remaining == n, "%d / %d\n",
714 set->set_remaining, n);
716 list_for_each_safe(tmp, next, &set->set_requests) {
717 struct ptlrpc_request *req =
718 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
719 list_del_init(&req->rq_set_chain);
721 LASSERT(req->rq_phase == expected_phase);
723 if (req->rq_phase == RQ_PHASE_NEW) {
725 if (req->rq_interpret_reply != NULL) {
726 int (*interpreter)(struct ptlrpc_request *,
728 req->rq_interpret_reply;
730 /* higher level (i.e. LOV) failed;
731 * let the sub reqs clean up */
732 req->rq_status = -EBADR;
733 interpreter(req, &req->rq_async_args,
736 set->set_remaining--;
740 ptlrpc_req_finished (req);
743 LASSERT(set->set_remaining == 0);
745 OBD_FREE(set, sizeof(*set));
749 int ptlrpc_set_add_cb(struct ptlrpc_request_set *set,
750 set_interpreter_func fn, void *data)
752 struct ptlrpc_set_cbdata *cbdata;
754 OBD_SLAB_ALLOC(cbdata, ptlrpc_cbdata_slab,
755 CFS_ALLOC_STD, sizeof(*cbdata));
759 cbdata->psc_interpret = fn;
760 cbdata->psc_data = data;
761 list_add_tail(&cbdata->psc_item, &set->set_cblist);
766 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
767 struct ptlrpc_request *req)
769 /* The set takes over the caller's request reference */
770 list_add_tail(&req->rq_set_chain, &set->set_requests);
772 set->set_remaining++;
776 * Lock so many callers can add things, the context that owns the set
777 * is supposed to notice these and move them into the set proper.
779 int ptlrpc_set_add_new_req(struct ptlrpcd_ctl *pc,
780 struct ptlrpc_request *req)
782 struct ptlrpc_request_set *set = pc->pc_set;
785 * Let caller know that we stopped and will not handle this request.
786 * It needs to take care itself of request.
788 if (test_bit(LIOD_STOP, &pc->pc_flags))
791 spin_lock(&set->set_new_req_lock);
793 * The set takes over the caller's request reference.
795 list_add_tail(&req->rq_set_chain, &set->set_new_requests);
797 spin_unlock(&set->set_new_req_lock);
800 * Let thead know that we added something and better it to wake up
803 cfs_waitq_signal(&set->set_waitq);
808 * Based on the current state of the import, determine if the request
809 * can be sent, is an error, or should be delayed.
811 * Returns true if this request should be delayed. If false, and
812 * *status is set, then the request can not be sent and *status is the
813 * error code. If false and status is 0, then request can be sent.
815 * The imp->imp_lock must be held.
817 static int ptlrpc_import_delay_req(struct obd_import *imp,
818 struct ptlrpc_request *req, int *status)
823 LASSERT (status != NULL);
826 if (imp->imp_state == LUSTRE_IMP_NEW) {
827 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
830 } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
831 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
833 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
834 imp->imp_state == LUSTRE_IMP_CONNECTING) {
835 /* allow CONNECT even if import is invalid */ ;
836 if (atomic_read(&imp->imp_inval_count) != 0) {
837 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
840 } else if ((imp->imp_invalid && (!imp->imp_recon_bk)) ||
841 imp->imp_obd->obd_no_recov) {
842 /* If the import has been invalidated (such as by an OST
843 * failure), and if the import(MGC) tried all of its connection
844 * list (Bug 13464), the request must fail with -ESHUTDOWN.
845 * This indicates the requests should be discarded; an -EIO
846 * may result in a resend of the request. */
847 if (!imp->imp_deactive)
848 DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
849 *status = -ESHUTDOWN; /* bz 12940 */
850 } else if (req->rq_import_generation != imp->imp_generation) {
851 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
853 } else if (req->rq_send_state != imp->imp_state) {
854 /* invalidate in progress - any requests should be drop */
855 if (atomic_read(&imp->imp_inval_count) != 0) {
856 DEBUG_REQ(D_ERROR, req, "invalidate in flight");
858 } else if (imp->imp_dlm_fake || req->rq_no_delay) {
859 *status = -EWOULDBLOCK;
868 static int ptlrpc_check_reply(struct ptlrpc_request *req)
871 const char *what = "";
874 /* serialise with network callback */
875 spin_lock(&req->rq_lock);
877 if (ptlrpc_client_replied(req)) {
882 if (req->rq_net_err && !req->rq_timedout) {
884 spin_unlock(&req->rq_lock);
885 rc = ptlrpc_expire_one_request(req, 0);
886 spin_lock(&req->rq_lock);
895 if (req->rq_resend) {
900 if (req->rq_restart) {
905 if (ptlrpc_client_early(req)) {
907 ptlrpc_at_recv_early_reply(req);
908 GOTO(out, rc = 0); /* keep waiting */
913 spin_unlock(&req->rq_lock);
914 DEBUG_REQ(D_NET, req, "%src = %d for", what, rc);
918 /* Conditionally suppress specific console messages */
919 static int ptlrpc_console_allow(struct ptlrpc_request *req)
921 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
924 /* Suppress particular reconnect errors which are to be expected. No
925 * errors are suppressed for the initial connection on an import */
926 if ((lustre_handle_is_used(&req->rq_import->imp_remote_handle)) &&
927 (opc == OST_CONNECT || opc == MDS_CONNECT || opc == MGS_CONNECT)) {
929 /* Suppress timed out reconnect requests */
930 if (req->rq_timedout)
933 /* Suppress unavailable/again reconnect requests */
934 err = lustre_msg_get_status(req->rq_repmsg);
935 if (err == -ENODEV || err == -EAGAIN)
942 static int ptlrpc_check_status(struct ptlrpc_request *req)
947 err = lustre_msg_get_status(req->rq_repmsg);
949 DEBUG_REQ(D_INFO, req, "status is %d", err);
950 } else if (err > 0) {
951 /* XXX: translate this error from net to host */
952 DEBUG_REQ(D_INFO, req, "status is %d", err);
955 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
956 struct obd_import *imp = req->rq_import;
957 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
959 if (ptlrpc_console_allow(req))
960 LCONSOLE_ERROR_MSG(0x011,"an error occurred while "
961 "communicating with %s. The %s "
962 "operation failed with %d\n",
964 imp->imp_connection->c_peer.nid),
965 ll_opcode2str(opc), err);
967 RETURN(err < 0 ? err : -EINVAL);
973 /* VBR: we should save pre-versions for replay*/
974 static void ptlrpc_save_versions(struct ptlrpc_request *req)
976 struct lustre_msg *repmsg = req->rq_repmsg;
977 struct lustre_msg *reqmsg = req->rq_reqmsg;
978 __u64 *versions = lustre_msg_get_versions(repmsg);
980 /* Interoperability with 1.6. This should be changed to LASSERT in HEAD */
981 if (versions == NULL)
984 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
987 lustre_msg_set_versions(reqmsg, versions);
988 CDEBUG(D_INFO, "Client save versions ["LPX64"/"LPX64"]\n",
989 versions[0], versions[1]);
994 static int after_reply(struct ptlrpc_request *req)
996 struct obd_import *imp = req->rq_import;
997 struct obd_device *obd = req->rq_import->imp_obd;
999 struct timeval work_start;
1003 LASSERT(!req->rq_receiving_reply);
1006 /* NB Until this point, the whole of the incoming message,
1007 * including buflens, status etc is in the sender's byte order. */
1009 if (req->rq_reply_truncate && !req->rq_no_resend) {
1011 OBD_FREE(req->rq_repbuf, req->rq_replen);
1012 req->rq_repbuf = NULL;
1013 req->rq_replen = req->rq_nob_received;
1017 LASSERT (req->rq_nob_received <= req->rq_replen);
1018 rc = unpack_reply(req);
1022 do_gettimeofday(&work_start);
1023 timediff = cfs_timeval_sub(&work_start, &req->rq_arrival_time, NULL);
1024 if (obd->obd_svc_stats != NULL) {
1025 lprocfs_counter_add(obd->obd_svc_stats, PTLRPC_REQWAIT_CNTR,
1027 ptlrpc_lprocfs_rpc_sent(req, timediff);
1030 OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_PAUSE_REP, obd_fail_val);
1031 ptlrpc_at_adj_service(req, lustre_msg_get_timeout(req->rq_repmsg));
1032 ptlrpc_at_adj_net_latency(req, lustre_msg_get_service_time(req->rq_repmsg));
1034 if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
1035 lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
1036 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
1037 lustre_msg_get_type(req->rq_repmsg));
1041 rc = ptlrpc_check_status(req);
1043 /* Either we've been evicted, or the server has failed for
1044 * some reason. Try to reconnect, and if that fails, punt to
1046 if (ll_rpc_recoverable_error(rc)) {
1047 if (req->rq_send_state != LUSTRE_IMP_FULL ||
1048 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
1051 ptlrpc_request_handle_notconn(req);
1055 /* Let's look if server sent slv. Do it only for RPC with
1057 ldlm_cli_update_pool(req);
1060 /* Store transno in reqmsg for replay. */
1061 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)) {
1062 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
1063 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
1066 if (imp->imp_replayable) {
1067 spin_lock(&imp->imp_lock);
1068 /* no point in adding already-committed requests to the replay
1069 * list, we will just remove them immediately. b=9829 */
1070 if (req->rq_transno != 0 &&
1072 lustre_msg_get_last_committed(req->rq_repmsg) ||
1074 /* version recovery */
1075 ptlrpc_save_versions(req);
1076 ptlrpc_retain_replayable_request(req, imp);
1077 } else if (req->rq_commit_cb != NULL) {
1078 spin_unlock(&imp->imp_lock);
1079 req->rq_commit_cb(req);
1080 spin_lock(&imp->imp_lock);
1083 /* Replay-enabled imports return commit-status information. */
1084 if (lustre_msg_get_last_committed(req->rq_repmsg))
1085 imp->imp_peer_committed_transno =
1086 lustre_msg_get_last_committed(req->rq_repmsg);
1087 ptlrpc_free_committed(imp);
1089 if (req->rq_transno > imp->imp_peer_committed_transno)
1090 ptlrpc_pinger_sending_on_import(imp);
1091 spin_unlock(&imp->imp_lock);
1097 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
1099 struct obd_import *imp;
1103 LASSERT(req->rq_phase == RQ_PHASE_NEW);
1104 if (req->rq_sent && (req->rq_sent > CURRENT_SECONDS))
1107 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
1109 imp = req->rq_import;
1110 spin_lock(&imp->imp_lock);
1112 req->rq_import_generation = imp->imp_generation;
1114 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1115 spin_lock(&req->rq_lock);
1116 req->rq_waiting = 1;
1117 spin_unlock(&req->rq_lock);
1119 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
1120 "(%s != %s)", lustre_msg_get_status(req->rq_reqmsg),
1121 ptlrpc_import_state_name(req->rq_send_state),
1122 ptlrpc_import_state_name(imp->imp_state));
1123 LASSERT(list_empty(&req->rq_list));
1124 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1125 atomic_inc(&req->rq_import->imp_inflight);
1126 spin_unlock(&imp->imp_lock);
1131 spin_unlock(&imp->imp_lock);
1132 req->rq_status = rc;
1133 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1137 LASSERT(list_empty(&req->rq_list));
1138 list_add_tail(&req->rq_list, &imp->imp_sending_list);
1139 atomic_inc(&req->rq_import->imp_inflight);
1140 spin_unlock(&imp->imp_lock);
1142 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
1143 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
1144 " %s:%s:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1145 imp->imp_obd->obd_uuid.uuid,
1146 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1147 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1148 lustre_msg_get_opc(req->rq_reqmsg));
1150 rc = ptl_send_rpc(req, 0);
1152 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
1153 req->rq_net_err = 1;
1159 /* this sends any unsent RPCs in @set and returns TRUE if all are sent */
1160 int ptlrpc_check_set(struct ptlrpc_request_set *set)
1162 struct list_head *tmp;
1163 int force_timer_recalc = 0;
1166 if (set->set_remaining == 0)
1169 list_for_each(tmp, &set->set_requests) {
1170 struct ptlrpc_request *req =
1171 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1172 struct obd_import *imp = req->rq_import;
1175 if (req->rq_phase == RQ_PHASE_NEW &&
1176 ptlrpc_send_new_req(req)) {
1177 force_timer_recalc = 1;
1180 /* delayed send - skip */
1181 if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
1184 if (!(req->rq_phase == RQ_PHASE_RPC ||
1185 req->rq_phase == RQ_PHASE_BULK ||
1186 req->rq_phase == RQ_PHASE_INTERPRET ||
1187 req->rq_phase == RQ_PHASE_UNREGISTERING ||
1188 req->rq_phase == RQ_PHASE_COMPLETE)) {
1189 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
1193 if (req->rq_phase == RQ_PHASE_UNREGISTERING) {
1194 LASSERT(req->rq_next_phase != req->rq_phase);
1195 LASSERT(req->rq_next_phase != RQ_PHASE_UNDEFINED);
1197 /* Skip processing until reply is unlinked. We
1198 * can't return to pool before that and we can't
1199 * call interpret before that. We need to make
1200 * sure that all rdma transfers finished and will
1201 * not corrupt any data. */
1202 if (ptlrpc_client_recv_or_unlink(req) ||
1203 ptlrpc_client_bulk_active(req))
1206 /* Turn repl fail_loc off to prevent it from looping
1208 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
1209 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK |
1213 /* Turn off bulk fail_loc. */
1214 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK)) {
1215 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_BULK_UNLINK |
1219 /* Move to next phase if reply was successfully
1221 ptlrpc_rqphase_move(req, req->rq_next_phase);
1224 if (req->rq_phase == RQ_PHASE_COMPLETE)
1227 if (req->rq_phase == RQ_PHASE_INTERPRET)
1228 GOTO(interpret, req->rq_status);
1230 /* Note that this also will start async reply unlink */
1231 if (req->rq_net_err && !req->rq_timedout) {
1232 ptlrpc_expire_one_request(req, 1);
1234 /* Check if we still need to wait for unlink. */
1235 if (ptlrpc_client_recv_or_unlink(req) ||
1236 ptlrpc_client_bulk_active(req))
1241 if (req->rq_status == 0)
1242 req->rq_status = -EIO;
1243 GOTO(interpret, req->rq_status);
1246 /* ptlrpc_queue_wait->l_wait_event guarantees that rq_intr
1247 * will only be set after rq_timedout, but the oig waiting
1248 * path sets rq_intr irrespective of whether ptlrpcd has
1249 * seen a timeout. our policy is to only interpret
1250 * interrupted rpcs after they have timed out */
1251 if (req->rq_intr && (req->rq_timedout || req->rq_waiting)) {
1252 req->rq_status = -EINTR;
1253 GOTO(interpret, req->rq_status);
1256 if (req->rq_phase == RQ_PHASE_RPC) {
1257 if (req->rq_timedout||req->rq_waiting||req->rq_resend) {
1260 if (!ptlrpc_unregister_reply(req, 1))
1263 spin_lock(&imp->imp_lock);
1264 if (ptlrpc_import_delay_req(imp, req, &status)){
1265 /* put on delay list - only if we wait
1266 * recovery finished - before send */
1267 list_del_init(&req->rq_list);
1268 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1269 spin_unlock(&imp->imp_lock);
1274 req->rq_status = status;
1275 spin_unlock(&imp->imp_lock);
1276 GOTO(interpret, req->rq_status);
1278 if (req->rq_no_resend) {
1279 req->rq_status = -ENOTCONN;
1280 spin_unlock(&imp->imp_lock);
1281 GOTO(interpret, req->rq_status);
1284 list_del_init(&req->rq_list);
1285 list_add_tail(&req->rq_list,
1286 &imp->imp_sending_list);
1288 spin_unlock(&imp->imp_lock);
1290 req->rq_waiting = 0;
1292 if (req->rq_timedout||req->rq_resend) {
1293 /* This is re-sending anyways,
1294 * let's mark req as resend. */
1299 if (!ptlrpc_unregister_bulk(req, 1))
1302 /* ensure previous bulk fails */
1303 old_xid = req->rq_xid;
1304 req->rq_xid = ptlrpc_next_xid();
1305 CDEBUG(D_HA, "resend bulk "
1308 old_xid, req->rq_xid);
1312 rc = ptl_send_rpc(req, 0);
1314 DEBUG_REQ(D_HA, req, "send failed (%d)",
1316 force_timer_recalc = 1;
1317 req->rq_net_err = 1;
1319 /* need to reset the timeout */
1320 force_timer_recalc = 1;
1323 spin_lock(&req->rq_lock);
1325 if (ptlrpc_client_early(req)) {
1326 ptlrpc_at_recv_early_reply(req);
1327 spin_unlock(&req->rq_lock);
1331 /* Still waiting for a reply? */
1332 if (ptlrpc_client_recv(req)) {
1333 spin_unlock(&req->rq_lock);
1337 /* Did we actually receive a reply? */
1338 if (!ptlrpc_client_replied(req)) {
1339 spin_unlock(&req->rq_lock);
1343 spin_unlock(&req->rq_lock);
1345 req->rq_status = after_reply(req);
1349 /* If there is no bulk associated with this request,
1350 * then we're done and should let the interpreter
1351 * process the reply. Similarly if the RPC returned
1352 * an error, and therefore the bulk will never arrive.
1354 if (req->rq_bulk == NULL || req->rq_status != 0)
1355 GOTO(interpret, req->rq_status);
1357 ptlrpc_rqphase_move(req, RQ_PHASE_BULK);
1360 LASSERT(req->rq_phase == RQ_PHASE_BULK);
1361 if (ptlrpc_client_bulk_active(req))
1364 if (!req->rq_bulk->bd_success) {
1365 /* The RPC reply arrived OK, but the bulk screwed
1366 * up! Dead wierd since the server told us the RPC
1367 * was good after getting the REPLY for her GET or
1368 * the ACK for her PUT. */
1369 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1370 req->rq_status = -EIO;
1371 GOTO(interpret, req->rq_status);
1374 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
1376 /* This moves to "unregistering" phase we need to wait for
1378 if (!ptlrpc_unregister_reply(req, 1))
1381 if (!ptlrpc_unregister_bulk(req, 1))
1384 /* When calling interpret receiving already should be
1386 LASSERT(!req->rq_receiving_reply);
1388 ptlrpc_req_interpret(req, req->rq_status);
1390 ptlrpc_rqphase_move(req, RQ_PHASE_COMPLETE);
1392 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
1393 "opc %s:%s:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
1394 imp->imp_obd->obd_uuid.uuid,
1395 req->rq_reqmsg ? lustre_msg_get_status(req->rq_reqmsg):-1,
1397 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1398 req->rq_reqmsg ? lustre_msg_get_opc(req->rq_reqmsg) : -1);
1400 spin_lock(&imp->imp_lock);
1401 /* Request already may be not on sending or delaying list. This
1402 * may happen in the case of marking it errorneous for the case
1403 * ptlrpc_import_delay_req(req, status) find it impossible to
1404 * allow sending this rpc and returns *status != 0. */
1405 if (!list_empty(&req->rq_list)) {
1406 list_del_init(&req->rq_list);
1407 atomic_dec(&imp->imp_inflight);
1409 spin_unlock(&imp->imp_lock);
1411 set->set_remaining--;
1412 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
1415 /* If we hit an error, we want to recover promptly. */
1416 RETURN(set->set_remaining == 0 || force_timer_recalc);
1419 /* Return 1 if we should give up, else 0 */
1420 int ptlrpc_expire_one_request(struct ptlrpc_request *req, int async_unlink)
1422 struct obd_import *imp = req->rq_import;
1426 DEBUG_REQ(req->rq_fake ? D_INFO : D_WARNING, req,
1427 "Request x"LPU64" sent from %s to NID %s"
1428 " %lus ago has %s (%lds prior to deadline).\n", req->rq_xid,
1429 imp ? imp->imp_obd->obd_name : "<?>",
1430 imp ? libcfs_nid2str(imp->imp_connection->c_peer.nid) : "<?>",
1431 cfs_time_current_sec() - req->rq_sent,
1432 req->rq_net_err ? "failed due to network error" : "timed out",
1433 req->rq_deadline - req->rq_sent);
1435 if (imp != NULL && obd_debug_peer_on_timeout)
1436 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1438 spin_lock(&req->rq_lock);
1439 req->rq_timedout = 1;
1440 spin_unlock(&req->rq_lock);
1442 ptlrpc_unregister_reply(req, async_unlink);
1443 ptlrpc_unregister_bulk(req, async_unlink);
1445 if (obd_dump_on_timeout)
1446 libcfs_debug_dumplog();
1449 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
1456 atomic_inc(&imp->imp_timeouts);
1458 /* The DLM server doesn't want recovery run on its imports. */
1459 if (imp->imp_dlm_fake)
1462 /* If this request is for recovery or other primordial tasks,
1463 * then error it out here. */
1464 if (req->rq_send_state != LUSTRE_IMP_FULL ||
1465 imp->imp_obd->obd_no_recov) {
1466 DEBUG_REQ(D_RPCTRACE, req, "err -110, sent_state=%s (now=%s)",
1467 ptlrpc_import_state_name(req->rq_send_state),
1468 ptlrpc_import_state_name(imp->imp_state));
1469 spin_lock(&req->rq_lock);
1470 req->rq_status = -ETIMEDOUT;
1472 spin_unlock(&req->rq_lock);
1476 /* if a request can't be resent we can't wait for an answer after
1478 if (req->rq_no_resend) {
1479 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1483 ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1488 int ptlrpc_expired_set(void *data)
1490 struct ptlrpc_request_set *set = data;
1491 struct list_head *tmp;
1492 time_t now = cfs_time_current_sec();
1495 LASSERT(set != NULL);
1497 /* A timeout expired; see which reqs it applies to... */
1498 list_for_each(tmp, &set->set_requests) {
1499 struct ptlrpc_request *req =
1500 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1502 /* Request in-flight? */
1503 if (!((req->rq_phase == RQ_PHASE_RPC &&
1504 !req->rq_waiting && !req->rq_resend) ||
1505 (req->rq_phase == RQ_PHASE_BULK)))
1508 if (req->rq_timedout || /* already dealt with */
1509 req->rq_deadline > now) /* not expired */
1512 /* Deal with this guy. Do it asynchronously to not block
1513 * ptlrpcd thread. */
1514 ptlrpc_expire_one_request(req, 1);
1517 /* When waiting for a whole set, we always to break out of the
1518 * sleep so we can recalculate the timeout, or enable interrupts
1519 * if everyone's timed out. */
1523 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1525 spin_lock(&req->rq_lock);
1527 spin_unlock(&req->rq_lock);
1530 void ptlrpc_interrupted_set(void *data)
1532 struct ptlrpc_request_set *set = data;
1533 struct list_head *tmp;
1535 LASSERT(set != NULL);
1536 CERROR("INTERRUPTED SET %p\n", set);
1538 list_for_each(tmp, &set->set_requests) {
1539 struct ptlrpc_request *req =
1540 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1542 if (req->rq_phase != RQ_PHASE_RPC &&
1543 req->rq_phase != RQ_PHASE_UNREGISTERING)
1546 ptlrpc_mark_interrupted(req);
1550 /* get the smallest timeout in the set; this does NOT set a timeout. */
1551 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1553 struct list_head *tmp;
1554 time_t now = cfs_time_current_sec();
1556 struct ptlrpc_request *req;
1560 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1562 list_for_each(tmp, &set->set_requests) {
1563 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1565 /* request in-flight? */
1566 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
1567 (req->rq_phase == RQ_PHASE_BULK) ||
1568 (req->rq_phase == RQ_PHASE_NEW)))
1571 /* Already timed out. */
1572 if (req->rq_timedout)
1575 if (req->rq_phase == RQ_PHASE_NEW)
1576 deadline = req->rq_sent; /* delayed send */
1578 deadline = req->rq_deadline;
1580 if (deadline <= now) { /* actually expired already */
1581 timeout = 1; /* ASAP */
1584 if ((timeout == 0) || (timeout > (deadline - now)))
1585 timeout = deadline - now;
1590 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
1592 struct list_head *tmp;
1593 struct ptlrpc_request *req;
1594 struct l_wait_info lwi;
1598 if (list_empty(&set->set_requests))
1601 list_for_each(tmp, &set->set_requests) {
1602 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1603 if (req->rq_phase == RQ_PHASE_NEW)
1604 (void)ptlrpc_send_new_req(req);
1608 timeout = ptlrpc_set_next_timeout(set);
1610 /* wait until all complete, interrupted, or an in-flight
1612 CDEBUG(D_RPCTRACE, "set %p going to sleep for %d seconds\n",
1614 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout ? timeout : 1),
1616 ptlrpc_interrupted_set, set);
1617 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(set), &lwi);
1619 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
1621 /* -EINTR => all requests have been flagged rq_intr so next
1623 * -ETIMEOUTD => someone timed out. When all reqs have
1624 * timed out, signals are enabled allowing completion with
1626 * I don't really care if we go once more round the loop in
1627 * the error cases -eeb. */
1628 } while (rc != 0 || set->set_remaining != 0);
1630 LASSERT(set->set_remaining == 0);
1633 list_for_each(tmp, &set->set_requests) {
1634 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1636 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
1637 if (req->rq_status != 0)
1638 rc = req->rq_status;
1641 if (set->set_interpret != NULL) {
1642 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
1644 rc = interpreter (set, set->set_arg, rc);
1646 struct ptlrpc_set_cbdata *cbdata, *n;
1649 list_for_each_entry_safe(cbdata, n,
1650 &set->set_cblist, psc_item) {
1651 list_del_init(&cbdata->psc_item);
1652 err = cbdata->psc_interpret(set, cbdata->psc_data, rc);
1655 OBD_SLAB_FREE(cbdata, ptlrpc_cbdata_slab,
1663 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
1665 struct ptlrpc_request_pool *pool = request->rq_pool;
1667 spin_lock(&pool->prp_lock);
1668 LASSERT(list_empty(&request->rq_list));
1669 LASSERT(!request->rq_receiving_reply);
1670 list_add_tail(&request->rq_list, &pool->prp_req_list);
1671 spin_unlock(&pool->prp_lock);
1674 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
1677 if (request == NULL) {
1682 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
1683 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
1684 LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
1685 LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
1686 LASSERTF(list_empty(&request->rq_exp_list), "req %p\n", request);
1687 LASSERTF(!request->rq_replay, "req %p\n", request);
1689 /* We must take it off the imp_replay_list first. Otherwise, we'll set
1690 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
1691 if (request->rq_import != NULL) {
1693 spin_lock(&request->rq_import->imp_lock);
1694 list_del_init(&request->rq_replay_list);
1696 spin_unlock(&request->rq_import->imp_lock);
1698 LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
1700 if (atomic_read(&request->rq_refcount) != 0) {
1701 DEBUG_REQ(D_ERROR, request,
1702 "freeing request with nonzero refcount");
1706 if (request->rq_repbuf != NULL) {
1707 OBD_FREE(request->rq_repbuf, request->rq_replen);
1708 request->rq_repbuf = NULL;
1709 request->rq_repmsg = NULL;
1711 if (request->rq_export != NULL) {
1712 class_export_put(request->rq_export);
1713 request->rq_export = NULL;
1715 if (request->rq_import != NULL) {
1716 class_import_put(request->rq_import);
1717 request->rq_import = NULL;
1719 if (request->rq_bulk != NULL)
1720 ptlrpc_free_bulk(request->rq_bulk);
1722 if (request->rq_pool) {
1723 __ptlrpc_free_req_to_pool(request);
1725 if (request->rq_reqmsg != NULL) {
1726 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
1727 request->rq_reqmsg = NULL;
1729 OBD_FREE(request, sizeof(*request));
1734 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
1735 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
1737 LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
1738 (void)__ptlrpc_req_finished(request, 1);
1741 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
1744 if (request == NULL)
1747 if (request == LP_POISON ||
1748 request->rq_reqmsg == LP_POISON) {
1749 CERROR("dereferencing freed request (bug 575)\n");
1754 DEBUG_REQ(D_INFO, request, "refcount now %u",
1755 atomic_read(&request->rq_refcount) - 1);
1757 if (atomic_dec_and_test(&request->rq_refcount)) {
1758 __ptlrpc_free_req(request, locked);
1765 void ptlrpc_req_finished(struct ptlrpc_request *request)
1767 __ptlrpc_req_finished(request, 0);
1770 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
1772 return request->rq_xid;
1774 EXPORT_SYMBOL(ptlrpc_req_xid);
1776 /* Disengage the client's reply buffer from the network
1777 * NB does _NOT_ unregister any client-side bulk.
1778 * IDEMPOTENT, but _not_ safe against concurrent callers.
1779 * The request owner (i.e. the thread doing the I/O) must call...
1781 int ptlrpc_unregister_reply(struct ptlrpc_request *request, int async)
1785 struct l_wait_info lwi;
1789 LASSERT(!in_interrupt());
1791 /* Let's setup deadline for reply unlink. */
1792 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK) &&
1793 async && request->rq_reply_deadline == 0)
1794 request->rq_reply_deadline = cfs_time_current_sec()+LONG_UNLINK;
1796 /* Nothing left to do. */
1797 if (!ptlrpc_client_recv_or_unlink(request))
1800 LNetMDUnlink(request->rq_reply_md_h);
1802 /* Let's check it once again. */
1803 if (!ptlrpc_client_recv_or_unlink(request))
1806 /* Move to "Unregistering" phase as reply was not unlinked yet. */
1807 ptlrpc_rqphase_move(request, RQ_PHASE_UNREGISTERING);
1809 /* Do not wait for unlink to finish. */
1813 /* We have to l_wait_event() whatever the result, to give liblustre
1814 * a chance to run reply_in_callback(), and to make sure we've
1815 * unlinked before returning a req to the pool */
1816 if (request->rq_set != NULL)
1817 wq = &request->rq_set->set_waitq;
1819 wq = &request->rq_reply_waitq;
1822 /* Network access will complete in finite time but the HUGE
1823 * timeout lets us CWARN for visibility of sluggish NALs */
1824 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(LONG_UNLINK),
1825 cfs_time_seconds(1), NULL, NULL);
1826 rc = l_wait_event(*wq, !ptlrpc_client_recv_or_unlink(request),
1829 ptlrpc_rqphase_move(request, request->rq_next_phase);
1833 LASSERT(rc == -ETIMEDOUT);
1834 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout "
1835 "rvcng=%d unlnk=%d", request->rq_receiving_reply,
1836 request->rq_must_unlink);
1841 /* caller must hold imp->imp_lock */
1842 void ptlrpc_free_committed(struct obd_import *imp)
1844 struct list_head *tmp, *saved;
1845 struct ptlrpc_request *req;
1846 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1849 LASSERT(imp != NULL);
1851 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1854 if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
1855 imp->imp_generation == imp->imp_last_generation_checked) {
1856 CDEBUG(D_RPCTRACE, "%s: skip recheck: last_committed "LPU64"\n",
1857 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1862 CDEBUG(D_RPCTRACE, "%s: committing for last_committed "LPU64" gen %d\n",
1863 imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
1864 imp->imp_generation);
1865 imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
1866 imp->imp_last_generation_checked = imp->imp_generation;
1868 list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1869 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1871 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
1872 LASSERT(req != last_req);
1875 if (req->rq_import_generation < imp->imp_generation) {
1876 DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
1880 if (req->rq_replay) {
1881 DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
1885 /* not yet committed */
1886 if (req->rq_transno > imp->imp_peer_committed_transno) {
1887 DEBUG_REQ(D_RPCTRACE, req, "stopping search");
1891 DEBUG_REQ(D_RPCTRACE, req, "commit (last_committed "LPU64")",
1892 imp->imp_peer_committed_transno);
1894 spin_lock(&req->rq_lock);
1896 spin_unlock(&req->rq_lock);
1897 if (req->rq_commit_cb != NULL)
1898 req->rq_commit_cb(req);
1899 list_del_init(&req->rq_replay_list);
1900 __ptlrpc_req_finished(req, 1);
1907 void ptlrpc_cleanup_client(struct obd_import *imp)
1914 void ptlrpc_resend_req(struct ptlrpc_request *req)
1916 DEBUG_REQ(D_HA, req, "going to resend");
1917 lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
1918 req->rq_status = -EAGAIN;
1920 spin_lock(&req->rq_lock);
1922 req->rq_net_err = 0;
1923 req->rq_timedout = 0;
1925 __u64 old_xid = req->rq_xid;
1927 /* ensure previous bulk fails */
1928 req->rq_xid = ptlrpc_next_xid();
1929 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
1930 old_xid, req->rq_xid);
1932 ptlrpc_client_wake_req(req);
1933 spin_unlock(&req->rq_lock);
1936 /* XXX: this function and rq_status are currently unused */
1937 void ptlrpc_restart_req(struct ptlrpc_request *req)
1939 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1940 req->rq_status = -ERESTARTSYS;
1942 spin_lock(&req->rq_lock);
1943 req->rq_restart = 1;
1944 req->rq_timedout = 0;
1945 ptlrpc_client_wake_req(req);
1946 spin_unlock(&req->rq_lock);
1949 static void interrupted_request(void *data)
1951 struct ptlrpc_request *req = data;
1952 DEBUG_REQ(D_HA, req, "request interrupted");
1953 spin_lock(&req->rq_lock);
1955 spin_unlock(&req->rq_lock);
1958 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1961 atomic_inc(&req->rq_refcount);
1965 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1966 struct obd_import *imp)
1968 struct list_head *tmp;
1970 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1972 /* clear this for new requests that were resent as well
1973 as resent replayed requests. */
1974 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1976 /* don't re-add requests that have been replayed */
1977 if (!list_empty(&req->rq_replay_list))
1980 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
1982 LASSERT(imp->imp_replayable);
1983 /* Balanced in ptlrpc_free_committed, usually. */
1984 ptlrpc_request_addref(req);
1985 list_for_each_prev(tmp, &imp->imp_replay_list) {
1986 struct ptlrpc_request *iter =
1987 list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1989 /* We may have duplicate transnos if we create and then
1990 * open a file, or for closes retained if to match creating
1991 * opens, so use req->rq_xid as a secondary key.
1992 * (See bugs 684, 685, and 428.)
1993 * XXX no longer needed, but all opens need transnos!
1995 if (iter->rq_transno > req->rq_transno)
1998 if (iter->rq_transno == req->rq_transno) {
1999 LASSERT(iter->rq_xid != req->rq_xid);
2000 if (iter->rq_xid > req->rq_xid)
2004 list_add(&req->rq_replay_list, &iter->rq_replay_list);
2008 list_add(&req->rq_replay_list, &imp->imp_replay_list);
2011 int ptlrpc_queue_wait(struct ptlrpc_request *req)
2015 struct l_wait_info lwi;
2016 struct obd_import *imp = req->rq_import;
2017 cfs_duration_t timeout = CFS_TICK;
2021 LASSERT(req->rq_set == NULL);
2022 LASSERT(!req->rq_receiving_reply);
2024 /* for distributed debugging */
2025 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
2026 LASSERT(imp->imp_obd != NULL);
2027 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc "
2028 "%s:%s:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
2029 imp->imp_obd->obd_uuid.uuid,
2030 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
2031 libcfs_nid2str(imp->imp_connection->c_peer.nid),
2032 lustre_msg_get_opc(req->rq_reqmsg));
2034 /* Mark phase here for a little debug help */
2035 ptlrpc_rqphase_move(req, RQ_PHASE_RPC);
2037 spin_lock(&imp->imp_lock);
2038 req->rq_import_generation = imp->imp_generation;
2040 if (ptlrpc_import_delay_req(imp, req, &rc)) {
2041 list_del_init(&req->rq_list);
2042 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
2043 atomic_inc(&imp->imp_inflight);
2044 spin_unlock(&imp->imp_lock);
2046 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%s != %s)",
2048 ptlrpc_import_state_name(req->rq_send_state),
2049 ptlrpc_import_state_name(imp->imp_state));
2050 lwi = LWI_INTR(interrupted_request, req);
2051 rc = l_wait_event(req->rq_reply_waitq,
2052 (req->rq_send_state == imp->imp_state ||
2053 req->rq_err || req->rq_intr),
2055 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%s == %s or %d/%d == 1)",
2057 ptlrpc_import_state_name(imp->imp_state),
2058 ptlrpc_import_state_name(req->rq_send_state),
2059 req->rq_err, req->rq_intr);
2061 spin_lock(&imp->imp_lock);
2062 list_del_init(&req->rq_list);
2063 atomic_dec(&imp->imp_inflight);
2066 /* rq_status was set locally */
2067 rc = req->rq_status ? req->rq_status : -EIO;
2069 else if (req->rq_intr) {
2072 else if (req->rq_no_resend) {
2081 spin_unlock(&imp->imp_lock);
2082 req->rq_status = rc; // XXX this ok?
2086 if (req->rq_resend) {
2087 if (req->rq_bulk != NULL) {
2088 ptlrpc_unregister_bulk(req, 0);
2090 /* bulk requests are supposed to be
2091 * idempotent, so we are free to bump the xid
2092 * here, which we need to do before
2093 * registering the bulk again (bug 6371).
2094 * print the old xid first for sanity.
2096 DEBUG_REQ(D_HA, req, "bumping xid for bulk: ");
2097 req->rq_xid = ptlrpc_next_xid();
2100 DEBUG_REQ(D_HA, req, "resending: ");
2103 /* XXX this is the same as ptlrpc_set_wait */
2104 LASSERT(list_empty(&req->rq_list));
2105 list_add_tail(&req->rq_list, &imp->imp_sending_list);
2106 atomic_inc(&imp->imp_inflight);
2107 spin_unlock(&imp->imp_lock);
2109 rc = ptl_send_rpc(req, 0);
2111 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
2113 timeoutl = req->rq_deadline - cfs_time_current_sec();
2114 timeout = (timeoutl <= 0 || rc) ? CFS_TICK :
2115 cfs_time_seconds(timeoutl);
2116 DEBUG_REQ(D_NET, req,
2117 "-- sleeping for "CFS_DURATION_T" ticks", timeout);
2118 lwi = LWI_TIMEOUT_INTR(timeout, NULL, interrupted_request, req);
2119 brc = l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req),
2121 /* Wait again if we changed deadline */
2122 } while ((brc == -ETIMEDOUT) &&
2123 (req->rq_deadline > cfs_time_current_sec()));
2125 if ((brc == -ETIMEDOUT) && !ptlrpc_expire_one_request(req, 0)) {
2126 /* Wait forever for reconnect / replay or failure */
2127 lwi = LWI_INTR(interrupted_request, req);
2128 brc = l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req),
2132 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:opc "
2133 "%s:%s:%d:x"LPU64":%s:%d\n", cfs_curproc_comm(),
2134 imp->imp_obd->obd_uuid.uuid,
2135 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
2136 libcfs_nid2str(imp->imp_connection->c_peer.nid),
2137 lustre_msg_get_opc(req->rq_reqmsg));
2139 /* If the reply was received normally, this just grabs the spinlock
2140 * (ensuring the reply callback has returned), sees that
2141 * req->rq_receiving_reply is clear and returns. */
2142 ptlrpc_unregister_reply(req, 0);
2144 spin_lock(&imp->imp_lock);
2145 list_del_init(&req->rq_list);
2146 atomic_dec(&imp->imp_inflight);
2147 spin_unlock(&imp->imp_lock);
2150 DEBUG_REQ(D_RPCTRACE, req, "err rc=%d status=%d",
2151 rc, req->rq_status);
2152 rc = rc ? rc : req->rq_status;
2153 GOTO(out, rc = rc ? rc : -EIO);
2157 /* Should only be interrupted if we timed out. */
2158 if (!req->rq_timedout)
2159 DEBUG_REQ(D_ERROR, req,
2160 "rq_intr set but rq_timedout not");
2161 GOTO(out, rc = -EINTR);
2164 /* Resend if we need to */
2165 if (req->rq_resend||req->rq_timedout) {
2166 /* ...unless we were specifically told otherwise. */
2167 if (req->rq_no_resend)
2168 GOTO(out, rc = -ETIMEDOUT);
2169 spin_lock(&imp->imp_lock);
2170 /* we can have rq_timeout on dlm fake import which not support
2171 * recovery - but me need resend request on this import instead
2172 * of return error */
2177 if (!ptlrpc_client_replied(req)) {
2178 /* How can this be? -eeb */
2179 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
2181 GOTO(out, rc = req->rq_status);
2184 rc = after_reply(req);
2185 /* NB may return +ve success rc */
2186 if (req->rq_resend) {
2187 spin_lock(&imp->imp_lock);
2192 if (req->rq_bulk != NULL) {
2194 /* success so far. Note that anything going wrong
2195 * with bulk now, is EXTREMELY strange, since the
2196 * server must have believed that the bulk
2197 * tranferred OK before she replied with success to
2199 lwi = LWI_TIMEOUT(timeout, NULL, NULL);
2200 brc = l_wait_event(req->rq_reply_waitq,
2201 !ptlrpc_client_bulk_active(req),
2203 LASSERT(brc == 0 || brc == -ETIMEDOUT);
2205 LASSERT(brc == -ETIMEDOUT);
2206 DEBUG_REQ(D_ERROR, req, "bulk timed out");
2208 } else if (!req->rq_bulk->bd_success) {
2209 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
2214 ptlrpc_unregister_bulk(req, 0);
2217 LASSERT(!req->rq_receiving_reply);
2218 ptlrpc_rqphase_move(req, RQ_PHASE_INTERPRET);
2219 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
2223 struct ptlrpc_replay_async_args {
2225 int praa_old_status;
2228 static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
2229 void * data, int rc)
2231 struct ptlrpc_replay_async_args *aa = data;
2232 struct obd_import *imp = req->rq_import;
2235 atomic_dec(&imp->imp_replay_inflight);
2237 if (!ptlrpc_client_replied(req)) {
2238 CERROR("request replay timed out, restarting recovery\n");
2239 GOTO(out, rc = -ETIMEDOUT);
2242 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
2243 (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
2244 lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
2245 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
2247 /* VBR: check version failure */
2248 if (lustre_msg_get_status(req->rq_repmsg) == -EOVERFLOW) {
2249 /* replay was failed due to version mismatch */
2250 DEBUG_REQ(D_WARNING, req, "Version mismatch during replay\n");
2251 spin_lock(&imp->imp_lock);
2252 imp->imp_vbr_failed = 1;
2253 spin_unlock(&imp->imp_lock);
2255 /* The transno had better not change over replay. */
2256 LASSERT(lustre_msg_get_transno(req->rq_reqmsg) ==
2257 lustre_msg_get_transno(req->rq_repmsg) ||
2258 lustre_msg_get_transno(req->rq_repmsg) == 0);
2261 spin_lock(&imp->imp_lock);
2262 imp->imp_last_replay_transno = lustre_msg_get_transno(req->rq_reqmsg);
2263 spin_unlock(&imp->imp_lock);
2264 LASSERT(imp->imp_last_replay_transno);
2266 DEBUG_REQ(D_HA, req, "got rep");
2268 /* let the callback do fixups, possibly including in the request */
2269 if (req->rq_replay_cb)
2270 req->rq_replay_cb(req);
2272 if (ptlrpc_client_replied(req) &&
2273 lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
2274 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
2275 lustre_msg_get_status(req->rq_repmsg),
2276 aa->praa_old_status);
2278 /* Put it back for re-replay. */
2279 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
2282 /* continue with recovery */
2283 rc = ptlrpc_import_recovery_state_machine(imp);
2285 req->rq_send_state = aa->praa_old_state;
2288 /* this replay failed, so restart recovery */
2289 ptlrpc_connect_import(imp, NULL);
2295 int ptlrpc_replay_req(struct ptlrpc_request *req)
2297 struct ptlrpc_replay_async_args *aa;
2300 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
2302 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2303 aa = ptlrpc_req_async_args(req);
2304 memset(aa, 0, sizeof *aa);
2306 /* Prepare request to be resent with ptlrpcd */
2307 aa->praa_old_state = req->rq_send_state;
2308 req->rq_send_state = LUSTRE_IMP_REPLAY;
2309 req->rq_phase = RQ_PHASE_NEW;
2310 req->rq_next_phase = RQ_PHASE_UNDEFINED;
2312 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
2314 req->rq_interpret_reply = ptlrpc_replay_interpret;
2315 /* Readjust the timeout for current conditions */
2316 ptlrpc_at_set_req_timeout(req);
2318 DEBUG_REQ(D_HA, req, "REPLAY");
2320 atomic_inc(&req->rq_import->imp_replay_inflight);
2321 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
2323 ptlrpcd_add_req(req);
2327 void ptlrpc_abort_inflight(struct obd_import *imp)
2329 struct list_head *tmp, *n;
2332 /* Make sure that no new requests get processed for this import.
2333 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
2334 * this flag and then putting requests on sending_list or delayed_list.
2336 spin_lock(&imp->imp_lock);
2338 /* XXX locking? Maybe we should remove each request with the list
2339 * locked? Also, how do we know if the requests on the list are
2340 * being freed at this time?
2342 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
2343 struct ptlrpc_request *req =
2344 list_entry(tmp, struct ptlrpc_request, rq_list);
2346 DEBUG_REQ(D_RPCTRACE, req, "inflight");
2348 spin_lock (&req->rq_lock);
2349 if (req->rq_import_generation < imp->imp_generation) {
2351 req->rq_status = -EINTR;
2352 ptlrpc_client_wake_req(req);
2354 spin_unlock (&req->rq_lock);
2357 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
2358 struct ptlrpc_request *req =
2359 list_entry(tmp, struct ptlrpc_request, rq_list);
2361 DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
2363 spin_lock (&req->rq_lock);
2364 if (req->rq_import_generation < imp->imp_generation) {
2366 req->rq_status = -EINTR;
2367 ptlrpc_client_wake_req(req);
2369 spin_unlock (&req->rq_lock);
2372 /* Last chance to free reqs left on the replay list, but we
2373 * will still leak reqs that haven't committed. */
2374 if (imp->imp_replayable)
2375 ptlrpc_free_committed(imp);
2377 spin_unlock(&imp->imp_lock);
2382 void ptlrpc_abort_set(struct ptlrpc_request_set *set)
2384 struct list_head *tmp, *pos;
2386 LASSERT(set != NULL);
2388 list_for_each_safe(pos, tmp, &set->set_requests) {
2389 struct ptlrpc_request *req =
2390 list_entry(pos, struct ptlrpc_request, rq_set_chain);
2392 spin_lock(&req->rq_lock);
2393 if (req->rq_phase != RQ_PHASE_RPC) {
2394 spin_unlock(&req->rq_lock);
2399 req->rq_status = -EINTR;
2400 ptlrpc_client_wake_req(req);
2401 spin_unlock(&req->rq_lock);
2405 static __u64 ptlrpc_last_xid;
2406 static spinlock_t ptlrpc_last_xid_lock;
2408 /* Initialize the XID for the node. This is common among all requests on
2409 * this node, and only requires the property that it is monotonically
2410 * increasing. It does not need to be sequential. Since this is also used
2411 * as the RDMA match bits, it is important that a single client NOT have
2412 * the same match bits for two different in-flight requests, hence we do
2413 * NOT want to have an XID per target or similar.
2415 * To avoid an unlikely collision between match bits after a client reboot
2416 * (which would deliver old data into the wrong RDMA buffer) we initialize
2417 * the XID based on the current time, assuming a maximum RPC rate of 1M RPC/s.
2418 * If the time is clearly incorrect, we instead use a 62-bit random number.
2419 * In the worst case the random number will overflow 1M RPCs per second in
2420 * 9133 years, or permutations thereof.
2422 #define YEAR_2004 (1ULL << 30)
2423 void ptlrpc_init_xid(void)
2425 time_t now = cfs_time_current_sec();
2427 spin_lock_init(&ptlrpc_last_xid_lock);
2428 if (now < YEAR_2004) {
2429 ll_get_random_bytes(&ptlrpc_last_xid, sizeof(ptlrpc_last_xid));
2430 ptlrpc_last_xid >>= 2;
2431 ptlrpc_last_xid |= (1ULL << 61);
2433 ptlrpc_last_xid = (__u64)now << 20;
2437 __u64 ptlrpc_next_xid(void)
2440 spin_lock(&ptlrpc_last_xid_lock);
2441 tmp = ++ptlrpc_last_xid;
2442 spin_unlock(&ptlrpc_last_xid_lock);
2446 __u64 ptlrpc_sample_next_xid(void)
2448 if (sizeof(long) < 8) {
2449 /* need to avoid possible word tearing on 32-bit systems */
2451 spin_lock(&ptlrpc_last_xid_lock);
2452 tmp = ptlrpc_last_xid + 1;
2453 spin_unlock(&ptlrpc_last_xid_lock);
2456 /* No need to lock, since returned value is racy anyways */
2457 return ptlrpc_last_xid + 1;
2459 EXPORT_SYMBOL(ptlrpc_sample_next_xid);