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 sptlrpc_enc_pool_put_pages(desc);
186 class_export_put(desc->bd_export);
188 class_import_put(desc->bd_import);
190 OBD_FREE(desc, offsetof(struct ptlrpc_bulk_desc,
191 bd_iov[desc->bd_max_iov]));
195 void ptlrpc_free_rq_pool(struct ptlrpc_request_pool *pool)
197 struct list_head *l, *tmp;
198 struct ptlrpc_request *req;
203 list_for_each_safe(l, tmp, &pool->prp_req_list) {
204 req = list_entry(l, struct ptlrpc_request, rq_list);
205 list_del(&req->rq_list);
206 LASSERT(req->rq_reqbuf);
207 LASSERT(req->rq_reqbuf_len == pool->prp_rq_size);
208 OBD_FREE(req->rq_reqbuf, pool->prp_rq_size);
209 OBD_FREE(req, sizeof(*req));
211 OBD_FREE(pool, sizeof(*pool));
214 void ptlrpc_add_rqs_to_pool(struct ptlrpc_request_pool *pool, int num_rq)
219 while (size < pool->prp_rq_size + SPTLRPC_MAX_PAYLOAD)
222 LASSERTF(list_empty(&pool->prp_req_list) || size == pool->prp_rq_size,
223 "Trying to change pool size with nonempty pool "
224 "from %d to %d bytes\n", pool->prp_rq_size, size);
226 spin_lock(&pool->prp_lock);
227 pool->prp_rq_size = size;
228 for (i = 0; i < num_rq; i++) {
229 struct ptlrpc_request *req;
230 struct lustre_msg *msg;
232 spin_unlock(&pool->prp_lock);
233 OBD_ALLOC(req, sizeof(struct ptlrpc_request));
236 OBD_ALLOC_GFP(msg, size, CFS_ALLOC_STD);
238 OBD_FREE(req, sizeof(struct ptlrpc_request));
241 req->rq_reqbuf = msg;
242 req->rq_reqbuf_len = size;
244 spin_lock(&pool->prp_lock);
245 list_add_tail(&req->rq_list, &pool->prp_req_list);
247 spin_unlock(&pool->prp_lock);
251 struct ptlrpc_request_pool *ptlrpc_init_rq_pool(int num_rq, int msgsize,
252 void (*populate_pool)(struct ptlrpc_request_pool *, int))
254 struct ptlrpc_request_pool *pool;
256 OBD_ALLOC(pool, sizeof (struct ptlrpc_request_pool));
260 /* Request next power of two for the allocation, because internally
261 kernel would do exactly this */
263 spin_lock_init(&pool->prp_lock);
264 CFS_INIT_LIST_HEAD(&pool->prp_req_list);
265 pool->prp_rq_size = msgsize;
266 pool->prp_populate = populate_pool;
268 populate_pool(pool, num_rq);
270 if (list_empty(&pool->prp_req_list)) {
271 /* have not allocated a single request for the pool */
272 OBD_FREE(pool, sizeof (struct ptlrpc_request_pool));
278 static struct ptlrpc_request *ptlrpc_prep_req_from_pool(struct ptlrpc_request_pool *pool)
280 struct ptlrpc_request *request;
281 struct lustre_msg *reqbuf;
286 spin_lock(&pool->prp_lock);
288 /* See if we have anything in a pool, and bail out if nothing,
289 * in writeout path, where this matters, this is safe to do, because
290 * nothing is lost in this case, and when some in-flight requests
291 * complete, this code will be called again. */
292 if (unlikely(list_empty(&pool->prp_req_list))) {
293 spin_unlock(&pool->prp_lock);
297 request = list_entry(pool->prp_req_list.next, struct ptlrpc_request,
299 list_del(&request->rq_list);
300 spin_unlock(&pool->prp_lock);
302 LASSERT(request->rq_reqbuf);
303 LASSERT(request->rq_pool);
305 reqbuf = request->rq_reqbuf;
306 memset(request, 0, sizeof(*request));
307 request->rq_reqbuf = reqbuf;
308 request->rq_reqbuf_len = pool->prp_rq_size;
309 request->rq_pool = pool;
313 static void __ptlrpc_free_req_to_pool(struct ptlrpc_request *request)
315 struct ptlrpc_request_pool *pool = request->rq_pool;
317 spin_lock(&pool->prp_lock);
318 LASSERT(list_empty(&request->rq_list));
319 list_add_tail(&request->rq_list, &pool->prp_req_list);
320 spin_unlock(&pool->prp_lock);
323 struct ptlrpc_request *
324 ptlrpc_prep_req_pool(struct obd_import *imp, __u32 version, int opcode,
325 int count, int *lengths, char **bufs,
326 struct ptlrpc_request_pool *pool,
327 struct ptlrpc_cli_ctx *ctx)
329 struct ptlrpc_request *request = NULL;
333 /* The obd disconnected */
337 LASSERT(imp != LP_POISON);
338 LASSERT((unsigned long)imp->imp_client > 0x1000);
339 LASSERT(imp->imp_client != LP_POISON);
342 request = ptlrpc_prep_req_from_pool(pool);
345 OBD_ALLOC(request, sizeof(*request));
348 CERROR("request allocation out of memory\n");
352 request->rq_import = class_import_get(imp);
355 request->rq_cli_ctx = sptlrpc_cli_ctx_get(ctx);
357 rc = sptlrpc_req_get_ctx(request);
362 sptlrpc_req_set_flavor(request, opcode);
364 rc = lustre_pack_request(request, imp->imp_msg_magic, count, lengths,
367 LASSERT(!request->rq_pool);
371 lustre_msg_add_version(request->rq_reqmsg, version);
373 if (imp->imp_server_timeout)
374 request->rq_timeout = obd_timeout / 2;
376 request->rq_timeout = obd_timeout;
377 request->rq_send_state = LUSTRE_IMP_FULL;
378 request->rq_type = PTL_RPC_MSG_REQUEST;
379 request->rq_export = NULL;
381 request->rq_req_cbid.cbid_fn = request_out_callback;
382 request->rq_req_cbid.cbid_arg = request;
384 request->rq_reply_cbid.cbid_fn = reply_in_callback;
385 request->rq_reply_cbid.cbid_arg = request;
387 request->rq_phase = RQ_PHASE_NEW;
389 /* XXX FIXME bug 249 */
390 request->rq_request_portal = imp->imp_client->cli_request_portal;
391 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
393 spin_lock_init(&request->rq_lock);
394 CFS_INIT_LIST_HEAD(&request->rq_list);
395 CFS_INIT_LIST_HEAD(&request->rq_replay_list);
396 CFS_INIT_LIST_HEAD(&request->rq_mod_list);
397 CFS_INIT_LIST_HEAD(&request->rq_ctx_chain);
398 CFS_INIT_LIST_HEAD(&request->rq_set_chain);
399 cfs_waitq_init(&request->rq_reply_waitq);
400 request->rq_xid = ptlrpc_next_xid();
401 atomic_set(&request->rq_refcount, 1);
403 lustre_msg_set_opc(request->rq_reqmsg, opcode);
404 lustre_msg_set_flags(request->rq_reqmsg, 0);
408 sptlrpc_req_put_ctx(request, 1);
410 class_import_put(imp);
411 if (request->rq_pool)
412 __ptlrpc_free_req_to_pool(request);
414 OBD_FREE(request, sizeof(*request));
418 struct ptlrpc_request *
419 ptlrpc_prep_req(struct obd_import *imp, __u32 version, int opcode, int count,
420 int *lengths, char **bufs)
422 return ptlrpc_prep_req_pool(imp, version, opcode, count, lengths, bufs,
426 struct ptlrpc_request_set *ptlrpc_prep_set(void)
428 struct ptlrpc_request_set *set;
431 OBD_ALLOC(set, sizeof *set);
434 CFS_INIT_LIST_HEAD(&set->set_requests);
435 cfs_waitq_init(&set->set_waitq);
436 set->set_remaining = 0;
437 spin_lock_init(&set->set_new_req_lock);
438 CFS_INIT_LIST_HEAD(&set->set_new_requests);
443 /* Finish with this set; opposite of prep_set. */
444 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
446 struct list_head *tmp;
447 struct list_head *next;
452 /* Requests on the set should either all be completed, or all be new */
453 expected_phase = (set->set_remaining == 0) ?
454 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
455 list_for_each (tmp, &set->set_requests) {
456 struct ptlrpc_request *req =
457 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
459 LASSERT(req->rq_phase == expected_phase);
463 LASSERT(set->set_remaining == 0 || set->set_remaining == n);
465 list_for_each_safe(tmp, next, &set->set_requests) {
466 struct ptlrpc_request *req =
467 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
468 list_del_init(&req->rq_set_chain);
470 LASSERT(req->rq_phase == expected_phase);
472 if (req->rq_phase == RQ_PHASE_NEW) {
474 if (req->rq_interpret_reply != NULL) {
475 int (*interpreter)(struct ptlrpc_request *,
477 req->rq_interpret_reply;
479 /* higher level (i.e. LOV) failed;
480 * let the sub reqs clean up */
481 req->rq_status = -EBADR;
482 interpreter(req, &req->rq_async_args,
485 set->set_remaining--;
489 ptlrpc_req_finished (req);
492 LASSERT(set->set_remaining == 0);
494 OBD_FREE(set, sizeof(*set));
498 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
499 struct ptlrpc_request *req)
501 /* The set takes over the caller's request reference */
502 list_add_tail(&req->rq_set_chain, &set->set_requests);
504 set->set_remaining++;
506 atomic_inc(&req->rq_import->imp_inflight);
509 /* lock so many callers can add things, the context that owns the set
510 * is supposed to notice these and move them into the set proper. */
511 void ptlrpc_set_add_new_req(struct ptlrpc_request_set *set,
512 struct ptlrpc_request *req)
514 spin_lock(&set->set_new_req_lock);
515 /* The set takes over the caller's request reference */
516 list_add_tail(&req->rq_set_chain, &set->set_new_requests);
518 spin_unlock(&set->set_new_req_lock);
522 * Based on the current state of the import, determine if the request
523 * can be sent, is an error, or should be delayed.
525 * Returns true if this request should be delayed. If false, and
526 * *status is set, then the request can not be sent and *status is the
527 * error code. If false and status is 0, then request can be sent.
529 * The imp->imp_lock must be held.
531 static int ptlrpc_import_delay_req(struct obd_import *imp,
532 struct ptlrpc_request *req, int *status)
537 LASSERT (status != NULL);
540 if (req->rq_ctx_init || req->rq_ctx_fini) {
541 /* always allow ctx init/fini rpc go through */
542 } else if (imp->imp_state == LUSTRE_IMP_NEW) {
543 DEBUG_REQ(D_ERROR, req, "Uninitialized import.");
546 } else if (imp->imp_state == LUSTRE_IMP_CLOSED) {
547 DEBUG_REQ(D_ERROR, req, "IMP_CLOSED ");
549 } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING &&
550 imp->imp_state == LUSTRE_IMP_CONNECTING) {
551 /* allow CONNECT even if import is invalid */ ;
552 } else if (imp->imp_invalid) {
553 /* If the import has been invalidated (such as by an OST
554 * failure), the request must fail with -EIO. */
555 if (!imp->imp_deactive)
556 DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
558 } else if (req->rq_import_generation != imp->imp_generation) {
559 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
561 } else if (req->rq_send_state != imp->imp_state) {
562 if (imp->imp_obd->obd_no_recov || imp->imp_dlm_fake ||
564 *status = -EWOULDBLOCK;
572 static int ptlrpc_check_reply(struct ptlrpc_request *req)
577 /* serialise with network callback */
578 spin_lock(&req->rq_lock);
583 if (req->rq_net_err && !req->rq_timedout) {
584 spin_unlock(&req->rq_lock);
585 rc = ptlrpc_expire_one_request(req);
586 spin_lock(&req->rq_lock);
600 spin_unlock(&req->rq_lock);
601 DEBUG_REQ(D_NET, req, "rc = %d for", rc);
605 static int ptlrpc_check_status(struct ptlrpc_request *req)
610 err = lustre_msg_get_status(req->rq_repmsg);
611 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
612 struct obd_import *imp = req->rq_import;
613 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
614 LCONSOLE_ERROR_MSG(0x011,"an error occurred while communicating"
615 " with %s. The %s operation failed with %d\n",
616 libcfs_nid2str(imp->imp_connection->c_peer.nid),
617 ll_opcode2str(opc), err);
618 RETURN(err < 0 ? err : -EINVAL);
622 DEBUG_REQ(D_INFO, req, "status is %d", err);
623 } else if (err > 0) {
624 /* XXX: translate this error from net to host */
625 DEBUG_REQ(D_INFO, req, "status is %d", err);
631 static int after_reply(struct ptlrpc_request *req)
633 struct obd_import *imp = req->rq_import;
637 LASSERT(!req->rq_receiving_reply);
638 LASSERT(req->rq_nob_received <= req->rq_repbuf_len);
640 /* NB Until this point, the whole of the incoming message,
641 * including buflens, status etc is in the sender's byte order. */
644 /* Clear reply swab mask; this is a new reply in sender's byte order */
645 req->rq_rep_swab_mask = 0;
647 rc = sptlrpc_cli_unwrap_reply(req);
649 DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
653 /* security layer unwrap might ask resend this request */
657 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
659 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
663 rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
665 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
669 if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
670 lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
671 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
672 lustre_msg_get_type(req->rq_repmsg));
676 rc = ptlrpc_check_status(req);
677 imp->imp_connect_error = rc;
679 /* Either we've been evicted, or the server has failed for
680 * some reason. Try to reconnect, and if that fails, punt to the
682 if ((rc == -ENOTCONN) || (rc == -ENODEV)) {
683 if (req->rq_send_state != LUSTRE_IMP_FULL ||
684 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
688 ptlrpc_request_handle_notconn(req);
693 /* Store transno in reqmsg for replay. */
694 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
695 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
697 if (req->rq_import->imp_replayable) {
698 spin_lock(&imp->imp_lock);
699 /* no point in adding already-committed requests to the replay
700 * list, we will just remove them immediately. b=9829 */
701 if (req->rq_transno != 0 &&
703 lustre_msg_get_last_committed(req->rq_repmsg) ||
705 ptlrpc_retain_replayable_request(req, imp);
706 else if (req->rq_commit_cb != NULL) {
707 spin_unlock(&imp->imp_lock);
708 req->rq_commit_cb(req);
709 spin_lock(&imp->imp_lock);
712 /* Replay-enabled imports return commit-status information. */
713 if (lustre_msg_get_last_committed(req->rq_repmsg)) {
714 imp->imp_peer_committed_transno =
715 lustre_msg_get_last_committed(req->rq_repmsg);
717 ptlrpc_free_committed(imp);
718 spin_unlock(&imp->imp_lock);
724 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
726 struct obd_import *imp;
730 LASSERT(req->rq_phase == RQ_PHASE_NEW);
731 req->rq_phase = RQ_PHASE_RPC;
733 imp = req->rq_import;
734 spin_lock(&imp->imp_lock);
736 req->rq_import_generation = imp->imp_generation;
738 if (ptlrpc_import_delay_req(imp, req, &rc)) {
739 spin_lock (&req->rq_lock);
741 spin_unlock (&req->rq_lock);
743 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
745 lustre_msg_get_status(req->rq_reqmsg) ,
746 ptlrpc_import_state_name(req->rq_send_state),
747 ptlrpc_import_state_name(imp->imp_state));
748 LASSERT(list_empty (&req->rq_list));
750 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
751 spin_unlock(&imp->imp_lock);
756 spin_unlock(&imp->imp_lock);
758 req->rq_phase = RQ_PHASE_INTERPRET;
762 /* XXX this is the same as ptlrpc_queue_wait */
763 LASSERT(list_empty(&req->rq_list));
764 list_add_tail(&req->rq_list, &imp->imp_sending_list);
765 spin_unlock(&imp->imp_lock);
767 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
769 rc = sptlrpc_req_refresh_ctx(req, -1);
775 /* here begins timeout counting */
776 req->rq_sent = CURRENT_SECONDS;
777 req->rq_wait_ctx = 1;
782 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
783 " %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
784 imp->imp_obd->obd_uuid.uuid,
785 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
786 libcfs_nid2str(imp->imp_connection->c_peer.nid),
787 lustre_msg_get_opc(req->rq_reqmsg));
789 rc = ptl_send_rpc(req, 0);
791 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
798 /* this sends any unsent RPCs in @set and returns TRUE if all are sent */
799 int ptlrpc_check_set(struct ptlrpc_request_set *set)
801 struct list_head *tmp;
802 int force_timer_recalc = 0;
805 if (set->set_remaining == 0)
808 list_for_each(tmp, &set->set_requests) {
809 struct ptlrpc_request *req =
810 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
811 struct obd_import *imp = req->rq_import;
814 if (req->rq_phase == RQ_PHASE_NEW &&
815 ptlrpc_send_new_req(req)) {
816 force_timer_recalc = 1;
819 if (!(req->rq_phase == RQ_PHASE_RPC ||
820 req->rq_phase == RQ_PHASE_BULK ||
821 req->rq_phase == RQ_PHASE_INTERPRET ||
822 req->rq_phase == RQ_PHASE_COMPLETE)) {
823 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
827 if (req->rq_phase == RQ_PHASE_COMPLETE)
830 if (req->rq_phase == RQ_PHASE_INTERPRET)
831 GOTO(interpret, req->rq_status);
833 if (req->rq_net_err && !req->rq_timedout)
834 ptlrpc_expire_one_request(req);
837 ptlrpc_unregister_reply(req);
839 if (req->rq_status == 0)
840 req->rq_status = -EIO;
841 req->rq_phase = RQ_PHASE_INTERPRET;
843 spin_lock(&imp->imp_lock);
844 list_del_init(&req->rq_list);
845 spin_unlock(&imp->imp_lock);
847 GOTO(interpret, req->rq_status);
850 /* ptlrpc_queue_wait->l_wait_event guarantees that rq_intr
851 * will only be set after rq_timedout, but the oig waiting
852 * path sets rq_intr irrespective of whether ptlrpcd has
853 * seen a timeout. our policy is to only interpret
854 * interrupted rpcs after they have timed out */
855 if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
857 /* NB could be on delayed list */
858 ptlrpc_unregister_reply(req);
859 req->rq_status = -EINTR;
860 req->rq_phase = RQ_PHASE_INTERPRET;
862 spin_lock(&imp->imp_lock);
863 list_del_init(&req->rq_list);
864 spin_unlock(&imp->imp_lock);
866 GOTO(interpret, req->rq_status);
869 if (req->rq_phase == RQ_PHASE_RPC) {
870 if (req->rq_timedout || req->rq_resend ||
871 req->rq_waiting || req->rq_wait_ctx) {
874 /* rq_wait_ctx is only touched in ptlrpcd,
875 * no lock needed here.
877 if (req->rq_wait_ctx)
880 ptlrpc_unregister_reply(req);
882 spin_lock(&imp->imp_lock);
884 if (ptlrpc_import_delay_req(imp, req, &status)){
885 spin_unlock(&imp->imp_lock);
889 list_del_init(&req->rq_list);
891 req->rq_status = status;
892 req->rq_phase = RQ_PHASE_INTERPRET;
893 spin_unlock(&imp->imp_lock);
894 GOTO(interpret, req->rq_status);
896 if (req->rq_no_resend && !req->rq_wait_ctx) {
897 req->rq_status = -ENOTCONN;
898 req->rq_phase = RQ_PHASE_INTERPRET;
899 spin_unlock(&imp->imp_lock);
900 GOTO(interpret, req->rq_status);
902 list_add_tail(&req->rq_list,
903 &imp->imp_sending_list);
905 spin_unlock(&imp->imp_lock);
908 if (req->rq_resend) {
909 lustre_msg_add_flags(req->rq_reqmsg,
912 __u64 old_xid = req->rq_xid;
914 ptlrpc_unregister_bulk (req);
916 /* ensure previous bulk fails */
917 req->rq_xid = ptlrpc_next_xid();
918 CDEBUG(D_HA, "resend bulk "
921 old_xid, req->rq_xid);
925 status = sptlrpc_req_refresh_ctx(req, -1);
928 req->rq_status = status;
929 force_timer_recalc = 1;
931 if (!req->rq_wait_ctx) {
932 /* begins timeout counting */
933 req->rq_sent = CURRENT_SECONDS;
934 req->rq_wait_ctx = 1;
939 req->rq_wait_ctx = 0;
942 rc = ptl_send_rpc(req, 0);
944 DEBUG_REQ(D_HA, req, "send failed (%d)",
946 force_timer_recalc = 1;
949 /* need to reset the timeout */
950 force_timer_recalc = 1;
953 /* Still waiting for a reply? */
954 if (ptlrpc_client_receiving_reply(req))
957 /* Did we actually receive a reply? */
958 if (!ptlrpc_client_replied(req))
961 spin_lock(&imp->imp_lock);
962 list_del_init(&req->rq_list);
963 spin_unlock(&imp->imp_lock);
965 req->rq_status = after_reply(req);
966 if (req->rq_resend) {
967 /* Add this req to the delayed list so
968 it can be errored if the import is
969 evicted after recovery. */
970 spin_lock(&imp->imp_lock);
971 list_add_tail(&req->rq_list,
972 &imp->imp_delayed_list);
973 spin_unlock(&imp->imp_lock);
977 /* If there is no bulk associated with this request,
978 * then we're done and should let the interpreter
979 * process the reply. Similarly if the RPC returned
980 * an error, and therefore the bulk will never arrive.
982 if (req->rq_bulk == NULL || req->rq_status != 0) {
983 req->rq_phase = RQ_PHASE_INTERPRET;
984 GOTO(interpret, req->rq_status);
987 req->rq_phase = RQ_PHASE_BULK;
990 LASSERT(req->rq_phase == RQ_PHASE_BULK);
991 if (ptlrpc_bulk_active(req->rq_bulk))
994 if (!req->rq_bulk->bd_success) {
995 /* The RPC reply arrived OK, but the bulk screwed
996 * up! Dead wierd since the server told us the RPC
997 * was good after getting the REPLY for her GET or
998 * the ACK for her PUT. */
999 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1003 req->rq_phase = RQ_PHASE_INTERPRET;
1006 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
1007 LASSERT(!req->rq_receiving_reply);
1009 ptlrpc_unregister_reply(req);
1010 if (req->rq_bulk != NULL)
1011 ptlrpc_unregister_bulk (req);
1013 req->rq_phase = RQ_PHASE_COMPLETE;
1015 if (req->rq_interpret_reply != NULL) {
1016 int (*interpreter)(struct ptlrpc_request *,void *,int) =
1017 req->rq_interpret_reply;
1018 req->rq_status = interpreter(req, &req->rq_async_args,
1022 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
1023 "opc %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1024 imp->imp_obd->obd_uuid.uuid,
1025 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1026 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1027 lustre_msg_get_opc(req->rq_reqmsg));
1029 set->set_remaining--;
1031 atomic_dec(&imp->imp_inflight);
1032 cfs_waitq_signal(&imp->imp_recovery_waitq);
1035 /* If we hit an error, we want to recover promptly. */
1036 RETURN(set->set_remaining == 0 || force_timer_recalc);
1039 int ptlrpc_expire_one_request(struct ptlrpc_request *req)
1041 struct obd_import *imp = req->rq_import;
1045 DEBUG_REQ(D_ERROR|D_NETERROR, req, "%s (sent at %lu, %lus ago)",
1046 req->rq_net_err ? "network error" : "timeout",
1047 (long)req->rq_sent, CURRENT_SECONDS - req->rq_sent);
1049 if (imp != NULL && obd_debug_peer_on_timeout)
1050 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1052 spin_lock(&req->rq_lock);
1053 req->rq_timedout = 1;
1054 req->rq_wait_ctx = 0;
1055 spin_unlock(&req->rq_lock);
1057 ptlrpc_unregister_reply (req);
1059 if (obd_dump_on_timeout)
1060 libcfs_debug_dumplog();
1062 if (req->rq_bulk != NULL)
1063 ptlrpc_unregister_bulk (req);
1066 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
1070 /* The DLM server doesn't want recovery run on its imports. */
1071 if (imp->imp_dlm_fake)
1074 /* If this request is for recovery or other primordial tasks,
1075 * then error it out here. */
1076 if (req->rq_ctx_init || req->rq_ctx_fini ||
1077 req->rq_send_state != LUSTRE_IMP_FULL ||
1078 imp->imp_obd->obd_no_recov) {
1079 spin_lock(&req->rq_lock);
1080 req->rq_status = -ETIMEDOUT;
1082 spin_unlock(&req->rq_lock);
1086 /* if request can't be resend we can't wait answer after timeout */
1087 if (req->rq_no_resend) {
1088 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1092 ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1097 int ptlrpc_expired_set(void *data)
1099 struct ptlrpc_request_set *set = data;
1100 struct list_head *tmp;
1101 time_t now = CURRENT_SECONDS;
1104 LASSERT(set != NULL);
1106 /* A timeout expired; see which reqs it applies to... */
1107 list_for_each (tmp, &set->set_requests) {
1108 struct ptlrpc_request *req =
1109 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1111 /* request in-flight? */
1112 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting &&
1114 (req->rq_phase == RQ_PHASE_BULK)))
1117 if (req->rq_timedout || /* already dealt with */
1118 req->rq_sent + req->rq_timeout > now) /* not expired */
1121 /* deal with this guy */
1122 ptlrpc_expire_one_request (req);
1125 /* When waiting for a whole set, we always to break out of the
1126 * sleep so we can recalculate the timeout, or enable interrupts
1127 * iff everyone's timed out.
1132 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1134 spin_lock(&req->rq_lock);
1136 spin_unlock(&req->rq_lock);
1139 void ptlrpc_interrupted_set(void *data)
1141 struct ptlrpc_request_set *set = data;
1142 struct list_head *tmp;
1144 LASSERT(set != NULL);
1145 CERROR("INTERRUPTED SET %p\n", set);
1147 list_for_each(tmp, &set->set_requests) {
1148 struct ptlrpc_request *req =
1149 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1151 if (req->rq_phase != RQ_PHASE_RPC)
1154 ptlrpc_mark_interrupted(req);
1158 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1160 struct list_head *tmp;
1161 time_t now = CURRENT_SECONDS;
1164 struct ptlrpc_request *req;
1167 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1169 list_for_each(tmp, &set->set_requests) {
1170 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1172 /* request in-flight? */
1173 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting) ||
1174 (req->rq_phase == RQ_PHASE_BULK)))
1177 if (req->rq_timedout) /* already timed out */
1180 deadline = req->rq_sent + req->rq_timeout;
1181 if (deadline <= now) /* actually expired already */
1182 timeout = 1; /* ASAP */
1183 else if (timeout == 0 || timeout > deadline - now)
1184 timeout = deadline - now;
1189 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
1191 struct list_head *tmp;
1192 struct ptlrpc_request *req;
1193 struct l_wait_info lwi;
1197 if (list_empty(&set->set_requests))
1200 list_for_each(tmp, &set->set_requests) {
1201 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1202 if (req->rq_phase == RQ_PHASE_NEW)
1203 (void)ptlrpc_send_new_req(req);
1207 timeout = ptlrpc_set_next_timeout(set);
1209 /* wait until all complete, interrupted, or an in-flight
1211 CDEBUG(D_HA, "set %p going to sleep for %d seconds\n",
1213 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout ? timeout : 1),
1215 ptlrpc_interrupted_set, set);
1216 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(set), &lwi);
1218 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
1220 /* -EINTR => all requests have been flagged rq_intr so next
1222 * -ETIMEOUTD => someone timed out. When all reqs have
1223 * timed out, signals are enabled allowing completion with
1225 * I don't really care if we go once more round the loop in
1226 * the error cases -eeb. */
1227 } while (rc != 0 || set->set_remaining != 0);
1229 LASSERT(set->set_remaining == 0);
1232 list_for_each(tmp, &set->set_requests) {
1233 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1235 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
1236 if (req->rq_status != 0)
1237 rc = req->rq_status;
1240 if (set->set_interpret != NULL) {
1241 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
1243 rc = interpreter (set, set->set_arg, rc);
1249 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
1252 if (request == NULL) {
1257 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
1258 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
1259 LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
1260 LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
1261 LASSERT(request->rq_cli_ctx);
1263 /* We must take it off the imp_replay_list first. Otherwise, we'll set
1264 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
1265 if (request->rq_import != NULL) {
1267 spin_lock(&request->rq_import->imp_lock);
1268 list_del_init(&request->rq_mod_list);
1269 list_del_init(&request->rq_replay_list);
1271 spin_unlock(&request->rq_import->imp_lock);
1273 LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
1275 if (atomic_read(&request->rq_refcount) != 0) {
1276 DEBUG_REQ(D_ERROR, request,
1277 "freeing request with nonzero refcount");
1281 if (request->rq_repbuf != NULL)
1282 sptlrpc_cli_free_repbuf(request);
1283 if (request->rq_export != NULL) {
1284 class_export_put(request->rq_export);
1285 request->rq_export = NULL;
1287 if (request->rq_import != NULL) {
1288 class_import_put(request->rq_import);
1289 request->rq_import = NULL;
1291 if (request->rq_bulk != NULL)
1292 ptlrpc_free_bulk(request->rq_bulk);
1294 if (request->rq_reqbuf != NULL || request->rq_clrbuf != NULL)
1295 sptlrpc_cli_free_reqbuf(request);
1297 sptlrpc_req_put_ctx(request, !locked);
1299 if (request->rq_pool)
1300 __ptlrpc_free_req_to_pool(request);
1302 OBD_FREE(request, sizeof(*request));
1306 void ptlrpc_free_req(struct ptlrpc_request *request)
1308 __ptlrpc_free_req(request, 0);
1311 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
1312 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
1314 LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
1315 (void)__ptlrpc_req_finished(request, 1);
1318 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
1321 if (request == NULL)
1324 if (request == LP_POISON ||
1325 request->rq_reqmsg == LP_POISON) {
1326 CERROR("dereferencing freed request (bug 575)\n");
1331 DEBUG_REQ(D_INFO, request, "refcount now %u",
1332 atomic_read(&request->rq_refcount) - 1);
1334 if (atomic_dec_and_test(&request->rq_refcount)) {
1335 __ptlrpc_free_req(request, locked);
1342 void ptlrpc_req_finished(struct ptlrpc_request *request)
1344 __ptlrpc_req_finished(request, 0);
1347 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
1349 return request->rq_xid;
1351 EXPORT_SYMBOL(ptlrpc_req_xid);
1353 /* Disengage the client's reply buffer from the network
1354 * NB does _NOT_ unregister any client-side bulk.
1355 * IDEMPOTENT, but _not_ safe against concurrent callers.
1356 * The request owner (i.e. the thread doing the I/O) must call...
1358 void ptlrpc_unregister_reply (struct ptlrpc_request *request)
1362 struct l_wait_info lwi;
1364 LASSERT(!in_interrupt ()); /* might sleep */
1366 if (!ptlrpc_client_receiving_reply(request))
1369 LNetMDUnlink (request->rq_reply_md_h);
1371 /* We have to l_wait_event() whatever the result, to give liblustre
1372 * a chance to run reply_in_callback() */
1374 if (request->rq_set != NULL)
1375 wq = &request->rq_set->set_waitq;
1377 wq = &request->rq_reply_waitq;
1380 /* Network access will complete in finite time but the HUGE
1381 * timeout lets us CWARN for visibility of sluggish NALs */
1382 lwi = LWI_TIMEOUT(cfs_time_seconds(300), NULL, NULL);
1383 rc = l_wait_event (*wq, !ptlrpc_client_receiving_reply(request), &lwi);
1387 LASSERT (rc == -ETIMEDOUT);
1388 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout");
1392 /* caller must hold imp->imp_lock */
1393 void ptlrpc_free_committed(struct obd_import *imp)
1395 struct list_head *tmp, *saved;
1396 struct ptlrpc_request *req;
1397 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1400 LASSERT(imp != NULL);
1402 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1405 if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
1406 imp->imp_generation == imp->imp_last_generation_checked) {
1407 CDEBUG(D_HA, "%s: skip recheck for last_committed "LPU64"\n",
1408 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1412 CDEBUG(D_HA, "%s: committing for last_committed "LPU64" gen %d\n",
1413 imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
1414 imp->imp_generation);
1415 imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
1416 imp->imp_last_generation_checked = imp->imp_generation;
1418 list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1419 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1421 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
1422 LASSERT(req != last_req);
1425 if (req->rq_import_generation < imp->imp_generation) {
1426 DEBUG_REQ(D_HA, req, "freeing request with old gen");
1430 if (req->rq_replay) {
1431 DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
1435 /* not yet committed */
1436 if (req->rq_transno > imp->imp_peer_committed_transno) {
1437 DEBUG_REQ(D_HA, req, "stopping search");
1441 DEBUG_REQ(D_HA, req, "committing (last_committed "LPU64")",
1442 imp->imp_peer_committed_transno);
1444 spin_lock(&req->rq_lock);
1446 spin_unlock(&req->rq_lock);
1447 if (req->rq_commit_cb != NULL)
1448 req->rq_commit_cb(req);
1449 list_del_init(&req->rq_replay_list);
1450 __ptlrpc_req_finished(req, 1);
1457 void ptlrpc_cleanup_client(struct obd_import *imp)
1464 void ptlrpc_resend_req(struct ptlrpc_request *req)
1466 DEBUG_REQ(D_HA, req, "going to resend");
1467 lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
1468 req->rq_status = -EAGAIN;
1470 spin_lock(&req->rq_lock);
1472 req->rq_net_err = 0;
1473 req->rq_timedout = 0;
1475 __u64 old_xid = req->rq_xid;
1477 /* ensure previous bulk fails */
1478 req->rq_xid = ptlrpc_next_xid();
1479 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
1480 old_xid, req->rq_xid);
1482 ptlrpc_wake_client_req(req);
1483 spin_unlock(&req->rq_lock);
1486 /* XXX: this function and rq_status are currently unused */
1487 void ptlrpc_restart_req(struct ptlrpc_request *req)
1489 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1490 req->rq_status = -ERESTARTSYS;
1492 spin_lock(&req->rq_lock);
1493 req->rq_restart = 1;
1494 req->rq_timedout = 0;
1495 ptlrpc_wake_client_req(req);
1496 spin_unlock(&req->rq_lock);
1499 static int expired_request(void *data)
1501 struct ptlrpc_request *req = data;
1504 /* some failure can suspend regular timeouts */
1505 if (ptlrpc_check_suspend())
1508 RETURN(ptlrpc_expire_one_request(req));
1511 static void interrupted_request(void *data)
1513 struct ptlrpc_request *req = data;
1514 DEBUG_REQ(D_HA, req, "request interrupted");
1515 spin_lock(&req->rq_lock);
1517 spin_unlock(&req->rq_lock);
1520 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1523 atomic_inc(&req->rq_refcount);
1527 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1528 struct obd_import *imp)
1530 struct list_head *tmp;
1532 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1534 /* clear this for new requests that were resent as well
1535 as resent replayed requests. */
1536 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1538 /* don't re-add requests that have been replayed */
1539 if (!list_empty(&req->rq_replay_list))
1542 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
1544 LASSERT(imp->imp_replayable);
1545 /* Balanced in ptlrpc_free_committed, usually. */
1546 ptlrpc_request_addref(req);
1547 list_for_each_prev(tmp, &imp->imp_replay_list) {
1548 struct ptlrpc_request *iter =
1549 list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1551 /* We may have duplicate transnos if we create and then
1552 * open a file, or for closes retained if to match creating
1553 * opens, so use req->rq_xid as a secondary key.
1554 * (See bugs 684, 685, and 428.)
1555 * XXX no longer needed, but all opens need transnos!
1557 if (iter->rq_transno > req->rq_transno)
1560 if (iter->rq_transno == req->rq_transno) {
1561 LASSERT(iter->rq_xid != req->rq_xid);
1562 if (iter->rq_xid > req->rq_xid)
1566 list_add(&req->rq_replay_list, &iter->rq_replay_list);
1570 list_add_tail(&req->rq_replay_list, &imp->imp_replay_list);
1573 int ptlrpc_queue_wait(struct ptlrpc_request *req)
1577 struct l_wait_info lwi;
1578 struct obd_import *imp = req->rq_import;
1579 cfs_duration_t timeout = 0;
1582 LASSERT(req->rq_set == NULL);
1583 LASSERT(!req->rq_receiving_reply);
1584 atomic_inc(&imp->imp_inflight);
1586 /* for distributed debugging */
1587 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
1588 LASSERT(imp->imp_obd != NULL);
1589 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc "
1590 "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1591 imp->imp_obd->obd_uuid.uuid,
1592 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1593 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1594 lustre_msg_get_opc(req->rq_reqmsg));
1596 /* Mark phase here for a little debug help */
1597 req->rq_phase = RQ_PHASE_RPC;
1599 spin_lock(&imp->imp_lock);
1600 req->rq_import_generation = imp->imp_generation;
1602 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1603 list_del(&req->rq_list);
1605 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1606 spin_unlock(&imp->imp_lock);
1608 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%s != %s)",
1610 ptlrpc_import_state_name(req->rq_send_state),
1611 ptlrpc_import_state_name(imp->imp_state));
1612 lwi = LWI_INTR(interrupted_request, req);
1613 rc = l_wait_event(req->rq_reply_waitq,
1614 (req->rq_send_state == imp->imp_state ||
1615 req->rq_err || req->rq_intr),
1617 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%s == %s or %d/%d == 1)",
1619 ptlrpc_import_state_name(imp->imp_state),
1620 ptlrpc_import_state_name(req->rq_send_state),
1621 req->rq_err, req->rq_intr);
1623 spin_lock(&imp->imp_lock);
1624 list_del_init(&req->rq_list);
1629 else if (req->rq_intr) {
1632 else if (req->rq_no_resend) {
1633 spin_unlock(&imp->imp_lock);
1634 GOTO(out, rc = -ETIMEDOUT);
1642 list_del_init(&req->rq_list);
1643 spin_unlock(&imp->imp_lock);
1644 req->rq_status = rc; // XXX this ok?
1648 if (req->rq_resend) {
1649 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
1651 if (req->rq_bulk != NULL) {
1652 ptlrpc_unregister_bulk (req);
1654 /* bulk requests are supposed to be
1655 * idempotent, so we are free to bump the xid
1656 * here, which we need to do before
1657 * registering the bulk again (bug 6371).
1658 * print the old xid first for sanity.
1660 DEBUG_REQ(D_HA, req, "bumping xid for bulk: ");
1661 req->rq_xid = ptlrpc_next_xid();
1664 DEBUG_REQ(D_HA, req, "resending: ");
1667 /* XXX this is the same as ptlrpc_set_wait */
1668 LASSERT(list_empty(&req->rq_list));
1669 list_add_tail(&req->rq_list, &imp->imp_sending_list);
1670 spin_unlock(&imp->imp_lock);
1672 rc = sptlrpc_req_refresh_ctx(req, 0);
1675 /* we got fatal ctx refresh error, directly jump out
1676 * thus we can pass back the actual error code.
1678 spin_lock(&imp->imp_lock);
1679 list_del_init(&req->rq_list);
1680 spin_unlock(&imp->imp_lock);
1682 CERROR("Failed to refresh ctx of req %p: %d\n", req, rc);
1685 /* simulating we got error during send rpc */
1689 rc = ptl_send_rpc(req, 0);
1691 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
1694 timeout = cfs_timeout_cap(cfs_time_seconds(req->rq_timeout));
1695 DEBUG_REQ(D_NET, req,
1696 "-- sleeping for "CFS_DURATION_T" jiffies", timeout);
1699 lwi = LWI_TIMEOUT_INTR(timeout, expired_request, interrupted_request,
1701 rc = l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req), &lwi);
1702 if (rc == -ETIMEDOUT && ptlrpc_check_and_wait_suspend(req))
1706 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:opc "
1707 "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1708 imp->imp_obd->obd_uuid.uuid,
1709 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1710 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1711 lustre_msg_get_opc(req->rq_reqmsg));
1713 spin_lock(&imp->imp_lock);
1714 list_del_init(&req->rq_list);
1715 spin_unlock(&imp->imp_lock);
1717 /* If the reply was received normally, this just grabs the spinlock
1718 * (ensuring the reply callback has returned), sees that
1719 * req->rq_receiving_reply is clear and returns. */
1720 ptlrpc_unregister_reply (req);
1723 GOTO(out, rc = -EIO);
1725 /* Resend if we need to, unless we were interrupted. */
1726 if (req->rq_resend && !req->rq_intr) {
1727 /* ...unless we were specifically told otherwise. */
1728 if (req->rq_no_resend)
1729 GOTO(out, rc = -ETIMEDOUT);
1730 spin_lock(&imp->imp_lock);
1735 /* Should only be interrupted if we timed out. */
1736 if (!req->rq_timedout)
1737 DEBUG_REQ(D_ERROR, req,
1738 "rq_intr set but rq_timedout not");
1739 GOTO(out, rc = -EINTR);
1742 if (req->rq_timedout) { /* non-recoverable timeout */
1743 GOTO(out, rc = -ETIMEDOUT);
1746 if (!req->rq_replied) {
1747 /* How can this be? -eeb */
1748 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
1750 GOTO(out, rc = req->rq_status);
1753 rc = after_reply(req);
1754 /* NB may return +ve success rc */
1755 if (req->rq_resend) {
1756 spin_lock(&imp->imp_lock);
1761 if (req->rq_bulk != NULL) {
1763 /* success so far. Note that anything going wrong
1764 * with bulk now, is EXTREMELY strange, since the
1765 * server must have believed that the bulk
1766 * tranferred OK before she replied with success to
1768 lwi = LWI_TIMEOUT(timeout, NULL, NULL);
1769 brc = l_wait_event(req->rq_reply_waitq,
1770 !ptlrpc_bulk_active(req->rq_bulk),
1772 LASSERT(brc == 0 || brc == -ETIMEDOUT);
1774 LASSERT(brc == -ETIMEDOUT);
1775 DEBUG_REQ(D_ERROR, req, "bulk timed out");
1777 } else if (!req->rq_bulk->bd_success) {
1778 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1783 ptlrpc_unregister_bulk (req);
1786 LASSERT(!req->rq_receiving_reply);
1787 req->rq_phase = RQ_PHASE_INTERPRET;
1789 atomic_dec(&imp->imp_inflight);
1790 cfs_waitq_signal(&imp->imp_recovery_waitq);
1794 struct ptlrpc_replay_async_args {
1796 int praa_old_status;
1799 static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
1800 void * data, int rc)
1802 struct ptlrpc_replay_async_args *aa = data;
1803 struct obd_import *imp = req->rq_import;
1806 atomic_dec(&imp->imp_replay_inflight);
1808 if (!req->rq_replied) {
1809 CERROR("request replay timed out, restarting recovery\n");
1810 GOTO(out, rc = -ETIMEDOUT);
1813 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
1814 lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN)
1815 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
1817 /* The transno had better not change over replay. */
1818 LASSERT(lustre_msg_get_transno(req->rq_reqmsg) ==
1819 lustre_msg_get_transno(req->rq_repmsg));
1821 DEBUG_REQ(D_HA, req, "got rep");
1823 /* let the callback do fixups, possibly including in the request */
1824 if (req->rq_replay_cb)
1825 req->rq_replay_cb(req);
1827 if (req->rq_replied &&
1828 lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
1829 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
1830 lustre_msg_get_status(req->rq_repmsg),
1831 aa->praa_old_status);
1833 /* Put it back for re-replay. */
1834 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
1838 * Errors while replay can set transno to 0, but
1839 * imp_last_replay_transno shouldn't be set to 0 anyway
1841 if (req->rq_transno > 0) {
1842 spin_lock(&imp->imp_lock);
1843 LASSERT(req->rq_transno <= imp->imp_last_replay_transno);
1844 imp->imp_last_replay_transno = req->rq_transno;
1845 spin_unlock(&imp->imp_lock);
1847 CERROR("Transno is 0 during replay!\n");
1848 /* continue with recovery */
1849 rc = ptlrpc_import_recovery_state_machine(imp);
1851 req->rq_send_state = aa->praa_old_state;
1854 /* this replay failed, so restart recovery */
1855 ptlrpc_connect_import(imp, NULL);
1860 int ptlrpc_replay_req(struct ptlrpc_request *req)
1862 struct ptlrpc_replay_async_args *aa;
1865 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
1867 /* Not handling automatic bulk replay yet (or ever?) */
1868 LASSERT(req->rq_bulk == NULL);
1870 DEBUG_REQ(D_HA, req, "REPLAY");
1872 LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
1873 aa = (struct ptlrpc_replay_async_args *)&req->rq_async_args;
1874 memset(aa, 0, sizeof *aa);
1876 /* Prepare request to be resent with ptlrpcd */
1877 aa->praa_old_state = req->rq_send_state;
1878 req->rq_send_state = LUSTRE_IMP_REPLAY;
1879 req->rq_phase = RQ_PHASE_NEW;
1880 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
1883 req->rq_interpret_reply = ptlrpc_replay_interpret;
1884 atomic_inc(&req->rq_import->imp_replay_inflight);
1885 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
1887 ptlrpcd_add_req(req);
1891 void ptlrpc_abort_inflight(struct obd_import *imp)
1893 struct list_head *tmp, *n;
1896 /* Make sure that no new requests get processed for this import.
1897 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
1898 * this flag and then putting requests on sending_list or delayed_list.
1900 spin_lock(&imp->imp_lock);
1902 /* XXX locking? Maybe we should remove each request with the list
1903 * locked? Also, how do we know if the requests on the list are
1904 * being freed at this time?
1906 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
1907 struct ptlrpc_request *req =
1908 list_entry(tmp, struct ptlrpc_request, rq_list);
1910 DEBUG_REQ(D_HA, req, "inflight");
1912 spin_lock (&req->rq_lock);
1913 if (req->rq_import_generation < imp->imp_generation) {
1915 ptlrpc_wake_client_req(req);
1917 spin_unlock (&req->rq_lock);
1920 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
1921 struct ptlrpc_request *req =
1922 list_entry(tmp, struct ptlrpc_request, rq_list);
1924 DEBUG_REQ(D_HA, req, "aborting waiting req");
1926 spin_lock (&req->rq_lock);
1927 if (req->rq_import_generation < imp->imp_generation) {
1929 ptlrpc_wake_client_req(req);
1931 spin_unlock (&req->rq_lock);
1934 /* Last chance to free reqs left on the replay list, but we
1935 * will still leak reqs that haven't committed. */
1936 if (imp->imp_replayable)
1937 ptlrpc_free_committed(imp);
1939 spin_unlock(&imp->imp_lock);
1944 static __u64 ptlrpc_last_xid = 0;
1945 spinlock_t ptlrpc_last_xid_lock;
1947 __u64 ptlrpc_next_xid(void)
1950 spin_lock(&ptlrpc_last_xid_lock);
1951 tmp = ++ptlrpc_last_xid;
1952 spin_unlock(&ptlrpc_last_xid_lock);
1956 __u64 ptlrpc_sample_next_xid(void)
1959 spin_lock(&ptlrpc_last_xid_lock);
1960 tmp = ptlrpc_last_xid + 1;
1961 spin_unlock(&ptlrpc_last_xid_lock);
1964 EXPORT_SYMBOL(ptlrpc_sample_next_xid);