xref: /titanic_52/usr/src/uts/common/os/modconf.c (revision 0f1702c5201310f0529cd5abb77652e5e9b241b6)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55a59a8b3Srsb  * Common Development and Distribution License (the "License").
65a59a8b3Srsb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22a204de77Scth  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/systm.h>
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/user.h>
307c478bd9Sstevel@tonic-gate #include <sys/vm.h>
317c478bd9Sstevel@tonic-gate #include <sys/conf.h>
327c478bd9Sstevel@tonic-gate #include <sys/class.h>
337c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
345a59a8b3Srsb #include <sys/vnode.h>
357c478bd9Sstevel@tonic-gate #include <sys/mount.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
387c478bd9Sstevel@tonic-gate #include <sys/exec.h>
397c478bd9Sstevel@tonic-gate #include <sys/exechdr.h>
407c478bd9Sstevel@tonic-gate #include <sys/devops.h>
417c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
437c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
447c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
457c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
467c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
477c478bd9Sstevel@tonic-gate #include <sys/disp.h>
487c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
497c478bd9Sstevel@tonic-gate #include <sys/instance.h>
507c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
517c478bd9Sstevel@tonic-gate #include <sys/dacf.h>
527c478bd9Sstevel@tonic-gate #include <sys/debug.h>
537c478bd9Sstevel@tonic-gate #include <ipp/ipp.h>
547c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
557c478bd9Sstevel@tonic-gate #include <sys/kcpc.h>
569acbbeafSnn35248 #include <sys/brand.h>
577c478bd9Sstevel@tonic-gate #include <sys/cpc_pcbe.h>
585a59a8b3Srsb #include <sys/kstat.h>
59facf4a8dSllai1 #include <sys/fs/sdev_node.h>
60*0f1702c5SYu Xiangning #include <sys/socketvar.h>
61d14d7d31Sis #include <sys/kiconv.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate extern int moddebug;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate extern struct cb_ops no_cb_ops;
667c478bd9Sstevel@tonic-gate extern struct dev_ops nodev_ops;
677c478bd9Sstevel@tonic-gate extern struct dev_ops mod_nodev_ops;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate extern struct modctl *mod_getctl(struct modlinkage *);
707c478bd9Sstevel@tonic-gate extern int errsys(), nodev(), nulldev();
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate extern int findmodbyname(char *);
737c478bd9Sstevel@tonic-gate extern int mod_getsysnum(char *);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate extern struct execsw execsw[];
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Define dev_ops for unused devopsp entry.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate struct dev_ops mod_nodev_ops = {
817c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev	*/
827c478bd9Sstevel@tonic-gate 	0,			/* refcnt	*/
837c478bd9Sstevel@tonic-gate 	ddi_no_info,		/* info */
847c478bd9Sstevel@tonic-gate 	nulldev,		/* identify	*/
857c478bd9Sstevel@tonic-gate 	nulldev,		/* probe	*/
867c478bd9Sstevel@tonic-gate 	ddifail,		/* attach	*/
877c478bd9Sstevel@tonic-gate 	nodev,			/* detach	*/
887c478bd9Sstevel@tonic-gate 	nulldev,		/* reset	*/
897c478bd9Sstevel@tonic-gate 	&no_cb_ops,		/* character/block driver operations */
907c478bd9Sstevel@tonic-gate 	(struct bus_ops *)0	/* bus operations for nexus drivers */
917c478bd9Sstevel@tonic-gate };
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Define mod_ops for each supported module type
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * Null operations; used for uninitialized and "misc" modules.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate static int mod_null(struct modldrv *, struct modlinkage *);
1017c478bd9Sstevel@tonic-gate static int mod_infonull(void *, struct modlinkage *, int *);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate struct mod_ops mod_miscops = {
1047c478bd9Sstevel@tonic-gate 	mod_null, mod_null, mod_infonull
1057c478bd9Sstevel@tonic-gate };
1067c478bd9Sstevel@tonic-gate 
1077aec1d6eScindi /* CPU Modules */
1087aec1d6eScindi struct mod_ops mod_cpuops = {
1097aec1d6eScindi 	mod_null, mod_null, mod_infonull
1107aec1d6eScindi };
1117aec1d6eScindi 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * Cryptographic Modules
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate struct mod_ops mod_cryptoops = {
1167c478bd9Sstevel@tonic-gate 	mod_null, mod_null, mod_infonull
1177c478bd9Sstevel@tonic-gate };
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * IP Policy Modules
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static int mod_installipp(struct modlipp *, struct modlinkage *);
1237c478bd9Sstevel@tonic-gate static int mod_removeipp(struct modlipp *, struct modlinkage *);
1247c478bd9Sstevel@tonic-gate static int mod_infoipp(struct modlipp *, struct modlinkage *, int *);
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate struct mod_ops mod_ippops = {
1277c478bd9Sstevel@tonic-gate 	mod_installipp, mod_removeipp, mod_infoipp
1287c478bd9Sstevel@tonic-gate };
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate  * Device drivers
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate static int mod_infodrv(struct modldrv *, struct modlinkage *, int *);
1347c478bd9Sstevel@tonic-gate static int mod_installdrv(struct modldrv *, struct modlinkage *);
1357c478bd9Sstevel@tonic-gate static int mod_removedrv(struct modldrv *, struct modlinkage *);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate struct mod_ops mod_driverops = {
1387c478bd9Sstevel@tonic-gate 	mod_installdrv, mod_removedrv, mod_infodrv
1397c478bd9Sstevel@tonic-gate };
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * System calls (new interface)
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate static int mod_infosys(struct modlsys *, struct modlinkage *, int *);
1457c478bd9Sstevel@tonic-gate static int mod_installsys(struct modlsys *, struct modlinkage *);
1467c478bd9Sstevel@tonic-gate static int mod_removesys(struct modlsys *, struct modlinkage *);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate struct mod_ops mod_syscallops = {
1497c478bd9Sstevel@tonic-gate 	mod_installsys, mod_removesys, mod_infosys
1507c478bd9Sstevel@tonic-gate };
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * 32-bit system calls in 64-bit kernel
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate static int mod_infosys32(struct modlsys *, struct modlinkage *, int *);
1577c478bd9Sstevel@tonic-gate static int mod_installsys32(struct modlsys *, struct modlinkage *);
1587c478bd9Sstevel@tonic-gate static int mod_removesys32(struct modlsys *, struct modlinkage *);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate struct mod_ops mod_syscallops32 = {
1617c478bd9Sstevel@tonic-gate 	mod_installsys32, mod_removesys32, mod_infosys32
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Filesystems
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate static int mod_infofs(struct modlfs *, struct modlinkage *, int *);
1697c478bd9Sstevel@tonic-gate static int mod_installfs(struct modlfs *, struct modlinkage *);
1707c478bd9Sstevel@tonic-gate static int mod_removefs(struct modlfs *, struct modlinkage *);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate struct mod_ops mod_fsops = {
1737c478bd9Sstevel@tonic-gate 	mod_installfs, mod_removefs, mod_infofs
1747c478bd9Sstevel@tonic-gate };
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /*
1777c478bd9Sstevel@tonic-gate  * Streams modules.
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate static int mod_infostrmod(struct modlstrmod *, struct modlinkage *, int *);
1807c478bd9Sstevel@tonic-gate static int mod_installstrmod(struct modlstrmod *, struct modlinkage *);
1817c478bd9Sstevel@tonic-gate static int mod_removestrmod(struct modlstrmod *, struct modlinkage *);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate struct mod_ops mod_strmodops = {
1847c478bd9Sstevel@tonic-gate 	mod_installstrmod, mod_removestrmod, mod_infostrmod
1857c478bd9Sstevel@tonic-gate };
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
188*0f1702c5SYu Xiangning  * Socket modules.
189*0f1702c5SYu Xiangning  */
190*0f1702c5SYu Xiangning static int mod_infosockmod(struct modlsockmod *, struct modlinkage *, int *);
191*0f1702c5SYu Xiangning static int mod_installsockmod(struct modlsockmod *, struct modlinkage *);
192*0f1702c5SYu Xiangning static int mod_removesockmod(struct modlsockmod *, struct modlinkage *);
193*0f1702c5SYu Xiangning 
194*0f1702c5SYu Xiangning struct mod_ops mod_sockmodops = {
195*0f1702c5SYu Xiangning 	mod_installsockmod, mod_removesockmod, mod_infosockmod
196*0f1702c5SYu Xiangning };
197*0f1702c5SYu Xiangning 
198*0f1702c5SYu Xiangning /*
1997c478bd9Sstevel@tonic-gate  * Scheduling classes.
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate static int mod_infosched(struct modlsched *, struct modlinkage *, int *);
2027c478bd9Sstevel@tonic-gate static int mod_installsched(struct modlsched *, struct modlinkage *);
2037c478bd9Sstevel@tonic-gate static int mod_removesched(struct modlsched *, struct modlinkage *);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate struct mod_ops mod_schedops = {
2067c478bd9Sstevel@tonic-gate 	mod_installsched, mod_removesched, mod_infosched
2077c478bd9Sstevel@tonic-gate };
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * Exec file type (like ELF, ...).
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate static int mod_infoexec(struct modlexec *, struct modlinkage *, int *);
2137c478bd9Sstevel@tonic-gate static int mod_installexec(struct modlexec *, struct modlinkage *);
2147c478bd9Sstevel@tonic-gate static int mod_removeexec(struct modlexec *, struct modlinkage *);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate struct mod_ops mod_execops = {
2177c478bd9Sstevel@tonic-gate 	mod_installexec, mod_removeexec, mod_infoexec
2187c478bd9Sstevel@tonic-gate };
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * Dacf (Dynamic Autoconfiguration) modules.
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate static int mod_infodacf(struct modldacf *, struct modlinkage *, int *);
2247c478bd9Sstevel@tonic-gate static int mod_installdacf(struct modldacf *, struct modlinkage *);
2257c478bd9Sstevel@tonic-gate static int mod_removedacf(struct modldacf *, struct modlinkage *);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate struct mod_ops mod_dacfops = {
2287c478bd9Sstevel@tonic-gate 	mod_installdacf, mod_removedacf, mod_infodacf
2297c478bd9Sstevel@tonic-gate };
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
232facf4a8dSllai1  * /dev fs modules
233facf4a8dSllai1  */
234facf4a8dSllai1 static int mod_infodev(struct modldev *, struct modlinkage *, int *);
235facf4a8dSllai1 static int mod_installdev(struct modldev *, struct modlinkage *);
236facf4a8dSllai1 static int mod_removedev(struct modldev *, struct modlinkage *);
237facf4a8dSllai1 
238facf4a8dSllai1 struct mod_ops mod_devfsops = {
239facf4a8dSllai1 	mod_installdev, mod_removedev, mod_infodev
240facf4a8dSllai1 };
241facf4a8dSllai1 
242facf4a8dSllai1 /*
2437c478bd9Sstevel@tonic-gate  * PCBE (Performance Counter BackEnd) modules.
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate static int mod_installpcbe(struct modlpcbe *, struct modlinkage *);
2467c478bd9Sstevel@tonic-gate static int mod_removepcbe(struct modlpcbe *, struct modlinkage *);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate struct mod_ops mod_pcbeops = {
2497c478bd9Sstevel@tonic-gate 	mod_installpcbe, mod_removepcbe, mod_infonull
2507c478bd9Sstevel@tonic-gate };
2517c478bd9Sstevel@tonic-gate 
2529acbbeafSnn35248 /*
2539acbbeafSnn35248  * Brand modules.
2549acbbeafSnn35248  */
2559acbbeafSnn35248 static int mod_installbrand(struct modlbrand *, struct modlinkage *);
2569acbbeafSnn35248 static int mod_removebrand(struct modlbrand *, struct modlinkage *);
2579acbbeafSnn35248 
2589acbbeafSnn35248 struct mod_ops mod_brandops = {
2599acbbeafSnn35248 	mod_installbrand, mod_removebrand, mod_infonull
2609acbbeafSnn35248 };
2619acbbeafSnn35248 
262d14d7d31Sis /*
263d14d7d31Sis  * kiconv modules.
264d14d7d31Sis  */
265d14d7d31Sis static int mod_installkiconv(struct modlkiconv *, struct modlinkage *);
266d14d7d31Sis static int mod_removekiconv(struct modlkiconv *, struct modlinkage *);
267d14d7d31Sis 
268d14d7d31Sis struct mod_ops mod_kiconvops = {
269d14d7d31Sis 	mod_installkiconv, mod_removekiconv, mod_infonull
270d14d7d31Sis };
271d14d7d31Sis 
2727c478bd9Sstevel@tonic-gate static struct sysent *mod_getsysent(struct modlinkage *, struct sysent *);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate static char uninstall_err[] = "Cannot uninstall %s; not installed";
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate  * Debugging support
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate #define	DRV_DBG		MODDEBUG_LOADMSG2
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
2827c478bd9Sstevel@tonic-gate static void mod_dprintf(int flag, const char *format, ...) __KPRINTFLIKE(2);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static void
2857c478bd9Sstevel@tonic-gate mod_dprintf(int flag, const char *format, ...)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	va_list alist;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if ((moddebug & flag) != 0) {
2907c478bd9Sstevel@tonic-gate 		va_start(alist, format);
2917c478bd9Sstevel@tonic-gate 		(void) vprintf(format, alist);
2927c478bd9Sstevel@tonic-gate 		va_end(alist);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * Install a module.
2987c478bd9Sstevel@tonic-gate  * (This routine is in the Solaris SPARC DDI/DKI)
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate int
3017c478bd9Sstevel@tonic-gate mod_install(struct modlinkage *modlp)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	int retval = -1;	/* No linkage structures */
3047c478bd9Sstevel@tonic-gate 	struct modlmisc **linkpp;
3057c478bd9Sstevel@tonic-gate 	struct modlmisc **linkpp1;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if (modlp->ml_rev != MODREV_1) {
3087c478bd9Sstevel@tonic-gate 		printf("mod_install:  modlinkage structure is not MODREV_1\n");
3097c478bd9Sstevel@tonic-gate 		return (EINVAL);
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 	linkpp = (struct modlmisc **)&modlp->ml_linkage[0];
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	while (*linkpp != NULL) {
3147c478bd9Sstevel@tonic-gate 		if ((retval = MODL_INSTALL(*linkpp, modlp)) != 0) {
3157c478bd9Sstevel@tonic-gate 			linkpp1 = (struct modlmisc **)&modlp->ml_linkage[0];
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 			while (linkpp1 != linkpp) {
3187c478bd9Sstevel@tonic-gate 				MODL_REMOVE(*linkpp1, modlp); /* clean up */
3197c478bd9Sstevel@tonic-gate 				linkpp1++;
3207c478bd9Sstevel@tonic-gate 			}
3217c478bd9Sstevel@tonic-gate 			break;
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 		linkpp++;
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 	return (retval);
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate static char *reins_err =
3297c478bd9Sstevel@tonic-gate 	"Could not reinstall %s\nReboot to correct the problem";
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate  * Remove a module.  This is called by the module wrapper routine.
3337c478bd9Sstevel@tonic-gate  * (This routine is in the Solaris SPARC DDI/DKI)
3347c478bd9Sstevel@tonic-gate  */
3357c478bd9Sstevel@tonic-gate int
3367c478bd9Sstevel@tonic-gate mod_remove(struct modlinkage *modlp)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	int retval = 0;
3397c478bd9Sstevel@tonic-gate 	struct modlmisc **linkpp, *last_linkp;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	linkpp = (struct modlmisc **)&modlp->ml_linkage[0];
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	while (*linkpp != NULL) {
3447c478bd9Sstevel@tonic-gate 		if ((retval = MODL_REMOVE(*linkpp, modlp)) != 0) {
3457c478bd9Sstevel@tonic-gate 			last_linkp = *linkpp;
3467c478bd9Sstevel@tonic-gate 			linkpp = (struct modlmisc **)&modlp->ml_linkage[0];
3477c478bd9Sstevel@tonic-gate 			while (*linkpp != last_linkp) {
3487c478bd9Sstevel@tonic-gate 				if (MODL_INSTALL(*linkpp, modlp) != 0) {
3497c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, reins_err,
3507c478bd9Sstevel@tonic-gate 					    (*linkpp)->misc_linkinfo);
3517c478bd9Sstevel@tonic-gate 					break;
3527c478bd9Sstevel@tonic-gate 				}
3537c478bd9Sstevel@tonic-gate 				linkpp++;
3547c478bd9Sstevel@tonic-gate 			}
3557c478bd9Sstevel@tonic-gate 			break;
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 		linkpp++;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 	return (retval);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * Get module status.
3647c478bd9Sstevel@tonic-gate  * (This routine is in the Solaris SPARC DDI/DKI)
3657c478bd9Sstevel@tonic-gate  */
3667c478bd9Sstevel@tonic-gate int
3677c478bd9Sstevel@tonic-gate mod_info(struct modlinkage *modlp, struct modinfo *modinfop)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	int i;
3707c478bd9Sstevel@tonic-gate 	int retval = 0;
3717c478bd9Sstevel@tonic-gate 	struct modspecific_info *msip;
3727c478bd9Sstevel@tonic-gate 	struct modlmisc **linkpp;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	modinfop->mi_rev = modlp->ml_rev;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	linkpp = (struct modlmisc **)modlp->ml_linkage;
3777c478bd9Sstevel@tonic-gate 	msip = &modinfop->mi_msinfo[0];
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	for (i = 0; i < MODMAXLINK; i++) {
3807c478bd9Sstevel@tonic-gate 		if (*linkpp == NULL) {
3817c478bd9Sstevel@tonic-gate 			msip->msi_linkinfo[0] = '\0';
3827c478bd9Sstevel@tonic-gate 		} else {
3837c478bd9Sstevel@tonic-gate 			(void) strncpy(msip->msi_linkinfo,
3847c478bd9Sstevel@tonic-gate 			    (*linkpp)->misc_linkinfo, MODMAXLINKINFOLEN);
3857c478bd9Sstevel@tonic-gate 			retval = MODL_INFO(*linkpp, modlp, &msip->msi_p0);
3867c478bd9Sstevel@tonic-gate 			if (retval != 0)
3877c478bd9Sstevel@tonic-gate 				break;
3887c478bd9Sstevel@tonic-gate 			linkpp++;
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 		msip++;
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	if (modinfop->mi_info == MI_INFO_LINKAGE) {
3947c478bd9Sstevel@tonic-gate 		/*
3957c478bd9Sstevel@tonic-gate 		 * Slight kludge used to extract the address of the
3967c478bd9Sstevel@tonic-gate 		 * modlinkage structure from the module (just after
3977c478bd9Sstevel@tonic-gate 		 * loading a module for the very first time)
3987c478bd9Sstevel@tonic-gate 		 */
3997c478bd9Sstevel@tonic-gate 		modinfop->mi_base = (void *)modlp;
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (retval == 0)
4037c478bd9Sstevel@tonic-gate 		return (1);
4047c478bd9Sstevel@tonic-gate 	return (0);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate  * Get module name.
4097c478bd9Sstevel@tonic-gate  */
410a204de77Scth const char *
4117c478bd9Sstevel@tonic-gate mod_modname(struct modlinkage *modlp)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	struct modctl	*mcp;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if ((mcp = mod_getctl(modlp)) == NULL)
4167c478bd9Sstevel@tonic-gate 		return (NULL);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	return (mcp->mod_modname);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate /*
4227c478bd9Sstevel@tonic-gate  * Null operation; return 0.
4237c478bd9Sstevel@tonic-gate  */
4247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4257c478bd9Sstevel@tonic-gate static int
4267c478bd9Sstevel@tonic-gate mod_null(struct modldrv *modl, struct modlinkage *modlp)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	return (0);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate /*
4327c478bd9Sstevel@tonic-gate  * Status for User modules.
4337c478bd9Sstevel@tonic-gate  */
4347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4357c478bd9Sstevel@tonic-gate static int
4367c478bd9Sstevel@tonic-gate mod_infonull(void *modl, struct modlinkage *modlp, int *p0)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	*p0 = -1;		/* for modinfo display */
4397c478bd9Sstevel@tonic-gate 	return (0);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate  * Driver status info
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4467c478bd9Sstevel@tonic-gate static int
4477c478bd9Sstevel@tonic-gate mod_infodrv(struct modldrv *modl, struct modlinkage *modlp, int *p0)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	struct modctl *mcp;
4507c478bd9Sstevel@tonic-gate 	char *mod_name;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	if ((mcp = mod_getctl(modlp)) == NULL) {
4537c478bd9Sstevel@tonic-gate 		*p0 = -1;
4547c478bd9Sstevel@tonic-gate 		return (0);	/* driver is not yet installed */
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	mod_name = mcp->mod_modname;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	*p0 = ddi_name_to_major(mod_name);
4607c478bd9Sstevel@tonic-gate 	return (0);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate  * Manage dacf (device autoconfiguration) modules
4657c478bd9Sstevel@tonic-gate  */
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4687c478bd9Sstevel@tonic-gate static int
4697c478bd9Sstevel@tonic-gate mod_infodacf(struct modldacf *modl, struct modlinkage *modlp, int *p0)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate 	if (mod_getctl(modlp) == NULL) {
4727c478bd9Sstevel@tonic-gate 		*p0 = -1;
4737c478bd9Sstevel@tonic-gate 		return (0);	/* module is not yet installed */
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	*p0 = 0;
4777c478bd9Sstevel@tonic-gate 	return (0);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static int
4817c478bd9Sstevel@tonic-gate mod_installdacf(struct modldacf *modl, struct modlinkage *modlp)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	struct modctl	*mcp;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if ((mcp = mod_getctl(modlp)) == NULL)
4867c478bd9Sstevel@tonic-gate 		return (EINVAL);
4877c478bd9Sstevel@tonic-gate 	return (dacf_module_register(mcp->mod_modname, modl->dacf_dacfsw));
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4917c478bd9Sstevel@tonic-gate static int
4927c478bd9Sstevel@tonic-gate mod_removedacf(struct modldacf *modl, struct modlinkage *modlp)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate 	struct modctl	*mcp;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	if ((mcp = mod_getctl(modlp)) == NULL)
4977c478bd9Sstevel@tonic-gate 		return (EINVAL);
4987c478bd9Sstevel@tonic-gate 	return (dacf_module_unregister(mcp->mod_modname));
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate /*
5027c478bd9Sstevel@tonic-gate  * Manage PCBE (Performance Counter BackEnd) modules.
5037c478bd9Sstevel@tonic-gate  */
5047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5057c478bd9Sstevel@tonic-gate static int
5067c478bd9Sstevel@tonic-gate mod_installpcbe(struct modlpcbe *modl, struct modlinkage *modlp)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate 	if (modl->pcbe_ops->pcbe_ver != PCBE_VER_1) {
5097c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "pcbe '%s' version mismatch",
5107c478bd9Sstevel@tonic-gate 		    modl->pcbe_linkinfo);
5117c478bd9Sstevel@tonic-gate 		return (EINVAL);
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	kcpc_register_pcbe(modl->pcbe_ops);
5157c478bd9Sstevel@tonic-gate 	return (0);
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate /*
5197c478bd9Sstevel@tonic-gate  * PCBEs may not be unloaded. It would make CPC locking too complex, and since
5207c478bd9Sstevel@tonic-gate  * PCBEs are loaded once and used for life, there is no harm done in leaving
5217c478bd9Sstevel@tonic-gate  * them in the system.
5227c478bd9Sstevel@tonic-gate  */
5237c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5247c478bd9Sstevel@tonic-gate static int
5257c478bd9Sstevel@tonic-gate mod_removepcbe(struct modlpcbe *modl, struct modlinkage *modlp)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	return (EBUSY);
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate 
530facf4a8dSllai1 /*
5319acbbeafSnn35248  * Manage BrandZ modules.
5329acbbeafSnn35248  */
5339acbbeafSnn35248 /*ARGSUSED*/
5349acbbeafSnn35248 static int
5359acbbeafSnn35248 mod_installbrand(struct modlbrand *modl, struct modlinkage *modlp)
5369acbbeafSnn35248 {
5379acbbeafSnn35248 	return (brand_register(modl->brand_branddef));
5389acbbeafSnn35248 }
5399acbbeafSnn35248 
5409acbbeafSnn35248 /*ARGSUSED*/
5419acbbeafSnn35248 static int
5429acbbeafSnn35248 mod_removebrand(struct modlbrand *modl, struct modlinkage *modlp)
5439acbbeafSnn35248 {
5449acbbeafSnn35248 	return (brand_unregister(modl->brand_branddef));
5459acbbeafSnn35248 }
5469acbbeafSnn35248 
5479acbbeafSnn35248 /*
548facf4a8dSllai1  * manage /dev fs modules
549facf4a8dSllai1  */
550facf4a8dSllai1 /*ARGSUSED*/
551facf4a8dSllai1 static int
552facf4a8dSllai1 mod_infodev(struct modldev *modl, struct modlinkage *modlp, int *p0)
553facf4a8dSllai1 {
554facf4a8dSllai1 	if (mod_getctl(modlp) == NULL) {
555facf4a8dSllai1 		*p0 = -1;
556facf4a8dSllai1 		return (0);	/* module is not yet installed */
557facf4a8dSllai1 	}
558facf4a8dSllai1 
559facf4a8dSllai1 	*p0 = 0;
560facf4a8dSllai1 	return (0);
561facf4a8dSllai1 }
562facf4a8dSllai1 
563facf4a8dSllai1 static int
564facf4a8dSllai1 mod_installdev(struct modldev *modl, struct modlinkage *modlp)
565facf4a8dSllai1 {
566facf4a8dSllai1 	struct modctl	*mcp;
567facf4a8dSllai1 
568facf4a8dSllai1 	if ((mcp = mod_getctl(modlp)) == NULL)
569facf4a8dSllai1 		return (EINVAL);
570facf4a8dSllai1 	return (sdev_module_register(mcp->mod_modname, modl->dev_ops));
571facf4a8dSllai1 }
572facf4a8dSllai1 
573facf4a8dSllai1 /*
574facf4a8dSllai1  * /dev modules are not unloadable.
575facf4a8dSllai1  */
576facf4a8dSllai1 /*ARGSUSED*/
577facf4a8dSllai1 static int
578facf4a8dSllai1 mod_removedev(struct modldev *modl, struct modlinkage *modlp)
579facf4a8dSllai1 {
580facf4a8dSllai1 	return (EBUSY);
581facf4a8dSllai1 }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate  * Install a new driver
5857c478bd9Sstevel@tonic-gate  */
5867c478bd9Sstevel@tonic-gate static int
5877c478bd9Sstevel@tonic-gate mod_installdrv(struct modldrv *modl, struct modlinkage *modlp)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	struct modctl *mcp;
5907c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
5917c478bd9Sstevel@tonic-gate 	char *modname;
5927c478bd9Sstevel@tonic-gate 	major_t major;
5937c478bd9Sstevel@tonic-gate 	struct dev_ops *dp;
5947c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
5957c478bd9Sstevel@tonic-gate 	struct streamtab *str;
5967c478bd9Sstevel@tonic-gate 	cdevsw_impl_t *cdp;
5977c478bd9Sstevel@tonic-gate 	uint_t sqtype;
5987c478bd9Sstevel@tonic-gate 	uint_t qflag;
5997c478bd9Sstevel@tonic-gate 	uint_t flag;
6007c478bd9Sstevel@tonic-gate 	int err = 0;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	/* sanity check module */
6037c478bd9Sstevel@tonic-gate 	if ((mcp = mod_getctl(modlp)) == NULL) {
6047c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "mod_install: bad module linkage data");
6057c478bd9Sstevel@tonic-gate 		err = ENXIO;
6067c478bd9Sstevel@tonic-gate 		goto done;
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 	modname = mcp->mod_modname;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/* Sanity check modname */
611a204de77Scth 	if ((major = ddi_name_to_major(modname)) == DDI_MAJOR_T_NONE) {
6127c478bd9Sstevel@tonic-gate #ifdef DEBUG
6137c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
6147c478bd9Sstevel@tonic-gate 		    "mod_installdrv: no major number for %s", modname);
6157c478bd9Sstevel@tonic-gate #endif
6167c478bd9Sstevel@tonic-gate 		err = ENXIO;
6177c478bd9Sstevel@tonic-gate 		goto done;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/* Verify MP safety flag */
6217c478bd9Sstevel@tonic-gate 	ops = modl->drv_dev_ops;
6227c478bd9Sstevel@tonic-gate 	if (ops->devo_bus_ops == NULL && ops->devo_cb_ops != NULL &&
6237c478bd9Sstevel@tonic-gate 	    !(ops->devo_cb_ops->cb_flag & D_MP)) {
6247c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
6257c478bd9Sstevel@tonic-gate 		    "mod_installdrv: MT-unsafe driver '%s' rejected", modname);
6267c478bd9Sstevel@tonic-gate 		err = ENXIO;
6277c478bd9Sstevel@tonic-gate 		goto done;
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/* Is bus_map_fault signature correct (version 8 and higher)? */
6327c478bd9Sstevel@tonic-gate 	if (ops->devo_bus_ops != NULL &&
6337c478bd9Sstevel@tonic-gate 	    ops->devo_bus_ops->bus_map_fault != NULL &&
6347c478bd9Sstevel@tonic-gate 	    ops->devo_bus_ops->bus_map_fault != i_ddi_map_fault &&
6357c478bd9Sstevel@tonic-gate 	    ops->devo_bus_ops->busops_rev < BUSO_REV_8) {
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
6387c478bd9Sstevel@tonic-gate 		    "mod_installdrv: busops' revision of '%s' is too low"
6397c478bd9Sstevel@tonic-gate 		    " (must be at least 8)", modname);
6407c478bd9Sstevel@tonic-gate 		err = ENXIO;
6417c478bd9Sstevel@tonic-gate 		goto done;
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	/* Make sure the driver is uninstalled */
6467c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
6477c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
6487c478bd9Sstevel@tonic-gate 	dp = devopsp[major];
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (dnp->dn_flags & DN_DRIVER_REMOVED) {
6517c478bd9Sstevel@tonic-gate #ifdef DEBUG
6527c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
6537c478bd9Sstevel@tonic-gate 		    "mod_installdrv: driver has been removed %s", modname);
6547c478bd9Sstevel@tonic-gate #endif
6557c478bd9Sstevel@tonic-gate 		err = ENXIO;
6567c478bd9Sstevel@tonic-gate 		goto unlock;
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if (dp != &nodev_ops && dp != &mod_nodev_ops) {
6607c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
6617c478bd9Sstevel@tonic-gate 		    "mod_installdrv: driver already installed %s", modname);
6627c478bd9Sstevel@tonic-gate 		err = EALREADY;
6637c478bd9Sstevel@tonic-gate 		goto unlock;
6647c478bd9Sstevel@tonic-gate 	}
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	devopsp[major] = ops; /* setup devopsp */
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	if ((str = STREAMSTAB(major)) != NULL) {	/* streams driver */
6697c478bd9Sstevel@tonic-gate 		flag = CBFLAG(major);
6707c478bd9Sstevel@tonic-gate 		if ((err = devflg_to_qflag(str, flag, &qflag, &sqtype)) != 0)
6717c478bd9Sstevel@tonic-gate 			goto unlock;
6727c478bd9Sstevel@tonic-gate 		cdp = &devimpl[major];
6737c478bd9Sstevel@tonic-gate 		ASSERT(cdp->d_str == NULL);
6747c478bd9Sstevel@tonic-gate 		cdp->d_str = str;
6757c478bd9Sstevel@tonic-gate 		cdp->d_qflag = qflag | QISDRV;
6767c478bd9Sstevel@tonic-gate 		cdp->d_sqtype = sqtype;
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (ops->devo_bus_ops == NULL)
6807c478bd9Sstevel@tonic-gate 		dnp->dn_flags |= DN_LEAF_DRIVER;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate unlock:
6837c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
6847c478bd9Sstevel@tonic-gate done:
6857c478bd9Sstevel@tonic-gate 	return (err);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate static int
6897c478bd9Sstevel@tonic-gate mod_removedrv(struct modldrv *modl, struct modlinkage *modlp)
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate 	struct modctl *mcp;
6927c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
6937c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
6947c478bd9Sstevel@tonic-gate 	struct dev_ops *dp;
6957c478bd9Sstevel@tonic-gate 	major_t major;
6967c478bd9Sstevel@tonic-gate 	char *modname;
6977c478bd9Sstevel@tonic-gate 	extern kthread_id_t mod_aul_thread;
6987c478bd9Sstevel@tonic-gate 	struct streamtab *str;
6997c478bd9Sstevel@tonic-gate 	cdevsw_impl_t *cdp;
7007c478bd9Sstevel@tonic-gate 	int err = 0;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	/* Don't auto unload modules on if moddebug flag is set */
7037c478bd9Sstevel@tonic-gate 	if ((moddebug & MODDEBUG_NOAUL_DRV) && (mod_aul_thread == curthread)) {
7047c478bd9Sstevel@tonic-gate 		err = EBUSY;
7057c478bd9Sstevel@tonic-gate 		goto done;
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	/* Verify modname has a driver major */
7097c478bd9Sstevel@tonic-gate 	mcp = mod_getctl(modlp);
7107c478bd9Sstevel@tonic-gate 	ASSERT(mcp != NULL);
7117c478bd9Sstevel@tonic-gate 	modname = mcp->mod_modname;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	if ((major = ddi_name_to_major(modname)) == -1) {
7147c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, uninstall_err, modname);
7157c478bd9Sstevel@tonic-gate 		err = EINVAL;
7167c478bd9Sstevel@tonic-gate 		goto done;
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	ops = modl->drv_dev_ops;
7207c478bd9Sstevel@tonic-gate 	dnp = &(devnamesp[major]);
7217c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(dnp->dn_lock));
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	dp = devopsp[major];
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	if (dp != ops)  {
7267c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "mod_removedrv: mismatched driver for %s",
7277c478bd9Sstevel@tonic-gate 		    modname);
7287c478bd9Sstevel@tonic-gate 		err = EBUSY;
7297c478bd9Sstevel@tonic-gate 		goto unlock;
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/*
7337c478bd9Sstevel@tonic-gate 	 * A driver is not unloadable if its dev_ops are held
7347c478bd9Sstevel@tonic-gate 	 */
7357c478bd9Sstevel@tonic-gate 	if (!DRV_UNLOADABLE(dp)) {
7367c478bd9Sstevel@tonic-gate 		mod_dprintf(DRV_DBG, "Cannot unload device driver <%s>,"
7377c478bd9Sstevel@tonic-gate 		    " refcnt %d\n", modname, dp->devo_refcnt);
7387c478bd9Sstevel@tonic-gate 		err = EBUSY;
7397c478bd9Sstevel@tonic-gate 		goto unlock;
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	/*
7437c478bd9Sstevel@tonic-gate 	 * OK to unload.
7447c478bd9Sstevel@tonic-gate 	 */
7457c478bd9Sstevel@tonic-gate 	if ((str = STREAMSTAB(major)) != NULL) {	/* streams driver */
7467c478bd9Sstevel@tonic-gate 		cdp = &devimpl[major];
7477c478bd9Sstevel@tonic-gate 		ASSERT(cdp->d_str == str);
7487c478bd9Sstevel@tonic-gate 		cdp->d_str = NULL;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		/* check for reference to per-dev syncq */
7517c478bd9Sstevel@tonic-gate 		if (cdp->d_dmp != NULL) {
7527c478bd9Sstevel@tonic-gate 			rele_dm(cdp->d_dmp);
7537c478bd9Sstevel@tonic-gate 			cdp->d_dmp = NULL;
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	devopsp[major] = &mod_nodev_ops;
7587c478bd9Sstevel@tonic-gate 	dnp->dn_flags &= ~(DN_DRIVER_HELD|DN_NO_AUTODETACH);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate unlock:
7617c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
7627c478bd9Sstevel@tonic-gate done:
7637c478bd9Sstevel@tonic-gate 	return (err);
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate /*
7677c478bd9Sstevel@tonic-gate  * System call subroutines
7687c478bd9Sstevel@tonic-gate  */
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate /*
7717c478bd9Sstevel@tonic-gate  * Compute system call number for given sysent and sysent table
7727c478bd9Sstevel@tonic-gate  */
7737c478bd9Sstevel@tonic-gate static int
7747c478bd9Sstevel@tonic-gate mod_infosysnum(struct modlinkage *modlp, struct sysent table[])
7757c478bd9Sstevel@tonic-gate {
7767c478bd9Sstevel@tonic-gate 	struct sysent *sysp;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	if ((sysp = mod_getsysent(modlp, table)) == NULL)
7797c478bd9Sstevel@tonic-gate 		return (-1);
7807c478bd9Sstevel@tonic-gate 	return ((int)(sysp - table));
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate /*
7847c478bd9Sstevel@tonic-gate  * Put a loadable system call entry into a sysent table.
7857c478bd9Sstevel@tonic-gate  */
7867c478bd9Sstevel@tonic-gate static int
7877c478bd9Sstevel@tonic-gate mod_installsys_sysent(
7887c478bd9Sstevel@tonic-gate 	struct modlsys		*modl,
7897c478bd9Sstevel@tonic-gate 	struct modlinkage	*modlp,
7907c478bd9Sstevel@tonic-gate 	struct sysent		table[])
7917c478bd9Sstevel@tonic-gate {
7927c478bd9Sstevel@tonic-gate 	struct sysent *sysp;
7937c478bd9Sstevel@tonic-gate 	struct sysent *mp;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate #ifdef DEBUG
7967c478bd9Sstevel@tonic-gate 	/*
7977c478bd9Sstevel@tonic-gate 	 * Before we even play with the sysent table, sanity check the
7987c478bd9Sstevel@tonic-gate 	 * incoming flags to make sure the entry is valid
7997c478bd9Sstevel@tonic-gate 	 */
8007c478bd9Sstevel@tonic-gate 	switch (modl->sys_sysent->sy_flags & SE_RVAL_MASK) {
8017c478bd9Sstevel@tonic-gate 	case SE_32RVAL1:
8027c478bd9Sstevel@tonic-gate 		/* only r_val1 returned */
8037c478bd9Sstevel@tonic-gate 	case SE_32RVAL1 | SE_32RVAL2:
8047c478bd9Sstevel@tonic-gate 		/* r_val1 and r_val2 returned */
8057c478bd9Sstevel@tonic-gate 	case SE_64RVAL:
8067c478bd9Sstevel@tonic-gate 		/* 64-bit rval returned */
8077c478bd9Sstevel@tonic-gate 		break;
8087c478bd9Sstevel@tonic-gate 	default:
8097c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "loadable syscall: %p: bad rval flags %x",
8107c478bd9Sstevel@tonic-gate 		    (void *)modl, modl->sys_sysent->sy_flags);
8117c478bd9Sstevel@tonic-gate 		return (ENOSYS);
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate #endif
8147c478bd9Sstevel@tonic-gate 	if ((sysp = mod_getsysent(modlp, table)) == NULL)
8157c478bd9Sstevel@tonic-gate 		return (ENOSPC);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	/*
8187c478bd9Sstevel@tonic-gate 	 * We should only block here until the reader in syscall gives
8197c478bd9Sstevel@tonic-gate 	 * up the lock.  Multiple writers are prevented in the mod layer.
8207c478bd9Sstevel@tonic-gate 	 */
8217c478bd9Sstevel@tonic-gate 	rw_enter(sysp->sy_lock, RW_WRITER);
8227c478bd9Sstevel@tonic-gate 	mp = modl->sys_sysent;
8237c478bd9Sstevel@tonic-gate 	sysp->sy_narg = mp->sy_narg;
8247c478bd9Sstevel@tonic-gate 	sysp->sy_call = mp->sy_call;
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	/*
8277c478bd9Sstevel@tonic-gate 	 * clear the old call method flag, and get the new one from the module.
8287c478bd9Sstevel@tonic-gate 	 */
8297c478bd9Sstevel@tonic-gate 	sysp->sy_flags &= ~SE_ARGC;
8307c478bd9Sstevel@tonic-gate 	sysp->sy_flags |= SE_LOADED |
8317c478bd9Sstevel@tonic-gate 	    (mp->sy_flags & (SE_ARGC | SE_NOUNLOAD | SE_RVAL_MASK));
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	/*
8347c478bd9Sstevel@tonic-gate 	 * If the syscall doesn't need or want unloading, it can avoid
8357c478bd9Sstevel@tonic-gate 	 * the locking overhead on each entry.  Convert the sysent to a
8367c478bd9Sstevel@tonic-gate 	 * normal non-loadable entry in that case.
8377c478bd9Sstevel@tonic-gate 	 */
8387c478bd9Sstevel@tonic-gate 	if (mp->sy_flags & SE_NOUNLOAD) {
8397c478bd9Sstevel@tonic-gate 		if (mp->sy_flags & SE_ARGC) {
8407c478bd9Sstevel@tonic-gate 			sysp->sy_callc = (int64_t (*)())mp->sy_call;
8417c478bd9Sstevel@tonic-gate 		} else {
8427c478bd9Sstevel@tonic-gate 			sysp->sy_callc = syscall_ap;
8437c478bd9Sstevel@tonic-gate 		}
8447c478bd9Sstevel@tonic-gate 		sysp->sy_flags &= ~SE_LOADABLE;
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate 	rw_exit(sysp->sy_lock);
8477c478bd9Sstevel@tonic-gate 	return (0);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /*
8517c478bd9Sstevel@tonic-gate  * Remove a loadable system call entry from a sysent table.
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate static int
8547c478bd9Sstevel@tonic-gate mod_removesys_sysent(
8557c478bd9Sstevel@tonic-gate 	struct modlsys		*modl,
8567c478bd9Sstevel@tonic-gate 	struct modlinkage	*modlp,
8577c478bd9Sstevel@tonic-gate 	struct sysent		table[])
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate 	struct sysent	*sysp;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	if ((sysp = mod_getsysent(modlp, table)) == NULL ||
8627c478bd9Sstevel@tonic-gate 	    (sysp->sy_flags & (SE_LOADABLE | SE_NOUNLOAD)) == 0 ||
8637c478bd9Sstevel@tonic-gate 	    sysp->sy_call != modl->sys_sysent->sy_call) {
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		struct modctl *mcp = mod_getctl(modlp);
8667c478bd9Sstevel@tonic-gate 		char *modname = mcp->mod_modname;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, uninstall_err, modname);
8697c478bd9Sstevel@tonic-gate 		return (EINVAL);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/* If we can't get the write lock, we can't unlink from the system */
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	if (!(moddebug & MODDEBUG_NOAUL_SYS) &&
8757c478bd9Sstevel@tonic-gate 	    rw_tryenter(sysp->sy_lock, RW_WRITER)) {
8767c478bd9Sstevel@tonic-gate 		/*
8777c478bd9Sstevel@tonic-gate 		 * Check the flags to be sure the syscall is still
8787c478bd9Sstevel@tonic-gate 		 * (un)loadable.
8797c478bd9Sstevel@tonic-gate 		 * If SE_NOUNLOAD is set, SE_LOADABLE will not be.
8807c478bd9Sstevel@tonic-gate 		 */
8817c478bd9Sstevel@tonic-gate 		if ((sysp->sy_flags & (SE_LOADED | SE_LOADABLE)) ==
8827c478bd9Sstevel@tonic-gate 		    (SE_LOADED | SE_LOADABLE)) {
8837c478bd9Sstevel@tonic-gate 			sysp->sy_flags &= ~SE_LOADED;
8847c478bd9Sstevel@tonic-gate 			sysp->sy_callc = loadable_syscall;
8857c478bd9Sstevel@tonic-gate 			sysp->sy_call = (int (*)())nosys;
8867c478bd9Sstevel@tonic-gate 			rw_exit(sysp->sy_lock);
8877c478bd9Sstevel@tonic-gate 			return (0);
8887c478bd9Sstevel@tonic-gate 		}
8897c478bd9Sstevel@tonic-gate 		rw_exit(sysp->sy_lock);
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate 	return (EBUSY);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate /*
8957c478bd9Sstevel@tonic-gate  * System call status info
8967c478bd9Sstevel@tonic-gate  */
8977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8987c478bd9Sstevel@tonic-gate static int
8997c478bd9Sstevel@tonic-gate mod_infosys(struct modlsys *modl, struct modlinkage *modlp, int *p0)
9007c478bd9Sstevel@tonic-gate {
9017c478bd9Sstevel@tonic-gate 	*p0 = mod_infosysnum(modlp, sysent);
9027c478bd9Sstevel@tonic-gate 	return (0);
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate /*
9067c478bd9Sstevel@tonic-gate  * Link a system call into the system by setting the proper sysent entry.
9077c478bd9Sstevel@tonic-gate  * Called from the module's _init routine.
9087c478bd9Sstevel@tonic-gate  */
9097c478bd9Sstevel@tonic-gate static int
9107c478bd9Sstevel@tonic-gate mod_installsys(struct modlsys *modl, struct modlinkage *modlp)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	return (mod_installsys_sysent(modl, modlp, sysent));
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate /*
9167c478bd9Sstevel@tonic-gate  * Unlink a system call from the system.
9177c478bd9Sstevel@tonic-gate  * Called from a modules _fini routine.
9187c478bd9Sstevel@tonic-gate  */
9197c478bd9Sstevel@tonic-gate static int
9207c478bd9Sstevel@tonic-gate mod_removesys(struct modlsys *modl, struct modlinkage *modlp)
9217c478bd9Sstevel@tonic-gate {
9227c478bd9Sstevel@tonic-gate 	return (mod_removesys_sysent(modl, modlp, sysent));
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate /*
9287c478bd9Sstevel@tonic-gate  * 32-bit system call status info
9297c478bd9Sstevel@tonic-gate  */
9307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9317c478bd9Sstevel@tonic-gate static int
9327c478bd9Sstevel@tonic-gate mod_infosys32(struct modlsys *modl, struct modlinkage *modlp, int *p0)
9337c478bd9Sstevel@tonic-gate {
9347c478bd9Sstevel@tonic-gate 	*p0 = mod_infosysnum(modlp, sysent32);
9357c478bd9Sstevel@tonic-gate 	return (0);
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate /*
9397c478bd9Sstevel@tonic-gate  * Link the 32-bit syscall into the system by setting the proper sysent entry.
9407c478bd9Sstevel@tonic-gate  * Also called from the module's _init routine.
9417c478bd9Sstevel@tonic-gate  */
9427c478bd9Sstevel@tonic-gate static int
9437c478bd9Sstevel@tonic-gate mod_installsys32(struct modlsys *modl, struct modlinkage *modlp)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate 	return (mod_installsys_sysent(modl, modlp, sysent32));
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate /*
9497c478bd9Sstevel@tonic-gate  * Unlink the 32-bit flavor of a system call from the system.
9507c478bd9Sstevel@tonic-gate  * Also called from a module's _fini routine.
9517c478bd9Sstevel@tonic-gate  */
9527c478bd9Sstevel@tonic-gate static int
9537c478bd9Sstevel@tonic-gate mod_removesys32(struct modlsys *modl, struct modlinkage *modlp)
9547c478bd9Sstevel@tonic-gate {
9557c478bd9Sstevel@tonic-gate 	return (mod_removesys_sysent(modl, modlp, sysent32));
9567c478bd9Sstevel@tonic-gate }
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate /*
9617c478bd9Sstevel@tonic-gate  * Filesystem status info
9627c478bd9Sstevel@tonic-gate  */
9637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9647c478bd9Sstevel@tonic-gate static int
9657c478bd9Sstevel@tonic-gate mod_infofs(struct modlfs *modl, struct modlinkage *modlp, int *p0)
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	RLOCK_VFSSW();
9707c478bd9Sstevel@tonic-gate 	if ((vswp = vfs_getvfsswbyname(modl->fs_vfsdef->name)) == NULL)
9717c478bd9Sstevel@tonic-gate 		*p0 = -1;
9727c478bd9Sstevel@tonic-gate 	else {
9737c478bd9Sstevel@tonic-gate 		*p0 = vswp - vfssw;
9747c478bd9Sstevel@tonic-gate 		vfs_unrefvfssw(vswp);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 	RUNLOCK_VFSSW();
9777c478bd9Sstevel@tonic-gate 	return (0);
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate  * Install a filesystem.
9827c478bd9Sstevel@tonic-gate  */
9837c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
9847c478bd9Sstevel@tonic-gate static int
9857c478bd9Sstevel@tonic-gate mod_installfs(struct modlfs *modl, struct modlinkage *modlp)
9867c478bd9Sstevel@tonic-gate {
9877c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
9887c478bd9Sstevel@tonic-gate 	struct modctl *mcp;
9897c478bd9Sstevel@tonic-gate 	char *fsname;
9905a59a8b3Srsb 	char ksname[KSTAT_STRLEN + 1];
9915a59a8b3Srsb 	int fstype;	/* index into vfssw[] and vsanchor_fstype[] */
9927c478bd9Sstevel@tonic-gate 	int allocated;
9937c478bd9Sstevel@tonic-gate 	int err;
9945a59a8b3Srsb 	int vsw_stats_enabled;
9955a59a8b3Srsb 	/* Not for public consumption so these aren't in a header file */
9965a59a8b3Srsb 	extern int	vopstats_enabled;
9975a59a8b3Srsb 	extern vopstats_t **vopstats_fstype;
9985a59a8b3Srsb 	extern kstat_t *new_vskstat(char *, vopstats_t *);
9995a59a8b3Srsb 	extern void initialize_vopstats(vopstats_t *);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (modl->fs_vfsdef->def_version == VFSDEF_VERSION) {
10027c478bd9Sstevel@tonic-gate 		/* Version matched */
10037c478bd9Sstevel@tonic-gate 		fsname = modl->fs_vfsdef->name;
10047c478bd9Sstevel@tonic-gate 	} else {
10057c478bd9Sstevel@tonic-gate 		if ((modl->fs_vfsdef->def_version > 0) &&
10067c478bd9Sstevel@tonic-gate 		    (modl->fs_vfsdef->def_version < VFSDEF_VERSION)) {
10077c478bd9Sstevel@tonic-gate 			/* Older VFSDEF_VERSION */
10087c478bd9Sstevel@tonic-gate 			fsname = modl->fs_vfsdef->name;
10097c478bd9Sstevel@tonic-gate 		} else if ((mcp = mod_getctl(modlp)) != NULL) {
10107c478bd9Sstevel@tonic-gate 			/* Pre-VFSDEF_VERSION */
10117c478bd9Sstevel@tonic-gate 			fsname = mcp->mod_modname;
10127c478bd9Sstevel@tonic-gate 		} else {
10137c478bd9Sstevel@tonic-gate 			/* If all else fails... */
10147c478bd9Sstevel@tonic-gate 			fsname = "<unknown file system type>";
10157c478bd9Sstevel@tonic-gate 		}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "file system '%s' version mismatch", fsname);
10187c478bd9Sstevel@tonic-gate 		return (ENXIO);
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	allocated = 0;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	WLOCK_VFSSW();
10247c478bd9Sstevel@tonic-gate 	if ((vswp = vfs_getvfsswbyname(fsname)) == NULL) {
10257c478bd9Sstevel@tonic-gate 		if ((vswp = allocate_vfssw(fsname)) == NULL) {
10267c478bd9Sstevel@tonic-gate 			WUNLOCK_VFSSW();
10277c478bd9Sstevel@tonic-gate 			/*
10287c478bd9Sstevel@tonic-gate 			 * See 1095689.  If this message appears, then
10297c478bd9Sstevel@tonic-gate 			 * we either need to make the vfssw table bigger
10307c478bd9Sstevel@tonic-gate 			 * statically, or make it grow dynamically.
10317c478bd9Sstevel@tonic-gate 			 */
10327c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "no room for '%s' in vfssw!", fsname);
10337c478bd9Sstevel@tonic-gate 			return (ENXIO);
10347c478bd9Sstevel@tonic-gate 		}
10357c478bd9Sstevel@tonic-gate 		allocated = 1;
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 	ASSERT(vswp != NULL);
10387c478bd9Sstevel@tonic-gate 
10395a59a8b3Srsb 	fstype = vswp - vfssw;	/* Pointer arithmetic to get the fstype */
10405a59a8b3Srsb 
10415a59a8b3Srsb 	/* Turn on everything by default *except* VSW_STATS */
10425a59a8b3Srsb 	vswp->vsw_flag = modl->fs_vfsdef->flags & ~(VSW_STATS);
10435a59a8b3Srsb 
10447c478bd9Sstevel@tonic-gate 	if (modl->fs_vfsdef->flags & VSW_HASPROTO) {
10457c478bd9Sstevel@tonic-gate 		vfs_mergeopttbl(&vfs_mntopts, modl->fs_vfsdef->optproto,
10467c478bd9Sstevel@tonic-gate 		    &vswp->vsw_optproto);
10477c478bd9Sstevel@tonic-gate 	} else {
10487c478bd9Sstevel@tonic-gate 		vfs_copyopttbl(&vfs_mntopts, &vswp->vsw_optproto);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	if (modl->fs_vfsdef->flags & VSW_CANRWRO) {
10527c478bd9Sstevel@tonic-gate 		/*
10537c478bd9Sstevel@tonic-gate 		 * This obviously implies VSW_CANREMOUNT.
10547c478bd9Sstevel@tonic-gate 		 */
10557c478bd9Sstevel@tonic-gate 		vswp->vsw_flag |= VSW_CANREMOUNT;
10567c478bd9Sstevel@tonic-gate 	}
10575a59a8b3Srsb 
10585a59a8b3Srsb 	/*
10595a59a8b3Srsb 	 * If stats are enabled system wide and for this fstype, then
10605a59a8b3Srsb 	 * set the VSW_STATS flag in the proper vfssw[] table entry.
10615a59a8b3Srsb 	 */
10625a59a8b3Srsb 	if (vopstats_enabled && modl->fs_vfsdef->flags & VSW_STATS) {
10635a59a8b3Srsb 		vswp->vsw_flag |= VSW_STATS;
10645a59a8b3Srsb 	}
10655a59a8b3Srsb 
10667c478bd9Sstevel@tonic-gate 	if (modl->fs_vfsdef->init == NULL)
10677c478bd9Sstevel@tonic-gate 		err = EFAULT;
10687c478bd9Sstevel@tonic-gate 	else
10695a59a8b3Srsb 		err = (*(modl->fs_vfsdef->init))(fstype, fsname);
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	if (err != 0) {
10727c478bd9Sstevel@tonic-gate 		if (allocated) {
10737c478bd9Sstevel@tonic-gate 			kmem_free(vswp->vsw_name, strlen(vswp->vsw_name)+1);
10747c478bd9Sstevel@tonic-gate 			vswp->vsw_name = "";
10757c478bd9Sstevel@tonic-gate 		}
10767c478bd9Sstevel@tonic-gate 		vswp->vsw_flag = 0;
10777c478bd9Sstevel@tonic-gate 		vswp->vsw_init = NULL;
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 
10805a59a8b3Srsb 	/* We don't want to hold the vfssw[] write lock over a kmem_alloc() */
10815a59a8b3Srsb 	vsw_stats_enabled = vswp->vsw_flag & VSW_STATS;
10825a59a8b3Srsb 
10837c478bd9Sstevel@tonic-gate 	vfs_unrefvfssw(vswp);
10847c478bd9Sstevel@tonic-gate 	WUNLOCK_VFSSW();
10857c478bd9Sstevel@tonic-gate 
10865a59a8b3Srsb 	/* If everything is on, set up the per-fstype vopstats */
10875a59a8b3Srsb 	if (vsw_stats_enabled && vopstats_enabled &&
10885a59a8b3Srsb 	    vopstats_fstype && vopstats_fstype[fstype] == NULL) {
10895a59a8b3Srsb 		(void) strlcpy(ksname, VOPSTATS_STR, sizeof (ksname));
10905a59a8b3Srsb 		(void) strlcat(ksname, vfssw[fstype].vsw_name, sizeof (ksname));
10915a59a8b3Srsb 		vopstats_fstype[fstype] =
10925a59a8b3Srsb 		    kmem_alloc(sizeof (vopstats_t), KM_SLEEP);
10935a59a8b3Srsb 		initialize_vopstats(vopstats_fstype[fstype]);
10945a59a8b3Srsb 		(void) new_vskstat(ksname, vopstats_fstype[fstype]);
10955a59a8b3Srsb 	}
10967c478bd9Sstevel@tonic-gate 	return (err);
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate /*
11007c478bd9Sstevel@tonic-gate  * Remove a filesystem
11017c478bd9Sstevel@tonic-gate  */
11027c478bd9Sstevel@tonic-gate static int
11037c478bd9Sstevel@tonic-gate mod_removefs(struct modlfs *modl, struct modlinkage *modlp)
11047c478bd9Sstevel@tonic-gate {
11057c478bd9Sstevel@tonic-gate 	struct vfssw *vswp;
11067c478bd9Sstevel@tonic-gate 	struct modctl *mcp;
11077c478bd9Sstevel@tonic-gate 	char *modname;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_NOAUL_FS)
11107c478bd9Sstevel@tonic-gate 		return (EBUSY);
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	WLOCK_VFSSW();
11137c478bd9Sstevel@tonic-gate 	if ((vswp = vfs_getvfsswbyname(modl->fs_vfsdef->name)) == NULL) {
11147c478bd9Sstevel@tonic-gate 		mcp = mod_getctl(modlp);
11157c478bd9Sstevel@tonic-gate 		ASSERT(mcp != NULL);
11167c478bd9Sstevel@tonic-gate 		modname = mcp->mod_modname;
11177c478bd9Sstevel@tonic-gate 		WUNLOCK_VFSSW();
11187c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, uninstall_err, modname);
11197c478bd9Sstevel@tonic-gate 		return (EINVAL);
11207c478bd9Sstevel@tonic-gate 	}
11217c478bd9Sstevel@tonic-gate 	if (vswp->vsw_count != 1) {
11227c478bd9Sstevel@tonic-gate 		vfs_unrefvfssw(vswp);
11237c478bd9Sstevel@tonic-gate 		WUNLOCK_VFSSW();
11247c478bd9Sstevel@tonic-gate 		return (EBUSY);
11257c478bd9Sstevel@tonic-gate 	}
11267c478bd9Sstevel@tonic-gate 
11279acbbeafSnn35248 	/*
11289acbbeafSnn35248 	 * A mounted filesystem could still have vsw_count = 0
11299acbbeafSnn35248 	 * so we must check whether anyone is actually using our ops
11309acbbeafSnn35248 	 */
11317c478bd9Sstevel@tonic-gate 	if (vfs_opsinuse(&vswp->vsw_vfsops)) {
1132d397866eSsp92102 		vfs_unrefvfssw(vswp);
11337c478bd9Sstevel@tonic-gate 		WUNLOCK_VFSSW();
11347c478bd9Sstevel@tonic-gate 		return (EBUSY);
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	vfs_freeopttbl(&vswp->vsw_optproto);
11387c478bd9Sstevel@tonic-gate 	vswp->vsw_optproto.mo_count = 0;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	vswp->vsw_flag = 0;
11417c478bd9Sstevel@tonic-gate 	vswp->vsw_init = NULL;
11427c478bd9Sstevel@tonic-gate 	vfs_unrefvfssw(vswp);
11437c478bd9Sstevel@tonic-gate 	WUNLOCK_VFSSW();
11447c478bd9Sstevel@tonic-gate 	return (0);
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate /*
11487c478bd9Sstevel@tonic-gate  * Get status of a streams module.
11497c478bd9Sstevel@tonic-gate  */
11507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11517c478bd9Sstevel@tonic-gate static int
11527c478bd9Sstevel@tonic-gate mod_infostrmod(struct modlstrmod *modl, struct modlinkage *modlp, int *p0)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	*p0 = -1;	/* no useful info */
11557c478bd9Sstevel@tonic-gate 	return (0);
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate /*
11607c478bd9Sstevel@tonic-gate  * Install a streams module.
11617c478bd9Sstevel@tonic-gate  */
11627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11637c478bd9Sstevel@tonic-gate static int
11647c478bd9Sstevel@tonic-gate mod_installstrmod(struct modlstrmod *modl, struct modlinkage *modlp)
11657c478bd9Sstevel@tonic-gate {
11667c478bd9Sstevel@tonic-gate 	struct fmodsw *fp = modl->strmod_fmodsw;
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	if (!(fp->f_flag & D_MP)) {
11697c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "mod_install: MT-unsafe strmod '%s' rejected",
11707c478bd9Sstevel@tonic-gate 		    fp->f_name);
11717c478bd9Sstevel@tonic-gate 		return (ENXIO);
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	return (fmodsw_register(fp->f_name, fp->f_str, fp->f_flag));
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate /*
11787c478bd9Sstevel@tonic-gate  * Remove a streams module.
11797c478bd9Sstevel@tonic-gate  */
11807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11817c478bd9Sstevel@tonic-gate static int
11827c478bd9Sstevel@tonic-gate mod_removestrmod(struct modlstrmod *modl, struct modlinkage *modlp)
11837c478bd9Sstevel@tonic-gate {
11847c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_NOAUL_STR)
11857c478bd9Sstevel@tonic-gate 		return (EBUSY);
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	return (fmodsw_unregister(modl->strmod_fmodsw->f_name));
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate /*
1191*0f1702c5SYu Xiangning  * Get status of a socket module.
1192*0f1702c5SYu Xiangning  */
1193*0f1702c5SYu Xiangning /*ARGSUSED*/
1194*0f1702c5SYu Xiangning static int
1195*0f1702c5SYu Xiangning mod_infosockmod(struct modlsockmod *modl, struct modlinkage *modlp, int *p0)
1196*0f1702c5SYu Xiangning {
1197*0f1702c5SYu Xiangning 	*p0 = -1;	/* no useful info */
1198*0f1702c5SYu Xiangning 	return (0);
1199*0f1702c5SYu Xiangning }
1200*0f1702c5SYu Xiangning 
1201*0f1702c5SYu Xiangning /*
1202*0f1702c5SYu Xiangning  * Install a socket module.
1203*0f1702c5SYu Xiangning  */
1204*0f1702c5SYu Xiangning /*ARGSUSED*/
1205*0f1702c5SYu Xiangning static int
1206*0f1702c5SYu Xiangning mod_installsockmod(struct modlsockmod *modl, struct modlinkage *modlp)
1207*0f1702c5SYu Xiangning {
1208*0f1702c5SYu Xiangning 	struct modctl *mcp;
1209*0f1702c5SYu Xiangning 	char *mod_name;
1210*0f1702c5SYu Xiangning 
1211*0f1702c5SYu Xiangning 	mcp = mod_getctl(modlp);
1212*0f1702c5SYu Xiangning 	ASSERT(mcp != NULL);
1213*0f1702c5SYu Xiangning 	mod_name = mcp->mod_modname;
1214*0f1702c5SYu Xiangning 	if (strcmp(mod_name, modl->sockmod_reg_info->smod_name) != 0) {
1215*0f1702c5SYu Xiangning #ifdef DEBUG
1216*0f1702c5SYu Xiangning 		cmn_err(CE_CONT, "mod_installsockmod: different names"
1217*0f1702c5SYu Xiangning 		    " %s != %s \n", mod_name,
1218*0f1702c5SYu Xiangning 		    modl->sockmod_reg_info->smod_name);
1219*0f1702c5SYu Xiangning #endif
1220*0f1702c5SYu Xiangning 		return (EINVAL);
1221*0f1702c5SYu Xiangning 	}
1222*0f1702c5SYu Xiangning 
1223*0f1702c5SYu Xiangning 	/*
1224*0f1702c5SYu Xiangning 	 * Register module.
1225*0f1702c5SYu Xiangning 	 */
1226*0f1702c5SYu Xiangning 	return (smod_register(modl->sockmod_reg_info));
1227*0f1702c5SYu Xiangning }
1228*0f1702c5SYu Xiangning 
1229*0f1702c5SYu Xiangning /*
1230*0f1702c5SYu Xiangning  * Remove a socket module.
1231*0f1702c5SYu Xiangning  */
1232*0f1702c5SYu Xiangning /*ARGSUSED*/
1233*0f1702c5SYu Xiangning static int
1234*0f1702c5SYu Xiangning mod_removesockmod(struct modlsockmod *modl, struct modlinkage *modlp)
1235*0f1702c5SYu Xiangning {
1236*0f1702c5SYu Xiangning 	/*
1237*0f1702c5SYu Xiangning 	 * unregister from the global socket creation table
1238*0f1702c5SYu Xiangning 	 * check the refcnt in the lookup table
1239*0f1702c5SYu Xiangning 	 */
1240*0f1702c5SYu Xiangning 	return (smod_unregister(modl->sockmod_reg_info->smod_name));
1241*0f1702c5SYu Xiangning }
1242*0f1702c5SYu Xiangning 
1243*0f1702c5SYu Xiangning /*
12447c478bd9Sstevel@tonic-gate  * Get status of a scheduling class module.
12457c478bd9Sstevel@tonic-gate  */
12467c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
12477c478bd9Sstevel@tonic-gate static int
12487c478bd9Sstevel@tonic-gate mod_infosched(struct modlsched *modl, struct modlinkage *modlp, int *p0)
12497c478bd9Sstevel@tonic-gate {
12507c478bd9Sstevel@tonic-gate 	int	status;
12517c478bd9Sstevel@tonic-gate 	auto id_t	cid;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	status = getcidbyname(modl->sched_class->cl_name, &cid);
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	if (status != 0)
12567c478bd9Sstevel@tonic-gate 		*p0 = -1;
12577c478bd9Sstevel@tonic-gate 	else
12587c478bd9Sstevel@tonic-gate 		*p0 = cid;
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	return (0);
12617c478bd9Sstevel@tonic-gate }
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate /*
12647c478bd9Sstevel@tonic-gate  * Install a scheduling class module.
12657c478bd9Sstevel@tonic-gate  */
12667c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
12677c478bd9Sstevel@tonic-gate static int
12687c478bd9Sstevel@tonic-gate mod_installsched(struct modlsched *modl, struct modlinkage *modlp)
12697c478bd9Sstevel@tonic-gate {
12707c478bd9Sstevel@tonic-gate 	sclass_t *clp;
12717c478bd9Sstevel@tonic-gate 	int status;
12727c478bd9Sstevel@tonic-gate 	id_t cid;
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	/*
12757c478bd9Sstevel@tonic-gate 	 * See if module is already installed.
12767c478bd9Sstevel@tonic-gate 	 */
12777c478bd9Sstevel@tonic-gate 	mutex_enter(&class_lock);
12787c478bd9Sstevel@tonic-gate 	status = alloc_cid(modl->sched_class->cl_name, &cid);
12797c478bd9Sstevel@tonic-gate 	mutex_exit(&class_lock);
12807c478bd9Sstevel@tonic-gate 	ASSERT(status == 0);
12817c478bd9Sstevel@tonic-gate 	clp = &sclass[cid];
12827c478bd9Sstevel@tonic-gate 	rw_enter(clp->cl_lock, RW_WRITER);
12837c478bd9Sstevel@tonic-gate 	if (SCHED_INSTALLED(clp)) {
12847c478bd9Sstevel@tonic-gate 		printf("scheduling class %s is already installed\n",
12857c478bd9Sstevel@tonic-gate 		    modl->sched_class->cl_name);
12867c478bd9Sstevel@tonic-gate 		rw_exit(clp->cl_lock);
12877c478bd9Sstevel@tonic-gate 		return (EBUSY);		/* it's already there */
12887c478bd9Sstevel@tonic-gate 	}
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	clp->cl_init = modl->sched_class->cl_init;
12917c478bd9Sstevel@tonic-gate 	clp->cl_funcs = modl->sched_class->cl_funcs;
12927c478bd9Sstevel@tonic-gate 	modl->sched_class = clp;
12937c478bd9Sstevel@tonic-gate 	disp_add(clp);
12947c478bd9Sstevel@tonic-gate 	loaded_classes++;		/* for priocntl system call */
12957c478bd9Sstevel@tonic-gate 	rw_exit(clp->cl_lock);
12967c478bd9Sstevel@tonic-gate 	return (0);
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate /*
13007c478bd9Sstevel@tonic-gate  * Remove a scheduling class module.
13017c478bd9Sstevel@tonic-gate  *
13027c478bd9Sstevel@tonic-gate  * we only null out the init func and the class functions because
13037c478bd9Sstevel@tonic-gate  * once a class has been loaded it has that slot in the class
13047c478bd9Sstevel@tonic-gate  * array until the next reboot. We don't decrement loaded_classes
13057c478bd9Sstevel@tonic-gate  * because this keeps count of the number of classes that have
13067c478bd9Sstevel@tonic-gate  * been loaded for this session. It will have to be this way until
13077c478bd9Sstevel@tonic-gate  * we implement the class array as a linked list and do true
13087c478bd9Sstevel@tonic-gate  * dynamic allocation.
13097c478bd9Sstevel@tonic-gate  */
13107c478bd9Sstevel@tonic-gate static int
13117c478bd9Sstevel@tonic-gate mod_removesched(struct modlsched *modl, struct modlinkage *modlp)
13127c478bd9Sstevel@tonic-gate {
13137c478bd9Sstevel@tonic-gate 	int status;
13147c478bd9Sstevel@tonic-gate 	sclass_t *clp;
13157c478bd9Sstevel@tonic-gate 	struct modctl *mcp;
13167c478bd9Sstevel@tonic-gate 	char *modname;
13177c478bd9Sstevel@tonic-gate 	id_t cid;
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	status = getcidbyname(modl->sched_class->cl_name, &cid);
13207c478bd9Sstevel@tonic-gate 	if (status != 0) {
13217c478bd9Sstevel@tonic-gate 		mcp = mod_getctl(modlp);
13227c478bd9Sstevel@tonic-gate 		ASSERT(mcp != NULL);
13237c478bd9Sstevel@tonic-gate 		modname = mcp->mod_modname;
13247c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, uninstall_err, modname);
13257c478bd9Sstevel@tonic-gate 		return (EINVAL);
13267c478bd9Sstevel@tonic-gate 	}
13277c478bd9Sstevel@tonic-gate 	clp = &sclass[cid];
13287c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_NOAUL_SCHED ||
13297c478bd9Sstevel@tonic-gate 	    !rw_tryenter(clp->cl_lock, RW_WRITER))
13307c478bd9Sstevel@tonic-gate 		return (EBUSY);
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	clp->cl_init = NULL;
13337c478bd9Sstevel@tonic-gate 	clp->cl_funcs = NULL;
13347c478bd9Sstevel@tonic-gate 	rw_exit(clp->cl_lock);
13357c478bd9Sstevel@tonic-gate 	return (0);
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate /*
13397c478bd9Sstevel@tonic-gate  * Get status of an exec module.
13407c478bd9Sstevel@tonic-gate  */
13417c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
13427c478bd9Sstevel@tonic-gate static int
13437c478bd9Sstevel@tonic-gate mod_infoexec(struct modlexec *modl, struct modlinkage *modlp, int *p0)
13447c478bd9Sstevel@tonic-gate {
13457c478bd9Sstevel@tonic-gate 	struct execsw *eswp;
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	if ((eswp = findexecsw(modl->exec_execsw->exec_magic)) == NULL)
13487c478bd9Sstevel@tonic-gate 		*p0 = -1;
13497c478bd9Sstevel@tonic-gate 	else
13507c478bd9Sstevel@tonic-gate 		*p0 = eswp - execsw;
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	return (0);
13537c478bd9Sstevel@tonic-gate }
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate /*
13567c478bd9Sstevel@tonic-gate  * Install an exec module.
13577c478bd9Sstevel@tonic-gate  */
13587c478bd9Sstevel@tonic-gate static int
13597c478bd9Sstevel@tonic-gate mod_installexec(struct modlexec *modl, struct modlinkage *modlp)
13607c478bd9Sstevel@tonic-gate {
13617c478bd9Sstevel@tonic-gate 	struct execsw *eswp;
13627c478bd9Sstevel@tonic-gate 	struct modctl *mcp;
13637c478bd9Sstevel@tonic-gate 	char *modname;
13647c478bd9Sstevel@tonic-gate 	char *magic;
13657c478bd9Sstevel@tonic-gate 	size_t magic_size;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	/*
13687c478bd9Sstevel@tonic-gate 	 * See if execsw entry is already allocated.  Can't use findexectype()
13697c478bd9Sstevel@tonic-gate 	 * because we may get a recursive call to here.
13707c478bd9Sstevel@tonic-gate 	 */
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	if ((eswp = findexecsw(modl->exec_execsw->exec_magic)) == NULL) {
13737c478bd9Sstevel@tonic-gate 		mcp = mod_getctl(modlp);
13747c478bd9Sstevel@tonic-gate 		ASSERT(mcp != NULL);
13757c478bd9Sstevel@tonic-gate 		modname = mcp->mod_modname;
13767c478bd9Sstevel@tonic-gate 		magic = modl->exec_execsw->exec_magic;
13777c478bd9Sstevel@tonic-gate 		magic_size = modl->exec_execsw->exec_maglen;
13787c478bd9Sstevel@tonic-gate 		if ((eswp = allocate_execsw(modname, magic, magic_size)) ==
13797c478bd9Sstevel@tonic-gate 		    NULL) {
13807c478bd9Sstevel@tonic-gate 			printf("no unused entries in 'execsw'\n");
13817c478bd9Sstevel@tonic-gate 			return (ENOSPC);
13827c478bd9Sstevel@tonic-gate 		}
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 	if (eswp->exec_func != NULL) {
13857c478bd9Sstevel@tonic-gate 		printf("exec type %x is already installed\n",
13867c478bd9Sstevel@tonic-gate 		    *eswp->exec_magic);
13877c478bd9Sstevel@tonic-gate 			return (EBUSY);		 /* it's already there! */
13887c478bd9Sstevel@tonic-gate 	}
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	rw_enter(eswp->exec_lock, RW_WRITER);
13917c478bd9Sstevel@tonic-gate 	eswp->exec_func = modl->exec_execsw->exec_func;
13927c478bd9Sstevel@tonic-gate 	eswp->exec_core = modl->exec_execsw->exec_core;
13937c478bd9Sstevel@tonic-gate 	rw_exit(eswp->exec_lock);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	return (0);
13967c478bd9Sstevel@tonic-gate }
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate /*
13997c478bd9Sstevel@tonic-gate  * Remove an exec module.
14007c478bd9Sstevel@tonic-gate  */
14017c478bd9Sstevel@tonic-gate static int
14027c478bd9Sstevel@tonic-gate mod_removeexec(struct modlexec *modl, struct modlinkage *modlp)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate 	struct execsw *eswp;
14057c478bd9Sstevel@tonic-gate 	struct modctl *mcp;
14067c478bd9Sstevel@tonic-gate 	char *modname;
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	eswp = findexecsw(modl->exec_execsw->exec_magic);
14097c478bd9Sstevel@tonic-gate 	if (eswp == NULL) {
14107c478bd9Sstevel@tonic-gate 		mcp = mod_getctl(modlp);
14117c478bd9Sstevel@tonic-gate 		ASSERT(mcp != NULL);
14127c478bd9Sstevel@tonic-gate 		modname = mcp->mod_modname;
14137c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, uninstall_err, modname);
14147c478bd9Sstevel@tonic-gate 		return (EINVAL);
14157c478bd9Sstevel@tonic-gate 	}
14167c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_NOAUL_EXEC ||
14177c478bd9Sstevel@tonic-gate 	    !rw_tryenter(eswp->exec_lock, RW_WRITER))
14187c478bd9Sstevel@tonic-gate 		return (EBUSY);
14197c478bd9Sstevel@tonic-gate 	eswp->exec_func = NULL;
14207c478bd9Sstevel@tonic-gate 	eswp->exec_core = NULL;
14217c478bd9Sstevel@tonic-gate 	rw_exit(eswp->exec_lock);
14227c478bd9Sstevel@tonic-gate 	return (0);
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate /*
14267c478bd9Sstevel@tonic-gate  * Find a free sysent entry or check if the specified one is free.
14277c478bd9Sstevel@tonic-gate  */
14287c478bd9Sstevel@tonic-gate static struct sysent *
14297c478bd9Sstevel@tonic-gate mod_getsysent(struct modlinkage *modlp, struct sysent *se)
14307c478bd9Sstevel@tonic-gate {
14317c478bd9Sstevel@tonic-gate 	int sysnum;
14327c478bd9Sstevel@tonic-gate 	struct modctl *mcp;
14337c478bd9Sstevel@tonic-gate 	char *mod_name;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	if ((mcp = mod_getctl(modlp)) == NULL) {
14367c478bd9Sstevel@tonic-gate 		/*
14377c478bd9Sstevel@tonic-gate 		 * This happens when we're looking up the module
14387c478bd9Sstevel@tonic-gate 		 * pointer as part of a stub installation.  So
14397c478bd9Sstevel@tonic-gate 		 * there's no need to whine at this point.
14407c478bd9Sstevel@tonic-gate 		 */
14417c478bd9Sstevel@tonic-gate 		return (NULL);
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	mod_name = mcp->mod_modname;
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	if ((sysnum = mod_getsysnum(mod_name)) == -1) {
14477c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "system call missing from bind file");
14487c478bd9Sstevel@tonic-gate 		return (NULL);
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	if (sysnum > 0 && sysnum < NSYSCALL &&
14527c478bd9Sstevel@tonic-gate 	    (se[sysnum].sy_flags & (SE_LOADABLE | SE_NOUNLOAD)))
14537c478bd9Sstevel@tonic-gate 		return (se + sysnum);
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	cmn_err(CE_WARN, "system call entry %d is already in use", sysnum);
14567c478bd9Sstevel@tonic-gate 	return (NULL);
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate /*
14607c478bd9Sstevel@tonic-gate  * IP Policy Modules.
14617c478bd9Sstevel@tonic-gate  */
14627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14637c478bd9Sstevel@tonic-gate static int
14647c478bd9Sstevel@tonic-gate mod_infoipp(struct modlipp *modl, struct modlinkage *modlp, int *p0)
14657c478bd9Sstevel@tonic-gate {
14667c478bd9Sstevel@tonic-gate 	struct modctl *mcp = mod_getctl(modlp);
14677c478bd9Sstevel@tonic-gate 	ipp_mod_id_t mid;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	if (mcp == NULL) {
14707c478bd9Sstevel@tonic-gate 		*p0 = -1;
14717c478bd9Sstevel@tonic-gate 		return (0);	/* module is not yet installed */
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	mid = ipp_mod_lookup(mcp->mod_modname);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	*p0 = mid;
14777c478bd9Sstevel@tonic-gate 	return (0);
14787c478bd9Sstevel@tonic-gate }
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate static int
14817c478bd9Sstevel@tonic-gate mod_installipp(struct modlipp *modl, struct modlinkage *modlp)
14827c478bd9Sstevel@tonic-gate {
14837c478bd9Sstevel@tonic-gate 	struct modctl *mcp = mod_getctl(modlp);
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	ASSERT(mcp != NULL);
14867c478bd9Sstevel@tonic-gate 	return (ipp_mod_register(mcp->mod_modname, modl->ipp_ops));
14877c478bd9Sstevel@tonic-gate }
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14907c478bd9Sstevel@tonic-gate static int
14917c478bd9Sstevel@tonic-gate mod_removeipp(struct modlipp *modl, struct modlinkage *modlp)
14927c478bd9Sstevel@tonic-gate {
14937c478bd9Sstevel@tonic-gate 	struct modctl *mcp = mod_getctl(modlp);
14947c478bd9Sstevel@tonic-gate 	extern kthread_id_t mod_aul_thread;
14957c478bd9Sstevel@tonic-gate 	ipp_mod_id_t mid;
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	ASSERT(mcp != NULL);
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	if ((moddebug & MODDEBUG_NOAUL_IPP) && (mod_aul_thread == curthread))
15007c478bd9Sstevel@tonic-gate 		return (EBUSY);
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	mid = ipp_mod_lookup(mcp->mod_modname);
15037c478bd9Sstevel@tonic-gate 	ASSERT(mid != IPP_MOD_INVAL);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	return (ipp_mod_unregister(mid));
15067c478bd9Sstevel@tonic-gate }
1507d14d7d31Sis 
1508d14d7d31Sis /*
1509d14d7d31Sis  * Manage kiconv modules.
1510d14d7d31Sis  */
1511d14d7d31Sis /*ARGSUSED*/
1512d14d7d31Sis static int
1513d14d7d31Sis mod_installkiconv(struct modlkiconv *modl, struct modlinkage *modlp)
1514d14d7d31Sis {
1515d14d7d31Sis 	return (kiconv_register_module(modl->kiconv_moddef));
1516d14d7d31Sis }
1517d14d7d31Sis 
1518d14d7d31Sis /*ARGSUSED*/
1519d14d7d31Sis static int
1520d14d7d31Sis mod_removekiconv(struct modlkiconv *modl, struct modlinkage *modlp)
1521d14d7d31Sis {
1522d14d7d31Sis 	return (kiconv_unregister_module(modl->kiconv_moddef));
1523d14d7d31Sis }
1524