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 <obd_support.h>
34 #include <obd_class.h>
35 #include <lustre_lib.h>
36 #include <lustre_ha.h>
37 #include <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 cfs_waitq_init(&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;
117 LASSERT(type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
118 desc = new_bulk(npages, type, portal);
122 desc->bd_import_generation = req->rq_import_generation;
123 desc->bd_import = class_import_get(imp);
126 desc->bd_cbid.cbid_fn = client_bulk_callback;
127 desc->bd_cbid.cbid_arg = desc;
129 /* This makes req own desc, and free it when she frees herself */
135 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp (struct ptlrpc_request *req,
136 int npages, int type, int portal)
138 struct obd_export *exp = req->rq_export;
139 struct ptlrpc_bulk_desc *desc;
142 LASSERT(type == BULK_PUT_SOURCE || type == BULK_GET_SINK);
144 desc = new_bulk(npages, type, portal);
148 desc->bd_export = class_export_get(exp);
151 desc->bd_cbid.cbid_fn = server_bulk_callback;
152 desc->bd_cbid.cbid_arg = desc;
154 /* NB we don't assign rq_bulk here; server-side requests are
155 * re-used, and the handler frees the bulk desc explicitly. */
160 void ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
161 cfs_page_t *page, int pageoffset, int len)
163 LASSERT(desc->bd_iov_count < desc->bd_max_iov);
164 LASSERT(page != NULL);
165 LASSERT(pageoffset >= 0);
167 LASSERT(pageoffset + len <= CFS_PAGE_SIZE);
171 ptlrpc_add_bulk_page(desc, page, pageoffset, len);
174 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
178 LASSERT(desc != NULL);
179 LASSERT(desc->bd_iov_count != LI_POISON); /* not freed already */
180 LASSERT(!desc->bd_network_rw); /* network hands off or */
181 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
183 class_export_put(desc->bd_export);
185 class_import_put(desc->bd_import);
187 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
188 bd_iov[desc->bd_max_iov]));
192 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
194 struct list_head *l, *tmp;
195 struct ptlrpc_request *req;
200 list_for_each_safe(l, tmp, &pool->prp_req_list) {
201 req = list_entry(l, struct ptlrpc_request, rq_list);
202 list_del(&req->rq_list);
203 LASSERT (req->rq_reqmsg);
204 OBD_FREE(req->rq_reqmsg, pool->prp_rq_size);
205 OBD_FREE(req, sizeof(*req));
207 OBD_FREE(pool, sizeof(*pool));
210 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
215 while (size < pool->prp_rq_size)
218 LASSERTF(list_empty(&pool->prp_req_list) || size == pool->prp_rq_size,
219 "Trying to change pool size with nonempty pool "
220 "from %d to %d bytes\n", pool->prp_rq_size, size);
222 spin_lock(&pool->prp_lock);
223 pool->prp_rq_size = size;
224 for (i = 0; i < num_rq; i++) {
225 struct ptlrpc_request *req;
226 struct lustre_msg *msg;
228 spin_unlock(&pool->prp_lock);
229 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
232 OBD_ALLOC_GFP(msg, size, CFS_ALLOC_STD);
234 OBD_FREE(req, sizeof(struct ptlrpc_request));
237 req->rq_reqmsg = msg;
239 spin_lock(&pool->prp_lock);
240 list_add_tail(&req->rq_list, &pool->prp_req_list);
242 spin_unlock(&pool->prp_lock);
246 struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int num_rq, int msgsize,
247 void (*populate_pool)(struct ptlrpc_request_pool *, int))
249 struct ptlrpc_request_pool *pool;
251 OBD_ALLOC(pool, sizeof (struct ptlrpc_request_pool));
255 /* Request next power of two for the allocation, because internally
256 kernel would do exactly this */
258 spin_lock_init(&pool->prp_lock);
259 CFS_INIT_LIST_HEAD(&pool->prp_req_list);
260 pool->prp_rq_size = msgsize;
261 pool->prp_populate = populate_pool;
263 populate_pool(pool, num_rq);
265 if (list_empty(&pool->prp_req_list)) {
266 /* have not allocated a single request for the pool */
267 OBD_FREE(pool, sizeof (struct ptlrpc_request_pool));
273 static struct ptlrpc_request *ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
275 struct ptlrpc_request *request;
276 struct lustre_msg *reqmsg;
281 spin_lock(&pool->prp_lock);
283 /* See if we have anything in a pool, and bail out if nothing,
284 * in writeout path, where this matters, this is safe to do, because
285 * nothing is lost in this case, and when some in-flight requests
286 * complete, this code will be called again. */
287 if (unlikely(list_empty(&pool->prp_req_list))) {
288 spin_unlock(&pool->prp_lock);
292 request = list_entry(pool->prp_req_list.next, struct ptlrpc_request,
294 list_del(&request->rq_list);
295 spin_unlock(&pool->prp_lock);
297 LASSERT(request->rq_reqmsg);
298 LASSERT(request->rq_pool);
300 reqmsg = request->rq_reqmsg;
301 memset(request, 0, sizeof(*request));
302 request->rq_reqmsg = reqmsg;
303 request->rq_pool = pool;
304 request->rq_reqlen = pool->prp_rq_size;
308 struct ptlrpc_request *
309 ptlrpc_prep_req_pool(struct obd_import *imp, __u32 version, int opcode,
310 int count, int *lengths, char **bufs,
311 struct ptlrpc_request_pool *pool)
313 struct ptlrpc_request *request = NULL;
317 /* The obd disconnected */
321 LASSERT(imp != LP_POISON);
322 LASSERT((unsigned long)imp->imp_client > 0x1000);
323 LASSERT(imp->imp_client != LP_POISON);
326 request = ptlrpc_prep_req_from_pool(pool);
329 OBD_ALLOC(request, sizeof(*request));
332 CERROR("request allocation out of memory\n");
336 rc = lustre_pack_request(request, imp->imp_msg_magic, count, lengths,
339 LASSERT(!request->rq_pool);
340 OBD_FREE(request, sizeof(*request));
344 lustre_msg_add_version(request->rq_reqmsg, version);
346 if (imp->imp_server_timeout)
347 request->rq_timeout = obd_timeout / 2;
349 request->rq_timeout = obd_timeout;
350 request->rq_send_state = LUSTRE_IMP_FULL;
351 request->rq_type = PTL_RPC_MSG_REQUEST;
352 request->rq_import = class_import_get(imp);
353 request->rq_export = NULL;
355 request->rq_req_cbid.cbid_fn = request_out_callback;
356 request->rq_req_cbid.cbid_arg = request;
358 request->rq_reply_cbid.cbid_fn = reply_in_callback;
359 request->rq_reply_cbid.cbid_arg = request;
361 request->rq_phase = RQ_PHASE_NEW;
363 /* XXX FIXME bug 249 */
364 request->rq_request_portal = imp->imp_client->cli_request_portal;
365 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
367 spin_lock_init(&request->rq_lock);
368 CFS_INIT_LIST_HEAD(&request->rq_list);
369 CFS_INIT_LIST_HEAD(&request->rq_replay_list);
370 CFS_INIT_LIST_HEAD(&request->rq_set_chain);
371 cfs_waitq_init(&request->rq_reply_waitq);
372 request->rq_xid = ptlrpc_next_xid();
373 atomic_set(&request->rq_refcount, 1);
375 lustre_msg_set_opc(request->rq_reqmsg, opcode);
376 lustre_msg_set_flags(request->rq_reqmsg, 0);
381 struct ptlrpc_request *
382 ptlrpc_prep_req(struct obd_import *imp, __u32 version, int opcode, int count,
383 int *lengths, char **bufs)
385 return ptlrpc_prep_req_pool(imp, version, opcode, count, lengths, bufs,
389 struct ptlrpc_request_set *ptlrpc_prep_set(void)
391 struct ptlrpc_request_set *set;
394 OBD_ALLOC(set, sizeof *set);
397 CFS_INIT_LIST_HEAD(&set->set_requests);
398 cfs_waitq_init(&set->set_waitq);
399 set->set_remaining = 0;
400 spin_lock_init(&set->set_new_req_lock);
401 CFS_INIT_LIST_HEAD(&set->set_new_requests);
406 /* Finish with this set; opposite of prep_set. */
407 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
409 struct list_head *tmp;
410 struct list_head *next;
415 /* Requests on the set should either all be completed, or all be new */
416 expected_phase = (set->set_remaining == 0) ?
417 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
418 list_for_each (tmp, &set->set_requests) {
419 struct ptlrpc_request *req =
420 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
422 LASSERT(req->rq_phase == expected_phase);
426 LASSERT(set->set_remaining == 0 || set->set_remaining == n);
428 list_for_each_safe(tmp, next, &set->set_requests) {
429 struct ptlrpc_request *req =
430 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
431 list_del_init(&req->rq_set_chain);
433 LASSERT(req->rq_phase == expected_phase);
435 if (req->rq_phase == RQ_PHASE_NEW) {
437 if (req->rq_interpret_reply != NULL) {
438 int (*interpreter)(struct ptlrpc_request *,
440 req->rq_interpret_reply;
442 /* higher level (i.e. LOV) failed;
443 * let the sub reqs clean up */
444 req->rq_status = -EBADR;
445 interpreter(req, &req->rq_async_args,
448 set->set_remaining--;
452 ptlrpc_req_finished (req);
455 LASSERT(set->set_remaining == 0);
457 OBD_FREE(set, sizeof(*set));
461 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
462 struct ptlrpc_request *req)
464 /* The set takes over the caller's request reference */
465 list_add_tail(&req->rq_set_chain, &set->set_requests);
467 set->set_remaining++;
469 atomic_inc(&req->rq_import->imp_inflight);
472 /* lock so many callers can add things, the context that owns the set
473 * is supposed to notice these and move them into the set proper. */
474 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *set,
475 struct ptlrpc_request *req)
477 spin_lock(&set->set_new_req_lock);
478 /* The set takes over the caller's request reference */
479 list_add_tail(&req->rq_set_chain, &set->set_new_requests);
481 spin_unlock(&set->set_new_req_lock);
485 * Based on the current state of the import, determine if the request
486 * can be sent, is an error, or should be delayed.
488 * Returns true if this request should be delayed. If false, and
489 * *status is set, then the request can not be sent and *status is the
490 * error code. If false and status is 0, then request can be sent.
492 * The imp->imp_lock must be held.
494 static int ptlrpc_import_delay_req(struct obd_import *imp,
495 struct ptlrpc_request *req, int *status)
500 LASSERT (status != NULL);
503 if (imp->imp_state == LUSTRE_IMP_NEW) {
504 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
507 } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
508 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
510 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
511 imp->imp_state == LUSTRE_IMP_CONNECTING) {
512 /* allow CONNECT even if import is invalid */ ;
513 } else if (imp->imp_invalid) {
514 /* If the import has been invalidated (such as by an OST
515 * failure), the request must fail with -EIO. */
516 if (!imp->imp_deactive)
517 DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
519 } else if (req->rq_import_generation != imp->imp_generation) {
520 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
522 } else if (req->rq_send_state != imp->imp_state) {
523 if (imp->imp_obd->obd_no_recov || imp->imp_dlm_fake ||
525 *status = -EWOULDBLOCK;
533 static int ptlrpc_check_reply(struct ptlrpc_request *req)
538 /* serialise with network callback */
539 spin_lock(&req->rq_lock);
544 if (req->rq_net_err && !req->rq_timedout) {
545 spin_unlock(&req->rq_lock);
546 rc = ptlrpc_expire_one_request(req);
547 spin_lock(&req->rq_lock);
561 spin_unlock(&req->rq_lock);
562 DEBUG_REQ(D_NET, req, "rc = %d for", rc);
566 static int ptlrpc_check_status(struct ptlrpc_request *req)
571 err = lustre_msg_get_status(req->rq_repmsg);
572 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
573 LCONSOLE_ERROR_MSG(0x011, "an error ocurred while communicating"
574 " with %s The %s operation failed with %d",
575 obd_export_nid2str(req->rq_export),
576 ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
578 RETURN(err < 0 ? err : -EINVAL);
582 DEBUG_REQ(D_INFO, req, "status is %d", err);
583 } else if (err > 0) {
584 /* XXX: translate this error from net to host */
585 DEBUG_REQ(D_INFO, req, "status is %d", err);
591 static int after_reply(struct ptlrpc_request *req)
593 struct obd_import *imp = req->rq_import;
597 LASSERT(!req->rq_receiving_reply);
599 /* NB Until this point, the whole of the incoming message,
600 * including buflens, status etc is in the sender's byte order. */
603 /* Clear reply swab mask; this is a new reply in sender's byte order */
604 req->rq_rep_swab_mask = 0;
606 LASSERT (req->rq_nob_received <= req->rq_replen);
607 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_nob_received);
609 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d\n", rc);
613 rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
615 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d\n", rc);
619 if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
620 lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
621 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)\n",
622 lustre_msg_get_type(req->rq_repmsg));
626 rc = ptlrpc_check_status(req);
628 /* Either we've been evicted, or the server has failed for
629 * some reason. Try to reconnect, and if that fails, punt to the
631 if ((rc == -ENOTCONN) || (rc == -ENODEV)) {
632 if (req->rq_send_state != LUSTRE_IMP_FULL ||
633 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
637 ptlrpc_request_handle_notconn(req);
642 /* Store transno in reqmsg for replay. */
643 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
644 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
646 if (req->rq_import->imp_replayable) {
647 spin_lock(&imp->imp_lock);
648 /* no point in adding already-committed requests to the replay
649 * list, we will just remove them immediately. b=9829 */
650 if (req->rq_transno != 0 &&
652 lustre_msg_get_last_committed(req->rq_repmsg) ||
654 ptlrpc_retain_replayable_request(req, imp);
655 else if (req->rq_commit_cb != NULL) {
656 spin_unlock(&imp->imp_lock);
657 req->rq_commit_cb(req);
658 spin_lock(&imp->imp_lock);
661 /* Replay-enabled imports return commit-status information. */
662 if (lustre_msg_get_last_committed(req->rq_repmsg))
663 imp->imp_peer_committed_transno =
664 lustre_msg_get_last_committed(req->rq_repmsg);
665 ptlrpc_free_committed(imp);
666 spin_unlock(&imp->imp_lock);
672 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
674 struct obd_import *imp;
678 LASSERT(req->rq_phase == RQ_PHASE_NEW);
679 req->rq_phase = RQ_PHASE_RPC;
681 imp = req->rq_import;
682 spin_lock(&imp->imp_lock);
684 req->rq_import_generation = imp->imp_generation;
686 if (ptlrpc_import_delay_req(imp, req, &rc)) {
687 spin_lock (&req->rq_lock);
689 spin_unlock (&req->rq_lock);
691 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
693 lustre_msg_get_status(req->rq_reqmsg) ,
694 ptlrpc_import_state_name(req->rq_send_state),
695 ptlrpc_import_state_name(imp->imp_state));
696 LASSERT(list_empty (&req->rq_list));
698 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
699 spin_unlock(&imp->imp_lock);
704 spin_unlock(&imp->imp_lock);
706 req->rq_phase = RQ_PHASE_INTERPRET;
710 /* XXX this is the same as ptlrpc_queue_wait */
711 LASSERT(list_empty(&req->rq_list));
712 list_add_tail(&req->rq_list, &imp->imp_sending_list);
713 spin_unlock(&imp->imp_lock);
715 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
716 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
717 " %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
718 imp->imp_obd->obd_uuid.uuid,
719 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
720 libcfs_nid2str(imp->imp_connection->c_peer.nid),
721 lustre_msg_get_opc(req->rq_reqmsg));
723 rc = ptl_send_rpc(req, 0);
725 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
732 /* this sends any unsent RPCs in @set and returns TRUE if all are sent */
733 int ptlrpc_check_set(struct ptlrpc_request_set *set)
735 struct list_head *tmp;
736 int force_timer_recalc = 0;
739 if (set->set_remaining == 0)
742 list_for_each(tmp, &set->set_requests) {
743 struct ptlrpc_request *req =
744 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
745 struct obd_import *imp = req->rq_import;
748 if (req->rq_phase == RQ_PHASE_NEW &&
749 ptlrpc_send_new_req(req)) {
750 force_timer_recalc = 1;
753 if (!(req->rq_phase == RQ_PHASE_RPC ||
754 req->rq_phase == RQ_PHASE_BULK ||
755 req->rq_phase == RQ_PHASE_INTERPRET ||
756 req->rq_phase == RQ_PHASE_COMPLETE)) {
757 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
761 if (req->rq_phase == RQ_PHASE_COMPLETE)
764 if (req->rq_phase == RQ_PHASE_INTERPRET)
765 GOTO(interpret, req->rq_status);
767 if (req->rq_net_err && !req->rq_timedout)
768 ptlrpc_expire_one_request(req);
771 ptlrpc_unregister_reply(req);
772 if (req->rq_status == 0)
773 req->rq_status = -EIO;
774 req->rq_phase = RQ_PHASE_INTERPRET;
776 spin_lock(&imp->imp_lock);
777 list_del_init(&req->rq_list);
778 spin_unlock(&imp->imp_lock);
780 GOTO(interpret, req->rq_status);
783 /* ptlrpc_queue_wait->l_wait_event guarantees that rq_intr
784 * will only be set after rq_timedout, but the oig waiting
785 * path sets rq_intr irrespective of whether ptlrpcd has
786 * seen a timeout. our policy is to only interpret
787 * interrupted rpcs after they have timed out */
788 if (req->rq_intr && (req->rq_timedout || req->rq_waiting)) {
789 /* NB could be on delayed list */
790 ptlrpc_unregister_reply(req);
791 req->rq_status = -EINTR;
792 req->rq_phase = RQ_PHASE_INTERPRET;
794 spin_lock(&imp->imp_lock);
795 list_del_init(&req->rq_list);
796 spin_unlock(&imp->imp_lock);
798 GOTO(interpret, req->rq_status);
801 if (req->rq_phase == RQ_PHASE_RPC) {
802 if (req->rq_timedout||req->rq_waiting||req->rq_resend) {
805 ptlrpc_unregister_reply(req);
807 spin_lock(&imp->imp_lock);
809 if (ptlrpc_import_delay_req(imp, req, &status)){
810 spin_unlock(&imp->imp_lock);
814 list_del_init(&req->rq_list);
816 req->rq_status = status;
817 req->rq_phase = RQ_PHASE_INTERPRET;
818 spin_unlock(&imp->imp_lock);
819 GOTO(interpret, req->rq_status);
821 if (req->rq_no_resend) {
822 req->rq_status = -ENOTCONN;
823 req->rq_phase = RQ_PHASE_INTERPRET;
824 spin_unlock(&imp->imp_lock);
825 GOTO(interpret, req->rq_status);
827 list_add_tail(&req->rq_list,
828 &imp->imp_sending_list);
830 spin_unlock(&imp->imp_lock);
833 if (req->rq_resend) {
834 lustre_msg_add_flags(req->rq_reqmsg,
837 __u64 old_xid = req->rq_xid;
839 ptlrpc_unregister_bulk (req);
841 /* ensure previous bulk fails */
842 req->rq_xid = ptlrpc_next_xid();
843 CDEBUG(D_HA, "resend bulk "
846 old_xid, req->rq_xid);
850 rc = ptl_send_rpc(req, 0);
852 DEBUG_REQ(D_HA, req, "send failed (%d)",
854 force_timer_recalc = 1;
857 /* need to reset the timeout */
858 force_timer_recalc = 1;
861 /* Still waiting for a reply? */
862 if (ptlrpc_client_receiving_reply(req))
865 /* Did we actually receive a reply? */
866 if (!ptlrpc_client_replied(req))
869 spin_lock(&imp->imp_lock);
870 list_del_init(&req->rq_list);
871 spin_unlock(&imp->imp_lock);
873 req->rq_status = after_reply(req);
874 if (req->rq_resend) {
875 /* Add this req to the delayed list so
876 it can be errored if the import is
877 evicted after recovery. */
878 spin_lock(&imp->imp_lock);
879 list_add_tail(&req->rq_list,
880 &imp->imp_delayed_list);
881 spin_unlock(&imp->imp_lock);
885 /* If there is no bulk associated with this request,
886 * then we're done and should let the interpreter
887 * process the reply. Similarly if the RPC returned
888 * an error, and therefore the bulk will never arrive.
890 if (req->rq_bulk == NULL || req->rq_status != 0) {
891 req->rq_phase = RQ_PHASE_INTERPRET;
892 GOTO(interpret, req->rq_status);
895 req->rq_phase = RQ_PHASE_BULK;
898 LASSERT(req->rq_phase == RQ_PHASE_BULK);
899 if (ptlrpc_bulk_active(req->rq_bulk))
902 if (!req->rq_bulk->bd_success) {
903 /* The RPC reply arrived OK, but the bulk screwed
904 * up! Dead wierd since the server told us the RPC
905 * was good after getting the REPLY for her GET or
906 * the ACK for her PUT. */
907 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
911 req->rq_phase = RQ_PHASE_INTERPRET;
914 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
915 LASSERT(!req->rq_receiving_reply);
917 ptlrpc_unregister_reply(req);
918 if (req->rq_bulk != NULL)
919 ptlrpc_unregister_bulk (req);
921 req->rq_phase = RQ_PHASE_COMPLETE;
923 if (req->rq_interpret_reply != NULL) {
924 int (*interpreter)(struct ptlrpc_request *,void *,int) =
925 req->rq_interpret_reply;
926 req->rq_status = interpreter(req, &req->rq_async_args,
930 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
931 "opc %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
932 imp->imp_obd->obd_uuid.uuid,
933 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
934 libcfs_nid2str(imp->imp_connection->c_peer.nid),
935 lustre_msg_get_opc(req->rq_reqmsg));
937 set->set_remaining--;
939 atomic_dec(&imp->imp_inflight);
940 cfs_waitq_signal(&imp->imp_recovery_waitq);
943 /* If we hit an error, we want to recover promptly. */
944 RETURN(set->set_remaining == 0 || force_timer_recalc);
947 int ptlrpc_expire_one_request(struct ptlrpc_request *req)
949 struct obd_import *imp = req->rq_import;
953 DEBUG_REQ(D_ERROR|D_NETERROR, req, "timeout (sent at %lu, %lus ago)",
954 (long)req->rq_sent, CURRENT_SECONDS - req->rq_sent);
956 if (imp != NULL && obd_debug_peer_on_timeout)
957 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
959 spin_lock(&req->rq_lock);
960 req->rq_timedout = 1;
961 spin_unlock(&req->rq_lock);
963 ptlrpc_unregister_reply (req);
965 if (obd_dump_on_timeout)
966 libcfs_debug_dumplog();
968 if (req->rq_bulk != NULL)
969 ptlrpc_unregister_bulk (req);
972 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
976 /* The DLM server doesn't want recovery run on its imports. */
977 if (imp->imp_dlm_fake)
980 /* If this request is for recovery or other primordial tasks,
981 * then error it out here. */
982 if (req->rq_send_state != LUSTRE_IMP_FULL ||
983 imp->imp_obd->obd_no_recov) {
984 spin_lock(&req->rq_lock);
985 req->rq_status = -ETIMEDOUT;
987 spin_unlock(&req->rq_lock);
991 /* if request can't be resend we can't wait answer after timeout */
992 if (req->rq_no_resend) {
993 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
997 ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1002 int ptlrpc_expired_set(void *data)
1004 struct ptlrpc_request_set *set = data;
1005 struct list_head *tmp;
1006 time_t now = CURRENT_SECONDS;
1009 LASSERT(set != NULL);
1011 /* A timeout expired; see which reqs it applies to... */
1012 list_for_each (tmp, &set->set_requests) {
1013 struct ptlrpc_request *req =
1014 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1016 /* request in-flight? */
1017 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting &&
1019 (req->rq_phase == RQ_PHASE_BULK)))
1022 if (req->rq_timedout || /* already dealt with */
1023 req->rq_sent + req->rq_timeout > now) /* not expired */
1026 /* deal with this guy */
1027 ptlrpc_expire_one_request (req);
1030 /* When waiting for a whole set, we always to break out of the
1031 * sleep so we can recalculate the timeout, or enable interrupts
1032 * iff everyone's timed out.
1037 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1039 spin_lock(&req->rq_lock);
1041 spin_unlock(&req->rq_lock);
1044 void ptlrpc_interrupted_set(void *data)
1046 struct ptlrpc_request_set *set = data;
1047 struct list_head *tmp;
1049 LASSERT(set != NULL);
1050 CERROR("INTERRUPTED SET %p\n", set);
1052 list_for_each(tmp, &set->set_requests) {
1053 struct ptlrpc_request *req =
1054 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1056 if (req->rq_phase != RQ_PHASE_RPC)
1059 ptlrpc_mark_interrupted(req);
1063 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1065 struct list_head *tmp;
1066 time_t now = CURRENT_SECONDS;
1069 struct ptlrpc_request *req;
1072 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1074 list_for_each(tmp, &set->set_requests) {
1075 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1077 /* request in-flight? */
1078 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting) ||
1079 (req->rq_phase == RQ_PHASE_BULK)))
1082 if (req->rq_timedout) /* already timed out */
1085 deadline = req->rq_sent + req->rq_timeout;
1086 if (deadline <= now) /* actually expired already */
1087 timeout = 1; /* ASAP */
1088 else if (timeout == 0 || timeout > deadline - now)
1089 timeout = deadline - now;
1094 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
1096 struct list_head *tmp;
1097 struct ptlrpc_request *req;
1098 struct l_wait_info lwi;
1102 if (list_empty(&set->set_requests))
1105 list_for_each(tmp, &set->set_requests) {
1106 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1107 if (req->rq_phase == RQ_PHASE_NEW)
1108 (void)ptlrpc_send_new_req(req);
1112 timeout = ptlrpc_set_next_timeout(set);
1114 /* wait until all complete, interrupted, or an in-flight
1116 CDEBUG(D_HA, "set %p going to sleep for %d seconds\n",
1118 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout ? timeout : 1),
1120 ptlrpc_interrupted_set, set);
1121 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(set), &lwi);
1123 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
1125 /* -EINTR => all requests have been flagged rq_intr so next
1127 * -ETIMEOUTD => someone timed out. When all reqs have
1128 * timed out, signals are enabled allowing completion with
1130 * I don't really care if we go once more round the loop in
1131 * the error cases -eeb. */
1132 } while (rc != 0 || set->set_remaining != 0);
1134 LASSERT(set->set_remaining == 0);
1137 list_for_each(tmp, &set->set_requests) {
1138 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1140 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
1141 if (req->rq_status != 0)
1142 rc = req->rq_status;
1145 if (set->set_interpret != NULL) {
1146 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
1148 rc = interpreter (set, set->set_arg, rc);
1154 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
1156 struct ptlrpc_request_pool *pool = request->rq_pool;
1158 spin_lock(&pool->prp_lock);
1159 list_add_tail(&request->rq_list, &pool->prp_req_list);
1160 spin_unlock(&pool->prp_lock);
1163 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
1166 if (request == NULL) {
1171 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
1172 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
1173 LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
1174 LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
1176 /* We must take it off the imp_replay_list first. Otherwise, we'll set
1177 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
1178 if (request->rq_import != NULL) {
1180 spin_lock(&request->rq_import->imp_lock);
1181 list_del_init(&request->rq_replay_list);
1183 spin_unlock(&request->rq_import->imp_lock);
1185 LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
1187 if (atomic_read(&request->rq_refcount) != 0) {
1188 DEBUG_REQ(D_ERROR, request,
1189 "freeing request with nonzero refcount");
1193 if (request->rq_repmsg != NULL) {
1194 OBD_FREE(request->rq_repmsg, request->rq_replen);
1195 request->rq_repmsg = NULL;
1197 if (request->rq_export != NULL) {
1198 class_export_put(request->rq_export);
1199 request->rq_export = NULL;
1201 if (request->rq_import != NULL) {
1202 class_import_put(request->rq_import);
1203 request->rq_import = NULL;
1205 if (request->rq_bulk != NULL)
1206 ptlrpc_free_bulk(request->rq_bulk);
1208 if (request->rq_pool) {
1209 __ptlrpc_free_req_to_pool(request);
1211 if (request->rq_reqmsg != NULL) {
1212 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
1213 request->rq_reqmsg = NULL;
1215 OBD_FREE(request, sizeof(*request));
1220 void ptlrpc_free_req(struct ptlrpc_request *request)
1222 __ptlrpc_free_req(request, 0);
1225 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
1226 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
1228 LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
1229 (void)__ptlrpc_req_finished(request, 1);
1232 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
1235 if (request == NULL)
1238 if (request == LP_POISON ||
1239 request->rq_reqmsg == LP_POISON) {
1240 CERROR("dereferencing freed request (bug 575)\n");
1245 DEBUG_REQ(D_INFO, request, "refcount now %u",
1246 atomic_read(&request->rq_refcount) - 1);
1248 if (atomic_dec_and_test(&request->rq_refcount)) {
1249 __ptlrpc_free_req(request, locked);
1256 void ptlrpc_req_finished(struct ptlrpc_request *request)
1258 __ptlrpc_req_finished(request, 0);
1261 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
1263 return request->rq_xid;
1265 EXPORT_SYMBOL(ptlrpc_req_xid);
1267 /* Disengage the client's reply buffer from the network
1268 * NB does _NOT_ unregister any client-side bulk.
1269 * IDEMPOTENT, but _not_ safe against concurrent callers.
1270 * The request owner (i.e. the thread doing the I/O) must call...
1272 void ptlrpc_unregister_reply (struct ptlrpc_request *request)
1276 struct l_wait_info lwi;
1279 LASSERT(!in_interrupt ()); /* might sleep */
1281 if (!ptlrpc_client_receiving_reply(request))
1284 LNetMDUnlink (request->rq_reply_md_h);
1286 /* We have to l_wait_event() whatever the result, to give liblustre
1287 * a chance to run reply_in_callback() */
1289 if (request->rq_set != NULL)
1290 wq = &request->rq_set->set_waitq;
1292 wq = &request->rq_reply_waitq;
1295 /* Network access will complete in finite time but the HUGE
1296 * timeout lets us CWARN for visibility of sluggish NALs */
1297 lwi = LWI_TIMEOUT(cfs_time_seconds(300), NULL, NULL);
1298 rc = l_wait_event (*wq, !ptlrpc_client_receiving_reply(request), &lwi);
1302 LASSERT (rc == -ETIMEDOUT);
1303 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout");
1308 /* caller must hold imp->imp_lock */
1309 void ptlrpc_free_committed(struct obd_import *imp)
1311 struct list_head *tmp, *saved;
1312 struct ptlrpc_request *req;
1313 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1316 LASSERT(imp != NULL);
1318 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1321 if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
1322 imp->imp_generation == imp->imp_last_generation_checked) {
1323 CDEBUG(D_HA, "%s: skip recheck for last_committed "LPU64"\n",
1324 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1328 CDEBUG(D_HA, "%s: committing for last_committed "LPU64" gen %d\n",
1329 imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
1330 imp->imp_generation);
1331 imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
1332 imp->imp_last_generation_checked = imp->imp_generation;
1334 list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1335 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1337 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
1338 LASSERT(req != last_req);
1341 if (req->rq_import_generation < imp->imp_generation) {
1342 DEBUG_REQ(D_HA, req, "freeing request with old gen");
1346 if (req->rq_replay) {
1347 DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
1351 /* not yet committed */
1352 if (req->rq_transno > imp->imp_peer_committed_transno) {
1353 DEBUG_REQ(D_HA, req, "stopping search");
1357 DEBUG_REQ(D_HA, req, "committing (last_committed "LPU64")",
1358 imp->imp_peer_committed_transno);
1360 spin_lock(&req->rq_lock);
1362 spin_unlock(&req->rq_lock);
1363 if (req->rq_commit_cb != NULL)
1364 req->rq_commit_cb(req);
1365 list_del_init(&req->rq_replay_list);
1366 __ptlrpc_req_finished(req, 1);
1373 void ptlrpc_cleanup_client(struct obd_import *imp)
1380 void ptlrpc_resend_req(struct ptlrpc_request *req)
1382 DEBUG_REQ(D_HA, req, "going to resend");
1383 lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
1384 req->rq_status = -EAGAIN;
1386 spin_lock(&req->rq_lock);
1388 req->rq_net_err = 0;
1389 req->rq_timedout = 0;
1391 __u64 old_xid = req->rq_xid;
1393 /* ensure previous bulk fails */
1394 req->rq_xid = ptlrpc_next_xid();
1395 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
1396 old_xid, req->rq_xid);
1398 ptlrpc_wake_client_req(req);
1399 spin_unlock(&req->rq_lock);
1402 /* XXX: this function and rq_status are currently unused */
1403 void ptlrpc_restart_req(struct ptlrpc_request *req)
1405 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1406 req->rq_status = -ERESTARTSYS;
1408 spin_lock(&req->rq_lock);
1409 req->rq_restart = 1;
1410 req->rq_timedout = 0;
1411 ptlrpc_wake_client_req(req);
1412 spin_unlock(&req->rq_lock);
1415 static int expired_request(void *data)
1417 struct ptlrpc_request *req = data;
1420 RETURN(ptlrpc_expire_one_request(req));
1423 static void interrupted_request(void *data)
1425 struct ptlrpc_request *req = data;
1426 DEBUG_REQ(D_HA, req, "request interrupted");
1427 spin_lock(&req->rq_lock);
1429 spin_unlock(&req->rq_lock);
1432 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1435 atomic_inc(&req->rq_refcount);
1439 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1440 struct obd_import *imp)
1442 struct list_head *tmp;
1444 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1446 /* clear this for new requests that were resent as well
1447 as resent replayed requests. */
1448 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1450 /* don't re-add requests that have been replayed */
1451 if (!list_empty(&req->rq_replay_list))
1454 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
1456 LASSERT(imp->imp_replayable);
1457 /* Balanced in ptlrpc_free_committed, usually. */
1458 ptlrpc_request_addref(req);
1459 list_for_each_prev(tmp, &imp->imp_replay_list) {
1460 struct ptlrpc_request *iter =
1461 list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1463 /* We may have duplicate transnos if we create and then
1464 * open a file, or for closes retained if to match creating
1465 * opens, so use req->rq_xid as a secondary key.
1466 * (See bugs 684, 685, and 428.)
1467 * XXX no longer needed, but all opens need transnos!
1469 if (iter->rq_transno > req->rq_transno)
1472 if (iter->rq_transno == req->rq_transno) {
1473 LASSERT(iter->rq_xid != req->rq_xid);
1474 if (iter->rq_xid > req->rq_xid)
1478 list_add(&req->rq_replay_list, &iter->rq_replay_list);
1482 list_add_tail(&req->rq_replay_list, &imp->imp_replay_list);
1485 int ptlrpc_queue_wait(struct ptlrpc_request *req)
1489 struct l_wait_info lwi;
1490 struct obd_import *imp = req->rq_import;
1491 cfs_duration_t timeout = 0;
1494 LASSERT(req->rq_set == NULL);
1495 LASSERT(!req->rq_receiving_reply);
1496 atomic_inc(&imp->imp_inflight);
1498 /* for distributed debugging */
1499 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
1500 LASSERT(imp->imp_obd != NULL);
1501 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc "
1502 "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1503 imp->imp_obd->obd_uuid.uuid,
1504 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1505 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1506 lustre_msg_get_opc(req->rq_reqmsg));
1508 /* Mark phase here for a little debug help */
1509 req->rq_phase = RQ_PHASE_RPC;
1511 spin_lock(&imp->imp_lock);
1512 req->rq_import_generation = imp->imp_generation;
1514 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1515 list_del(&req->rq_list);
1517 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1518 spin_unlock(&imp->imp_lock);
1520 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%s != %s)",
1522 ptlrpc_import_state_name(req->rq_send_state),
1523 ptlrpc_import_state_name(imp->imp_state));
1524 lwi = LWI_INTR(interrupted_request, req);
1525 rc = l_wait_event(req->rq_reply_waitq,
1526 (req->rq_send_state == imp->imp_state ||
1527 req->rq_err || req->rq_intr),
1529 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%s == %s or %d/%d == 1)",
1531 ptlrpc_import_state_name(imp->imp_state),
1532 ptlrpc_import_state_name(req->rq_send_state),
1533 req->rq_err, req->rq_intr);
1535 spin_lock(&imp->imp_lock);
1536 list_del_init(&req->rq_list);
1541 else if (req->rq_intr) {
1544 else if (req->rq_no_resend) {
1545 spin_unlock(&imp->imp_lock);
1546 GOTO(out, rc = -ETIMEDOUT);
1554 list_del_init(&req->rq_list);
1555 spin_unlock(&imp->imp_lock);
1556 req->rq_status = rc; // XXX this ok?
1560 if (req->rq_resend) {
1561 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
1563 if (req->rq_bulk != NULL) {
1564 ptlrpc_unregister_bulk (req);
1566 /* bulk requests are supposed to be
1567 * idempotent, so we are free to bump the xid
1568 * here, which we need to do before
1569 * registering the bulk again (bug 6371).
1570 * print the old xid first for sanity.
1572 DEBUG_REQ(D_HA, req, "bumping xid for bulk: ");
1573 req->rq_xid = ptlrpc_next_xid();
1576 DEBUG_REQ(D_HA, req, "resending: ");
1579 /* XXX this is the same as ptlrpc_set_wait */
1580 LASSERT(list_empty(&req->rq_list));
1581 list_add_tail(&req->rq_list, &imp->imp_sending_list);
1582 spin_unlock(&imp->imp_lock);
1584 rc = ptl_send_rpc(req, 0);
1586 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
1589 timeout = cfs_timeout_cap(cfs_time_seconds(req->rq_timeout));
1590 DEBUG_REQ(D_NET, req,
1591 "-- sleeping for "CFS_DURATION_T" jiffies", timeout);
1593 lwi = LWI_TIMEOUT_INTR(timeout, expired_request, interrupted_request,
1595 l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req), &lwi);
1597 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:opc "
1598 "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1599 imp->imp_obd->obd_uuid.uuid,
1600 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1601 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1602 lustre_msg_get_opc(req->rq_reqmsg));
1604 spin_lock(&imp->imp_lock);
1605 list_del_init(&req->rq_list);
1606 spin_unlock(&imp->imp_lock);
1608 /* If the reply was received normally, this just grabs the spinlock
1609 * (ensuring the reply callback has returned), sees that
1610 * req->rq_receiving_reply is clear and returns. */
1611 ptlrpc_unregister_reply (req);
1614 GOTO(out, rc = -EIO);
1616 /* Resend if we need to, unless we were interrupted. */
1617 if (req->rq_resend && !req->rq_intr) {
1618 /* ...unless we were specifically told otherwise. */
1619 if (req->rq_no_resend)
1620 GOTO(out, rc = -ETIMEDOUT);
1621 spin_lock(&imp->imp_lock);
1626 /* Should only be interrupted if we timed out. */
1627 if (!req->rq_timedout)
1628 DEBUG_REQ(D_ERROR, req,
1629 "rq_intr set but rq_timedout not");
1630 GOTO(out, rc = -EINTR);
1633 if (req->rq_timedout) { /* non-recoverable timeout */
1634 GOTO(out, rc = -ETIMEDOUT);
1637 if (!req->rq_replied) {
1638 /* How can this be? -eeb */
1639 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
1641 GOTO(out, rc = req->rq_status);
1644 rc = after_reply(req);
1645 /* NB may return +ve success rc */
1646 if (req->rq_resend) {
1647 spin_lock(&imp->imp_lock);
1652 if (req->rq_bulk != NULL) {
1654 /* success so far. Note that anything going wrong
1655 * with bulk now, is EXTREMELY strange, since the
1656 * server must have believed that the bulk
1657 * tranferred OK before she replied with success to
1659 lwi = LWI_TIMEOUT(timeout, NULL, NULL);
1660 brc = l_wait_event(req->rq_reply_waitq,
1661 !ptlrpc_bulk_active(req->rq_bulk),
1663 LASSERT(brc == 0 || brc == -ETIMEDOUT);
1665 LASSERT(brc == -ETIMEDOUT);
1666 DEBUG_REQ(D_ERROR, req, "bulk timed out");
1668 } else if (!req->rq_bulk->bd_success) {
1669 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1674 ptlrpc_unregister_bulk (req);
1677 LASSERT(!req->rq_receiving_reply);
1678 req->rq_phase = RQ_PHASE_INTERPRET;
1680 atomic_dec(&imp->imp_inflight);
1681 cfs_waitq_signal(&imp->imp_recovery_waitq);
1685 struct ptlrpc_replay_async_args {
1687 int praa_old_status;
1690 static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
1691 void * data, int rc)
1693 struct ptlrpc_replay_async_args *aa = data;
1694 struct obd_import *imp = req->rq_import;
1697 atomic_dec(&imp->imp_replay_inflight);
1699 if (!req->rq_replied) {
1700 CERROR("request replay timed out, restarting recovery\n");
1701 GOTO(out, rc = -ETIMEDOUT);
1704 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
1705 lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN)
1706 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
1708 /* The transno had better not change over replay. */
1709 LASSERT(lustre_msg_get_transno(req->rq_reqmsg) ==
1710 lustre_msg_get_transno(req->rq_repmsg));
1712 DEBUG_REQ(D_HA, req, "got rep");
1714 /* let the callback do fixups, possibly including in the request */
1715 if (req->rq_replay_cb)
1716 req->rq_replay_cb(req);
1718 if (req->rq_replied &&
1719 lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
1720 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
1721 lustre_msg_get_status(req->rq_repmsg),
1722 aa->praa_old_status);
1724 /* Put it back for re-replay. */
1725 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
1728 spin_lock(&imp->imp_lock);
1729 imp->imp_last_replay_transno = req->rq_transno;
1730 spin_unlock(&imp->imp_lock);
1732 /* continue with recovery */
1733 rc = ptlrpc_import_recovery_state_machine(imp);
1735 req->rq_send_state = aa->praa_old_state;
1738 /* this replay failed, so restart recovery */
1739 ptlrpc_connect_import(imp, NULL);
1745 int ptlrpc_replay_req(struct ptlrpc_request *req)
1747 struct ptlrpc_replay_async_args *aa;
1750 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
1752 /* Not handling automatic bulk replay yet (or ever?) */
1753 LASSERT(req->rq_bulk == NULL);
1755 DEBUG_REQ(D_HA, req, "REPLAY");
1757 LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
1758 aa = (struct ptlrpc_replay_async_args *)&req->rq_async_args;
1759 memset(aa, 0, sizeof *aa);
1761 /* Prepare request to be resent with ptlrpcd */
1762 aa->praa_old_state = req->rq_send_state;
1763 req->rq_send_state = LUSTRE_IMP_REPLAY;
1764 req->rq_phase = RQ_PHASE_NEW;
1765 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
1768 req->rq_interpret_reply = ptlrpc_replay_interpret;
1769 atomic_inc(&req->rq_import->imp_replay_inflight);
1770 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
1772 ptlrpcd_add_req(req);
1776 void ptlrpc_abort_inflight(struct obd_import *imp)
1778 struct list_head *tmp, *n;
1781 /* Make sure that no new requests get processed for this import.
1782 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
1783 * this flag and then putting requests on sending_list or delayed_list.
1785 spin_lock(&imp->imp_lock);
1787 /* XXX locking? Maybe we should remove each request with the list
1788 * locked? Also, how do we know if the requests on the list are
1789 * being freed at this time?
1791 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
1792 struct ptlrpc_request *req =
1793 list_entry(tmp, struct ptlrpc_request, rq_list);
1795 DEBUG_REQ(D_HA, req, "inflight");
1797 spin_lock (&req->rq_lock);
1798 if (req->rq_import_generation < imp->imp_generation) {
1800 ptlrpc_wake_client_req(req);
1802 spin_unlock (&req->rq_lock);
1805 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
1806 struct ptlrpc_request *req =
1807 list_entry(tmp, struct ptlrpc_request, rq_list);
1809 DEBUG_REQ(D_HA, req, "aborting waiting req");
1811 spin_lock (&req->rq_lock);
1812 if (req->rq_import_generation < imp->imp_generation) {
1814 ptlrpc_wake_client_req(req);
1816 spin_unlock (&req->rq_lock);
1819 /* Last chance to free reqs left on the replay list, but we
1820 * will still leak reqs that haven't committed. */
1821 if (imp->imp_replayable)
1822 ptlrpc_free_committed(imp);
1824 spin_unlock(&imp->imp_lock);
1829 static __u64 ptlrpc_last_xid = 0;
1830 spinlock_t ptlrpc_last_xid_lock;
1832 __u64 ptlrpc_next_xid(void)
1835 spin_lock(&ptlrpc_last_xid_lock);
1836 tmp = ++ptlrpc_last_xid;
1837 spin_unlock(&ptlrpc_last_xid_lock);
1841 __u64 ptlrpc_sample_next_xid(void)
1844 spin_lock(&ptlrpc_last_xid_lock);
1845 tmp = ptlrpc_last_xid + 1;
1846 spin_unlock(&ptlrpc_last_xid_lock);
1849 EXPORT_SYMBOL(ptlrpc_sample_next_xid);