1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_RPC
27 #include <liblustre.h>
30 #include <linux/obd_support.h>
31 #include <linux/obd_class.h>
32 #include <linux/lustre_lib.h>
33 #include <linux/lustre_ha.h>
34 #include <linux/lustre_import.h>
36 #include "ptlrpc_internal.h"
38 void ptlrpc_init_client(int req_portal, int rep_portal, char *name,
39 struct ptlrpc_client *cl)
41 cl->cli_request_portal = req_portal;
42 cl->cli_reply_portal = rep_portal;
46 struct obd_uuid *ptlrpc_req_to_uuid(struct ptlrpc_request *req)
48 return &req->rq_connection->c_remote_uuid;
51 struct ptlrpc_connection *ptlrpc_uuid_to_connection(struct obd_uuid *uuid)
53 struct ptlrpc_connection *c;
54 struct ptlrpc_peer peer;
57 err = ptlrpc_uuid_to_peer(uuid, &peer);
59 CERROR("cannot find peer %s!\n", uuid->uuid);
63 c = ptlrpc_get_connection(&peer, uuid);
65 memcpy(c->c_remote_uuid.uuid,
66 uuid->uuid, sizeof(c->c_remote_uuid.uuid));
70 CDEBUG(D_INFO, "%s -> %p\n", uuid->uuid, c);
75 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn,
76 struct obd_uuid *uuid)
78 struct ptlrpc_peer peer;
81 err = ptlrpc_uuid_to_peer (uuid, &peer);
83 CERROR("cannot find peer %s!\n", uuid->uuid);
87 memcpy (&conn->c_peer, &peer, sizeof (peer));
91 static inline struct ptlrpc_bulk_desc *new_bulk(void)
93 struct ptlrpc_bulk_desc *desc;
95 OBD_ALLOC(desc, sizeof(*desc));
99 spin_lock_init (&desc->bd_lock);
100 init_waitqueue_head(&desc->bd_waitq);
101 INIT_LIST_HEAD(&desc->bd_page_list);
102 desc->bd_md_h = PTL_HANDLE_NONE;
103 desc->bd_me_h = PTL_HANDLE_NONE;
108 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_imp (struct ptlrpc_request *req,
109 int type, int portal)
111 struct obd_import *imp = req->rq_import;
113 struct ptlrpc_bulk_desc *desc;
115 LASSERT (type == BULK_PUT_SINK || type == BULK_GET_SOURCE);
121 /* Is this sampled at the right place? Do we want to get the import
122 * generation just before we send? Should it match the generation of
124 spin_lock_irqsave(&imp->imp_lock, flags);
125 desc->bd_import_generation = imp->imp_generation;
126 spin_unlock_irqrestore(&imp->imp_lock, flags);
128 desc->bd_import = class_import_get(imp);
130 desc->bd_type = type;
131 desc->bd_portal = portal;
133 /* This makes req own desc, and free it when she frees herself */
139 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk_exp (struct ptlrpc_request *req,
140 int type, int portal)
142 struct obd_export *exp = req->rq_export;
143 struct ptlrpc_bulk_desc *desc;
145 LASSERT (type == BULK_PUT_SOURCE || type == BULK_GET_SINK);
151 desc->bd_export = class_export_get(exp);
153 desc->bd_type = type;
154 desc->bd_portal = portal;
156 /* NB we don't assign rq_bulk here; server-side requests are
157 * re-used, and the handler frees the bulk desc explicitly. */
162 int ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc,
163 struct page *page, int pageoffset, int len)
165 struct ptlrpc_bulk_page *bulk;
167 OBD_ALLOC(bulk, sizeof(*bulk));
171 LASSERT (page != NULL);
172 LASSERT (pageoffset >= 0);
174 LASSERT (pageoffset + len <= PAGE_SIZE);
176 bulk->bp_page = page;
177 bulk->bp_pageoffset = pageoffset;
178 bulk->bp_buflen = len;
180 bulk->bp_desc = desc;
181 list_add_tail(&bulk->bp_link, &desc->bd_page_list);
182 desc->bd_page_count++;
186 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *desc)
188 struct list_head *tmp, *next;
191 LASSERT (desc != NULL);
192 LASSERT (desc->bd_page_count != 0x5a5a5a5a); /* not freed already */
193 LASSERT (!desc->bd_network_rw); /* network hands off or */
195 list_for_each_safe(tmp, next, &desc->bd_page_list) {
196 struct ptlrpc_bulk_page *bulk;
197 bulk = list_entry(tmp, struct ptlrpc_bulk_page, bp_link);
198 ptlrpc_free_bulk_page(bulk);
201 LASSERT (desc->bd_page_count == 0);
202 LASSERT((desc->bd_export != NULL) ^ (desc->bd_import != NULL));
205 class_export_put(desc->bd_export);
207 class_import_put(desc->bd_import);
209 OBD_FREE(desc, sizeof(*desc));
213 void ptlrpc_free_bulk_page(struct ptlrpc_bulk_page *bulk)
215 LASSERT (bulk != NULL);
217 list_del(&bulk->bp_link);
218 bulk->bp_desc->bd_page_count--;
219 OBD_FREE(bulk, sizeof(*bulk));
222 struct ptlrpc_request *ptlrpc_prep_req(struct obd_import *imp, int opcode,
223 int count, int *lengths, char **bufs)
225 struct ptlrpc_request *request;
229 LASSERT((unsigned long)imp > 0x1000);
231 OBD_ALLOC(request, sizeof(*request));
233 CERROR("request allocation out of memory\n");
237 rc = lustre_pack_msg(count, lengths, bufs,
238 &request->rq_reqlen, &request->rq_reqmsg);
240 CERROR("cannot pack request %d\n", rc);
241 OBD_FREE(request, sizeof(*request));
245 request->rq_timeout = obd_timeout;
246 request->rq_level = LUSTRE_CONN_FULL;
247 request->rq_type = PTL_RPC_MSG_REQUEST;
248 request->rq_import = class_import_get(imp);
249 request->rq_phase = RQ_PHASE_NEW;
251 /* XXX FIXME bug 249 */
252 request->rq_request_portal = imp->imp_client->cli_request_portal;
253 request->rq_reply_portal = imp->imp_client->cli_reply_portal;
255 request->rq_connection = ptlrpc_connection_addref(imp->imp_connection);
257 spin_lock_init (&request->rq_lock);
258 INIT_LIST_HEAD(&request->rq_list);
259 init_waitqueue_head(&request->rq_wait_for_rep);
260 request->rq_xid = ptlrpc_next_xid();
261 atomic_set(&request->rq_refcount, 1);
263 request->rq_reqmsg->opc = opcode;
264 request->rq_reqmsg->flags = 0;
269 struct ptlrpc_request_set *ptlrpc_prep_set(void)
271 struct ptlrpc_request_set *set;
273 OBD_ALLOC(set, sizeof *set);
276 INIT_LIST_HEAD(&set->set_requests);
277 init_waitqueue_head(&set->set_waitq);
278 set->set_remaining = 0;
283 /* Finish with this set; opposite of prep_set. */
284 void ptlrpc_set_destroy(struct ptlrpc_request_set *set)
286 struct list_head *tmp;
287 struct list_head *next;
292 /* Requests on the set should either all be completed, or all be new */
293 expected_phase = (set->set_remaining == 0) ?
294 RQ_PHASE_COMPLETE : RQ_PHASE_NEW;
295 list_for_each (tmp, &set->set_requests) {
296 struct ptlrpc_request *req =
297 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
299 LASSERT (req->rq_phase == expected_phase);
303 LASSERT (set->set_remaining == 0 || set->set_remaining == n);
305 list_for_each_safe(tmp, next, &set->set_requests) {
306 struct ptlrpc_request *req =
307 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
308 list_del_init(&req->rq_set_chain);
310 LASSERT (req->rq_phase == expected_phase);
312 if (req->rq_phase == RQ_PHASE_NEW) {
314 if (req->rq_interpret_reply != NULL) {
315 int (*interpreter)(struct ptlrpc_request *, void *, int) =
316 req->rq_interpret_reply;
318 /* higher level (i.e. LOV) failed;
319 * let the sub reqs clean up */
320 req->rq_status = -EBADR;
321 interpreter(req, &req->rq_async_args, req->rq_status);
323 set->set_remaining--;
327 ptlrpc_req_finished (req);
330 LASSERT(set->set_remaining == 0);
332 OBD_FREE(set, sizeof(*set));
336 void ptlrpc_set_add_req(struct ptlrpc_request_set *set,
337 struct ptlrpc_request *req)
339 /* The set takes over the caller's request reference */
340 list_add_tail(&req->rq_set_chain, &set->set_requests);
342 set->set_remaining++;
345 static int ptlrpc_check_reply(struct ptlrpc_request *req)
351 /* serialise with network callback */
352 spin_lock_irqsave (&req->rq_lock, flags);
354 if (req->rq_replied) {
355 DEBUG_REQ(D_NET, req, "REPLIED:");
360 DEBUG_REQ(D_ERROR, req, "ABORTED:");
364 if (req->rq_resend) {
365 DEBUG_REQ(D_ERROR, req, "RESEND:");
369 if (req->rq_restart) {
370 DEBUG_REQ(D_ERROR, req, "RESTART:");
375 spin_unlock_irqrestore (&req->rq_lock, flags);
376 DEBUG_REQ(D_NET, req, "rc = %d for", rc);
380 static int ptlrpc_check_status(struct ptlrpc_request *req)
385 err = req->rq_repmsg->status;
386 if (req->rq_repmsg->type == PTL_RPC_MSG_ERR) {
387 DEBUG_REQ(D_ERROR, req, "type == PTL_RPC_MSG_ERR (%d)", err);
389 CERROR("Error Reply has >= zero status\n");
390 RETURN(err < 0 ? err : -EINVAL);
394 DEBUG_REQ(D_INFO, req, "status is %d", err);
395 } else if (err > 0) {
396 /* XXX: translate this error from net to host */
397 DEBUG_REQ(D_INFO, req, "status is %d", err);
403 #warning this needs to change after robert fixes eviction handling
405 after_reply(struct ptlrpc_request *req, int *restartp)
408 struct obd_import *imp = req->rq_import;
412 LASSERT (!req->rq_receiving_reply);
413 LASSERT (req->rq_replied);
415 if (restartp != NULL)
418 /* NB Until this point, the whole of the incoming message,
419 * including buflens, status etc is in the sender's byte order. */
422 /* Clear reply swab mask; this is a new reply in sender's byte order */
423 req->rq_rep_swab_mask = 0;
425 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
427 CERROR("unpack_rep failed: %d\n", rc);
431 if (req->rq_repmsg->type != PTL_RPC_MSG_REPLY &&
432 req->rq_repmsg->type != PTL_RPC_MSG_ERR) {
433 CERROR("invalid packet type received (type=%u)\n",
434 req->rq_repmsg->type);
438 /* Store transno in reqmsg for replay. */
439 req->rq_reqmsg->transno = req->rq_transno = req->rq_repmsg->transno;
441 rc = ptlrpc_check_status(req);
443 /* Either we've been evicted, or the server has failed for
444 * some reason. Try to reconnect, and if that fails, punt to
446 if (rc == -ENOTCONN) {
447 if (req->rq_level < LUSTRE_CONN_FULL || req->rq_no_recov ||
448 imp->imp_obd->obd_no_recov || imp->imp_dlm_fake) {
452 rc = ptlrpc_request_handle_eviction(req);
454 CERROR("can't reconnect to %s@%s: %d\n",
455 imp->imp_target_uuid.uuid,
456 imp->imp_connection->c_remote_uuid.uuid, rc);
458 ptlrpc_wake_delayed(imp);
463 if (req->rq_resend) {
464 if (restartp == NULL)
465 LBUG(); /* async resend not supported yet */
466 spin_lock_irqsave (&req->rq_lock, flags);
468 spin_unlock_irqrestore (&req->rq_lock, flags);
470 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
471 DEBUG_REQ(D_HA, req, "resending: ");
475 CERROR("request should be err or resend: %p\n", req);
479 if (req->rq_import->imp_replayable) {
480 spin_lock_irqsave(&imp->imp_lock, flags);
481 if ((req->rq_replay || req->rq_transno != 0) && rc >= 0)
482 ptlrpc_retain_replayable_request(req, imp);
484 if (req->rq_transno > imp->imp_max_transno)
485 imp->imp_max_transno = req->rq_transno;
487 /* Replay-enabled imports return commit-status information. */
488 if (req->rq_repmsg->last_committed) {
489 if (req->rq_repmsg->last_committed <
490 imp->imp_peer_committed_transno) {
491 CERROR("%s went back in time (transno "LPD64
492 " was committed, server claims "LPD64
493 ")! is shared storage not coherent?\n",
494 imp->imp_target_uuid.uuid,
495 imp->imp_peer_committed_transno,
496 req->rq_repmsg->last_committed);
498 imp->imp_peer_committed_transno =
499 req->rq_repmsg->last_committed;
501 ptlrpc_free_committed(imp);
502 spin_unlock_irqrestore(&imp->imp_lock, flags);
508 static int check_set(struct ptlrpc_request_set *set)
511 struct list_head *tmp;
514 if (set->set_remaining == 0)
517 list_for_each(tmp, &set->set_requests) {
518 struct ptlrpc_request *req =
519 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
520 struct obd_import *imp = req->rq_import;
523 LASSERT (req->rq_phase == RQ_PHASE_RPC ||
524 req->rq_phase == RQ_PHASE_BULK ||
525 req->rq_phase == RQ_PHASE_COMPLETE);
527 if (req->rq_phase == RQ_PHASE_COMPLETE)
531 ptlrpc_unregister_reply(req);
532 if (req->rq_status == 0)
533 req->rq_status = -EIO;
534 req->rq_phase = RQ_PHASE_INTERPRET;
536 spin_lock_irqsave(&imp->imp_lock, flags);
537 list_del_init(&req->rq_list);
538 spin_unlock_irqrestore(&imp->imp_lock, flags);
540 GOTO (interpret, req->rq_status);
544 /* NB could be on delayed list */
545 ptlrpc_unregister_reply(req);
546 req->rq_status = -EINTR;
547 req->rq_phase = RQ_PHASE_INTERPRET;
549 spin_lock_irqsave(&imp->imp_lock, flags);
550 list_del_init(&req->rq_list);
551 spin_unlock_irqrestore(&imp->imp_lock, flags);
553 GOTO (interpret, req->rq_status);
556 if (req->rq_phase == RQ_PHASE_RPC) {
558 if (req->rq_waiting || req->rq_resend) {
559 spin_lock_irqsave(&imp->imp_lock, flags);
561 if (req->rq_level > imp->imp_level) {
562 spin_unlock_irqrestore(&imp->imp_lock,
567 list_del(&req->rq_list);
568 list_add_tail(&req->rq_list,
569 &imp->imp_sending_list);
570 spin_unlock_irqrestore(&imp->imp_lock, flags);
573 if (req->rq_resend) {
574 lustre_msg_add_flags(req->rq_reqmsg,
576 spin_lock_irqsave(&req->rq_lock, flags);
578 spin_unlock_irqrestore(&req->rq_lock,
580 ptlrpc_unregister_reply(req);
582 ptlrpc_unregister_bulk(req);
585 rc = ptl_send_rpc(req);
588 req->rq_phase = RQ_PHASE_INTERPRET;
589 GOTO (interpret, req->rq_status);
594 /* Ensure the network callback returned */
595 spin_lock_irqsave (&req->rq_lock, flags);
596 if (!req->rq_replied) {
597 spin_unlock_irqrestore (&req->rq_lock, flags);
600 spin_unlock_irqrestore (&req->rq_lock, flags);
602 spin_lock_irqsave(&imp->imp_lock, flags);
603 list_del_init(&req->rq_list);
604 spin_unlock_irqrestore(&imp->imp_lock, flags);
606 req->rq_status = after_reply(req, &do_restart);
608 req->rq_resend = 1; /* ugh */
612 if (req->rq_bulk == NULL) {
613 req->rq_phase = RQ_PHASE_INTERPRET;
614 GOTO (interpret, req->rq_status);
617 req->rq_phase = RQ_PHASE_BULK;
620 LASSERT (req->rq_phase == RQ_PHASE_BULK);
621 if (!ptlrpc_bulk_complete (req->rq_bulk))
624 req->rq_phase = RQ_PHASE_INTERPRET;
627 LASSERT (req->rq_phase == RQ_PHASE_INTERPRET);
628 LASSERT (!req->rq_receiving_reply);
630 if (req->rq_bulk != NULL)
631 ptlrpc_unregister_bulk (req);
633 if (req->rq_interpret_reply != NULL) {
634 int (*interpreter)(struct ptlrpc_request *, void *, int) =
635 req->rq_interpret_reply;
636 req->rq_status = interpreter(req, &req->rq_async_args,
640 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:ni:nid:"
641 "opc %s:%s:%d:"LPU64":%s:"LPX64":%d\n", current->comm,
642 imp->imp_obd->obd_uuid.uuid, req->rq_reqmsg->status,
644 imp->imp_connection->c_peer.peer_ni->pni_name,
645 imp->imp_connection->c_peer.peer_nid,
646 req->rq_reqmsg->opc);
648 req->rq_phase = RQ_PHASE_COMPLETE;
649 set->set_remaining--;
652 RETURN (set->set_remaining == 0);
655 static int expire_one_request(struct ptlrpc_request *req)
658 struct obd_import *imp = req->rq_import;
661 DEBUG_REQ(D_ERROR, req, "timeout");
663 spin_lock_irqsave (&req->rq_lock, flags);
664 req->rq_timedout = 1;
665 spin_unlock_irqrestore (&req->rq_lock, flags);
667 ptlrpc_unregister_reply (req);
670 DEBUG_REQ(D_HA, req, "NULL import: already cleaned up?");
674 /* The DLM server doesn't want recovery run on its imports. */
675 if (imp->imp_dlm_fake)
678 /* If this request is for recovery or other primordial tasks,
679 * don't go back to sleep, and don't start recovery again.. */
680 if (req->rq_level < LUSTRE_CONN_FULL || req->rq_no_recov ||
681 imp->imp_obd->obd_no_recov)
684 ptlrpc_fail_import(imp, req->rq_import_generation);
689 static int expired_set(void *data)
691 struct ptlrpc_request_set *set = data;
692 struct list_head *tmp;
693 time_t now = LTIME_S (CURRENT_TIME);
696 LASSERT (set != NULL);
697 CERROR("EXPIRED SET %p\n", set);
699 /* A timeout expired; see which reqs it applies to... */
700 list_for_each (tmp, &set->set_requests) {
701 struct ptlrpc_request *req =
702 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
704 /* request in-flight? */
705 if (!((req->rq_phase == RQ_PHASE_RPC && !req->rq_waiting) ||
706 (req->rq_phase == RQ_PHASE_BULK)))
709 if (req->rq_timedout || /* already dealt with */
710 req->rq_sent + req->rq_timeout > now) /* not expired */
713 /* deal with this guy */
714 expire_one_request (req);
717 /* When waiting for a whole set, we always to break out of the
718 * sleep so we can recalculate the timeout, or enable interrupts
719 * iff everyone's timed out.
724 static void interrupted_set(void *data)
726 struct ptlrpc_request_set *set = data;
727 struct list_head *tmp;
730 LASSERT (set != NULL);
731 CERROR("INTERRUPTED SET %p\n", set);
733 list_for_each(tmp, &set->set_requests) {
734 struct ptlrpc_request *req =
735 list_entry(tmp, struct ptlrpc_request, rq_set_chain);
737 if (req->rq_phase != RQ_PHASE_RPC)
740 spin_lock_irqsave (&req->rq_lock, flags);
742 spin_unlock_irqrestore (&req->rq_lock, flags);
746 int ptlrpc_set_wait(struct ptlrpc_request_set *set)
748 struct list_head *tmp;
749 struct obd_import *imp;
750 struct ptlrpc_request *req;
751 struct l_wait_info lwi;
759 list_for_each(tmp, &set->set_requests) {
760 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
762 LASSERT (req->rq_level == LUSTRE_CONN_FULL);
763 LASSERT (req->rq_phase == RQ_PHASE_NEW);
764 req->rq_phase = RQ_PHASE_RPC;
766 imp = req->rq_import;
767 spin_lock_irqsave(&imp->imp_lock, flags);
769 if (imp->imp_invalid) {
770 spin_unlock_irqrestore(&imp->imp_lock, flags);
771 req->rq_status = -EIO;
772 req->rq_phase = RQ_PHASE_INTERPRET;
776 if (req->rq_level > imp->imp_level) {
777 if (req->rq_no_recov || imp->imp_obd->obd_no_recov ||
779 spin_unlock_irqrestore(&imp->imp_lock, flags);
780 req->rq_status = -EWOULDBLOCK;
781 req->rq_phase = RQ_PHASE_INTERPRET;
785 spin_lock (&req->rq_lock);
787 spin_unlock (&req->rq_lock);
788 LASSERT (list_empty (&req->rq_list));
789 // list_del(&req->rq_list);
790 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
791 spin_unlock_irqrestore(&imp->imp_lock, flags);
795 /* XXX this is the same as ptlrpc_queue_wait */
796 LASSERT(list_empty(&req->rq_list));
797 list_add_tail(&req->rq_list, &imp->imp_sending_list);
798 req->rq_import_generation = imp->imp_generation;
799 spin_unlock_irqrestore(&imp->imp_lock, flags);
801 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:ni:nid:opc"
802 " %s:%s:%d:"LPU64":%s:"LPX64":%d\n", current->comm,
803 imp->imp_obd->obd_uuid.uuid, req->rq_reqmsg->status,
805 imp->imp_connection->c_peer.peer_ni->pni_name,
806 imp->imp_connection->c_peer.peer_nid,
807 req->rq_reqmsg->opc);
809 rc = ptl_send_rpc(req);
812 req->rq_phase = RQ_PHASE_INTERPRET;
817 now = LTIME_S (CURRENT_TIME);
819 list_for_each (tmp, &set->set_requests) {
820 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
822 /* request in-flight? */
823 if (!((req->rq_phase == RQ_PHASE_RPC &&
825 (req->rq_phase == RQ_PHASE_BULK)))
828 if (req->rq_timedout) /* already timed out */
831 deadline = req->rq_sent + req->rq_timeout;
832 if (deadline <= now) /* actually expired already */
833 timeout = 1; /* ASAP */
834 else if (timeout == 0 || timeout > deadline - now)
835 timeout = deadline - now;
838 /* wait until all complete, interrupted, or an in-flight
840 CDEBUG(D_HA, "set %p going to sleep for %d seconds\n",
842 lwi = LWI_TIMEOUT_INTR(timeout * HZ,
843 expired_set, interrupted_set, set);
844 rc = l_wait_event(set->set_waitq, check_set(set), &lwi);
846 LASSERT (rc == 0 || rc == -EINTR || rc == -ETIMEDOUT);
848 /* -EINTR => all requests have been flagged rq_intr so next
850 * -ETIMEOUTD => someone timed out. When all reqs have
851 * timed out, signals are enabled allowing completion with
853 * I don't really care if we go once more round the loop in
854 * the error cases -eeb. */
857 LASSERT (set->set_remaining == 0);
860 list_for_each(tmp, &set->set_requests) {
861 req = list_entry(tmp, struct ptlrpc_request, rq_set_chain);
863 LASSERT (req->rq_phase == RQ_PHASE_COMPLETE);
864 if (req->rq_status != 0)
868 if (set->set_interpret != NULL) {
869 int (*interpreter)(struct ptlrpc_request_set *set, void *, int) =
871 rc = interpreter (set, &set->set_args, rc);
877 static void __ptlrpc_free_req(struct ptlrpc_request *request, int locked)
880 if (request == NULL) {
885 LASSERT (!request->rq_receiving_reply);
887 /* We must take it off the imp_replay_list first. Otherwise, we'll set
888 * request->rq_reqmsg to NULL while osc_close is dereferencing it. */
889 if (request->rq_import != NULL) {
890 unsigned long flags = 0;
892 spin_lock_irqsave(&request->rq_import->imp_lock, flags);
893 list_del_init(&request->rq_list);
895 spin_unlock_irqrestore(&request->rq_import->imp_lock,
899 if (atomic_read(&request->rq_refcount) != 0) {
900 DEBUG_REQ(D_ERROR, request,
901 "freeing request with nonzero refcount");
905 if (request->rq_repmsg != NULL) {
906 OBD_FREE(request->rq_repmsg, request->rq_replen);
907 request->rq_repmsg = NULL;
909 if (request->rq_reqmsg != NULL) {
910 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
911 request->rq_reqmsg = NULL;
913 if (request->rq_export != NULL) {
914 class_export_put(request->rq_export);
915 request->rq_export = NULL;
917 if (request->rq_import != NULL) {
918 class_import_put(request->rq_import);
919 request->rq_import = NULL;
921 if (request->rq_bulk != NULL)
922 ptlrpc_free_bulk(request->rq_bulk);
924 ptlrpc_put_connection(request->rq_connection);
925 OBD_FREE(request, sizeof(*request));
929 void ptlrpc_free_req(struct ptlrpc_request *request)
931 __ptlrpc_free_req(request, 0);
934 static int __ptlrpc_req_finished(struct ptlrpc_request *request, int locked)
940 if (request == (void *)(long)(0x5a5a5a5a5a5a5a5a)) {
941 CERROR("dereferencing freed request (bug 575)\n");
946 DEBUG_REQ(D_INFO, request, "refcount now %u",
947 atomic_read(&request->rq_refcount) - 1);
949 if (atomic_dec_and_test(&request->rq_refcount)) {
950 __ptlrpc_free_req(request, locked);
957 void ptlrpc_req_finished(struct ptlrpc_request *request)
959 __ptlrpc_req_finished(request, 0);
962 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
964 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
965 request->rq_reqmsg = NULL;
966 request->rq_reqlen = 0;
969 /* Disengage the client's reply buffer from the network
970 * NB does _NOT_ unregister any client-side bulk.
971 * IDEMPOTENT, but _not_ safe against concurrent callers.
972 * The request owner (i.e. the thread doing the I/O) must call...
974 void ptlrpc_unregister_reply (struct ptlrpc_request *request)
980 LASSERT (!in_interrupt ()); /* might sleep */
982 spin_lock_irqsave (&request->rq_lock, flags);
983 if (!request->rq_receiving_reply) { /* not waiting for a reply */
984 spin_unlock_irqrestore (&request->rq_lock, flags);
986 /* NB reply buffer not freed here */
990 LASSERT (!request->rq_replied); /* callback hasn't completed */
991 spin_unlock_irqrestore (&request->rq_lock, flags);
993 rc = PtlMDUnlink (request->rq_reply_md_h);
998 case PTL_OK: /* unlinked before completion */
999 LASSERT (request->rq_receiving_reply);
1000 LASSERT (!request->rq_replied);
1001 spin_lock_irqsave (&request->rq_lock, flags);
1002 request->rq_receiving_reply = 0;
1003 spin_unlock_irqrestore (&request->rq_lock, flags);
1004 OBD_FREE(request->rq_repmsg, request->rq_replen);
1005 request->rq_repmsg = NULL;
1009 case PTL_MD_INUSE: /* callback in progress */
1011 /* Network access will complete in finite time but
1012 * the timeout lets us CERROR for visibility */
1013 struct l_wait_info lwi = LWI_TIMEOUT (10 * HZ, NULL, NULL);
1015 rc = l_wait_event (request->rq_wait_for_rep,
1016 request->rq_replied, &lwi);
1017 LASSERT (rc == 0 || rc == -ETIMEDOUT);
1019 spin_lock_irqsave (&request->rq_lock, flags);
1020 /* Ensure the callback has completed scheduling me
1021 * and taken its hands off the request */
1022 spin_unlock_irqrestore (&request->rq_lock, flags);
1026 CERROR ("Unexpectedly long timeout: req %p\n", request);
1030 case PTL_INV_MD: /* callback completed */
1031 LASSERT (!request->rq_receiving_reply);
1032 LASSERT (request->rq_replied);
1039 /* caller must hold imp->imp_lock */
1040 void ptlrpc_free_committed(struct obd_import *imp)
1042 struct list_head *tmp, *saved;
1043 struct ptlrpc_request *req;
1044 struct ptlrpc_request *last_req = NULL; /* temporary fire escape */
1047 LASSERT(imp != NULL);
1050 LASSERT(spin_is_locked(&imp->imp_lock));
1053 CDEBUG(D_HA, "%s: committing for last_committed "LPU64"\n",
1054 imp->imp_obd->obd_name, imp->imp_peer_committed_transno);
1056 list_for_each_safe(tmp, saved, &imp->imp_replay_list) {
1057 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1059 /* XXX ok to remove when 1357 resolved - rread 05/29/03 */
1060 LASSERT (req != last_req);
1063 if (req->rq_replay) {
1064 DEBUG_REQ(D_HA, req, "keeping (FL_REPLAY)");
1068 /* not yet committed */
1069 if (req->rq_transno > imp->imp_peer_committed_transno) {
1070 DEBUG_REQ(D_HA, req, "stopping search");
1074 DEBUG_REQ(D_HA, req, "committing (last_committed "LPU64")",
1075 imp->imp_peer_committed_transno);
1076 list_del_init(&req->rq_list);
1077 __ptlrpc_req_finished(req, 1);
1084 void ptlrpc_cleanup_client(struct obd_import *imp)
1091 void ptlrpc_resend_req(struct ptlrpc_request *req)
1093 unsigned long flags;
1095 DEBUG_REQ(D_HA, req, "resending");
1096 req->rq_reqmsg->handle.cookie = 0;
1097 ptlrpc_put_connection(req->rq_connection);
1098 req->rq_connection =
1099 ptlrpc_connection_addref(req->rq_import->imp_connection);
1100 req->rq_status = -EAGAIN;
1102 spin_lock_irqsave (&req->rq_lock, flags);
1104 req->rq_timedout = 0;
1105 if (req->rq_set != NULL)
1106 wake_up (&req->rq_set->set_waitq);
1108 wake_up(&req->rq_wait_for_rep);
1109 spin_unlock_irqrestore (&req->rq_lock, flags);
1112 /* XXX: this function and rq_status are currently unused */
1113 void ptlrpc_restart_req(struct ptlrpc_request *req)
1115 unsigned long flags;
1117 DEBUG_REQ(D_HA, req, "restarting (possibly-)completed request");
1118 req->rq_status = -ERESTARTSYS;
1120 spin_lock_irqsave (&req->rq_lock, flags);
1121 req->rq_restart = 1;
1122 req->rq_timedout = 0;
1123 if (req->rq_set != NULL)
1124 wake_up (&req->rq_set->set_waitq);
1126 wake_up(&req->rq_wait_for_rep);
1127 spin_unlock_irqrestore (&req->rq_lock, flags);
1130 static int expired_request(void *data)
1132 struct ptlrpc_request *req = data;
1135 RETURN(expire_one_request(req));
1138 static void interrupted_request(void *data)
1140 unsigned long flags;
1142 struct ptlrpc_request *req = data;
1143 DEBUG_REQ(D_HA, req, "request interrupted");
1144 spin_lock_irqsave (&req->rq_lock, flags);
1146 spin_unlock_irqrestore (&req->rq_lock, flags);
1149 struct ptlrpc_request *ptlrpc_request_addref(struct ptlrpc_request *req)
1152 atomic_inc(&req->rq_refcount);
1156 void ptlrpc_retain_replayable_request(struct ptlrpc_request *req,
1157 struct obd_import *imp)
1159 struct list_head *tmp;
1162 LASSERT(spin_is_locked(&imp->imp_lock));
1165 LASSERT(imp->imp_replayable);
1166 /* Balanced in ptlrpc_free_committed, usually. */
1167 ptlrpc_request_addref(req);
1168 list_for_each_prev(tmp, &imp->imp_replay_list) {
1169 struct ptlrpc_request *iter =
1170 list_entry(tmp, struct ptlrpc_request, rq_list);
1172 /* We may have duplicate transnos if we create and then
1173 * open a file, or for closes retained if to match creating
1174 * opens, so use req->rq_xid as a secondary key.
1175 * (See bugs 684, 685, and 428.)
1176 * XXX no longer needed, but all opens need transnos!
1178 if (iter->rq_transno > req->rq_transno)
1181 if (iter->rq_transno == req->rq_transno) {
1182 LASSERT(iter->rq_xid != req->rq_xid);
1183 if (iter->rq_xid > req->rq_xid)
1187 list_add(&req->rq_list, &iter->rq_list);
1191 list_add_tail(&req->rq_list, &imp->imp_replay_list);
1194 int ptlrpc_queue_wait(struct ptlrpc_request *req)
1198 struct l_wait_info lwi;
1199 struct obd_import *imp = req->rq_import;
1200 struct obd_device *obd = imp->imp_obd;
1201 struct ptlrpc_connection *conn = imp->imp_connection;
1207 LASSERT (req->rq_set == NULL);
1208 LASSERT (!req->rq_receiving_reply);
1210 /* for distributed debugging */
1211 req->rq_reqmsg->status = current->pid;
1212 LASSERT(imp->imp_obd != NULL);
1213 CDEBUG(D_RPCTRACE, "Sending RPC pname:cluuid:pid:xid:ni:nid:opc "
1214 "%s:%s:%d:"LPU64":%s:"LPX64":%d\n", current->comm,
1215 imp->imp_obd->obd_uuid.uuid,
1216 req->rq_reqmsg->status, req->rq_xid,
1217 conn->c_peer.peer_ni->pni_name, conn->c_peer.peer_nid,
1218 req->rq_reqmsg->opc);
1220 /* Mark phase here for a little debug help */
1221 req->rq_phase = RQ_PHASE_RPC;
1225 * If the import has been invalidated (such as by an OST failure), the
1226 * request must fail with -EIO. Recovery requests are allowed to go
1227 * through, though, so that they have a chance to revalidate the
1230 spin_lock_irqsave(&imp->imp_lock, flags);
1231 if (req->rq_import->imp_invalid && req->rq_level == LUSTRE_CONN_FULL) {
1232 DEBUG_REQ(D_ERROR, req, "IMP_INVALID:");
1233 spin_unlock_irqrestore(&imp->imp_lock, flags);
1234 GOTO (out, rc = -EIO);
1237 if (req->rq_level > imp->imp_level) {
1238 list_del(&req->rq_list);
1239 if (req->rq_no_recov || obd->obd_no_recov ||
1240 imp->imp_dlm_fake) {
1241 spin_unlock_irqrestore(&imp->imp_lock, flags);
1242 GOTO (out, rc = -EWOULDBLOCK);
1245 list_add_tail(&req->rq_list, &imp->imp_delayed_list);
1246 spin_unlock_irqrestore(&imp->imp_lock, flags);
1248 DEBUG_REQ(D_HA, req, "\"%s\" waiting for recovery: (%d > %d)",
1249 current->comm, req->rq_level, imp->imp_level);
1250 lwi = LWI_INTR(NULL, NULL);
1251 rc = l_wait_event(req->rq_wait_for_rep,
1252 (req->rq_level <= imp->imp_level ||
1255 DEBUG_REQ(D_HA, req, "\"%s\" awake: (%d > %d)",
1256 current->comm, req->rq_level, imp->imp_level);
1258 spin_lock_irqsave(&imp->imp_lock, flags);
1259 list_del_init(&req->rq_list);
1265 spin_unlock_irqrestore(&imp->imp_lock, flags);
1269 CERROR("process %d resumed\n", current->pid);
1272 /* XXX this is the same as ptlrpc_set_wait */
1273 LASSERT(list_empty(&req->rq_list));
1274 list_add_tail(&req->rq_list, &imp->imp_sending_list);
1275 req->rq_import_generation = imp->imp_generation;
1276 spin_unlock_irqrestore(&imp->imp_lock, flags);
1278 rc = ptl_send_rpc(req);
1280 /* The DLM's fake imports want to avoid all forms of
1282 if (imp->imp_dlm_fake) {
1283 spin_lock_irqsave(&imp->imp_lock, flags);
1284 list_del_init(&req->rq_list);
1285 spin_unlock_irqrestore(&imp->imp_lock, flags);
1289 DEBUG_REQ(D_ERROR, req, "send failed (%d); recovering", rc);
1291 ptlrpc_fail_import(imp, req->rq_import_generation);
1293 /* If we've been told to not wait, we're done. */
1294 if (req->rq_level < LUSTRE_CONN_FULL || req->rq_no_recov ||
1295 obd->obd_no_recov) {
1296 spin_lock_irqsave(&imp->imp_lock, flags);
1297 list_del_init(&req->rq_list);
1298 spin_unlock_irqrestore(&imp->imp_lock, flags);
1302 /* If we errored, allow the user to interrupt immediately */
1305 timeout = req->rq_timeout * HZ;
1306 DEBUG_REQ(D_NET, req, "-- sleeping");
1309 lwi = LWI_TIMEOUT_INTR(timeout, expired_request, interrupted_request,
1311 l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
1314 extern int reply_in_callback(ptl_event_t *ev);
1315 ptl_event_t reply_ev;
1316 PtlEQWait(req->rq_connection->c_peer.peer_ni->pni_reply_in_eq_h,
1318 reply_in_callback(&reply_ev);
1320 LASSERT (reply_ev.mem_desc.user_ptr == (void *)req);
1321 // ptlrpc_check_reply(req);
1322 // not required now it only tests
1326 DEBUG_REQ(D_NET, req, "-- done sleeping");
1328 CDEBUG(D_RPCTRACE, "Completed RPC pname:cluuid:pid:xid:ni:nid:opc "
1329 "%s:%s:%d:"LPU64":%s:"LPX64":%d\n", current->comm,
1330 imp->imp_obd->obd_uuid.uuid,
1331 req->rq_reqmsg->status, req->rq_xid,
1332 conn->c_peer.peer_ni->pni_name, conn->c_peer.peer_nid,
1333 req->rq_reqmsg->opc);
1335 spin_lock_irqsave(&imp->imp_lock, flags);
1336 list_del_init(&req->rq_list);
1337 spin_unlock_irqrestore(&imp->imp_lock, flags);
1339 /* If the reply was received normally, this just grabs the spinlock
1340 * (ensuring the reply callback has returned), sees that
1341 * req->rq_receiving_reply is clear and returns. */
1342 ptlrpc_unregister_reply (req);
1345 GOTO(out, rc = -EIO);
1347 /* Resend if we need to, unless we were interrupted. */
1348 if (req->rq_resend && !req->rq_intr) {
1349 /* ...unless we were specifically told otherwise. */
1350 if (req->rq_no_resend) {
1351 spin_lock_irqsave (&req->rq_lock, flags);
1352 req->rq_no_resend = 0;
1353 spin_unlock_irqrestore (&req->rq_lock, flags);
1354 GOTO(out, rc = -ETIMEDOUT);
1356 spin_lock_irqsave (&req->rq_lock, flags);
1358 spin_unlock_irqrestore (&req->rq_lock, flags);
1359 lustre_msg_add_flags(req->rq_reqmsg, MSG_RESENT);
1361 if (req->rq_bulk != NULL)
1362 ptlrpc_unregister_bulk (req);
1364 DEBUG_REQ(D_HA, req, "resending: ");
1369 /* Should only be interrupted if we timed out. */
1370 if (!req->rq_timedout)
1371 DEBUG_REQ(D_ERROR, req,
1372 "rq_intr set but rq_timedout not");
1373 GOTO(out, rc = -EINTR);
1376 if (req->rq_timedout) { /* non-recoverable timeout */
1377 GOTO(out, rc = -ETIMEDOUT);
1380 if (!req->rq_replied) {
1381 /* How can this be? -eeb */
1382 DEBUG_REQ(D_ERROR, req, "!rq_replied: ");
1384 GOTO(out, rc = req->rq_status);
1387 rc = after_reply (req, &do_restart);
1388 /* NB may return +ve success rc */
1390 if (req->rq_bulk != NULL)
1391 ptlrpc_unregister_bulk (req);
1392 DEBUG_REQ(D_HA, req, "resending: ");
1397 if (req->rq_bulk != NULL) {
1398 if (rc >= 0) { /* success so far */
1399 lwi = LWI_TIMEOUT (timeout, NULL, NULL);
1400 brc = l_wait_event (req->rq_wait_for_rep,
1401 ptlrpc_bulk_complete (req->rq_bulk), &lwi);
1403 LASSERT (brc == -ETIMEDOUT);
1404 CERROR ("Timed out waiting for bulk\n");
1409 /* MDS blocks for put ACKs before replying */
1410 /* OSC sets rq_no_resend for the time being */
1411 LASSERT (req->rq_no_resend);
1412 ptlrpc_unregister_bulk (req);
1416 LASSERT (!req->rq_receiving_reply);
1417 req->rq_phase = RQ_PHASE_INTERPRET;
1421 int ptlrpc_replay_req(struct ptlrpc_request *req)
1423 int rc = 0, old_level, old_status = 0;
1424 // struct ptlrpc_client *cli = req->rq_import->imp_client;
1425 struct l_wait_info lwi;
1428 /* I don't touch rq_phase here, so the debug log can show what
1429 * state it was left in */
1431 /* Not handling automatic bulk replay yet (or ever?) */
1432 LASSERT (req->rq_bulk == NULL);
1434 DEBUG_REQ(D_NET, req, "about to replay");
1436 /* Update request's state, since we might have a new connection. */
1437 ptlrpc_put_connection(req->rq_connection);
1438 req->rq_connection =
1439 ptlrpc_connection_addref(req->rq_import->imp_connection);
1441 /* temporarily set request to RECOVD level (reset at out:) */
1442 old_level = req->rq_level;
1443 if (req->rq_replied)
1444 old_status = req->rq_repmsg->status;
1445 req->rq_level = LUSTRE_CONN_RECOVD;
1446 rc = ptl_send_rpc(req);
1448 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
1449 ptlrpc_cleanup_request_buf(req);
1450 // up(&cli->cli_rpc_sem);
1451 GOTO(out, rc = -rc);
1454 CDEBUG(D_OTHER, "-- sleeping\n");
1455 lwi = LWI_INTR(NULL, NULL); /* XXX needs timeout, nested recovery */
1456 l_wait_event(req->rq_wait_for_rep, ptlrpc_check_reply(req), &lwi);
1457 CDEBUG(D_OTHER, "-- done\n");
1459 // up(&cli->cli_rpc_sem);
1461 /* If the reply was received normally, this just grabs the spinlock
1462 * (ensuring the reply callback has returned), sees that
1463 * req->rq_receiving_reply is clear and returns. */
1464 ptlrpc_unregister_reply (req);
1466 if (!req->rq_replied) {
1467 CERROR("Unknown reason for wakeup\n");
1468 /* XXX Phil - I end up here when I kill obdctl */
1469 /* ...that's because signals aren't all masked in
1470 * l_wait_event() -eeb */
1471 GOTO(out, rc = -EINTR);
1475 /* Clear reply swab mask; this is a new reply in sender's byte order */
1476 req->rq_rep_swab_mask = 0;
1478 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
1480 CERROR("unpack_rep failed: %d\n", rc);
1481 GOTO(out, rc = -EPROTO);
1484 /* FIXME: Enable when BlueArc makes new release */
1485 if (req->rq_repmsg->type != PTL_RPC_MSG_REPLY &&
1486 req->rq_repmsg->type != PTL_RPC_MSG_ERR) {
1487 CERROR("invalid packet type received (type=%u)\n",
1488 req->rq_repmsg->type);
1489 GOTO(out, rc = -EPROTO);
1493 /* The transno had better not change over replay. */
1494 LASSERT(req->rq_reqmsg->transno == req->rq_repmsg->transno);
1496 CDEBUG(D_NET, "got rep "LPD64"\n", req->rq_xid);
1498 /* let the callback do fixups, possibly including in the request */
1499 if (req->rq_replay_cb)
1500 req->rq_replay_cb(req);
1502 if (req->rq_replied && req->rq_repmsg->status != old_status) {
1503 DEBUG_REQ(D_HA, req, "status %d, old was %d",
1504 req->rq_repmsg->status, old_status);
1508 req->rq_level = old_level;
1512 void ptlrpc_abort_inflight(struct obd_import *imp)
1514 unsigned long flags;
1515 struct list_head *tmp, *n;
1518 /* Make sure that no new requests get processed for this import.
1519 * ptlrpc_{queue,set}_wait must (and does) hold imp_lock while testing
1520 * this flag and then putting requests on sending_list or delayed_list.
1522 spin_lock_irqsave(&imp->imp_lock, flags);
1523 if (!imp->imp_replayable)
1524 /* on b_devel, I moved this line to
1525 ptlrpc_set_import_active because I thought it made
1526 more sense there and possibly not all callers of
1527 this function expect this. I'll leave it here until
1528 I can figure out if it's correct or not. - rread 5/12/03 */
1529 imp->imp_invalid = 1;
1531 /* XXX locking? Maybe we should remove each request with the list
1532 * locked? Also, how do we know if the requests on the list are
1533 * being freed at this time?
1535 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
1536 struct ptlrpc_request *req =
1537 list_entry(tmp, struct ptlrpc_request, rq_list);
1539 DEBUG_REQ(D_HA, req, "inflight");
1541 spin_lock (&req->rq_lock);
1543 if (req->rq_set != NULL)
1544 wake_up(&req->rq_set->set_waitq);
1546 wake_up(&req->rq_wait_for_rep);
1547 spin_unlock (&req->rq_lock);
1550 list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
1551 struct ptlrpc_request *req =
1552 list_entry(tmp, struct ptlrpc_request, rq_list);
1554 DEBUG_REQ(D_HA, req, "aborting waiting req");
1556 spin_lock (&req->rq_lock);
1558 if (req->rq_set != NULL)
1559 wake_up(&req->rq_set->set_waitq);
1561 wake_up(&req->rq_wait_for_rep);
1562 spin_unlock (&req->rq_lock);
1565 /* Last chance to free reqs left on the replay list, but we
1566 * will still leak reqs that haven't comitted. */
1567 if (imp->imp_replayable)
1568 ptlrpc_free_committed(imp);
1570 spin_unlock_irqrestore(&imp->imp_lock, flags);
1575 static __u64 ptlrpc_last_xid = 0;
1576 static spinlock_t ptlrpc_last_xid_lock = SPIN_LOCK_UNLOCKED;
1578 __u64 ptlrpc_next_xid(void)
1581 spin_lock(&ptlrpc_last_xid_lock);
1582 tmp = ++ptlrpc_last_xid;
1583 spin_unlock(&ptlrpc_last_xid_lock);