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 the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 #define DEBUG_SUBSYSTEM S_RPC
30 #include <liblustre.h>
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/lustre_lib.h>
36 #include <linux/lustre_ha.h>
37 #include <linux/lustre_import.h>
39 #include "ptlrpc_internal.h"
41 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
42 struct ptlrpc_client *cl)
44 cl->cli_request_portal = req_portal;
45 cl->cli_reply_portal = rep_portal;
49 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
51 struct ptlrpc_connection *c;
53 lnet_process_id_t peer;
56 err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
58 CERROR("cannot find peer %s!\n", uuid->uuid);
62 c = ptlrpc_get_connection(peer, self, uuid);
64 memcpy(c->c_remote_uuid.uuid,
65 uuid->uuid, sizeof(c->c_remote_uuid.uuid));
68 CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
73 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn,
74 struct obd_uuid *uuid)
77 lnet_process_id_t peer;
80 err = ptlrpc_uuid_to_peer(uuid, &peer, &self);
82 CERROR("cannot find peer %s!\n", uuid->uuid);
91 static inline struct ptlrpc_bulk_desc *new_bulk(int npages, int type, int portal)
93 struct ptlrpc_bulk_desc *desc;
95 OBD_ALLOC(desc, offsetof (struct ptlrpc_bulk_desc, bd_iov[npages]));
99 spin_lock_init(&desc->bd_lock);
100 init_waitqueue_head(&desc->bd_waitq);
101 desc->bd_max_iov = npages;
102 desc->bd_iov_count = 0;
103 desc->bd_md_h = LNET_INVALID_HANDLE;
104 desc->bd_portal = portal;
105 desc->bd_type = type;
110 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
111 int npages, int type, int portal)
113 struct obd_import *imp = req->rq_import;
114 struct ptlrpc_bulk_desc *desc;
116 LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
117 desc = new_bulk(npages, type, portal);
121 desc->bd_import_generation = req->rq_import_generation;
122 desc->bd_import = class_import_get(imp);
125 desc->bd_cbid.cbid_fn = client_bulk_callback;
126 desc->bd_cbid.cbid_arg = desc;
128 /* This makes req own desc, and free it when she frees herself */
134 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp (struct ptlrpc_request *req,
135 int npages, int type, int portal)
137 struct obd_export *exp = req->rq_export;
138 struct ptlrpc_bulk_desc *desc;
140 LASSERT(type == BULK_PUT_SOURCE || type == BULK_GET_SINK);
142 desc = new_bulk(npages, type, portal);
146 desc->bd_export = class_export_get(exp);
149 desc->bd_cbid.cbid_fn = server_bulk_callback;
150 desc->bd_cbid.cbid_arg = desc;
152 /* NB we don't assign rq_bulk here; server-side requests are
153 * re-used, and the handler frees the bulk desc explicitly. */
158 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
159 struct page *page, int pageoffset, int len)
161 LASSERT(desc->bd_iov_count < desc->bd_max_iov);
162 LASSERT(page != NULL);
163 LASSERT(pageoffset >= 0);
165 LASSERT(pageoffset + len <= PAGE_SIZE);
169 ptlrpc_add_bulk_page(desc, page, pageoffset, len);
172 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
176 LASSERT(desc != NULL);
177 LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
178 LASSERT(!desc->bd_network_rw); /* network hands off or */
179 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
181 class_export_put(desc->bd_export);
183 class_import_put(desc->bd_import);
185 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
186 bd_iov[desc->bd_max_iov]));
190 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
192 struct list_head *l, *tmp;
193 struct ptlrpc_request *req;
198 list_for_each_safe(l, tmp, &pool->prp_req_list) {
199 req = list_entry(l, struct ptlrpc_request, rq_list);
200 list_del(&req->rq_list);
201 LASSERT (req->rq_reqmsg);
202 OBD_FREE(req->rq_reqmsg, pool->prp_rq_size);
203 OBD_FREE(req, sizeof(*req));
205 OBD_FREE(pool, sizeof(*pool));
208 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
213 while (size < pool->prp_rq_size)
216 LASSERTF(list_empty(&pool->prp_req_list) || size == pool->prp_rq_size,
217 "Trying to change pool size with nonempty pool "
218 "from %d to %d bytes\n", pool->prp_rq_size, size);
220 spin_lock(&pool->prp_lock);
221 pool->prp_rq_size = size;
222 for (i = 0; i < num_rq; i++) {
223 struct ptlrpc_request *req;
224 struct lustre_msg *msg;
225 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
228 OBD_ALLOC_GFP(msg, size, GFP_KERNEL);
230 OBD_FREE(req, sizeof(struct ptlrpc_request));
233 req->rq_reqmsg = msg;
235 list_add_tail(&req->rq_list, &pool->prp_req_list);
238 spin_unlock(&pool->prp_lock);
242 struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int num_rq, int msgsize,
243 void (*populate_pool)(struct ptlrpc_request_pool *, int))
245 struct ptlrpc_request_pool *pool;
247 OBD_ALLOC(pool, sizeof (struct ptlrpc_request_pool));
251 /* Request next power of two for the allocation, because internally
252 kernel would do exactly this */
254 spin_lock_init(&pool->prp_lock);
255 INIT_LIST_HEAD(&pool->prp_req_list);
256 pool->prp_rq_size = msgsize;
257 pool->prp_populate = populate_pool;
259 populate_pool(pool, num_rq);
261 if (list_empty(&pool->prp_req_list)) {
262 /* have not allocated a single request for the pool */
263 OBD_FREE(pool, sizeof (struct ptlrpc_request_pool));
269 static struct ptlrpc_request *ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
271 struct ptlrpc_request *request;
272 struct lustre_msg *reqmsg;
277 spin_lock(&pool->prp_lock);
279 /* See if we have anything in a pool, and bail out if nothing,
280 * in writeout path, where this matters, this is safe to do, because
281 * nothing is lost in this case, and when some in-flight requests
282 * complete, this code will be called again. */
283 if (unlikely(list_empty(&pool->prp_req_list))) {
284 spin_unlock(&pool->prp_lock);
288 request = list_entry(pool->prp_req_list.next, struct ptlrpc_request,
290 list_del(&request->rq_list);
291 spin_unlock(&pool->prp_lock);
293 LASSERT(request->rq_reqmsg);
294 LASSERT(request->rq_pool);
296 reqmsg = request->rq_reqmsg;
297 memset(request, 0, sizeof(*request));
298 request->rq_reqmsg = reqmsg;
299 request->rq_pool = pool;
300 request->rq_reqlen = pool->prp_rq_size;
304 struct ptlrpc_request *ptlrpc_prep_req_pool(struct obd_import *imp, int opcode,
305 int count, int *lengths,
307 struct ptlrpc_request_pool *pool)
309 struct ptlrpc_request *request = NULL;
313 LASSERT((unsigned long)imp > 0x1000);
314 LASSERT(imp != LP_POISON);
315 LASSERT((unsigned long)imp->imp_client > 0x1000);
316 LASSERT(imp->imp_client != LP_POISON);
319 request = ptlrpc_prep_req_from_pool(pool);
322 OBD_ALLOC(request, sizeof(*request));
325 CERROR("request allocation out of memory\n");
329 rc = lustre_pack_request(request, count, lengths, bufs);
331 LASSERT(!request->rq_pool);
332 OBD_FREE(request, sizeof(*request));
336 if (imp->imp_server_timeout)
337 request->rq_timeout = obd_timeout / 2;
339 request->rq_timeout = obd_timeout;
340 request->rq_send_state = LUSTRE_IMP_FULL;
341 request->rq_type = PTL_RPC_MSG_REQUEST;
342 request->rq_import = class_import_get(imp);
343 request->rq_export = NULL;
345 request->rq_req_cbid.cbid_fn = request_out_callback;
346 request->rq_req_cbid.cbid_arg = request;
348 request->rq_reply_cbid.cbid_fn = reply_in_callback;
349 request->rq_reply_cbid.cbid_arg = request;
351 request->rq_phase = RQ_PHASE_NEW;
353 /* XXX FIXME bug 249 */
354 request->rq_request_portal = imp->imp_client->cli_request_portal;
355 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
357 spin_lock_init(&request->rq_lock);
358 INIT_LIST_HEAD(&request->rq_list);
359 INIT_LIST_HEAD(&request->rq_replay_list);
360 INIT_LIST_HEAD(&request->rq_set_chain);
361 init_waitqueue_head(&request->rq_reply_waitq);
362 request->rq_xid = ptlrpc_next_xid();
363 atomic_set(&request->rq_refcount, 1);
365 request->rq_reqmsg->opc = opcode;
366 request->rq_reqmsg->flags = 0;
371 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, int opcode,
372 int count, int *lengths, char **bufs)
374 return ptlrpc_prep_req_pool(imp, opcode, count, lengths, bufs, NULL);
378 struct ptlrpc_request_set *ptlrpc_prep_set(void)
380 struct ptlrpc_request_set *set;
382 OBD_ALLOC(set, sizeof *set);
385 INIT_LIST_HEAD(&set->set_requests);
386 init_waitqueue_head(&set->set_waitq);
387 set->set_remaining = 0;
388 spin_lock_init(&set->set_new_req_lock);
389 INIT_LIST_HEAD(&set->set_new_requests);
394 /* Finish with this set; opposite of prep_set. */
395 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
397 struct list_head *tmp;
398 struct list_head *next;
403 /* Requests on the set should either all be completed, or all be new */
404 expected_phase = (set->set_remaining == 0) ?
405 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
406 list_for_each (tmp, &set->set_requests) {
407 struct ptlrpc_request *req =
408 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
410 LASSERT(req->rq_phase == expected_phase);
414 LASSERT(set->set_remaining == 0 || set->set_remaining == n);
416 list_for_each_safe(tmp, next, &set->set_requests) {
417 struct ptlrpc_request *req =
418 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
419 list_del_init(&req->rq_set_chain);
421 LASSERT(req->rq_phase == expected_phase);
423 if (req->rq_phase == RQ_PHASE_NEW) {
425 if (req->rq_interpret_reply != NULL) {
426 int (*interpreter)(struct ptlrpc_request *,
428 req->rq_interpret_reply;
430 /* higher level (i.e. LOV) failed;
431 * let the sub reqs clean up */
432 req->rq_status = -EBADR;
433 interpreter(req, &req->rq_async_args,
436 set->set_remaining--;
440 ptlrpc_req_finished (req);
443 LASSERT(set->set_remaining == 0);
445 OBD_FREE(set, sizeof(*set));
449 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
450 struct ptlrpc_request *req)
452 /* The set takes over the caller's request reference */
453 list_add_tail(&req->rq_set_chain, &set->set_requests);
455 set->set_remaining++;
456 atomic_inc(&req->rq_import->imp_inflight);
459 /* lock so many callers can add things, the context that owns the set
460 * is supposed to notice these and move them into the set proper. */
461 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *set,
462 struct ptlrpc_request *req)
465 spin_lock_irqsave(&set->set_new_req_lock, flags);
466 /* The set takes over the caller's request reference */
467 list_add_tail(&req->rq_set_chain, &set->set_new_requests);
469 spin_unlock_irqrestore(&set->set_new_req_lock, flags);
473 * Based on the current state of the import, determine if the request
474 * can be sent, is an error, or should be delayed.
476 * Returns true if this request should be delayed. If false, and
477 * *status is set, then the request can not be sent and *status is the
478 * error code. If false and status is 0, then request can be sent.
480 * The imp->imp_lock must be held.
482 static int ptlrpc_import_delay_req(struct obd_import *imp,
483 struct ptlrpc_request *req, int *status)
488 LASSERT (status != NULL);
491 if (imp->imp_state == LUSTRE_IMP_NEW) {
492 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
495 } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
496 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
498 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
499 imp->imp_state == LUSTRE_IMP_CONNECTING) {
500 /* allow CONNECT even if import is invalid */ ;
501 } else if (imp->imp_invalid) {
502 /* If the import has been invalidated (such as by an OST
503 * failure), the request must fail with -EIO. */
504 if (!imp->imp_deactive)
505 DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
507 } else if (req->rq_import_generation != imp->imp_generation) {
508 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
510 } else if (req->rq_send_state != imp->imp_state) {
511 if (imp->imp_obd->obd_no_recov || imp->imp_dlm_fake ||
513 *status = -EWOULDBLOCK;
521 static int ptlrpc_check_reply(struct ptlrpc_request *req)
527 /* serialise with network callback */
528 spin_lock_irqsave (&req->rq_lock, flags);
530 if (req->rq_replied) {
531 DEBUG_REQ(D_NET, req, "REPLIED:");
535 if (req->rq_net_err && !req->rq_timedout) {
536 spin_unlock_irqrestore (&req->rq_lock, flags);
537 rc = ptlrpc_expire_one_request(req);
538 spin_lock_irqsave (&req->rq_lock, flags);
543 DEBUG_REQ(D_ERROR, req, "ABORTED:");
547 if (req->rq_resend) {
548 DEBUG_REQ(D_ERROR, req, "RESEND:");
552 if (req->rq_restart) {
553 DEBUG_REQ(D_ERROR, req, "RESTART:");
558 spin_unlock_irqrestore (&req->rq_lock, flags);
559 DEBUG_REQ(D_NET, req, "rc = %d for", rc);
563 static int ptlrpc_check_status(struct ptlrpc_request *req)
568 err = req->rq_repmsg->status;
569 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
570 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR, err == %d",
572 RETURN(err < 0 ? err : -EINVAL);
576 DEBUG_REQ(D_INFO, req, "status is %d", err);
577 } else if (err > 0) {
578 /* XXX: translate this error from net to host */
579 DEBUG_REQ(D_INFO, req, "status is %d", err);
585 static int after_reply(struct ptlrpc_request *req)
588 struct obd_import *imp = req->rq_import;
592 LASSERT(!req->rq_receiving_reply);
594 /* NB Until this point, the whole of the incoming message,
595 * including buflens, status etc is in the sender's byte order. */
598 /* Clear reply swab mask; this is a new reply in sender's byte order */
599 req->rq_rep_swab_mask = 0;
601 LASSERT (req->rq_nob_received <= req->rq_replen);
602 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_nob_received);
604 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d\n", rc);
608 if (req->rq_repmsg->type != PTL_RPC_MSG_REPLY &&
609 req->rq_repmsg->type != PTL_RPC_MSG_ERR) {
610 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)\n",
611 req->rq_repmsg->type);
615 rc = ptlrpc_check_status(req);
617 /* Either we've been evicted, or the server has failed for
618 * some reason. Try to reconnect, and if that fails, punt to the
620 if ((rc == -ENOTCONN) || (rc == -ENODEV)) {
621 if (req->rq_send_state != LUSTRE_IMP_FULL ||
622 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
626 ptlrpc_request_handle_notconn(req);
631 /* Store transno in reqmsg for replay. */
632 req->rq_reqmsg->transno = req->rq_transno = req->rq_repmsg->transno;
634 if (req->rq_import->imp_replayable) {
635 spin_lock_irqsave(&imp->imp_lock, flags);
636 if (req->rq_transno != 0)
637 ptlrpc_retain_replayable_request(req, imp);
638 else if (req->rq_commit_cb != NULL) {
639 spin_unlock_irqrestore(&imp->imp_lock, flags);
640 req->rq_commit_cb(req);
641 spin_lock_irqsave(&imp->imp_lock, flags);
644 if (req->rq_transno > imp->imp_max_transno)
645 imp->imp_max_transno = req->rq_transno;
647 /* Replay-enabled imports return commit-status information. */
648 if (req->rq_repmsg->last_committed)
649 imp->imp_peer_committed_transno =
650 req->rq_repmsg->last_committed;
651 ptlrpc_free_committed(imp);
652 spin_unlock_irqrestore(&imp->imp_lock, flags);
658 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
660 struct obd_import *imp;
665 LASSERT(req->rq_phase == RQ_PHASE_NEW);
666 req->rq_phase = RQ_PHASE_RPC;
668 imp = req->rq_import;
669 spin_lock_irqsave(&imp->imp_lock, flags);
671 req->rq_import_generation = imp->imp_generation;
673 if (ptlrpc_import_delay_req(imp, req, &rc)) {
674 spin_lock (&req->rq_lock);
676 spin_unlock (&req->rq_lock);
678 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
680 req->rq_reqmsg->status,
681 ptlrpc_import_state_name(req->rq_send_state),
682 ptlrpc_import_state_name(imp->imp_state));
683 LASSERT(list_empty (&req->rq_list));
685 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
686 spin_unlock_irqrestore(&imp->imp_lock, flags);
691 spin_unlock_irqrestore(&imp->imp_lock, flags);
693 req->rq_phase = RQ_PHASE_INTERPRET;
697 /* XXX this is the same as ptlrpc_queue_wait */
698 LASSERT(list_empty(&req->rq_list));
699 list_add_tail(&req->rq_list, &imp->imp_sending_list);
700 spin_unlock_irqrestore(&imp->imp_lock, flags);
702 req->rq_reqmsg->status = current->pid;
703 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
704 " %s:%s:%d:"LPU64":%s:%d\n", current->comm,
705 imp->imp_obd->obd_uuid.uuid, req->rq_reqmsg->status,
707 libcfs_nid2str(imp->imp_connection->c_peer.nid),
708 req->rq_reqmsg->opc);
710 rc = ptl_send_rpc(req);
712 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
719 int ptlrpc_check_set(struct ptlrpc_request_set *set)
722 struct list_head *tmp;
723 int force_timer_recalc = 0;
726 if (set->set_remaining == 0)
729 list_for_each(tmp, &set->set_requests) {
730 struct ptlrpc_request *req =
731 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
732 struct obd_import *imp = req->rq_import;
735 if (req->rq_phase == RQ_PHASE_NEW &&
736 ptlrpc_send_new_req(req)) {
737 force_timer_recalc = 1;
740 if (!(req->rq_phase == RQ_PHASE_RPC ||
741 req->rq_phase == RQ_PHASE_BULK ||
742 req->rq_phase == RQ_PHASE_INTERPRET ||
743 req->rq_phase == RQ_PHASE_COMPLETE)) {
744 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
748 if (req->rq_phase == RQ_PHASE_COMPLETE)
751 if (req->rq_phase == RQ_PHASE_INTERPRET)
752 GOTO(interpret, req->rq_status);
754 if (req->rq_net_err && !req->rq_timedout)
755 ptlrpc_expire_one_request(req);
758 ptlrpc_unregister_reply(req);
759 if (req->rq_status == 0)
760 req->rq_status = -EIO;
761 req->rq_phase = RQ_PHASE_INTERPRET;
763 spin_lock_irqsave(&imp->imp_lock, flags);
764 list_del_init(&req->rq_list);
765 spin_unlock_irqrestore(&imp->imp_lock, flags);
767 GOTO(interpret, req->rq_status);
770 /* ptlrpc_queue_wait->l_wait_event guarantees that rq_intr
771 * will only be set after rq_timedout, but the oig waiting
772 * path sets rq_intr irrespective of whether ptlrpcd has
773 * seen a timeout. our policy is to only interpret
774 * interrupted rpcs after they have timed out */
775 if (req->rq_intr && (req->rq_timedout || req->rq_waiting)) {
776 /* NB could be on delayed list */
777 ptlrpc_unregister_reply(req);
778 req->rq_status = -EINTR;
779 req->rq_phase = RQ_PHASE_INTERPRET;
781 spin_lock_irqsave(&imp->imp_lock, flags);
782 list_del_init(&req->rq_list);
783 spin_unlock_irqrestore(&imp->imp_lock, flags);
785 GOTO(interpret, req->rq_status);
788 if (req->rq_phase == RQ_PHASE_RPC) {
789 if (req->rq_timedout||req->rq_waiting||req->rq_resend) {
792 ptlrpc_unregister_reply(req);
794 spin_lock_irqsave(&imp->imp_lock, flags);
796 if (ptlrpc_import_delay_req(imp, req, &status)){
797 spin_unlock_irqrestore(&imp->imp_lock,
802 list_del_init(&req->rq_list);
804 req->rq_status = status;
805 req->rq_phase = RQ_PHASE_INTERPRET;
806 spin_unlock_irqrestore(&imp->imp_lock,
808 GOTO(interpret, req->rq_status);
810 if (req->rq_no_resend) {
811 req->rq_status = -ENOTCONN;
812 req->rq_phase = RQ_PHASE_INTERPRET;
813 spin_unlock_irqrestore(&imp->imp_lock,
815 GOTO(interpret, req->rq_status);
817 list_add_tail(&req->rq_list,
818 &imp->imp_sending_list);
820 spin_unlock_irqrestore(&imp->imp_lock, flags);
823 if (req->rq_resend) {
824 lustre_msg_add_flags(req->rq_reqmsg,
827 __u64 old_xid = req->rq_xid;
829 ptlrpc_unregister_bulk (req);
831 /* ensure previous bulk fails */
832 req->rq_xid = ptlrpc_next_xid();
833 CDEBUG(D_HA, "resend bulk "
836 old_xid, req->rq_xid);
840 rc = ptl_send_rpc(req);
842 DEBUG_REQ(D_HA, req, "send failed (%d)",
844 force_timer_recalc = 1;
847 /* need to reset the timeout */
848 force_timer_recalc = 1;
851 /* Still waiting for a reply? */
852 if (ptlrpc_client_receiving_reply(req))
855 /* Did we actually receive a reply? */
856 if (!ptlrpc_client_replied(req))
859 spin_lock_irqsave(&imp->imp_lock, flags);
860 list_del_init(&req->rq_list);
861 spin_unlock_irqrestore(&imp->imp_lock, flags);
863 req->rq_status = after_reply(req);
864 if (req->rq_resend) {
865 /* Add this req to the delayed list so
866 it can be errored if the import is
867 evicted after recovery. */
868 spin_lock_irqsave (&req->rq_lock, flags);
869 list_add_tail(&req->rq_list,
870 &imp->imp_delayed_list);
871 spin_unlock_irqrestore(&req->rq_lock, flags);
875 /* If there is no bulk associated with this request,
876 * then we're done and should let the interpreter
877 * process the reply. Similarly if the RPC returned
878 * an error, and therefore the bulk will never arrive.
880 if (req->rq_bulk == NULL || req->rq_status != 0) {
881 req->rq_phase = RQ_PHASE_INTERPRET;
882 GOTO(interpret, req->rq_status);
885 req->rq_phase = RQ_PHASE_BULK;
888 LASSERT(req->rq_phase == RQ_PHASE_BULK);
889 if (ptlrpc_bulk_active(req->rq_bulk))
892 if (!req->rq_bulk->bd_success) {
893 /* The RPC reply arrived OK, but the bulk screwed
894 * up! Dead wierd since the server told us the RPC
895 * was good after getting the REPLY for her GET or
896 * the ACK for her PUT. */
897 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
901 req->rq_phase = RQ_PHASE_INTERPRET;
904 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
905 LASSERT(!req->rq_receiving_reply);
907 ptlrpc_unregister_reply(req);
908 if (req->rq_bulk != NULL)
909 ptlrpc_unregister_bulk (req);
911 req->rq_phase = RQ_PHASE_COMPLETE;
913 if (req->rq_interpret_reply != NULL) {
914 int (*interpreter)(struct ptlrpc_request *,void *,int) =
915 req->rq_interpret_reply;
916 req->rq_status = interpreter(req, &req->rq_async_args,
920 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
921 "opc %s:%s:%d:"LPU64":%s:%d\n", current->comm,
922 imp->imp_obd->obd_uuid.uuid, req->rq_reqmsg->status,
924 libcfs_nid2str(imp->imp_connection->c_peer.nid),
925 req->rq_reqmsg->opc);
927 set->set_remaining--;
929 atomic_dec(&imp->imp_inflight);
930 wake_up(&imp->imp_recovery_waitq);
933 /* If we hit an error, we want to recover promptly. */
934 RETURN(set->set_remaining == 0 || force_timer_recalc);
937 int ptlrpc_expire_one_request(struct ptlrpc_request *req)
940 struct obd_import *imp = req->rq_import;
943 DEBUG_REQ(D_ERROR, req, "timeout (sent at %lu, %lus ago)",
944 (long)req->rq_sent, CURRENT_SECONDS - req->rq_sent);
946 spin_lock_irqsave (&req->rq_lock, flags);
947 req->rq_timedout = 1;
948 spin_unlock_irqrestore (&req->rq_lock, flags);
950 ptlrpc_unregister_reply (req);
952 if (obd_dump_on_timeout)
953 libcfs_debug_dumplog();
955 if (req->rq_bulk != NULL)
956 ptlrpc_unregister_bulk (req);
959 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
963 /* The DLM server doesn't want recovery run on its imports. */
964 if (imp->imp_dlm_fake)
967 /* If this request is for recovery or other primordial tasks,
968 * then error it out here. */
969 if (req->rq_send_state != LUSTRE_IMP_FULL ||
970 imp->imp_obd->obd_no_recov) {
971 spin_lock_irqsave (&req->rq_lock, flags);
972 req->rq_status = -ETIMEDOUT;
974 spin_unlock_irqrestore (&req->rq_lock, flags);
978 ptlrpc_fail_import(imp, req->rq_import_generation);
983 int ptlrpc_expired_set(void *data)
985 struct ptlrpc_request_set *set = data;
986 struct list_head *tmp;
987 time_t now = CURRENT_SECONDS;
990 LASSERT(set != NULL);
992 /* A timeout expired; see which reqs it applies to... */
993 list_for_each (tmp, &set->set_requests) {
994 struct ptlrpc_request *req =
995 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
997 /* request in-flight? */
998 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting &&
1000 (req->rq_phase == RQ_PHASE_BULK)))
1003 if (req->rq_timedout || /* already dealt with */
1004 req->rq_sent + req->rq_timeout > now) /* not expired */
1007 /* deal with this guy */
1008 ptlrpc_expire_one_request (req);
1011 /* When waiting for a whole set, we always to break out of the
1012 * sleep so we can recalculate the timeout, or enable interrupts
1013 * iff everyone's timed out.
1018 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1020 unsigned long flags;
1021 spin_lock_irqsave(&req->rq_lock, flags);
1023 spin_unlock_irqrestore(&req->rq_lock, flags);
1026 void ptlrpc_interrupted_set(void *data)
1028 struct ptlrpc_request_set *set = data;
1029 struct list_head *tmp;
1031 LASSERT(set != NULL);
1032 CERROR("INTERRUPTED SET %p\n", set);
1034 list_for_each(tmp, &set->set_requests) {
1035 struct ptlrpc_request *req =
1036 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1038 if (req->rq_phase != RQ_PHASE_RPC)
1041 ptlrpc_mark_interrupted(req);
1045 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1047 struct list_head *tmp;
1048 time_t now = CURRENT_SECONDS;
1051 struct ptlrpc_request *req;
1054 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1056 list_for_each(tmp, &set->set_requests) {
1057 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1059 /* request in-flight? */
1060 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting) ||
1061 (req->rq_phase == RQ_PHASE_BULK)))
1064 if (req->rq_timedout) /* already timed out */
1067 deadline = req->rq_sent + req->rq_timeout;
1068 if (deadline <= now) /* actually expired already */
1069 timeout = 1; /* ASAP */
1070 else if (timeout == 0 || timeout > deadline - now)
1071 timeout = deadline - now;
1076 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
1078 struct list_head *tmp;
1079 struct ptlrpc_request *req;
1080 struct l_wait_info lwi;
1084 LASSERT(!list_empty(&set->set_requests));
1085 list_for_each(tmp, &set->set_requests) {
1086 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1087 if (req->rq_phase == RQ_PHASE_NEW)
1088 (void)ptlrpc_send_new_req(req);
1092 timeout = ptlrpc_set_next_timeout(set);
1094 /* wait until all complete, interrupted, or an in-flight
1096 CDEBUG(D_HA, "set %p going to sleep for %d seconds\n",
1098 lwi = LWI_TIMEOUT_INTR((timeout ? timeout : 1) * HZ,
1100 ptlrpc_interrupted_set, set);
1101 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(set), &lwi);
1103 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
1105 /* -EINTR => all requests have been flagged rq_intr so next
1107 * -ETIMEOUTD => someone timed out. When all reqs have
1108 * timed out, signals are enabled allowing completion with
1110 * I don't really care if we go once more round the loop in
1111 * the error cases -eeb. */
1112 } while (rc != 0 || set->set_remaining != 0);
1114 LASSERT(set->set_remaining == 0);
1117 list_for_each(tmp, &set->set_requests) {
1118 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1120 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
1121 if (req->rq_status != 0)
1122 rc = req->rq_status;
1125 if (set->set_interpret != NULL) {
1126 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
1128 rc = interpreter (set, set->set_arg, rc);
1134 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
1136 struct ptlrpc_request_pool *pool = request->rq_pool;
1138 spin_lock(&pool->prp_lock);
1139 list_add_tail(&request->rq_list, &pool->prp_req_list);
1140 spin_unlock(&pool->prp_lock);
1143 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
1146 if (request == NULL) {
1151 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
1152 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
1153 LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
1154 LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
1156 /* We must take it off the imp_replay_list first. Otherwise, we'll set
1157 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
1158 if (request->rq_import != NULL) {
1159 unsigned long flags = 0;
1161 spin_lock_irqsave(&request->rq_import->imp_lock, flags);
1162 list_del_init(&request->rq_replay_list);
1164 spin_unlock_irqrestore(&request->rq_import->imp_lock,
1167 LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
1169 if (atomic_read(&request->rq_refcount) != 0) {
1170 DEBUG_REQ(D_ERROR, request,
1171 "freeing request with nonzero refcount");
1175 if (request->rq_repmsg != NULL) {
1176 OBD_FREE(request->rq_repmsg, request->rq_replen);
1177 request->rq_repmsg = NULL;
1179 if (request->rq_export != NULL) {
1180 class_export_put(request->rq_export);
1181 request->rq_export = NULL;
1183 if (request->rq_import != NULL) {
1184 class_import_put(request->rq_import);
1185 request->rq_import = NULL;
1187 if (request->rq_bulk != NULL)
1188 ptlrpc_free_bulk(request->rq_bulk);
1190 if (request->rq_pool) {
1191 __ptlrpc_free_req_to_pool(request);
1193 if (request->rq_reqmsg != NULL) {
1194 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
1195 request->rq_reqmsg = NULL;
1197 OBD_FREE(request, sizeof(*request));
1202 void ptlrpc_free_req(struct ptlrpc_request *request)
1204 __ptlrpc_free_req(request, 0);
1207 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
1208 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
1210 LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
1211 (void)__ptlrpc_req_finished(request, 1);
1214 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
1217 if (request == NULL)
1220 if (request == LP_POISON ||
1221 request->rq_reqmsg == LP_POISON) {
1222 CERROR("dereferencing freed request (bug 575)\n");
1227 DEBUG_REQ(D_INFO, request, "refcount now %u",
1228 atomic_read(&request->rq_refcount) - 1);
1230 if (atomic_dec_and_test(&request->rq_refcount)) {
1231 __ptlrpc_free_req(request, locked);
1238 void ptlrpc_req_finished(struct ptlrpc_request *request)
1240 __ptlrpc_req_finished(request, 0);
1243 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
1245 return request->rq_xid;
1247 EXPORT_SYMBOL(ptlrpc_req_xid);
1249 /* Disengage the client's reply buffer from the network
1250 * NB does _NOT_ unregister any client-side bulk.
1251 * IDEMPOTENT, but _not_ safe against concurrent callers.
1252 * The request owner (i.e. the thread doing the I/O) must call...
1254 void ptlrpc_unregister_reply (struct ptlrpc_request *request)
1257 wait_queue_head_t *wq;
1258 struct l_wait_info lwi;
1260 LASSERT(!in_interrupt ()); /* might sleep */
1262 if (!ptlrpc_client_receiving_reply(request))
1265 LNetMDUnlink (request->rq_reply_md_h);
1267 /* We have to l_wait_event() whatever the result, to give liblustre
1268 * a chance to run reply_in_callback() */
1270 if (request->rq_set != NULL)
1271 wq = &request->rq_set->set_waitq;
1273 wq = &request->rq_reply_waitq;
1276 /* Network access will complete in finite time but the HUGE
1277 * timeout lets us CWARN for visibility of sluggish NALs */
1278 lwi = LWI_TIMEOUT(300 * HZ, NULL, NULL);
1279 rc = l_wait_event (*wq, !ptlrpc_client_receiving_reply(request), &lwi);
1283 LASSERT (rc == -ETIMEDOUT);
1284 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout");
1288 /* caller must hold imp->imp_lock */
1289 void ptlrpc_free_committed(struct obd_import *imp)
1291 struct list_head *tmp, *saved;
1292 struct ptlrpc_request *req;
1293 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1296 LASSERT(imp != NULL);
1298 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1300 CDEBUG(D_HA, "%s: committing for last_committed "LPU64"\n",
1301 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1303 list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1304 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1306 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
1307 LASSERT(req != last_req);
1310 if (req->rq_import_generation < imp->imp_generation) {
1311 DEBUG_REQ(D_HA, req, "freeing request with old gen");
1315 if (req->rq_replay) {
1316 DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
1320 /* not yet committed */
1321 if (req->rq_transno > imp->imp_peer_committed_transno) {
1322 DEBUG_REQ(D_HA, req, "stopping search");
1326 DEBUG_REQ(D_HA, req, "committing (last_committed "LPU64")",
1327 imp->imp_peer_committed_transno);
1329 if (req->rq_commit_cb != NULL)
1330 req->rq_commit_cb(req);
1331 list_del_init(&req->rq_replay_list);
1332 __ptlrpc_req_finished(req, 1);
1339 void ptlrpc_cleanup_client(struct obd_import *imp)
1346 void ptlrpc_resend_req(struct ptlrpc_request *req)
1348 unsigned long flags;
1350 DEBUG_REQ(D_HA, req, "going to resend");
1351 req->rq_reqmsg->handle.cookie = 0;
1352 req->rq_status = -EAGAIN;
1354 spin_lock_irqsave (&req->rq_lock, flags);
1356 req->rq_net_err = 0;
1357 req->rq_timedout = 0;
1359 __u64 old_xid = req->rq_xid;
1361 /* ensure previous bulk fails */
1362 req->rq_xid = ptlrpc_next_xid();
1363 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
1364 old_xid, req->rq_xid);
1366 ptlrpc_wake_client_req(req);
1367 spin_unlock_irqrestore (&req->rq_lock, flags);
1370 /* XXX: this function and rq_status are currently unused */
1371 void ptlrpc_restart_req(struct ptlrpc_request *req)
1373 unsigned long flags;
1375 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1376 req->rq_status = -ERESTARTSYS;
1378 spin_lock_irqsave (&req->rq_lock, flags);
1379 req->rq_restart = 1;
1380 req->rq_timedout = 0;
1381 ptlrpc_wake_client_req(req);
1382 spin_unlock_irqrestore (&req->rq_lock, flags);
1385 static int expired_request(void *data)
1387 struct ptlrpc_request *req = data;
1390 RETURN(ptlrpc_expire_one_request(req));
1393 static void interrupted_request(void *data)
1395 unsigned long flags;
1397 struct ptlrpc_request *req = data;
1398 DEBUG_REQ(D_HA, req, "request interrupted");
1399 spin_lock_irqsave (&req->rq_lock, flags);
1401 spin_unlock_irqrestore (&req->rq_lock, flags);
1404 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1407 atomic_inc(&req->rq_refcount);
1411 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1412 struct obd_import *imp)
1414 struct list_head *tmp;
1416 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1418 /* clear this for new requests that were resent as well
1419 as resent replayed requests. */
1420 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1422 /* don't re-add requests that have been replayed */
1423 if (!list_empty(&req->rq_replay_list))
1426 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
1428 LASSERT(imp->imp_replayable);
1429 /* Balanced in ptlrpc_free_committed, usually. */
1430 ptlrpc_request_addref(req);
1431 list_for_each_prev(tmp, &imp->imp_replay_list) {
1432 struct ptlrpc_request *iter =
1433 list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1435 /* We may have duplicate transnos if we create and then
1436 * open a file, or for closes retained if to match creating
1437 * opens, so use req->rq_xid as a secondary key.
1438 * (See bugs 684, 685, and 428.)
1439 * XXX no longer needed, but all opens need transnos!
1441 if (iter->rq_transno > req->rq_transno)
1444 if (iter->rq_transno == req->rq_transno) {
1445 LASSERT(iter->rq_xid != req->rq_xid);
1446 if (iter->rq_xid > req->rq_xid)
1450 list_add(&req->rq_replay_list, &iter->rq_replay_list);
1454 list_add_tail(&req->rq_replay_list, &imp->imp_replay_list);
1457 int ptlrpc_queue_wait(struct ptlrpc_request *req)
1461 struct l_wait_info lwi;
1462 struct obd_import *imp = req->rq_import;
1463 unsigned long flags;
1467 LASSERT(req->rq_set == NULL);
1468 LASSERT(!req->rq_receiving_reply);
1469 atomic_inc(&imp->imp_inflight);
1471 /* for distributed debugging */
1472 req->rq_reqmsg->status = current->pid;
1473 LASSERT(imp->imp_obd != NULL);
1474 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc "
1475 "%s:%s:%d:"LPU64":%s:%d\n", current->comm,
1476 imp->imp_obd->obd_uuid.uuid,
1477 req->rq_reqmsg->status, req->rq_xid,
1478 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1479 req->rq_reqmsg->opc);
1481 /* Mark phase here for a little debug help */
1482 req->rq_phase = RQ_PHASE_RPC;
1484 spin_lock_irqsave(&imp->imp_lock, flags);
1485 req->rq_import_generation = imp->imp_generation;
1487 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1488 list_del(&req->rq_list);
1490 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1491 spin_unlock_irqrestore(&imp->imp_lock, flags);
1493 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%s != %s)",
1495 ptlrpc_import_state_name(req->rq_send_state),
1496 ptlrpc_import_state_name(imp->imp_state));
1497 lwi = LWI_INTR(interrupted_request, req);
1498 rc = l_wait_event(req->rq_reply_waitq,
1499 (req->rq_send_state == imp->imp_state ||
1500 req->rq_err || req->rq_intr),
1502 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%s == %s or %d/%d == 1)",
1504 ptlrpc_import_state_name(imp->imp_state),
1505 ptlrpc_import_state_name(req->rq_send_state),
1506 req->rq_err, req->rq_intr);
1508 spin_lock_irqsave(&imp->imp_lock, flags);
1509 list_del_init(&req->rq_list);
1514 else if (req->rq_intr) {
1517 else if (req->rq_no_resend) {
1518 spin_unlock_irqrestore(&imp->imp_lock, flags);
1519 GOTO(out, rc = -ETIMEDOUT);
1527 list_del_init(&req->rq_list);
1528 spin_unlock_irqrestore(&imp->imp_lock, flags);
1529 req->rq_status = rc; // XXX this ok?
1533 if (req->rq_resend) {
1534 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
1536 if (req->rq_bulk != NULL) {
1537 ptlrpc_unregister_bulk (req);
1539 /* bulk requests are supposed to be
1540 * idempotent, so we are free to bump the xid
1541 * here, which we need to do before
1542 * registering the bulk again (bug 6371).
1543 * print the old xid first for sanity.
1545 DEBUG_REQ(D_HA, req, "bumping xid for bulk: ");
1546 req->rq_xid = ptlrpc_next_xid();
1549 DEBUG_REQ(D_HA, req, "resending: ");
1552 /* XXX this is the same as ptlrpc_set_wait */
1553 LASSERT(list_empty(&req->rq_list));
1554 list_add_tail(&req->rq_list, &imp->imp_sending_list);
1555 spin_unlock_irqrestore(&imp->imp_lock, flags);
1557 rc = ptl_send_rpc(req);
1559 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
1562 timeout = MAX(req->rq_timeout * HZ, 1);
1563 DEBUG_REQ(D_NET, req, "-- sleeping for %d jiffies", timeout);
1565 lwi = LWI_TIMEOUT_INTR(timeout, expired_request, interrupted_request,
1567 l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req), &lwi);
1568 DEBUG_REQ(D_NET, req, "-- done sleeping");
1570 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:opc "
1571 "%s:%s:%d:"LPU64":%s:%d\n", current->comm,
1572 imp->imp_obd->obd_uuid.uuid,
1573 req->rq_reqmsg->status, req->rq_xid,
1574 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1575 req->rq_reqmsg->opc);
1577 spin_lock_irqsave(&imp->imp_lock, flags);
1578 list_del_init(&req->rq_list);
1579 spin_unlock_irqrestore(&imp->imp_lock, flags);
1581 /* If the reply was received normally, this just grabs the spinlock
1582 * (ensuring the reply callback has returned), sees that
1583 * req->rq_receiving_reply is clear and returns. */
1584 ptlrpc_unregister_reply (req);
1587 GOTO(out, rc = -EIO);
1589 /* Resend if we need to, unless we were interrupted. */
1590 if (req->rq_resend && !req->rq_intr) {
1591 /* ...unless we were specifically told otherwise. */
1592 if (req->rq_no_resend)
1593 GOTO(out, rc = -ETIMEDOUT);
1594 spin_lock_irqsave(&imp->imp_lock, flags);
1599 /* Should only be interrupted if we timed out. */
1600 if (!req->rq_timedout)
1601 DEBUG_REQ(D_ERROR, req,
1602 "rq_intr set but rq_timedout not");
1603 GOTO(out, rc = -EINTR);
1606 if (req->rq_timedout) { /* non-recoverable timeout */
1607 GOTO(out, rc = -ETIMEDOUT);
1610 if (!req->rq_replied) {
1611 /* How can this be? -eeb */
1612 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
1614 GOTO(out, rc = req->rq_status);
1617 rc = after_reply (req);
1618 /* NB may return +ve success rc */
1619 if (req->rq_resend) {
1620 spin_lock_irqsave(&imp->imp_lock, flags);
1625 if (req->rq_bulk != NULL) {
1627 /* success so far. Note that anything going wrong
1628 * with bulk now, is EXTREMELY strange, since the
1629 * server must have believed that the bulk
1630 * tranferred OK before she replied with success to
1632 lwi = LWI_TIMEOUT(timeout, NULL, NULL);
1633 brc = l_wait_event(req->rq_reply_waitq,
1634 !ptlrpc_bulk_active(req->rq_bulk),
1636 LASSERT(brc == 0 || brc == -ETIMEDOUT);
1638 LASSERT(brc == -ETIMEDOUT);
1639 DEBUG_REQ(D_ERROR, req, "bulk timed out");
1641 } else if (!req->rq_bulk->bd_success) {
1642 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1647 ptlrpc_unregister_bulk (req);
1650 LASSERT(!req->rq_receiving_reply);
1651 req->rq_phase = RQ_PHASE_INTERPRET;
1653 atomic_dec(&imp->imp_inflight);
1654 wake_up(&imp->imp_recovery_waitq);
1658 struct ptlrpc_replay_async_args {
1660 int praa_old_status;
1663 static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
1664 void * data, int rc)
1666 struct ptlrpc_replay_async_args *aa = data;
1667 struct obd_import *imp = req->rq_import;
1668 unsigned long flags;
1670 atomic_dec(&imp->imp_replay_inflight);
1672 if (!req->rq_replied) {
1673 CERROR("request replay timed out, restarting recovery\n");
1674 GOTO(out, rc = -ETIMEDOUT);
1678 /* Clear reply swab mask; this is a new reply in sender's byte order */
1679 req->rq_rep_swab_mask = 0;
1681 LASSERT (req->rq_nob_received <= req->rq_replen);
1682 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_nob_received);
1684 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d\n", rc);
1685 GOTO(out, rc = -EPROTO);
1688 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR &&
1689 req->rq_repmsg->status == -ENOTCONN)
1690 GOTO(out, rc = req->rq_repmsg->status);
1692 /* The transno had better not change over replay. */
1693 LASSERT(req->rq_reqmsg->transno == req->rq_repmsg->transno);
1695 DEBUG_REQ(D_HA, req, "got rep");
1697 /* let the callback do fixups, possibly including in the request */
1698 if (req->rq_replay_cb)
1699 req->rq_replay_cb(req);
1701 if (req->rq_replied && req->rq_repmsg->status != aa->praa_old_status) {
1702 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
1703 req->rq_repmsg->status, aa->praa_old_status);
1705 /* Put it back for re-replay. */
1706 req->rq_repmsg->status = aa->praa_old_status;
1709 spin_lock_irqsave(&imp->imp_lock, flags);
1710 imp->imp_last_replay_transno = req->rq_transno;
1711 spin_unlock_irqrestore(&imp->imp_lock, flags);
1713 /* continue with recovery */
1714 rc = ptlrpc_import_recovery_state_machine(imp);
1716 req->rq_send_state = aa->praa_old_state;
1719 /* this replay failed, so restart recovery */
1720 ptlrpc_connect_import(imp, NULL);
1726 int ptlrpc_replay_req(struct ptlrpc_request *req)
1728 struct ptlrpc_replay_async_args *aa;
1731 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
1733 /* Not handling automatic bulk replay yet (or ever?) */
1734 LASSERT(req->rq_bulk == NULL);
1736 DEBUG_REQ(D_HA, req, "REPLAY");
1738 LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
1739 aa = (struct ptlrpc_replay_async_args *)&req->rq_async_args;
1740 memset(aa, 0, sizeof *aa);
1742 /* Prepare request to be resent with ptlrpcd */
1743 aa->praa_old_state = req->rq_send_state;
1744 req->rq_send_state = LUSTRE_IMP_REPLAY;
1745 req->rq_phase = RQ_PHASE_NEW;
1746 aa->praa_old_status = req->rq_repmsg->status;
1749 req->rq_interpret_reply = ptlrpc_replay_interpret;
1750 atomic_inc(&req->rq_import->imp_replay_inflight);
1751 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
1753 ptlrpcd_add_req(req);
1757 void ptlrpc_abort_inflight(struct obd_import *imp)
1759 unsigned long flags;
1760 struct list_head *tmp, *n;
1763 /* Make sure that no new requests get processed for this import.
1764 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
1765 * this flag and then putting requests on sending_list or delayed_list.
1767 spin_lock_irqsave(&imp->imp_lock, flags);
1769 /* XXX locking? Maybe we should remove each request with the list
1770 * locked? Also, how do we know if the requests on the list are
1771 * being freed at this time?
1773 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
1774 struct ptlrpc_request *req =
1775 list_entry(tmp, struct ptlrpc_request, rq_list);
1777 DEBUG_REQ(D_HA, req, "inflight");
1779 spin_lock (&req->rq_lock);
1780 if (req->rq_import_generation < imp->imp_generation) {
1782 ptlrpc_wake_client_req(req);
1784 spin_unlock (&req->rq_lock);
1787 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
1788 struct ptlrpc_request *req =
1789 list_entry(tmp, struct ptlrpc_request, rq_list);
1791 DEBUG_REQ(D_HA, req, "aborting waiting req");
1793 spin_lock (&req->rq_lock);
1794 if (req->rq_import_generation < imp->imp_generation) {
1796 ptlrpc_wake_client_req(req);
1798 spin_unlock (&req->rq_lock);
1801 /* Last chance to free reqs left on the replay list, but we
1802 * will still leak reqs that haven't comitted. */
1803 if (imp->imp_replayable)
1804 ptlrpc_free_committed(imp);
1806 spin_unlock_irqrestore(&imp->imp_lock, flags);
1811 static __u64 ptlrpc_last_xid = 0;
1812 static spinlock_t ptlrpc_last_xid_lock = SPIN_LOCK_UNLOCKED;
1814 __u64 ptlrpc_next_xid(void)
1817 spin_lock(&ptlrpc_last_xid_lock);
1818 tmp = ++ptlrpc_last_xid;
1819 spin_unlock(&ptlrpc_last_xid_lock);
1823 __u64 ptlrpc_sample_next_xid(void)
1826 spin_lock(&ptlrpc_last_xid_lock);
1827 tmp = ptlrpc_last_xid + 1;
1828 spin_unlock(&ptlrpc_last_xid_lock);
1831 EXPORT_SYMBOL(ptlrpc_sample_next_xid);