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_cli_ctx_put(request->rq_cli_ctx, 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 it is mgc, wait for recovry. b=13464 */
554 if (imp->imp_recon_bk && !imp->imp_obd->obd_no_recov)
556 /* If the import has been invalidated (such as by an OST
557 * failure) the request must fail with -ESHUTDOWN. This
558 * indicates the requests should be discarded; an -EIO
559 * may result in a resend of the request. */
560 if (!imp->imp_deactive)
561 DEBUG_REQ(D_ERROR, req, "IMP_INVALID");
562 *status = -ESHUTDOWN; /* bz 12940 */
563 } else if (req->rq_import_generation != imp->imp_generation) {
564 DEBUG_REQ(D_ERROR, req, "req wrong generation:");
566 } else if (req->rq_send_state != imp->imp_state) {
567 if (imp->imp_obd->obd_no_recov || imp->imp_dlm_fake ||
569 *status = -EWOULDBLOCK;
577 static int ptlrpc_check_reply(struct ptlrpc_request *req)
582 /* serialise with network callback */
583 spin_lock(&req->rq_lock);
588 if (req->rq_net_err && !req->rq_timedout) {
589 spin_unlock(&req->rq_lock);
590 rc = ptlrpc_expire_one_request(req);
591 spin_lock(&req->rq_lock);
605 spin_unlock(&req->rq_lock);
606 DEBUG_REQ(D_NET, req, "rc = %d for", rc);
610 static int ptlrpc_check_status(struct ptlrpc_request *req)
615 err = lustre_msg_get_status(req->rq_repmsg);
616 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
617 struct obd_import *imp = req->rq_import;
618 __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
619 LCONSOLE_ERROR_MSG(0x011,"an error occurred while communicating"
620 " with %s. The %s operation failed with %d\n",
621 libcfs_nid2str(imp->imp_connection->c_peer.nid),
622 ll_opcode2str(opc), err);
623 RETURN(err < 0 ? err : -EINVAL);
627 DEBUG_REQ(D_INFO, req, "status is %d", err);
628 } else if (err > 0) {
629 /* XXX: translate this error from net to host */
630 DEBUG_REQ(D_INFO, req, "status is %d", err);
636 static int after_reply(struct ptlrpc_request *req)
638 struct obd_import *imp = req->rq_import;
642 LASSERT(!req->rq_receiving_reply);
643 LASSERT(req->rq_nob_received <= req->rq_repbuf_len);
645 /* NB Until this point, the whole of the incoming message,
646 * including buflens, status etc is in the sender's byte order. */
648 /* Clear reply swab mask; this is a new reply in sender's byte order */
649 req->rq_rep_swab_mask = 0;
651 rc = sptlrpc_cli_unwrap_reply(req);
653 DEBUG_REQ(D_ERROR, req, "unwrap reply failed (%d):", rc);
657 /* security layer unwrap might ask resend this request */
661 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
663 DEBUG_REQ(D_ERROR, req, "unpack_rep failed: %d", rc);
667 rc = lustre_unpack_rep_ptlrpc_body(req, MSG_PTLRPC_BODY_OFF);
669 DEBUG_REQ(D_ERROR, req, "unpack ptlrpc body failed: %d", rc);
673 if (lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_REPLY &&
674 lustre_msg_get_type(req->rq_repmsg) != PTL_RPC_MSG_ERR) {
675 DEBUG_REQ(D_ERROR, req, "invalid packet received (type=%u)",
676 lustre_msg_get_type(req->rq_repmsg));
680 rc = ptlrpc_check_status(req);
681 imp->imp_connect_error = rc;
684 /* Either we've been evicted, or the server has failed for
685 * some reason. Try to reconnect, and if that fails, punt to
687 if (ll_rpc_recoverable_error(rc)) {
688 if (req->rq_send_state != LUSTRE_IMP_FULL ||
689 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
692 ptlrpc_request_handle_notconn(req);
696 /* Let's look if server send slv. Do it only for RPC with
698 if (imp->imp_obd->obd_namespace) {
699 /* Disconnect rpc is sent when namespace is already
700 * destroyed. Let's check this and will not try update
702 ldlm_cli_update_pool(req);
706 /* Store transno in reqmsg for replay. */
707 req->rq_transno = lustre_msg_get_transno(req->rq_repmsg);
708 lustre_msg_set_transno(req->rq_reqmsg, req->rq_transno);
710 if (req->rq_import->imp_replayable) {
711 spin_lock(&imp->imp_lock);
712 /* no point in adding already-committed requests to the replay
713 * list, we will just remove them immediately. b=9829 */
714 if (req->rq_transno != 0 &&
716 lustre_msg_get_last_committed(req->rq_repmsg) ||
718 ptlrpc_retain_replayable_request(req, imp);
719 else if (req->rq_commit_cb != NULL) {
720 spin_unlock(&imp->imp_lock);
721 req->rq_commit_cb(req);
722 spin_lock(&imp->imp_lock);
725 /* Replay-enabled imports return commit-status information. */
726 if (lustre_msg_get_last_committed(req->rq_repmsg)) {
727 imp->imp_peer_committed_transno =
728 lustre_msg_get_last_committed(req->rq_repmsg);
730 ptlrpc_free_committed(imp);
731 spin_unlock(&imp->imp_lock);
737 static int ptlrpc_send_new_req(struct ptlrpc_request *req)
739 struct obd_import *imp;
743 LASSERT(req->rq_phase == RQ_PHASE_NEW);
744 if (req->rq_sent && (req->rq_sent > CURRENT_SECONDS))
747 req->rq_phase = RQ_PHASE_RPC;
749 imp = req->rq_import;
750 spin_lock(&imp->imp_lock);
752 req->rq_import_generation = imp->imp_generation;
754 if (ptlrpc_import_delay_req(imp, req, &rc)) {
755 spin_lock (&req->rq_lock);
757 spin_unlock (&req->rq_lock);
759 DEBUG_REQ(D_HA, req, "req from PID %d waiting for recovery: "
761 lustre_msg_get_status(req->rq_reqmsg) ,
762 ptlrpc_import_state_name(req->rq_send_state),
763 ptlrpc_import_state_name(imp->imp_state));
764 LASSERT(list_empty (&req->rq_list));
766 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
767 spin_unlock(&imp->imp_lock);
772 spin_unlock(&imp->imp_lock);
774 req->rq_phase = RQ_PHASE_INTERPRET;
778 /* XXX this is the same as ptlrpc_queue_wait */
779 LASSERT(list_empty(&req->rq_list));
780 list_add_tail(&req->rq_list, &imp->imp_sending_list);
781 spin_unlock(&imp->imp_lock);
783 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
785 rc = sptlrpc_req_refresh_ctx(req, -1);
791 /* here begins timeout counting */
792 req->rq_sent = CURRENT_SECONDS;
793 req->rq_wait_ctx = 1;
798 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc"
799 " %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
800 imp->imp_obd->obd_uuid.uuid,
801 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
802 libcfs_nid2str(imp->imp_connection->c_peer.nid),
803 lustre_msg_get_opc(req->rq_reqmsg));
805 rc = ptl_send_rpc(req, 0);
807 DEBUG_REQ(D_HA, req, "send failed (%d); expect timeout", rc);
814 /* this sends any unsent RPCs in @set and returns TRUE if all are sent */
815 int ptlrpc_check_set(struct ptlrpc_request_set *set)
817 struct list_head *tmp;
818 int force_timer_recalc = 0;
821 if (set->set_remaining == 0)
824 list_for_each(tmp, &set->set_requests) {
825 struct ptlrpc_request *req =
826 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
827 struct obd_import *imp = req->rq_import;
830 if (req->rq_phase == RQ_PHASE_NEW &&
831 ptlrpc_send_new_req(req)) {
832 force_timer_recalc = 1;
834 /* delayed send - skip */
835 if (req->rq_phase == RQ_PHASE_NEW && req->rq_sent)
838 if (!(req->rq_phase == RQ_PHASE_RPC ||
839 req->rq_phase == RQ_PHASE_BULK ||
840 req->rq_phase == RQ_PHASE_INTERPRET ||
841 req->rq_phase == RQ_PHASE_COMPLETE)) {
842 DEBUG_REQ(D_ERROR, req, "bad phase %x", req->rq_phase);
846 if (req->rq_phase == RQ_PHASE_COMPLETE)
849 if (req->rq_phase == RQ_PHASE_INTERPRET)
850 GOTO(interpret, req->rq_status);
852 if (req->rq_net_err && !req->rq_timedout)
853 ptlrpc_expire_one_request(req);
856 ptlrpc_unregister_reply(req);
858 if (req->rq_status == 0)
859 req->rq_status = -EIO;
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 /* ptlrpc_queue_wait->l_wait_event guarantees that rq_intr
870 * will only be set after rq_timedout, but the oig waiting
871 * path sets rq_intr irrespective of whether ptlrpcd has
872 * seen a timeout. our policy is to only interpret
873 * interrupted rpcs after they have timed out */
874 if (req->rq_intr && (req->rq_timedout || req->rq_waiting ||
876 /* NB could be on delayed list */
877 ptlrpc_unregister_reply(req);
878 req->rq_status = -EINTR;
879 req->rq_phase = RQ_PHASE_INTERPRET;
881 spin_lock(&imp->imp_lock);
882 list_del_init(&req->rq_list);
883 spin_unlock(&imp->imp_lock);
885 GOTO(interpret, req->rq_status);
888 if (req->rq_phase == RQ_PHASE_RPC) {
889 if (req->rq_timedout || req->rq_resend ||
890 req->rq_waiting || req->rq_wait_ctx) {
893 /* rq_wait_ctx is only touched in ptlrpcd,
894 * no lock needed here.
896 if (req->rq_wait_ctx)
899 ptlrpc_unregister_reply(req);
901 spin_lock(&imp->imp_lock);
903 if (ptlrpc_import_delay_req(imp, req, &status)){
904 spin_unlock(&imp->imp_lock);
908 list_del_init(&req->rq_list);
910 req->rq_status = status;
911 req->rq_phase = RQ_PHASE_INTERPRET;
912 spin_unlock(&imp->imp_lock);
913 GOTO(interpret, req->rq_status);
915 if (req->rq_no_resend && !req->rq_wait_ctx) {
916 req->rq_status = -ENOTCONN;
917 req->rq_phase = RQ_PHASE_INTERPRET;
918 spin_unlock(&imp->imp_lock);
919 GOTO(interpret, req->rq_status);
921 list_add_tail(&req->rq_list,
922 &imp->imp_sending_list);
924 spin_unlock(&imp->imp_lock);
927 if (req->rq_resend) {
928 lustre_msg_add_flags(req->rq_reqmsg,
931 __u64 old_xid = req->rq_xid;
933 ptlrpc_unregister_bulk (req);
935 /* ensure previous bulk fails */
936 req->rq_xid = ptlrpc_next_xid();
937 CDEBUG(D_HA, "resend bulk "
940 old_xid, req->rq_xid);
944 status = sptlrpc_req_refresh_ctx(req, -1);
947 req->rq_status = status;
948 force_timer_recalc = 1;
950 if (!req->rq_wait_ctx) {
951 /* begins timeout counting */
952 req->rq_sent = CURRENT_SECONDS;
953 req->rq_wait_ctx = 1;
958 req->rq_wait_ctx = 0;
961 rc = ptl_send_rpc(req, 0);
963 DEBUG_REQ(D_HA, req, "send failed (%d)",
965 force_timer_recalc = 1;
968 /* need to reset the timeout */
969 force_timer_recalc = 1;
972 /* Still waiting for a reply? */
973 if (ptlrpc_client_receiving_reply(req))
976 /* Did we actually receive a reply? */
977 if (!ptlrpc_client_replied(req))
980 spin_lock(&imp->imp_lock);
981 list_del_init(&req->rq_list);
982 spin_unlock(&imp->imp_lock);
984 req->rq_status = after_reply(req);
985 if (req->rq_resend) {
986 /* Add this req to the delayed list so
987 it can be errored if the import is
988 evicted after recovery. */
989 spin_lock(&imp->imp_lock);
990 list_add_tail(&req->rq_list,
991 &imp->imp_delayed_list);
992 spin_unlock(&imp->imp_lock);
996 /* If there is no bulk associated with this request,
997 * then we're done and should let the interpreter
998 * process the reply. Similarly if the RPC returned
999 * an error, and therefore the bulk will never arrive.
1001 if (req->rq_bulk == NULL || req->rq_status != 0) {
1002 req->rq_phase = RQ_PHASE_INTERPRET;
1003 GOTO(interpret, req->rq_status);
1006 req->rq_phase = RQ_PHASE_BULK;
1009 LASSERT(req->rq_phase == RQ_PHASE_BULK);
1010 if (ptlrpc_bulk_active(req->rq_bulk))
1013 if (!req->rq_bulk->bd_success) {
1014 /* The RPC reply arrived OK, but the bulk screwed
1015 * up! Dead wierd since the server told us the RPC
1016 * was good after getting the REPLY for her GET or
1017 * the ACK for her PUT. */
1018 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1022 req->rq_phase = RQ_PHASE_INTERPRET;
1025 LASSERT(req->rq_phase == RQ_PHASE_INTERPRET);
1026 LASSERT(!req->rq_receiving_reply);
1028 ptlrpc_unregister_reply(req);
1029 if (req->rq_bulk != NULL)
1030 ptlrpc_unregister_bulk (req);
1032 if (req->rq_interpret_reply != NULL) {
1033 int (*interpreter)(struct ptlrpc_request *,void *,int) =
1034 req->rq_interpret_reply;
1035 req->rq_status = interpreter(req, &req->rq_async_args,
1038 req->rq_phase = RQ_PHASE_COMPLETE;
1040 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:"
1041 "opc %s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1042 imp->imp_obd->obd_uuid.uuid,
1043 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1044 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1045 lustre_msg_get_opc(req->rq_reqmsg));
1047 set->set_remaining--;
1049 atomic_dec(&imp->imp_inflight);
1050 cfs_waitq_signal(&imp->imp_recovery_waitq);
1053 /* If we hit an error, we want to recover promptly. */
1054 RETURN(set->set_remaining == 0 || force_timer_recalc);
1057 int ptlrpc_expire_one_request(struct ptlrpc_request *req)
1059 struct obd_import *imp = req->rq_import;
1063 DEBUG_REQ(D_ERROR|D_NETERROR, req, "%s (sent at %lu, %lus ago)",
1064 req->rq_net_err ? "network error" : "timeout",
1065 (long)req->rq_sent, CURRENT_SECONDS - req->rq_sent);
1067 if (imp != NULL && obd_debug_peer_on_timeout)
1068 LNetCtl(IOC_LIBCFS_DEBUG_PEER, &imp->imp_connection->c_peer);
1070 spin_lock(&req->rq_lock);
1071 req->rq_timedout = 1;
1072 req->rq_wait_ctx = 0;
1073 spin_unlock(&req->rq_lock);
1075 ptlrpc_unregister_reply (req);
1077 if (obd_dump_on_timeout)
1078 libcfs_debug_dumplog();
1080 if (req->rq_bulk != NULL)
1081 ptlrpc_unregister_bulk (req);
1084 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
1088 /* The DLM server doesn't want recovery run on its imports. */
1089 if (imp->imp_dlm_fake)
1092 /* If this request is for recovery or other primordial tasks,
1093 * then error it out here. */
1094 if (req->rq_ctx_init || req->rq_ctx_fini ||
1095 req->rq_send_state != LUSTRE_IMP_FULL ||
1096 imp->imp_obd->obd_no_recov) {
1097 spin_lock(&req->rq_lock);
1098 req->rq_status = -ETIMEDOUT;
1100 spin_unlock(&req->rq_lock);
1104 /* if request can't be resend we can't wait answer after timeout */
1105 if (req->rq_no_resend) {
1106 DEBUG_REQ(D_RPCTRACE, req, "TIMEOUT-NORESEND:");
1110 ptlrpc_fail_import(imp, lustre_msg_get_conn_cnt(req->rq_reqmsg));
1115 int ptlrpc_expired_set(void *data)
1117 struct ptlrpc_request_set *set = data;
1118 struct list_head *tmp;
1119 time_t now = CURRENT_SECONDS;
1122 LASSERT(set != NULL);
1124 /* A timeout expired; see which reqs it applies to... */
1125 list_for_each (tmp, &set->set_requests) {
1126 struct ptlrpc_request *req =
1127 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1129 /* request in-flight? */
1130 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting &&
1132 (req->rq_phase == RQ_PHASE_BULK)))
1135 if (req->rq_timedout || /* already dealt with */
1136 req->rq_sent + req->rq_timeout > now) /* not expired */
1139 /* deal with this guy */
1140 ptlrpc_expire_one_request (req);
1143 /* When waiting for a whole set, we always to break out of the
1144 * sleep so we can recalculate the timeout, or enable interrupts
1145 * iff everyone's timed out.
1150 void ptlrpc_mark_interrupted(struct ptlrpc_request *req)
1152 spin_lock(&req->rq_lock);
1154 spin_unlock(&req->rq_lock);
1157 void ptlrpc_interrupted_set(void *data)
1159 struct ptlrpc_request_set *set = data;
1160 struct list_head *tmp;
1162 LASSERT(set != NULL);
1163 CERROR("INTERRUPTED SET %p\n", set);
1165 list_for_each(tmp, &set->set_requests) {
1166 struct ptlrpc_request *req =
1167 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1169 if (req->rq_phase != RQ_PHASE_RPC)
1172 ptlrpc_mark_interrupted(req);
1176 int ptlrpc_set_next_timeout(struct ptlrpc_request_set *set)
1178 struct list_head *tmp;
1179 time_t now = CURRENT_SECONDS;
1182 struct ptlrpc_request *req;
1185 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1187 list_for_each(tmp, &set->set_requests) {
1188 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1190 /* request in-flight? */
1191 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting) ||
1192 (req->rq_phase == RQ_PHASE_BULK) ||
1193 (req->rq_phase == RQ_PHASE_NEW)))
1196 if (req->rq_timedout) /* already timed out */
1199 if (req->rq_phase == RQ_PHASE_NEW)
1200 deadline = req->rq_sent;
1202 deadline = req->rq_sent + req->rq_timeout;
1204 if (deadline <= now) /* actually expired already */
1205 timeout = 1; /* ASAP */
1206 else if (timeout == 0 || timeout > deadline - now)
1207 timeout = deadline - now;
1212 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
1214 struct list_head *tmp;
1215 struct ptlrpc_request *req;
1216 struct l_wait_info lwi;
1220 if (list_empty(&set->set_requests))
1223 list_for_each(tmp, &set->set_requests) {
1224 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1225 if (req->rq_phase == RQ_PHASE_NEW)
1226 (void)ptlrpc_send_new_req(req);
1230 timeout = ptlrpc_set_next_timeout(set);
1232 /* wait until all complete, interrupted, or an in-flight
1234 CDEBUG(D_RPCTRACE, "set %p going to sleep for %d seconds\n",
1236 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout ? timeout : 1),
1238 ptlrpc_interrupted_set, set);
1239 rc = l_wait_event(set->set_waitq, ptlrpc_check_set(set), &lwi);
1241 LASSERT(rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
1243 /* -EINTR => all requests have been flagged rq_intr so next
1245 * -ETIMEOUTD => someone timed out. When all reqs have
1246 * timed out, signals are enabled allowing completion with
1248 * I don't really care if we go once more round the loop in
1249 * the error cases -eeb. */
1250 } while (rc != 0 || set->set_remaining != 0);
1252 LASSERT(set->set_remaining == 0);
1255 list_for_each(tmp, &set->set_requests) {
1256 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
1258 LASSERT(req->rq_phase == RQ_PHASE_COMPLETE);
1259 if (req->rq_status != 0)
1260 rc = req->rq_status;
1263 if (set->set_interpret != NULL) {
1264 int (*interpreter)(struct ptlrpc_request_set *set,void *,int) =
1266 rc = interpreter (set, set->set_arg, rc);
1272 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
1275 if (request == NULL) {
1280 LASSERTF(!request->rq_receiving_reply, "req %p\n", request);
1281 LASSERTF(request->rq_rqbd == NULL, "req %p\n",request);/* client-side */
1282 LASSERTF(list_empty(&request->rq_list), "req %p\n", request);
1283 LASSERTF(list_empty(&request->rq_set_chain), "req %p\n", request);
1284 LASSERT(request->rq_cli_ctx);
1286 /* We must take it off the imp_replay_list first. Otherwise, we'll set
1287 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
1288 if (request->rq_import != NULL) {
1290 spin_lock(&request->rq_import->imp_lock);
1291 list_del_init(&request->rq_mod_list);
1292 list_del_init(&request->rq_replay_list);
1294 spin_unlock(&request->rq_import->imp_lock);
1296 LASSERTF(list_empty(&request->rq_replay_list), "req %p\n", request);
1298 if (atomic_read(&request->rq_refcount) != 0) {
1299 DEBUG_REQ(D_ERROR, request,
1300 "freeing request with nonzero refcount");
1304 if (request->rq_repbuf != NULL)
1305 sptlrpc_cli_free_repbuf(request);
1306 if (request->rq_export != NULL) {
1307 class_export_put(request->rq_export);
1308 request->rq_export = NULL;
1310 if (request->rq_import != NULL) {
1311 class_import_put(request->rq_import);
1312 request->rq_import = NULL;
1314 if (request->rq_bulk != NULL)
1315 ptlrpc_free_bulk(request->rq_bulk);
1317 if (request->rq_reqbuf != NULL || request->rq_clrbuf != NULL)
1318 sptlrpc_cli_free_reqbuf(request);
1320 sptlrpc_req_put_ctx(request, !locked);
1322 if (request->rq_pool)
1323 __ptlrpc_free_req_to_pool(request);
1325 OBD_FREE(request, sizeof(*request));
1329 void ptlrpc_free_req(struct ptlrpc_request *request)
1331 __ptlrpc_free_req(request, 0);
1334 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked);
1335 void ptlrpc_req_finished_with_imp_lock(struct ptlrpc_request *request)
1337 LASSERT_SPIN_LOCKED(&request->rq_import->imp_lock);
1338 (void)__ptlrpc_req_finished(request, 1);
1341 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
1344 if (request == NULL)
1347 if (request == LP_POISON ||
1348 request->rq_reqmsg == LP_POISON) {
1349 CERROR("dereferencing freed request (bug 575)\n");
1354 DEBUG_REQ(D_INFO, request, "refcount now %u",
1355 atomic_read(&request->rq_refcount) - 1);
1357 if (atomic_dec_and_test(&request->rq_refcount)) {
1358 __ptlrpc_free_req(request, locked);
1365 void ptlrpc_req_finished(struct ptlrpc_request *request)
1367 __ptlrpc_req_finished(request, 0);
1370 __u64 ptlrpc_req_xid(struct ptlrpc_request *request)
1372 return request->rq_xid;
1374 EXPORT_SYMBOL(ptlrpc_req_xid);
1376 /* Disengage the client's reply buffer from the network
1377 * NB does _NOT_ unregister any client-side bulk.
1378 * IDEMPOTENT, but _not_ safe against concurrent callers.
1379 * The request owner (i.e. the thread doing the I/O) must call...
1381 void ptlrpc_unregister_reply (struct ptlrpc_request *request)
1385 struct l_wait_info lwi;
1387 LASSERT(!in_interrupt ()); /* might sleep */
1389 if (!ptlrpc_client_receiving_reply(request))
1392 LNetMDUnlink (request->rq_reply_md_h);
1394 /* We have to l_wait_event() whatever the result, to give liblustre
1395 * a chance to run reply_in_callback() */
1397 if (request->rq_set != NULL)
1398 wq = &request->rq_set->set_waitq;
1400 wq = &request->rq_reply_waitq;
1403 /* Network access will complete in finite time but the HUGE
1404 * timeout lets us CWARN for visibility of sluggish NALs */
1405 lwi = LWI_TIMEOUT(cfs_time_seconds(300), NULL, NULL);
1406 rc = l_wait_event (*wq, !ptlrpc_client_receiving_reply(request), &lwi);
1410 LASSERT (rc == -ETIMEDOUT);
1411 DEBUG_REQ(D_WARNING, request, "Unexpectedly long timeout");
1415 /* caller must hold imp->imp_lock */
1416 void ptlrpc_free_committed(struct obd_import *imp)
1418 struct list_head *tmp, *saved;
1419 struct ptlrpc_request *req;
1420 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1423 LASSERT(imp != NULL);
1425 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1428 if (imp->imp_peer_committed_transno == imp->imp_last_transno_checked &&
1429 imp->imp_generation == imp->imp_last_generation_checked) {
1430 CDEBUG(D_RPCTRACE, "%s: skip recheck: last_committed "LPU64"\n",
1431 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1435 CDEBUG(D_RPCTRACE, "%s: committing for last_committed "LPU64" gen %d\n",
1436 imp->imp_obd->obd_name, imp->imp_peer_committed_transno,
1437 imp->imp_generation);
1438 imp->imp_last_transno_checked = imp->imp_peer_committed_transno;
1439 imp->imp_last_generation_checked = imp->imp_generation;
1441 list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1442 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1444 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
1445 LASSERT(req != last_req);
1448 if (req->rq_import_generation < imp->imp_generation) {
1449 DEBUG_REQ(D_RPCTRACE, req, "free request with old gen");
1453 if (req->rq_replay) {
1454 DEBUG_REQ(D_RPCTRACE, req, "keeping (FL_REPLAY)");
1458 /* not yet committed */
1459 if (req->rq_transno > imp->imp_peer_committed_transno) {
1460 DEBUG_REQ(D_RPCTRACE, req, "stopping search");
1464 DEBUG_REQ(D_RPCTRACE, req, "commit (last_committed "LPU64")",
1465 imp->imp_peer_committed_transno);
1467 spin_lock(&req->rq_lock);
1469 spin_unlock(&req->rq_lock);
1470 if (req->rq_commit_cb != NULL)
1471 req->rq_commit_cb(req);
1472 list_del_init(&req->rq_replay_list);
1473 __ptlrpc_req_finished(req, 1);
1480 void ptlrpc_cleanup_client(struct obd_import *imp)
1487 void ptlrpc_resend_req(struct ptlrpc_request *req)
1489 DEBUG_REQ(D_HA, req, "going to resend");
1490 lustre_msg_set_handle(req->rq_reqmsg, &(struct lustre_handle){ 0 });
1491 req->rq_status = -EAGAIN;
1493 spin_lock(&req->rq_lock);
1495 req->rq_net_err = 0;
1496 req->rq_timedout = 0;
1498 __u64 old_xid = req->rq_xid;
1500 /* ensure previous bulk fails */
1501 req->rq_xid = ptlrpc_next_xid();
1502 CDEBUG(D_HA, "resend bulk old x"LPU64" new x"LPU64"\n",
1503 old_xid, req->rq_xid);
1505 ptlrpc_wake_client_req(req);
1506 spin_unlock(&req->rq_lock);
1509 /* XXX: this function and rq_status are currently unused */
1510 void ptlrpc_restart_req(struct ptlrpc_request *req)
1512 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1513 req->rq_status = -ERESTARTSYS;
1515 spin_lock(&req->rq_lock);
1516 req->rq_restart = 1;
1517 req->rq_timedout = 0;
1518 ptlrpc_wake_client_req(req);
1519 spin_unlock(&req->rq_lock);
1522 static int expired_request(void *data)
1524 struct ptlrpc_request *req = data;
1527 /* some failure can suspend regular timeouts */
1528 if (ptlrpc_check_suspend())
1531 RETURN(ptlrpc_expire_one_request(req));
1534 static void interrupted_request(void *data)
1536 struct ptlrpc_request *req = data;
1537 DEBUG_REQ(D_HA, req, "request interrupted");
1538 spin_lock(&req->rq_lock);
1540 spin_unlock(&req->rq_lock);
1543 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1546 atomic_inc(&req->rq_refcount);
1550 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1551 struct obd_import *imp)
1553 struct list_head *tmp;
1555 LASSERT_SPIN_LOCKED(&imp->imp_lock);
1557 /* clear this for new requests that were resent as well
1558 as resent replayed requests. */
1559 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1561 /* don't re-add requests that have been replayed */
1562 if (!list_empty(&req->rq_replay_list))
1565 lustre_msg_add_flags(req->rq_reqmsg, MSG_REPLAY);
1567 LASSERT(imp->imp_replayable);
1568 /* Balanced in ptlrpc_free_committed, usually. */
1569 ptlrpc_request_addref(req);
1570 list_for_each_prev(tmp, &imp->imp_replay_list) {
1571 struct ptlrpc_request *iter =
1572 list_entry(tmp, struct ptlrpc_request, rq_replay_list);
1574 /* We may have duplicate transnos if we create and then
1575 * open a file, or for closes retained if to match creating
1576 * opens, so use req->rq_xid as a secondary key.
1577 * (See bugs 684, 685, and 428.)
1578 * XXX no longer needed, but all opens need transnos!
1580 if (iter->rq_transno > req->rq_transno)
1583 if (iter->rq_transno == req->rq_transno) {
1584 LASSERT(iter->rq_xid != req->rq_xid);
1585 if (iter->rq_xid > req->rq_xid)
1589 list_add(&req->rq_replay_list, &iter->rq_replay_list);
1593 list_add_tail(&req->rq_replay_list, &imp->imp_replay_list);
1596 int ptlrpc_queue_wait(struct ptlrpc_request *req)
1600 struct l_wait_info lwi;
1601 struct obd_import *imp = req->rq_import;
1602 cfs_duration_t timeout = 0;
1605 LASSERT(req->rq_set == NULL);
1606 LASSERT(!req->rq_receiving_reply);
1607 atomic_inc(&imp->imp_inflight);
1609 /* for distributed debugging */
1610 lustre_msg_set_status(req->rq_reqmsg, cfs_curproc_pid());
1611 LASSERT(imp->imp_obd != NULL);
1612 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:nid:opc "
1613 "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1614 imp->imp_obd->obd_uuid.uuid,
1615 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1616 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1617 lustre_msg_get_opc(req->rq_reqmsg));
1619 /* Mark phase here for a little debug help */
1620 req->rq_phase = RQ_PHASE_RPC;
1622 spin_lock(&imp->imp_lock);
1624 req->rq_import_generation = imp->imp_generation;
1625 if (ptlrpc_import_delay_req(imp, req, &rc)) {
1626 list_del(&req->rq_list);
1628 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1629 spin_unlock(&imp->imp_lock);
1631 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%s != %s)",
1633 ptlrpc_import_state_name(req->rq_send_state),
1634 ptlrpc_import_state_name(imp->imp_state));
1635 lwi = LWI_INTR(interrupted_request, req);
1636 rc = l_wait_event(req->rq_reply_waitq,
1637 (req->rq_send_state == imp->imp_state ||
1638 req->rq_err || req->rq_intr),
1640 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%s == %s or %d/%d == 1)",
1642 ptlrpc_import_state_name(imp->imp_state),
1643 ptlrpc_import_state_name(req->rq_send_state),
1644 req->rq_err, req->rq_intr);
1646 spin_lock(&imp->imp_lock);
1647 list_del_init(&req->rq_list);
1652 else if (req->rq_intr) {
1655 else if (req->rq_no_resend) {
1656 spin_unlock(&imp->imp_lock);
1657 GOTO(out, rc = -ETIMEDOUT);
1665 list_del_init(&req->rq_list);
1666 spin_unlock(&imp->imp_lock);
1667 req->rq_status = rc; // XXX this ok?
1671 if (req->rq_resend) {
1672 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
1674 if (req->rq_bulk != NULL) {
1675 ptlrpc_unregister_bulk (req);
1677 /* bulk requests are supposed to be
1678 * idempotent, so we are free to bump the xid
1679 * here, which we need to do before
1680 * registering the bulk again (bug 6371).
1681 * print the old xid first for sanity.
1683 DEBUG_REQ(D_HA, req, "bumping xid for bulk: ");
1684 req->rq_xid = ptlrpc_next_xid();
1687 DEBUG_REQ(D_HA, req, "resending: ");
1690 /* XXX this is the same as ptlrpc_set_wait */
1691 LASSERT(list_empty(&req->rq_list));
1692 list_add_tail(&req->rq_list, &imp->imp_sending_list);
1693 spin_unlock(&imp->imp_lock);
1695 rc = sptlrpc_req_refresh_ctx(req, 0);
1698 /* we got fatal ctx refresh error, directly jump out
1699 * thus we can pass back the actual error code.
1701 spin_lock(&imp->imp_lock);
1702 list_del_init(&req->rq_list);
1703 spin_unlock(&imp->imp_lock);
1705 CERROR("Failed to refresh ctx of req %p: %d\n", req, rc);
1708 /* simulating we got error during send rpc */
1712 rc = ptl_send_rpc(req, 0);
1714 DEBUG_REQ(D_HA, req, "send failed (%d); recovering", rc);
1717 timeout = cfs_timeout_cap(cfs_time_seconds(req->rq_timeout));
1718 DEBUG_REQ(D_NET, req,
1719 "-- sleeping for "CFS_DURATION_T" jiffies", timeout);
1722 lwi = LWI_TIMEOUT_INTR(timeout, expired_request, interrupted_request,
1724 rc = l_wait_event(req->rq_reply_waitq, ptlrpc_check_reply(req), &lwi);
1725 if (rc == -ETIMEDOUT && ptlrpc_check_and_wait_suspend(req))
1729 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:nid:opc "
1730 "%s:%s:%d:"LPU64":%s:%d\n", cfs_curproc_comm(),
1731 imp->imp_obd->obd_uuid.uuid,
1732 lustre_msg_get_status(req->rq_reqmsg), req->rq_xid,
1733 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1734 lustre_msg_get_opc(req->rq_reqmsg));
1736 spin_lock(&imp->imp_lock);
1737 list_del_init(&req->rq_list);
1738 spin_unlock(&imp->imp_lock);
1740 /* If the reply was received normally, this just grabs the spinlock
1741 * (ensuring the reply callback has returned), sees that
1742 * req->rq_receiving_reply is clear and returns. */
1743 ptlrpc_unregister_reply (req);
1746 GOTO(out, rc = -EIO);
1748 /* Resend if we need to, unless we were interrupted. */
1749 if (req->rq_resend && !req->rq_intr) {
1750 /* ...unless we were specifically told otherwise. */
1751 if (req->rq_no_resend)
1752 GOTO(out, rc = -ETIMEDOUT);
1753 spin_lock(&imp->imp_lock);
1758 /* Should only be interrupted if we timed out. */
1759 if (!req->rq_timedout)
1760 DEBUG_REQ(D_ERROR, req,
1761 "rq_intr set but rq_timedout not");
1762 GOTO(out, rc = -EINTR);
1765 if (req->rq_timedout) { /* non-recoverable timeout */
1766 GOTO(out, rc = -ETIMEDOUT);
1769 if (!req->rq_replied) {
1770 /* How can this be? -eeb */
1771 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
1773 GOTO(out, rc = req->rq_status);
1776 rc = after_reply(req);
1777 /* NB may return +ve success rc */
1778 if (req->rq_resend) {
1779 spin_lock(&imp->imp_lock);
1784 if (req->rq_bulk != NULL) {
1786 /* success so far. Note that anything going wrong
1787 * with bulk now, is EXTREMELY strange, since the
1788 * server must have believed that the bulk
1789 * tranferred OK before she replied with success to
1791 lwi = LWI_TIMEOUT(timeout, NULL, NULL);
1792 brc = l_wait_event(req->rq_reply_waitq,
1793 !ptlrpc_bulk_active(req->rq_bulk),
1795 LASSERT(brc == 0 || brc == -ETIMEDOUT);
1797 LASSERT(brc == -ETIMEDOUT);
1798 DEBUG_REQ(D_ERROR, req, "bulk timed out");
1800 } else if (!req->rq_bulk->bd_success) {
1801 DEBUG_REQ(D_ERROR, req, "bulk transfer failed");
1806 ptlrpc_unregister_bulk (req);
1809 LASSERT(!req->rq_receiving_reply);
1810 req->rq_phase = RQ_PHASE_INTERPRET;
1812 atomic_dec(&imp->imp_inflight);
1813 cfs_waitq_signal(&imp->imp_recovery_waitq);
1817 struct ptlrpc_replay_async_args {
1819 int praa_old_status;
1822 static int ptlrpc_replay_interpret(struct ptlrpc_request *req,
1823 void * data, int rc)
1825 struct ptlrpc_replay_async_args *aa = data;
1826 struct obd_import *imp = req->rq_import;
1829 atomic_dec(&imp->imp_replay_inflight);
1831 if (!req->rq_replied) {
1832 CERROR("request replay timed out, restarting recovery\n");
1833 GOTO(out, rc = -ETIMEDOUT);
1836 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR &&
1837 (lustre_msg_get_status(req->rq_repmsg) == -ENOTCONN ||
1838 lustre_msg_get_status(req->rq_repmsg) == -ENODEV))
1839 GOTO(out, rc = lustre_msg_get_status(req->rq_repmsg));
1841 /* The transno had better not change over replay. */
1842 LASSERT(lustre_msg_get_transno(req->rq_reqmsg) ==
1843 lustre_msg_get_transno(req->rq_repmsg));
1845 DEBUG_REQ(D_HA, req, "got rep");
1847 /* let the callback do fixups, possibly including in the request */
1848 if (req->rq_replay_cb)
1849 req->rq_replay_cb(req);
1851 if (req->rq_replied &&
1852 lustre_msg_get_status(req->rq_repmsg) != aa->praa_old_status) {
1853 DEBUG_REQ(D_ERROR, req, "status %d, old was %d",
1854 lustre_msg_get_status(req->rq_repmsg),
1855 aa->praa_old_status);
1857 /* Put it back for re-replay. */
1858 lustre_msg_set_status(req->rq_repmsg, aa->praa_old_status);
1862 * Errors while replay can set transno to 0, but
1863 * imp_last_replay_transno shouldn't be set to 0 anyway
1865 if (req->rq_transno > 0) {
1866 spin_lock(&imp->imp_lock);
1867 LASSERT(req->rq_transno <= imp->imp_last_replay_transno);
1868 imp->imp_last_replay_transno = req->rq_transno;
1869 spin_unlock(&imp->imp_lock);
1871 CERROR("Transno is 0 during replay!\n");
1872 /* continue with recovery */
1873 rc = ptlrpc_import_recovery_state_machine(imp);
1875 req->rq_send_state = aa->praa_old_state;
1878 /* this replay failed, so restart recovery */
1879 ptlrpc_connect_import(imp, NULL);
1884 int ptlrpc_replay_req(struct ptlrpc_request *req)
1886 struct ptlrpc_replay_async_args *aa;
1889 LASSERT(req->rq_import->imp_state == LUSTRE_IMP_REPLAY);
1891 /* Not handling automatic bulk replay yet (or ever?) */
1892 LASSERT(req->rq_bulk == NULL);
1894 DEBUG_REQ(D_HA, req, "REPLAY");
1896 LASSERT (sizeof (*aa) <= sizeof (req->rq_async_args));
1897 aa = (struct ptlrpc_replay_async_args *)&req->rq_async_args;
1898 memset(aa, 0, sizeof *aa);
1900 /* Prepare request to be resent with ptlrpcd */
1901 aa->praa_old_state = req->rq_send_state;
1902 req->rq_send_state = LUSTRE_IMP_REPLAY;
1903 req->rq_phase = RQ_PHASE_NEW;
1904 aa->praa_old_status = lustre_msg_get_status(req->rq_repmsg);
1907 req->rq_interpret_reply = ptlrpc_replay_interpret;
1908 atomic_inc(&req->rq_import->imp_replay_inflight);
1909 ptlrpc_request_addref(req); /* ptlrpcd needs a ref */
1911 ptlrpcd_add_req(req);
1915 void ptlrpc_abort_inflight(struct obd_import *imp)
1917 struct list_head *tmp, *n;
1920 /* Make sure that no new requests get processed for this import.
1921 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
1922 * this flag and then putting requests on sending_list or delayed_list.
1924 spin_lock(&imp->imp_lock);
1926 /* XXX locking? Maybe we should remove each request with the list
1927 * locked? Also, how do we know if the requests on the list are
1928 * being freed at this time?
1930 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
1931 struct ptlrpc_request *req =
1932 list_entry(tmp, struct ptlrpc_request, rq_list);
1934 DEBUG_REQ(D_RPCTRACE, req, "inflight");
1936 spin_lock (&req->rq_lock);
1937 if (req->rq_import_generation < imp->imp_generation) {
1939 ptlrpc_wake_client_req(req);
1941 spin_unlock (&req->rq_lock);
1944 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
1945 struct ptlrpc_request *req =
1946 list_entry(tmp, struct ptlrpc_request, rq_list);
1948 DEBUG_REQ(D_RPCTRACE, req, "aborting waiting req");
1950 spin_lock (&req->rq_lock);
1951 if (req->rq_import_generation < imp->imp_generation) {
1953 ptlrpc_wake_client_req(req);
1955 spin_unlock (&req->rq_lock);
1958 /* Last chance to free reqs left on the replay list, but we
1959 * will still leak reqs that haven't committed. */
1960 if (imp->imp_replayable)
1961 ptlrpc_free_committed(imp);
1963 spin_unlock(&imp->imp_lock);
1968 static __u64 ptlrpc_last_xid = 0;
1969 spinlock_t ptlrpc_last_xid_lock;
1971 __u64 ptlrpc_next_xid(void)
1974 spin_lock(&ptlrpc_last_xid_lock);
1975 tmp = ++ptlrpc_last_xid;
1976 spin_unlock(&ptlrpc_last_xid_lock);
1980 __u64 ptlrpc_sample_next_xid(void)
1983 spin_lock(&ptlrpc_last_xid_lock);
1984 tmp = ptlrpc_last_xid + 1;
1985 spin_unlock(&ptlrpc_last_xid_lock);
1988 EXPORT_SYMBOL(ptlrpc_sample_next_xid);