1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_RPC
27 #include <liblustre.h>
30 #include <linux/obd_support.h>
31 #include <linux/obd_class.h>
32 #include <linux/lustre_lib.h>
33 #include <linux/lustre_ha.h>
34 #include <linux/lustre_import.h>
35 #include <linux/lustre_sec.h>
37 #include "ptlrpc_internal.h"
39 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
40 struct ptlrpc_client *cl)
42 cl->cli_request_portal = req_portal;
43 cl->cli_reply_portal = rep_portal;
47 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
49 struct ptlrpc_connection *c;
50 struct ptlrpc_peer peer;
53 err = ptlrpc_uuid_to_peer(uuid, &peer);
55 CERROR("cannot find peer %s!\n", uuid->uuid);
59 c = ptlrpc_get_connection(&peer, uuid);
61 memcpy(c->c_remote_uuid.uuid,
62 uuid->uuid, sizeof(c->c_remote_uuid.uuid));
65 CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
70 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn,
71 struct obd_uuid *uuid)
73 struct ptlrpc_peer peer;
76 err = ptlrpc_uuid_to_peer(uuid, &peer);
78 CERROR("cannot find peer %s!\n", uuid->uuid);
82 memcpy(&conn->c_peer, &peer, sizeof (peer));
86 static inline struct ptlrpc_bulk_desc *new_bulk(int npages, int type, int portal)
88 struct ptlrpc_bulk_desc *desc;
90 OBD_ALLOC(desc, offsetof (struct ptlrpc_bulk_desc, bd_iov[npages]));
94 spin_lock_init(&desc->bd_lock);
95 init_waitqueue_head(&desc->bd_waitq);
96 desc->bd_max_iov = npages;
97 desc->bd_iov_count = 0;
98 desc->bd_md_h = PTL_INVALID_HANDLE;
99 desc->bd_portal = portal;
100 desc->bd_type = type;
105 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
106 int npages, int type, int portal)
108 struct obd_import *imp = req->rq_import;
109 struct ptlrpc_bulk_desc *desc;
111 LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
112 desc = new_bulk(npages, type, portal);
116 desc->bd_import_generation = req->rq_import_generation;
117 desc->bd_import = class_import_get(imp);
120 desc->bd_cbid.cbid_fn = client_bulk_callback;
121 desc->bd_cbid.cbid_arg = desc;
123 /* This makes req own desc, and free it when she frees herself */
129 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp (struct ptlrpc_request *req,
130 int npages, int type, int portal)
132 struct obd_export *exp = req->rq_export;
133 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 struct page *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 <= PAGE_SIZE);
164 ptlrpc_add_bulk_page(desc, page, pageoffset, len);
167 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
171 LASSERT(desc != NULL);
172 LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
173 LASSERT(!desc->bd_network_rw); /* network hands off or */
174 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
176 class_export_put(desc->bd_export);
178 class_import_put(desc->bd_import);
180 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
181 bd_iov[desc->bd_max_iov]));
185 /* FIXME prep_req now should return error code other than NULL. but
186 * this is called everywhere :(
188 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, __u32 version,
189 int opcode, int count, int *lengths,
192 struct ptlrpc_request *request;
196 LASSERT((unsigned long)imp > 0x1000);
198 OBD_ALLOC(request, sizeof(*request));
200 CERROR("request allocation out of memory\n");
204 request->rq_import = class_import_get(imp);
206 rc = ptlrpcs_req_get_cred(request);
208 CDEBUG(D_SEC, "failed to get credential\n");
212 /* try to refresh the cred. we do this here in order to let fewer
213 * refresh be performed in ptlrpcd context (which might block ptlrpcd).
214 * fail out only if a fatal ptlrpcs error occured.
216 ptlrpcs_req_refresh_cred(request);
217 if (request->rq_ptlrpcs_err)
218 GOTO(out_cred, rc = -EPERM);
220 /* set default sec flavor for this req. in the future we might need
221 * increase security strengh, e.g. AUTH -> PRIV
223 request->rq_req_secflvr = imp->imp_sec->ps_flavor;
225 rc = lustre_pack_request(request, count, lengths, bufs);
227 CERROR("cannot pack request %d\n", rc);
230 request->rq_reqmsg->version |= version;
232 if (imp->imp_server_timeout)
233 request->rq_timeout = obd_timeout / 2;
235 request->rq_timeout = obd_timeout;
237 request->rq_send_state = LUSTRE_IMP_FULL;
238 request->rq_type = PTL_RPC_MSG_REQUEST;
240 request->rq_req_cbid.cbid_fn = request_out_callback;
241 request->rq_req_cbid.cbid_arg = request;
243 request->rq_reply_cbid.cbid_fn = reply_in_callback;
244 request->rq_reply_cbid.cbid_arg = request;
246 request->rq_phase = RQ_PHASE_NEW;
248 /* XXX FIXME bug 249 */
249 request->rq_request_portal = imp->imp_client->cli_request_portal;
250 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
252 spin_lock_init(&request->rq_lock);
253 INIT_LIST_HEAD(&request->rq_list);
254 INIT_LIST_HEAD(&request->rq_replay_list);
255 INIT_LIST_HEAD(&request->rq_set_chain);
256 init_waitqueue_head(&request->rq_reply_waitq);
257 request->rq_xid = ptlrpc_next_xid();
258 atomic_set(&request->rq_refcount, 1);
260 request->rq_reqmsg->opc = opcode;
261 request->rq_reqmsg->flags = 0;
264 ptlrpcs_req_drop_cred(request);
266 class_import_put(imp);
267 OBD_FREE(request, sizeof(*request));
271 struct ptlrpc_request_set *ptlrpc_prep_set(void)
273 struct ptlrpc_request_set *set;
275 OBD_ALLOC(set, sizeof *set);
278 INIT_LIST_HEAD(&set->set_requests);
279 init_waitqueue_head(&set->set_waitq);
280 set->set_remaining = 0;
281 spin_lock_init(&set->set_new_req_lock);
282 INIT_LIST_HEAD(&set->set_new_requests);
287 /* Finish with this set; opposite of prep_set. */
288 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
290 struct list_head *tmp;
291 struct list_head *next;
296 /* Requests on the set should either all be completed, or all be new */
297 expected_phase = (set->set_remaining == 0) ?
298 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
299 list_for_each (tmp, &set->set_requests) {
300 struct ptlrpc_request *req =
301 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
303 LASSERT(req->rq_phase == expected_phase);
307 LASSERT(set->set_remaining == 0 || set->set_remaining == n);
309 list_for_each_safe(tmp, next, &set->set_requests) {
310 struct ptlrpc_request *req =
311 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
312 list_del_init(&req->rq_set_chain);
314 LASSERT(req->rq_phase == expected_phase);
316 if (req->rq_phase == RQ_PHASE_NEW) {
318 if (req->rq_interpret_reply != NULL) {
319 int (*interpreter)(struct ptlrpc_request *,
321 req->rq_interpret_reply;
323 /* higher level (i.e. LOV) failed;
324 * let the sub reqs clean up */
325 req->rq_status = -EBADR;
326 interpreter(req, &req->rq_async_args,
329 set->set_remaining--;
333 ptlrpc_req_finished (req);
336 LASSERT(set->set_remaining == 0);
338 OBD_FREE(set, sizeof(*set));
342 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
343 struct ptlrpc_request *req)
345 /* The set takes over the caller's request reference */
346 list_add_tail(&req->rq_set_chain, &set->set_requests);
348 set->set_remaining++;
349 atomic_inc(&req->rq_import->imp_inflight);
352 /* lock so many callers can add things, the context that owns the set
353 * is supposed to notice these and move them into the set proper. */
354 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *set,
355 struct ptlrpc_request *req)
358 spin_lock_irqsave(&set->set_new_req_lock, flags);
359 /* The set takes over the caller's request reference */
360 list_add_tail(&req->rq_set_chain, &set->set_new_requests);
362 spin_unlock_irqrestore(&set->set_new_req_lock, flags);
366 * Based on the current state of the import, determine if the request
367 * can be sent, is an error, or should be delayed.
369 * Returns true if this request should be delayed. If false, and
370 * *status is set, then the request can not be sent and *status is the
371 * error code. If false and status is 0, then request can be sent.
373 * The imp->imp_lock must be held.
375 static int ptlrpc_import_delay_req(struct obd_import *imp,
376 struct ptlrpc_request *req, int *status)
381 LASSERT (status != NULL);
384 if (imp->imp_state == LUSTRE_IMP_NEW) {
385 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
389 else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
390 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
393 /* allow CONNECT even if import is invalid */
394 else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
395 imp->imp_state == LUSTRE_IMP_CONNECTING) {
399 * If the import has been invalidated (such as by an OST failure), the
400 * request must fail with -EIO.
402 else if (imp->imp_invalid) {
403 DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
406 else if (req->rq_import_generation != imp->imp_generation) {
407 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
410 else if (req->rq_send_state != imp->imp_state) {
411 if (imp->imp_obd->obd_no_recov || imp->imp_dlm_fake
413 *status = -EWOULDBLOCK;
421 static int ptlrpc_check_reply(struct ptlrpc_request *req)
427 /* serialise with network callback */
428 spin_lock_irqsave (&req->rq_lock, flags);
430 if (req->rq_replied) {
431 DEBUG_REQ(D_NET, req, "REPLIED:");
435 if (req->rq_net_err && !req->rq_timedout) {
436 spin_unlock_irqrestore (&req->rq_lock, flags);
437 rc = ptlrpc_expire_one_request(req);
438 spin_lock_irqsave (&req->rq_lock, flags);
443 DEBUG_REQ(D_ERROR, req, "ABORTED:");
447 if (req->rq_resend) {
448 DEBUG_REQ(D_ERROR, req, "RESEND:");
452 if (req->rq_restart) {
453 DEBUG_REQ(D_ERROR, req, "RESTART:");
458 spin_unlock_irqrestore (&req->rq_lock, flags);
459 DEBUG_REQ(D_NET, req, "rc = %d for", rc);
463 static int ptlrpc_check_status(struct ptlrpc_request *req)
468 err = req->rq_repmsg->status;
469 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
470 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err == %d",
472 RETURN(err < 0 ? err : -EINVAL);
476 DEBUG_REQ(D_INFO, req, "status is %d", err);
477 } else if (err > 0) {
478 /* XXX: translate this error from net to host */
479 DEBUG_REQ(D_INFO, req, "status is %d", err);
485 static int after_reply(struct ptlrpc_request *req)
488 struct obd_import *imp = req->rq_import;
492 LASSERT(!req->rq_receiving_reply);
494 /* NB Until this point, the whole of the incoming message,
495 * including buflens, status etc is in the sender's byte order. */
498 /* Clear reply swab mask; this is a new reply in sender's byte order */
499 req->rq_rep_swab_mask = 0;
501 LASSERT (req->rq_nob_received <= req->rq_repbuf_len);
502 rc = ptlrpcs_cli_unwrap_reply(req);
504 CERROR("verify reply error: %d\n", rc);
507 /* unwrap_reply may request rpc be resend */
508 if (req->rq_ptlrpcs_restart) {
513 /* unwrap_reply will set rq_replen as the actual received
516 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
518 CERROR("unpack_rep failed: %d\n", rc);
522 if (req->rq_repmsg->type != PTL_RPC_MSG_REPLY &&
523 req->rq_repmsg->type != PTL_RPC_MSG_ERR) {
524 CERROR("invalid packet type received (type=%u)\n",
525 req->rq_repmsg->type);
529 rc = ptlrpc_check_status(req);
531 /* Either we've been evicted, or the server has failed for
532 * some reason. Try to reconnect, and if that fails, punt to the
534 if (rc == -ENOTCONN) {
535 if (req->rq_send_state != LUSTRE_IMP_FULL ||
536 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
540 ptlrpc_request_handle_notconn(req);
545 /* Store transno in reqmsg for replay. */
546 req->rq_reqmsg->transno = req->rq_transno = req->rq_repmsg->transno;
548 if (req->rq_import->imp_replayable) {
549 spin_lock_irqsave(&imp->imp_lock, flags);
550 if (req->rq_transno != 0)
551 ptlrpc_retain_replayable_request(req, imp);
552 else if (req->rq_commit_cb != NULL) {
553 spin_unlock_irqrestore(&imp->imp_lock, flags);
554 req->rq_commit_cb(req);
555 spin_lock_irqsave(&imp->imp_lock, flags);
558 if (req->rq_transno > imp->imp_max_transno)
559 imp->imp_max_transno = req->rq_transno;
561 /* Replay-enabled imports return commit-status information. */
562 if (req->rq_repmsg->last_committed)
563 imp->imp_peer_committed_transno =
564 req->rq_repmsg->last_committed;
565 ptlrpc_free_committed(imp);
566 spin_unlock_irqrestore(&imp->imp_lock, flags);
572 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
574 char str[PTL_NALFMT_SIZE];
575 struct obd_import *imp;
580 LASSERT(req->rq_phase == RQ_PHASE_NEW);
581 req->rq_phase = RQ_PHASE_RPC;
583 imp = req->rq_import;
584 spin_lock_irqsave(&imp->imp_lock, flags);
586 req->rq_import_generation = imp->imp_generation;
588 if (ptlrpc_import_delay_req(imp, req, &rc)) {
589 spin_lock (&req->rq_lock);
591 spin_unlock (&req->rq_lock);
593 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
595 req->rq_reqmsg->status,
596 ptlrpc_import_state_name(req->rq_send_state),
597 ptlrpc_import_state_name(imp->imp_state));
598 LASSERT(list_empty (&req->rq_list));
600 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
601 spin_unlock_irqrestore(&imp->imp_lock, flags);
606 spin_unlock_irqrestore(&imp->imp_lock, flags);
608 req->rq_phase = RQ_PHASE_INTERPRET;
612 /* XXX this is the same as ptlrpc_queue_wait */
613 LASSERT(list_empty(&req->rq_list));
614 list_add_tail(&req->rq_list, &imp->imp_sending_list);
615 spin_unlock_irqrestore(&imp->imp_lock, flags);
617 req->rq_reqmsg->status = current->pid;
618 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:ni:nid:opc"
619 " %s:%s:%d:"LPU64":%s:%s:%d\n", current->comm,
620 imp->imp_obd->obd_uuid.uuid, req->rq_reqmsg->status,
622 imp->imp_connection->c_peer.peer_ni->pni_name,
623 ptlrpc_peernid2str(&imp->imp_connection->c_peer, str),
624 req->rq_reqmsg->opc);
626 rc = ptl_send_rpc(req);
628 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
635 int ptlrpc_check_set(struct ptlrpc_request_set *set)
637 char str[PTL_NALFMT_SIZE];
639 struct list_head *tmp;
640 int force_timer_recalc = 0;
643 if (set->set_remaining == 0)
646 list_for_each(tmp, &set->set_requests) {
647 struct ptlrpc_request *req =
648 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
649 struct obd_import *imp = req->rq_import;
652 if (req->rq_phase == RQ_PHASE_NEW &&
653 ptlrpc_send_new_req(req)) {
654 force_timer_recalc = 1;
657 if (!(req->rq_phase == RQ_PHASE_RPC ||
658 req->rq_phase == RQ_PHASE_BULK ||
659 req->rq_phase == RQ_PHASE_INTERPRET ||
660 req->rq_phase == RQ_PHASE_COMPLETE)) {
661 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
665 if (req->rq_phase == RQ_PHASE_COMPLETE)
668 if (req->rq_phase == RQ_PHASE_INTERPRET)
669 GOTO(interpret, req->rq_status);
671 if (req->rq_net_err && !req->rq_timedout)
672 ptlrpc_expire_one_request(req);
674 if (req->rq_err || req->rq_ptlrpcs_err) {
675 ptlrpc_unregister_reply(req);
676 if (req->rq_status == 0)
677 req->rq_status = req->rq_err ? -EIO : -EPERM;
678 req->rq_phase = RQ_PHASE_INTERPRET;
680 spin_lock_irqsave(&imp->imp_lock, flags);
681 list_del_init(&req->rq_list);
682 spin_unlock_irqrestore(&imp->imp_lock, flags);
684 GOTO(interpret, req->rq_status);
687 /* ptlrpc_queue_wait->l_wait_event guarantees that rq_intr
688 * will only be set after rq_timedout, but the oig waiting
689 * path sets rq_intr irrespective of whether ptlrpcd has
690 * seen a timeout. our policy is to only interpret
691 * interrupted rpcs after they have timed out */
692 if (req->rq_intr && (req->rq_timedout || req->rq_waiting)) {
693 /* NB could be on delayed list */
694 ptlrpc_unregister_reply(req);
695 req->rq_status = -EINTR;
696 req->rq_phase = RQ_PHASE_INTERPRET;
698 spin_lock_irqsave(&imp->imp_lock, flags);
699 list_del_init(&req->rq_list);
700 spin_unlock_irqrestore(&imp->imp_lock, flags);
702 GOTO(interpret, req->rq_status);
705 if (req->rq_phase == RQ_PHASE_RPC) {
706 if (req->rq_timedout||req->rq_waiting||req->rq_resend) {
709 ptlrpc_unregister_reply(req);
711 spin_lock_irqsave(&imp->imp_lock, flags);
713 if (ptlrpc_import_delay_req(imp, req, &status)){
714 spin_unlock_irqrestore(&imp->imp_lock,
719 list_del_init(&req->rq_list);
721 req->rq_status = status;
722 req->rq_phase = RQ_PHASE_INTERPRET;
723 spin_unlock_irqrestore(&imp->imp_lock,
725 GOTO(interpret, req->rq_status);
727 if (req->rq_no_resend) {
728 req->rq_status = -ENOTCONN;
729 req->rq_phase = RQ_PHASE_INTERPRET;
730 spin_unlock_irqrestore(&imp->imp_lock,
732 GOTO(interpret, req->rq_status);
734 list_add_tail(&req->rq_list,
735 &imp->imp_sending_list);
737 spin_unlock_irqrestore(&imp->imp_lock, flags);
740 if (req->rq_resend) {
741 if (!req->rq_ptlrpcs_restart)
742 lustre_msg_add_flags(
746 __u64 old_xid = req->rq_xid;
748 ptlrpc_unregister_bulk (req);
750 /* ensure previous bulk fails */
751 req->rq_xid = ptlrpc_next_xid();
752 CDEBUG(D_HA, "resend bulk "
755 old_xid, req->rq_xid);
759 rc = ptl_send_rpc(req);
761 DEBUG_REQ(D_HA, req, "send failed (%d)",
763 force_timer_recalc = 1;
766 /* need to reset the timeout */
767 force_timer_recalc = 1;
770 /* Still waiting for a reply? */
771 if (ptlrpc_client_receiving_reply(req))
774 /* Did we actually receive a reply? */
775 if (!ptlrpc_client_replied(req))
778 spin_lock_irqsave(&imp->imp_lock, flags);
779 list_del_init(&req->rq_list);
780 spin_unlock_irqrestore(&imp->imp_lock, flags);
782 req->rq_status = after_reply(req);
783 if (req->rq_resend) {
784 /* Add this req to the delayed list so
785 it can be errored if the import is
786 evicted after recovery. */
787 spin_lock_irqsave (&req->rq_lock, flags);
788 list_add_tail(&req->rq_list,
789 &imp->imp_delayed_list);
790 spin_unlock_irqrestore(&req->rq_lock, flags);
794 /* If there is no bulk associated with this request,
795 * then we're done and should let the interpreter
796 * process the reply. Similarly if the RPC returned
797 * an error, and therefore the bulk will never arrive.
799 if (req->rq_bulk == NULL || req->rq_status != 0) {
800 req->rq_phase = RQ_PHASE_INTERPRET;
801 GOTO(interpret, req->rq_status);
804 req->rq_phase = RQ_PHASE_BULK;
807 LASSERT(req->rq_phase == RQ_PHASE_BULK);
808 if (ptlrpc_bulk_active(req->rq_bulk))
811 if (!req->rq_bulk->bd_success) {
812 /* The RPC reply arrived OK, but the bulk screwed
813 * up! Dead wierd since the server told us the RPC
814 * was good after getting the REPLY for her GET or
815 * the ACK for her PUT. */
816 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
820 req->rq_phase = RQ_PHASE_INTERPRET;
823 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
824 LASSERT(!req->rq_receiving_reply);
826 ptlrpc_unregister_reply(req);
827 if (req->rq_bulk != NULL)
828 ptlrpc_unregister_bulk (req);
830 req->rq_phase = RQ_PHASE_COMPLETE;
832 if (req->rq_interpret_reply != NULL) {
833 int (*interpreter)(struct ptlrpc_request *,void *,int) =
834 req->rq_interpret_reply;
835 req->rq_status = interpreter(req, &req->rq_async_args,
839 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:ni:nid:"
840 "opc %s:%s:%d:"LPU64":%s:%s:%d\n", current->comm,
841 imp->imp_obd->obd_uuid.uuid, req->rq_reqmsg->status,
843 imp->imp_connection->c_peer.peer_ni->pni_name,
844 ptlrpc_peernid2str(&imp->imp_connection->c_peer, str),
845 req->rq_reqmsg->opc);
847 set->set_remaining--;
849 atomic_dec(&imp->imp_inflight);
850 wake_up(&imp->imp_recovery_waitq);
853 /* If we hit an error, we want to recover promptly. */
854 RETURN(set->set_remaining == 0 || force_timer_recalc);
857 int ptlrpc_expire_one_request(struct ptlrpc_request *req)
860 struct obd_import *imp = req->rq_import;
864 DEBUG_REQ(D_ERROR, req, "timeout (sent at %lu, %lus ago)",
865 (long)req->rq_sent, LTIME_S(CURRENT_TIME) - req->rq_sent);
867 spin_lock_irqsave (&req->rq_lock, flags);
868 replied = req->rq_replied;
870 req->rq_timedout = 1;
871 spin_unlock_irqrestore (&req->rq_lock, flags);
876 ptlrpc_unregister_reply (req);
878 if (obd_dump_on_timeout)
879 portals_debug_dumplog();
881 if (req->rq_bulk != NULL)
882 ptlrpc_unregister_bulk (req);
885 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
889 /* The DLM server doesn't want recovery run on its imports. */
890 if (imp->imp_dlm_fake)
893 /* If this request is for recovery or other primordial tasks,
894 * then error it out here. */
895 if (req->rq_send_state != LUSTRE_IMP_FULL ||
896 imp->imp_obd->obd_no_recov) {
897 spin_lock_irqsave (&req->rq_lock, flags);
898 req->rq_status = -ETIMEDOUT;
900 spin_unlock_irqrestore (&req->rq_lock, flags);
904 ptlrpc_fail_import(imp, req->rq_import_generation);
909 int ptlrpc_expired_set(void *data)
911 struct ptlrpc_request_set *set = data;
912 struct list_head *tmp;
913 time_t now = LTIME_S(CURRENT_TIME);
916 LASSERT(set != NULL);
918 /* A timeout expired; see which reqs it applies to... */
919 list_for_each (tmp, &set->set_requests) {
920 struct ptlrpc_request *req =
921 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
923 /* request in-flight? */
924 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting
925 && !req->rq_resend) ||
926 (req->rq_phase == RQ_PHASE_BULK)))
929 if (req->rq_timedout || /* already dealt with */
930 req->rq_sent + req->rq_timeout > now) /* not expired */
933 /* deal with this guy */
934 ptlrpc_expire_one_request (req);
937 /* When waiting for a whole set, we always to break out of the
938 * sleep so we can recalculate the timeout, or enable interrupts
939 * iff everyone's timed out.
944 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
947 spin_lock_irqsave(&req->rq_lock, flags);
949 spin_unlock_irqrestore(&req->rq_lock, flags);
952 void ptlrpc_interrupted_set(void *data)
954 struct ptlrpc_request_set *set = data;
955 struct list_head *tmp;
957 LASSERT(set != NULL);
958 CERROR("INTERRUPTED SET %p\n", set);
960 list_for_each(tmp, &set->set_requests) {
961 struct ptlrpc_request *req =
962 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
964 if (req->rq_phase != RQ_PHASE_RPC)
967 ptlrpc_mark_interrupted(req);
971 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
973 struct list_head *tmp;
974 time_t now = LTIME_S(CURRENT_TIME);
977 struct ptlrpc_request *req;
980 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
982 list_for_each(tmp, &set->set_requests) {
983 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
985 /* request in-flight? */
986 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting) ||
987 (req->rq_phase == RQ_PHASE_BULK)))
990 if (req->rq_timedout) /* already timed out */
993 deadline = req->rq_sent + req->rq_timeout;
994 if (deadline <= now) /* actually expired already */
995 timeout = 1; /* ASAP */
996 else if (timeout == 0 || timeout > deadline - now)
997 timeout = deadline - now;
1003 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
1005 struct list_head *tmp;
1006 struct ptlrpc_request *req;
1007 struct l_wait_info lwi;
1011 LASSERT(!list_empty(&set->set_requests));
1012 list_for_each(tmp, &set->set_requests) {
1013 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1014 if (req->rq_phase == RQ_PHASE_NEW)
1015 (void)ptlrpc_send_new_req(req);
1019 timeout = ptlrpc_set_next_timeout(set);
1021 /* wait until all complete, interrupted, or an in-flight
1023 CDEBUG(D_HA, "set %p going to sleep for %d seconds\n",
1025 lwi = LWI_TIMEOUT_INTR((timeout ? timeout : 1) * HZ,
1027 ptlrpc_interrupted_set, set);
1028 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(set), &lwi);
1030 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
1032 /* -EINTR => all requests have been flagged rq_intr so next
1034 * -ETIMEOUTD => someone timed out. When all reqs have
1035 * timed out, signals are enabled allowing completion with
1037 * I don't really care if we go once more round the loop in
1038 * the error cases -eeb. */
1039 } while (rc != 0 || set->set_remaining != 0);
1041 LASSERT(set->set_remaining == 0);
1044 list_for_each(tmp, &set->set_requests) {
1045 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1047 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
1048 if (req->rq_status != 0)
1049 rc = req->rq_status;
1052 if (set->set_interpret != NULL) {
1053 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
1055 rc = interpreter (set, set->set_arg, rc);
1061 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
1064 if (request == NULL) {
1069 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
1070 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
1071 LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
1072 LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
1073 LASSERT(request->rq_cred);
1075 /* We must take it off the imp_replay_list first. Otherwise, we'll set
1076 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
1077 if (request->rq_import != NULL) {
1078 unsigned long flags = 0;
1080 spin_lock_irqsave(&request->rq_import->imp_lock, flags);
1081 list_del_init(&request->rq_replay_list);
1083 spin_unlock_irqrestore(&request->rq_import->imp_lock,
1086 LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
1088 if (atomic_read(&request->rq_refcount) != 0) {
1089 DEBUG_REQ(D_ERROR, request,
1090 "freeing request with nonzero refcount");
1094 if (request->rq_repbuf != NULL)
1095 ptlrpcs_cli_free_repbuf(request);
1096 if (request->rq_reqbuf != NULL)
1097 ptlrpcs_cli_free_reqbuf(request);
1099 if (request->rq_export != NULL) {
1100 class_export_put(request->rq_export);
1101 request->rq_export = NULL;
1103 if (request->rq_import != NULL) {
1104 class_import_put(request->rq_import);
1105 request->rq_import = NULL;
1107 if (request->rq_bulk != NULL)
1108 ptlrpc_free_bulk(request->rq_bulk);
1110 ptlrpcs_req_drop_cred(request);
1111 OBD_FREE(request, sizeof(*request));
1115 void ptlrpc_free_req(struct ptlrpc_request *request)
1117 __ptlrpc_free_req(request, 0);
1120 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
1121 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
1123 LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
1124 (void)__ptlrpc_req_finished(request, 1);
1127 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
1130 if (request == NULL)
1133 if (request == LP_POISON ||
1134 request->rq_reqmsg == LP_POISON) {
1135 CERROR("dereferencing freed request (bug 575)\n");
1140 DEBUG_REQ(D_INFO, request, "refcount now %u",
1141 atomic_read(&request->rq_refcount) - 1);
1143 if (atomic_dec_and_test(&request->rq_refcount)) {
1144 __ptlrpc_free_req(request, locked);
1151 void ptlrpc_req_finished(struct ptlrpc_request *request)
1153 __ptlrpc_req_finished(request, 0);
1156 /* Disengage the client's reply buffer from the network
1157 * NB does _NOT_ unregister any client-side bulk.
1158 * IDEMPOTENT, but _not_ safe against concurrent callers.
1159 * The request owner (i.e. the thread doing the I/O) must call...
1161 void ptlrpc_unregister_reply (struct ptlrpc_request *request)
1164 wait_queue_head_t *wq;
1165 struct l_wait_info lwi;
1167 LASSERT(!in_interrupt ()); /* might sleep */
1169 if (!ptlrpc_client_receiving_reply(request))
1172 PtlMDUnlink (request->rq_reply_md_h);
1174 /* We have to l_wait_event() whatever the result, to give liblustre
1175 * a chance to run reply_in_callback() */
1177 if (request->rq_set != NULL)
1178 wq = &request->rq_set->set_waitq;
1180 wq = &request->rq_reply_waitq;
1183 /* Network access will complete in finite time but the HUGE
1184 * timeout lets us CWARN for visibility of sluggish NALs */
1185 lwi = LWI_TIMEOUT(300 * HZ, NULL, NULL);
1186 rc = l_wait_event (*wq, !ptlrpc_client_receiving_reply(request), &lwi);
1190 LASSERT (rc == -ETIMEDOUT);
1191 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout");
1195 /* caller must hold imp->imp_lock */
1196 void ptlrpc_free_committed(struct obd_import *imp)
1198 struct list_head *tmp, *saved;
1199 struct ptlrpc_request *req;
1200 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1203 LASSERT(imp != NULL);
1205 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1207 CDEBUG(D_HA, "%s: committing for last_committed "LPU64"\n",
1208 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1210 list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1211 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1213 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
1214 LASSERT(req != last_req);
1217 if (req->rq_import_generation < imp->imp_generation) {
1218 DEBUG_REQ(D_HA, req, "freeing request with old gen");
1222 if (req->rq_replay) {
1223 /* this is debug flag to avoid flooding of logs -bzzz */
1224 if (imp->imp_debug_open_replays)
1225 DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
1229 /* not yet committed */
1230 if (req->rq_transno > imp->imp_peer_committed_transno) {
1231 DEBUG_REQ(D_HA, req, "stopping search");
1235 DEBUG_REQ(D_HA, req, "committing (last_committed "LPU64")",
1236 imp->imp_peer_committed_transno);
1238 if (req->rq_commit_cb != NULL)
1239 req->rq_commit_cb(req);
1240 list_del_init(&req->rq_replay_list);
1241 __ptlrpc_req_finished(req, 1);
1243 imp->imp_debug_open_replays = 0;
1249 void ptlrpc_cleanup_client(struct obd_import *imp)
1256 void ptlrpc_resend_req(struct ptlrpc_request *req)
1258 unsigned long flags;
1260 DEBUG_REQ(D_HA, req, "going to resend");
1261 req->rq_reqmsg->handle.cookie = 0;
1262 req->rq_status = -EAGAIN;
1264 spin_lock_irqsave (&req->rq_lock, flags);
1266 req->rq_net_err = 0;
1267 req->rq_timedout = 0;
1269 __u64 old_xid = req->rq_xid;
1271 /* ensure previous bulk fails */
1272 req->rq_xid = ptlrpc_next_xid();
1273 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
1274 old_xid, req->rq_xid);
1276 ptlrpc_wake_client_req(req);
1277 spin_unlock_irqrestore (&req->rq_lock, flags);
1280 /* XXX: this function and rq_status are currently unused */
1281 void ptlrpc_restart_req(struct ptlrpc_request *req)
1283 unsigned long flags;
1285 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1286 req->rq_status = -ERESTARTSYS;
1288 spin_lock_irqsave (&req->rq_lock, flags);
1289 req->rq_restart = 1;
1290 req->rq_timedout = 0;
1291 ptlrpc_wake_client_req(req);
1292 spin_unlock_irqrestore (&req->rq_lock, flags);
1295 static int expired_request(void *data)
1297 struct ptlrpc_request *req = data;
1300 /* some failure can suspend regular timeouts */
1301 if (ptlrpc_check_suspend())
1304 RETURN(ptlrpc_expire_one_request(req));
1307 static void interrupted_request(void *data)
1309 unsigned long flags;
1311 struct ptlrpc_request *req = data;
1312 DEBUG_REQ(D_HA, req, "request interrupted");
1313 spin_lock_irqsave (&req->rq_lock, flags);
1315 spin_unlock_irqrestore (&req->rq_lock, flags);
1318 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1321 atomic_inc(&req->rq_refcount);
1325 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1326 struct obd_import *imp)
1328 struct list_head *tmp;
1330 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1332 /* clear this for new requests that were resent as well
1333 as resent replayed requests. */
1334 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1336 /* don't re-add requests that have been replayed */
1337 if (!list_empty(&req->rq_replay_list))
1340 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
1342 LASSERT(imp->imp_replayable);
1343 /* Balanced in ptlrpc_free_committed, usually. */
1344 ptlrpc_request_addref(req);
1345 list_for_each_prev(tmp, &imp->imp_replay_list) {
1346 struct ptlrpc_request *iter =
1347 list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1349 /* We may have duplicate transnos if we create and then
1350 * open a file, or for closes retained if to match creating
1351 * opens, so use req->rq_xid as a secondary key.
1352 * (See bugs 684, 685, and 428.)
1353 * XXX no longer needed, but all opens need transnos!
1355 if (iter->rq_transno > req->rq_transno)
1358 if (iter->rq_transno == req->rq_transno) {
1359 LASSERT(iter->rq_xid != req->rq_xid);
1360 if (iter->rq_xid > req->rq_xid)
1364 list_add(&req->rq_replay_list, &iter->rq_replay_list);
1368 list_add_tail(&req->rq_replay_list, &imp->imp_replay_list);
1371 int ptlrpc_queue_wait(struct ptlrpc_request *req)
1373 char str[PTL_NALFMT_SIZE];
1376 struct l_wait_info lwi;
1377 struct obd_import *imp = req->rq_import;
1378 unsigned long flags;
1382 LASSERT(req->rq_set == NULL);
1383 LASSERT(!req->rq_receiving_reply);
1384 atomic_inc(&imp->imp_inflight);
1386 if (imp->imp_connection == NULL) {
1387 CERROR("request on not connected import %s\n",
1388 imp->imp_obd->obd_name);
1392 /* for distributed debugging */
1393 req->rq_reqmsg->status = current->pid;
1394 LASSERT(imp->imp_obd != NULL);
1395 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:ni:nid:opc "
1396 "%s:%s:%d:"LPU64":%s:%s:%d\n", current->comm,
1397 imp->imp_obd->obd_uuid.uuid,
1398 req->rq_reqmsg->status, req->rq_xid,
1399 imp->imp_connection->c_peer.peer_ni->pni_name,
1400 ptlrpc_peernid2str(&imp->imp_connection->c_peer, str),
1401 req->rq_reqmsg->opc);
1403 /* Mark phase here for a little debug help */
1404 req->rq_phase = RQ_PHASE_RPC;
1406 spin_lock_irqsave(&imp->imp_lock, flags);
1407 req->rq_import_generation = imp->imp_generation;
1409 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1410 list_del(&req->rq_list);
1412 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1413 spin_unlock_irqrestore(&imp->imp_lock, flags);
1415 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%s != %s)",
1417 ptlrpc_import_state_name(req->rq_send_state),
1418 ptlrpc_import_state_name(imp->imp_state));
1419 lwi = LWI_INTR(interrupted_request, req);
1420 rc = l_wait_event(req->rq_reply_waitq,
1421 (req->rq_send_state == imp->imp_state ||
1424 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%s == %s or %d == 1)",
1426 ptlrpc_import_state_name(imp->imp_state),
1427 ptlrpc_import_state_name(req->rq_send_state),
1430 spin_lock_irqsave(&imp->imp_lock, flags);
1431 list_del_init(&req->rq_list);
1436 else if (req->rq_intr) {
1439 else if (req->rq_no_resend) {
1440 spin_unlock_irqrestore(&imp->imp_lock, flags);
1441 GOTO(out, rc = -ETIMEDOUT);
1449 list_del_init(&req->rq_list);
1450 spin_unlock_irqrestore(&imp->imp_lock, flags);
1451 req->rq_status = rc; // XXX this ok?
1455 if (req->rq_resend) {
1456 if (!req->rq_ptlrpcs_restart)
1457 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
1459 if (req->rq_bulk != NULL)
1460 ptlrpc_unregister_bulk (req);
1462 DEBUG_REQ(D_HA, req, "resending: ");
1465 /* XXX this is the same as ptlrpc_set_wait */
1466 LASSERT(list_empty(&req->rq_list));
1467 list_add_tail(&req->rq_list, &imp->imp_sending_list);
1468 spin_unlock_irqrestore(&imp->imp_lock, flags);
1470 rc = ptl_send_rpc(req);
1472 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
1475 timeout = MAX(req->rq_timeout * HZ, 1);
1476 DEBUG_REQ(D_NET, req, "-- sleeping for %d jiffies", timeout);
1479 lwi = LWI_TIMEOUT_INTR(timeout, expired_request, interrupted_request,
1481 rc = l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req), &lwi);
1482 if (rc == -ETIMEDOUT && ptlrpc_check_and_wait_suspend(req))
1484 DEBUG_REQ(D_NET, req, "-- done sleeping");
1486 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:ni:nid:opc "
1487 "%s:%s:%d:"LPU64":%s:%s:%d\n", current->comm,
1488 imp->imp_obd->obd_uuid.uuid,
1489 req->rq_reqmsg->status, req->rq_xid,
1490 imp->imp_connection->c_peer.peer_ni->pni_name,
1491 ptlrpc_peernid2str(&imp->imp_connection->c_peer, str),
1492 req->rq_reqmsg->opc);
1494 spin_lock_irqsave(&imp->imp_lock, flags);
1495 list_del_init(&req->rq_list);
1496 spin_unlock_irqrestore(&imp->imp_lock, flags);
1498 /* If the reply was received normally, this just grabs the spinlock
1499 * (ensuring the reply callback has returned), sees that
1500 * req->rq_receiving_reply is clear and returns. */
1501 ptlrpc_unregister_reply (req);
1504 GOTO(out, rc = -EIO);
1506 if (req->rq_ptlrpcs_err)
1507 GOTO(out, rc = -EPERM);
1509 /* Resend if we need to, unless we were interrupted. */
1510 if (req->rq_resend && !req->rq_intr) {
1511 /* ...unless we were specifically told otherwise. */
1512 if (req->rq_no_resend)
1513 GOTO(out, rc = -ETIMEDOUT);
1514 spin_lock_irqsave(&imp->imp_lock, flags);
1519 /* Should only be interrupted if we timed out. */
1520 if (!req->rq_timedout)
1521 DEBUG_REQ(D_ERROR, req,
1522 "rq_intr set but rq_timedout not");
1523 GOTO(out, rc = -EINTR);
1526 if (req->rq_timedout) { /* non-recoverable timeout */
1527 GOTO(out, rc = -ETIMEDOUT);
1530 if (!req->rq_replied) {
1531 /* How can this be? -eeb */
1532 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
1534 GOTO(out, rc = req->rq_status);
1537 rc = after_reply (req);
1538 /* NB may return +ve success rc */
1539 if (req->rq_resend) {
1540 spin_lock_irqsave(&imp->imp_lock, flags);
1545 if (req->rq_bulk != NULL) {
1547 /* success so far. Note that anything going wrong
1548 * with bulk now, is EXTREMELY strange, since the
1549 * server must have believed that the bulk
1550 * tranferred OK before she replied with success to
1552 lwi = LWI_TIMEOUT(timeout, NULL, NULL);
1553 brc = l_wait_event(req->rq_reply_waitq,
1554 !ptlrpc_bulk_active(req->rq_bulk),
1556 LASSERT(brc == 0 || brc == -ETIMEDOUT);
1558 LASSERT(brc == -ETIMEDOUT);
1559 DEBUG_REQ(D_ERROR, req, "bulk timed out");
1561 } else if (!req->rq_bulk->bd_success) {
1562 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1567 ptlrpc_unregister_bulk (req);
1570 LASSERT(!req->rq_receiving_reply);
1571 req->rq_phase = RQ_PHASE_INTERPRET;
1573 atomic_dec(&imp->imp_inflight);
1574 wake_up(&imp->imp_recovery_waitq);
1578 struct ptlrpc_replay_async_args {
1580 int praa_old_status;
1583 static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
1584 void * data, int rc)
1586 struct ptlrpc_replay_async_args *aa = data;
1587 struct obd_import *imp = req->rq_import;
1588 unsigned long flags;
1590 atomic_dec(&imp->imp_replay_inflight);
1592 if (!req->rq_replied) {
1593 CERROR("request replay timed out, restarting recovery\n");
1594 GOTO(out, rc = -ETIMEDOUT);
1598 /* Clear reply swab mask; this is a new reply in sender's byte order */
1599 req->rq_rep_swab_mask = 0;
1601 LASSERT (req->rq_nob_received <= req->rq_repbuf_len);
1602 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
1604 CERROR("unpack_rep failed: %d\n", rc);
1605 GOTO(out, rc = -EPROTO);
1608 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR &&
1609 req->rq_repmsg->status == -ENOTCONN)
1610 GOTO(out, rc = req->rq_repmsg->status);
1612 /* The transno had better not change over replay. */
1613 LASSERT(req->rq_reqmsg->transno == req->rq_repmsg->transno);
1615 DEBUG_REQ(D_HA, req, "got rep");
1617 /* let the callback do fixups, possibly including in the request */
1618 if (req->rq_replay_cb)
1619 req->rq_replay_cb(req);
1621 if (req->rq_replied && req->rq_repmsg->status != aa->praa_old_status) {
1622 DEBUG_REQ(D_HA, req, "status %d, old was %d",
1623 req->rq_repmsg->status, aa->praa_old_status);
1625 /* Put it back for re-replay. */
1626 req->rq_repmsg->status = aa->praa_old_status;
1629 spin_lock_irqsave(&imp->imp_lock, flags);
1630 imp->imp_last_replay_transno = req->rq_transno;
1631 spin_unlock_irqrestore(&imp->imp_lock, flags);
1633 /* continue with recovery */
1634 rc = ptlrpc_import_recovery_state_machine(imp);
1636 req->rq_send_state = aa->praa_old_state;
1639 /* this replay failed, so restart recovery */
1640 ptlrpc_connect_import(imp, NULL);
1646 int ptlrpc_replay_req(struct ptlrpc_request *req)
1648 struct ptlrpc_replay_async_args *aa;
1651 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
1653 /* Not handling automatic bulk replay yet (or ever?) */
1654 LASSERT(req->rq_bulk == NULL);
1656 DEBUG_REQ(D_HA, req, "REPLAY");
1658 LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
1659 aa = (struct ptlrpc_replay_async_args *)&req->rq_async_args;
1660 memset(aa, 0, sizeof *aa);
1662 /* Prepare request to be resent with ptlrpcd */
1663 aa->praa_old_state = req->rq_send_state;
1664 req->rq_send_state = LUSTRE_IMP_REPLAY;
1665 req->rq_phase = RQ_PHASE_NEW;
1666 aa->praa_old_status = req->rq_repmsg->status;
1669 req->rq_interpret_reply = ptlrpc_replay_interpret;
1670 atomic_inc(&req->rq_import->imp_replay_inflight);
1671 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
1673 ptlrpcd_add_req(req);
1677 void ptlrpc_abort_inflight(struct obd_import *imp)
1679 unsigned long flags;
1680 struct list_head *tmp, *n;
1683 /* Make sure that no new requests get processed for this import.
1684 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
1685 * this flag and then putting requests on sending_list or delayed_list.
1687 spin_lock_irqsave(&imp->imp_lock, flags);
1689 /* XXX locking? Maybe we should remove each request with the list
1690 * locked? Also, how do we know if the requests on the list are
1691 * being freed at this time?
1693 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
1694 struct ptlrpc_request *req =
1695 list_entry(tmp, struct ptlrpc_request, rq_list);
1697 DEBUG_REQ(D_HA, req, "inflight");
1699 spin_lock (&req->rq_lock);
1700 if (req->rq_import_generation < imp->imp_generation) {
1702 ptlrpc_wake_client_req(req);
1704 spin_unlock (&req->rq_lock);
1707 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
1708 struct ptlrpc_request *req =
1709 list_entry(tmp, struct ptlrpc_request, rq_list);
1711 DEBUG_REQ(D_HA, req, "aborting waiting req");
1713 spin_lock (&req->rq_lock);
1714 if (req->rq_import_generation < imp->imp_generation) {
1716 ptlrpc_wake_client_req(req);
1718 spin_unlock (&req->rq_lock);
1721 list_for_each_safe(tmp, n, &imp->imp_rawrpc_list) {
1722 struct ptlrpc_request *req =
1723 list_entry(tmp, struct ptlrpc_request, rq_list);
1725 DEBUG_REQ(D_HA, req, "aborting raw rpc");
1727 spin_lock(&req->rq_lock);
1729 ptlrpc_wake_client_req(req);
1730 spin_unlock(&req->rq_lock);
1733 /* Last chance to free reqs left on the replay list, but we
1734 * will still leak reqs that haven't comitted. */
1735 if (imp->imp_replayable)
1736 ptlrpc_free_committed(imp);
1738 spin_unlock_irqrestore(&imp->imp_lock, flags);
1743 static __u64 ptlrpc_last_xid = 0;
1744 static spinlock_t ptlrpc_last_xid_lock = SPIN_LOCK_UNLOCKED;
1746 __u64 ptlrpc_next_xid(void)
1749 spin_lock(&ptlrpc_last_xid_lock);
1750 tmp = ++ptlrpc_last_xid;
1751 spin_unlock(&ptlrpc_last_xid_lock);