xref: /onnv-gate/usr/src/cmd/lvm/rpc.mdcommd/mdmn_commd_server.c (revision 12812:3e6822aec2be)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52150Sjeanm  * Common Development and Distribution License (the "License").
62150Sjeanm  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
218452SJohn.Wren.Kennedy@Sun.COM 
220Sstevel@tonic-gate /*
23*12812Sjames.hall@oracle.com  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <unistd.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/stat.h>
290Sstevel@tonic-gate #include <sys/statvfs.h>
300Sstevel@tonic-gate #include <sys/uadmin.h>
312150Sjeanm #include <sys/resource.h>
320Sstevel@tonic-gate #include <fcntl.h>
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <thread.h>
350Sstevel@tonic-gate #include <meta.h>
360Sstevel@tonic-gate #include <sdssc.h>
370Sstevel@tonic-gate #include <mdmn_changelog.h>
380Sstevel@tonic-gate #include "mdmn_subr.h"
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * This is the communication daemon for SVM Multi Node Disksets.
420Sstevel@tonic-gate  * It runs on every node and provides the following rpc services:
438452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_send_svc_2
448452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_work_svc_2
458452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_wakeup_initiator_svc_2
468452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_wakeup_master_svc_2
478452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_comm_lock_svc_2
488452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_comm_unlock_svc_2
498452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_comm_suspend_svc_2
508452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_comm_resume_svc_2
518452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_comm_reinit_set_svc_2
520Sstevel@tonic-gate  * where send, lock, unlock and reinit are meant for external use,
530Sstevel@tonic-gate  * work and the two wakeups are for internal use only.
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  * NOTE:
568452SJohn.Wren.Kennedy@Sun.COM  * On every node only one of those xxx_2 functions can be active at the
570Sstevel@tonic-gate  * same time because the daemon is single threaded.
580Sstevel@tonic-gate  *
598452SJohn.Wren.Kennedy@Sun.COM  * (not quite true, as mdmn_send_svc_2 and mdmn_work_svc_2 do thr_create()s
608452SJohn.Wren.Kennedy@Sun.COM  * as part of their handlers, so those aspects are multi-threaded)
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  * In case an event occurs that has to be propagated to all the nodes...
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  * One node (the initiator)
650Sstevel@tonic-gate  *	calls the libmeta function mdmn_send_message()
668452SJohn.Wren.Kennedy@Sun.COM  *	This function calls the local daemon thru mdmn_send_svc_2.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * On the initiator:
698452SJohn.Wren.Kennedy@Sun.COM  *	mdmn_send_svc_2()
700Sstevel@tonic-gate  *	    - starts a thread -> mdmn_send_to_work() and returns.
710Sstevel@tonic-gate  *	mdmn_send_to_work()
720Sstevel@tonic-gate  *	    - sends this message over to the master of the diskset.
738452SJohn.Wren.Kennedy@Sun.COM  *	      This is done by calling mdmn_work_svc_2 on the master.
740Sstevel@tonic-gate  *	    - registers to the initiator_table
750Sstevel@tonic-gate  *	    - exits without doing a svc_sendreply() for the call to
768452SJohn.Wren.Kennedy@Sun.COM  *	      mdmn_send_svc_2. This means that call is blocked until somebody
770Sstevel@tonic-gate  *	      (see end of this comment) does a svc_sendreply().
780Sstevel@tonic-gate  *	      This means mdmn_send_message() does not yet return.
790Sstevel@tonic-gate  *	    - A timeout surveillance is started at this point.
800Sstevel@tonic-gate  *	      This means in case the master doesn't reply at all in an
810Sstevel@tonic-gate  *	      aproppriate time, an error condition is returned
820Sstevel@tonic-gate  *	      to the caller.
830Sstevel@tonic-gate  *
840Sstevel@tonic-gate  * On the master:
858452SJohn.Wren.Kennedy@Sun.COM  *	mdmn_work_svc_2()
860Sstevel@tonic-gate  *	    - starts a thread -> mdmn_master_process_msg() and returns
870Sstevel@tonic-gate  *	mdmn_master_process_msg()
880Sstevel@tonic-gate  *	    - logs the message to the change log
890Sstevel@tonic-gate  *	    - executes the message locally
900Sstevel@tonic-gate  *	    - flags the message in the change log
918452SJohn.Wren.Kennedy@Sun.COM  *	    - sends the message to mdmn_work_svc_2() on all the
920Sstevel@tonic-gate  *	      other nodes (slaves)
938452SJohn.Wren.Kennedy@Sun.COM  *	      after each call to mdmn_work_svc_2 the thread goes to sleep and
948452SJohn.Wren.Kennedy@Sun.COM  *	      will be woken up by mdmn_wakeup_master_svc_2() as soon as the
950Sstevel@tonic-gate  *	      slave node is done with this message.
960Sstevel@tonic-gate  *	    - In case the slave doesn't respond in a apropriate time, an error
970Sstevel@tonic-gate  *	      is assumed to ensure the master doesn't wait forever.
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  * On a slave:
1008452SJohn.Wren.Kennedy@Sun.COM  *	mdmn_work_svc_2()
1010Sstevel@tonic-gate  *	    - starts a thread -> mdmn_slave_process_msg() and returns
1020Sstevel@tonic-gate  *	mdmn_slave_process_msg()
1030Sstevel@tonic-gate  *	    - processes this message locally by calling the appropriate message
1040Sstevel@tonic-gate  *	      handler, that creates some result.
1058452SJohn.Wren.Kennedy@Sun.COM  *	    - sends that result thru a call to mdmn_wakeup_master_svc_2() to
1060Sstevel@tonic-gate  *	      the master.
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  * Back on the master:
1098452SJohn.Wren.Kennedy@Sun.COM  *	mdmn_wakeup_master_svc_2()
1100Sstevel@tonic-gate  *	    - stores the result into the master_table.
1110Sstevel@tonic-gate  *	    - signals the mdmn_master_process_msg-thread.
1120Sstevel@tonic-gate  *	    - returns
1130Sstevel@tonic-gate  *	mdmn_master_process_msg()
1140Sstevel@tonic-gate  *	    - after getting the results from all nodes
1150Sstevel@tonic-gate  *	    - sends them back to the initiating node thru a call to
1168452SJohn.Wren.Kennedy@Sun.COM  *	      mdmn_wakeup_initiator_svc_2.
1170Sstevel@tonic-gate  *
1180Sstevel@tonic-gate  * Back on the initiator:
1198452SJohn.Wren.Kennedy@Sun.COM  *	mdmn_wakeup_initiator_svc_2()
1208452SJohn.Wren.Kennedy@Sun.COM  *	    - calls svc_sendreply() which makes the call to mdmn_send_svc_2()
1210Sstevel@tonic-gate  *	      return.
1220Sstevel@tonic-gate  *	      which allows the initial mdmn_send_message() call to return.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate FILE *commdout;		/* debug output for the commd */
1260Sstevel@tonic-gate char *commdoutfile;	/* file name for the above output */
1270Sstevel@tonic-gate /* want at least 10 MB free space when logging into a file */
1280Sstevel@tonic-gate #define	MIN_FS_SPACE	(10LL * 1024 * 1024)
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate  * Number of outstanding messages that were initiated by this node.
1320Sstevel@tonic-gate  * If zero, check_timeouts goes to sleep
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate uint_t	messages_on_their_way;
1350Sstevel@tonic-gate mutex_t	check_timeout_mutex;	/* need mutex to protect above */
1360Sstevel@tonic-gate cond_t	check_timeout_cv;	/* trigger for check_timeouts */
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /* for printing out time stamps */
1390Sstevel@tonic-gate hrtime_t __savetime;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /* RPC clients for every set and every node and their protecting locks */
1420Sstevel@tonic-gate CLIENT	*client[MD_MAXSETS][NNODES];
1430Sstevel@tonic-gate rwlock_t client_rwlock[MD_MAXSETS];
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /* the descriptors of all possible sets and their protectors */
1460Sstevel@tonic-gate struct md_set_desc *set_descriptor[MD_MAXSETS];
1470Sstevel@tonic-gate rwlock_t set_desc_rwlock[MD_MAXSETS];
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /* the daemon to daemon communication has to timeout quickly */
1500Sstevel@tonic-gate static struct timeval FOUR_SECS = { 4, 0 };
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /* These indicate if a set has already been setup */
1530Sstevel@tonic-gate int md_mn_set_inited[MD_MAXSETS];
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /* For every set we have a message completion table and protecting mutexes */
1560Sstevel@tonic-gate md_mn_mct_t *mct[MD_MAXSETS];
1570Sstevel@tonic-gate mutex_t	mct_mutex[MD_MAXSETS][MD_MN_NCLASSES];
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate /* Stuff to describe the global status of the commd on one node */
1600Sstevel@tonic-gate #define	MD_CGS_INITED		0x0001
1610Sstevel@tonic-gate #define	MD_CGS_ABORTED		0x0002	/* return everything with MDMNE_ABORT */
1620Sstevel@tonic-gate uint_t md_commd_global_state = 0;	/* No state when starting up */
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate  * Global verbosity level for the daemon
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate uint_t md_commd_global_verb;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * libmeta doesn't like multiple threads in metaget_setdesc().
1710Sstevel@tonic-gate  * So we must protect access to it with a global lock
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate mutex_t get_setdesc_mutex;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate  * Need a way to block single message types,
1770Sstevel@tonic-gate  * hence an array with a status for every message type
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate uint_t msgtype_lock_state[MD_MN_NMESSAGES];
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /* for reading in the config file */
1820Sstevel@tonic-gate #define	MAX_LINE_SIZE 1024
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate extern char *commd_get_outfile(void);
1850Sstevel@tonic-gate extern uint_t commd_get_verbosity(void);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate  * mdmn_clnt_create is a helper function for meta_client_create_retry.  It
1890Sstevel@tonic-gate  * merely needs to call clnt_create_timed, and meta_client_create_retry
1900Sstevel@tonic-gate  * will take care of the rest.
1910Sstevel@tonic-gate  */
1920Sstevel@tonic-gate /* ARGSUSED */
1930Sstevel@tonic-gate static CLIENT *
mdmn_clnt_create(char * ignore,void * data,struct timeval * time_out)1940Sstevel@tonic-gate mdmn_clnt_create(char *ignore, void *data, struct timeval *time_out)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	md_mnnode_desc	*node = (md_mnnode_desc *)data;
1970Sstevel@tonic-gate 
1988452SJohn.Wren.Kennedy@Sun.COM 	return (clnt_create_timed(node->nd_priv_ic, MDMN_COMMD, TWO, "tcp",
1998452SJohn.Wren.Kennedy@Sun.COM 	    time_out));
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate #define	FLUSH_DEBUGFILE() \
2030Sstevel@tonic-gate 	if (commdout != (FILE *)NULL) { \
20411053SSurya.Prakki@Sun.COM 		(void) fflush(commdout); \
20511053SSurya.Prakki@Sun.COM 		(void) fsync(fileno(commdout)); \
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate static void
panic_system(int nid,md_mn_msgtype_t type,int master_err,int master_exitval,md_mn_result_t * slave_result)2090Sstevel@tonic-gate panic_system(int nid, md_mn_msgtype_t type, int master_err, int master_exitval,
2100Sstevel@tonic-gate     md_mn_result_t *slave_result)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	md_mn_commd_err_t	commd_err;
2130Sstevel@tonic-gate 	md_error_t		mne = mdnullerror;
2140Sstevel@tonic-gate 	char			*msg_buf;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	msg_buf = (char *)calloc(MAXPATHLEN + 1, sizeof (char));
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	FLUSH_DEBUGFILE();
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (master_err != MDMNE_ACK) {
22111053SSurya.Prakki@Sun.COM 		(void) snprintf(msg_buf, MAXPATHLEN, "rpc.mdcommd: RPC "
22211053SSurya.Prakki@Sun.COM 		    "fail on master when processing message type %d\n", type);
2230Sstevel@tonic-gate 	} else if (slave_result == NULL) {
22411053SSurya.Prakki@Sun.COM 		(void) snprintf(msg_buf, MAXPATHLEN, "rpc.mdcommd: RPC fail "
22511053SSurya.Prakki@Sun.COM 		    "on node %d when processing message type %d\n", nid, type);
2260Sstevel@tonic-gate 	} else {
22711053SSurya.Prakki@Sun.COM 		(void) snprintf(msg_buf, MAXPATHLEN, "rpc.mdcommd: "
22811053SSurya.Prakki@Sun.COM 		    "Inconsistent return value from node %d when processing "
22911053SSurya.Prakki@Sun.COM 		    "message type %d. Master exitval = %d, "
23011053SSurya.Prakki@Sun.COM 		    "Slave exitval = %d\n", nid, type, master_exitval,
23111053SSurya.Prakki@Sun.COM 		    slave_result->mmr_exitval);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 	commd_err.size = strlen(msg_buf);
23462Sjeanm 	commd_err.md_message = (uint64_t)(uintptr_t)&msg_buf[0];
2350Sstevel@tonic-gate 
23611053SSurya.Prakki@Sun.COM 	(void) metaioctl(MD_MN_COMMD_ERR, &commd_err, &mne, "rpc.mdcommd");
2370Sstevel@tonic-gate 	(void) uadmin(A_DUMP, AD_BOOT, NULL);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate static void
flush_fcout()2410Sstevel@tonic-gate flush_fcout()
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	struct statvfs64 vfsbuf;
2440Sstevel@tonic-gate 	long long avail_bytes;
2450Sstevel@tonic-gate 	int warned = 0;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	for (; ; ) {
24811053SSurya.Prakki@Sun.COM 		(void) sleep(10);
2490Sstevel@tonic-gate 		/* No output file, nothing to do */
2500Sstevel@tonic-gate 		if (commdout == (FILE *)NULL)
2510Sstevel@tonic-gate 			continue;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		/*
2540Sstevel@tonic-gate 		 * stat the appropriate filesystem to check for available space.
2550Sstevel@tonic-gate 		 */
2560Sstevel@tonic-gate 		if (statvfs64(commdoutfile, &vfsbuf)) {
2570Sstevel@tonic-gate 			continue;
2580Sstevel@tonic-gate 		}
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 		avail_bytes = vfsbuf.f_frsize * vfsbuf.f_bavail;
2610Sstevel@tonic-gate 		/*
2620Sstevel@tonic-gate 		 * If we don't have enough space, we print out a warning.
2630Sstevel@tonic-gate 		 * And we drop the verbosity level to NULL
2640Sstevel@tonic-gate 		 * In case the condtion doesn't go away, we don't repeat
2650Sstevel@tonic-gate 		 * the warning.
2660Sstevel@tonic-gate 		 */
2670Sstevel@tonic-gate 		if (avail_bytes < MIN_FS_SPACE) {
2680Sstevel@tonic-gate 			if (warned) {
2690Sstevel@tonic-gate 				continue;
2700Sstevel@tonic-gate 			}
2710Sstevel@tonic-gate 			commd_debug(MD_MMV_SYSLOG,
2720Sstevel@tonic-gate 			    "NOT enough space available for logging\n");
2730Sstevel@tonic-gate 			commd_debug(MD_MMV_SYSLOG,
2740Sstevel@tonic-gate 			    "Have %lld bytes, need %lld bytes\n",
2750Sstevel@tonic-gate 			    avail_bytes, MIN_FS_SPACE);
2760Sstevel@tonic-gate 			warned = 1;
2770Sstevel@tonic-gate 			md_commd_global_verb = MD_MMV_NULL;
2780Sstevel@tonic-gate 		} else {
2790Sstevel@tonic-gate 			warned = 0;
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 
28211053SSurya.Prakki@Sun.COM 		(void) fflush(commdout);
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /* safer version of clnt_destroy. If clnt is NULL don't do anything */
2870Sstevel@tonic-gate #define	mdmn_clnt_destroy(clnt) {	\
2880Sstevel@tonic-gate 	if (clnt)			\
2890Sstevel@tonic-gate 		clnt_destroy(clnt);	\
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate  * Own version of svc_sendreply that checks the integrity of the transport
2940Sstevel@tonic-gate  * handle and so prevents us from core dumps in the real svc_sendreply()
2950Sstevel@tonic-gate  */
2960Sstevel@tonic-gate void
mdmn_svc_sendreply(SVCXPRT * transp,xdrproc_t xdr,caddr_t data)2970Sstevel@tonic-gate mdmn_svc_sendreply(SVCXPRT *transp, xdrproc_t xdr, caddr_t data)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	if (SVC_STAT(transp) == XPRT_DIED) {
3000Sstevel@tonic-gate 		commd_debug(MD_MMV_MISC,
3010Sstevel@tonic-gate 		    "mdmn_svc_sendreply: XPRT_DIED\n");
3020Sstevel@tonic-gate 		return;
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 	(void) svc_sendreply(transp, xdr, data);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate  * timeout_initiator(set, class)
3090Sstevel@tonic-gate  *
3100Sstevel@tonic-gate  * Alas, I sent a message and didn't get a response back in aproppriate time.
3110Sstevel@tonic-gate  *
3120Sstevel@tonic-gate  * timeout_initiator() takes care for doing the needed svc_sendreply() to the
3130Sstevel@tonic-gate  * calling mdmn_send_message, so that guy doesn't wait forever
3140Sstevel@tonic-gate  * What is done here is pretty much the same as what is done in
3150Sstevel@tonic-gate  * wakeup initiator. The difference is that we cannot provide for any results,
3160Sstevel@tonic-gate  * of course and we set the comm_state to MDMNE_TIMEOUT.
3170Sstevel@tonic-gate  *
3180Sstevel@tonic-gate  * By doing so, mdmn_send_message can decide if a retry would make sense or not.
3190Sstevel@tonic-gate  * It's not our's to decide that here.
3200Sstevel@tonic-gate  */
3210Sstevel@tonic-gate void
timeout_initiator(set_t setno,md_mn_msgclass_t class)3220Sstevel@tonic-gate timeout_initiator(set_t setno, md_mn_msgclass_t class)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	SVCXPRT		*transp;
3250Sstevel@tonic-gate 	md_mn_msgid_t	mid;
3260Sstevel@tonic-gate 	md_mn_result_t *resultp;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	resultp = Zalloc(sizeof (md_mn_result_t));
3290Sstevel@tonic-gate 	resultp->mmr_comm_state	= MDMNE_TIMEOUT;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	commd_debug(MD_MMV_MISC,
3320Sstevel@tonic-gate 	    "timeout_initiator set = %d, class = %d\n", setno, class);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	transp = mdmn_get_initiator_table_transp(setno, class);
3350Sstevel@tonic-gate 	mdmn_get_initiator_table_id(setno, class, &mid);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	commd_debug(MD_MMV_MISC, "timeout_ini: (%d, 0x%llx-%d)\n",
3380Sstevel@tonic-gate 	    MSGID_ELEMS(mid));
3398452SJohn.Wren.Kennedy@Sun.COM 	/*
3408452SJohn.Wren.Kennedy@Sun.COM 	 * Give the result the corresponding msgid from the failed message.
3418452SJohn.Wren.Kennedy@Sun.COM 	 */
3428452SJohn.Wren.Kennedy@Sun.COM 	MSGID_COPY(&mid, &(resultp->mmr_msgid));
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/* return to mdmn_send_message() and let it deal with the situation */
3450Sstevel@tonic-gate 	mdmn_svc_sendreply(transp, xdr_md_mn_result_t, (char *)resultp);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	free(resultp);
3480Sstevel@tonic-gate 	commd_debug(MD_MMV_MISC, "timeout_ini: sendreplied\n");
3498452SJohn.Wren.Kennedy@Sun.COM 	svc_done(transp);
3500Sstevel@tonic-gate 	mdmn_unregister_initiator_table(setno, class);
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * check_timeouts - thread
3560Sstevel@tonic-gate  *
3570Sstevel@tonic-gate  * This implements a timeout surveillance for messages sent from the
3580Sstevel@tonic-gate  * initiator to the master.
3590Sstevel@tonic-gate  *
3600Sstevel@tonic-gate  * If a message is started, this thread is triggered thru
3610Sstevel@tonic-gate  * cond_signal(&check_timeout_cv) and we keep track of the numbers of
3620Sstevel@tonic-gate  * messages that are outstanding (messages_on_their_way).
3630Sstevel@tonic-gate  *
3640Sstevel@tonic-gate  * As long as there are messages on their way, this thread never goes to sleep.
3650Sstevel@tonic-gate  * It'll keep checking all class/set combinations for outstanding messages.
3660Sstevel@tonic-gate  * If one is found, it's checked if this message is overdue. In that case,
3670Sstevel@tonic-gate  * timeout_initiator() is called to wakeup the calling mdmn_send_message and
3680Sstevel@tonic-gate  * to clean up the mess.
3690Sstevel@tonic-gate  *
3700Sstevel@tonic-gate  * If the result from the master arrives later, this message is considered
3710Sstevel@tonic-gate  * to be unsolicited. And will be ignored.
3720Sstevel@tonic-gate  */
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate void
check_timeouts()3750Sstevel@tonic-gate check_timeouts()
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	set_t			setno;
3780Sstevel@tonic-gate 	time_t			now, then;
3790Sstevel@tonic-gate 	mutex_t			*mx;
3800Sstevel@tonic-gate 	md_mn_msgclass_t	class;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	for (; ; ) {
3830Sstevel@tonic-gate 		now = time((time_t *)NULL);
3840Sstevel@tonic-gate 		for (setno = 1; setno < MD_MAXSETS; setno++) {
3850Sstevel@tonic-gate 			if (md_mn_set_inited[setno] != MDMN_SET_READY) {
3860Sstevel@tonic-gate 				continue;
3870Sstevel@tonic-gate 			}
3880Sstevel@tonic-gate 			for (class = MD_MSG_CLASS1; class < MD_MN_NCLASSES;
3890Sstevel@tonic-gate 			    class++) {
3900Sstevel@tonic-gate 				mx = mdmn_get_initiator_table_mx(setno, class);
39111053SSurya.Prakki@Sun.COM 				(void) mutex_lock(mx);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 				/* then is the registered time */
3940Sstevel@tonic-gate 				then =
3950Sstevel@tonic-gate 				    mdmn_get_initiator_table_time(setno, class);
3960Sstevel@tonic-gate 				if ((then != 0) && (now > then)) {
3970Sstevel@tonic-gate 					timeout_initiator(setno, class);
3980Sstevel@tonic-gate 				}
39911053SSurya.Prakki@Sun.COM 				(void) mutex_unlock(mx);
4000Sstevel@tonic-gate 			}
4010Sstevel@tonic-gate 		}
4020Sstevel@tonic-gate 		/* it's ok to check only once per second */
40311053SSurya.Prakki@Sun.COM 		(void) sleep(1);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 		/* is there work to do? */
40611053SSurya.Prakki@Sun.COM 		(void) mutex_lock(&check_timeout_mutex);
4070Sstevel@tonic-gate 		if (messages_on_their_way == 0) {
40811053SSurya.Prakki@Sun.COM 			(void) cond_wait(&check_timeout_cv,
40911053SSurya.Prakki@Sun.COM 			    &check_timeout_mutex);
4100Sstevel@tonic-gate 		}
41111053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&check_timeout_mutex);
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate void
setup_debug(void)4160Sstevel@tonic-gate setup_debug(void)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	char	*tmp_dir;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/* Read in the debug-controlling tokens from runtime.cf */
4210Sstevel@tonic-gate 	md_commd_global_verb = commd_get_verbosity();
4220Sstevel@tonic-gate 	/*
4230Sstevel@tonic-gate 	 * If the user didn't specify a verbosity level in runtime.cf
4240Sstevel@tonic-gate 	 * we can safely return here. As we don't intend to printout
4250Sstevel@tonic-gate 	 * debug messages, we don't need to check for the output file.
4260Sstevel@tonic-gate 	 */
4270Sstevel@tonic-gate 	if (md_commd_global_verb == 0) {
4280Sstevel@tonic-gate 		return;
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/* if commdout is non-NULL it is an open FILE, we'd better close it */
4320Sstevel@tonic-gate 	if (commdout != (FILE *)NULL) {
43311053SSurya.Prakki@Sun.COM 		(void) fclose(commdout);
4340Sstevel@tonic-gate 	}
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	commdoutfile = commd_get_outfile();
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	/* setup the debug output */
4390Sstevel@tonic-gate 	if (commdoutfile == (char *)NULL) {
4400Sstevel@tonic-gate 		/* if no valid file was specified, use the default */
4410Sstevel@tonic-gate 		commdoutfile = "/var/run/commd.out";
4420Sstevel@tonic-gate 		commdout = fopen(commdoutfile, "a");
4430Sstevel@tonic-gate 	} else {
4440Sstevel@tonic-gate 		/* check if the directory exists and is writable */
4450Sstevel@tonic-gate 		tmp_dir = strdup(commdoutfile);
4460Sstevel@tonic-gate 		if ((access(dirname(tmp_dir), X_OK|W_OK)) ||
4470Sstevel@tonic-gate 		    ((commdout = fopen(commdoutfile, "a")) == (FILE *)NULL)) {
4480Sstevel@tonic-gate 			syslog(LOG_ERR,
4490Sstevel@tonic-gate 			    "Can't write to specified output file %s,\n"
4500Sstevel@tonic-gate 			    "using /var/run/commd.out instead\n", commdoutfile);
4510Sstevel@tonic-gate 			free(commdoutfile);
4520Sstevel@tonic-gate 			commdoutfile = "/var/run/commd.out";
4530Sstevel@tonic-gate 			commdout = fopen(commdoutfile, "a");
4540Sstevel@tonic-gate 		}
4550Sstevel@tonic-gate 		free(tmp_dir);
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if (commdout == (FILE *)NULL) {
4590Sstevel@tonic-gate 		syslog(LOG_ERR, "Can't write to debug output file %s\n",
4600Sstevel@tonic-gate 		    commdoutfile);
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate }
463393Sskamm 
464393Sskamm /*
465393Sskamm  * mdmn_is_node_dead checks to see if a node is dead using
466393Sskamm  * the SunCluster infrastructure which is a stable interface.
467393Sskamm  * If unable to contact SunCuster the node is assumed to be alive.
468393Sskamm  * Return values:
469393Sskamm  *	1 - node is dead
470393Sskamm  *	0 - node is alive
471393Sskamm  */
472393Sskamm int
mdmn_is_node_dead(md_mnnode_desc * node)473393Sskamm mdmn_is_node_dead(md_mnnode_desc *node)
474393Sskamm {
475393Sskamm 	char	*fmt = "/usr/cluster/bin/scha_cluster_get -O NODESTATE_NODE ";
476393Sskamm 	char	*cmd;
477393Sskamm 	size_t	size;
478393Sskamm 	char	buf[10];
479393Sskamm 	FILE	*ptr;
480393Sskamm 	int	retval = 0;
481393Sskamm 
482393Sskamm 	/* I know that I'm alive */
483393Sskamm 	if (strcmp(node->nd_nodename, mynode()) == 0)
484393Sskamm 		return (retval);
485393Sskamm 
486393Sskamm 	size = strlen(fmt) + strlen(node->nd_nodename) + 1;
487393Sskamm 	cmd = Zalloc(size);
488393Sskamm 	(void) strlcat(cmd, fmt, size);
489393Sskamm 	(void) strlcat(cmd, node->nd_nodename, size);
490393Sskamm 
491393Sskamm 	if ((ptr = popen(cmd, "r")) != NULL) {
492393Sskamm 		if (fgets(buf, sizeof (buf), ptr) != NULL) {
493393Sskamm 			/* If scha_cluster_get returned DOWN - return dead */
494393Sskamm 			if (strncmp(buf, "DOWN", 4) == 0)
495393Sskamm 				retval = 1;
496393Sskamm 		}
497393Sskamm 		(void) pclose(ptr);
498393Sskamm 	}
499393Sskamm 	Free(cmd);
500393Sskamm 	return (retval);
501393Sskamm }
502393Sskamm 
5030Sstevel@tonic-gate /*
5040Sstevel@tonic-gate  * global_init()
5050Sstevel@tonic-gate  *
5060Sstevel@tonic-gate  * Perform some global initializations.
5070Sstevel@tonic-gate  *
5080Sstevel@tonic-gate  * the following routines have to call this before operation can start:
5098452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_send_svc_2
5108452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_work_svc_2
5118452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_comm_lock_svc_2
5128452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_comm_unlock_svc_2
5138452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_comm_suspend_svc_2
5148452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_comm_resume_svc_2
5158452SJohn.Wren.Kennedy@Sun.COM  *  - mdmn_comm_reinit_set_svc_2
5160Sstevel@tonic-gate  *
5170Sstevel@tonic-gate  * This is a single threaded daemon, so it can only be in one of the above
5180Sstevel@tonic-gate  * routines at the same time.
5190Sstevel@tonic-gate  * This means, global_init() cannot be called more than once at the same time.
5200Sstevel@tonic-gate  * Hence, no lock is needed.
5210Sstevel@tonic-gate  */
5220Sstevel@tonic-gate void
global_init(void)5230Sstevel@tonic-gate global_init(void)
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate 	set_t			set;
5260Sstevel@tonic-gate 	md_mn_msgclass_t	class;
5270Sstevel@tonic-gate 	struct sigaction	sighandler;
5280Sstevel@tonic-gate 	time_t			clock_val;
5292150Sjeanm 	struct rlimit		commd_limit;
5302150Sjeanm 
5312150Sjeanm 
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	/* Do these global initializations only once */
5340Sstevel@tonic-gate 	if (md_commd_global_state & MD_CGS_INITED) {
5350Sstevel@tonic-gate 		return;
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 	(void) sdssc_bind_library();
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/* setup the debug options from the config file */
5400Sstevel@tonic-gate 	setup_debug();
5410Sstevel@tonic-gate 
5422150Sjeanm 	/* make sure that we don't run out of file descriptors */
5432150Sjeanm 	commd_limit.rlim_cur = commd_limit.rlim_max = RLIM_INFINITY;
5442150Sjeanm 	if (setrlimit(RLIMIT_NOFILE, &commd_limit) != 0) {
5452150Sjeanm 		syslog(LOG_WARNING, gettext("setrlimit failed."
5462150Sjeanm 		    "Could not increase the max file descriptors"));
5472150Sjeanm 	}
5482150Sjeanm 
5490Sstevel@tonic-gate 	/* Make setup_debug() be the action in case of SIGHUP */
5500Sstevel@tonic-gate 	sighandler.sa_flags = 0;
55111053SSurya.Prakki@Sun.COM 	(void) sigfillset(&sighandler.sa_mask);
5520Sstevel@tonic-gate 	sighandler.sa_handler = (void (*)(int)) setup_debug;
55311387SSurya.Prakki@Sun.COM 	(void) sigaction(SIGHUP, &sighandler, NULL);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	__savetime = gethrtime();
5560Sstevel@tonic-gate 	(void) time(&clock_val);
5578452SJohn.Wren.Kennedy@Sun.COM 	commd_debug(MD_MMV_MISC, "global init called %s\n", ctime(&clock_val));
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	/* start a thread that flushes out the debug on a regular basis */
56011053SSurya.Prakki@Sun.COM 	(void) thr_create(NULL, 0, (void *(*)(void *))flush_fcout,
5610Sstevel@tonic-gate 	    (void *) NULL, THR_DETACHED, NULL);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	/* global rwlock's / mutex's / cond_t's go here */
56411053SSurya.Prakki@Sun.COM 	(void) mutex_init(&check_timeout_mutex, USYNC_THREAD, NULL);
56511053SSurya.Prakki@Sun.COM 	(void) cond_init(&check_timeout_cv, USYNC_THREAD, NULL);
56611053SSurya.Prakki@Sun.COM 	(void) mutex_init(&get_setdesc_mutex, USYNC_THREAD, NULL);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	/* Make sure the initiator table is initialized correctly */
5690Sstevel@tonic-gate 	for (set = 0; set < MD_MAXSETS; set++) {
5700Sstevel@tonic-gate 		for (class = 0; class < MD_MN_NCLASSES; class++) {
5710Sstevel@tonic-gate 			mdmn_unregister_initiator_table(set, class);
5720Sstevel@tonic-gate 		}
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/* setup the check for timeouts */
57711053SSurya.Prakki@Sun.COM 	(void) thr_create(NULL, 0, (void *(*)(void *))check_timeouts,
5780Sstevel@tonic-gate 	    (void *) NULL, THR_DETACHED, NULL);
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	md_commd_global_state |= MD_CGS_INITED;
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate /*
5850Sstevel@tonic-gate  * mdmn_init_client(setno, nodeid)
5860Sstevel@tonic-gate  * called if client[setno][nodeid] is NULL
5870Sstevel@tonic-gate  *
5880Sstevel@tonic-gate  * NOTE: Must be called with set_desc_rwlock held as a reader
5890Sstevel@tonic-gate  * NOTE: Must be called with client_rwlock held as a writer
5900Sstevel@tonic-gate  *
5910Sstevel@tonic-gate  * If the rpc client for this node has not been setup for any set, we do it now.
5920Sstevel@tonic-gate  *
5930Sstevel@tonic-gate  * Returns	0 on success (node found in set, rpc client setup)
5940Sstevel@tonic-gate  *		-1 if metaget_setdesc failed,
5950Sstevel@tonic-gate  *		-2 if node not part of set
5960Sstevel@tonic-gate  *		-3 if clnt_create fails
5970Sstevel@tonic-gate  */
5980Sstevel@tonic-gate static int
mdmn_init_client(set_t setno,md_mn_nodeid_t nid)5990Sstevel@tonic-gate mdmn_init_client(set_t setno, md_mn_nodeid_t nid)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate 	md_error_t	ep = mdnullerror;
6020Sstevel@tonic-gate 	md_mnnode_desc	*node;
6030Sstevel@tonic-gate 	md_set_desc	*sd;	/* just an abbr for set_descriptor[setno] */
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	sd = set_descriptor[setno];
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	/*
6080Sstevel@tonic-gate 	 * Is the appropriate set_descriptor already initialized ?
6090Sstevel@tonic-gate 	 * Can't think of a scenario where this is not the case, but we'd better
6100Sstevel@tonic-gate 	 * check for it anyway.
6110Sstevel@tonic-gate 	 */
6120Sstevel@tonic-gate 	if (sd == NULL) {
6130Sstevel@tonic-gate 		mdsetname_t	*sp;
6140Sstevel@tonic-gate 
61511053SSurya.Prakki@Sun.COM 		/* readlock -> writelock */
61611053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&set_desc_rwlock[setno]);
61711053SSurya.Prakki@Sun.COM 		(void) rw_wrlock(&set_desc_rwlock[setno]);
6180Sstevel@tonic-gate 		sp = metasetnosetname(setno, &ep);
6190Sstevel@tonic-gate 		/* Only one thread is supposed to be in metaget_setdesc() */
62011053SSurya.Prakki@Sun.COM 		(void) mutex_lock(&get_setdesc_mutex);
6210Sstevel@tonic-gate 		sd = metaget_setdesc(sp, &ep);
62211053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&get_setdesc_mutex);
6230Sstevel@tonic-gate 		if (sd == NULL) {
62411053SSurya.Prakki@Sun.COM 			/* back to ... */
62511053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&set_desc_rwlock[setno]);
62611053SSurya.Prakki@Sun.COM 			/* ... readlock */
62711053SSurya.Prakki@Sun.COM 			(void) rw_rdlock(&set_desc_rwlock[setno]);
6280Sstevel@tonic-gate 			return (-1);
6290Sstevel@tonic-gate 		}
6300Sstevel@tonic-gate 		set_descriptor[setno] = sd;
63111053SSurya.Prakki@Sun.COM 		/* back to readlock */
63211053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&set_desc_rwlock[setno]);
63311053SSurya.Prakki@Sun.COM 		(void) rw_rdlock(&set_desc_rwlock[setno]);
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/* first we have to find the node name for this node id */
6370Sstevel@tonic-gate 	for (node = sd->sd_nodelist; node; node = node->nd_next) {
6380Sstevel@tonic-gate 		if (node->nd_nodeid == nid)
6390Sstevel@tonic-gate 			break; /* we found our node in this set */
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	if (node == (md_mnnode_desc *)NULL) {
6440Sstevel@tonic-gate 		commd_debug(MD_MMV_SYSLOG,
6450Sstevel@tonic-gate 		    "FATAL: node %d not found in set %d\n", nid, setno);
64611053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&set_desc_rwlock[setno]);
6470Sstevel@tonic-gate 		return (-2);
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	commd_debug(MD_MMV_INIT, "init: %s has the flags: 0x%x\n",
6510Sstevel@tonic-gate 	    node->nd_nodename ? node->nd_nodename : "NULL", node->nd_flags);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/* Did this node join the diskset?  */
6540Sstevel@tonic-gate 	if ((node->nd_flags & MD_MN_NODE_OWN) == 0) {
6550Sstevel@tonic-gate 		commd_debug(MD_MMV_INIT, "init: %s didn't join set %d\n",
6560Sstevel@tonic-gate 		    node->nd_nodename ? node->nd_nodename : "NULL", setno);
65711053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&set_desc_rwlock[setno]);
6580Sstevel@tonic-gate 		return (-2);
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	/* if clnt_create has not been done for that node, do it now */
6620Sstevel@tonic-gate 	if (client[setno][nid] == (CLIENT *) NULL) {
663393Sskamm 		time_t	tout = 0;
664393Sskamm 
665393Sskamm 		/*
666393Sskamm 		 * While trying to create a connection to a node,
667393Sskamm 		 * periodically check to see if the node has been marked
668393Sskamm 		 * dead by the SunCluster infrastructure.
669393Sskamm 		 * This periodic check is needed since a non-responsive
670393Sskamm 		 * rpc.mdcommd (while it is attempting to create a connection
671393Sskamm 		 * to a dead node) can lead to large delays and/or failures
672393Sskamm 		 * in the reconfig steps.
673393Sskamm 		 */
674393Sskamm 		while ((client[setno][nid] == (CLIENT *) NULL) &&
675393Sskamm 		    (tout < MD_CLNT_CREATE_TOUT)) {
6768452SJohn.Wren.Kennedy@Sun.COM 			client[setno][nid] = meta_client_create_retry(
6778452SJohn.Wren.Kennedy@Sun.COM 			    node->nd_nodename, mdmn_clnt_create,
6788452SJohn.Wren.Kennedy@Sun.COM 			    (void *) node, MD_CLNT_CREATE_SUBTIMEOUT, &ep);
679393Sskamm 			/* Is the node dead? */
680393Sskamm 			if (mdmn_is_node_dead(node) == 1) {
681393Sskamm 				commd_debug(MD_MMV_SYSLOG,
682393Sskamm 				    "rpc.mdcommd: no client for dead node %s\n",
683393Sskamm 				    node->nd_nodename);
684393Sskamm 				break;
685393Sskamm 			} else
686393Sskamm 				tout += MD_CLNT_CREATE_SUBTIMEOUT;
687393Sskamm 		}
688393Sskamm 
6890Sstevel@tonic-gate 		if (client[setno][nid] == (CLIENT *) NULL) {
6900Sstevel@tonic-gate 			clnt_pcreateerror(node->nd_nodename);
69111053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&set_desc_rwlock[setno]);
6920Sstevel@tonic-gate 			return (-3);
6930Sstevel@tonic-gate 		}
6940Sstevel@tonic-gate 		/* this node has the license to send */
6950Sstevel@tonic-gate 		commd_debug(MD_MMV_MISC, "init_client: calling add_lic\n");
6960Sstevel@tonic-gate 		add_license(node);
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 		/* set the timeout value */
6990Sstevel@tonic-gate 		clnt_control(client[setno][nid], CLSET_TIMEOUT,
7000Sstevel@tonic-gate 		    (char *)&FOUR_SECS);
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	}
70311053SSurya.Prakki@Sun.COM 	(void) rw_unlock(&set_desc_rwlock[setno]);
7040Sstevel@tonic-gate 	return (0);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate /*
7080Sstevel@tonic-gate  * check_client(setno, nodeid)
7090Sstevel@tonic-gate  *
7100Sstevel@tonic-gate  * must be called with reader lock held for set_desc_rwlock[setno]
7110Sstevel@tonic-gate  * and must be called with reader lock held for client_rwlock[setno]
7120Sstevel@tonic-gate  * Checks if the client for this set/node combination is already setup
7130Sstevel@tonic-gate  * if not it upgrades the lock to a writer lock
7140Sstevel@tonic-gate  * and tries to initialize the client.
7150Sstevel@tonic-gate  * Finally it's checked if the client nulled out again due to some race
7160Sstevel@tonic-gate  *
7170Sstevel@tonic-gate  * returns 0 if there is a usable client
7180Sstevel@tonic-gate  * returns MDMNE_RPC_FAIL otherwise
7190Sstevel@tonic-gate  */
7200Sstevel@tonic-gate static int
check_client(set_t setno,md_mn_nodeid_t nodeid)7210Sstevel@tonic-gate check_client(set_t setno, md_mn_nodeid_t nodeid)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	int ret = 0;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	while ((client[setno][nodeid] == (CLIENT *)NULL) && (ret == 0)) {
72611053SSurya.Prakki@Sun.COM 		/* upgrade reader ... */
72711053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&client_rwlock[setno]);
72811053SSurya.Prakki@Sun.COM 		/* ... to writer lock. */
72911053SSurya.Prakki@Sun.COM 		(void) rw_wrlock(&client_rwlock[setno]);
7300Sstevel@tonic-gate 		if (mdmn_init_client(setno, nodeid) != 0) {
7310Sstevel@tonic-gate 			ret = MDMNE_RPC_FAIL;
7320Sstevel@tonic-gate 		}
73311053SSurya.Prakki@Sun.COM 		/* downgrade writer ... */
73411053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&client_rwlock[setno]);
73511053SSurya.Prakki@Sun.COM 		/* ... back to reader lock. */
73611053SSurya.Prakki@Sun.COM 		(void) rw_rdlock(&client_rwlock[setno]);
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate 	return (ret);
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate /*
7420Sstevel@tonic-gate  * mdmn_init_set(setno, todo)
7430Sstevel@tonic-gate  * setno is the number of the set to be initialized.
7440Sstevel@tonic-gate  * todo is one of the MDMN_SET_* thingies or MDMN_SET_READY
7450Sstevel@tonic-gate  * If called with MDMN_SET_READY everything is initialized.
7460Sstevel@tonic-gate  *
7470Sstevel@tonic-gate  * If the set mutexes are already initialized, the caller has to hold
7480Sstevel@tonic-gate  * both set_desc_rwlock[setno] and client_rwlock[setno] as a writer, before
7490Sstevel@tonic-gate  * calling mdmn_init_set()
7500Sstevel@tonic-gate  */
7510Sstevel@tonic-gate int
mdmn_init_set(set_t setno,int todo)7520Sstevel@tonic-gate mdmn_init_set(set_t setno, int todo)
7530Sstevel@tonic-gate {
7540Sstevel@tonic-gate 	int class;
7550Sstevel@tonic-gate 	md_mnnode_desc	*node;
7560Sstevel@tonic-gate 	md_set_desc	*sd; /* just an abbr for set_descriptor[setno] */
7570Sstevel@tonic-gate 	mdsetname_t	*sp;
7580Sstevel@tonic-gate 	md_error_t	ep = mdnullerror;
7590Sstevel@tonic-gate 	md_mn_nodeid_t	nid;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	/*
7620Sstevel@tonic-gate 	 * Check if we are told to setup the mutexes and
7630Sstevel@tonic-gate 	 * if these are not yet setup
7640Sstevel@tonic-gate 	 */
7650Sstevel@tonic-gate 	if ((todo & MDMN_SET_MUTEXES) &&
7660Sstevel@tonic-gate 	    ((md_mn_set_inited[setno] & MDMN_SET_MUTEXES) == 0)) {
76711053SSurya.Prakki@Sun.COM 		(void) mutex_init(&mdmn_busy_mutex[setno], USYNC_THREAD, NULL);
76811053SSurya.Prakki@Sun.COM 		(void) cond_init(&mdmn_busy_cv[setno], USYNC_THREAD, NULL);
76911053SSurya.Prakki@Sun.COM 		(void) rwlock_init(&client_rwlock[setno], USYNC_THREAD, NULL);
77011053SSurya.Prakki@Sun.COM 		(void) rwlock_init(&set_desc_rwlock[setno], USYNC_THREAD, NULL);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 		for (class = MD_MSG_CLASS1; class < MD_MN_NCLASSES; class++) {
77311053SSurya.Prakki@Sun.COM 			(void) mutex_init(mdmn_get_master_table_mx(setno,
77411053SSurya.Prakki@Sun.COM 			    class), USYNC_THREAD, NULL);
77511053SSurya.Prakki@Sun.COM 			(void) cond_init(mdmn_get_master_table_cv(setno, class),
7760Sstevel@tonic-gate 			    USYNC_THREAD, NULL);
77711053SSurya.Prakki@Sun.COM 			(void) mutex_init(mdmn_get_initiator_table_mx(setno,
77811053SSurya.Prakki@Sun.COM 			    class), USYNC_THREAD, NULL);
7790Sstevel@tonic-gate 		}
7800Sstevel@tonic-gate 		md_mn_set_inited[setno] |= MDMN_SET_MUTEXES;
7810Sstevel@tonic-gate 	}
7820Sstevel@tonic-gate 	if ((todo & MDMN_SET_MCT) &&
7830Sstevel@tonic-gate 	    ((md_mn_set_inited[setno] & MDMN_SET_MCT) == 0)) {
7840Sstevel@tonic-gate 		int	fd;
7850Sstevel@tonic-gate 		size_t	filesize;
7860Sstevel@tonic-gate 		caddr_t	addr;
7870Sstevel@tonic-gate 		char table_name[32];
788*12812Sjames.hall@oracle.com 		struct flock	fl;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 		filesize = (sizeof (md_mn_mct_t));
7910Sstevel@tonic-gate 		(void) snprintf(table_name, sizeof (table_name), "%s%d",
7920Sstevel@tonic-gate 		    MD_MN_MSG_COMP_TABLE, setno);
7930Sstevel@tonic-gate 		/*
7940Sstevel@tonic-gate 		 * If the mct file exists we map it into memory.
7950Sstevel@tonic-gate 		 * Otherwise we create an empty file of appropriate
7960Sstevel@tonic-gate 		 * size and map that into memory.
7970Sstevel@tonic-gate 		 * The mapped areas are stored in mct[setno].
7980Sstevel@tonic-gate 		 */
7990Sstevel@tonic-gate 		fd = open(table_name, O_RDWR|O_CREAT|O_DSYNC, 0600);
8000Sstevel@tonic-gate 		if (fd < 0) {
8010Sstevel@tonic-gate 			commd_debug(MD_MMV_MISC,
8020Sstevel@tonic-gate 			    "init_set: Can't open MCT\n");
8030Sstevel@tonic-gate 			return (-1);
8040Sstevel@tonic-gate 		}
8050Sstevel@tonic-gate 		/*
806*12812Sjames.hall@oracle.com 		 * Ensure that we are the only process that has this file
807*12812Sjames.hall@oracle.com 		 * mapped. If another instance of rpc.mdcommd has beaten us
808*12812Sjames.hall@oracle.com 		 * then we display the failing process and attempt to terminate
809*12812Sjames.hall@oracle.com 		 * it. The next call of this routine should establish us as
810*12812Sjames.hall@oracle.com 		 * the only rpc.mdcommd on the system.
811*12812Sjames.hall@oracle.com 		 */
812*12812Sjames.hall@oracle.com 		(void) memset(&fl, 0, sizeof (fl));
813*12812Sjames.hall@oracle.com 		fl.l_type = F_WRLCK;
814*12812Sjames.hall@oracle.com 		fl.l_whence = SEEK_SET;
815*12812Sjames.hall@oracle.com 		fl.l_start = 0;
816*12812Sjames.hall@oracle.com 		fl.l_len = filesize + 1;
817*12812Sjames.hall@oracle.com 
818*12812Sjames.hall@oracle.com 		if (fcntl(fd, F_SETLK, &fl) == -1) {
819*12812Sjames.hall@oracle.com 			commd_debug(MD_MMV_SYSLOG,
820*12812Sjames.hall@oracle.com 			    "init_set: Cannot lock MCT '%s'\n", table_name);
821*12812Sjames.hall@oracle.com 			if (fcntl(fd, F_GETLK, &fl) != -1) {
822*12812Sjames.hall@oracle.com 				commd_debug(MD_MMV_SYSLOG, "rpc.mdcommd:"
823*12812Sjames.hall@oracle.com 				    "Process %d holds lock\n", fl.l_pid);
824*12812Sjames.hall@oracle.com 				(void) close(fd);
825*12812Sjames.hall@oracle.com 			} else {
826*12812Sjames.hall@oracle.com 				commd_debug(MD_MMV_SYSLOG, "rpc.mdcommd:"
827*12812Sjames.hall@oracle.com 				    "F_GETLK failed\n");
828*12812Sjames.hall@oracle.com 				(void) close(fd);
829*12812Sjames.hall@oracle.com 				return (-1);
830*12812Sjames.hall@oracle.com 			}
831*12812Sjames.hall@oracle.com 
832*12812Sjames.hall@oracle.com 			/*
833*12812Sjames.hall@oracle.com 			 * Try to terminate other mdcommd process so that we
834*12812Sjames.hall@oracle.com 			 * can establish ourselves.
835*12812Sjames.hall@oracle.com 			 */
836*12812Sjames.hall@oracle.com 			if (sigsend(P_PID, fl.l_pid, 0) == 0) {
837*12812Sjames.hall@oracle.com 				if (sigsend(P_PID, fl.l_pid, SIGKILL) < 0) {
838*12812Sjames.hall@oracle.com 					commd_debug(MD_MMV_SYSLOG,
839*12812Sjames.hall@oracle.com 					    "rpc.mdcommd:"
840*12812Sjames.hall@oracle.com 					    "SIGKILL of %d failed\n", fl.l_pid);
841*12812Sjames.hall@oracle.com 				} else {
842*12812Sjames.hall@oracle.com 					commd_debug(MD_MMV_SYSLOG,
843*12812Sjames.hall@oracle.com 					    "rpc.mdcommd:"
844*12812Sjames.hall@oracle.com 					    "Process %d killed\n", fl.l_pid);
845*12812Sjames.hall@oracle.com 				}
846*12812Sjames.hall@oracle.com 			} else {
847*12812Sjames.hall@oracle.com 				commd_debug(MD_MMV_SYSLOG, "rpc.mdcommd:"
848*12812Sjames.hall@oracle.com 				    "Process %d not killable\n", fl.l_pid);
849*12812Sjames.hall@oracle.com 			}
850*12812Sjames.hall@oracle.com 			return (-1);
851*12812Sjames.hall@oracle.com 		}
852*12812Sjames.hall@oracle.com 		/*
8530Sstevel@tonic-gate 		 * To ensure that the file has the appropriate size,
8540Sstevel@tonic-gate 		 * we write a byte at the end of the file.
8550Sstevel@tonic-gate 		 */
85611053SSurya.Prakki@Sun.COM 		(void) lseek(fd, filesize + 1, SEEK_SET);
85711053SSurya.Prakki@Sun.COM 		(void) write(fd, "\0", 1);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 		/* at this point we have a file in place that we can mmap */
8600Sstevel@tonic-gate 		addr = mmap(0, filesize, PROT_READ | PROT_WRITE,
8610Sstevel@tonic-gate 		    MAP_SHARED, fd, (off_t)0);
8620Sstevel@tonic-gate 		if (addr == MAP_FAILED) {
8630Sstevel@tonic-gate 			commd_debug(MD_MMV_INIT,
8640Sstevel@tonic-gate 			    "init_set: mmap mct error %d\n",
8650Sstevel@tonic-gate 			    errno);
8660Sstevel@tonic-gate 			return (-1);
8670Sstevel@tonic-gate 		}
8680Sstevel@tonic-gate 		/* LINTED pointer alignment */
8690Sstevel@tonic-gate 		mct[setno] = (md_mn_mct_t *)addr;
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 		/* finally we initialize the mutexes that protect the mct */
8720Sstevel@tonic-gate 		for (class = MD_MSG_CLASS1; class < MD_MN_NCLASSES; class++) {
87311053SSurya.Prakki@Sun.COM 			(void) mutex_init(&(mct_mutex[setno][class]),
8740Sstevel@tonic-gate 			    USYNC_THREAD, NULL);
8750Sstevel@tonic-gate 		}
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 		md_mn_set_inited[setno] |= MDMN_SET_MCT;
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 	/*
8800Sstevel@tonic-gate 	 * Check if we are told to setup the nodes and
8810Sstevel@tonic-gate 	 * if these are not yet setup
8820Sstevel@tonic-gate 	 * (Attention: negative logic here compared to above!)
8830Sstevel@tonic-gate 	 */
8840Sstevel@tonic-gate 	if (((todo & MDMN_SET_NODES) == 0) ||
8850Sstevel@tonic-gate 	    (md_mn_set_inited[setno] & MDMN_SET_NODES)) {
8860Sstevel@tonic-gate 		return (0); /* success */
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	if ((sp = metasetnosetname(setno, &ep)) == NULL) {
8900Sstevel@tonic-gate 		commd_debug(MD_MMV_SYSLOG,
8910Sstevel@tonic-gate 		    "metasetnosetname(%d) returned NULL\n", setno);
8920Sstevel@tonic-gate 		return (MDMNE_NOT_JOINED);
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	/* flush local copy of rpc.metad data */
8960Sstevel@tonic-gate 	metaflushsetname(sp);
8970Sstevel@tonic-gate 
89811053SSurya.Prakki@Sun.COM 	(void) mutex_lock(&get_setdesc_mutex);
8990Sstevel@tonic-gate 	sd = metaget_setdesc(sp, &ep);
90011053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(&get_setdesc_mutex);
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	if (sd == NULL) {
9030Sstevel@tonic-gate 		commd_debug(MD_MMV_SYSLOG,
9040Sstevel@tonic-gate 		    "metaget_setdesc(%d) returned NULL\n", setno);
9050Sstevel@tonic-gate 		return (MDMNE_NOT_JOINED);
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	/*
9090Sstevel@tonic-gate 	 * if this set is not a multinode set or
9100Sstevel@tonic-gate 	 * this node didn't join yet the diskset, better don't do anything
9110Sstevel@tonic-gate 	 */
9120Sstevel@tonic-gate 	if ((MD_MNSET_DESC(sd) == 0) ||
9130Sstevel@tonic-gate 	    (sd->sd_mn_mynode->nd_flags & MD_MN_NODE_OWN) == 0) {
9140Sstevel@tonic-gate 		commd_debug(MD_MMV_INIT, "didn't yet join set %d\n", setno);
9150Sstevel@tonic-gate 		return (MDMNE_NOT_JOINED);
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	for (node = sd->sd_nodelist; node != NULL; node = node->nd_next) {
919393Sskamm 		time_t	tout = 0;
9200Sstevel@tonic-gate 		nid = node->nd_nodeid;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 		commd_debug(MD_MMV_INIT,
9230Sstevel@tonic-gate 		    "setting up: node=%s, priv_ic=%s, flags=0x%x\n",
9240Sstevel@tonic-gate 		    node->nd_nodename ? node->nd_nodename : "NULL",
9250Sstevel@tonic-gate 		    node->nd_priv_ic ? node->nd_priv_ic : "NULL",
9260Sstevel@tonic-gate 		    node->nd_flags);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 		if ((node->nd_flags & MD_MN_NODE_OWN) == 0) {
9290Sstevel@tonic-gate 			commd_debug(MD_MMV_INIT,
9300Sstevel@tonic-gate 			    "init: %s didn't join set %d\n",
9310Sstevel@tonic-gate 			    node->nd_nodename ? node->nd_nodename : "NULL",
9320Sstevel@tonic-gate 			    setno);
9330Sstevel@tonic-gate 			continue;
9340Sstevel@tonic-gate 		}
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 		if (client[setno][nid] != (CLIENT *) NULL) {
9370Sstevel@tonic-gate 			/* already inited */
9380Sstevel@tonic-gate 			commd_debug(MD_MMV_INIT, "init: already: node=%s\n",
9390Sstevel@tonic-gate 			    node->nd_nodename ? node->nd_nodename : "NULL");
9400Sstevel@tonic-gate 			continue;
9410Sstevel@tonic-gate 		}
942393Sskamm 
943393Sskamm 		/*
944393Sskamm 		 * While trying to create a connection to a node,
945393Sskamm 		 * periodically check to see if the node has been marked
946393Sskamm 		 * dead by the SunCluster infrastructure.
947393Sskamm 		 * This periodic check is needed since a non-responsive
948393Sskamm 		 * rpc.mdcommd (while it is attempting to create a connection
949393Sskamm 		 * to a dead node) can lead to large delays and/or failures
950393Sskamm 		 * in the reconfig steps.
951393Sskamm 		 */
952393Sskamm 		while ((client[setno][nid] == (CLIENT *) NULL) &&
953393Sskamm 		    (tout < MD_CLNT_CREATE_TOUT)) {
9548452SJohn.Wren.Kennedy@Sun.COM 			client[setno][nid] = meta_client_create_retry(
9558452SJohn.Wren.Kennedy@Sun.COM 			    node->nd_nodename, mdmn_clnt_create,
9568452SJohn.Wren.Kennedy@Sun.COM 			    (void *) node, MD_CLNT_CREATE_SUBTIMEOUT, &ep);
957393Sskamm 			/* Is the node dead? */
958393Sskamm 			if (mdmn_is_node_dead(node) == 1) {
959393Sskamm 				commd_debug(MD_MMV_SYSLOG,
960393Sskamm 				    "rpc.mdcommd: no client for dead node %s\n",
961393Sskamm 				    node->nd_nodename);
962393Sskamm 				break;
963393Sskamm 			} else
964393Sskamm 				tout += MD_CLNT_CREATE_SUBTIMEOUT;
965393Sskamm 		}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 		if (client[setno][nid] == (CLIENT *) NULL) {
9680Sstevel@tonic-gate 			clnt_pcreateerror(node->nd_nodename);
9690Sstevel@tonic-gate 			/*
9700Sstevel@tonic-gate 			 * If we cannot connect to a single node
9710Sstevel@tonic-gate 			 * (maybe because it is down) we mark this node as not
9720Sstevel@tonic-gate 			 * owned and continue with the next node in the list.
9730Sstevel@tonic-gate 			 * This is better than failing the entire starting up
9740Sstevel@tonic-gate 			 * of the commd system.
9750Sstevel@tonic-gate 			 */
9760Sstevel@tonic-gate 			node->nd_flags &= ~MD_MN_NODE_OWN;
9770Sstevel@tonic-gate 			commd_debug(MD_MMV_SYSLOG,
9780Sstevel@tonic-gate 			    "WARNING couldn't create client for %s\n"
9790Sstevel@tonic-gate 			    "Reconfig cycle required\n",
9800Sstevel@tonic-gate 			    node->nd_nodename);
9810Sstevel@tonic-gate 			commd_debug(MD_MMV_INIT,
9820Sstevel@tonic-gate 			    "WARNING couldn't create client for %s\n"
9830Sstevel@tonic-gate 			    "Reconfig cycle required\n",
9840Sstevel@tonic-gate 			    node->nd_nodename);
9850Sstevel@tonic-gate 			continue;
9860Sstevel@tonic-gate 		}
9870Sstevel@tonic-gate 		/* this node has the license to send */
9880Sstevel@tonic-gate 		commd_debug(MD_MMV_MISC, "init_set: calling add_lic\n");
9890Sstevel@tonic-gate 		add_license(node);
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 		/* set the timeout value */
9920Sstevel@tonic-gate 		clnt_control(client[setno][nid], CLSET_TIMEOUT,
9930Sstevel@tonic-gate 		    (char *)&FOUR_SECS);
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 		commd_debug(MD_MMV_INIT, "init: done: node=%s\n",
9960Sstevel@tonic-gate 		    node->nd_nodename ? node->nd_nodename : "NULL");
9970Sstevel@tonic-gate 	}
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	set_descriptor[setno] = sd;
10000Sstevel@tonic-gate 	md_mn_set_inited[setno] |= MDMN_SET_NODES;
10010Sstevel@tonic-gate 	return (0); /* success */
10020Sstevel@tonic-gate }
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate void *
mdmn_send_to_work(void * arg)10050Sstevel@tonic-gate mdmn_send_to_work(void *arg)
10060Sstevel@tonic-gate {
10078452SJohn.Wren.Kennedy@Sun.COM 	int			*rpc_err = NULL;
10080Sstevel@tonic-gate 	int			success;
10090Sstevel@tonic-gate 	int			try_master;
10100Sstevel@tonic-gate 	set_t			setno;
10110Sstevel@tonic-gate 	mutex_t			*mx;	/* protection for initiator_table */
10120Sstevel@tonic-gate 	SVCXPRT			*transp;
10130Sstevel@tonic-gate 	md_mn_msg_t		*msg;
10140Sstevel@tonic-gate 	md_mn_nodeid_t		set_master;
10150Sstevel@tonic-gate 	md_mn_msgclass_t	class;
10160Sstevel@tonic-gate 	md_mn_msg_and_transp_t	*matp = (md_mn_msg_and_transp_t *)arg;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	msg			= matp->mat_msg;
10190Sstevel@tonic-gate 	transp			= matp->mat_transp;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	class = mdmn_get_message_class(msg->msg_type);
10220Sstevel@tonic-gate 	setno = msg->msg_setno;
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	/* set the sender, so the master knows who to send the results */
102511053SSurya.Prakki@Sun.COM 	(void) rw_rdlock(&set_desc_rwlock[setno]);
10260Sstevel@tonic-gate 	msg->msg_sender = set_descriptor[setno]->sd_mn_mynode->nd_nodeid;
10270Sstevel@tonic-gate 	set_master	= set_descriptor[setno]->sd_mn_master_nodeid;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	mx = mdmn_get_initiator_table_mx(setno, class);
103011053SSurya.Prakki@Sun.COM 	(void) mutex_lock(mx);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	/*
10330Sstevel@tonic-gate 	 * Here we check, if the initiator table slot for this set/class
10340Sstevel@tonic-gate 	 * combination is free to use.
10350Sstevel@tonic-gate 	 * If this is not the case, we return CLASS_BUSY forcing the
10360Sstevel@tonic-gate 	 * initiating send_message call to retry
10370Sstevel@tonic-gate 	 */
10380Sstevel@tonic-gate 	success = mdmn_check_initiator_table(setno, class);
10390Sstevel@tonic-gate 	if (success == MDMNE_CLASS_BUSY) {
10400Sstevel@tonic-gate 		md_mn_msgid_t		active_mid;
10410Sstevel@tonic-gate 
10428452SJohn.Wren.Kennedy@Sun.COM 		mdmn_get_initiator_table_id(setno, class, &active_mid);
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 		commd_debug(MD_MMV_SEND,
10450Sstevel@tonic-gate 		    "send_to_work: received but locally busy "
10460Sstevel@tonic-gate 		    "(%d, 0x%llx-%d), set=%d, class=%d, type=%d, "
10470Sstevel@tonic-gate 		    "active msg=(%d, 0x%llx-%d)\n",
10480Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid), setno, class,
10490Sstevel@tonic-gate 		    msg->msg_type, MSGID_ELEMS(active_mid));
10500Sstevel@tonic-gate 	} else {
10510Sstevel@tonic-gate 		commd_debug(MD_MMV_SEND,
10520Sstevel@tonic-gate 		    "send_to_work: received (%d, 0x%llx-%d), "
10530Sstevel@tonic-gate 		    "set=%d, class=%d, type=%d\n",
10540Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid), setno, class, msg->msg_type);
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	try_master = 2; /* return failure after two retries */
10580Sstevel@tonic-gate 	while ((success == MDMNE_ACK) && (try_master--)) {
105911053SSurya.Prakki@Sun.COM 		(void) rw_rdlock(&client_rwlock[setno]);
10600Sstevel@tonic-gate 		/* is the rpc client to the master still around ? */
10610Sstevel@tonic-gate 		if (check_client(setno, set_master)) {
10620Sstevel@tonic-gate 			success = MDMNE_RPC_FAIL;
10630Sstevel@tonic-gate 			FLUSH_DEBUGFILE();
106411053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
10650Sstevel@tonic-gate 			break; /* out of try_master-loop */
10660Sstevel@tonic-gate 		}
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 		/*
10690Sstevel@tonic-gate 		 * Send the request to the work function on the master
10700Sstevel@tonic-gate 		 * this call will return immediately
10710Sstevel@tonic-gate 		 */
10728452SJohn.Wren.Kennedy@Sun.COM 		rpc_err = mdmn_work_2(msg, client[setno][set_master],
10738452SJohn.Wren.Kennedy@Sun.COM 		    set_master);
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 		/* Everything's Ok? */
10760Sstevel@tonic-gate 		if (rpc_err == NULL) {
10770Sstevel@tonic-gate 			success = MDMNE_RPC_FAIL;
10780Sstevel@tonic-gate 			/*
10790Sstevel@tonic-gate 			 * Probably something happened to the daemon on the
10800Sstevel@tonic-gate 			 * master. Kill the client, and try again...
10810Sstevel@tonic-gate 			 */
108211053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
108311053SSurya.Prakki@Sun.COM 			(void) rw_wrlock(&client_rwlock[setno]);
10840Sstevel@tonic-gate 			mdmn_clnt_destroy(client[setno][set_master]);
10850Sstevel@tonic-gate 			if (client[setno][set_master] != (CLIENT *)NULL) {
10860Sstevel@tonic-gate 				client[setno][set_master] = (CLIENT *)NULL;
10870Sstevel@tonic-gate 			}
108811053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
10890Sstevel@tonic-gate 			continue;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 		} else  if (*rpc_err != MDMNE_ACK) {
10920Sstevel@tonic-gate 			/* something went wrong, break out */
10930Sstevel@tonic-gate 			success = *rpc_err;
10940Sstevel@tonic-gate 			free(rpc_err);
109511053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
10960Sstevel@tonic-gate 			break; /* out of try_master-loop */
10970Sstevel@tonic-gate 		}
10980Sstevel@tonic-gate 
109911053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&client_rwlock[setno]);
11000Sstevel@tonic-gate 		free(rpc_err);
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 		/*
11030Sstevel@tonic-gate 		 * If we are here, we sucessfully delivered the message.
11040Sstevel@tonic-gate 		 * We register the initiator_table, so that
11058452SJohn.Wren.Kennedy@Sun.COM 		 * wakeup_initiator_2 can do the sendreply with the
11060Sstevel@tonic-gate 		 * results for us.
11070Sstevel@tonic-gate 		 */
11080Sstevel@tonic-gate 		success = MDMNE_ACK;
11090Sstevel@tonic-gate 		mdmn_register_initiator_table(setno, class, msg, transp);
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 		/* tell check_timeouts, there's work to do */
111211053SSurya.Prakki@Sun.COM 		(void) mutex_lock(&check_timeout_mutex);
11130Sstevel@tonic-gate 		messages_on_their_way++;
111411053SSurya.Prakki@Sun.COM 		(void) cond_signal(&check_timeout_cv);
111511053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&check_timeout_mutex);
11160Sstevel@tonic-gate 		break; /* out of try_master-loop */
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 
111911053SSurya.Prakki@Sun.COM 	(void) rw_unlock(&set_desc_rwlock[setno]);
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	if (success == MDMNE_ACK) {
11220Sstevel@tonic-gate 		commd_debug(MD_MMV_SEND,
11230Sstevel@tonic-gate 		    "send_to_work: registered (%d, 0x%llx-%d)\n",
11240Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid));
11250Sstevel@tonic-gate 	} else {
11260Sstevel@tonic-gate 		/* In case of failure do the sendreply now */
11270Sstevel@tonic-gate 		md_mn_result_t *resultp;
11280Sstevel@tonic-gate 		resultp = Zalloc(sizeof (md_mn_result_t));
11290Sstevel@tonic-gate 		resultp->mmr_comm_state = success;
11308452SJohn.Wren.Kennedy@Sun.COM 		/*
11318452SJohn.Wren.Kennedy@Sun.COM 		 * copy the MSGID so that we know _which_ message
11328452SJohn.Wren.Kennedy@Sun.COM 		 * failed (if the transp has got mangled)
11338452SJohn.Wren.Kennedy@Sun.COM 		 */
11348452SJohn.Wren.Kennedy@Sun.COM 		MSGID_COPY(&(msg->msg_msgid), &(resultp->mmr_msgid));
11350Sstevel@tonic-gate 		mdmn_svc_sendreply(transp, xdr_md_mn_result_t, (char *)resultp);
11360Sstevel@tonic-gate 		commd_debug(MD_MMV_SEND,
11370Sstevel@tonic-gate 		    "send_to_work: not registered (%d, 0x%llx-%d) cs=%d\n",
11380Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid), success);
11390Sstevel@tonic-gate 		free_result(resultp);
11408452SJohn.Wren.Kennedy@Sun.COM 		/*
11418452SJohn.Wren.Kennedy@Sun.COM 		 * We don't have a timeout registered to wake us up, so we're
11428452SJohn.Wren.Kennedy@Sun.COM 		 * now done with this handle. Release it back to the pool.
11438452SJohn.Wren.Kennedy@Sun.COM 		 */
11448452SJohn.Wren.Kennedy@Sun.COM 		svc_done(transp);
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	}
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	free_msg(msg);
11498452SJohn.Wren.Kennedy@Sun.COM 	/* the alloc was done in mdmn_send_svc_2 */
11508452SJohn.Wren.Kennedy@Sun.COM 	Free(matp);
115111053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(mx);
11520Sstevel@tonic-gate 	return (NULL);
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate /*
11570Sstevel@tonic-gate  * do_message_locally(msg, result)
11580Sstevel@tonic-gate  * Process a message locally on the master
11590Sstevel@tonic-gate  * Lookup the MCT if the message has already been processed.
11600Sstevel@tonic-gate  * If not, call the handler and store the result
11610Sstevel@tonic-gate  * If yes, retrieve the result from the MCT.
11620Sstevel@tonic-gate  * Return:
11630Sstevel@tonic-gate  *	MDMNE_ACK in case of success
11640Sstevel@tonic-gate  *	MDMNE_LOG_FAIL if the MCT could not be checked
11650Sstevel@tonic-gate  */
11660Sstevel@tonic-gate static int
do_message_locally(md_mn_msg_t * msg,md_mn_result_t * result)11670Sstevel@tonic-gate do_message_locally(md_mn_msg_t *msg, md_mn_result_t *result)
11680Sstevel@tonic-gate {
11690Sstevel@tonic-gate 	int			completed;
11700Sstevel@tonic-gate 	set_t			setno;
11710Sstevel@tonic-gate 	md_mn_msgtype_t		msgtype = msg->msg_type;
11720Sstevel@tonic-gate 	md_mn_msgclass_t	class;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	void (*handler)(md_mn_msg_t *msg, uint_t flags, md_mn_result_t *res);
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	handler = mdmn_get_handler(msgtype);
11770Sstevel@tonic-gate 	if (handler == NULL) {
11780Sstevel@tonic-gate 		result->mmr_exitval = 0;
11790Sstevel@tonic-gate 		/* let the sender decide if this is an error or not */
11800Sstevel@tonic-gate 		result->mmr_comm_state = MDMNE_NO_HANDLER;
11810Sstevel@tonic-gate 		return (MDMNE_NO_HANDLER);
11820Sstevel@tonic-gate 	}
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	class = mdmn_get_message_class(msg->msg_type);
11850Sstevel@tonic-gate 	setno = msg->msg_setno;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	result->mmr_msgtype	= msgtype;
11880Sstevel@tonic-gate 	result->mmr_flags	= msg->msg_flags;
11890Sstevel@tonic-gate 	MSGID_COPY(&(msg->msg_msgid), &(result->mmr_msgid));
11900Sstevel@tonic-gate 
119111053SSurya.Prakki@Sun.COM 	(void) mutex_lock(&mct_mutex[setno][class]);
11920Sstevel@tonic-gate 	completed = mdmn_check_completion(msg, result);
11930Sstevel@tonic-gate 	if (completed == MDMN_MCT_NOT_DONE) {
11940Sstevel@tonic-gate 		/* message not yet processed locally */
11950Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M, "proc_mas: "
11960Sstevel@tonic-gate 		    "calling handler for (%d,0x%llx-%d) type %d\n",
11970Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid), msgtype);
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 		/*
12000Sstevel@tonic-gate 		 * Mark the message as being currently processed,
12010Sstevel@tonic-gate 		 * so we won't start a second handler for it
12020Sstevel@tonic-gate 		 */
12030Sstevel@tonic-gate 		(void) mdmn_mark_completion(msg, NULL, MDMN_MCT_IN_PROGRESS);
120411053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&mct_mutex[setno][class]);
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 		/* here we actually process the message on the master */
12070Sstevel@tonic-gate 		(*handler)(msg, MD_MSGF_ON_MASTER, result);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M, "proc_mas: "
12100Sstevel@tonic-gate 		    "finished handler for (%d,0x%llx-%d) type %d\n",
12110Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid), msgtype);
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 		/* Mark the message as fully processed, store the result */
121411053SSurya.Prakki@Sun.COM 		(void) mutex_lock(&mct_mutex[setno][class]);
12150Sstevel@tonic-gate 		(void) mdmn_mark_completion(msg, result, MDMN_MCT_DONE);
12160Sstevel@tonic-gate 	} else if (completed == MDMN_MCT_DONE) {
12170Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M, "proc_mas: "
12180Sstevel@tonic-gate 		    "result for (%d, 0x%llx-%d) from MCT\n",
12190Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid), msgtype);
12200Sstevel@tonic-gate 	} else if (completed == MDMN_MCT_IN_PROGRESS) {
12210Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M, "proc_mas: "
12220Sstevel@tonic-gate 		    "(%d, 0x%llx-%d) is currently being processed\n",
12230Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid), msgtype);
12240Sstevel@tonic-gate 	} else {
12250Sstevel@tonic-gate 		/* MCT error occurred (should never happen) */
122611053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&mct_mutex[setno][class]);
12270Sstevel@tonic-gate 		result->mmr_comm_state = MDMNE_LOG_FAIL;
12280Sstevel@tonic-gate 		commd_debug(MD_MMV_SYSLOG, "WARNING "
12290Sstevel@tonic-gate 		    "mdmn_check_completion returned %d "
12300Sstevel@tonic-gate 		    "for (%d,0x%llx-%d)\n", completed,
12310Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid));
12320Sstevel@tonic-gate 		return (MDMNE_LOG_FAIL);
12330Sstevel@tonic-gate 	}
123411053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(&mct_mutex[setno][class]);
12350Sstevel@tonic-gate 	return (MDMNE_ACK);
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate }
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate /*
12400Sstevel@tonic-gate  * do_send_message(msg, node)
12410Sstevel@tonic-gate  *
12420Sstevel@tonic-gate  * Send a message to a given node and wait for a acknowledgment, that the
12430Sstevel@tonic-gate  * message has arrived on the remote node.
12440Sstevel@tonic-gate  * Make sure that the client for the set is setup correctly.
12450Sstevel@tonic-gate  * If no ACK arrives, destroy and recreate the RPC client and retry the
12460Sstevel@tonic-gate  * message one time
12470Sstevel@tonic-gate  * After actually sending wait no longer than the appropriate number of
12480Sstevel@tonic-gate  * before timing out the message.
12490Sstevel@tonic-gate  *
12500Sstevel@tonic-gate  * Note must be called with set_desc_wrlock held in reader mode
12510Sstevel@tonic-gate  */
12520Sstevel@tonic-gate static int
do_send_message(md_mn_msg_t * msg,md_mnnode_desc * node)12530Sstevel@tonic-gate do_send_message(md_mn_msg_t *msg, md_mnnode_desc *node)
12540Sstevel@tonic-gate {
12550Sstevel@tonic-gate 	int			err;
12560Sstevel@tonic-gate 	int			rpc_retries;
12570Sstevel@tonic-gate 	int			timeout_retries = 0;
12580Sstevel@tonic-gate 	int			*ret = NULL;
12590Sstevel@tonic-gate 	set_t			setno;
12608452SJohn.Wren.Kennedy@Sun.COM 	cond_t			*cv;	/* see mdmn_wakeup_master_svc_2 */
12610Sstevel@tonic-gate 	mutex_t			*mx;	/* protection for class_busy */
12620Sstevel@tonic-gate 	timestruc_t		timeout; /* surveillance for remote daemon */
12630Sstevel@tonic-gate 	md_mn_nodeid_t		nid;
12640Sstevel@tonic-gate 	md_mn_msgtype_t		msgtype;
12650Sstevel@tonic-gate 	md_mn_msgclass_t	class;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	nid	= node->nd_nodeid;
12680Sstevel@tonic-gate 	msgtype = msg->msg_type;
12690Sstevel@tonic-gate 	setno	= msg->msg_setno;
12700Sstevel@tonic-gate 	class	= mdmn_get_message_class(msgtype);
12710Sstevel@tonic-gate 	mx	= mdmn_get_master_table_mx(setno, class);
12720Sstevel@tonic-gate 	cv	= mdmn_get_master_table_cv(setno, class);
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate retry_rpc:
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	/* We try two times to send the message */
12770Sstevel@tonic-gate 	rpc_retries = 2;
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	/*
12800Sstevel@tonic-gate 	 * if sending the message doesn't succeed the first time due to a
12810Sstevel@tonic-gate 	 * RPC problem, we retry one time
12820Sstevel@tonic-gate 	 */
12830Sstevel@tonic-gate 	while ((rpc_retries != 0) && (ret == NULL)) {
12840Sstevel@tonic-gate 		/*  in abort state, we error out immediately */
12850Sstevel@tonic-gate 		if (md_commd_global_state & MD_CGS_ABORTED) {
12860Sstevel@tonic-gate 			return (MDMNE_ABORT);
12870Sstevel@tonic-gate 		}
12880Sstevel@tonic-gate 
128911053SSurya.Prakki@Sun.COM 		(void) rw_rdlock(&client_rwlock[setno]);
12900Sstevel@tonic-gate 		/* unable to create client? Ignore it */
12910Sstevel@tonic-gate 		if (check_client(setno, nid)) {
12920Sstevel@tonic-gate 			/*
12930Sstevel@tonic-gate 			 * In case we cannot establish an RPC client, we
12940Sstevel@tonic-gate 			 * take this node out of our considerations.
12950Sstevel@tonic-gate 			 * This will be reset by a reconfig
12960Sstevel@tonic-gate 			 * cycle that should come pretty soon.
12970Sstevel@tonic-gate 			 * MNISSUE: Should a reconfig cycle
12980Sstevel@tonic-gate 			 * be forced on SunCluster?
12990Sstevel@tonic-gate 			 */
13000Sstevel@tonic-gate 			node->nd_flags &= ~MD_MN_NODE_OWN;
13010Sstevel@tonic-gate 			commd_debug(MD_MMV_SYSLOG,
13020Sstevel@tonic-gate 			    "WARNING couldn't create client for %s\n"
13030Sstevel@tonic-gate 			    "Reconfig cycle required\n",
13040Sstevel@tonic-gate 			    node->nd_nodename);
13050Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_M, "proc_mas: (%d,0x%llx-%d) "
13060Sstevel@tonic-gate 			    "WARNING couldn't create client for %s\n",
13070Sstevel@tonic-gate 			    MSGID_ELEMS(msg->msg_msgid), node->nd_nodename);
130811053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
13090Sstevel@tonic-gate 			return (MDMNE_IGNORE_NODE);
13100Sstevel@tonic-gate 		}
13110Sstevel@tonic-gate 		/* let's be paranoid and check again before sending */
13120Sstevel@tonic-gate 		if (client[setno][nid] == NULL) {
13130Sstevel@tonic-gate 			/*
13140Sstevel@tonic-gate 			 * if this is true, strange enough, we catch our breath,
13150Sstevel@tonic-gate 			 * and then continue, so that the client is set up
13160Sstevel@tonic-gate 			 * once again.
13170Sstevel@tonic-gate 			 */
13180Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_M, "client is NULL\n");
131911053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
132011053SSurya.Prakki@Sun.COM 			(void) sleep(1);
13210Sstevel@tonic-gate 			continue;
13220Sstevel@tonic-gate 		}
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 		/* send it over, it will return immediately */
13258452SJohn.Wren.Kennedy@Sun.COM 		ret = mdmn_work_2(msg, client[setno][nid], nid);
13260Sstevel@tonic-gate 
132711053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&client_rwlock[setno]);
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 		if (ret != NULL) {
13300Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_M,
13310Sstevel@tonic-gate 			    "proc_mas: sending (%d,0x%llx-%d) to %d returned "
13320Sstevel@tonic-gate 			    " 0x%x\n",
13330Sstevel@tonic-gate 			    MSGID_ELEMS(msg->msg_msgid), nid, *ret);
13340Sstevel@tonic-gate 		} else {
13350Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_M,
13360Sstevel@tonic-gate 			    "proc_mas: sending (%d,0x%llx-%d) to %d returned "
13370Sstevel@tonic-gate 			    " NULL \n",
13380Sstevel@tonic-gate 			    MSGID_ELEMS(msg->msg_msgid), nid);
13390Sstevel@tonic-gate 		}
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 		if ((ret == NULL) || (*ret == MDMNE_CANNOT_CONNECT) ||
13420Sstevel@tonic-gate 		    (*ret == MDMNE_THR_CREATE_FAIL)) {
13430Sstevel@tonic-gate 			/*
13440Sstevel@tonic-gate 			 * Something happened to the daemon on the other side.
13450Sstevel@tonic-gate 			 * Kill the client, and try again.
13460Sstevel@tonic-gate 			 * check_client() will create a new client
13470Sstevel@tonic-gate 			 */
134811053SSurya.Prakki@Sun.COM 			(void) rw_wrlock(&client_rwlock[setno]);
13490Sstevel@tonic-gate 			mdmn_clnt_destroy(client[setno][nid]);
13500Sstevel@tonic-gate 			if (client[setno][nid] != (CLIENT *)NULL) {
13510Sstevel@tonic-gate 				client[setno][nid] = (CLIENT *)NULL;
13520Sstevel@tonic-gate 			}
135311053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 			/* ... but don't try infinitely */
13560Sstevel@tonic-gate 			--rpc_retries;
13570Sstevel@tonic-gate 			continue;
13580Sstevel@tonic-gate 		}
13590Sstevel@tonic-gate 		/*
13600Sstevel@tonic-gate 		 * If the class is locked on the other node, keep trying.
13610Sstevel@tonic-gate 		 * This situation will go away automatically,
13620Sstevel@tonic-gate 		 * if we wait long enough
13630Sstevel@tonic-gate 		 */
13640Sstevel@tonic-gate 		if (*ret == MDMNE_CLASS_LOCKED) {
136511053SSurya.Prakki@Sun.COM 			(void) sleep(1);
13660Sstevel@tonic-gate 			free(ret);
13670Sstevel@tonic-gate 			ret = NULL;
13680Sstevel@tonic-gate 			continue;
13690Sstevel@tonic-gate 		}
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 	if (ret == NULL) {
13720Sstevel@tonic-gate 		return (MDMNE_RPC_FAIL);
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	/* if the slave is in abort state, we just ignore it. */
13770Sstevel@tonic-gate 	if (*ret == MDMNE_ABORT) {
13780Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M,
13790Sstevel@tonic-gate 		    "proc_mas: work(%d,0x%llx-%d) returned "
13800Sstevel@tonic-gate 		    "MDMNE_ABORT\n",
13810Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid));
13820Sstevel@tonic-gate 		free(ret);
13830Sstevel@tonic-gate 		return (MDMNE_IGNORE_NODE);
13840Sstevel@tonic-gate 	}
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	/* Did the remote processing succeed? */
13870Sstevel@tonic-gate 	if (*ret != MDMNE_ACK) {
13880Sstevel@tonic-gate 		/*
13890Sstevel@tonic-gate 		 * Some commd failure in the middle of sending the msg
13900Sstevel@tonic-gate 		 * to the nodes. We don't continue here.
13910Sstevel@tonic-gate 		 */
13920Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M,
13930Sstevel@tonic-gate 		    "proc_mas: work(%d,0x%llx-%d) returns %d\n",
13940Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid), *ret);
13950Sstevel@tonic-gate 		free(ret);
13960Sstevel@tonic-gate 		return (MDMNE_RPC_FAIL);
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate 	free(ret);
13990Sstevel@tonic-gate 	ret = NULL;
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	/*
14020Sstevel@tonic-gate 	 * When we are here, we have sent the message to the other node and
14030Sstevel@tonic-gate 	 * we know that node has accepted it.
14040Sstevel@tonic-gate 	 * We go to sleep and have trust to be woken up by wakeup.
14050Sstevel@tonic-gate 	 * If we wakeup due to a timeout, or a signal, no result has been
14060Sstevel@tonic-gate 	 * placed in the appropriate slot.
14070Sstevel@tonic-gate 	 * If we timeout, it is likely that this is because the node has
14080Sstevel@tonic-gate 	 * gone away, so we will destroy the client and try it again in the
14090Sstevel@tonic-gate 	 * expectation that the rpc will fail and we will return
14100Sstevel@tonic-gate 	 * MDMNE_IGNORE_NODE. If that is not the case, the message must still
14110Sstevel@tonic-gate 	 * be being processed on the slave. In this case just timeout for 4
14120Sstevel@tonic-gate 	 * more seconds and then return RPC_FAIL if the message is not complete.
14130Sstevel@tonic-gate 	 */
14140Sstevel@tonic-gate 	timeout.tv_nsec = 0;
14150Sstevel@tonic-gate 	timeout.tv_sec = (timeout_retries == 0) ? mdmn_get_timeout(msgtype) :
14160Sstevel@tonic-gate 	    FOUR_SECS.tv_sec;
14170Sstevel@tonic-gate 	err = cond_reltimedwait(cv, mx, &timeout);
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	if (err == 0) {
14200Sstevel@tonic-gate 		/* everything's fine, return success */
14210Sstevel@tonic-gate 		return (MDMNE_ACK);
14220Sstevel@tonic-gate 	}
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 	if (err == ETIME) {
14250Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M, "proc_mas: "
14260Sstevel@tonic-gate 		    "timeout occured, set=%d, class=%d, "
14270Sstevel@tonic-gate 		    "msgid=(%d, 0x%llx-%d), timeout_retries=%d\n",
14280Sstevel@tonic-gate 		    setno, class, MSGID_ELEMS(msg->msg_msgid), timeout_retries);
14290Sstevel@tonic-gate 		if (timeout_retries == 0) {
14300Sstevel@tonic-gate 			timeout_retries++;
14310Sstevel@tonic-gate 			/*
14320Sstevel@tonic-gate 			 * Destroy the client and try the rpc call again
14330Sstevel@tonic-gate 			 */
143411053SSurya.Prakki@Sun.COM 			(void) rw_wrlock(&client_rwlock[setno]);
14350Sstevel@tonic-gate 			mdmn_clnt_destroy(client[setno][nid]);
14360Sstevel@tonic-gate 			client[setno][nid] = (CLIENT *)NULL;
143711053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
14380Sstevel@tonic-gate 			goto retry_rpc;
14390Sstevel@tonic-gate 		}
14400Sstevel@tonic-gate 	} else if (err == EINTR) {
14410Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M, "proc_mas: "
14420Sstevel@tonic-gate 		    "commd signalled, set=%d, class=%d, "
14430Sstevel@tonic-gate 		    "msgid=(%d, 0x%llx-%d)\n",
14440Sstevel@tonic-gate 		    setno, class, MSGID_ELEMS(msg->msg_msgid));
14450Sstevel@tonic-gate 	} else {
14460Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M, "proc_mas: "
14470Sstevel@tonic-gate 		    "cond_reltimedwait err=%d, set=%d, "
14480Sstevel@tonic-gate 		    "class=%d, msgid=(%d, 0x%llx-%d)\n",
14490Sstevel@tonic-gate 		    err, setno, class,
14500Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid));
14510Sstevel@tonic-gate 	}
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	/* some failure happened */
14540Sstevel@tonic-gate 	return (MDMNE_RPC_FAIL);
14550Sstevel@tonic-gate }
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate /*
14580Sstevel@tonic-gate  * before we return we have to
14590Sstevel@tonic-gate  * free_msg(msg); because we are working on a copied message
14600Sstevel@tonic-gate  */
14610Sstevel@tonic-gate void
mdmn_master_process_msg(md_mn_msg_t * msg)14620Sstevel@tonic-gate mdmn_master_process_msg(md_mn_msg_t *msg)
14630Sstevel@tonic-gate {
14640Sstevel@tonic-gate 	int		*ret;
14650Sstevel@tonic-gate 	int		err;
14660Sstevel@tonic-gate 	int		nmsgs;		/* total number of msgs */
14670Sstevel@tonic-gate 	int		curmsg;		/* index of current msg */
14680Sstevel@tonic-gate 	set_t		setno;
14690Sstevel@tonic-gate 	uint_t		inherit_flags = 0;
14700Sstevel@tonic-gate 	uint_t		secdiff, usecdiff; /* runtime of this message */
14710Sstevel@tonic-gate 	md_error_t	mde = mdnullerror;
14720Sstevel@tonic-gate 	md_mn_msg_t	*msglist[MAX_SUBMESSAGES]; /* all msgs to process */
14730Sstevel@tonic-gate 	md_mn_msg_t	*cmsg;		/* current msg */
14740Sstevel@tonic-gate 	md_mn_msgid_t	dummyid;
14750Sstevel@tonic-gate 	md_mn_result_t	*result;
14760Sstevel@tonic-gate 	md_mn_result_t	*slave_result;
14770Sstevel@tonic-gate 	md_mn_nodeid_t	sender;
14780Sstevel@tonic-gate 	md_mn_nodeid_t	set_master;
14790Sstevel@tonic-gate 	md_mnnode_desc	*node;
14800Sstevel@tonic-gate 	md_mn_msgtype_t	orig_type;	/* type of the original message */
14810Sstevel@tonic-gate 	md_mn_msgtype_t	msgtype;	/* type of the current message */
14820Sstevel@tonic-gate 	md_mn_msgclass_t orig_class;	/* class of the original message */
14830Sstevel@tonic-gate 	md_mn_msgclass_t class;		/* class of the current message */
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	int (*smgen)(md_mn_msg_t *msg, md_mn_msg_t **msglist);
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	orig_type = msgtype = msg->msg_type;
14880Sstevel@tonic-gate 	sender	= msg->msg_sender;
14890Sstevel@tonic-gate 	setno	= msg->msg_setno;
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	result = Zalloc(sizeof (md_mn_result_t));
14920Sstevel@tonic-gate 	result->mmr_setno	= setno;
14930Sstevel@tonic-gate 	result->mmr_msgtype	= msgtype;
14940Sstevel@tonic-gate 	MSGID_COPY(&(msg->msg_msgid), &(result->mmr_msgid));
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	orig_class = mdmn_get_message_class(msgtype);
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	commd_debug(MD_MMV_PROC_M,
14990Sstevel@tonic-gate 	    "proc_mas: received (%d, 0x%llx-%d) set=%d, class=%d, type=%d\n",
15000Sstevel@tonic-gate 	    MSGID_ELEMS(msg->msg_msgid), setno, orig_class, msgtype);
15010Sstevel@tonic-gate 
150211053SSurya.Prakki@Sun.COM 	(void) rw_rdlock(&set_desc_rwlock[setno]);
15030Sstevel@tonic-gate 	set_master = set_descriptor[setno]->sd_mn_master_nodeid;
15040Sstevel@tonic-gate 	result->mmr_sender	= set_master;
15050Sstevel@tonic-gate 	/*
15060Sstevel@tonic-gate 	 * Put message into the change log unless told otherwise
15070Sstevel@tonic-gate 	 * Note that we only log original messages.
15080Sstevel@tonic-gate 	 * If they are generated by some smgen, we don't log them!
15090Sstevel@tonic-gate 	 * Replay messages aren't logged either.
15100Sstevel@tonic-gate 	 * Note, that replay messages are unlogged on completion.
15110Sstevel@tonic-gate 	 */
15120Sstevel@tonic-gate 	if ((msg->msg_flags & (MD_MSGF_NO_LOG | MD_MSGF_REPLAY_MSG)) == 0) {
15130Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M,
15140Sstevel@tonic-gate 		    "proc_mas: calling log_msg for (%d,0x%llx-%d) type %d\n",
15150Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid), msgtype);
15160Sstevel@tonic-gate 		err = mdmn_log_msg(msg);
15170Sstevel@tonic-gate 		if (err == MDMNE_NULL) {
15180Sstevel@tonic-gate 			/* msg logged successfully */
15190Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_M, "proc_mas: "
15200Sstevel@tonic-gate 			    "done log_msg for (%d,0x%llx-%d) type %d\n",
15210Sstevel@tonic-gate 			    MSGID_ELEMS(msg->msg_msgid), msgtype);
15220Sstevel@tonic-gate 			goto proceed;
15230Sstevel@tonic-gate 		}
15240Sstevel@tonic-gate 		if (err == MDMNE_ACK) {
15250Sstevel@tonic-gate 			/* Same msg in the slot, proceed */
15260Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_M, "proc_mas: "
15270Sstevel@tonic-gate 			    "already logged (%d,0x%llx-%d) type %d\n",
15280Sstevel@tonic-gate 			    MSGID_ELEMS(msg->msg_msgid), msgtype);
15290Sstevel@tonic-gate 			goto proceed;
15300Sstevel@tonic-gate 		}
15310Sstevel@tonic-gate 		if (err == MDMNE_LOG_FAIL) {
15320Sstevel@tonic-gate 			/* Oh, bad, the log is non functional. */
15330Sstevel@tonic-gate 			result->mmr_comm_state = MDMNE_LOG_FAIL;
15340Sstevel@tonic-gate 			/*
15350Sstevel@tonic-gate 			 * Note that the mark_busy was already done by
15368452SJohn.Wren.Kennedy@Sun.COM 			 * mdmn_work_svc_2()
15370Sstevel@tonic-gate 			 */
153811053SSurya.Prakki@Sun.COM 			(void) mutex_lock(&mdmn_busy_mutex[setno]);
15390Sstevel@tonic-gate 			mdmn_mark_class_unbusy(setno, orig_class);
154011053SSurya.Prakki@Sun.COM 			(void) mutex_unlock(&mdmn_busy_mutex[setno]);
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 		}
15430Sstevel@tonic-gate 		if (err == MDMNE_CLASS_BUSY) {
15440Sstevel@tonic-gate 			/*
15450Sstevel@tonic-gate 			 * The log is occupied with a different message
15460Sstevel@tonic-gate 			 * that needs to be played first.
15470Sstevel@tonic-gate 			 * We reject the current message with MDMNE_CLASS_BUSY
15480Sstevel@tonic-gate 			 * to the initiator and do not unbusy the set/class,
15490Sstevel@tonic-gate 			 * because we will proceed with the logged message,
15500Sstevel@tonic-gate 			 * which has the same set/class combination
15510Sstevel@tonic-gate 			 */
15520Sstevel@tonic-gate 			result->mmr_comm_state = MDMNE_CLASS_BUSY;
15530Sstevel@tonic-gate 		}
15540Sstevel@tonic-gate 		ret = (int *)NULL;
155511053SSurya.Prakki@Sun.COM 		(void) rw_rdlock(&client_rwlock[setno]);
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 		if (check_client(setno, sender)) {
15580Sstevel@tonic-gate 			commd_debug(MD_MMV_SYSLOG,
15590Sstevel@tonic-gate 			    "proc_mas: No client for initiator \n");
15600Sstevel@tonic-gate 		} else {
15618452SJohn.Wren.Kennedy@Sun.COM 			ret = mdmn_wakeup_initiator_2(result,
15628452SJohn.Wren.Kennedy@Sun.COM 			    client[setno][sender], sender);
15630Sstevel@tonic-gate 		}
156411053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&client_rwlock[setno]);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 		if (ret == (int *)NULL) {
15670Sstevel@tonic-gate 			commd_debug(MD_MMV_SYSLOG,
15680Sstevel@tonic-gate 			    "proc_mas: couldn't wakeup_initiator \n");
15690Sstevel@tonic-gate 		} else {
15700Sstevel@tonic-gate 			if (*ret != MDMNE_ACK) {
15710Sstevel@tonic-gate 				commd_debug(MD_MMV_SYSLOG, "proc_mas: "
15720Sstevel@tonic-gate 				    "wakeup_initiator returned %d\n", *ret);
15730Sstevel@tonic-gate 			}
15740Sstevel@tonic-gate 			free(ret);
15750Sstevel@tonic-gate 		}
15760Sstevel@tonic-gate 		free_msg(msg);
15770Sstevel@tonic-gate 
15780Sstevel@tonic-gate 		if (err == MDMNE_LOG_FAIL) {
15790Sstevel@tonic-gate 			/* we can't proceed here */
15800Sstevel@tonic-gate 			free_result(result);
158111053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&set_desc_rwlock[setno]);
15820Sstevel@tonic-gate 			return;
15830Sstevel@tonic-gate 		} else if (err == MDMNE_CLASS_BUSY) {
15840Sstevel@tonic-gate 			mdmn_changelog_record_t *lr;
15850Sstevel@tonic-gate 			lr = mdmn_get_changelogrec(setno, orig_class);
15860Sstevel@tonic-gate 			assert(lr != NULL);
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 			/* proceed with the logged message */
15890Sstevel@tonic-gate 			msg = copy_msg(&(lr->lr_msg), NULL);
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 			/*
15920Sstevel@tonic-gate 			 * The logged message has to have the same class but
15930Sstevel@tonic-gate 			 * type and sender can be different
15940Sstevel@tonic-gate 			 */
15950Sstevel@tonic-gate 			orig_type = msgtype = msg->msg_type;
15960Sstevel@tonic-gate 			sender	= msg->msg_sender;
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_M,
15990Sstevel@tonic-gate 			    "proc_mas: Got new message from change log: "
16000Sstevel@tonic-gate 			    "(%d,0x%llx-%d) type %d\n",
16010Sstevel@tonic-gate 			    MSGID_ELEMS(msg->msg_msgid), msgtype);
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 			/* continue normal operation with this message */
16040Sstevel@tonic-gate 		}
16050Sstevel@tonic-gate 	}
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate proceed:
16080Sstevel@tonic-gate 	smgen = mdmn_get_submessage_generator(msgtype);
16090Sstevel@tonic-gate 	if (smgen == NULL) {
16100Sstevel@tonic-gate 		/* no submessages to create, just use the original message */
16110Sstevel@tonic-gate 		msglist[0] = msg;
16120Sstevel@tonic-gate 		nmsgs = 1;
16130Sstevel@tonic-gate 	} else {
16140Sstevel@tonic-gate 		/* some bits are passed on to submessages */
16150Sstevel@tonic-gate 		inherit_flags = msg->msg_flags & MD_MSGF_INHERIT_BITS;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 		nmsgs = smgen(msg, msglist);
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 		/* some settings for the submessages */
16200Sstevel@tonic-gate 		for (curmsg = 0; curmsg < nmsgs; curmsg++) {
16210Sstevel@tonic-gate 			cmsg    = msglist[curmsg];
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 			/* Apply the inherited flags */
16240Sstevel@tonic-gate 			cmsg->msg_flags |= inherit_flags;
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 			/*
16270Sstevel@tonic-gate 			 * Make sure the submessage ID is set correctly
16280Sstevel@tonic-gate 			 * Note: first submessage has mid_smid of 1 (not 0)
16290Sstevel@tonic-gate 			 */
16300Sstevel@tonic-gate 			cmsg->msg_msgid.mid_smid = curmsg + 1;
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 			/* need the original class set in msgID (for MCT) */
16330Sstevel@tonic-gate 			cmsg->msg_msgid.mid_oclass = orig_class;
16340Sstevel@tonic-gate 		}
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M,
16370Sstevel@tonic-gate 		    "smgen generated %d submsgs, origclass = %d\n",
16380Sstevel@tonic-gate 		    nmsgs, orig_class);
16390Sstevel@tonic-gate 	}
16400Sstevel@tonic-gate 	/*
16410Sstevel@tonic-gate 	 * This big loop does the following.
16420Sstevel@tonic-gate 	 * For all messages:
16430Sstevel@tonic-gate 	 *	process message on the master first (a message completion
16440Sstevel@tonic-gate 	 *		table MCT ensures a message is not processed twice)
16450Sstevel@tonic-gate 	 *	in case of an error break out of message loop
16460Sstevel@tonic-gate 	 *	for all nodes -- unless MD_MSGF_NO_BCAST is set --
16470Sstevel@tonic-gate 	 *		send message to node until that succeeds
16480Sstevel@tonic-gate 	 *		merge result -- not yet implemented
16490Sstevel@tonic-gate 	 *		respect MD_MSGF_STOP_ON_ERROR
16500Sstevel@tonic-gate 	 */
16510Sstevel@tonic-gate 	for (curmsg = 0; curmsg < nmsgs; curmsg++) {
16520Sstevel@tonic-gate 		int	break_msg_loop = 0;
16530Sstevel@tonic-gate 		mutex_t	*mx;		/* protection for class_busy */
16540Sstevel@tonic-gate 		int	master_err;
16550Sstevel@tonic-gate 		int	master_exitval = -1;
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 		cmsg	= msglist[curmsg];
16580Sstevel@tonic-gate 		msgtype = cmsg->msg_type;
16590Sstevel@tonic-gate 		class	= mdmn_get_message_class(msgtype);
16600Sstevel@tonic-gate 		node	= NULL;
16610Sstevel@tonic-gate 		mx	= mdmn_get_master_table_mx(setno, class);
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 		/* If we are in the abort state, we error out immediately */
16640Sstevel@tonic-gate 		if (md_commd_global_state & MD_CGS_ABORTED) {
16650Sstevel@tonic-gate 			break; /* out of the message loop */
16660Sstevel@tonic-gate 		}
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M, "class=%d, orig_class=%d\n",
16690Sstevel@tonic-gate 		    class, orig_class);
16700Sstevel@tonic-gate 		/*
16710Sstevel@tonic-gate 		 * If the current class is different from the original class,
16720Sstevel@tonic-gate 		 * we have to lock it down.
16730Sstevel@tonic-gate 		 * The original class is already marked busy.
16740Sstevel@tonic-gate 		 * At this point we cannot refuse the message because the
16750Sstevel@tonic-gate 		 * class is busy right now, so we wait until the class becomes
16760Sstevel@tonic-gate 		 * available again. As soon as something changes for this set
16770Sstevel@tonic-gate 		 * we will be cond_signal'ed (in mdmn_mark_class_unbusy)
16780Sstevel@tonic-gate 		 *
16790Sstevel@tonic-gate 		 * Granularity could be finer (setno/class)
16800Sstevel@tonic-gate 		 */
16810Sstevel@tonic-gate 		if (class != orig_class) {
168211053SSurya.Prakki@Sun.COM 			(void) mutex_lock(&mdmn_busy_mutex[setno]);
16830Sstevel@tonic-gate 			while (mdmn_mark_class_busy(setno, class) == FALSE) {
168411053SSurya.Prakki@Sun.COM 				(void) cond_wait(&mdmn_busy_cv[setno],
16850Sstevel@tonic-gate 				    &mdmn_busy_mutex[setno]);
16860Sstevel@tonic-gate 			}
168711053SSurya.Prakki@Sun.COM 			(void) mutex_unlock(&mdmn_busy_mutex[setno]);
16880Sstevel@tonic-gate 		}
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 		master_err = do_message_locally(cmsg, result);
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 		if ((master_err != MDMNE_ACK) ||
16930Sstevel@tonic-gate 		    ((master_err == MDMNE_ACK) && (result->mmr_exitval != 0))) {
16940Sstevel@tonic-gate 			result->mmr_failing_node = set_master;
16950Sstevel@tonic-gate 			if (cmsg->msg_flags & MD_MSGF_STOP_ON_ERROR) {
16960Sstevel@tonic-gate 				/*
16970Sstevel@tonic-gate 				 * if appropriate, unbusy the class and
16980Sstevel@tonic-gate 				 * break out of the message loop
16990Sstevel@tonic-gate 				 */
17000Sstevel@tonic-gate 				if (class != orig_class) {
170111053SSurya.Prakki@Sun.COM 					(void) mutex_lock(
170211053SSurya.Prakki@Sun.COM 					    &mdmn_busy_mutex[setno]);
17030Sstevel@tonic-gate 					mdmn_mark_class_unbusy(setno, class);
170411053SSurya.Prakki@Sun.COM 					(void) mutex_unlock(
170511053SSurya.Prakki@Sun.COM 					    &mdmn_busy_mutex[setno]);
17060Sstevel@tonic-gate 				}
17070Sstevel@tonic-gate 				break;
17080Sstevel@tonic-gate 			}
17090Sstevel@tonic-gate 		}
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 		if (master_err == MDMNE_ACK)
17120Sstevel@tonic-gate 			master_exitval = result->mmr_exitval;
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 		/* No broadcast? => next message */
17150Sstevel@tonic-gate 		if (cmsg->msg_flags & MD_MSGF_NO_BCAST) {
17160Sstevel@tonic-gate 			/* if appropriate, unbusy the class */
17170Sstevel@tonic-gate 			if (class != orig_class) {
171811053SSurya.Prakki@Sun.COM 				(void) mutex_lock(&mdmn_busy_mutex[setno]);
17190Sstevel@tonic-gate 				mdmn_mark_class_unbusy(setno, class);
172011053SSurya.Prakki@Sun.COM 				(void) mutex_unlock(&mdmn_busy_mutex[setno]);
17210Sstevel@tonic-gate 			}
17220Sstevel@tonic-gate 			continue;
17230Sstevel@tonic-gate 		}
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 		/* fake sender, so we get notified when the results are avail */
17270Sstevel@tonic-gate 		cmsg->msg_sender = set_master;
17280Sstevel@tonic-gate 		/*
17290Sstevel@tonic-gate 		 * register to the master_table. It's needed by wakeup_master to
17300Sstevel@tonic-gate 		 * wakeup the sleeping thread.
17310Sstevel@tonic-gate 		 * Access is protected by the class lock: mdmn_mark_class_busy()
17320Sstevel@tonic-gate 		 */
17330Sstevel@tonic-gate 		mdmn_set_master_table_id(setno, class, &(cmsg->msg_msgid));
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 
173711053SSurya.Prakki@Sun.COM 		(void) rw_rdlock(&set_desc_rwlock[setno]);
17380Sstevel@tonic-gate 		/* Send the message  to all other nodes */
17390Sstevel@tonic-gate 		for (node = set_descriptor[setno]->sd_nodelist; node;
17400Sstevel@tonic-gate 		    node = node->nd_next) {
17410Sstevel@tonic-gate 			md_mn_nodeid_t nid = node->nd_nodeid;
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 			/* We are master and have already processed the msg */
17440Sstevel@tonic-gate 			if (node == set_descriptor[setno]->sd_mn_masternode) {
17450Sstevel@tonic-gate 				continue;
17460Sstevel@tonic-gate 			}
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 			/* If this node didn't join the disk set, ignore it */
17490Sstevel@tonic-gate 			if ((node->nd_flags & MD_MN_NODE_OWN) == 0) {
17500Sstevel@tonic-gate 				continue;
17510Sstevel@tonic-gate 			}
17520Sstevel@tonic-gate 
17538452SJohn.Wren.Kennedy@Sun.COM 			/* If a DIRECTED message, skip non-recipient nodes */
17548452SJohn.Wren.Kennedy@Sun.COM 			if ((cmsg->msg_flags & MD_MSGF_DIRECTED) &&
17558452SJohn.Wren.Kennedy@Sun.COM 			    nid != cmsg->msg_recipient) {
17568452SJohn.Wren.Kennedy@Sun.COM 				continue;
17578452SJohn.Wren.Kennedy@Sun.COM 			}
17588452SJohn.Wren.Kennedy@Sun.COM 
175911053SSurya.Prakki@Sun.COM 			(void) mutex_lock(mx);
17600Sstevel@tonic-gate 			/*
17610Sstevel@tonic-gate 			 * Register the node that is addressed,
17620Sstevel@tonic-gate 			 * so we can detect unsolicited messages
17630Sstevel@tonic-gate 			 */
17640Sstevel@tonic-gate 			mdmn_set_master_table_addr(setno, class, nid);
17650Sstevel@tonic-gate 			slave_result = (md_mn_result_t *)NULL;
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 			/*
17680Sstevel@tonic-gate 			 * Now send it. do_send_message() will return if
17690Sstevel@tonic-gate 			 *	a failure occurs or
17700Sstevel@tonic-gate 			 *	the results are available
17710Sstevel@tonic-gate 			 */
17720Sstevel@tonic-gate 			err = do_send_message(cmsg, node);
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 			/*  in abort state, we error out immediately */
17750Sstevel@tonic-gate 			if (md_commd_global_state & MD_CGS_ABORTED) {
17760Sstevel@tonic-gate 				break;
17770Sstevel@tonic-gate 			}
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 			if (err == MDMNE_ACK) {
17800Sstevel@tonic-gate 				slave_result =
17810Sstevel@tonic-gate 				    mdmn_get_master_table_res(setno, class);
17820Sstevel@tonic-gate 				commd_debug(MD_MMV_PROC_M,
17830Sstevel@tonic-gate 				    "proc_mas: got result for (%d,0x%llx-%d)\n",
17840Sstevel@tonic-gate 				    MSGID_ELEMS(cmsg->msg_msgid));
17850Sstevel@tonic-gate 			} else if (err == MDMNE_IGNORE_NODE) {
178611053SSurya.Prakki@Sun.COM 				(void) mutex_unlock(mx);
17870Sstevel@tonic-gate 				continue; /* send to next node */
17880Sstevel@tonic-gate 			}
178911053SSurya.Prakki@Sun.COM 			(void) mutex_unlock(mx);
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 			/*
17930Sstevel@tonic-gate 			 * If the result is NULL, or err doesn't show success,
17940Sstevel@tonic-gate 			 * something went wrong with this RPC call.
17950Sstevel@tonic-gate 			 */
17960Sstevel@tonic-gate 			if ((slave_result == NULL) || (err != MDMNE_ACK)) {
17970Sstevel@tonic-gate 				/*
17980Sstevel@tonic-gate 				 * If PANIC_WHEN_INCONSISTENT set,
17990Sstevel@tonic-gate 				 * panic if the master succeeded while
18000Sstevel@tonic-gate 				 * this node failed
18010Sstevel@tonic-gate 				 */
18020Sstevel@tonic-gate 				if ((cmsg->msg_flags &
18030Sstevel@tonic-gate 				    MD_MSGF_PANIC_WHEN_INCONSISTENT) &&
18040Sstevel@tonic-gate 				    (master_err == MDMNE_ACK))
18050Sstevel@tonic-gate 					panic_system(nid, cmsg->msg_type,
18060Sstevel@tonic-gate 					    master_err, master_exitval,
18070Sstevel@tonic-gate 					    slave_result);
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 				result->mmr_failing_node = nid;
18100Sstevel@tonic-gate 				/* are we supposed to stop in case of error? */
18110Sstevel@tonic-gate 				if (cmsg->msg_flags & MD_MSGF_STOP_ON_ERROR) {
18120Sstevel@tonic-gate 					result->mmr_exitval = MDMNE_RPC_FAIL;
18130Sstevel@tonic-gate 					commd_debug(MD_MMV_SYSLOG, "proc_mas: "
18140Sstevel@tonic-gate 					    "result (%d,0x%llx-%d) is NULL\n",
18150Sstevel@tonic-gate 					    MSGID_ELEMS(cmsg->msg_msgid));
18160Sstevel@tonic-gate 					FLUSH_DEBUGFILE();
18170Sstevel@tonic-gate 					break_msg_loop = 1;
18180Sstevel@tonic-gate 					break; /* out of node loop first */
18190Sstevel@tonic-gate 				} else {
18200Sstevel@tonic-gate 					/* send msg to the next node */
18210Sstevel@tonic-gate 					continue;
18220Sstevel@tonic-gate 				}
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 			}
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 			/*
18270Sstevel@tonic-gate 			 * Message processed on remote node.
18280Sstevel@tonic-gate 			 * If PANIC_WHEN_INCONSISTENT set, panic if the
18290Sstevel@tonic-gate 			 * result is different on this node from the result
18300Sstevel@tonic-gate 			 * on the master
18310Sstevel@tonic-gate 			 */
18320Sstevel@tonic-gate 			if ((cmsg->msg_flags &
18330Sstevel@tonic-gate 			    MD_MSGF_PANIC_WHEN_INCONSISTENT) &&
18340Sstevel@tonic-gate 			    ((master_err != MDMNE_ACK) ||
18350Sstevel@tonic-gate 			    (slave_result->mmr_exitval != master_exitval)))
18360Sstevel@tonic-gate 				panic_system(nid, cmsg->msg_type, master_err,
18370Sstevel@tonic-gate 				    master_exitval, slave_result);
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 			/*
18400Sstevel@tonic-gate 			 * At this point we know we have a message that was
18410Sstevel@tonic-gate 			 * processed on the remote node.
18420Sstevel@tonic-gate 			 * We now check if the exitval is non zero.
18430Sstevel@tonic-gate 			 * In that case we discard the previous result and
18440Sstevel@tonic-gate 			 * rather use the current.
18450Sstevel@tonic-gate 			 * This means: If a message fails on no node,
18460Sstevel@tonic-gate 			 * the result from the master will be returned.
18470Sstevel@tonic-gate 			 * There's currently no such thing as merge of results
18480Sstevel@tonic-gate 			 * If additionally STOP_ON_ERROR is set, we bail out
18490Sstevel@tonic-gate 			 */
18500Sstevel@tonic-gate 			if (slave_result->mmr_exitval != 0) {
18510Sstevel@tonic-gate 				/* throw away the previously allocated result */
18520Sstevel@tonic-gate 				free_result(result);
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 				/* copy_result() allocates new memory */
18550Sstevel@tonic-gate 				result = copy_result(slave_result);
18560Sstevel@tonic-gate 				free_result(slave_result);
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 				dump_result(MD_MMV_PROC_M, "proc_mas", result);
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 				result->mmr_failing_node = nid;
18610Sstevel@tonic-gate 				if (cmsg->msg_flags & MD_MSGF_STOP_ON_ERROR) {
18620Sstevel@tonic-gate 					break_msg_loop = 1;
18630Sstevel@tonic-gate 					break; /* out of node loop */
18640Sstevel@tonic-gate 				}
18650Sstevel@tonic-gate 				continue; /* try next node */
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 			} else {
18680Sstevel@tonic-gate 				/*
18690Sstevel@tonic-gate 				 * MNIssue: may want to merge the results
18700Sstevel@tonic-gate 				 * from all slaves.  Currently only report
18710Sstevel@tonic-gate 				 * the results from the master.
18720Sstevel@tonic-gate 				 */
18730Sstevel@tonic-gate 				free_result(slave_result);
18740Sstevel@tonic-gate 			}
18750Sstevel@tonic-gate 
18760Sstevel@tonic-gate 		} /* End of loop over the nodes */
187711053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&set_desc_rwlock[setno]);
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 		/* release the current class again */
18810Sstevel@tonic-gate 		if (class != orig_class) {
188211053SSurya.Prakki@Sun.COM 			(void) mutex_lock(&mdmn_busy_mutex[setno]);
18830Sstevel@tonic-gate 			mdmn_mark_class_unbusy(setno, class);
188411053SSurya.Prakki@Sun.COM 			(void) mutex_unlock(&mdmn_busy_mutex[setno]);
18850Sstevel@tonic-gate 		}
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 		/* are we supposed to quit entirely ? */
18880Sstevel@tonic-gate 		if (break_msg_loop ||
18890Sstevel@tonic-gate 		    (md_commd_global_state & MD_CGS_ABORTED)) {
18900Sstevel@tonic-gate 			break; /* out of msg loop */
18910Sstevel@tonic-gate 		}
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 	} /* End of loop over the messages */
18940Sstevel@tonic-gate 	/*
18950Sstevel@tonic-gate 	 * If we are here, there's two possibilities:
18960Sstevel@tonic-gate 	 * 	- we processed all messages on all nodes without an error.
18970Sstevel@tonic-gate 	 *	    In this case we return the result from the master.
18980Sstevel@tonic-gate 	 *	    (to be implemented: return the merged result)
18990Sstevel@tonic-gate 	 *	- we encountered an error in which case result has been
19000Sstevel@tonic-gate 	 *	    set accordingly already.
19010Sstevel@tonic-gate 	 */
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 	if (md_commd_global_state & MD_CGS_ABORTED) {
19040Sstevel@tonic-gate 		result->mmr_comm_state = MDMNE_ABORT;
19050Sstevel@tonic-gate 	}
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 	/*
19080Sstevel@tonic-gate 	 * This message has been processed completely.
19090Sstevel@tonic-gate 	 * Remove it from the changelog.
19100Sstevel@tonic-gate 	 * Do this for replay messages too.
19110Sstevel@tonic-gate 	 * Note that the message is unlogged before waking up the
19120Sstevel@tonic-gate 	 * initiator.  This is done for two reasons.
19130Sstevel@tonic-gate 	 * 1. Remove a race condition that occurs when back to back
19140Sstevel@tonic-gate 	 *   messages are sent for the same class, the registeration is
19150Sstevel@tonic-gate 	 *   is lost.
19160Sstevel@tonic-gate 	 * 2. If the initiator died but the action was completed on all the
19170Sstevel@tonic-gate 	 *   the nodes, we want that to be marked "done" quickly.
19180Sstevel@tonic-gate 	 */
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 	if ((msg->msg_flags & MD_MSGF_NO_LOG) == 0) {
19210Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M,
19220Sstevel@tonic-gate 		    "proc_mas: calling unlog_msg for (%d,0x%llx-%d) type %d\n",
19230Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid), msgtype);
192411053SSurya.Prakki@Sun.COM 		(void) mdmn_unlog_msg(msg);
19250Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M,
19260Sstevel@tonic-gate 		    "proc_mas: done unlog_msg for (%d,0x%llx-%d) type %d\n",
19270Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid), msgtype);
19280Sstevel@tonic-gate 	}
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 	/*
19310Sstevel@tonic-gate 	 * In case of submessages, we increased the submessage ID in the
19320Sstevel@tonic-gate 	 * result structure. We restore the message ID to the value that
19330Sstevel@tonic-gate 	 * the initiator is waiting for.
19340Sstevel@tonic-gate 	 */
19350Sstevel@tonic-gate 	result->mmr_msgid.mid_smid	= 0;
19360Sstevel@tonic-gate 	result->mmr_msgtype		= orig_type;
19370Sstevel@tonic-gate 	result->mmr_sender		= set_master;
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	/* if we have an inited client, send result */
19400Sstevel@tonic-gate 	ret = (int *)NULL;
19410Sstevel@tonic-gate 
194211053SSurya.Prakki@Sun.COM 	(void) rw_rdlock(&client_rwlock[setno]);
19430Sstevel@tonic-gate 	if (check_client(setno, sender)) {
19440Sstevel@tonic-gate 		commd_debug(MD_MMV_SYSLOG,
19450Sstevel@tonic-gate 		    "proc_mas: unable to create client for initiator\n");
19460Sstevel@tonic-gate 	} else {
19478452SJohn.Wren.Kennedy@Sun.COM 		ret = mdmn_wakeup_initiator_2(result, client[setno][sender],
19488452SJohn.Wren.Kennedy@Sun.COM 		    sender);
19490Sstevel@tonic-gate 	}
195011053SSurya.Prakki@Sun.COM 	(void) rw_unlock(&client_rwlock[setno]);
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	if (ret == (int *)NULL) {
19530Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_M,
19540Sstevel@tonic-gate 		    "proc_mas: couldn't wakeup initiator\n");
19550Sstevel@tonic-gate 	} else {
19560Sstevel@tonic-gate 		if (*ret != MDMNE_ACK) {
19570Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_M,
19580Sstevel@tonic-gate 			    "proc_mas: wakeup_initiator returned %d\n",
19590Sstevel@tonic-gate 			    *ret);
19600Sstevel@tonic-gate 		}
19610Sstevel@tonic-gate 		free(ret);
19620Sstevel@tonic-gate 	}
19630Sstevel@tonic-gate 
196411053SSurya.Prakki@Sun.COM 	(void) rw_unlock(&set_desc_rwlock[setno]);
19650Sstevel@tonic-gate 	/* Free all submessages, if there were any */
19660Sstevel@tonic-gate 	if (nmsgs > 1) {
19670Sstevel@tonic-gate 		for (curmsg = 0; curmsg < nmsgs; curmsg++) {
19680Sstevel@tonic-gate 			free_msg(msglist[curmsg]);
19690Sstevel@tonic-gate 		}
19700Sstevel@tonic-gate 	}
19710Sstevel@tonic-gate 	/* Free the result */
19720Sstevel@tonic-gate 	free_result(result);
19730Sstevel@tonic-gate 
197411053SSurya.Prakki@Sun.COM 	(void) mutex_lock(&mdmn_busy_mutex[setno]);
19750Sstevel@tonic-gate 	mdmn_mark_class_unbusy(setno, orig_class);
197611053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(&mdmn_busy_mutex[setno]);
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 	/*
19800Sstevel@tonic-gate 	 * We use this ioctl just to get the time in the same format as used in
19810Sstevel@tonic-gate 	 * the messageID. If it fails, all we get is a bad runtime output.
19820Sstevel@tonic-gate 	 */
19830Sstevel@tonic-gate 	(void) metaioctl(MD_IOCGUNIQMSGID, &dummyid, &mde, NULL);
19840Sstevel@tonic-gate 	secdiff = (dummyid.mid_time - msg->msg_msgid.mid_time) >> 32;
19850Sstevel@tonic-gate 	usecdiff = (dummyid.mid_time - msg->msg_msgid.mid_time) & 0xfffff;
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 	/* catching possible overflow */
19880Sstevel@tonic-gate 	if (usecdiff >= 1000000) {
19890Sstevel@tonic-gate 		usecdiff -= 1000000;
19900Sstevel@tonic-gate 		secdiff++;
19910Sstevel@tonic-gate 	}
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	commd_debug(MD_MMV_PROC_M, "proc_mas: done (%d, 0x%llx-%d) type=%02d "
19950Sstevel@tonic-gate 	    "%5d.%06d secs runtime\n",
19960Sstevel@tonic-gate 	    MSGID_ELEMS(msg->msg_msgid), orig_type, secdiff, usecdiff);
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 	/* Free the original message */
19990Sstevel@tonic-gate 	free_msg(msg);
20000Sstevel@tonic-gate }
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate void
mdmn_slave_process_msg(md_mn_msg_t * msg)20030Sstevel@tonic-gate mdmn_slave_process_msg(md_mn_msg_t *msg)
20040Sstevel@tonic-gate {
20050Sstevel@tonic-gate 	int			*ret = NULL;
20060Sstevel@tonic-gate 	int			completed;
20070Sstevel@tonic-gate 	int			retries;
20080Sstevel@tonic-gate 	int			successfully_returned;
20090Sstevel@tonic-gate 	set_t			setno;
20100Sstevel@tonic-gate 	md_mn_result_t		*result;
20110Sstevel@tonic-gate 	md_mn_nodeid_t		sender;
20120Sstevel@tonic-gate 	md_mn_nodeid_t		whoami;
20130Sstevel@tonic-gate 	md_mn_msgtype_t		msgtype;
20140Sstevel@tonic-gate 	md_mn_msgclass_t	class;
20150Sstevel@tonic-gate 
20160Sstevel@tonic-gate 	void (*handler)(md_mn_msg_t *msg, uint_t flags, md_mn_result_t *res);
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 	setno	= msg->msg_setno;
20190Sstevel@tonic-gate 	sender	= msg->msg_sender; /* this is always the master of the set */
20200Sstevel@tonic-gate 	msgtype	= msg->msg_type;
20210Sstevel@tonic-gate 
202211053SSurya.Prakki@Sun.COM 	(void) rw_rdlock(&set_desc_rwlock[setno]);
20230Sstevel@tonic-gate 	whoami		= set_descriptor[setno]->sd_mn_mynode->nd_nodeid;
202411053SSurya.Prakki@Sun.COM 	(void) rw_unlock(&set_desc_rwlock[setno]);
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 	result = Zalloc(sizeof (md_mn_result_t));
20270Sstevel@tonic-gate 	result->mmr_flags	= msg->msg_flags;
20280Sstevel@tonic-gate 	result->mmr_setno	= setno;
20290Sstevel@tonic-gate 	result->mmr_msgtype	= msgtype;
20300Sstevel@tonic-gate 	result->mmr_sender	= whoami;
20310Sstevel@tonic-gate 	result->mmr_comm_state	= MDMNE_ACK; /* Ok state */
20320Sstevel@tonic-gate 	MSGID_COPY(&(msg->msg_msgid), &(result->mmr_msgid));
20330Sstevel@tonic-gate 	class = mdmn_get_message_class(msgtype);
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	commd_debug(MD_MMV_PROC_S,
20360Sstevel@tonic-gate 	    "proc_sla: received (%d, 0x%llx-%d) set=%d, class=%d, type=%d\n",
20370Sstevel@tonic-gate 	    MSGID_ELEMS(msg->msg_msgid), setno, class, msgtype);
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	handler = mdmn_get_handler(msgtype);
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 	if (handler == NULL) {
20420Sstevel@tonic-gate 		result->mmr_exitval = 0;
20430Sstevel@tonic-gate 		/* let the sender decide if this is an error or not */
20440Sstevel@tonic-gate 		result->mmr_comm_state = MDMNE_NO_HANDLER;
20450Sstevel@tonic-gate 		commd_debug(MD_MMV_PROC_S,
20460Sstevel@tonic-gate 		    "proc_sla: No handler for (%d, 0x%llx-%d)\n",
20470Sstevel@tonic-gate 		    MSGID_ELEMS(msg->msg_msgid));
20480Sstevel@tonic-gate 	} else {
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate 		/* Did we already process this message ? */
205111053SSurya.Prakki@Sun.COM 		(void) mutex_lock(&mct_mutex[setno][class]);
20520Sstevel@tonic-gate 		completed = mdmn_check_completion(msg, result);
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 		if (completed == MDMN_MCT_NOT_DONE) {
20550Sstevel@tonic-gate 			/* message not yet processed locally */
20560Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_S,
20570Sstevel@tonic-gate 			    "proc_sla: calling handler for (%d, 0x%llx-%d)\n",
20580Sstevel@tonic-gate 			    MSGID_ELEMS(msg->msg_msgid));
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate 			/*
20610Sstevel@tonic-gate 			 * Mark the message as being currently processed,
20620Sstevel@tonic-gate 			 * so we won't start a second handler for it
20630Sstevel@tonic-gate 			 */
20640Sstevel@tonic-gate 			(void) mdmn_mark_completion(msg, NULL,
20650Sstevel@tonic-gate 			    MDMN_MCT_IN_PROGRESS);
20660Sstevel@tonic-gate 
206711053SSurya.Prakki@Sun.COM 			(void) mutex_unlock(&mct_mutex[setno][class]);
20680Sstevel@tonic-gate 			(*handler)(msg, MD_MSGF_ON_SLAVE, result);
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_S,
20710Sstevel@tonic-gate 			    "proc_sla: finished handler for (%d, 0x%llx-%d)\n",
20720Sstevel@tonic-gate 			    MSGID_ELEMS(msg->msg_msgid));
20730Sstevel@tonic-gate 
207411053SSurya.Prakki@Sun.COM 			(void) mutex_lock(&mct_mutex[setno][class]);
20750Sstevel@tonic-gate 			/* Mark the message as fully done, store the result */
20760Sstevel@tonic-gate 			(void) mdmn_mark_completion(msg, result, MDMN_MCT_DONE);
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 		} else if (completed == MDMN_MCT_DONE) {
20790Sstevel@tonic-gate 			/* message processed previously, got result from MCT */
20800Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_S,
20810Sstevel@tonic-gate 			    "proc_sla: result for (%d, 0x%llx-%d) from MCT\n",
20820Sstevel@tonic-gate 			    MSGID_ELEMS(msg->msg_msgid));
20830Sstevel@tonic-gate 		} else if (completed == MDMN_MCT_IN_PROGRESS) {
20840Sstevel@tonic-gate 			/*
20850Sstevel@tonic-gate 			 * If the message is curruntly being processed,
20860Sstevel@tonic-gate 			 * we can return here, without sending a result back.
20870Sstevel@tonic-gate 			 * This will be done by the initial message handling
20880Sstevel@tonic-gate 			 * thread
20890Sstevel@tonic-gate 			 */
209011053SSurya.Prakki@Sun.COM 			(void) mutex_unlock(&mct_mutex[setno][class]);
20910Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_M, "proc_sla: "
20920Sstevel@tonic-gate 			    "(%d, 0x%llx-%d) is currently being processed\n",
20930Sstevel@tonic-gate 			    MSGID_ELEMS(msg->msg_msgid), msgtype);
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 			free_msg(msg);
20960Sstevel@tonic-gate 			free_result(result);
20970Sstevel@tonic-gate 			return;
20980Sstevel@tonic-gate 		} else {
20990Sstevel@tonic-gate 			/* MCT error occurred (should never happen) */
21000Sstevel@tonic-gate 			result->mmr_comm_state = MDMNE_LOG_FAIL;
21010Sstevel@tonic-gate 			commd_debug(MD_MMV_PROC_S,
21020Sstevel@tonic-gate 			    "proc_sla: MCT error for (%d, 0x%llx-%d)\n",
21030Sstevel@tonic-gate 			    MSGID_ELEMS(msg->msg_msgid));
21040Sstevel@tonic-gate 		}
210511053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&mct_mutex[setno][class]);
21060Sstevel@tonic-gate 	}
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 	/*
21090Sstevel@tonic-gate 	 * At this point we have a result (even in an error case)
21100Sstevel@tonic-gate 	 * that we return to the master.
21110Sstevel@tonic-gate 	 */
211211053SSurya.Prakki@Sun.COM 	(void) rw_rdlock(&set_desc_rwlock[setno]);
21130Sstevel@tonic-gate 	retries = 2; /* we will try two times to send the results */
21140Sstevel@tonic-gate 	successfully_returned = 0;
21150Sstevel@tonic-gate 
21160Sstevel@tonic-gate 	while (!successfully_returned && (retries != 0)) {
21170Sstevel@tonic-gate 		ret = (int *)NULL;
211811053SSurya.Prakki@Sun.COM 		(void) rw_rdlock(&client_rwlock[setno]);
21190Sstevel@tonic-gate 		if (check_client(setno, sender)) {
21200Sstevel@tonic-gate 			/*
21210Sstevel@tonic-gate 			 * If we cannot setup the rpc connection to the master,
21220Sstevel@tonic-gate 			 * we can't do anything besides logging this fact.
21230Sstevel@tonic-gate 			 */
21240Sstevel@tonic-gate 			commd_debug(MD_MMV_SYSLOG,
21250Sstevel@tonic-gate 			    "proc_mas: unable to create client for master\n");
212611053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
21270Sstevel@tonic-gate 			break;
21280Sstevel@tonic-gate 		} else {
21298452SJohn.Wren.Kennedy@Sun.COM 			ret = mdmn_wakeup_master_2(result,
21308452SJohn.Wren.Kennedy@Sun.COM 			    client[setno][sender], sender);
21310Sstevel@tonic-gate 			/*
21328452SJohn.Wren.Kennedy@Sun.COM 			 * if mdmn_wakeup_master_2 returns NULL, it can be that
21330Sstevel@tonic-gate 			 * the master (or the commd on the master) had died.
21340Sstevel@tonic-gate 			 * In that case, we destroy the client to the master
21350Sstevel@tonic-gate 			 * and retry.
21368452SJohn.Wren.Kennedy@Sun.COM 			 * If mdmn_wakeup_master_2 doesn't return MDMNE_ACK,
21370Sstevel@tonic-gate 			 * the commd on the master is alive but
21380Sstevel@tonic-gate 			 * something else is wrong,
21390Sstevel@tonic-gate 			 * in that case a retry doesn't make sense => break out
21400Sstevel@tonic-gate 			 */
21410Sstevel@tonic-gate 			if (ret == (int *)NULL) {
21420Sstevel@tonic-gate 				commd_debug(MD_MMV_PROC_S,
21430Sstevel@tonic-gate 				    "proc_sla: wakeup_master returned NULL\n");
21440Sstevel@tonic-gate 				/* release reader lock, grab writer lock */
214511053SSurya.Prakki@Sun.COM 				(void) rw_unlock(&client_rwlock[setno]);
214611053SSurya.Prakki@Sun.COM 				(void) rw_wrlock(&client_rwlock[setno]);
21470Sstevel@tonic-gate 				mdmn_clnt_destroy(client[setno][sender]);
21480Sstevel@tonic-gate 				if (client[setno][sender] != (CLIENT *)NULL) {
21490Sstevel@tonic-gate 					client[setno][sender] = (CLIENT *)NULL;
21500Sstevel@tonic-gate 				}
215111053SSurya.Prakki@Sun.COM 				(void) rw_unlock(&client_rwlock[setno]);
21520Sstevel@tonic-gate 				retries--;
21530Sstevel@tonic-gate 				commd_debug(MD_MMV_PROC_S,
21540Sstevel@tonic-gate 				    "retries = %d\n", retries);
21550Sstevel@tonic-gate 				continue;
21560Sstevel@tonic-gate 			}
21570Sstevel@tonic-gate 			if (*ret != MDMNE_ACK) {
21580Sstevel@tonic-gate 				commd_debug(MD_MMV_PROC_S, "proc_sla: "
21590Sstevel@tonic-gate 				    "wakeup_master returned %d\n", *ret);
216011053SSurya.Prakki@Sun.COM 				(void) rw_unlock(&client_rwlock[setno]);
21610Sstevel@tonic-gate 				break;
21620Sstevel@tonic-gate 			} else { /* Good case */
21630Sstevel@tonic-gate 				successfully_returned = 1;
216411053SSurya.Prakki@Sun.COM 				(void) rw_unlock(&client_rwlock[setno]);
21650Sstevel@tonic-gate 			}
21660Sstevel@tonic-gate 		}
21670Sstevel@tonic-gate 	}
21680Sstevel@tonic-gate 
216911053SSurya.Prakki@Sun.COM 	(void) rw_unlock(&set_desc_rwlock[setno]);
21700Sstevel@tonic-gate 	commd_debug(MD_MMV_PROC_S, "proc_sla: done (%d, 0x%llx-%d)\n",
21710Sstevel@tonic-gate 	    MSGID_ELEMS(msg->msg_msgid));
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 	if (ret != (int *)NULL)
21740Sstevel@tonic-gate 		free(ret);
21750Sstevel@tonic-gate 	free_msg(msg);
21760Sstevel@tonic-gate 	free_result(result);
21770Sstevel@tonic-gate }
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 
21808452SJohn.Wren.Kennedy@Sun.COM /*
21818452SJohn.Wren.Kennedy@Sun.COM  * mdmn_send_svc_2:
21828452SJohn.Wren.Kennedy@Sun.COM  * ---------------
21838452SJohn.Wren.Kennedy@Sun.COM  * Check that the issuing node is a legitimate one (i.e. is licensed to send
21848452SJohn.Wren.Kennedy@Sun.COM  * messages to us), that the RPC request can be staged.
21858452SJohn.Wren.Kennedy@Sun.COM  *
21868452SJohn.Wren.Kennedy@Sun.COM  * Returns:
21878452SJohn.Wren.Kennedy@Sun.COM  *	0	=> no RPC request is in-flight, no deferred svc_sendreply()
21888452SJohn.Wren.Kennedy@Sun.COM  *	1	=> queued RPC request in-flight. Completion will be made (later)
21898452SJohn.Wren.Kennedy@Sun.COM  *		   by a wakeup_initiator_2() [hopefully]
21908452SJohn.Wren.Kennedy@Sun.COM  */
21918452SJohn.Wren.Kennedy@Sun.COM int
mdmn_send_svc_2(md_mn_msg_t * omsg,struct svc_req * rqstp)21928452SJohn.Wren.Kennedy@Sun.COM mdmn_send_svc_2(md_mn_msg_t *omsg, struct svc_req *rqstp)
21930Sstevel@tonic-gate {
21940Sstevel@tonic-gate 	int			err;
21950Sstevel@tonic-gate 	set_t			setno;
21960Sstevel@tonic-gate 	SVCXPRT			*transp = rqstp->rq_xprt;
21970Sstevel@tonic-gate 	md_mn_msg_t		*msg;
21980Sstevel@tonic-gate 	md_mn_result_t		*resultp;
21990Sstevel@tonic-gate 	md_mn_msgclass_t	class;
22000Sstevel@tonic-gate 	md_mn_msg_and_transp_t	*matp;
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	msg = copy_msg(omsg, NULL);
22030Sstevel@tonic-gate 	xdr_free(xdr_md_mn_msg_t, (caddr_t)omsg);
22040Sstevel@tonic-gate 
22050Sstevel@tonic-gate 	setno = msg->msg_setno;
22060Sstevel@tonic-gate 	class = mdmn_get_message_class(msg->msg_type);
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	/* If we are in the abort state, we error out immediately */
22090Sstevel@tonic-gate 	if (md_commd_global_state & MD_CGS_ABORTED) {
22100Sstevel@tonic-gate 		resultp = Zalloc(sizeof (md_mn_result_t));
22110Sstevel@tonic-gate 		resultp->mmr_comm_state = MDMNE_ABORT;
22120Sstevel@tonic-gate 		mdmn_svc_sendreply(transp, xdr_md_mn_result_t, (char *)resultp);
22130Sstevel@tonic-gate 		free_result(resultp);
22140Sstevel@tonic-gate 		svc_freeargs(transp, xdr_md_mn_msg_t, (caddr_t)msg);
22158452SJohn.Wren.Kennedy@Sun.COM 		return (0);
22160Sstevel@tonic-gate 	}
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 	/* check if the global initialization is done */
22190Sstevel@tonic-gate 	if ((md_commd_global_state & MD_CGS_INITED) == 0) {
22200Sstevel@tonic-gate 		global_init();
22210Sstevel@tonic-gate 	}
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	commd_debug(MD_MMV_SEND,
22240Sstevel@tonic-gate 	    "send: received (%d, 0x%llx-%d), set=%d, class=%d, type=%d\n",
22250Sstevel@tonic-gate 	    MSGID_ELEMS(msg->msg_msgid), setno, class, msg->msg_type);
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	/* Check for verbosity related message */
22280Sstevel@tonic-gate 	if (msg->msg_type == MD_MN_MSG_VERBOSITY) {
22290Sstevel@tonic-gate 		md_mn_verbose_t *d;
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate 		d = (md_mn_verbose_t *)((void *)(msg->msg_event_data));
22320Sstevel@tonic-gate 		md_commd_global_verb = d->mmv_what;
22330Sstevel@tonic-gate 		/* everytime the bitmask is set, we reset the timer */
22340Sstevel@tonic-gate 		__savetime = gethrtime();
22350Sstevel@tonic-gate 		/*
22360Sstevel@tonic-gate 		 * If local-only-flag is set, we are done here,
22370Sstevel@tonic-gate 		 * otherwise we pass that message on to the master.
22380Sstevel@tonic-gate 		 */
22390Sstevel@tonic-gate 		if (msg->msg_flags & MD_MSGF_LOCAL_ONLY) {
22400Sstevel@tonic-gate 			resultp = Zalloc(sizeof (md_mn_result_t));
22410Sstevel@tonic-gate 			resultp->mmr_comm_state = MDMNE_ACK;
22420Sstevel@tonic-gate 			mdmn_svc_sendreply(transp, xdr_md_mn_result_t,
22430Sstevel@tonic-gate 			    (char *)resultp);
22440Sstevel@tonic-gate 			free_result(resultp);
22450Sstevel@tonic-gate 			svc_freeargs(transp, xdr_md_mn_msg_t, (caddr_t)msg);
22468452SJohn.Wren.Kennedy@Sun.COM 			return (0);
22470Sstevel@tonic-gate 		}
22480Sstevel@tonic-gate 	}
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 	/*
22510Sstevel@tonic-gate 	 * Are we entering the abort state?
22520Sstevel@tonic-gate 	 * Here we don't even need to check for MD_MSGF_LOCAL_ONLY, because
22530Sstevel@tonic-gate 	 * this message cannot be distributed anyway.
22540Sstevel@tonic-gate 	 * So, it's safe to return immediately.
22550Sstevel@tonic-gate 	 */
22560Sstevel@tonic-gate 	if (msg->msg_type == MD_MN_MSG_ABORT) {
22570Sstevel@tonic-gate 		md_commd_global_state |= MD_CGS_ABORTED;
22580Sstevel@tonic-gate 		resultp = Zalloc(sizeof (md_mn_result_t));
22590Sstevel@tonic-gate 		resultp->mmr_comm_state = MDMNE_ACK;
22600Sstevel@tonic-gate 		mdmn_svc_sendreply(transp, xdr_md_mn_result_t, (char *)resultp);
22610Sstevel@tonic-gate 		free_result(resultp);
22620Sstevel@tonic-gate 		svc_freeargs(transp, xdr_md_mn_msg_t, (caddr_t)msg);
22638452SJohn.Wren.Kennedy@Sun.COM 		return (0);
22640Sstevel@tonic-gate 	}
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	/*
22680Sstevel@tonic-gate 	 * Is this message type blocked?
22690Sstevel@tonic-gate 	 * If so we return MDMNE_CLASS_LOCKED, immediately
22700Sstevel@tonic-gate 	 */
22710Sstevel@tonic-gate 	if (msgtype_lock_state[msg->msg_type] == MMTL_LOCK) {
22720Sstevel@tonic-gate 		resultp = Zalloc(sizeof (md_mn_result_t));
22730Sstevel@tonic-gate 		resultp->mmr_comm_state = MDMNE_CLASS_LOCKED;
22740Sstevel@tonic-gate 		mdmn_svc_sendreply(transp, xdr_md_mn_result_t, (char *)resultp);
22750Sstevel@tonic-gate 		free_result(resultp);
22760Sstevel@tonic-gate 		svc_freeargs(transp, xdr_md_mn_msg_t, (caddr_t)msg);
22770Sstevel@tonic-gate 		commd_debug(MD_MMV_SEND,
22788452SJohn.Wren.Kennedy@Sun.COM 		    "send: type locked (%d, 0x%llx-%d), set=%d, class=%d, "
22798452SJohn.Wren.Kennedy@Sun.COM 		    "type=%d\n", MSGID_ELEMS(msg->msg_msgid), setno, class,
22808452SJohn.Wren.Kennedy@Sun.COM 		    msg->msg_type);
22818452SJohn.Wren.Kennedy@Sun.COM 		return (0);
22820Sstevel@tonic-gate 	}
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 	if (md_mn_set_inited[setno] != MDMN_SET_READY) {
22860Sstevel@tonic-gate 		/* Can only use the appropriate mutexes if they are inited */
22870Sstevel@tonic-gate 		if (md_mn_set_inited[setno] & MDMN_SET_MUTEXES) {
228811053SSurya.Prakki@Sun.COM 			(void) rw_wrlock(&set_desc_rwlock[setno]);
228911053SSurya.Prakki@Sun.COM 			(void) rw_wrlock(&client_rwlock[setno]);
22900Sstevel@tonic-gate 			err = mdmn_init_set(setno, MDMN_SET_READY);
229111053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
229211053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&set_desc_rwlock[setno]);
22930Sstevel@tonic-gate 		} else {
22940Sstevel@tonic-gate 			err = mdmn_init_set(setno, MDMN_SET_READY);
22950Sstevel@tonic-gate 		}
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 		if (err) {
22980Sstevel@tonic-gate 			/* couldn't initialize connections, cannot proceed */
22990Sstevel@tonic-gate 			resultp = Zalloc(sizeof (md_mn_result_t));
23000Sstevel@tonic-gate 			resultp->mmr_comm_state = err;
23010Sstevel@tonic-gate 			mdmn_svc_sendreply(transp, xdr_md_mn_result_t,
23020Sstevel@tonic-gate 			    (char *)resultp);
23030Sstevel@tonic-gate 			svc_freeargs(transp, xdr_md_mn_msg_t, (caddr_t)msg);
23040Sstevel@tonic-gate 			free_result(resultp);
23050Sstevel@tonic-gate 			commd_debug(MD_MMV_SEND,
23060Sstevel@tonic-gate 			    "send: init err = %d\n", err);
23078452SJohn.Wren.Kennedy@Sun.COM 			return (0);
23080Sstevel@tonic-gate 		}
23090Sstevel@tonic-gate 	}
23100Sstevel@tonic-gate 
231111053SSurya.Prakki@Sun.COM 	(void) mutex_lock(&mdmn_busy_mutex[setno]);
23120Sstevel@tonic-gate 	if ((mdmn_is_class_suspended(setno, class) == TRUE) &&
23130Sstevel@tonic-gate 	    ((msg->msg_flags & MD_MSGF_OVERRIDE_SUSPEND) == 0)) {
231411053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&mdmn_busy_mutex[setno]);
23150Sstevel@tonic-gate 		resultp = Zalloc(sizeof (md_mn_result_t));
23160Sstevel@tonic-gate 		resultp->mmr_comm_state = MDMNE_SUSPENDED;
23170Sstevel@tonic-gate 		mdmn_svc_sendreply(transp, xdr_md_mn_result_t, (char *)resultp);
23180Sstevel@tonic-gate 		svc_freeargs(transp, xdr_md_mn_msg_t, (caddr_t)msg);
23190Sstevel@tonic-gate 		free_result(resultp);
23200Sstevel@tonic-gate 		commd_debug(MD_MMV_SEND,
23218452SJohn.Wren.Kennedy@Sun.COM 		    "send: class suspended (%d, 0x%llx-%d), set=%d, "
23228452SJohn.Wren.Kennedy@Sun.COM 		    "class=%d, type=%d\n", MSGID_ELEMS(msg->msg_msgid),
23238452SJohn.Wren.Kennedy@Sun.COM 		    setno, class, msg->msg_type);
23248452SJohn.Wren.Kennedy@Sun.COM 		return (0);
23250Sstevel@tonic-gate 	}
232611053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(&mdmn_busy_mutex[setno]);
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 	/* is this rpc request coming from the local node? */
23290Sstevel@tonic-gate 	if (check_license(rqstp, 0) == FALSE) {
23300Sstevel@tonic-gate 		svc_freeargs(transp, xdr_md_mn_msg_t, (caddr_t)msg);
23310Sstevel@tonic-gate 		commd_debug(MD_MMV_SEND,
23328452SJohn.Wren.Kennedy@Sun.COM 		    "send: check licence fail(%d, 0x%llx-%d), set=%d, "
23338452SJohn.Wren.Kennedy@Sun.COM 		    "class=%d, type=%d\n", MSGID_ELEMS(msg->msg_msgid),
23348452SJohn.Wren.Kennedy@Sun.COM 		    setno, class, msg->msg_type);
23358452SJohn.Wren.Kennedy@Sun.COM 		return (0);
23360Sstevel@tonic-gate 	}
23370Sstevel@tonic-gate 
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 	/*
23400Sstevel@tonic-gate 	 * We allocate a structure that can take two pointers in order to pass
23410Sstevel@tonic-gate 	 * both the message and the transp into thread_create.
23420Sstevel@tonic-gate 	 * The free for this alloc is done in mdmn_send_to_work()
23430Sstevel@tonic-gate 	 */
23440Sstevel@tonic-gate 	matp = Malloc(sizeof (md_mn_msg_and_transp_t));
23450Sstevel@tonic-gate 	matp->mat_msg = msg;
23460Sstevel@tonic-gate 	matp->mat_transp = transp;
23470Sstevel@tonic-gate 
23480Sstevel@tonic-gate 	/*
23490Sstevel@tonic-gate 	 * create a thread here that calls work on the master.
23500Sstevel@tonic-gate 	 * If we are already on the master, this would block if running
23510Sstevel@tonic-gate 	 * in the same context. (our service is single threaded)(
23520Sstevel@tonic-gate 	 * Make it a detached thread because it will not communicate with
23530Sstevel@tonic-gate 	 * anybody thru thr_* mechanisms
23540Sstevel@tonic-gate 	 */
235511053SSurya.Prakki@Sun.COM 	(void) thr_create(NULL, 0, mdmn_send_to_work, (void *) matp,
235611053SSurya.Prakki@Sun.COM 	    THR_DETACHED, NULL);
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 	commd_debug(MD_MMV_SEND, "send: done (%d, 0x%llx-%d)\n",
23590Sstevel@tonic-gate 	    MSGID_ELEMS(msg->msg_msgid));
23600Sstevel@tonic-gate 	/*
23610Sstevel@tonic-gate 	 * We return here without sending results. This will be done by
23628452SJohn.Wren.Kennedy@Sun.COM 	 * mdmn_wakeup_initiator_svc_2() as soon as the results are available.
23630Sstevel@tonic-gate 	 * Until then the calling send_message will be blocked, while we
23640Sstevel@tonic-gate 	 * are able to take calls.
23650Sstevel@tonic-gate 	 */
23660Sstevel@tonic-gate 
23678452SJohn.Wren.Kennedy@Sun.COM 	return (1);
23680Sstevel@tonic-gate }
23690Sstevel@tonic-gate 
23700Sstevel@tonic-gate /* ARGSUSED */
23710Sstevel@tonic-gate int *
mdmn_work_svc_2(md_mn_msg_t * omsg,struct svc_req * rqstp)23728452SJohn.Wren.Kennedy@Sun.COM mdmn_work_svc_2(md_mn_msg_t *omsg, struct svc_req *rqstp)
23730Sstevel@tonic-gate {
23740Sstevel@tonic-gate 	int		err;
23750Sstevel@tonic-gate 	set_t		setno;
23760Sstevel@tonic-gate 	thread_t	tid;
23770Sstevel@tonic-gate 	int		*retval;
23780Sstevel@tonic-gate 	md_mn_msg_t	*msg;
23790Sstevel@tonic-gate 	md_mn_msgclass_t class;
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	retval = Malloc(sizeof (int));
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	/* If we are in the abort state, we error out immediately */
23840Sstevel@tonic-gate 	if (md_commd_global_state & MD_CGS_ABORTED) {
23850Sstevel@tonic-gate 	xdr_free(xdr_md_mn_msg_t, (caddr_t)omsg);
23860Sstevel@tonic-gate 		*retval = MDMNE_ABORT;
23870Sstevel@tonic-gate 		return (retval);
23880Sstevel@tonic-gate 	}
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate 	msg = copy_msg(omsg, NULL);
23910Sstevel@tonic-gate 	xdr_free(xdr_md_mn_msg_t, (caddr_t)omsg);
23920Sstevel@tonic-gate 
23930Sstevel@tonic-gate 	/*
23940Sstevel@tonic-gate 	 * Is this message type blocked?
23950Sstevel@tonic-gate 	 * If so we return MDMNE_CLASS_LOCKED, immediately.
23960Sstevel@tonic-gate 	 * This check is performed on master and slave.
23970Sstevel@tonic-gate 	 */
23980Sstevel@tonic-gate 	if (msgtype_lock_state[msg->msg_type] == MMTL_LOCK) {
23990Sstevel@tonic-gate 		*retval = MDMNE_CLASS_LOCKED;
24000Sstevel@tonic-gate 		return (retval);
24010Sstevel@tonic-gate 	}
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 	/* check if the global initialization is done */
24040Sstevel@tonic-gate 	if ((md_commd_global_state & MD_CGS_INITED) == 0) {
24050Sstevel@tonic-gate 		global_init();
24060Sstevel@tonic-gate 	}
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	class = mdmn_get_message_class(msg->msg_type);
24090Sstevel@tonic-gate 	setno = msg->msg_setno;
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate 	if (md_mn_set_inited[setno] != MDMN_SET_READY) {
24120Sstevel@tonic-gate 		/* Can only use the appropriate mutexes if they are inited */
24130Sstevel@tonic-gate 		if (md_mn_set_inited[setno] & MDMN_SET_MUTEXES) {
241411053SSurya.Prakki@Sun.COM 			(void) rw_wrlock(&set_desc_rwlock[setno]);
241511053SSurya.Prakki@Sun.COM 			(void) rw_wrlock(&client_rwlock[setno]);
24160Sstevel@tonic-gate 			err = mdmn_init_set(setno, MDMN_SET_READY);
241711053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
241811053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&set_desc_rwlock[setno]);
24190Sstevel@tonic-gate 		} else {
24200Sstevel@tonic-gate 			err = mdmn_init_set(setno, MDMN_SET_READY);
24210Sstevel@tonic-gate 		}
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 		if (err) {
24240Sstevel@tonic-gate 			*retval = MDMNE_CANNOT_CONNECT;
24250Sstevel@tonic-gate 			free_msg(msg);
24260Sstevel@tonic-gate 			return (retval);
24270Sstevel@tonic-gate 		}
24280Sstevel@tonic-gate 	}
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 	/* is this rpc request coming from a licensed node? */
24310Sstevel@tonic-gate 	if (check_license(rqstp, msg->msg_sender) == FALSE) {
24320Sstevel@tonic-gate 		free_msg(msg);
24330Sstevel@tonic-gate 		*retval = MDMNE_RPC_FAIL;
24340Sstevel@tonic-gate 		return (retval);
24350Sstevel@tonic-gate 	}
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 	commd_debug(MD_MMV_WORK,
24380Sstevel@tonic-gate 	    "work: received (%d, 0x%llx-%d), set=%d, class=%d, type=%d, "
24390Sstevel@tonic-gate 	    "flags=0x%x\n",
24400Sstevel@tonic-gate 	    MSGID_ELEMS(msg->msg_msgid), setno, class, msg->msg_type,
24410Sstevel@tonic-gate 	    msg->msg_flags);
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	/* Check for various CLASS0 message types */
24440Sstevel@tonic-gate 	if (msg->msg_type == MD_MN_MSG_VERBOSITY) {
24450Sstevel@tonic-gate 		md_mn_verbose_t *d;
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 		d = (md_mn_verbose_t *)((void *)(msg->msg_event_data));
24480Sstevel@tonic-gate 		/* for now we ignore set / class in md_mn_verbose_t */
24490Sstevel@tonic-gate 		md_commd_global_verb = d->mmv_what;
24500Sstevel@tonic-gate 		/* everytime the bitmask is set, we reset the timer */
24510Sstevel@tonic-gate 		__savetime = gethrtime();
24520Sstevel@tonic-gate 	}
24530Sstevel@tonic-gate 
245411053SSurya.Prakki@Sun.COM 	(void) mutex_lock(&mdmn_busy_mutex[setno]);
24550Sstevel@tonic-gate 
24568452SJohn.Wren.Kennedy@Sun.COM 	/* check if class is locked via a call to mdmn_comm_lock_svc_2 */
24570Sstevel@tonic-gate 	if (mdmn_is_class_locked(setno, class) == TRUE) {
245811053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&mdmn_busy_mutex[setno]);
24590Sstevel@tonic-gate 		*retval = MDMNE_CLASS_LOCKED;
24600Sstevel@tonic-gate 		free_msg(msg);
24610Sstevel@tonic-gate 		return (retval);
24620Sstevel@tonic-gate 	}
246311053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(&mdmn_busy_mutex[setno]);
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 	/* Check if the class is busy right now. Do it only on the master */
246611053SSurya.Prakki@Sun.COM 	(void) rw_rdlock(&set_desc_rwlock[setno]);
24670Sstevel@tonic-gate 	if (set_descriptor[setno]->sd_mn_am_i_master) {
246811053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&set_desc_rwlock[setno]);
24690Sstevel@tonic-gate 		/*
24700Sstevel@tonic-gate 		 * If the class is currently suspended, don't accept new
24710Sstevel@tonic-gate 		 * messages, unless they are flagged with an override bit.
24720Sstevel@tonic-gate 		 */
247311053SSurya.Prakki@Sun.COM 		(void) mutex_lock(&mdmn_busy_mutex[setno]);
24740Sstevel@tonic-gate 		if ((mdmn_is_class_suspended(setno, class) == TRUE) &&
24750Sstevel@tonic-gate 		    ((msg->msg_flags & MD_MSGF_OVERRIDE_SUSPEND) == 0)) {
247611053SSurya.Prakki@Sun.COM 			(void) mutex_unlock(&mdmn_busy_mutex[setno]);
24770Sstevel@tonic-gate 			*retval = MDMNE_SUSPENDED;
24780Sstevel@tonic-gate 			commd_debug(MD_MMV_SEND,
24790Sstevel@tonic-gate 			    "send: set %d is suspended\n", setno);
24800Sstevel@tonic-gate 			free_msg(msg);
24810Sstevel@tonic-gate 			return (retval);
24820Sstevel@tonic-gate 		}
24830Sstevel@tonic-gate 		if (mdmn_mark_class_busy(setno, class) == FALSE) {
248411053SSurya.Prakki@Sun.COM 			(void) mutex_unlock(&mdmn_busy_mutex[setno]);
24850Sstevel@tonic-gate 			*retval = MDMNE_CLASS_BUSY;
24860Sstevel@tonic-gate 			free_msg(msg);
24870Sstevel@tonic-gate 			return (retval);
24880Sstevel@tonic-gate 		}
248911053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&mdmn_busy_mutex[setno]);
24900Sstevel@tonic-gate 		/*
24910Sstevel@tonic-gate 		 * Because the real processing of the message takes time we
24920Sstevel@tonic-gate 		 * create a thread for it. So the master thread can continue
24930Sstevel@tonic-gate 		 * to run and accept further messages.
24940Sstevel@tonic-gate 		 */
24950Sstevel@tonic-gate 		*retval = thr_create(NULL, 0,
24960Sstevel@tonic-gate 		    (void *(*)(void *))mdmn_master_process_msg, (void *)msg,
24970Sstevel@tonic-gate 		    THR_DETACHED|THR_SUSPENDED, &tid);
24980Sstevel@tonic-gate 	} else {
249911053SSurya.Prakki@Sun.COM 		(void) rw_unlock(&set_desc_rwlock[setno]);
25000Sstevel@tonic-gate 		*retval = thr_create(NULL, 0,
25010Sstevel@tonic-gate 		    (void *(*)(void *)) mdmn_slave_process_msg, (void *)msg,
25020Sstevel@tonic-gate 		    THR_DETACHED|THR_SUSPENDED, &tid);
25030Sstevel@tonic-gate 	}
25040Sstevel@tonic-gate 
25050Sstevel@tonic-gate 	if (*retval != 0) {
25060Sstevel@tonic-gate 		*retval = MDMNE_THR_CREATE_FAIL;
25070Sstevel@tonic-gate 		free_msg(msg);
25080Sstevel@tonic-gate 		return (retval);
25090Sstevel@tonic-gate 	}
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate 	/* Now run the new thread */
251211053SSurya.Prakki@Sun.COM 	(void) thr_continue(tid);
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 	commd_debug(MD_MMV_WORK,
25150Sstevel@tonic-gate 	    "work: done (%d, 0x%llx-%d), set=%d, class=%d, type=%d\n",
25160Sstevel@tonic-gate 	    MSGID_ELEMS(msg->msg_msgid), setno, class, msg->msg_type);
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate 	*retval = MDMNE_ACK; /* this means success */
25190Sstevel@tonic-gate 	return (retval);
25200Sstevel@tonic-gate }
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate /* ARGSUSED */
25230Sstevel@tonic-gate int *
mdmn_wakeup_initiator_svc_2(md_mn_result_t * res,struct svc_req * rqstp)25248452SJohn.Wren.Kennedy@Sun.COM mdmn_wakeup_initiator_svc_2(md_mn_result_t *res, struct svc_req *rqstp)
25250Sstevel@tonic-gate {
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 	int		*retval;
25280Sstevel@tonic-gate 	int		err;
25290Sstevel@tonic-gate 	set_t		setno;
25300Sstevel@tonic-gate 	mutex_t		*mx;   /* protection of initiator_table */
25318452SJohn.Wren.Kennedy@Sun.COM 	SVCXPRT		*transp = NULL;
25320Sstevel@tonic-gate 	md_mn_msgid_t	initiator_table_id;
25330Sstevel@tonic-gate 	md_mn_msgclass_t class;
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	retval = Malloc(sizeof (int));
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 	/* check if the global initialization is done */
25380Sstevel@tonic-gate 	if ((md_commd_global_state & MD_CGS_INITED) == 0) {
25390Sstevel@tonic-gate 		global_init();
25400Sstevel@tonic-gate 	}
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	setno	= res->mmr_setno;
25430Sstevel@tonic-gate 
25440Sstevel@tonic-gate 	if (md_mn_set_inited[setno] != MDMN_SET_READY) {
25450Sstevel@tonic-gate 		/* set not ready means we just crashed are restarted now */
25460Sstevel@tonic-gate 		/* Can only use the appropriate mutexes if they are inited */
25470Sstevel@tonic-gate 		if (md_mn_set_inited[setno] & MDMN_SET_MUTEXES) {
254811053SSurya.Prakki@Sun.COM 			(void) rw_wrlock(&set_desc_rwlock[setno]);
254911053SSurya.Prakki@Sun.COM 			(void) rw_wrlock(&client_rwlock[setno]);
25500Sstevel@tonic-gate 			err = mdmn_init_set(setno, MDMN_SET_READY);
255111053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
255211053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&set_desc_rwlock[setno]);
25530Sstevel@tonic-gate 		} else {
25540Sstevel@tonic-gate 			err = mdmn_init_set(setno, MDMN_SET_READY);
25550Sstevel@tonic-gate 		}
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 		if (err) {
25580Sstevel@tonic-gate 			*retval = MDMNE_CANNOT_CONNECT;
25590Sstevel@tonic-gate 			xdr_free(xdr_md_mn_result_t, (caddr_t)res);
25600Sstevel@tonic-gate 			return (retval);
25610Sstevel@tonic-gate 		}
25620Sstevel@tonic-gate 	}
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	/* is this rpc request coming from a licensed node? */
25650Sstevel@tonic-gate 	if (check_license(rqstp, res->mmr_sender) == FALSE) {
25660Sstevel@tonic-gate 		xdr_free(xdr_md_mn_result_t, (caddr_t)res);
25670Sstevel@tonic-gate 		*retval = MDMNE_RPC_FAIL;
25680Sstevel@tonic-gate 		return (retval);
25690Sstevel@tonic-gate 	}
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 
25720Sstevel@tonic-gate 	class	= mdmn_get_message_class(res->mmr_msgtype);
25730Sstevel@tonic-gate 	mx	= mdmn_get_initiator_table_mx(setno, class);
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 	commd_debug(MD_MMV_WAKE_I,
25760Sstevel@tonic-gate 	    "wake_ini: received (%d, 0x%llx-%d) set=%d, class=%d, type=%d\n",
25770Sstevel@tonic-gate 	    MSGID_ELEMS(res->mmr_msgid), setno, class, res->mmr_msgtype);
25780Sstevel@tonic-gate 
257911053SSurya.Prakki@Sun.COM 	(void) mutex_lock(mx);
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	/*
25820Sstevel@tonic-gate 	 * Search the initiator wakeup table.
25830Sstevel@tonic-gate 	 * If we find an entry here (which should always be true)
25840Sstevel@tonic-gate 	 * we are on the initiating node and we wakeup the original
25858452SJohn.Wren.Kennedy@Sun.COM 	 * local rpc call.
25860Sstevel@tonic-gate 	 */
25870Sstevel@tonic-gate 	mdmn_get_initiator_table_id(setno, class, &initiator_table_id);
25880Sstevel@tonic-gate 
25890Sstevel@tonic-gate 	if (MSGID_CMP(&(initiator_table_id), &(res->mmr_msgid))) {
25900Sstevel@tonic-gate 		transp = mdmn_get_initiator_table_transp(setno, class);
25910Sstevel@tonic-gate 		mdmn_svc_sendreply(transp, xdr_md_mn_result_t, (char *)res);
25928452SJohn.Wren.Kennedy@Sun.COM 		svc_done(transp);
25930Sstevel@tonic-gate 		mdmn_unregister_initiator_table(setno, class);
25940Sstevel@tonic-gate 		*retval = MDMNE_ACK;
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate 		commd_debug(MD_MMV_WAKE_I,
25970Sstevel@tonic-gate 		    "wake_ini: replied (%d, 0x%llx-%d)\n",
25980Sstevel@tonic-gate 		    MSGID_ELEMS(res->mmr_msgid));
25990Sstevel@tonic-gate 	} else {
26000Sstevel@tonic-gate 		commd_debug(MD_MMV_WAKE_I,
26010Sstevel@tonic-gate 		    "wakeup initiator: unsolicited message (%d, 0x%llx-%d)\n",
26020Sstevel@tonic-gate 		    MSGID_ELEMS(res->mmr_msgid));
26030Sstevel@tonic-gate 		*retval = MDMNE_NO_WAKEUP_ENTRY;
26040Sstevel@tonic-gate 	}
260511053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(mx);
26060Sstevel@tonic-gate 	/* less work for check_timeouts */
260711053SSurya.Prakki@Sun.COM 	(void) mutex_lock(&check_timeout_mutex);
26080Sstevel@tonic-gate 	if (messages_on_their_way == 0) {
26090Sstevel@tonic-gate 		commd_debug(MD_MMV_WAKE_I,
26100Sstevel@tonic-gate 		    "Oops, messages_on_their_way < 0 (%d, 0x%llx-%d)\n",
26110Sstevel@tonic-gate 		    MSGID_ELEMS(res->mmr_msgid));
26120Sstevel@tonic-gate 	} else {
26130Sstevel@tonic-gate 		messages_on_their_way--;
26140Sstevel@tonic-gate 	}
261511053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(&check_timeout_mutex);
26160Sstevel@tonic-gate 	xdr_free(xdr_md_mn_result_t, (caddr_t)res);
26170Sstevel@tonic-gate 
26180Sstevel@tonic-gate 	return (retval);
26190Sstevel@tonic-gate }
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 
26220Sstevel@tonic-gate /*
26230Sstevel@tonic-gate  * res must be free'd by the thread we wake up
26240Sstevel@tonic-gate  */
26250Sstevel@tonic-gate /* ARGSUSED */
26260Sstevel@tonic-gate int *
mdmn_wakeup_master_svc_2(md_mn_result_t * ores,struct svc_req * rqstp)26278452SJohn.Wren.Kennedy@Sun.COM mdmn_wakeup_master_svc_2(md_mn_result_t *ores, struct svc_req *rqstp)
26280Sstevel@tonic-gate {
26290Sstevel@tonic-gate 
26300Sstevel@tonic-gate 	int		*retval;
26310Sstevel@tonic-gate 	int		err;
26320Sstevel@tonic-gate 	set_t		setno;
26330Sstevel@tonic-gate 	cond_t		*cv;
26340Sstevel@tonic-gate 	mutex_t		*mx;
26350Sstevel@tonic-gate 	md_mn_msgid_t	master_table_id;
26360Sstevel@tonic-gate 	md_mn_nodeid_t	sender;
26370Sstevel@tonic-gate 	md_mn_result_t	*res;
26380Sstevel@tonic-gate 	md_mn_msgclass_t class;
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 	retval = Malloc(sizeof (int));
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	/* check if the global initialization is done */
26430Sstevel@tonic-gate 	if ((md_commd_global_state & MD_CGS_INITED) == 0) {
26440Sstevel@tonic-gate 		global_init();
26450Sstevel@tonic-gate 	}
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 	/* Need to copy the results here, as they are static for RPC */
26480Sstevel@tonic-gate 	res = copy_result(ores);
26490Sstevel@tonic-gate 	xdr_free(xdr_md_mn_result_t, (caddr_t)ores);
26500Sstevel@tonic-gate 
26510Sstevel@tonic-gate 	class = mdmn_get_message_class(res->mmr_msgtype);
26520Sstevel@tonic-gate 	setno = res->mmr_setno;
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 	if (md_mn_set_inited[setno] != MDMN_SET_READY) {
26550Sstevel@tonic-gate 		/* set not ready means we just crashed are restarted now */
26560Sstevel@tonic-gate 		/* Can only use the appropriate mutexes if they are inited */
26570Sstevel@tonic-gate 		if (md_mn_set_inited[setno] & MDMN_SET_MUTEXES) {
265811053SSurya.Prakki@Sun.COM 			(void) rw_wrlock(&set_desc_rwlock[setno]);
265911053SSurya.Prakki@Sun.COM 			(void) rw_wrlock(&client_rwlock[setno]);
26600Sstevel@tonic-gate 			err = mdmn_init_set(setno, MDMN_SET_READY);
266111053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&client_rwlock[setno]);
266211053SSurya.Prakki@Sun.COM 			(void) rw_unlock(&set_desc_rwlock[setno]);
26630Sstevel@tonic-gate 		} else {
26640Sstevel@tonic-gate 			err = mdmn_init_set(setno, MDMN_SET_READY);
26650Sstevel@tonic-gate 		}
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 		if (err) {
26680Sstevel@tonic-gate 			*retval = MDMNE_CANNOT_CONNECT;
26690Sstevel@tonic-gate 			xdr_free(xdr_md_mn_result_t, (caddr_t)res);
26700Sstevel@tonic-gate 			return (retval);
26710Sstevel@tonic-gate 		}
26720Sstevel@tonic-gate 	}
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 	/* is this rpc request coming from a licensed node? */
26750Sstevel@tonic-gate 	if (check_license(rqstp, res->mmr_sender) == FALSE) {
26760Sstevel@tonic-gate 		*retval = MDMNE_RPC_FAIL;
26770Sstevel@tonic-gate 		xdr_free(xdr_md_mn_result_t, (caddr_t)res);
26780Sstevel@tonic-gate 		return (retval);
26790Sstevel@tonic-gate 	}
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate 
26820Sstevel@tonic-gate 	commd_debug(MD_MMV_WAKE_M,
26830Sstevel@tonic-gate 	    "wake_mas: received (%d, 0x%llx-%d) set=%d, class=%d, type=%d "
26840Sstevel@tonic-gate 	    "from %d\n",
26850Sstevel@tonic-gate 	    MSGID_ELEMS(res->mmr_msgid), setno, class, res->mmr_msgtype,
26860Sstevel@tonic-gate 	    res->mmr_sender);
26870Sstevel@tonic-gate 	/*
26880Sstevel@tonic-gate 	 * The mutex and cv are needed for waking up the thread
26890Sstevel@tonic-gate 	 * sleeping in mdmn_master_process_msg()
26900Sstevel@tonic-gate 	 */
26910Sstevel@tonic-gate 	mx = mdmn_get_master_table_mx(setno, class);
26920Sstevel@tonic-gate 	cv = mdmn_get_master_table_cv(setno, class);
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 	/*
26950Sstevel@tonic-gate 	 * lookup the master wakeup table
26960Sstevel@tonic-gate 	 * If we find our message, we are on the master and
26970Sstevel@tonic-gate 	 * called by a slave that finished processing a message.
26980Sstevel@tonic-gate 	 * We store the results in the appropriate slot and
26990Sstevel@tonic-gate 	 * wakeup the thread (mdmn_master_process_msg()) waiting for them.
27000Sstevel@tonic-gate 	 */
270111053SSurya.Prakki@Sun.COM 	(void) mutex_lock(mx);
27020Sstevel@tonic-gate 	mdmn_get_master_table_id(setno, class, &master_table_id);
27030Sstevel@tonic-gate 	sender = mdmn_get_master_table_addr(setno, class);
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate 	if (MSGID_CMP(&(master_table_id), &(res->mmr_msgid))) {
27060Sstevel@tonic-gate 		if (sender == res->mmr_sender) {
27070Sstevel@tonic-gate 			mdmn_set_master_table_res(setno, class, res);
270811053SSurya.Prakki@Sun.COM 			(void) cond_signal(cv);
27090Sstevel@tonic-gate 			*retval = MDMNE_ACK;
27100Sstevel@tonic-gate 		} else {
27110Sstevel@tonic-gate 			/* id is correct but wrong sender (I smell a timeout) */
27120Sstevel@tonic-gate 			commd_debug(MD_MMV_WAKE_M,
27130Sstevel@tonic-gate 			    "wakeup master got unsolicited message: "
27140Sstevel@tonic-gate 			    "(%d, 0x%llx-%d) from %d\n",
27150Sstevel@tonic-gate 			    MSGID_ELEMS(res->mmr_msgid), res->mmr_sender);
27160Sstevel@tonic-gate 			free_result(res);
27170Sstevel@tonic-gate 			*retval = MDMNE_TIMEOUT;
27180Sstevel@tonic-gate 		}
27190Sstevel@tonic-gate 	} else {
27200Sstevel@tonic-gate 		/* id is wrong, smells like a very late timeout */
27210Sstevel@tonic-gate 		commd_debug(MD_MMV_WAKE_M,
27220Sstevel@tonic-gate 		    "wakeup master got unsolicited message: "
27230Sstevel@tonic-gate 		    "(%d, 0x%llx-%d) from %d, expected (%d, 0x%llx-%d)\n",
27240Sstevel@tonic-gate 		    MSGID_ELEMS(res->mmr_msgid), res->mmr_sender,
27250Sstevel@tonic-gate 		    MSGID_ELEMS(master_table_id));
27260Sstevel@tonic-gate 		free_result(res);
27270Sstevel@tonic-gate 		*retval = MDMNE_NO_WAKEUP_ENTRY;
27280Sstevel@tonic-gate 	}
27290Sstevel@tonic-gate 
273011053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(mx);
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	return (retval);
27330Sstevel@tonic-gate }
27340Sstevel@tonic-gate 
27350Sstevel@tonic-gate /*
27360Sstevel@tonic-gate  * Lock a set/class combination.
27370Sstevel@tonic-gate  * This is mainly done for debug purpose.
27380Sstevel@tonic-gate  * This set/class combination immediately is blocked,
27390Sstevel@tonic-gate  * even in the middle of sending messages to multiple slaves.
27408452SJohn.Wren.Kennedy@Sun.COM  * This remains until the user issues a mdmn_comm_unlock_svc_2 for the same
27410Sstevel@tonic-gate  * set/class combination.
27420Sstevel@tonic-gate  *
27430Sstevel@tonic-gate  * Special messages of class MD_MSG_CLASS0 can never be locked.
27440Sstevel@tonic-gate  * 	e.g. MD_MN_MSG_VERBOSITY, MD_MN_MSG_ABORT
27450Sstevel@tonic-gate  *
27460Sstevel@tonic-gate  * That means, if MD_MSG_CLASS0 is specified, we lock all classes from
27470Sstevel@tonic-gate  * >= MD_MSG_CLASS1 to < MD_MN_NCLASSES
27480Sstevel@tonic-gate  *
27490Sstevel@tonic-gate  * set must be between 1 and MD_MAXSETS
27500Sstevel@tonic-gate  * class can be:
27510Sstevel@tonic-gate  *	MD_MSG_CLASS0 which means all other classes in this case
27520Sstevel@tonic-gate  *	or one specific class (< MD_MN_NCLASSES)
27530Sstevel@tonic-gate  *
27540Sstevel@tonic-gate  * Returns:
27550Sstevel@tonic-gate  *	MDMNE_ACK on sucess (locking a locked class is Ok)
27560Sstevel@tonic-gate  *	MDMNE_EINVAL if a parameter is out of range
27570Sstevel@tonic-gate  */
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate /* ARGSUSED */
27600Sstevel@tonic-gate int *
mdmn_comm_lock_svc_2(md_mn_set_and_class_t * msc,struct svc_req * rqstp)27618452SJohn.Wren.Kennedy@Sun.COM mdmn_comm_lock_svc_2(md_mn_set_and_class_t *msc, struct svc_req *rqstp)
27620Sstevel@tonic-gate {
27630Sstevel@tonic-gate 	int			*retval;
27640Sstevel@tonic-gate 	set_t			setno = msc->msc_set;
27650Sstevel@tonic-gate 	md_mn_msgclass_t	class = msc->msc_class;
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 	retval = Malloc(sizeof (int));
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate 	/* check if the global initialization is done */
27700Sstevel@tonic-gate 	if ((md_commd_global_state & MD_CGS_INITED) == 0) {
27710Sstevel@tonic-gate 		global_init();
27720Sstevel@tonic-gate 	}
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate 	/* is this rpc request coming from the local node ? */
27750Sstevel@tonic-gate 	if (check_license(rqstp, 0) == FALSE) {
27760Sstevel@tonic-gate 		xdr_free(xdr_md_mn_set_and_class_t, (caddr_t)msc);
27770Sstevel@tonic-gate 		*retval = MDMNE_RPC_FAIL;
27780Sstevel@tonic-gate 		return (retval);
27790Sstevel@tonic-gate 	}
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 	/* Perform some range checking */
27820Sstevel@tonic-gate 	if ((setno == 0) || (setno >= MD_MAXSETS) ||
27830Sstevel@tonic-gate 	    (class < MD_MSG_CLASS0) || (class >= MD_MN_NCLASSES)) {
27840Sstevel@tonic-gate 		*retval = MDMNE_EINVAL;
27850Sstevel@tonic-gate 		return (retval);
27860Sstevel@tonic-gate 	}
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate 	commd_debug(MD_MMV_MISC, "lock: set=%d, class=%d\n", setno, class);
278911053SSurya.Prakki@Sun.COM 	(void) mutex_lock(&mdmn_busy_mutex[setno]);
27900Sstevel@tonic-gate 	if (class != MD_MSG_CLASS0) {
27910Sstevel@tonic-gate 		mdmn_mark_class_locked(setno, class);
27920Sstevel@tonic-gate 	} else {
27930Sstevel@tonic-gate 		/* MD_MSG_CLASS0 is used as a wild card for all classes */
27940Sstevel@tonic-gate 		for (class = MD_MSG_CLASS1; class < MD_MN_NCLASSES; class++) {
27950Sstevel@tonic-gate 			mdmn_mark_class_locked(setno, class);
27960Sstevel@tonic-gate 		}
27970Sstevel@tonic-gate 	}
279811053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(&mdmn_busy_mutex[setno]);
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 	*retval = MDMNE_ACK;
28010Sstevel@tonic-gate 	return (retval);
28020Sstevel@tonic-gate }
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate /*
28050Sstevel@tonic-gate  * Unlock a set/class combination.
28060Sstevel@tonic-gate  * set must be between 1 and MD_MAXSETS
28070Sstevel@tonic-gate  * class can be:
28080Sstevel@tonic-gate  *	MD_MSG_CLASS0 which means all other classes in this case (like above)
28090Sstevel@tonic-gate  *	or one specific class (< MD_MN_NCLASSES)
28100Sstevel@tonic-gate  *
28110Sstevel@tonic-gate  * Returns:
28120Sstevel@tonic-gate  *	MDMNE_ACK on sucess (unlocking an unlocked class is Ok)
28130Sstevel@tonic-gate  *	MDMNE_EINVAL if a parameter is out of range
28140Sstevel@tonic-gate  */
28150Sstevel@tonic-gate /* ARGSUSED */
28160Sstevel@tonic-gate int *
mdmn_comm_unlock_svc_2(md_mn_set_and_class_t * msc,struct svc_req * rqstp)28178452SJohn.Wren.Kennedy@Sun.COM mdmn_comm_unlock_svc_2(md_mn_set_and_class_t *msc, struct svc_req *rqstp)
28180Sstevel@tonic-gate {
28190Sstevel@tonic-gate 	int			*retval;
28200Sstevel@tonic-gate 	set_t			setno  = msc->msc_set;
28210Sstevel@tonic-gate 	md_mn_msgclass_t	class  = msc->msc_class;
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate 	retval = Malloc(sizeof (int));
28240Sstevel@tonic-gate 
28250Sstevel@tonic-gate 	/* check if the global initialization is done */
28260Sstevel@tonic-gate 	if ((md_commd_global_state & MD_CGS_INITED) == 0) {
28270Sstevel@tonic-gate 		global_init();
28280Sstevel@tonic-gate 	}
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 	/* is this rpc request coming from the local node ? */
28310Sstevel@tonic-gate 	if (check_license(rqstp, 0) == FALSE) {
28320Sstevel@tonic-gate 		xdr_free(xdr_md_mn_set_and_class_t, (caddr_t)msc);
28330Sstevel@tonic-gate 		*retval = MDMNE_RPC_FAIL;
28340Sstevel@tonic-gate 		return (retval);
28350Sstevel@tonic-gate 	}
28360Sstevel@tonic-gate 
28370Sstevel@tonic-gate 	/* Perform some range checking */
28380Sstevel@tonic-gate 	if ((setno == 0) || (setno >= MD_MAXSETS) ||
28390Sstevel@tonic-gate 	    (class < MD_MSG_CLASS0) || (class >= MD_MN_NCLASSES)) {
28400Sstevel@tonic-gate 		*retval = MDMNE_EINVAL;
28410Sstevel@tonic-gate 		return (retval);
28420Sstevel@tonic-gate 	}
28430Sstevel@tonic-gate 	commd_debug(MD_MMV_MISC, "unlock: set=%d, class=%d\n", setno, class);
28440Sstevel@tonic-gate 
284511053SSurya.Prakki@Sun.COM 	(void) mutex_lock(&mdmn_busy_mutex[setno]);
28460Sstevel@tonic-gate 	if (class != MD_MSG_CLASS0) {
28470Sstevel@tonic-gate 		mdmn_mark_class_unlocked(setno, class);
28480Sstevel@tonic-gate 	} else {
28490Sstevel@tonic-gate 		/* MD_MSG_CLASS0 is used as a wild card for all classes */
28500Sstevel@tonic-gate 		for (class = MD_MSG_CLASS1; class < MD_MN_NCLASSES; class++) {
28510Sstevel@tonic-gate 			mdmn_mark_class_unlocked(setno, class);
28520Sstevel@tonic-gate 		}
28530Sstevel@tonic-gate 	}
285411053SSurya.Prakki@Sun.COM 	(void) mutex_unlock(&mdmn_busy_mutex[setno]);
28550Sstevel@tonic-gate 
28560Sstevel@tonic-gate 	*retval = MDMNE_ACK;
28570Sstevel@tonic-gate 	return (retval);
28580Sstevel@tonic-gate }
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate /*
28618452SJohn.Wren.Kennedy@Sun.COM  * mdmn_comm_suspend_svc_2(setno, class)
28620Sstevel@tonic-gate  *
28630Sstevel@tonic-gate  * Drain all outstanding messages for a given set/class combination
28640Sstevel@tonic-gate  * and don't allow new messages to be processed.
28650Sstevel@tonic-gate  *
28660Sstevel@tonic-gate  * Special messages of class MD_MSG_CLASS0 can never be locked.
28670Sstevel@tonic-gate  * 	e.g. MD_MN_MSG_VERBOSITY
28680Sstevel@tonic-gate  *
28690Sstevel@tonic-gate  * 1 <= setno < MD_MAXSETS	or setno == MD_COMM_ALL_SETS
28700Sstevel@tonic-gate  * 1 <= class < MD_MN_NCLASSES	or class == MD_COMM_ALL_CLASSES
28710Sstevel@tonic-gate  *
28720Sstevel@tonic-gate  * If class _is_not_ MD_COMM_ALL_CLASSES, then we simply mark this
28730Sstevel@tonic-gate  * one class as being suspended.
28740Sstevel@tonic-gate  * If messages for this class are currently on their way,
28750Sstevel@tonic-gate  * MDMNE_SET_NOT_DRAINED is returned. Otherwise MDMNE_ACK is returned.
28760Sstevel@tonic-gate  *
28770Sstevel@tonic-gate  * If class _is_ MD_COMM_ALL_CLASSES we drain all classes of this set.
28780Sstevel@tonic-gate  * Messages must be generated in ascending order.
28790Sstevel@tonic-gate  * This means, a message cannot create submessages with the same or lower class.
28800Sstevel@tonic-gate  * Draining messages must go from 1 to NCLASSES in order to ensure we don't
28810Sstevel@tonic-gate  * generate a hanging situation here.
28820Sstevel@tonic-gate  * We mark class 1 as being suspended.
28830Sstevel@tonic-gate  * if the class is not busy, we proceed with class 2
28840Sstevel@tonic-gate  * and so on
28850Sstevel@tonic-gate  * if a class *is* busy, we cannot continue here, but return
28860Sstevel@tonic-gate  * MDMNE_SET_NOT_DRAINED.
28870Sstevel@tonic-gate  * We expect the caller to hold on for some seconds and try again.
28880Sstevel@tonic-gate  * When that message, that held the class busy is done in
28890Sstevel@tonic-gate  * mdmn_master_process_msg(), mdmn_mark_class_unbusy() called.
28900Sstevel@tonic-gate  * There it is checked if the class is about to drain.
28910Sstevel@tonic-gate  * In that case it tries to drain all higher classes there.
28920Sstevel@tonic-gate  *
28930Sstevel@tonic-gate  * If setno is MD_COMM_ALL_SETS then we perform this on all possible sets.
28940Sstevel@tonic-gate  * In that case we return MDMNE_SET_NOT_DRAINED if not all sets are
28950Sstevel@tonic-gate  * completely drained.
28960Sstevel@tonic-gate  *
28970Sstevel@tonic-gate  * Returns:
28980Sstevel@tonic-gate  *	MDMNE_ACK on sucess (set is drained, no outstanding messages)
28990Sstevel@tonic-gate  *	MDMNE_SET_NOT_DRAINED  if drain process is started, but there are
29000Sstevel@tonic-gate  *		still outstanding messages for this set(s)
29010Sstevel@tonic-gate  *	MDMNE_EINVAL if setno is out of range
29020Sstevel@tonic-gate  *	MDMNE_NOT_JOINED if the set is not yet initialized on this node
29030Sstevel@tonic-gate  */
29040Sstevel@tonic-gate 
29050Sstevel@tonic-gate /* ARGSUSED */
29060Sstevel@tonic-gate int *
mdmn_comm_suspend_svc_2(md_mn_set_and_class_t * msc,struct svc_req * rqstp)29078452SJohn.Wren.Kennedy@Sun.COM mdmn_comm_suspend_svc_2(md_mn_set_and_class_t *msc, struct svc_req *rqstp)
29080Sstevel@tonic-gate {
29090Sstevel@tonic-gate 	int			*retval;
29100Sstevel@tonic-gate 	int			failure = 0;
29110Sstevel@tonic-gate 	set_t			startset, endset;
29120Sstevel@tonic-gate 	set_t			setno  = msc->msc_set;
29130Sstevel@tonic-gate 	md_mn_msgclass_t	oclass = msc->msc_class;
29140Sstevel@tonic-gate #ifdef NOT_YET_NEEDED
29150Sstevel@tonic-gate 	uint_t			flags  = msc->msc_flags;
29160Sstevel@tonic-gate #endif /* NOT_YET_NEEDED */
29170Sstevel@tonic-gate 	md_mn_msgclass_t	class;
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 	retval = Malloc(sizeof (int));
29200Sstevel@tonic-gate 
29210Sstevel@tonic-gate 	/* check if the global initialization is done */
29220Sstevel@tonic-gate 	if ((md_commd_global_state & MD_CGS_INITED) == 0) {
29230Sstevel@tonic-gate 		global_init();
29240Sstevel@tonic-gate 	}
29250Sstevel@tonic-gate 
29260Sstevel@tonic-gate 	/* is this rpc request coming from the local node ? */
29270Sstevel@tonic-gate 	if (check_license(rqstp, 0) == FALSE) {
29280Sstevel@tonic-gate 		xdr_free(xdr_md_mn_set_and_class_t, (caddr_t)msc);
29290Sstevel@tonic-gate 		*retval = MDMNE_RPC_FAIL;
29300Sstevel@tonic-gate 		return (retval);
29310Sstevel@tonic-gate 	}
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 	commd_debug(MD_MMV_MISC, "suspend: called for set=%d class=%d\n",
29340Sstevel@tonic-gate 	    setno, oclass);
29350Sstevel@tonic-gate 
29360Sstevel@tonic-gate 	/* Perform some range checking */
29370Sstevel@tonic-gate 	if (setno >= MD_MAXSETS) {
29380Sstevel@tonic-gate 		*retval = MDMNE_EINVAL;
29390Sstevel@tonic-gate 		commd_debug(MD_MMV_MISC, "suspend: returning MDMNE_EINVAL\n");
29400Sstevel@tonic-gate 		return (retval);
29410Sstevel@tonic-gate 	}
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 	/*  setno == MD_COMM_ALL_SETS means: we walk thru all possible sets. */
29440Sstevel@tonic-gate 	if (setno == MD_COMM_ALL_SETS) {
29450Sstevel@tonic-gate 		startset = 1;
29460Sstevel@tonic-gate 		endset = MD_MAXSETS - 1;
29470Sstevel@tonic-gate 	} else {
29480Sstevel@tonic-gate 		startset = setno;
29490Sstevel@tonic-gate 		endset = setno;
29500Sstevel@tonic-gate 	}
29510Sstevel@tonic-gate 
29520Sstevel@tonic-gate 	for (setno = startset; setno <= endset; setno++) {
29530Sstevel@tonic-gate 		/* Here we need the mutexes for the set to be setup */
29540Sstevel@tonic-gate 		if (md_mn_set_inited[setno] != MDMN_SET_MUTEXES) {
29550Sstevel@tonic-gate 			(void) mdmn_init_set(setno, MDMN_SET_MUTEXES);
29560Sstevel@tonic-gate 		}
29570Sstevel@tonic-gate 
295811053SSurya.Prakki@Sun.COM 		(void) mutex_lock(&mdmn_busy_mutex[setno]);
29590Sstevel@tonic-gate 		/* shall we drain all classes of this set? */
29600Sstevel@tonic-gate 		if (oclass == MD_COMM_ALL_CLASSES) {
29610Sstevel@tonic-gate 			for (class = 1; class < MD_MN_NCLASSES; class ++) {
29620Sstevel@tonic-gate 				commd_debug(MD_MMV_MISC,
29630Sstevel@tonic-gate 				    "suspend: suspending set %d, class %d\n",
29640Sstevel@tonic-gate 				    setno, class);
29650Sstevel@tonic-gate 				*retval = mdmn_mark_class_suspended(setno,
29660Sstevel@tonic-gate 				    class, MDMN_SUSPEND_ALL);
29670Sstevel@tonic-gate 				if (*retval == MDMNE_SET_NOT_DRAINED) {
29680Sstevel@tonic-gate 					failure++;
29690Sstevel@tonic-gate 				}
29700Sstevel@tonic-gate 			}
29710Sstevel@tonic-gate 		} else {
29720Sstevel@tonic-gate 			/* only drain one specific class */
29730Sstevel@tonic-gate 			commd_debug(MD_MMV_MISC,
29740Sstevel@tonic-gate 			    "suspend: suspending set=%d class=%d\n",
29750Sstevel@tonic-gate 			    setno, oclass);
29760Sstevel@tonic-gate 			*retval = mdmn_mark_class_suspended(setno, oclass,
29770Sstevel@tonic-gate 			    MDMN_SUSPEND_1);
29780Sstevel@tonic-gate 			if (*retval == MDMNE_SET_NOT_DRAINED) {
29790Sstevel@tonic-gate 				failure++;
29800Sstevel@tonic-gate 			}
29810Sstevel@tonic-gate 		}
298211053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&mdmn_busy_mutex[setno]);
29830Sstevel@tonic-gate 	}
29840Sstevel@tonic-gate 	/* If one or more sets are not entirely drained, failure is non-zero */
29850Sstevel@tonic-gate 	if (failure != 0) {
29860Sstevel@tonic-gate 		*retval = MDMNE_SET_NOT_DRAINED;
29870Sstevel@tonic-gate 		commd_debug(MD_MMV_MISC,
29880Sstevel@tonic-gate 		    "suspend: returning MDMNE_SET_NOT_DRAINED\n");
29890Sstevel@tonic-gate 	} else {
29900Sstevel@tonic-gate 		*retval = MDMNE_ACK;
29910Sstevel@tonic-gate 	}
29920Sstevel@tonic-gate 
29930Sstevel@tonic-gate 	return (retval);
29940Sstevel@tonic-gate }
29950Sstevel@tonic-gate 
29960Sstevel@tonic-gate /*
29978452SJohn.Wren.Kennedy@Sun.COM  * mdmn_comm_resume_svc_2(setno, class)
29980Sstevel@tonic-gate  *
29990Sstevel@tonic-gate  * Resume processing messages for a given set.
30000Sstevel@tonic-gate  * This incorporates the repeal of a previous suspend operation.
30010Sstevel@tonic-gate  *
30020Sstevel@tonic-gate  * 1 <= setno < MD_MAXSETS	or setno == MD_COMM_ALL_SETS
30030Sstevel@tonic-gate  * 1 <= class < MD_MN_NCLASSES	or class == MD_COMM_ALL_CLASSES
30040Sstevel@tonic-gate  *
30050Sstevel@tonic-gate  * If class _is_not_ MD_COMM_ALL_CLASSES, then we simply mark this
30060Sstevel@tonic-gate  * one class as being resumed.
30070Sstevel@tonic-gate  *
30080Sstevel@tonic-gate  * If class _is_ MD_COMM_ALL_CLASSES we resume all classes of this set.
30090Sstevel@tonic-gate  *
30100Sstevel@tonic-gate  * If setno is MD_COMM_ALL_SETS then we perform this on all possible sets.
30110Sstevel@tonic-gate  *
30120Sstevel@tonic-gate  * If both setno is MD_COMM_ALL_SETS and class is MD_COMM_ALL_CLASSES we also
30130Sstevel@tonic-gate  * reset any ABORT flag from the global state.
30140Sstevel@tonic-gate  *
30150Sstevel@tonic-gate  * Returns:
30160Sstevel@tonic-gate  *	MDMNE_ACK on sucess (resuming an unlocked set is Ok)
30170Sstevel@tonic-gate  *	MDMNE_EINVAL if setno is out of range
30180Sstevel@tonic-gate  *	MDMNE_NOT_JOINED if the set is not yet initialized on this node
30190Sstevel@tonic-gate  */
30200Sstevel@tonic-gate /* ARGSUSED */
30210Sstevel@tonic-gate int *
mdmn_comm_resume_svc_2(md_mn_set_and_class_t * msc,struct svc_req * rqstp)30228452SJohn.Wren.Kennedy@Sun.COM mdmn_comm_resume_svc_2(md_mn_set_and_class_t *msc, struct svc_req *rqstp)
30230Sstevel@tonic-gate {
30240Sstevel@tonic-gate 	int			*retval;
30250Sstevel@tonic-gate 	set_t			startset, endset;
30260Sstevel@tonic-gate 	set_t			setno  = msc->msc_set;
30270Sstevel@tonic-gate 	md_mn_msgclass_t	oclass = msc->msc_class;
30280Sstevel@tonic-gate 	uint_t			flags  = msc->msc_flags;
30290Sstevel@tonic-gate 	md_mn_msgclass_t	class;
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate 	retval = Malloc(sizeof (int));
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate 	/* check if the global initialization is done */
30340Sstevel@tonic-gate 	if ((md_commd_global_state & MD_CGS_INITED) == 0) {
30350Sstevel@tonic-gate 		global_init();
30360Sstevel@tonic-gate 	}
30370Sstevel@tonic-gate 
30380Sstevel@tonic-gate 	/* is this rpc request coming from the local node ? */
30390Sstevel@tonic-gate 	if (check_license(rqstp, 0) == FALSE) {
30400Sstevel@tonic-gate 		xdr_free(xdr_md_mn_set_and_class_t, (caddr_t)msc);
30410Sstevel@tonic-gate 		*retval = MDMNE_RPC_FAIL;
30420Sstevel@tonic-gate 		return (retval);
30430Sstevel@tonic-gate 	}
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 	commd_debug(MD_MMV_MISC, "resume: called for set=%d class=%d\n",
30460Sstevel@tonic-gate 	    setno, oclass);
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 	/* Perform some range checking */
30490Sstevel@tonic-gate 	if (setno > MD_MAXSETS) {
30500Sstevel@tonic-gate 		*retval = MDMNE_EINVAL;
30510Sstevel@tonic-gate 		return (retval);
30520Sstevel@tonic-gate 	}
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 	if (setno == MD_COMM_ALL_SETS) {
30550Sstevel@tonic-gate 		startset = 1;
30560Sstevel@tonic-gate 		endset = MD_MAXSETS - 1;
30570Sstevel@tonic-gate 		if (oclass == MD_COMM_ALL_CLASSES) {
30580Sstevel@tonic-gate 			/* This is the point where we "unabort" the commd */
30590Sstevel@tonic-gate 			commd_debug(MD_MMV_MISC, "resume: resetting ABORT\n");
30600Sstevel@tonic-gate 			md_commd_global_state &= ~MD_CGS_ABORTED;
30610Sstevel@tonic-gate 		}
30620Sstevel@tonic-gate 	} else {
30630Sstevel@tonic-gate 		startset = setno;
30640Sstevel@tonic-gate 		endset = setno;
30650Sstevel@tonic-gate 	}
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 	for (setno = startset; setno <= endset; setno++) {
30680Sstevel@tonic-gate 
30690Sstevel@tonic-gate 		/* Here we need the mutexes for the set to be setup */
30700Sstevel@tonic-gate 		if ((md_mn_set_inited[setno] & MDMN_SET_MUTEXES) == 0) {
30710Sstevel@tonic-gate 			(void) mdmn_init_set(setno, MDMN_SET_MUTEXES);
30720Sstevel@tonic-gate 		}
30730Sstevel@tonic-gate 
307411053SSurya.Prakki@Sun.COM 		(void) mutex_lock(&mdmn_busy_mutex[setno]);
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 		if (oclass == MD_COMM_ALL_CLASSES) {
30770Sstevel@tonic-gate 			int end_class = 1;
30780Sstevel@tonic-gate 			/*
30790Sstevel@tonic-gate 			 * When SUSPENDing all classes, we go
30800Sstevel@tonic-gate 			 * from 1 to MD_MN_NCLASSES-1
30810Sstevel@tonic-gate 			 * The correct reverse action is RESUMing
30820Sstevel@tonic-gate 			 * from MD_MN_NCLASSES-1 to 1 (or 2)
30830Sstevel@tonic-gate 			 */
30840Sstevel@tonic-gate 
30850Sstevel@tonic-gate 			if (flags & MD_MSCF_DONT_RESUME_CLASS1) {
30860Sstevel@tonic-gate 				end_class = 2;
30870Sstevel@tonic-gate 			}
30880Sstevel@tonic-gate 
30890Sstevel@tonic-gate 			/*
30900Sstevel@tonic-gate 			 * Then mark all classes of this set as no longer
30910Sstevel@tonic-gate 			 * suspended. This supersedes any previous suspend(1)
30920Sstevel@tonic-gate 			 * calls and resumes the set entirely.
30930Sstevel@tonic-gate 			 */
30940Sstevel@tonic-gate 			for (class = MD_MN_NCLASSES - 1; class >= end_class;
30950Sstevel@tonic-gate 			    class --) {
30960Sstevel@tonic-gate 				commd_debug(MD_MMV_MISC,
30970Sstevel@tonic-gate 				    "resume: resuming set=%d class=%d\n",
30980Sstevel@tonic-gate 				    setno, class);
30990Sstevel@tonic-gate 				mdmn_mark_class_resumed(setno, class,
31000Sstevel@tonic-gate 				    (MDMN_SUSPEND_ALL | MDMN_SUSPEND_1));
31010Sstevel@tonic-gate 			}
31020Sstevel@tonic-gate 		} else {
31030Sstevel@tonic-gate 			/*
31040Sstevel@tonic-gate 			 * In this case only one class is marked as not
31050Sstevel@tonic-gate 			 * suspended. If a suspend(all) is currently active for
31060Sstevel@tonic-gate 			 * this set, this class will still be suspended.
31070Sstevel@tonic-gate 			 * That state will be cleared by a suspend(all)
31080Sstevel@tonic-gate 			 * (see above)
31090Sstevel@tonic-gate 			 */
31100Sstevel@tonic-gate 			commd_debug(MD_MMV_MISC,
31110Sstevel@tonic-gate 			    "resume: resuming set=%d class=%d\n",
31120Sstevel@tonic-gate 			    setno, oclass);
31130Sstevel@tonic-gate 			mdmn_mark_class_resumed(setno, oclass, MDMN_SUSPEND_1);
31140Sstevel@tonic-gate 		}
31150Sstevel@tonic-gate 
311611053SSurya.Prakki@Sun.COM 		(void) mutex_unlock(&mdmn_busy_mutex[setno]);
31170Sstevel@tonic-gate 	}
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate 	*retval = MDMNE_ACK;
31200Sstevel@tonic-gate 	return (retval);
31210Sstevel@tonic-gate }
31220Sstevel@tonic-gate /* ARGSUSED */
31230Sstevel@tonic-gate int *
mdmn_comm_reinit_set_svc_2(set_t * setnop,struct svc_req * rqstp)31248452SJohn.Wren.Kennedy@Sun.COM mdmn_comm_reinit_set_svc_2(set_t *setnop, struct svc_req *rqstp)
31250Sstevel@tonic-gate {
31260Sstevel@tonic-gate 	int		*retval;
31270Sstevel@tonic-gate 	md_mnnode_desc	*node;
31280Sstevel@tonic-gate 	set_t		 setno = *setnop;
31290Sstevel@tonic-gate 
31300Sstevel@tonic-gate 	retval = Malloc(sizeof (int));
31310Sstevel@tonic-gate 
31320Sstevel@tonic-gate 	/* check if the global initialization is done */
31330Sstevel@tonic-gate 	if ((md_commd_global_state & MD_CGS_INITED) == 0) {
31340Sstevel@tonic-gate 		global_init();
31350Sstevel@tonic-gate 	}
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 	/* is this rpc request coming from the local node ? */
31380Sstevel@tonic-gate 	if (check_license(rqstp, 0) == FALSE) {
31390Sstevel@tonic-gate 		xdr_free(xdr_set_t, (caddr_t)setnop);
31400Sstevel@tonic-gate 		*retval = MDMNE_RPC_FAIL;
31410Sstevel@tonic-gate 		return (retval);
31420Sstevel@tonic-gate 	}
31430Sstevel@tonic-gate 
31440Sstevel@tonic-gate 	commd_debug(MD_MMV_MISC, "reinit: set=%d\n", setno);
31450Sstevel@tonic-gate 
314611053SSurya.Prakki@Sun.COM 	(void) rw_rdlock(&set_desc_rwlock[setno]);
31470Sstevel@tonic-gate 	/*
31480Sstevel@tonic-gate 	 * We assume, that all messages have been suspended previously.
31490Sstevel@tonic-gate 	 *
31500Sstevel@tonic-gate 	 * As we are modifying lots of clients here we grab the client_rwlock
31510Sstevel@tonic-gate 	 * in writer mode. This ensures, no new messages come in.
31520Sstevel@tonic-gate 	 */
315311053SSurya.Prakki@Sun.COM 	(void) rw_wrlock(&client_rwlock[setno]);
31540Sstevel@tonic-gate 	/* This set is no longer initialized */
31550Sstevel@tonic-gate 
31560Sstevel@tonic-gate 	if ((set_descriptor[setno] != NULL) &&
31570Sstevel@tonic-gate 	    (md_mn_set_inited[setno] & MDMN_SET_NODES)) {
31580Sstevel@tonic-gate 		/* destroy all rpc clients from this set */
31590Sstevel@tonic-gate 		for (node = set_descriptor[setno]->sd_nodelist; node;
31600Sstevel@tonic-gate 		    node = node->nd_next) {
31619017SJohn.Wren.Kennedy@Sun.COM 			/*
31629017SJohn.Wren.Kennedy@Sun.COM 			 * Since the CLIENT for ourself will be recreated
31639017SJohn.Wren.Kennedy@Sun.COM 			 * shortly, and this node is guaranteed to be
31649017SJohn.Wren.Kennedy@Sun.COM 			 * there after a reconfig, there's no reason to go
31659017SJohn.Wren.Kennedy@Sun.COM 			 * through destroying it.  It also avoids an issue
31669017SJohn.Wren.Kennedy@Sun.COM 			 * with calling clnt_create() later from within the
31679017SJohn.Wren.Kennedy@Sun.COM 			 * server thread, which can effectively deadlock
31689017SJohn.Wren.Kennedy@Sun.COM 			 * itself due to RPC design limitations.
31699017SJohn.Wren.Kennedy@Sun.COM 			 */
31709017SJohn.Wren.Kennedy@Sun.COM 			if (node == set_descriptor[setno]->sd_mn_mynode)
31719017SJohn.Wren.Kennedy@Sun.COM 				continue;
31720Sstevel@tonic-gate 			mdmn_clnt_destroy(client[setno][node->nd_nodeid]);
31730Sstevel@tonic-gate 			if (client[setno][node->nd_nodeid] != (CLIENT *)NULL) {
31740Sstevel@tonic-gate 				client[setno][node->nd_nodeid] = (CLIENT *)NULL;
31750Sstevel@tonic-gate 			}
31760Sstevel@tonic-gate 		}
31779017SJohn.Wren.Kennedy@Sun.COM 		md_mn_set_inited[setno] &= ~MDMN_SET_NODES;
31780Sstevel@tonic-gate 	}
31790Sstevel@tonic-gate 
31800Sstevel@tonic-gate 	commd_debug(MD_MMV_MISC, "reinit: done init_set(%d)\n", setno);
31810Sstevel@tonic-gate 
318211053SSurya.Prakki@Sun.COM 	(void) rw_unlock(&client_rwlock[setno]);
318311053SSurya.Prakki@Sun.COM 	(void) rw_unlock(&set_desc_rwlock[setno]);
31840Sstevel@tonic-gate 	*retval = MDMNE_ACK;
31850Sstevel@tonic-gate 	return (retval);
31860Sstevel@tonic-gate }
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate /*
31890Sstevel@tonic-gate  * This is just an interface for testing purpose.
31900Sstevel@tonic-gate  * Here we can disable single message types.
31910Sstevel@tonic-gate  * If we block a message type, this is valid for all MN sets.
31920Sstevel@tonic-gate  * If a message arrives later, and  it's message type is blocked, it will
31930Sstevel@tonic-gate  * be returned immediately with MDMNE_CLASS_LOCKED, which causes the sender to
31940Sstevel@tonic-gate  * resend this message over and over again.
31950Sstevel@tonic-gate  */
31960Sstevel@tonic-gate 
31970Sstevel@tonic-gate /* ARGSUSED */
31980Sstevel@tonic-gate int *
mdmn_comm_msglock_svc_2(md_mn_type_and_lock_t * mmtl,struct svc_req * rqstp)31998452SJohn.Wren.Kennedy@Sun.COM mdmn_comm_msglock_svc_2(md_mn_type_and_lock_t *mmtl, struct svc_req *rqstp)
32000Sstevel@tonic-gate {
32010Sstevel@tonic-gate 	int			*retval;
32020Sstevel@tonic-gate 	md_mn_msgtype_t		type = mmtl->mmtl_type;
32030Sstevel@tonic-gate 	uint_t			lock = mmtl->mmtl_lock;
32040Sstevel@tonic-gate 
32050Sstevel@tonic-gate 	retval = Malloc(sizeof (int));
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 	/* check if the global initialization is done */
32080Sstevel@tonic-gate 	if ((md_commd_global_state & MD_CGS_INITED) == 0) {
32090Sstevel@tonic-gate 		global_init();
32100Sstevel@tonic-gate 	}
32110Sstevel@tonic-gate 
32120Sstevel@tonic-gate 	/* is this rpc request coming from the local node ? */
32130Sstevel@tonic-gate 	if (check_license(rqstp, 0) == FALSE) {
32140Sstevel@tonic-gate 		xdr_free(xdr_md_mn_type_and_lock_t, (caddr_t)mmtl);
32150Sstevel@tonic-gate 		*retval = MDMNE_RPC_FAIL;
32160Sstevel@tonic-gate 		return (retval);
32170Sstevel@tonic-gate 	}
32180Sstevel@tonic-gate 
32190Sstevel@tonic-gate 	/* Perform some range checking */
32200Sstevel@tonic-gate 	if ((type == 0) || (type >= MD_MN_NMESSAGES)) {
32210Sstevel@tonic-gate 		*retval = MDMNE_EINVAL;
32220Sstevel@tonic-gate 		return (retval);
32230Sstevel@tonic-gate 	}
32240Sstevel@tonic-gate 
32250Sstevel@tonic-gate 	commd_debug(MD_MMV_MISC, "msglock: type=%d, lock=%d\n", type, lock);
32260Sstevel@tonic-gate 	msgtype_lock_state[type] = lock;
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 	*retval = MDMNE_ACK;
32290Sstevel@tonic-gate 	return (retval);
32300Sstevel@tonic-gate }
3231