xref: /titanic_44/usr/src/uts/common/os/modctl.c (revision 269473047d747f7815af570197e4ef7322d3632c)
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
5facf4a8dSllai1  * Common Development and Distribution License (the "License").
6facf4a8dSllai1  * 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  */
217aec1d6eScindi 
227c478bd9Sstevel@tonic-gate /*
236532b960SJerry Gilliam  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * modctl system call for loadable module support.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/user.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/exec.h>
357c478bd9Sstevel@tonic-gate #include <sys/file.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/conf.h>
387c478bd9Sstevel@tonic-gate #include <sys/time.h>
397c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
407c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
417c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysconf.h>
437c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
447c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
457c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
467c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
477c478bd9Sstevel@tonic-gate #include <sys/ndi_impldefs.h>
487c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
497c478bd9Sstevel@tonic-gate #include <sys/ddi_implfuncs.h>
507c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
517c478bd9Sstevel@tonic-gate #include <sys/dc_ki.h>
527c478bd9Sstevel@tonic-gate #include <sys/cladm.h>
537c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
547c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h>
577c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
587c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
597c478bd9Sstevel@tonic-gate #include <sys/devops.h>
607c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
617c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
627c478bd9Sstevel@tonic-gate #include <sys/callb.h>
637c478bd9Sstevel@tonic-gate #include <sys/debug.h>
647c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
657c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
667c478bd9Sstevel@tonic-gate #include <sys/sysevent.h>
677c478bd9Sstevel@tonic-gate #include <sys/sysevent_impl.h>
687c478bd9Sstevel@tonic-gate #include <sys/instance.h>
697c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
707c478bd9Sstevel@tonic-gate #include <sys/modhash_impl.h>
717c478bd9Sstevel@tonic-gate #include <sys/dacf_impl.h>
727c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
737c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
747c478bd9Sstevel@tonic-gate #include <sys/console.h>
757c478bd9Sstevel@tonic-gate #include <sys/policy.h>
767c478bd9Sstevel@tonic-gate #include <ipp/ipp_impl.h>
777c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
787c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
793c5e027bSEric Taylor #include <sys/fs/sdev_impl.h>
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static int		mod_circdep(struct modctl *);
827c478bd9Sstevel@tonic-gate static int		modinfo(modid_t, struct modinfo *);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static void		mod_uninstall_all(void);
857c478bd9Sstevel@tonic-gate static int		mod_getinfo(struct modctl *, struct modinfo *);
867aec1d6eScindi static struct modctl	*allocate_modp(const char *, const char *);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate static int		mod_load(struct modctl *, int);
897c478bd9Sstevel@tonic-gate static void		mod_unload(struct modctl *);
907c478bd9Sstevel@tonic-gate static int		modinstall(struct modctl *);
917c478bd9Sstevel@tonic-gate static int		moduninstall(struct modctl *);
927c478bd9Sstevel@tonic-gate 
937aec1d6eScindi static struct modctl	*mod_hold_by_name_common(struct modctl *, const char *);
947c478bd9Sstevel@tonic-gate static struct modctl	*mod_hold_next_by_id(modid_t);
957c478bd9Sstevel@tonic-gate static struct modctl	*mod_hold_loaded_mod(struct modctl *, char *, int *);
9620c794b3Sgavinm static struct modctl	*mod_hold_installed_mod(char *, int, int, int *);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static void		mod_release(struct modctl *);
997c478bd9Sstevel@tonic-gate static void		mod_make_requisite(struct modctl *, struct modctl *);
1007c478bd9Sstevel@tonic-gate static int		mod_install_requisites(struct modctl *);
1017c478bd9Sstevel@tonic-gate static void		check_esc_sequences(char *, char *);
1027c478bd9Sstevel@tonic-gate static struct modctl	*mod_hold_by_name_requisite(struct modctl *, char *);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * module loading thread control structure. Calls to kobj_load_module()() are
1067c478bd9Sstevel@tonic-gate  * handled off to a separate thead using this structure.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate struct loadmt {
1097c478bd9Sstevel@tonic-gate 	ksema_t		sema;
1107c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
1117c478bd9Sstevel@tonic-gate 	int		usepath;
1127c478bd9Sstevel@tonic-gate 	kthread_t	*owner;
1137c478bd9Sstevel@tonic-gate 	int		retval;
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static void	modload_thread(struct loadmt *);
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate kcondvar_t	mod_cv;
1197c478bd9Sstevel@tonic-gate kcondvar_t	mod_uninstall_cv;	/* Communication between swapper */
1207c478bd9Sstevel@tonic-gate 					/* and the uninstall daemon. */
1217c478bd9Sstevel@tonic-gate kmutex_t	mod_lock;		/* protects &modules insert linkage, */
1227c478bd9Sstevel@tonic-gate 					/* mod_busy, mod_want, and mod_ref. */
1237c478bd9Sstevel@tonic-gate 					/* blocking operations while holding */
1247c478bd9Sstevel@tonic-gate 					/* mod_lock should be avoided */
1257c478bd9Sstevel@tonic-gate kmutex_t	mod_uninstall_lock;	/* protects mod_uninstall_cv */
1267c478bd9Sstevel@tonic-gate kthread_id_t	mod_aul_thread;
1277c478bd9Sstevel@tonic-gate 
128a7aa4df7Scth int		modunload_wait;
129a7aa4df7Scth kmutex_t	modunload_wait_mutex;
130a7aa4df7Scth kcondvar_t	modunload_wait_cv;
131a7aa4df7Scth int		modunload_active_count;
132a7aa4df7Scth int		modunload_disable_count;
133a7aa4df7Scth 
1347c478bd9Sstevel@tonic-gate int	isminiroot;		/* set if running as miniroot */
1357c478bd9Sstevel@tonic-gate int	modrootloaded;		/* set after root driver and fs are loaded */
1367c478bd9Sstevel@tonic-gate int	moddebug = 0x0;		/* debug flags for module writers */
1377c478bd9Sstevel@tonic-gate int	swaploaded;		/* set after swap driver and fs are loaded */
1387c478bd9Sstevel@tonic-gate int	bop_io_quiesced = 0;	/* set when BOP I/O can no longer be used */
1397c478bd9Sstevel@tonic-gate int	last_module_id;
1407c478bd9Sstevel@tonic-gate clock_t	mod_uninstall_interval = 0;
1417c478bd9Sstevel@tonic-gate int	ddi_modclose_unload = 1;	/* 0 -> just decrement reference */
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate struct devnames *devnamesp;
1447c478bd9Sstevel@tonic-gate struct devnames orphanlist;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate krwlock_t	devinfo_tree_lock;	/* obsolete, to be removed */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate #define	MAJBINDFILE "/etc/name_to_major"
1497c478bd9Sstevel@tonic-gate #define	SYSBINDFILE "/etc/name_to_sysnum"
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static char	majbind[] = MAJBINDFILE;
1527c478bd9Sstevel@tonic-gate static char	sysbind[] = SYSBINDFILE;
1537c478bd9Sstevel@tonic-gate static uint_t	mod_autounload_key;	/* for module autounload detection */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate extern int obpdebug;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #define	DEBUGGER_PRESENT	((boothowto & RB_DEBUG) || (obpdebug != 0))
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate static int minorperm_loaded = 0;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate void
1627c478bd9Sstevel@tonic-gate mod_setup(void)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	struct sysent *callp;
1657c478bd9Sstevel@tonic-gate 	int callnum, exectype;
1667c478bd9Sstevel@tonic-gate 	int	num_devs;
1677c478bd9Sstevel@tonic-gate 	int	i;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Initialize the list of loaded driver dev_ops.
1717c478bd9Sstevel@tonic-gate 	 * XXX - This must be done before reading the system file so that
1727c478bd9Sstevel@tonic-gate 	 * forceloads of drivers will work.
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	num_devs = read_binding_file(majbind, mb_hashtab, make_mbind);
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * Since read_binding_file is common code, it doesn't enforce that all
1777c478bd9Sstevel@tonic-gate 	 * of the binding file entries have major numbers <= MAXMAJ32.	Thus,
1787c478bd9Sstevel@tonic-gate 	 * ensure that we don't allocate some massive amount of space due to a
1797c478bd9Sstevel@tonic-gate 	 * bad entry.  We can't have major numbers bigger than MAXMAJ32
1807c478bd9Sstevel@tonic-gate 	 * until file system support for larger major numbers exists.
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * Leave space for expansion, but not more than L_MAXMAJ32
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	devcnt = MIN(num_devs + 30, L_MAXMAJ32);
1877c478bd9Sstevel@tonic-gate 	devopsp = kmem_alloc(devcnt * sizeof (struct dev_ops *), KM_SLEEP);
1887c478bd9Sstevel@tonic-gate 	for (i = 0; i < devcnt; i++)
1897c478bd9Sstevel@tonic-gate 		devopsp[i] = &mod_nodev_ops;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	init_devnamesp(devcnt);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/*
1947c478bd9Sstevel@tonic-gate 	 * Sync up with the work that the stand-alone linker has already done.
1957c478bd9Sstevel@tonic-gate 	 */
1967c478bd9Sstevel@tonic-gate 	(void) kobj_sync();
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
1997c478bd9Sstevel@tonic-gate 		kdi_dvec_modavail();
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	make_aliases(mb_hashtab);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/*
2047c478bd9Sstevel@tonic-gate 	 * Initialize streams device implementation structures.
2057c478bd9Sstevel@tonic-gate 	 */
2067c478bd9Sstevel@tonic-gate 	devimpl = kmem_zalloc(devcnt * sizeof (cdevsw_impl_t), KM_SLEEP);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * If the cl_bootstrap module is present,
2107c478bd9Sstevel@tonic-gate 	 * we should be configured as a cluster. Loading this module
2117c478bd9Sstevel@tonic-gate 	 * will set "cluster_bootflags" to non-zero.
2127c478bd9Sstevel@tonic-gate 	 */
2137c478bd9Sstevel@tonic-gate 	(void) modload("misc", "cl_bootstrap");
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	(void) read_binding_file(sysbind, sb_hashtab, make_mbind);
2167c478bd9Sstevel@tonic-gate 	init_syscallnames(NSYSCALL);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	/*
2197c478bd9Sstevel@tonic-gate 	 * Start up dynamic autoconfiguration framework (dacf).
2207c478bd9Sstevel@tonic-gate 	 */
2217c478bd9Sstevel@tonic-gate 	mod_hash_init();
2227c478bd9Sstevel@tonic-gate 	dacf_init();
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * Start up IP policy framework (ipp).
2267c478bd9Sstevel@tonic-gate 	 */
2277c478bd9Sstevel@tonic-gate 	ipp_init();
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * Allocate loadable native system call locks.
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	for (callnum = 0, callp = sysent; callnum < NSYSCALL;
2337c478bd9Sstevel@tonic-gate 	    callnum++, callp++) {
2347c478bd9Sstevel@tonic-gate 		if (LOADABLE_SYSCALL(callp)) {
2357c478bd9Sstevel@tonic-gate 			if (mod_getsysname(callnum) != NULL) {
2367c478bd9Sstevel@tonic-gate 				callp->sy_lock =
2377c478bd9Sstevel@tonic-gate 				    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
2387c478bd9Sstevel@tonic-gate 				rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
2397c478bd9Sstevel@tonic-gate 			} else {
2407c478bd9Sstevel@tonic-gate 				callp->sy_flags &= ~SE_LOADABLE;
2417c478bd9Sstevel@tonic-gate 				callp->sy_callc = nosys;
2427c478bd9Sstevel@tonic-gate 			}
2437c478bd9Sstevel@tonic-gate #ifdef DEBUG
2447c478bd9Sstevel@tonic-gate 		} else {
2457c478bd9Sstevel@tonic-gate 			/*
2467c478bd9Sstevel@tonic-gate 			 * Do some sanity checks on the sysent table
2477c478bd9Sstevel@tonic-gate 			 */
2487c478bd9Sstevel@tonic-gate 			switch (callp->sy_flags & SE_RVAL_MASK) {
2497c478bd9Sstevel@tonic-gate 			case SE_32RVAL1:
2507c478bd9Sstevel@tonic-gate 				/* only r_val1 returned */
2517c478bd9Sstevel@tonic-gate 			case SE_32RVAL1 | SE_32RVAL2:
2527c478bd9Sstevel@tonic-gate 				/* r_val1 and r_val2 returned */
2537c478bd9Sstevel@tonic-gate 			case SE_64RVAL:
2547c478bd9Sstevel@tonic-gate 				/* 64-bit rval returned */
2557c478bd9Sstevel@tonic-gate 				break;
2567c478bd9Sstevel@tonic-gate 			default:
2577c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent[%d]: bad flags %x",
2587c478bd9Sstevel@tonic-gate 				    callnum, callp->sy_flags);
2597c478bd9Sstevel@tonic-gate 			}
2607c478bd9Sstevel@tonic-gate #endif
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * Allocate loadable system call locks for 32-bit compat syscalls
2677c478bd9Sstevel@tonic-gate 	 */
2687c478bd9Sstevel@tonic-gate 	for (callnum = 0, callp = sysent32; callnum < NSYSCALL;
2697c478bd9Sstevel@tonic-gate 	    callnum++, callp++) {
2707c478bd9Sstevel@tonic-gate 		if (LOADABLE_SYSCALL(callp)) {
2717c478bd9Sstevel@tonic-gate 			if (mod_getsysname(callnum) != NULL) {
2727c478bd9Sstevel@tonic-gate 				callp->sy_lock =
2737c478bd9Sstevel@tonic-gate 				    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
2747c478bd9Sstevel@tonic-gate 				rw_init(callp->sy_lock, NULL, RW_DEFAULT, NULL);
2757c478bd9Sstevel@tonic-gate 			} else {
2767c478bd9Sstevel@tonic-gate 				callp->sy_flags &= ~SE_LOADABLE;
2777c478bd9Sstevel@tonic-gate 				callp->sy_callc = nosys;
2787c478bd9Sstevel@tonic-gate 			}
2797c478bd9Sstevel@tonic-gate #ifdef DEBUG
2807c478bd9Sstevel@tonic-gate 		} else {
2817c478bd9Sstevel@tonic-gate 			/*
2827c478bd9Sstevel@tonic-gate 			 * Do some sanity checks on the sysent table
2837c478bd9Sstevel@tonic-gate 			 */
2847c478bd9Sstevel@tonic-gate 			switch (callp->sy_flags & SE_RVAL_MASK) {
2857c478bd9Sstevel@tonic-gate 			case SE_32RVAL1:
2867c478bd9Sstevel@tonic-gate 				/* only r_val1 returned */
2877c478bd9Sstevel@tonic-gate 			case SE_32RVAL1 | SE_32RVAL2:
2887c478bd9Sstevel@tonic-gate 				/* r_val1 and r_val2 returned */
2897c478bd9Sstevel@tonic-gate 			case SE_64RVAL:
2907c478bd9Sstevel@tonic-gate 				/* 64-bit rval returned */
2917c478bd9Sstevel@tonic-gate 				break;
2927c478bd9Sstevel@tonic-gate 			default:
2937c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent32[%d]: bad flags %x",
2947c478bd9Sstevel@tonic-gate 				    callnum, callp->sy_flags);
2957c478bd9Sstevel@tonic-gate 				goto skip;
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 			/*
2997c478bd9Sstevel@tonic-gate 			 * Cross-check the native and compatibility tables.
3007c478bd9Sstevel@tonic-gate 			 */
3017c478bd9Sstevel@tonic-gate 			if (callp->sy_callc == nosys ||
3027c478bd9Sstevel@tonic-gate 			    sysent[callnum].sy_callc == nosys)
3037c478bd9Sstevel@tonic-gate 				continue;
3047c478bd9Sstevel@tonic-gate 			/*
3057c478bd9Sstevel@tonic-gate 			 * If only one or the other slot is loadable, then
3067c478bd9Sstevel@tonic-gate 			 * there's an error -- they should match!
3077c478bd9Sstevel@tonic-gate 			 */
3087c478bd9Sstevel@tonic-gate 			if ((callp->sy_callc == loadable_syscall) ^
3097c478bd9Sstevel@tonic-gate 			    (sysent[callnum].sy_callc == loadable_syscall)) {
3107c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent[%d] loadable?",
3117c478bd9Sstevel@tonic-gate 				    callnum);
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 			/*
3147c478bd9Sstevel@tonic-gate 			 * This is more of a heuristic test -- if the
3157c478bd9Sstevel@tonic-gate 			 * system call returns two values in the 32-bit
3167c478bd9Sstevel@tonic-gate 			 * world, it should probably return two 32-bit
3177c478bd9Sstevel@tonic-gate 			 * values in the 64-bit world too.
3187c478bd9Sstevel@tonic-gate 			 */
3197c478bd9Sstevel@tonic-gate 			if (((callp->sy_flags & SE_32RVAL2) == 0) ^
3207c478bd9Sstevel@tonic-gate 			    ((sysent[callnum].sy_flags & SE_32RVAL2) == 0)) {
3217c478bd9Sstevel@tonic-gate 				cmn_err(CE_WARN, "sysent[%d] rval2 mismatch!",
3227c478bd9Sstevel@tonic-gate 				    callnum);
3237c478bd9Sstevel@tonic-gate 			}
3247c478bd9Sstevel@tonic-gate skip:;
3257c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * Allocate loadable exec locks.  (Assumes all execs are loadable)
3327c478bd9Sstevel@tonic-gate 	 */
3337c478bd9Sstevel@tonic-gate 	for (exectype = 0; exectype < nexectype; exectype++) {
3347c478bd9Sstevel@tonic-gate 		execsw[exectype].exec_lock =
3357c478bd9Sstevel@tonic-gate 		    kobj_zalloc(sizeof (krwlock_t), KM_SLEEP);
3367c478bd9Sstevel@tonic-gate 		rw_init(execsw[exectype].exec_lock, NULL, RW_DEFAULT, NULL);
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	read_class_file();
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* init thread specific structure for mod_uninstall_all */
3427c478bd9Sstevel@tonic-gate 	tsd_create(&mod_autounload_key, NULL);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate static int
3467c478bd9Sstevel@tonic-gate modctl_modload(int use_path, char *filename, int *rvp)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	struct modctl *modp;
3497c478bd9Sstevel@tonic-gate 	int retval = 0;
3507c478bd9Sstevel@tonic-gate 	char *filenamep;
3517c478bd9Sstevel@tonic-gate 	int modid;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (copyinstr(filename, filenamep, MOD_MAXPATH, 0)) {
3567c478bd9Sstevel@tonic-gate 		retval = EFAULT;
3577c478bd9Sstevel@tonic-gate 		goto out;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	filenamep[MOD_MAXPATH - 1] = 0;
36120c794b3Sgavinm 	modp = mod_hold_installed_mod(filenamep, use_path, 0, &retval);
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (modp == NULL)
3647c478bd9Sstevel@tonic-gate 		goto out;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	modp->mod_loadflags |= MOD_NOAUTOUNLOAD;
3677c478bd9Sstevel@tonic-gate 	modid = modp->mod_id;
3687c478bd9Sstevel@tonic-gate 	mod_release_mod(modp);
3690b38a8bdSahl 	CPU_STATS_ADDQ(CPU, sys, modload, 1);
3707c478bd9Sstevel@tonic-gate 	if (rvp != NULL && copyout(&modid, rvp, sizeof (modid)) != 0)
3717c478bd9Sstevel@tonic-gate 		retval = EFAULT;
3727c478bd9Sstevel@tonic-gate out:
3737c478bd9Sstevel@tonic-gate 	kmem_free(filenamep, MOD_MAXPATH);
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	return (retval);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate static int
3797c478bd9Sstevel@tonic-gate modctl_modunload(modid_t id)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	int rval = 0;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	if (id == 0) {
3847c478bd9Sstevel@tonic-gate #ifdef DEBUG
3857c478bd9Sstevel@tonic-gate 		/*
3867c478bd9Sstevel@tonic-gate 		 * Turn on mod_uninstall_daemon
3877c478bd9Sstevel@tonic-gate 		 */
3887c478bd9Sstevel@tonic-gate 		if (mod_uninstall_interval == 0) {
3897c478bd9Sstevel@tonic-gate 			mod_uninstall_interval = 60;
3907c478bd9Sstevel@tonic-gate 			modreap();
3917c478bd9Sstevel@tonic-gate 			return (rval);
3927c478bd9Sstevel@tonic-gate 		}
3937c478bd9Sstevel@tonic-gate #endif
3947c478bd9Sstevel@tonic-gate 		mod_uninstall_all();
3957c478bd9Sstevel@tonic-gate 	} else {
3967c478bd9Sstevel@tonic-gate 		rval = modunload(id);
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 	return (rval);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate static int
4027c478bd9Sstevel@tonic-gate modctl_modinfo(modid_t id, struct modinfo *umodi)
4037c478bd9Sstevel@tonic-gate {
4047c478bd9Sstevel@tonic-gate 	int retval;
4057c478bd9Sstevel@tonic-gate 	struct modinfo modi;
4067c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
4077c478bd9Sstevel@tonic-gate 	int nobase;
4087c478bd9Sstevel@tonic-gate 	struct modinfo32 modi32;
4097c478bd9Sstevel@tonic-gate #endif
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
4127c478bd9Sstevel@tonic-gate 		if (copyin(umodi, &modi, sizeof (struct modinfo)) != 0)
4137c478bd9Sstevel@tonic-gate 			return (EFAULT);
4147c478bd9Sstevel@tonic-gate 	}
4157c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4167c478bd9Sstevel@tonic-gate 	else {
4177c478bd9Sstevel@tonic-gate 		bzero(&modi, sizeof (modi));
4187c478bd9Sstevel@tonic-gate 		if (copyin(umodi, &modi32, sizeof (struct modinfo32)) != 0)
4197c478bd9Sstevel@tonic-gate 			return (EFAULT);
4207c478bd9Sstevel@tonic-gate 		modi.mi_info = modi32.mi_info;
4217c478bd9Sstevel@tonic-gate 		modi.mi_id = modi32.mi_id;
4227c478bd9Sstevel@tonic-gate 		modi.mi_nextid = modi32.mi_nextid;
4237c478bd9Sstevel@tonic-gate 		nobase = modi.mi_info & MI_INFO_NOBASE;
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate #endif
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * This flag is -only- for the kernels use.
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	modi.mi_info &= ~MI_INFO_LINKAGE;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	retval = modinfo(id, &modi);
4327c478bd9Sstevel@tonic-gate 	if (retval)
4337c478bd9Sstevel@tonic-gate 		return (retval);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
4367c478bd9Sstevel@tonic-gate 		if (copyout(&modi, umodi, sizeof (struct modinfo)) != 0)
4377c478bd9Sstevel@tonic-gate 			retval = EFAULT;
4387c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
4397c478bd9Sstevel@tonic-gate 	} else {
4407c478bd9Sstevel@tonic-gate 		int i;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		if (!nobase && (uintptr_t)modi.mi_base > UINT32_MAX)
4437c478bd9Sstevel@tonic-gate 			return (EOVERFLOW);
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		modi32.mi_info = modi.mi_info;
4467c478bd9Sstevel@tonic-gate 		modi32.mi_state = modi.mi_state;
4477c478bd9Sstevel@tonic-gate 		modi32.mi_id = modi.mi_id;
4487c478bd9Sstevel@tonic-gate 		modi32.mi_nextid = modi.mi_nextid;
4497c478bd9Sstevel@tonic-gate 		modi32.mi_base = (caddr32_t)(uintptr_t)modi.mi_base;
4507c478bd9Sstevel@tonic-gate 		modi32.mi_size = modi.mi_size;
4517c478bd9Sstevel@tonic-gate 		modi32.mi_rev = modi.mi_rev;
4527c478bd9Sstevel@tonic-gate 		modi32.mi_loadcnt = modi.mi_loadcnt;
4537c478bd9Sstevel@tonic-gate 		bcopy(modi.mi_name, modi32.mi_name, sizeof (modi32.mi_name));
4547c478bd9Sstevel@tonic-gate 		for (i = 0; i < MODMAXLINK32; i++) {
4557c478bd9Sstevel@tonic-gate 			modi32.mi_msinfo[i].msi_p0 = modi.mi_msinfo[i].msi_p0;
4567c478bd9Sstevel@tonic-gate 			bcopy(modi.mi_msinfo[i].msi_linkinfo,
4577c478bd9Sstevel@tonic-gate 			    modi32.mi_msinfo[i].msi_linkinfo,
4587c478bd9Sstevel@tonic-gate 			    sizeof (modi32.mi_msinfo[0].msi_linkinfo));
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate 		if (copyout(&modi32, umodi, sizeof (struct modinfo32)) != 0)
4617c478bd9Sstevel@tonic-gate 			retval = EFAULT;
4627c478bd9Sstevel@tonic-gate #endif
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	return (retval);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  * Return the last major number in the range of permissible major numbers.
4707c478bd9Sstevel@tonic-gate  */
4717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4727c478bd9Sstevel@tonic-gate static int
4737c478bd9Sstevel@tonic-gate modctl_modreserve(modid_t id, int *data)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate 	if (copyout(&devcnt, data, sizeof (devcnt)) != 0)
4767c478bd9Sstevel@tonic-gate 		return (EFAULT);
4777c478bd9Sstevel@tonic-gate 	return (0);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4806532b960SJerry Gilliam /* Add/Remove driver and binding aliases */
4817c478bd9Sstevel@tonic-gate static int
4826532b960SJerry Gilliam modctl_update_driver_aliases(int add, int *data)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	struct modconfig	mc;
4856532b960SJerry Gilliam 	int			i, n, rv = 0;
4867c478bd9Sstevel@tonic-gate 	struct aliases		alias;
4877c478bd9Sstevel@tonic-gate 	struct aliases		*ap;
4887c478bd9Sstevel@tonic-gate 	char			name[MAXMODCONFNAME];
4897c478bd9Sstevel@tonic-gate 	char			cname[MAXMODCONFNAME];
4907c478bd9Sstevel@tonic-gate 	char			*drvname;
4916532b960SJerry Gilliam 	int			resid;
4926532b960SJerry Gilliam 	struct alias_info {
4936532b960SJerry Gilliam 		char	*alias_name;
4946532b960SJerry Gilliam 		int	alias_resid;
4956532b960SJerry Gilliam 	} *aliases, *aip;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	bzero(&mc, sizeof (struct modconfig));
4987c478bd9Sstevel@tonic-gate 	if (get_udatamodel() == DATAMODEL_NATIVE) {
4997c478bd9Sstevel@tonic-gate 		if (copyin(data, &mc, sizeof (struct modconfig)) != 0)
5007c478bd9Sstevel@tonic-gate 			return (EFAULT);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5037c478bd9Sstevel@tonic-gate 	else {
5047c478bd9Sstevel@tonic-gate 		struct modconfig32 modc32;
5057c478bd9Sstevel@tonic-gate 		if (copyin(data, &modc32, sizeof (struct modconfig32)) != 0)
5067c478bd9Sstevel@tonic-gate 			return (EFAULT);
5077c478bd9Sstevel@tonic-gate 		else {
5087c478bd9Sstevel@tonic-gate 			bcopy(modc32.drvname, mc.drvname,
5097c478bd9Sstevel@tonic-gate 			    sizeof (modc32.drvname));
5107c478bd9Sstevel@tonic-gate 			bcopy(modc32.drvclass, mc.drvclass,
5117c478bd9Sstevel@tonic-gate 			    sizeof (modc32.drvclass));
5127c478bd9Sstevel@tonic-gate 			mc.major = modc32.major;
5136532b960SJerry Gilliam 			mc.flags = modc32.flags;
5147c478bd9Sstevel@tonic-gate 			mc.num_aliases = modc32.num_aliases;
5157c478bd9Sstevel@tonic-gate 			mc.ap = (struct aliases *)(uintptr_t)modc32.ap;
5167c478bd9Sstevel@tonic-gate 		}
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate #endif
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * If the driver is already in the mb_hashtab, and the name given
5227c478bd9Sstevel@tonic-gate 	 * doesn't match that driver's name, fail.  Otherwise, pass, since
5237c478bd9Sstevel@tonic-gate 	 * we may be adding aliases.
5247c478bd9Sstevel@tonic-gate 	 */
5256532b960SJerry Gilliam 	drvname = mod_major_to_name(mc.major);
5266532b960SJerry Gilliam 	if ((drvname != NULL) && strcmp(drvname, mc.drvname) != 0)
5277c478bd9Sstevel@tonic-gate 		return (EINVAL);
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	/*
5306532b960SJerry Gilliam 	 * Precede alias removal by unbinding as many devices as possible.
5316532b960SJerry Gilliam 	 */
5326532b960SJerry Gilliam 	if (add == 0) {
5336532b960SJerry Gilliam 		(void) i_ddi_unload_drvconf(mc.major);
5346532b960SJerry Gilliam 		i_ddi_unbind_devs(mc.major);
5356532b960SJerry Gilliam 	}
5366532b960SJerry Gilliam 
5376532b960SJerry Gilliam 	/*
5386532b960SJerry Gilliam 	 * Add/remove each supplied driver alias to/from mb_hashtab
5397c478bd9Sstevel@tonic-gate 	 */
5407c478bd9Sstevel@tonic-gate 	ap = mc.ap;
5416532b960SJerry Gilliam 	if (mc.num_aliases > 0)
5426532b960SJerry Gilliam 		aliases = kmem_zalloc(
5436532b960SJerry Gilliam 		    mc.num_aliases * sizeof (struct alias_info), KM_SLEEP);
5446532b960SJerry Gilliam 	aip = aliases;
5457c478bd9Sstevel@tonic-gate 	for (i = 0; i < mc.num_aliases; i++) {
5467c478bd9Sstevel@tonic-gate 		bzero(&alias, sizeof (struct aliases));
5477c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
5486532b960SJerry Gilliam 			if (copyin(ap, &alias, sizeof (struct aliases)) != 0) {
5496532b960SJerry Gilliam 				rv = EFAULT;
5506532b960SJerry Gilliam 				goto error;
5516532b960SJerry Gilliam 			}
5526532b960SJerry Gilliam 			if (alias.a_len > MAXMODCONFNAME) {
5536532b960SJerry Gilliam 				rv = EINVAL;
5546532b960SJerry Gilliam 				goto error;
5556532b960SJerry Gilliam 			}
5566532b960SJerry Gilliam 			if (copyin(alias.a_name, name, alias.a_len) != 0) {
5576532b960SJerry Gilliam 				rv = EFAULT;
5586532b960SJerry Gilliam 				goto error;
5596532b960SJerry Gilliam 			}
5606532b960SJerry Gilliam 			if (name[alias.a_len - 1] != '\0') {
5616532b960SJerry Gilliam 				rv = EINVAL;
5626532b960SJerry Gilliam 				goto error;
5636532b960SJerry Gilliam 			}
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
5667c478bd9Sstevel@tonic-gate 		else {
5677c478bd9Sstevel@tonic-gate 			struct aliases32 al32;
5687c478bd9Sstevel@tonic-gate 			bzero(&al32, sizeof (struct aliases32));
5696532b960SJerry Gilliam 			if (copyin(ap, &al32, sizeof (struct aliases32)) != 0) {
5706532b960SJerry Gilliam 				rv = EFAULT;
5716532b960SJerry Gilliam 				goto error;
5726532b960SJerry Gilliam 			}
5736532b960SJerry Gilliam 			if (al32.a_len > MAXMODCONFNAME) {
5746532b960SJerry Gilliam 				rv = EINVAL;
5756532b960SJerry Gilliam 				goto error;
5766532b960SJerry Gilliam 			}
5777c478bd9Sstevel@tonic-gate 			if (copyin((void *)(uintptr_t)al32.a_name,
5786532b960SJerry Gilliam 			    name, al32.a_len) != 0) {
5796532b960SJerry Gilliam 				rv = EFAULT;
5806532b960SJerry Gilliam 				goto error;
5816532b960SJerry Gilliam 			}
5826532b960SJerry Gilliam 			if (name[al32.a_len - 1] != '\0') {
5836532b960SJerry Gilliam 				rv = EINVAL;
5846532b960SJerry Gilliam 				goto error;
5856532b960SJerry Gilliam 			}
5867c478bd9Sstevel@tonic-gate 			alias.a_next = (void *)(uintptr_t)al32.a_next;
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate #endif
5897c478bd9Sstevel@tonic-gate 		check_esc_sequences(name, cname);
5907f0b8309SEdward Pilatowicz 		aip->alias_name = strdup(cname);
5917c478bd9Sstevel@tonic-gate 		ap = alias.a_next;
5926532b960SJerry Gilliam 		aip++;
5936532b960SJerry Gilliam 	}
5946532b960SJerry Gilliam 
5956532b960SJerry Gilliam 	if (add == 0) {
5966532b960SJerry Gilliam 		ap = mc.ap;
5976532b960SJerry Gilliam 		resid = 0;
5986532b960SJerry Gilliam 		aip = aliases;
5996532b960SJerry Gilliam 		/* attempt to unbind all devices bound to each alias */
6006532b960SJerry Gilliam 		for (i = 0; i < mc.num_aliases; i++) {
6016532b960SJerry Gilliam 			n = i_ddi_unbind_devs_by_alias(
6026532b960SJerry Gilliam 			    mc.major, aip->alias_name);
6036532b960SJerry Gilliam 			resid += n;
6046532b960SJerry Gilliam 			aip->alias_resid = n;
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		/*
6086532b960SJerry Gilliam 		 * If some device bound to an alias remains in use,
6096532b960SJerry Gilliam 		 * and override wasn't specified, no change is made to
6106532b960SJerry Gilliam 		 * the binding state and we fail the operation.
6116532b960SJerry Gilliam 		 */
6126532b960SJerry Gilliam 		if (resid > 0 && ((mc.flags & MOD_UNBIND_OVERRIDE) == 0)) {
6136532b960SJerry Gilliam 			rv = EBUSY;
6146532b960SJerry Gilliam 			goto error;
6156532b960SJerry Gilliam 		}
6166532b960SJerry Gilliam 
6176532b960SJerry Gilliam 		/*
6186532b960SJerry Gilliam 		 * No device remains bound of any of the aliases,
6196532b960SJerry Gilliam 		 * or force was requested.  Mark each alias as
6206532b960SJerry Gilliam 		 * inactive via delete_mbind so no future binds
6216532b960SJerry Gilliam 		 * to this alias take place and that a new
6226532b960SJerry Gilliam 		 * binding can be established.
6236532b960SJerry Gilliam 		 */
6246532b960SJerry Gilliam 		aip = aliases;
6256532b960SJerry Gilliam 		for (i = 0; i < mc.num_aliases; i++) {
6266532b960SJerry Gilliam 			if (moddebug & MODDEBUG_BINDING)
6276532b960SJerry Gilliam 				cmn_err(CE_CONT, "Removing binding for %s "
6286532b960SJerry Gilliam 				    "(%d active references)\n",
6296532b960SJerry Gilliam 				    aip->alias_name, aip->alias_resid);
6306532b960SJerry Gilliam 			delete_mbind(aip->alias_name, mb_hashtab);
6316532b960SJerry Gilliam 			aip++;
6326532b960SJerry Gilliam 		}
6336532b960SJerry Gilliam 		rv = 0;
6346532b960SJerry Gilliam 	} else {
6356532b960SJerry Gilliam 		aip = aliases;
6366532b960SJerry Gilliam 		for (i = 0; i < mc.num_aliases; i++) {
6376532b960SJerry Gilliam 			if (moddebug & MODDEBUG_BINDING)
6386532b960SJerry Gilliam 				cmn_err(CE_NOTE, "Adding binding for '%s'\n",
6396532b960SJerry Gilliam 				    aip->alias_name);
6406532b960SJerry Gilliam 			(void) make_mbind(aip->alias_name,
6416532b960SJerry Gilliam 			    mc.major, NULL, mb_hashtab);
6426532b960SJerry Gilliam 			aip++;
6436532b960SJerry Gilliam 		}
6446532b960SJerry Gilliam 		/*
6456532b960SJerry Gilliam 		 * Try to establish an mbinding for mc.drvname, and add it to
6466532b960SJerry Gilliam 		 * devnames. Add class if any after establishing the major
6476532b960SJerry Gilliam 		 * number.
6487c478bd9Sstevel@tonic-gate 		 */
6497c478bd9Sstevel@tonic-gate 		(void) make_mbind(mc.drvname, mc.major, NULL, mb_hashtab);
650c9cc1492SJerry Gilliam 		if ((rv = make_devname(mc.drvname, mc.major,
651c9cc1492SJerry Gilliam 		    (mc.flags & MOD_ADDMAJBIND_UPDATE) ?
652c9cc1492SJerry Gilliam 		    DN_DRIVER_INACTIVE : 0)) != 0) {
6536532b960SJerry Gilliam 			goto error;
654c9cc1492SJerry Gilliam 		}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		if (mc.drvclass[0] != '\0')
6577c478bd9Sstevel@tonic-gate 			add_class(mc.drvname, mc.drvclass);
658c9cc1492SJerry Gilliam 		if ((mc.flags & MOD_ADDMAJBIND_UPDATE) == 0) {
6597c478bd9Sstevel@tonic-gate 			(void) i_ddi_load_drvconf(mc.major);
6606532b960SJerry Gilliam 		}
661c9cc1492SJerry Gilliam 	}
6626532b960SJerry Gilliam 
6636532b960SJerry Gilliam 	/*
6646532b960SJerry Gilliam 	 * Ensure that all nodes are bound to the most appropriate driver
6656532b960SJerry Gilliam 	 * possible, attempting demotion and rebind when a more appropriate
666c9cc1492SJerry Gilliam 	 * driver now exists.  But not when adding a driver update-only.
6676532b960SJerry Gilliam 	 */
668c9cc1492SJerry Gilliam 	if ((add == 0) || ((mc.flags & MOD_ADDMAJBIND_UPDATE) == 0)) {
6697c478bd9Sstevel@tonic-gate 		i_ddi_bind_devs();
6704c06356bSdh142964 		i_ddi_di_cache_invalidate();
671c9cc1492SJerry Gilliam 	}
6726532b960SJerry Gilliam 
6736532b960SJerry Gilliam error:
6746532b960SJerry Gilliam 	if (mc.num_aliases > 0) {
6756532b960SJerry Gilliam 		aip = aliases;
6766532b960SJerry Gilliam 		for (i = 0; i < mc.num_aliases; i++) {
6776532b960SJerry Gilliam 			if (aip->alias_name != NULL)
6786532b960SJerry Gilliam 				strfree(aip->alias_name);
6796532b960SJerry Gilliam 			aip++;
6806532b960SJerry Gilliam 		}
6816532b960SJerry Gilliam 		kmem_free(aliases, mc.num_aliases * sizeof (struct alias_info));
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 	return (rv);
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate static int
6876532b960SJerry Gilliam modctl_add_driver_aliases(int *data)
6886532b960SJerry Gilliam {
6896532b960SJerry Gilliam 	return (modctl_update_driver_aliases(1, data));
6906532b960SJerry Gilliam }
6916532b960SJerry Gilliam 
6926532b960SJerry Gilliam static int
6936532b960SJerry Gilliam modctl_remove_driver_aliases(int *data)
6946532b960SJerry Gilliam {
6956532b960SJerry Gilliam 	return (modctl_update_driver_aliases(0, data));
6966532b960SJerry Gilliam }
6976532b960SJerry Gilliam 
6986532b960SJerry Gilliam static int
6997c478bd9Sstevel@tonic-gate modctl_rem_major(major_t major)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	if (major >= devcnt)
7047c478bd9Sstevel@tonic-gate 		return (EINVAL);
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/* mark devnames as removed */
7077c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
7087c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
7097c478bd9Sstevel@tonic-gate 	if (dnp->dn_name == NULL ||
7107c478bd9Sstevel@tonic-gate 	    (dnp->dn_flags & (DN_DRIVER_REMOVED | DN_TAKEN_GETUDEV))) {
7117c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7127c478bd9Sstevel@tonic-gate 		return (EINVAL);
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DN_DRIVER_REMOVED;
7157c478bd9Sstevel@tonic-gate 	pm_driver_removed(major);
7167c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	(void) i_ddi_unload_drvconf(major);
7197c478bd9Sstevel@tonic-gate 	i_ddi_unbind_devs(major);
7206532b960SJerry Gilliam 	i_ddi_bind_devs();
7214c06356bSdh142964 	i_ddi_di_cache_invalidate();
7226532b960SJerry Gilliam 
7236532b960SJerry Gilliam 	/* purge all the bindings to this driver */
7246532b960SJerry Gilliam 	purge_mbind(major, mb_hashtab);
7257c478bd9Sstevel@tonic-gate 	return (0);
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate static struct vfs *
7297c478bd9Sstevel@tonic-gate path_to_vfs(char *name)
7307c478bd9Sstevel@tonic-gate {
7317c478bd9Sstevel@tonic-gate 	vnode_t *vp;
7327c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (lookupname(name, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp))
7357c478bd9Sstevel@tonic-gate 		return (NULL);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	vfsp = vp->v_vfsp;
7387c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
7397c478bd9Sstevel@tonic-gate 	return (vfsp);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate static int
7437c478bd9Sstevel@tonic-gate new_vfs_in_modpath()
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	static int n_modpath = 0;
7467c478bd9Sstevel@tonic-gate 	static char *modpath_copy;
7477c478bd9Sstevel@tonic-gate 	static struct pathvfs {
7487c478bd9Sstevel@tonic-gate 		char *path;
7497c478bd9Sstevel@tonic-gate 		struct vfs *vfsp;
7507c478bd9Sstevel@tonic-gate 	} *pathvfs;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	int i, new_vfs = 0;
7537c478bd9Sstevel@tonic-gate 	char *tmp, *tmp1;
7547c478bd9Sstevel@tonic-gate 	struct vfs *vfsp;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	if (n_modpath != 0) {
7577c478bd9Sstevel@tonic-gate 		for (i = 0; i < n_modpath; i++) {
7587c478bd9Sstevel@tonic-gate 			vfsp = path_to_vfs(pathvfs[i].path);
7597c478bd9Sstevel@tonic-gate 			if (vfsp != pathvfs[i].vfsp) {
7607c478bd9Sstevel@tonic-gate 				pathvfs[i].vfsp = vfsp;
7617c478bd9Sstevel@tonic-gate 				if (vfsp)
7627c478bd9Sstevel@tonic-gate 					new_vfs = 1;
7637c478bd9Sstevel@tonic-gate 			}
7647c478bd9Sstevel@tonic-gate 		}
7657c478bd9Sstevel@tonic-gate 		return (new_vfs);
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	/*
7697c478bd9Sstevel@tonic-gate 	 * First call, initialize the pathvfs structure
7707c478bd9Sstevel@tonic-gate 	 */
7717c478bd9Sstevel@tonic-gate 	modpath_copy = i_ddi_strdup(default_path, KM_SLEEP);
7727c478bd9Sstevel@tonic-gate 	tmp = modpath_copy;
7737c478bd9Sstevel@tonic-gate 	n_modpath = 1;
7747c478bd9Sstevel@tonic-gate 	tmp1 = strchr(tmp, ' ');
7757c478bd9Sstevel@tonic-gate 	while (tmp1) {
7767c478bd9Sstevel@tonic-gate 		*tmp1 = '\0';
7777c478bd9Sstevel@tonic-gate 		n_modpath++;
7787c478bd9Sstevel@tonic-gate 		tmp = tmp1 + 1;
7797c478bd9Sstevel@tonic-gate 		tmp1 = strchr(tmp, ' ');
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	pathvfs = kmem_zalloc(n_modpath * sizeof (struct pathvfs), KM_SLEEP);
7837c478bd9Sstevel@tonic-gate 	tmp = modpath_copy;
7847c478bd9Sstevel@tonic-gate 	for (i = 0; i < n_modpath; i++) {
7857c478bd9Sstevel@tonic-gate 		pathvfs[i].path = tmp;
7867c478bd9Sstevel@tonic-gate 		vfsp = path_to_vfs(tmp);
7877c478bd9Sstevel@tonic-gate 		pathvfs[i].vfsp = vfsp;
7887c478bd9Sstevel@tonic-gate 		tmp += strlen(tmp) + 1;
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 	return (1);	/* always reread driver.conf the first time */
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
793facf4a8dSllai1 static int
794c9cc1492SJerry Gilliam modctl_load_drvconf(major_t major, int flags)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	int ret;
7977c478bd9Sstevel@tonic-gate 
798c9cc1492SJerry Gilliam 	/*
799c9cc1492SJerry Gilliam 	 * devfsadm -u - read all new driver.conf files
800c9cc1492SJerry Gilliam 	 * and bind and configure devices for new drivers.
801c9cc1492SJerry Gilliam 	 */
802c9cc1492SJerry Gilliam 	if (flags & MOD_LOADDRVCONF_RECONF) {
803c9cc1492SJerry Gilliam 		(void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE);
804c9cc1492SJerry Gilliam 		i_ddi_bind_devs();
805c9cc1492SJerry Gilliam 		i_ddi_di_cache_invalidate();
806c9cc1492SJerry Gilliam 		return (0);
807c9cc1492SJerry Gilliam 	}
808c9cc1492SJerry Gilliam 
809c9cc1492SJerry Gilliam 	/*
810c9cc1492SJerry Gilliam 	 * update_drv <drv> - reload driver.conf for the specified driver
811c9cc1492SJerry Gilliam 	 */
812a204de77Scth 	if (major != DDI_MAJOR_T_NONE) {
8137c478bd9Sstevel@tonic-gate 		ret = i_ddi_load_drvconf(major);
8147c478bd9Sstevel@tonic-gate 		if (ret == 0)
8157c478bd9Sstevel@tonic-gate 			i_ddi_bind_devs();
8167c478bd9Sstevel@tonic-gate 		return (ret);
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	/*
8207c478bd9Sstevel@tonic-gate 	 * We are invoked to rescan new driver.conf files. It is
8217c478bd9Sstevel@tonic-gate 	 * only necessary if a new file system was mounted in the
8227c478bd9Sstevel@tonic-gate 	 * module_path. Because rescanning driver.conf files can
8237c478bd9Sstevel@tonic-gate 	 * take some time on older platforms (sun4m), the following
8247c478bd9Sstevel@tonic-gate 	 * code skips unnecessary driver.conf rescans to optimize
8257c478bd9Sstevel@tonic-gate 	 * boot performance.
8267c478bd9Sstevel@tonic-gate 	 */
8277c478bd9Sstevel@tonic-gate 	if (new_vfs_in_modpath()) {
828a204de77Scth 		(void) i_ddi_load_drvconf(DDI_MAJOR_T_NONE);
8297c478bd9Sstevel@tonic-gate 		/*
8307c478bd9Sstevel@tonic-gate 		 * If we are still initializing io subsystem,
8317c478bd9Sstevel@tonic-gate 		 * load drivers with ddi-forceattach property
8327c478bd9Sstevel@tonic-gate 		 */
8337c478bd9Sstevel@tonic-gate 		if (!i_ddi_io_initialized())
8347c478bd9Sstevel@tonic-gate 			i_ddi_forceattach_drivers();
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 	return (0);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8396532b960SJerry Gilliam /*
8406532b960SJerry Gilliam  * Unload driver.conf file and follow up by attempting
8416532b960SJerry Gilliam  * to rebind devices to more appropriate driver.
8426532b960SJerry Gilliam  */
8437c478bd9Sstevel@tonic-gate static int
8447c478bd9Sstevel@tonic-gate modctl_unload_drvconf(major_t major)
8457c478bd9Sstevel@tonic-gate {
8467c478bd9Sstevel@tonic-gate 	int ret;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	if (major >= devcnt)
8497c478bd9Sstevel@tonic-gate 		return (EINVAL);
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	ret = i_ddi_unload_drvconf(major);
8527c478bd9Sstevel@tonic-gate 	if (ret != 0)
8537c478bd9Sstevel@tonic-gate 		return (ret);
8547c478bd9Sstevel@tonic-gate 	(void) i_ddi_unbind_devs(major);
8556532b960SJerry Gilliam 	i_ddi_bind_devs();
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	return (0);
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate static void
8617c478bd9Sstevel@tonic-gate check_esc_sequences(char *str, char *cstr)
8627c478bd9Sstevel@tonic-gate {
8637c478bd9Sstevel@tonic-gate 	int i;
8647c478bd9Sstevel@tonic-gate 	size_t len;
8657c478bd9Sstevel@tonic-gate 	char *p;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	len = strlen(str);
8687c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++, str++, cstr++) {
8697c478bd9Sstevel@tonic-gate 		if (*str != '\\') {
8707c478bd9Sstevel@tonic-gate 			*cstr = *str;
8717c478bd9Sstevel@tonic-gate 		} else {
8727c478bd9Sstevel@tonic-gate 			p = str + 1;
8737c478bd9Sstevel@tonic-gate 			/*
8747c478bd9Sstevel@tonic-gate 			 * we only handle octal escape sequences for SPACE
8757c478bd9Sstevel@tonic-gate 			 */
8767c478bd9Sstevel@tonic-gate 			if (*p++ == '0' && *p++ == '4' && *p == '0') {
8777c478bd9Sstevel@tonic-gate 				*cstr = ' ';
8787c478bd9Sstevel@tonic-gate 				str += 3;
8797c478bd9Sstevel@tonic-gate 			} else {
8807c478bd9Sstevel@tonic-gate 				*cstr = *str;
8817c478bd9Sstevel@tonic-gate 			}
8827c478bd9Sstevel@tonic-gate 		}
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 	*cstr = 0;
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate static int
8887c478bd9Sstevel@tonic-gate modctl_getmodpathlen(int *data)
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	int len;
8917c478bd9Sstevel@tonic-gate 	len = strlen(default_path);
8927c478bd9Sstevel@tonic-gate 	if (copyout(&len, data, sizeof (len)) != 0)
8937c478bd9Sstevel@tonic-gate 		return (EFAULT);
8947c478bd9Sstevel@tonic-gate 	return (0);
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate static int
8987c478bd9Sstevel@tonic-gate modctl_getmodpath(char *data)
8997c478bd9Sstevel@tonic-gate {
9007c478bd9Sstevel@tonic-gate 	if (copyout(default_path, data, strlen(default_path) + 1) != 0)
9017c478bd9Sstevel@tonic-gate 		return (EFAULT);
9027c478bd9Sstevel@tonic-gate 	return (0);
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate static int
9067c478bd9Sstevel@tonic-gate modctl_read_sysbinding_file(void)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	(void) read_binding_file(sysbind, sb_hashtab, make_mbind);
9097c478bd9Sstevel@tonic-gate 	return (0);
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate static int
9137c478bd9Sstevel@tonic-gate modctl_getmaj(char *uname, uint_t ulen, int *umajorp)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 	char name[256];
9167c478bd9Sstevel@tonic-gate 	int retval;
9177c478bd9Sstevel@tonic-gate 	major_t major;
9187c478bd9Sstevel@tonic-gate 
919facf4a8dSllai1 	if (ulen == 0)
920facf4a8dSllai1 		return (EINVAL);
9217c478bd9Sstevel@tonic-gate 	if ((retval = copyinstr(uname, name,
9227c478bd9Sstevel@tonic-gate 	    (ulen < 256) ? ulen : 256, 0)) != 0)
9237c478bd9Sstevel@tonic-gate 		return (retval);
924a204de77Scth 	if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
9257c478bd9Sstevel@tonic-gate 		return (ENODEV);
9267c478bd9Sstevel@tonic-gate 	if (copyout(&major, umajorp, sizeof (major_t)) != 0)
9277c478bd9Sstevel@tonic-gate 		return (EFAULT);
9287c478bd9Sstevel@tonic-gate 	return (0);
9297c478bd9Sstevel@tonic-gate }
9307c478bd9Sstevel@tonic-gate 
93125e8c5aaSvikram static char **
93225e8c5aaSvikram convert_constraint_string(char *constraints, size_t len)
93325e8c5aaSvikram {
93425e8c5aaSvikram 	int	i;
93525e8c5aaSvikram 	int	n;
93625e8c5aaSvikram 	char	*p;
93725e8c5aaSvikram 	char	**array;
93825e8c5aaSvikram 
93925e8c5aaSvikram 	ASSERT(constraints != NULL);
94025e8c5aaSvikram 	ASSERT(len > 0);
94125e8c5aaSvikram 
9421e1ddd6cScth 	for (i = 0, p = constraints; strlen(p) > 0; i++, p += strlen(p) + 1)
9431e1ddd6cScth 		;
94425e8c5aaSvikram 
94525e8c5aaSvikram 	n = i;
94625e8c5aaSvikram 
94725e8c5aaSvikram 	if (n == 0) {
94825e8c5aaSvikram 		kmem_free(constraints, len);
94925e8c5aaSvikram 		return (NULL);
95025e8c5aaSvikram 	}
95125e8c5aaSvikram 
95225e8c5aaSvikram 	array = kmem_alloc((n + 1) * sizeof (char *), KM_SLEEP);
95325e8c5aaSvikram 
95425e8c5aaSvikram 	for (i = 0, p = constraints; i < n; i++, p += strlen(p) + 1) {
95525e8c5aaSvikram 		array[i] = i_ddi_strdup(p, KM_SLEEP);
95625e8c5aaSvikram 	}
95725e8c5aaSvikram 	array[n] = NULL;
95825e8c5aaSvikram 
95925e8c5aaSvikram 	kmem_free(constraints, len);
96025e8c5aaSvikram 
96125e8c5aaSvikram 	return (array);
96225e8c5aaSvikram }
96325e8c5aaSvikram /*ARGSUSED*/
96425e8c5aaSvikram static int
96525e8c5aaSvikram modctl_retire(char *path, char *uconstraints, size_t ulen)
96625e8c5aaSvikram {
96725e8c5aaSvikram 	char	*pathbuf;
96825e8c5aaSvikram 	char	*devpath;
96925e8c5aaSvikram 	size_t	pathsz;
97025e8c5aaSvikram 	int	retval;
97125e8c5aaSvikram 	char	*constraints;
97225e8c5aaSvikram 	char	**cons_array;
97325e8c5aaSvikram 
97425e8c5aaSvikram 	if (path == NULL)
97525e8c5aaSvikram 		return (EINVAL);
97625e8c5aaSvikram 
97725e8c5aaSvikram 	if ((uconstraints == NULL) ^ (ulen == 0))
97825e8c5aaSvikram 		return (EINVAL);
97925e8c5aaSvikram 
98025e8c5aaSvikram 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
98125e8c5aaSvikram 	retval = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
98225e8c5aaSvikram 	if (retval != 0) {
98325e8c5aaSvikram 		kmem_free(pathbuf, MAXPATHLEN);
98425e8c5aaSvikram 		return (retval);
98525e8c5aaSvikram 	}
98625e8c5aaSvikram 	devpath = i_ddi_strdup(pathbuf, KM_SLEEP);
98725e8c5aaSvikram 	kmem_free(pathbuf, MAXPATHLEN);
98825e8c5aaSvikram 
98925e8c5aaSvikram 	/*
99025e8c5aaSvikram 	 * First check if the device is already retired.
99125e8c5aaSvikram 	 * If it is, this becomes a NOP
99225e8c5aaSvikram 	 */
99325e8c5aaSvikram 	if (e_ddi_device_retired(devpath)) {
99425e8c5aaSvikram 		cmn_err(CE_NOTE, "Device: already retired: %s", devpath);
99525e8c5aaSvikram 		kmem_free(devpath, strlen(devpath) + 1);
99625e8c5aaSvikram 		return (0);
99725e8c5aaSvikram 	}
99825e8c5aaSvikram 
99925e8c5aaSvikram 	cons_array = NULL;
100025e8c5aaSvikram 	if (uconstraints) {
100125e8c5aaSvikram 		constraints = kmem_alloc(ulen, KM_SLEEP);
100225e8c5aaSvikram 		if (copyin(uconstraints, constraints, ulen)) {
100325e8c5aaSvikram 			kmem_free(constraints, ulen);
100425e8c5aaSvikram 			kmem_free(devpath, strlen(devpath) + 1);
100525e8c5aaSvikram 			return (EFAULT);
100625e8c5aaSvikram 		}
100725e8c5aaSvikram 		cons_array = convert_constraint_string(constraints, ulen);
100825e8c5aaSvikram 	}
100925e8c5aaSvikram 
101025e8c5aaSvikram 	/*
101125e8c5aaSvikram 	 * Try to retire the device first. The following
101225e8c5aaSvikram 	 * routine will return an error only if the device
101325e8c5aaSvikram 	 * is not retireable i.e. retire constraints forbid
101425e8c5aaSvikram 	 * a retire. A return of success from this routine
101525e8c5aaSvikram 	 * indicates that device is retireable.
101625e8c5aaSvikram 	 */
101725e8c5aaSvikram 	retval = e_ddi_retire_device(devpath, cons_array);
101825e8c5aaSvikram 	if (retval != DDI_SUCCESS) {
101925e8c5aaSvikram 		cmn_err(CE_WARN, "constraints forbid retire: %s", devpath);
102025e8c5aaSvikram 		kmem_free(devpath, strlen(devpath) + 1);
102125e8c5aaSvikram 		return (ENOTSUP);
102225e8c5aaSvikram 	}
102325e8c5aaSvikram 
102425e8c5aaSvikram 	/*
102525e8c5aaSvikram 	 * Ok, the retire succeeded. Persist the retire.
102625e8c5aaSvikram 	 * If retiring a nexus, we need to only persist the
102725e8c5aaSvikram 	 * nexus retire. Any children of a retired nexus
102825e8c5aaSvikram 	 * are automatically covered by the retire store
102925e8c5aaSvikram 	 * code.
103025e8c5aaSvikram 	 */
103125e8c5aaSvikram 	retval = e_ddi_retire_persist(devpath);
103225e8c5aaSvikram 	if (retval != 0) {
103325e8c5aaSvikram 		cmn_err(CE_WARN, "Failed to persist device retire: error %d: "
103425e8c5aaSvikram 		    "%s", retval, devpath);
103525e8c5aaSvikram 		kmem_free(devpath, strlen(devpath) + 1);
103625e8c5aaSvikram 		return (retval);
103725e8c5aaSvikram 	}
103825e8c5aaSvikram 	if (moddebug & MODDEBUG_RETIRE)
103925e8c5aaSvikram 		cmn_err(CE_NOTE, "Persisted retire of device: %s", devpath);
104025e8c5aaSvikram 
104125e8c5aaSvikram 	kmem_free(devpath, strlen(devpath) + 1);
104225e8c5aaSvikram 	return (0);
104325e8c5aaSvikram }
104425e8c5aaSvikram 
104525e8c5aaSvikram static int
104625e8c5aaSvikram modctl_is_retired(char *path, int *statep)
104725e8c5aaSvikram {
104825e8c5aaSvikram 	char	*pathbuf;
104925e8c5aaSvikram 	char	*devpath;
105025e8c5aaSvikram 	size_t	pathsz;
105125e8c5aaSvikram 	int	error;
105225e8c5aaSvikram 	int	status;
105325e8c5aaSvikram 
105425e8c5aaSvikram 	if (path == NULL || statep == NULL)
105525e8c5aaSvikram 		return (EINVAL);
105625e8c5aaSvikram 
105725e8c5aaSvikram 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
105825e8c5aaSvikram 	error = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
105925e8c5aaSvikram 	if (error != 0) {
106025e8c5aaSvikram 		kmem_free(pathbuf, MAXPATHLEN);
106125e8c5aaSvikram 		return (error);
106225e8c5aaSvikram 	}
106325e8c5aaSvikram 	devpath = i_ddi_strdup(pathbuf, KM_SLEEP);
106425e8c5aaSvikram 	kmem_free(pathbuf, MAXPATHLEN);
106525e8c5aaSvikram 
106625e8c5aaSvikram 	if (e_ddi_device_retired(devpath))
106725e8c5aaSvikram 		status = 1;
106825e8c5aaSvikram 	else
106925e8c5aaSvikram 		status = 0;
107025e8c5aaSvikram 	kmem_free(devpath, strlen(devpath) + 1);
107125e8c5aaSvikram 
107225e8c5aaSvikram 	return (copyout(&status, statep, sizeof (status)) ? EFAULT : 0);
107325e8c5aaSvikram }
107425e8c5aaSvikram 
107525e8c5aaSvikram static int
107625e8c5aaSvikram modctl_unretire(char *path)
107725e8c5aaSvikram {
107825e8c5aaSvikram 	char	*pathbuf;
107925e8c5aaSvikram 	char	*devpath;
108025e8c5aaSvikram 	size_t	pathsz;
108125e8c5aaSvikram 	int	retired;
108225e8c5aaSvikram 	int	retval;
108325e8c5aaSvikram 
108425e8c5aaSvikram 	if (path == NULL)
108525e8c5aaSvikram 		return (EINVAL);
108625e8c5aaSvikram 
108725e8c5aaSvikram 	pathbuf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
108825e8c5aaSvikram 	retval = copyinstr(path, pathbuf, MAXPATHLEN, &pathsz);
108925e8c5aaSvikram 	if (retval != 0) {
109025e8c5aaSvikram 		kmem_free(pathbuf, MAXPATHLEN);
109125e8c5aaSvikram 		return (retval);
109225e8c5aaSvikram 	}
109325e8c5aaSvikram 	devpath = i_ddi_strdup(pathbuf, KM_SLEEP);
109425e8c5aaSvikram 	kmem_free(pathbuf, MAXPATHLEN);
109525e8c5aaSvikram 
109625e8c5aaSvikram 	/*
109725e8c5aaSvikram 	 * We check if a device is retired (first) before
109825e8c5aaSvikram 	 * unpersisting the retire, because we use the
109925e8c5aaSvikram 	 * retire store to determine if a device is retired.
110025e8c5aaSvikram 	 * If we unpersist first, the device will always appear
110125e8c5aaSvikram 	 * to be unretired. For the rationale behind unpersisting
110225e8c5aaSvikram 	 * a device that is not retired, see the next comment.
110325e8c5aaSvikram 	 */
110425e8c5aaSvikram 	retired = e_ddi_device_retired(devpath);
110525e8c5aaSvikram 
110625e8c5aaSvikram 	/*
110725e8c5aaSvikram 	 * We call unpersist unconditionally because the lookup
110825e8c5aaSvikram 	 * for retired devices (e_ddi_device_retired()), skips "bypassed"
110925e8c5aaSvikram 	 * devices. We still want to be able remove "bypassed" entries
111025e8c5aaSvikram 	 * from the persistent store, so we unpersist unconditionally
111125e8c5aaSvikram 	 * i.e. whether or not the entry is found on a lookup.
111225e8c5aaSvikram 	 *
111325e8c5aaSvikram 	 * e_ddi_retire_unpersist() returns 1 if it found and cleared
111425e8c5aaSvikram 	 * an entry from the retire store or 0 otherwise.
111525e8c5aaSvikram 	 */
111625e8c5aaSvikram 	if (e_ddi_retire_unpersist(devpath))
111725e8c5aaSvikram 		if (moddebug & MODDEBUG_RETIRE) {
111825e8c5aaSvikram 			cmn_err(CE_NOTE, "Unpersisted retire of device: %s",
111925e8c5aaSvikram 			    devpath);
112025e8c5aaSvikram 		}
112125e8c5aaSvikram 
112225e8c5aaSvikram 	/*
112325e8c5aaSvikram 	 * Check if the device is already unretired. If so,
112425e8c5aaSvikram 	 * the unretire becomes a NOP
112525e8c5aaSvikram 	 */
112625e8c5aaSvikram 	if (!retired) {
112725e8c5aaSvikram 		cmn_err(CE_NOTE, "Not retired: %s", devpath);
112825e8c5aaSvikram 		kmem_free(devpath, strlen(devpath) + 1);
112925e8c5aaSvikram 		return (0);
113025e8c5aaSvikram 	}
113125e8c5aaSvikram 
113225e8c5aaSvikram 	retval = e_ddi_unretire_device(devpath);
113325e8c5aaSvikram 	if (retval != 0) {
113425e8c5aaSvikram 		cmn_err(CE_WARN, "cannot unretire device: error %d, path %s\n",
113525e8c5aaSvikram 		    retval, devpath);
113625e8c5aaSvikram 	}
113725e8c5aaSvikram 
113825e8c5aaSvikram 	kmem_free(devpath, strlen(devpath) + 1);
113925e8c5aaSvikram 
114025e8c5aaSvikram 	return (retval);
114125e8c5aaSvikram }
114225e8c5aaSvikram 
11437c478bd9Sstevel@tonic-gate static int
11447c478bd9Sstevel@tonic-gate modctl_getname(char *uname, uint_t ulen, int *umajorp)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	char *name;
11477c478bd9Sstevel@tonic-gate 	major_t major;
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	if (copyin(umajorp, &major, sizeof (major)) != 0)
11507c478bd9Sstevel@tonic-gate 		return (EFAULT);
11517c478bd9Sstevel@tonic-gate 	if ((name = mod_major_to_name(major)) == NULL)
11527c478bd9Sstevel@tonic-gate 		return (ENODEV);
11537c478bd9Sstevel@tonic-gate 	if ((strlen(name) + 1) > ulen)
11547c478bd9Sstevel@tonic-gate 		return (ENOSPC);
11557c478bd9Sstevel@tonic-gate 	return (copyoutstr(name, uname, ulen, NULL));
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate static int
11597c478bd9Sstevel@tonic-gate modctl_devt2instance(dev_t dev, int *uinstancep)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate 	int	instance;
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	if ((instance = dev_to_instance(dev)) == -1)
11647c478bd9Sstevel@tonic-gate 		return (EINVAL);
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	return (copyout(&instance, uinstancep, sizeof (int)));
11677c478bd9Sstevel@tonic-gate }
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate /*
11707c478bd9Sstevel@tonic-gate  * Return the sizeof of the device id.
11717c478bd9Sstevel@tonic-gate  */
11727c478bd9Sstevel@tonic-gate static int
11737c478bd9Sstevel@tonic-gate modctl_sizeof_devid(dev_t dev, uint_t *len)
11747c478bd9Sstevel@tonic-gate {
11757c478bd9Sstevel@tonic-gate 	uint_t		sz;
11767c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	/* get device id */
11797c478bd9Sstevel@tonic-gate 	if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
11807c478bd9Sstevel@tonic-gate 		return (EINVAL);
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	sz = ddi_devid_sizeof(devid);
11837c478bd9Sstevel@tonic-gate 	ddi_devid_free(devid);
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	/* copyout device id size */
11867c478bd9Sstevel@tonic-gate 	if (copyout(&sz, len, sizeof (sz)) != 0)
11877c478bd9Sstevel@tonic-gate 		return (EFAULT);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	return (0);
11907c478bd9Sstevel@tonic-gate }
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate /*
11937c478bd9Sstevel@tonic-gate  * Return a copy of the device id.
11947c478bd9Sstevel@tonic-gate  */
11957c478bd9Sstevel@tonic-gate static int
11967c478bd9Sstevel@tonic-gate modctl_get_devid(dev_t dev, uint_t len, ddi_devid_t udevid)
11977c478bd9Sstevel@tonic-gate {
11987c478bd9Sstevel@tonic-gate 	uint_t		sz;
11997c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid;
12007c478bd9Sstevel@tonic-gate 	int		err = 0;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	/* get device id */
12037c478bd9Sstevel@tonic-gate 	if (ddi_lyr_get_devid(dev, &devid) == DDI_FAILURE)
12047c478bd9Sstevel@tonic-gate 		return (EINVAL);
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	sz = ddi_devid_sizeof(devid);
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	/* Error if device id is larger than space allocated */
12097c478bd9Sstevel@tonic-gate 	if (sz > len) {
12107c478bd9Sstevel@tonic-gate 		ddi_devid_free(devid);
12117c478bd9Sstevel@tonic-gate 		return (ENOSPC);
12127c478bd9Sstevel@tonic-gate 	}
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	/* copy out device id */
12157c478bd9Sstevel@tonic-gate 	if (copyout(devid, udevid, sz) != 0)
12167c478bd9Sstevel@tonic-gate 		err = EFAULT;
12177c478bd9Sstevel@tonic-gate 	ddi_devid_free(devid);
12187c478bd9Sstevel@tonic-gate 	return (err);
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate /*
12227c478bd9Sstevel@tonic-gate  * return the /devices paths associated with the specified devid and
12237c478bd9Sstevel@tonic-gate  * minor name.
12247c478bd9Sstevel@tonic-gate  */
12257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12267c478bd9Sstevel@tonic-gate static int
12277c478bd9Sstevel@tonic-gate modctl_devid2paths(ddi_devid_t udevid, char *uminor_name, uint_t flag,
12287c478bd9Sstevel@tonic-gate 	size_t *ulensp, char *upaths)
12297c478bd9Sstevel@tonic-gate {
12307c478bd9Sstevel@tonic-gate 	ddi_devid_t	devid = NULL;
12317c478bd9Sstevel@tonic-gate 	int		devid_len;
12327c478bd9Sstevel@tonic-gate 	char		*minor_name = NULL;
12337c478bd9Sstevel@tonic-gate 	dev_info_t	*dip = NULL;
1234b9ccdc5aScth 	int		circ;
12357c478bd9Sstevel@tonic-gate 	struct ddi_minor_data	*dmdp;
12367c478bd9Sstevel@tonic-gate 	char		*path = NULL;
12377c478bd9Sstevel@tonic-gate 	int		ulens;
12387c478bd9Sstevel@tonic-gate 	int		lens;
12397c478bd9Sstevel@tonic-gate 	int		len;
12407c478bd9Sstevel@tonic-gate 	dev_t		*devlist = NULL;
12417c478bd9Sstevel@tonic-gate 	int		ndevs;
12427c478bd9Sstevel@tonic-gate 	int		i;
12437c478bd9Sstevel@tonic-gate 	int		ret = 0;
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	/*
12467c478bd9Sstevel@tonic-gate 	 * If upaths is NULL then we are only computing the amount of space
12477c478bd9Sstevel@tonic-gate 	 * needed to hold the paths and returning the value in *ulensp. If we
12487c478bd9Sstevel@tonic-gate 	 * are copying out paths then we get the amount of space allocated by
12497c478bd9Sstevel@tonic-gate 	 * the caller. If the actual space needed for paths is larger, or
12507c478bd9Sstevel@tonic-gate 	 * things are changing out from under us, then we return EAGAIN.
12517c478bd9Sstevel@tonic-gate 	 */
12527c478bd9Sstevel@tonic-gate 	if (upaths) {
12537c478bd9Sstevel@tonic-gate 		if (ulensp == NULL)
12547c478bd9Sstevel@tonic-gate 			return (EINVAL);
12557c478bd9Sstevel@tonic-gate 		if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
12567c478bd9Sstevel@tonic-gate 			return (EFAULT);
12577c478bd9Sstevel@tonic-gate 	}
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	/*
12607c478bd9Sstevel@tonic-gate 	 * copyin enough of the devid to determine the length then
12617c478bd9Sstevel@tonic-gate 	 * reallocate and copy in the entire devid.
12627c478bd9Sstevel@tonic-gate 	 */
12637c478bd9Sstevel@tonic-gate 	devid_len = ddi_devid_sizeof(NULL);
12647c478bd9Sstevel@tonic-gate 	devid = kmem_alloc(devid_len, KM_SLEEP);
12657c478bd9Sstevel@tonic-gate 	if (copyin(udevid, devid, devid_len)) {
12667c478bd9Sstevel@tonic-gate 		ret = EFAULT;
12677c478bd9Sstevel@tonic-gate 		goto out;
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate 	len = devid_len;
12707c478bd9Sstevel@tonic-gate 	devid_len = ddi_devid_sizeof(devid);
12717c478bd9Sstevel@tonic-gate 	kmem_free(devid, len);
12727c478bd9Sstevel@tonic-gate 	devid = kmem_alloc(devid_len, KM_SLEEP);
12737c478bd9Sstevel@tonic-gate 	if (copyin(udevid, devid, devid_len)) {
12747c478bd9Sstevel@tonic-gate 		ret = EFAULT;
12757c478bd9Sstevel@tonic-gate 		goto out;
12767c478bd9Sstevel@tonic-gate 	}
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	/* copyin the minor name if specified. */
12797c478bd9Sstevel@tonic-gate 	minor_name = uminor_name;
12807c478bd9Sstevel@tonic-gate 	if ((minor_name != DEVID_MINOR_NAME_ALL) &&
12817c478bd9Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
12827c478bd9Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_BLK)) {
12837c478bd9Sstevel@tonic-gate 		minor_name = kmem_alloc(MAXPATHLEN, KM_SLEEP);
12847c478bd9Sstevel@tonic-gate 		if (copyinstr(uminor_name, minor_name, MAXPATHLEN, 0)) {
12857c478bd9Sstevel@tonic-gate 			ret = EFAULT;
12867c478bd9Sstevel@tonic-gate 			goto out;
12877c478bd9Sstevel@tonic-gate 		}
12887c478bd9Sstevel@tonic-gate 	}
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	/*
12917c478bd9Sstevel@tonic-gate 	 * Use existing function to resolve the devid into a devlist.
12927c478bd9Sstevel@tonic-gate 	 *
12937c478bd9Sstevel@tonic-gate 	 * NOTE: there is a loss of spectype information in the current
12947c478bd9Sstevel@tonic-gate 	 * ddi_lyr_devid_to_devlist implementation. We work around this by not
12957c478bd9Sstevel@tonic-gate 	 * passing down DEVID_MINOR_NAME_ALL here, but reproducing all minor
12967c478bd9Sstevel@tonic-gate 	 * node forms in the loop processing the devlist below. It would be
12977c478bd9Sstevel@tonic-gate 	 * best if at some point the use of this interface here was replaced
12987c478bd9Sstevel@tonic-gate 	 * with a path oriented call.
12997c478bd9Sstevel@tonic-gate 	 */
13007c478bd9Sstevel@tonic-gate 	if (ddi_lyr_devid_to_devlist(devid,
13017c478bd9Sstevel@tonic-gate 	    (minor_name == DEVID_MINOR_NAME_ALL) ?
13027c478bd9Sstevel@tonic-gate 	    DEVID_MINOR_NAME_ALL_CHR : minor_name,
13037c478bd9Sstevel@tonic-gate 	    &ndevs, &devlist) != DDI_SUCCESS) {
13047c478bd9Sstevel@tonic-gate 		ret = EINVAL;
13057c478bd9Sstevel@tonic-gate 		goto out;
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	/*
13097c478bd9Sstevel@tonic-gate 	 * loop over the devlist, converting each devt to a path and doing
13107c478bd9Sstevel@tonic-gate 	 * a copyout of the path and computation of the amount of space
13117c478bd9Sstevel@tonic-gate 	 * needed to hold all the paths
13127c478bd9Sstevel@tonic-gate 	 */
13137c478bd9Sstevel@tonic-gate 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
13147c478bd9Sstevel@tonic-gate 	for (i = 0, lens = 0; i < ndevs; i++) {
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 		/* find the dip associated with the dev_t */
13177c478bd9Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_dev(devlist[i], 0)) == NULL)
13187c478bd9Sstevel@tonic-gate 			continue;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 		/* loop over all the minor nodes, skipping ones we don't want */
1321b9ccdc5aScth 		ndi_devi_enter(dip, &circ);
13227c478bd9Sstevel@tonic-gate 		for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
13237c478bd9Sstevel@tonic-gate 			if ((dmdp->ddm_dev != devlist[i]) ||
13247c478bd9Sstevel@tonic-gate 			    (dmdp->type != DDM_MINOR))
13257c478bd9Sstevel@tonic-gate 				continue;
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 			if ((minor_name != DEVID_MINOR_NAME_ALL) &&
13287c478bd9Sstevel@tonic-gate 			    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
13297c478bd9Sstevel@tonic-gate 			    (minor_name != DEVID_MINOR_NAME_ALL_BLK) &&
13307c478bd9Sstevel@tonic-gate 			    strcmp(minor_name, dmdp->ddm_name))
13317c478bd9Sstevel@tonic-gate 				continue;
13327c478bd9Sstevel@tonic-gate 			else {
13337c478bd9Sstevel@tonic-gate 				if ((minor_name == DEVID_MINOR_NAME_ALL_CHR) &&
13347c478bd9Sstevel@tonic-gate 				    (dmdp->ddm_spec_type != S_IFCHR))
13357c478bd9Sstevel@tonic-gate 					continue;
13367c478bd9Sstevel@tonic-gate 				if ((minor_name == DEVID_MINOR_NAME_ALL_BLK) &&
13377c478bd9Sstevel@tonic-gate 				    (dmdp->ddm_spec_type != S_IFBLK))
13387c478bd9Sstevel@tonic-gate 					continue;
13397c478bd9Sstevel@tonic-gate 			}
13407c478bd9Sstevel@tonic-gate 
1341f9722deaSChris Horne 			(void) ddi_pathname_minor(dmdp, path);
13427c478bd9Sstevel@tonic-gate 			len = strlen(path) + 1;
13437c478bd9Sstevel@tonic-gate 			*(path + len) = '\0';	/* set double termination */
13447c478bd9Sstevel@tonic-gate 			lens += len;
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 			/* copyout the path with double terminations */
13477c478bd9Sstevel@tonic-gate 			if (upaths) {
13487c478bd9Sstevel@tonic-gate 				if (lens > ulens) {
13497c478bd9Sstevel@tonic-gate 					ret = EAGAIN;
13507c478bd9Sstevel@tonic-gate 					goto out;
13517c478bd9Sstevel@tonic-gate 				}
13527c478bd9Sstevel@tonic-gate 				if (copyout(path, upaths, len + 1)) {
13537c478bd9Sstevel@tonic-gate 					ret = EFAULT;
13547c478bd9Sstevel@tonic-gate 					goto out;
13557c478bd9Sstevel@tonic-gate 				}
13567c478bd9Sstevel@tonic-gate 				upaths += len;
13577c478bd9Sstevel@tonic-gate 			}
13587c478bd9Sstevel@tonic-gate 		}
1359b9ccdc5aScth 		ndi_devi_exit(dip, circ);
13607c478bd9Sstevel@tonic-gate 		ddi_release_devi(dip);
13617c478bd9Sstevel@tonic-gate 		dip = NULL;
13627c478bd9Sstevel@tonic-gate 	}
13637c478bd9Sstevel@tonic-gate 	lens++;		/* add one for double termination */
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	/* copy out the amount of space needed to hold the paths */
13667c478bd9Sstevel@tonic-gate 	if (ulensp && copyout(&lens, ulensp, sizeof (lens))) {
13677c478bd9Sstevel@tonic-gate 		ret = EFAULT;
13687c478bd9Sstevel@tonic-gate 		goto out;
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 	ret = 0;
13717c478bd9Sstevel@tonic-gate 
1372b9ccdc5aScth out:	if (dip) {
1373b9ccdc5aScth 		ndi_devi_exit(dip, circ);
13747c478bd9Sstevel@tonic-gate 		ddi_release_devi(dip);
1375b9ccdc5aScth 	}
13767c478bd9Sstevel@tonic-gate 	if (path)
13777c478bd9Sstevel@tonic-gate 		kmem_free(path, MAXPATHLEN);
13787c478bd9Sstevel@tonic-gate 	if (devlist)
13797c478bd9Sstevel@tonic-gate 		ddi_lyr_free_devlist(devlist, ndevs);
13807c478bd9Sstevel@tonic-gate 	if (minor_name &&
13817c478bd9Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL) &&
13827c478bd9Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_CHR) &&
13837c478bd9Sstevel@tonic-gate 	    (minor_name != DEVID_MINOR_NAME_ALL_BLK))
13847c478bd9Sstevel@tonic-gate 		kmem_free(minor_name, MAXPATHLEN);
13857c478bd9Sstevel@tonic-gate 	if (devid)
13867c478bd9Sstevel@tonic-gate 		kmem_free(devid, devid_len);
13877c478bd9Sstevel@tonic-gate 	return (ret);
13887c478bd9Sstevel@tonic-gate }
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate /*
13917c478bd9Sstevel@tonic-gate  * Return the size of the minor name.
13927c478bd9Sstevel@tonic-gate  */
13937c478bd9Sstevel@tonic-gate static int
13947c478bd9Sstevel@tonic-gate modctl_sizeof_minorname(dev_t dev, int spectype, uint_t *len)
13957c478bd9Sstevel@tonic-gate {
13967c478bd9Sstevel@tonic-gate 	uint_t	sz;
13977c478bd9Sstevel@tonic-gate 	char	*name;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	/* get the minor name */
14007c478bd9Sstevel@tonic-gate 	if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
14017c478bd9Sstevel@tonic-gate 		return (EINVAL);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	sz = strlen(name) + 1;
14047c478bd9Sstevel@tonic-gate 	kmem_free(name, sz);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	/* copy out the size of the minor name */
14077c478bd9Sstevel@tonic-gate 	if (copyout(&sz, len, sizeof (sz)) != 0)
14087c478bd9Sstevel@tonic-gate 		return (EFAULT);
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	return (0);
14117c478bd9Sstevel@tonic-gate }
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate /*
14147c478bd9Sstevel@tonic-gate  * Return the minor name.
14157c478bd9Sstevel@tonic-gate  */
14167c478bd9Sstevel@tonic-gate static int
14177c478bd9Sstevel@tonic-gate modctl_get_minorname(dev_t dev, int spectype, uint_t len, char *uname)
14187c478bd9Sstevel@tonic-gate {
14197c478bd9Sstevel@tonic-gate 	uint_t	sz;
14207c478bd9Sstevel@tonic-gate 	char	*name;
14217c478bd9Sstevel@tonic-gate 	int	err = 0;
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	/* get the minor name */
14247c478bd9Sstevel@tonic-gate 	if (ddi_lyr_get_minor_name(dev, spectype, &name) == DDI_FAILURE)
14257c478bd9Sstevel@tonic-gate 		return (EINVAL);
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 	sz = strlen(name) + 1;
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	/* Error if the minor name is larger than the space allocated */
14307c478bd9Sstevel@tonic-gate 	if (sz > len) {
14317c478bd9Sstevel@tonic-gate 		kmem_free(name, sz);
14327c478bd9Sstevel@tonic-gate 		return (ENOSPC);
14337c478bd9Sstevel@tonic-gate 	}
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	/* copy out the minor name */
14367c478bd9Sstevel@tonic-gate 	if (copyout(name, uname, sz) != 0)
14377c478bd9Sstevel@tonic-gate 		err = EFAULT;
14387c478bd9Sstevel@tonic-gate 	kmem_free(name, sz);
14397c478bd9Sstevel@tonic-gate 	return (err);
14407c478bd9Sstevel@tonic-gate }
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate /*
1443a08731ecScth  * Return the size of the (dev_t,spectype) devfspath name.
14447c478bd9Sstevel@tonic-gate  */
14457c478bd9Sstevel@tonic-gate static int
14467c478bd9Sstevel@tonic-gate modctl_devfspath_len(dev_t dev, int spectype, uint_t *len)
14477c478bd9Sstevel@tonic-gate {
14487c478bd9Sstevel@tonic-gate 	uint_t	sz;
14497c478bd9Sstevel@tonic-gate 	char	*name;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 	/* get the path name */
14527c478bd9Sstevel@tonic-gate 	name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
14537c478bd9Sstevel@tonic-gate 	if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
14547c478bd9Sstevel@tonic-gate 		kmem_free(name, MAXPATHLEN);
14557c478bd9Sstevel@tonic-gate 		return (EINVAL);
14567c478bd9Sstevel@tonic-gate 	}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	sz = strlen(name) + 1;
14597c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXPATHLEN);
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	/* copy out the size of the path name */
14627c478bd9Sstevel@tonic-gate 	if (copyout(&sz, len, sizeof (sz)) != 0)
14637c478bd9Sstevel@tonic-gate 		return (EFAULT);
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	return (0);
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate /*
1469a08731ecScth  * Return the (dev_t,spectype) devfspath name.
14707c478bd9Sstevel@tonic-gate  */
14717c478bd9Sstevel@tonic-gate static int
14727c478bd9Sstevel@tonic-gate modctl_devfspath(dev_t dev, int spectype, uint_t len, char *uname)
14737c478bd9Sstevel@tonic-gate {
14747c478bd9Sstevel@tonic-gate 	uint_t	sz;
14757c478bd9Sstevel@tonic-gate 	char	*name;
14767c478bd9Sstevel@tonic-gate 	int	err = 0;
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 	/* get the path name */
14797c478bd9Sstevel@tonic-gate 	name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
14807c478bd9Sstevel@tonic-gate 	if (ddi_dev_pathname(dev, spectype, name) == DDI_FAILURE) {
14817c478bd9Sstevel@tonic-gate 		kmem_free(name, MAXPATHLEN);
14827c478bd9Sstevel@tonic-gate 		return (EINVAL);
14837c478bd9Sstevel@tonic-gate 	}
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	sz = strlen(name) + 1;
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	/* Error if the path name is larger than the space allocated */
14887c478bd9Sstevel@tonic-gate 	if (sz > len) {
14897c478bd9Sstevel@tonic-gate 		kmem_free(name, MAXPATHLEN);
14907c478bd9Sstevel@tonic-gate 		return (ENOSPC);
14917c478bd9Sstevel@tonic-gate 	}
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	/* copy out the path name */
14947c478bd9Sstevel@tonic-gate 	if (copyout(name, uname, sz) != 0)
14957c478bd9Sstevel@tonic-gate 		err = EFAULT;
14967c478bd9Sstevel@tonic-gate 	kmem_free(name, MAXPATHLEN);
14977c478bd9Sstevel@tonic-gate 	return (err);
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate 
1500a08731ecScth /*
1501a08731ecScth  * Return the size of the (major,instance) devfspath name.
1502a08731ecScth  */
1503a08731ecScth static int
1504a08731ecScth modctl_devfspath_mi_len(major_t major, int instance, uint_t *len)
1505a08731ecScth {
1506a08731ecScth 	uint_t	sz;
1507a08731ecScth 	char	*name;
1508a08731ecScth 
1509a08731ecScth 	/* get the path name */
1510a08731ecScth 	name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1511a08731ecScth 	if (e_ddi_majorinstance_to_path(major, instance, name) != DDI_SUCCESS) {
1512a08731ecScth 		kmem_free(name, MAXPATHLEN);
1513a08731ecScth 		return (EINVAL);
1514a08731ecScth 	}
1515a08731ecScth 
1516a08731ecScth 	sz = strlen(name) + 1;
1517a08731ecScth 	kmem_free(name, MAXPATHLEN);
1518a08731ecScth 
1519a08731ecScth 	/* copy out the size of the path name */
1520a08731ecScth 	if (copyout(&sz, len, sizeof (sz)) != 0)
1521a08731ecScth 		return (EFAULT);
1522a08731ecScth 
1523a08731ecScth 	return (0);
1524a08731ecScth }
1525a08731ecScth 
1526a08731ecScth /*
1527a08731ecScth  * Return the (major_instance) devfspath name.
1528a08731ecScth  * NOTE: e_ddi_majorinstance_to_path does not require the device to attach to
1529a08731ecScth  * return a path - it uses the instance tree.
1530a08731ecScth  */
1531a08731ecScth static int
1532a08731ecScth modctl_devfspath_mi(major_t major, int instance, uint_t len, char *uname)
1533a08731ecScth {
1534a08731ecScth 	uint_t	sz;
1535a08731ecScth 	char	*name;
1536a08731ecScth 	int	err = 0;
1537a08731ecScth 
1538a08731ecScth 	/* get the path name */
1539a08731ecScth 	name = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1540a08731ecScth 	if (e_ddi_majorinstance_to_path(major, instance, name) != DDI_SUCCESS) {
1541a08731ecScth 		kmem_free(name, MAXPATHLEN);
1542a08731ecScth 		return (EINVAL);
1543a08731ecScth 	}
1544a08731ecScth 
1545a08731ecScth 	sz = strlen(name) + 1;
1546a08731ecScth 
1547a08731ecScth 	/* Error if the path name is larger than the space allocated */
1548a08731ecScth 	if (sz > len) {
1549a08731ecScth 		kmem_free(name, MAXPATHLEN);
1550a08731ecScth 		return (ENOSPC);
1551a08731ecScth 	}
1552a08731ecScth 
1553a08731ecScth 	/* copy out the path name */
1554a08731ecScth 	if (copyout(name, uname, sz) != 0)
1555a08731ecScth 		err = EFAULT;
1556a08731ecScth 	kmem_free(name, MAXPATHLEN);
1557a08731ecScth 	return (err);
1558a08731ecScth }
1559a08731ecScth 
15607c478bd9Sstevel@tonic-gate static int
15617c478bd9Sstevel@tonic-gate modctl_get_fbname(char *path)
15627c478bd9Sstevel@tonic-gate {
15637c478bd9Sstevel@tonic-gate 	extern dev_t fbdev;
15647c478bd9Sstevel@tonic-gate 	char *pathname = NULL;
15657c478bd9Sstevel@tonic-gate 	int rval = 0;
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/* make sure fbdev is set before we plunge in */
15687c478bd9Sstevel@tonic-gate 	if (fbdev == NODEV)
15697c478bd9Sstevel@tonic-gate 		return (ENODEV);
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	pathname = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15727c478bd9Sstevel@tonic-gate 	if ((rval = ddi_dev_pathname(fbdev, S_IFCHR,
15737c478bd9Sstevel@tonic-gate 	    pathname)) == DDI_SUCCESS) {
15747c478bd9Sstevel@tonic-gate 		if (copyout(pathname, path, strlen(pathname)+1) != 0) {
15757c478bd9Sstevel@tonic-gate 			rval = EFAULT;
15767c478bd9Sstevel@tonic-gate 		}
15777c478bd9Sstevel@tonic-gate 	}
15787c478bd9Sstevel@tonic-gate 	kmem_free(pathname, MAXPATHLEN);
15797c478bd9Sstevel@tonic-gate 	return (rval);
15807c478bd9Sstevel@tonic-gate }
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate /*
15837c478bd9Sstevel@tonic-gate  * modctl_reread_dacf()
15847c478bd9Sstevel@tonic-gate  *	Reread the dacf rules database from the named binding file.
15857c478bd9Sstevel@tonic-gate  *	If NULL is specified, pass along the NULL, it means 'use the default'.
15867c478bd9Sstevel@tonic-gate  */
15877c478bd9Sstevel@tonic-gate static int
15887c478bd9Sstevel@tonic-gate modctl_reread_dacf(char *path)
15897c478bd9Sstevel@tonic-gate {
15907c478bd9Sstevel@tonic-gate 	int rval = 0;
15917c478bd9Sstevel@tonic-gate 	char *filename, *filenamep;
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	filename = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 	if (path == NULL) {
15967c478bd9Sstevel@tonic-gate 		filenamep = NULL;
15977c478bd9Sstevel@tonic-gate 	} else {
15987c478bd9Sstevel@tonic-gate 		if (copyinstr(path, filename, MAXPATHLEN, 0) != 0) {
15997c478bd9Sstevel@tonic-gate 			rval = EFAULT;
16007c478bd9Sstevel@tonic-gate 			goto out;
16017c478bd9Sstevel@tonic-gate 		}
16027c478bd9Sstevel@tonic-gate 		filenamep = filename;
16037c478bd9Sstevel@tonic-gate 		filenamep[MAXPATHLEN - 1] = '\0';
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	rval = read_dacf_binding_file(filenamep);
16077c478bd9Sstevel@tonic-gate out:
16087c478bd9Sstevel@tonic-gate 	kmem_free(filename, MAXPATHLEN);
16097c478bd9Sstevel@tonic-gate 	return (rval);
16107c478bd9Sstevel@tonic-gate }
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16137c478bd9Sstevel@tonic-gate static int
16147c478bd9Sstevel@tonic-gate modctl_modevents(int subcmd, uintptr_t a2, uintptr_t a3, uintptr_t a4,
16157c478bd9Sstevel@tonic-gate     uint_t flag)
16167c478bd9Sstevel@tonic-gate {
16177c478bd9Sstevel@tonic-gate 	int error = 0;
16187c478bd9Sstevel@tonic-gate 	char *filenamep;
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	switch (subcmd) {
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	case MODEVENTS_FLUSH:
16237c478bd9Sstevel@tonic-gate 		/* flush all currently queued events */
16247c478bd9Sstevel@tonic-gate 		log_sysevent_flushq(subcmd, flag);
16257c478bd9Sstevel@tonic-gate 		break;
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	case MODEVENTS_SET_DOOR_UPCALL_FILENAME:
16287c478bd9Sstevel@tonic-gate 		/*
16297c478bd9Sstevel@tonic-gate 		 * bind door_upcall to filename
16307c478bd9Sstevel@tonic-gate 		 * this should only be done once per invocation
16317c478bd9Sstevel@tonic-gate 		 * of the event daemon.
16327c478bd9Sstevel@tonic-gate 		 */
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 		filenamep = kmem_zalloc(MOD_MAXPATH, KM_SLEEP);
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 		if (copyinstr((char *)a2, filenamep, MOD_MAXPATH, 0)) {
16377c478bd9Sstevel@tonic-gate 			error = EFAULT;
16387c478bd9Sstevel@tonic-gate 		} else {
16397c478bd9Sstevel@tonic-gate 			error = log_sysevent_filename(filenamep);
16407c478bd9Sstevel@tonic-gate 		}
16417c478bd9Sstevel@tonic-gate 		kmem_free(filenamep, MOD_MAXPATH);
16427c478bd9Sstevel@tonic-gate 		break;
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	case MODEVENTS_GETDATA:
16457c478bd9Sstevel@tonic-gate 		error = log_sysevent_copyout_data((sysevent_id_t *)a2,
16467c478bd9Sstevel@tonic-gate 		    (size_t)a3, (caddr_t)a4);
16477c478bd9Sstevel@tonic-gate 		break;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	case MODEVENTS_FREEDATA:
16507c478bd9Sstevel@tonic-gate 		error = log_sysevent_free_data((sysevent_id_t *)a2);
16517c478bd9Sstevel@tonic-gate 		break;
16527c478bd9Sstevel@tonic-gate 	case MODEVENTS_POST_EVENT:
16537c478bd9Sstevel@tonic-gate 		error = log_usr_sysevent((sysevent_t *)a2, (uint32_t)a3,
16547c478bd9Sstevel@tonic-gate 		    (sysevent_id_t *)a4);
16557c478bd9Sstevel@tonic-gate 		break;
16567c478bd9Sstevel@tonic-gate 	case MODEVENTS_REGISTER_EVENT:
16577c478bd9Sstevel@tonic-gate 		error = log_sysevent_register((char *)a2, (char *)a3,
16587c478bd9Sstevel@tonic-gate 		    (se_pubsub_t *)a4);
16597c478bd9Sstevel@tonic-gate 		break;
16607c478bd9Sstevel@tonic-gate 	default:
16617c478bd9Sstevel@tonic-gate 		error = EINVAL;
16627c478bd9Sstevel@tonic-gate 	}
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	return (error);
16657c478bd9Sstevel@tonic-gate }
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate static void
16687c478bd9Sstevel@tonic-gate free_mperm(mperm_t *mp)
16697c478bd9Sstevel@tonic-gate {
16707c478bd9Sstevel@tonic-gate 	int len;
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	if (mp->mp_minorname) {
16737c478bd9Sstevel@tonic-gate 		len = strlen(mp->mp_minorname) + 1;
16747c478bd9Sstevel@tonic-gate 		kmem_free(mp->mp_minorname, len);
16757c478bd9Sstevel@tonic-gate 	}
16767c478bd9Sstevel@tonic-gate 	kmem_free(mp, sizeof (mperm_t));
16777c478bd9Sstevel@tonic-gate }
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate #define	MP_NO_DRV_ERR	\
16807c478bd9Sstevel@tonic-gate 	"/etc/minor_perm: no driver for %s\n"
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate #define	MP_EMPTY_MINOR	\
16837c478bd9Sstevel@tonic-gate 	"/etc/minor_perm: empty minor name for driver %s\n"
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate #define	MP_NO_MINOR	\
16867c478bd9Sstevel@tonic-gate 	"/etc/minor_perm: no minor matching %s for driver %s\n"
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate /*
16897c478bd9Sstevel@tonic-gate  * Remove mperm entry with matching minorname
16907c478bd9Sstevel@tonic-gate  */
16917c478bd9Sstevel@tonic-gate static void
16927c478bd9Sstevel@tonic-gate rem_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
16937c478bd9Sstevel@tonic-gate {
16947c478bd9Sstevel@tonic-gate 	mperm_t **mp_head;
16957c478bd9Sstevel@tonic-gate 	mperm_t *freemp = NULL;
16967c478bd9Sstevel@tonic-gate 	struct devnames *dnp = &devnamesp[major];
16977c478bd9Sstevel@tonic-gate 	mperm_t **wildmp;
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
17027c478bd9Sstevel@tonic-gate 	if (strcmp(mp->mp_minorname, "*") == 0) {
17037c478bd9Sstevel@tonic-gate 		wildmp = ((is_clone == 0) ?
17047c478bd9Sstevel@tonic-gate 		    &dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
17057c478bd9Sstevel@tonic-gate 		if (*wildmp)
17067c478bd9Sstevel@tonic-gate 			freemp = *wildmp;
17077c478bd9Sstevel@tonic-gate 		*wildmp = NULL;
17087c478bd9Sstevel@tonic-gate 	} else {
17097c478bd9Sstevel@tonic-gate 		mp_head = &dnp->dn_mperm;
17107c478bd9Sstevel@tonic-gate 		while (*mp_head) {
17117c478bd9Sstevel@tonic-gate 			if (strcmp((*mp_head)->mp_minorname,
17127c478bd9Sstevel@tonic-gate 			    mp->mp_minorname) != 0) {
17137c478bd9Sstevel@tonic-gate 				mp_head = &(*mp_head)->mp_next;
17147c478bd9Sstevel@tonic-gate 				continue;
17157c478bd9Sstevel@tonic-gate 			}
17167c478bd9Sstevel@tonic-gate 			/* remove the entry */
17177c478bd9Sstevel@tonic-gate 			freemp = *mp_head;
17187c478bd9Sstevel@tonic-gate 			*mp_head = freemp->mp_next;
17197c478bd9Sstevel@tonic-gate 			break;
17207c478bd9Sstevel@tonic-gate 		}
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate 	if (freemp) {
17237c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM) {
17247c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
17257c478bd9Sstevel@tonic-gate 			    drvname, freemp->mp_minorname,
17267c478bd9Sstevel@tonic-gate 			    freemp->mp_mode & 0777,
17277c478bd9Sstevel@tonic-gate 			    freemp->mp_uid, freemp->mp_gid);
17287c478bd9Sstevel@tonic-gate 		}
17297c478bd9Sstevel@tonic-gate 		free_mperm(freemp);
17307c478bd9Sstevel@tonic-gate 	} else {
17317c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM) {
17327c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, MP_NO_MINOR,
17337c478bd9Sstevel@tonic-gate 			    drvname, mp->mp_minorname);
17347c478bd9Sstevel@tonic-gate 		}
17357c478bd9Sstevel@tonic-gate 	}
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
17387c478bd9Sstevel@tonic-gate }
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate /*
17417c478bd9Sstevel@tonic-gate  * Add minor perm entry
17427c478bd9Sstevel@tonic-gate  */
17437c478bd9Sstevel@tonic-gate static void
17447c478bd9Sstevel@tonic-gate add_minorperm(major_t major, char *drvname, mperm_t *mp, int is_clone)
17457c478bd9Sstevel@tonic-gate {
17467c478bd9Sstevel@tonic-gate 	mperm_t **mp_head;
17477c478bd9Sstevel@tonic-gate 	mperm_t *freemp = NULL;
17487c478bd9Sstevel@tonic-gate 	struct devnames *dnp = &devnamesp[major];
17497c478bd9Sstevel@tonic-gate 	mperm_t **wildmp;
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	ASSERT(mp->mp_minorname && strlen(mp->mp_minorname) > 0);
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 	/*
17547c478bd9Sstevel@tonic-gate 	 * Note that update_drv replace semantics require
17557c478bd9Sstevel@tonic-gate 	 * replacing matching entries with the new permissions.
17567c478bd9Sstevel@tonic-gate 	 */
17577c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
17587c478bd9Sstevel@tonic-gate 	if (strcmp(mp->mp_minorname, "*") == 0) {
17597c478bd9Sstevel@tonic-gate 		wildmp = ((is_clone == 0) ?
17607c478bd9Sstevel@tonic-gate 		    &dnp->dn_mperm_wild : &dnp->dn_mperm_clone);
17617c478bd9Sstevel@tonic-gate 		if (*wildmp)
17627c478bd9Sstevel@tonic-gate 			freemp = *wildmp;
17637c478bd9Sstevel@tonic-gate 		*wildmp = mp;
17647c478bd9Sstevel@tonic-gate 	} else {
17657c478bd9Sstevel@tonic-gate 		mperm_t *p, *v = NULL;
17667c478bd9Sstevel@tonic-gate 		for (p = dnp->dn_mperm; p; v = p, p = p->mp_next) {
17677c478bd9Sstevel@tonic-gate 			if (strcmp(p->mp_minorname, mp->mp_minorname) == 0) {
17687c478bd9Sstevel@tonic-gate 				if (v == NULL)
17697c478bd9Sstevel@tonic-gate 					dnp->dn_mperm = mp;
17707c478bd9Sstevel@tonic-gate 				else
17717c478bd9Sstevel@tonic-gate 					v->mp_next = mp;
17727c478bd9Sstevel@tonic-gate 				mp->mp_next = p->mp_next;
17737c478bd9Sstevel@tonic-gate 				freemp = p;
17747c478bd9Sstevel@tonic-gate 				goto replaced;
17757c478bd9Sstevel@tonic-gate 			}
17767c478bd9Sstevel@tonic-gate 		}
17777c478bd9Sstevel@tonic-gate 		if (p == NULL) {
17787c478bd9Sstevel@tonic-gate 			mp_head = &dnp->dn_mperm;
17797c478bd9Sstevel@tonic-gate 			if (*mp_head == NULL) {
17807c478bd9Sstevel@tonic-gate 				*mp_head = mp;
17817c478bd9Sstevel@tonic-gate 			} else {
17827c478bd9Sstevel@tonic-gate 				mp->mp_next = *mp_head;
17837c478bd9Sstevel@tonic-gate 				*mp_head = mp;
17847c478bd9Sstevel@tonic-gate 			}
17857c478bd9Sstevel@tonic-gate 		}
17867c478bd9Sstevel@tonic-gate 	}
17877c478bd9Sstevel@tonic-gate replaced:
17887c478bd9Sstevel@tonic-gate 	if (freemp) {
17897c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM) {
17907c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "< %s %s 0%o %d %d\n",
17917c478bd9Sstevel@tonic-gate 			    drvname, freemp->mp_minorname,
17927c478bd9Sstevel@tonic-gate 			    freemp->mp_mode & 0777,
17937c478bd9Sstevel@tonic-gate 			    freemp->mp_uid, freemp->mp_gid);
17947c478bd9Sstevel@tonic-gate 		}
17957c478bd9Sstevel@tonic-gate 		free_mperm(freemp);
17967c478bd9Sstevel@tonic-gate 	}
17977c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_MINORPERM) {
17987c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT, "> %s %s 0%o %d %d\n",
17997c478bd9Sstevel@tonic-gate 		    drvname, mp->mp_minorname, mp->mp_mode & 0777,
18007c478bd9Sstevel@tonic-gate 		    mp->mp_uid, mp->mp_gid);
18017c478bd9Sstevel@tonic-gate 	}
18027c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
18037c478bd9Sstevel@tonic-gate }
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate static int
18077c478bd9Sstevel@tonic-gate process_minorperm(int cmd, nvlist_t *nvl)
18087c478bd9Sstevel@tonic-gate {
18097c478bd9Sstevel@tonic-gate 	char *minor;
18107c478bd9Sstevel@tonic-gate 	major_t major;
18117c478bd9Sstevel@tonic-gate 	mperm_t *mp;
18127c478bd9Sstevel@tonic-gate 	nvpair_t *nvp;
18137c478bd9Sstevel@tonic-gate 	char *name;
18147c478bd9Sstevel@tonic-gate 	int is_clone;
18157c478bd9Sstevel@tonic-gate 	major_t minmaj;
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	ASSERT(cmd == MODLOADMINORPERM ||
18187c478bd9Sstevel@tonic-gate 	    cmd == MODADDMINORPERM || cmd == MODREMMINORPERM);
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	nvp = NULL;
18217c478bd9Sstevel@tonic-gate 	while ((nvp = nvlist_next_nvpair(nvl, nvp)) != NULL) {
18227c478bd9Sstevel@tonic-gate 		name = nvpair_name(nvp);
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 		is_clone = 0;
18257c478bd9Sstevel@tonic-gate 		(void) nvpair_value_string(nvp, &minor);
18267c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(name);
1827a204de77Scth 		if (major != DDI_MAJOR_T_NONE) {
18287c478bd9Sstevel@tonic-gate 			mp = kmem_zalloc(sizeof (*mp), KM_SLEEP);
18297c478bd9Sstevel@tonic-gate 			if (minor == NULL || strlen(minor) == 0) {
18307c478bd9Sstevel@tonic-gate 				if (moddebug & MODDEBUG_MINORPERM) {
18317c478bd9Sstevel@tonic-gate 					cmn_err(CE_CONT, MP_EMPTY_MINOR, name);
18327c478bd9Sstevel@tonic-gate 				}
18337c478bd9Sstevel@tonic-gate 				minor = "*";
18347c478bd9Sstevel@tonic-gate 			}
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 			/*
18377c478bd9Sstevel@tonic-gate 			 * The minor name of a node using the clone
18387c478bd9Sstevel@tonic-gate 			 * driver must be the driver name.  To avoid
18397c478bd9Sstevel@tonic-gate 			 * multiple searches, we map entries in the form
18407c478bd9Sstevel@tonic-gate 			 * clone:<driver> to <driver>:*.  This also allows us
18417c478bd9Sstevel@tonic-gate 			 * to filter out some of the litter in /etc/minor_perm.
18427c478bd9Sstevel@tonic-gate 			 * Minor perm alias entries where the name is not
18437c478bd9Sstevel@tonic-gate 			 * the driver kept on the clone list itself.
18447c478bd9Sstevel@tonic-gate 			 * This all seems very fragile as a driver could
18457c478bd9Sstevel@tonic-gate 			 * be introduced with an existing alias name.
18467c478bd9Sstevel@tonic-gate 			 */
18477c478bd9Sstevel@tonic-gate 			if (strcmp(name, "clone") == 0) {
18487c478bd9Sstevel@tonic-gate 				minmaj = ddi_name_to_major(minor);
1849a204de77Scth 				if (minmaj != DDI_MAJOR_T_NONE) {
18507c478bd9Sstevel@tonic-gate 					if (moddebug & MODDEBUG_MINORPERM) {
18517c478bd9Sstevel@tonic-gate 						cmn_err(CE_CONT,
18527c478bd9Sstevel@tonic-gate 						    "mapping %s:%s to %s:*\n",
18537c478bd9Sstevel@tonic-gate 						    name, minor, minor);
18547c478bd9Sstevel@tonic-gate 					}
18557c478bd9Sstevel@tonic-gate 					major = minmaj;
18567c478bd9Sstevel@tonic-gate 					name = minor;
18577c478bd9Sstevel@tonic-gate 					minor = "*";
18587c478bd9Sstevel@tonic-gate 					is_clone = 1;
18597c478bd9Sstevel@tonic-gate 				}
18607c478bd9Sstevel@tonic-gate 			}
18617c478bd9Sstevel@tonic-gate 
18627c478bd9Sstevel@tonic-gate 			if (mp) {
18637c478bd9Sstevel@tonic-gate 				mp->mp_minorname =
18647c478bd9Sstevel@tonic-gate 				    i_ddi_strdup(minor, KM_SLEEP);
18657c478bd9Sstevel@tonic-gate 			}
18667c478bd9Sstevel@tonic-gate 		} else {
18677c478bd9Sstevel@tonic-gate 			mp = NULL;
18687c478bd9Sstevel@tonic-gate 			if (moddebug & MODDEBUG_MINORPERM) {
18697c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, MP_NO_DRV_ERR, name);
18707c478bd9Sstevel@tonic-gate 			}
18717c478bd9Sstevel@tonic-gate 		}
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 		/* mode */
18747c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(nvl, nvp);
18757c478bd9Sstevel@tonic-gate 		ASSERT(strcmp(nvpair_name(nvp), "mode") == 0);
18767c478bd9Sstevel@tonic-gate 		if (mp)
18777c478bd9Sstevel@tonic-gate 			(void) nvpair_value_int32(nvp, (int *)&mp->mp_mode);
18787c478bd9Sstevel@tonic-gate 		/* uid */
18797c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(nvl, nvp);
18807c478bd9Sstevel@tonic-gate 		ASSERT(strcmp(nvpair_name(nvp), "uid") == 0);
18817c478bd9Sstevel@tonic-gate 		if (mp)
1882f48205beScasper 			(void) nvpair_value_uint32(nvp, &mp->mp_uid);
18837c478bd9Sstevel@tonic-gate 		/* gid */
18847c478bd9Sstevel@tonic-gate 		nvp = nvlist_next_nvpair(nvl, nvp);
18857c478bd9Sstevel@tonic-gate 		ASSERT(strcmp(nvpair_name(nvp), "gid") == 0);
18867c478bd9Sstevel@tonic-gate 		if (mp) {
1887f48205beScasper 			(void) nvpair_value_uint32(nvp, &mp->mp_gid);
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 			if (cmd == MODREMMINORPERM) {
18907c478bd9Sstevel@tonic-gate 				rem_minorperm(major, name, mp, is_clone);
18917c478bd9Sstevel@tonic-gate 				free_mperm(mp);
18927c478bd9Sstevel@tonic-gate 			} else {
18937c478bd9Sstevel@tonic-gate 				add_minorperm(major, name, mp, is_clone);
18947c478bd9Sstevel@tonic-gate 			}
18957c478bd9Sstevel@tonic-gate 		}
18967c478bd9Sstevel@tonic-gate 	}
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	if (cmd == MODLOADMINORPERM)
18997c478bd9Sstevel@tonic-gate 		minorperm_loaded = 1;
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate 	/*
19027c478bd9Sstevel@tonic-gate 	 * Reset permissions of cached dv_nodes
19037c478bd9Sstevel@tonic-gate 	 */
19047c478bd9Sstevel@tonic-gate 	(void) devfs_reset_perm(DV_RESET_PERM);
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	return (0);
19077c478bd9Sstevel@tonic-gate }
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate static int
19107c478bd9Sstevel@tonic-gate modctl_minorperm(int cmd, char *usrbuf, size_t buflen)
19117c478bd9Sstevel@tonic-gate {
19127c478bd9Sstevel@tonic-gate 	int error;
19137c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
19147c478bd9Sstevel@tonic-gate 	char *buf = kmem_alloc(buflen, KM_SLEEP);
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 	if ((error = ddi_copyin(usrbuf, buf, buflen, 0)) != 0) {
19177c478bd9Sstevel@tonic-gate 		kmem_free(buf, buflen);
19187c478bd9Sstevel@tonic-gate 		return (error);
19197c478bd9Sstevel@tonic-gate 	}
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	error = nvlist_unpack(buf, buflen, &nvl, KM_SLEEP);
19227c478bd9Sstevel@tonic-gate 	kmem_free(buf, buflen);
19237c478bd9Sstevel@tonic-gate 	if (error)
19247c478bd9Sstevel@tonic-gate 		return (error);
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	error = process_minorperm(cmd, nvl);
19277c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
19287c478bd9Sstevel@tonic-gate 	return (error);
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate struct walk_args {
19327c478bd9Sstevel@tonic-gate 	char		*wa_drvname;
19337c478bd9Sstevel@tonic-gate 	list_t		wa_pathlist;
19347c478bd9Sstevel@tonic-gate };
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate struct path_elem {
19377c478bd9Sstevel@tonic-gate 	char		*pe_dir;
19387c478bd9Sstevel@tonic-gate 	char		*pe_nodename;
19397c478bd9Sstevel@tonic-gate 	list_node_t	pe_node;
19407c478bd9Sstevel@tonic-gate 	int		pe_dirlen;
19417c478bd9Sstevel@tonic-gate };
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19447c478bd9Sstevel@tonic-gate static int
19457c478bd9Sstevel@tonic-gate modctl_inst_walker(const char *path, in_node_t *np, in_drv_t *dp, void *arg)
19467c478bd9Sstevel@tonic-gate {
19477c478bd9Sstevel@tonic-gate 	struct walk_args *wargs = (struct walk_args *)arg;
19487c478bd9Sstevel@tonic-gate 	struct path_elem *pe;
19497c478bd9Sstevel@tonic-gate 	char *nodename;
19507c478bd9Sstevel@tonic-gate 
19511aef0e11Sjg 	/*
19521aef0e11Sjg 	 * Search may be restricted to a single driver in the case of rem_drv
19531aef0e11Sjg 	 */
19541aef0e11Sjg 	if (wargs->wa_drvname &&
19551aef0e11Sjg 	    strcmp(dp->ind_driver_name, wargs->wa_drvname) != 0)
19567c478bd9Sstevel@tonic-gate 		return (INST_WALK_CONTINUE);
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 	pe = kmem_zalloc(sizeof (*pe), KM_SLEEP);
19597c478bd9Sstevel@tonic-gate 	pe->pe_dir = i_ddi_strdup((char *)path, KM_SLEEP);
19607c478bd9Sstevel@tonic-gate 	pe->pe_dirlen = strlen(pe->pe_dir) + 1;
19617c478bd9Sstevel@tonic-gate 	ASSERT(strrchr(pe->pe_dir, '/') != NULL);
19627c478bd9Sstevel@tonic-gate 	nodename = strrchr(pe->pe_dir, '/');
19637c478bd9Sstevel@tonic-gate 	*nodename++ = 0;
19647c478bd9Sstevel@tonic-gate 	pe->pe_nodename = nodename;
19657c478bd9Sstevel@tonic-gate 	list_insert_tail(&wargs->wa_pathlist, pe);
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	return (INST_WALK_CONTINUE);
19687c478bd9Sstevel@tonic-gate }
19697c478bd9Sstevel@tonic-gate 
19701aef0e11Sjg /*
19711aef0e11Sjg  * /devices attribute nodes clean-up optionally performed
19721aef0e11Sjg  * when removing a driver (rem_drv -C).
19731aef0e11Sjg  *
19741aef0e11Sjg  * Removing attribute nodes allows a machine to be reprovisioned
19751aef0e11Sjg  * without the side-effect of inadvertently picking up stale
19761aef0e11Sjg  * device node ownership or permissions.
19771aef0e11Sjg  *
19781aef0e11Sjg  * Preserving attributes (not performing cleanup) allows devices
19791aef0e11Sjg  * attribute changes to be preserved across upgrades, as
19801aef0e11Sjg  * upgrade rather heavy-handedly does a rem_drv/add_drv cycle.
19811aef0e11Sjg  */
19827c478bd9Sstevel@tonic-gate static int
19837c478bd9Sstevel@tonic-gate modctl_remdrv_cleanup(const char *u_drvname)
19847c478bd9Sstevel@tonic-gate {
19857c478bd9Sstevel@tonic-gate 	struct walk_args *wargs;
19867c478bd9Sstevel@tonic-gate 	struct path_elem *pe;
19877c478bd9Sstevel@tonic-gate 	char *drvname;
19887c478bd9Sstevel@tonic-gate 	int err, rval = 0;
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	drvname = kmem_alloc(MAXMODCONFNAME, KM_SLEEP);
19917c478bd9Sstevel@tonic-gate 	if ((err = copyinstr(u_drvname, drvname, MAXMODCONFNAME, 0))) {
19927c478bd9Sstevel@tonic-gate 		kmem_free(drvname, MAXMODCONFNAME);
19937c478bd9Sstevel@tonic-gate 		return (err);
19947c478bd9Sstevel@tonic-gate 	}
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	/*
19977c478bd9Sstevel@tonic-gate 	 * First go through the instance database.  For each
19987c478bd9Sstevel@tonic-gate 	 * instance of a device bound to the driver being
19997c478bd9Sstevel@tonic-gate 	 * removed, remove any underlying devfs attribute nodes.
20007c478bd9Sstevel@tonic-gate 	 *
20017c478bd9Sstevel@tonic-gate 	 * This is a two-step process.	First we go through
20027c478bd9Sstevel@tonic-gate 	 * the instance data itself, constructing a list of
20037c478bd9Sstevel@tonic-gate 	 * the nodes discovered.  The second step is then
20047c478bd9Sstevel@tonic-gate 	 * to find and remove any devfs attribute nodes
20057c478bd9Sstevel@tonic-gate 	 * for the instances discovered in the first step.
20067c478bd9Sstevel@tonic-gate 	 * The two-step process avoids any difficulties
20077c478bd9Sstevel@tonic-gate 	 * which could arise by holding the instance data
20087c478bd9Sstevel@tonic-gate 	 * lock with simultaneous devfs operations.
20097c478bd9Sstevel@tonic-gate 	 */
20107c478bd9Sstevel@tonic-gate 	wargs = kmem_zalloc(sizeof (*wargs), KM_SLEEP);
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	wargs->wa_drvname = drvname;
20137c478bd9Sstevel@tonic-gate 	list_create(&wargs->wa_pathlist,
20147c478bd9Sstevel@tonic-gate 	    sizeof (struct path_elem), offsetof(struct path_elem, pe_node));
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	(void) e_ddi_walk_instances(modctl_inst_walker, (void *)wargs);
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	for (pe = list_head(&wargs->wa_pathlist); pe != NULL;
20197c478bd9Sstevel@tonic-gate 	    pe = list_next(&wargs->wa_pathlist, pe)) {
20207c478bd9Sstevel@tonic-gate 		err = devfs_remdrv_cleanup((const char *)pe->pe_dir,
20217c478bd9Sstevel@tonic-gate 		    (const char *)pe->pe_nodename);
20227c478bd9Sstevel@tonic-gate 		if (rval == 0)
20237c478bd9Sstevel@tonic-gate 			rval = err;
20247c478bd9Sstevel@tonic-gate 	}
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	while ((pe = list_head(&wargs->wa_pathlist)) != NULL) {
20277c478bd9Sstevel@tonic-gate 		list_remove(&wargs->wa_pathlist, pe);
20287c478bd9Sstevel@tonic-gate 		kmem_free(pe->pe_dir, pe->pe_dirlen);
20297c478bd9Sstevel@tonic-gate 		kmem_free(pe, sizeof (*pe));
20307c478bd9Sstevel@tonic-gate 	}
20317c478bd9Sstevel@tonic-gate 	kmem_free(wargs, sizeof (*wargs));
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	/*
20347c478bd9Sstevel@tonic-gate 	 * Pseudo nodes aren't recorded in the instance database
20357c478bd9Sstevel@tonic-gate 	 * so any such nodes need to be handled separately.
20367c478bd9Sstevel@tonic-gate 	 */
20377c478bd9Sstevel@tonic-gate 	err = devfs_remdrv_cleanup("pseudo", (const char *)drvname);
20387c478bd9Sstevel@tonic-gate 	if (rval == 0)
20397c478bd9Sstevel@tonic-gate 		rval = err;
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	kmem_free(drvname, MAXMODCONFNAME);
20427c478bd9Sstevel@tonic-gate 	return (rval);
20437c478bd9Sstevel@tonic-gate }
20447c478bd9Sstevel@tonic-gate 
20451aef0e11Sjg /*
20461aef0e11Sjg  * Perform a cleanup of non-existent /devices attribute nodes,
20471aef0e11Sjg  * similar to rem_drv -C, but for all drivers/devices.
20481aef0e11Sjg  * This is also optional, performed as part of devfsadm -C.
20491aef0e11Sjg  */
20501aef0e11Sjg void
20511aef0e11Sjg dev_devices_cleanup()
20521aef0e11Sjg {
20531aef0e11Sjg 	struct walk_args *wargs;
20541aef0e11Sjg 	struct path_elem *pe;
20551aef0e11Sjg 	dev_info_t *devi;
20561aef0e11Sjg 	char *path;
20571aef0e11Sjg 	int err;
20581aef0e11Sjg 
20591aef0e11Sjg 	/*
20601aef0e11Sjg 	 * It's expected that all drivers have been loaded and
20611aef0e11Sjg 	 * module unloading disabled while performing cleanup.
20621aef0e11Sjg 	 */
20631aef0e11Sjg 	ASSERT(modunload_disable_count > 0);
20641aef0e11Sjg 
20651aef0e11Sjg 	wargs = kmem_zalloc(sizeof (*wargs), KM_SLEEP);
20661aef0e11Sjg 	wargs->wa_drvname = NULL;
20671aef0e11Sjg 	list_create(&wargs->wa_pathlist,
20681aef0e11Sjg 	    sizeof (struct path_elem), offsetof(struct path_elem, pe_node));
20691aef0e11Sjg 
20701aef0e11Sjg 	(void) e_ddi_walk_instances(modctl_inst_walker, (void *)wargs);
20711aef0e11Sjg 
20721aef0e11Sjg 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
20731aef0e11Sjg 
20741aef0e11Sjg 	for (pe = list_head(&wargs->wa_pathlist); pe != NULL;
20751aef0e11Sjg 	    pe = list_next(&wargs->wa_pathlist, pe)) {
20761aef0e11Sjg 		(void) snprintf(path, MAXPATHLEN, "%s/%s",
20771aef0e11Sjg 		    pe->pe_dir, pe->pe_nodename);
20781aef0e11Sjg 		devi = e_ddi_hold_devi_by_path(path, 0);
20791aef0e11Sjg 		if (devi != NULL) {
20801aef0e11Sjg 			ddi_release_devi(devi);
20811aef0e11Sjg 		} else {
20821aef0e11Sjg 			err = devfs_remdrv_cleanup((const char *)pe->pe_dir,
20831aef0e11Sjg 			    (const char *)pe->pe_nodename);
20841aef0e11Sjg 			if (err) {
20851aef0e11Sjg 				cmn_err(CE_CONT,
20861aef0e11Sjg 				    "devfs: %s: clean-up error %d\n",
20871aef0e11Sjg 				    path, err);
20881aef0e11Sjg 			}
20891aef0e11Sjg 		}
20901aef0e11Sjg 	}
20911aef0e11Sjg 
20921aef0e11Sjg 	while ((pe = list_head(&wargs->wa_pathlist)) != NULL) {
20931aef0e11Sjg 		list_remove(&wargs->wa_pathlist, pe);
20941aef0e11Sjg 		kmem_free(pe->pe_dir, pe->pe_dirlen);
20951aef0e11Sjg 		kmem_free(pe, sizeof (*pe));
20961aef0e11Sjg 	}
20971aef0e11Sjg 	kmem_free(wargs, sizeof (*wargs));
20981aef0e11Sjg 	kmem_free(path, MAXPATHLEN);
20991aef0e11Sjg }
21001aef0e11Sjg 
21017c478bd9Sstevel@tonic-gate static int
21027c478bd9Sstevel@tonic-gate modctl_allocpriv(const char *name)
21037c478bd9Sstevel@tonic-gate {
21047c478bd9Sstevel@tonic-gate 	char *pstr = kmem_alloc(PRIVNAME_MAX, KM_SLEEP);
21057c478bd9Sstevel@tonic-gate 	int error;
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 	if ((error = copyinstr(name, pstr, PRIVNAME_MAX, 0))) {
21087c478bd9Sstevel@tonic-gate 		kmem_free(pstr, PRIVNAME_MAX);
21097c478bd9Sstevel@tonic-gate 		return (error);
21107c478bd9Sstevel@tonic-gate 	}
21117c478bd9Sstevel@tonic-gate 	error = priv_getbyname(pstr, PRIV_ALLOC);
21127c478bd9Sstevel@tonic-gate 	if (error < 0)
21137c478bd9Sstevel@tonic-gate 		error = -error;
21147c478bd9Sstevel@tonic-gate 	else
21157c478bd9Sstevel@tonic-gate 		error = 0;
21167c478bd9Sstevel@tonic-gate 	kmem_free(pstr, PRIVNAME_MAX);
21177c478bd9Sstevel@tonic-gate 	return (error);
21187c478bd9Sstevel@tonic-gate }
21197c478bd9Sstevel@tonic-gate 
2120facf4a8dSllai1 static int
2121facf4a8dSllai1 modctl_devexists(const char *upath, int pathlen)
2122facf4a8dSllai1 {
2123facf4a8dSllai1 	char	*path;
2124facf4a8dSllai1 	int	ret;
2125facf4a8dSllai1 
2126facf4a8dSllai1 	/*
2127facf4a8dSllai1 	 * copy in the path, including the terminating null
2128facf4a8dSllai1 	 */
2129facf4a8dSllai1 	pathlen++;
2130facf4a8dSllai1 	if (pathlen <= 1 || pathlen > MAXPATHLEN)
2131facf4a8dSllai1 		return (EINVAL);
2132facf4a8dSllai1 	path = kmem_zalloc(pathlen + 1, KM_SLEEP);
2133facf4a8dSllai1 	if ((ret = copyinstr(upath, path, pathlen, NULL)) == 0) {
2134facf4a8dSllai1 		ret = sdev_modctl_devexists(path);
2135facf4a8dSllai1 	}
2136facf4a8dSllai1 
2137facf4a8dSllai1 	kmem_free(path, pathlen + 1);
2138facf4a8dSllai1 	return (ret);
2139facf4a8dSllai1 }
2140facf4a8dSllai1 
2141facf4a8dSllai1 static int
2142facf4a8dSllai1 modctl_devreaddir(const char *udir, int udirlen,
2143facf4a8dSllai1     char *upaths, int64_t *ulensp)
2144facf4a8dSllai1 {
2145facf4a8dSllai1 	char	*paths = NULL;
2146facf4a8dSllai1 	char	**dirlist = NULL;
2147facf4a8dSllai1 	char	*dir;
2148facf4a8dSllai1 	int64_t	ulens;
2149facf4a8dSllai1 	int64_t	lens;
2150facf4a8dSllai1 	int	i, n;
2151facf4a8dSllai1 	int	ret = 0;
2152facf4a8dSllai1 	char	*p;
2153facf4a8dSllai1 	int	npaths;
2154facf4a8dSllai1 	int	npaths_alloc;
2155facf4a8dSllai1 
2156facf4a8dSllai1 	/*
2157facf4a8dSllai1 	 * If upaths is NULL then we are only computing the amount of space
2158facf4a8dSllai1 	 * needed to return the paths, with the value returned in *ulensp. If we
2159facf4a8dSllai1 	 * are copying out paths then we get the amount of space allocated by
2160facf4a8dSllai1 	 * the caller. If the actual space needed for paths is larger, or
2161facf4a8dSllai1 	 * things are changing out from under us, then we return EAGAIN.
2162facf4a8dSllai1 	 */
2163facf4a8dSllai1 	if (upaths) {
2164facf4a8dSllai1 		if (ulensp == NULL)
2165facf4a8dSllai1 			return (EINVAL);
2166facf4a8dSllai1 		if (copyin(ulensp, &ulens, sizeof (ulens)) != 0)
2167facf4a8dSllai1 			return (EFAULT);
2168facf4a8dSllai1 	}
2169facf4a8dSllai1 
2170facf4a8dSllai1 	/*
2171facf4a8dSllai1 	 * copyin the /dev path including terminating null
2172facf4a8dSllai1 	 */
2173facf4a8dSllai1 	udirlen++;
2174facf4a8dSllai1 	if (udirlen <= 1 || udirlen > MAXPATHLEN)
2175facf4a8dSllai1 		return (EINVAL);
2176facf4a8dSllai1 	dir = kmem_zalloc(udirlen + 1, KM_SLEEP);
2177facf4a8dSllai1 	if ((ret = copyinstr(udir, dir, udirlen, NULL)) != 0)
2178facf4a8dSllai1 		goto err;
2179facf4a8dSllai1 
2180facf4a8dSllai1 	if ((ret = sdev_modctl_readdir(dir, &dirlist,
2181e37c6c37Scth 	    &npaths, &npaths_alloc, 0)) != 0) {
2182facf4a8dSllai1 		ASSERT(dirlist == NULL);
2183facf4a8dSllai1 		goto err;
2184facf4a8dSllai1 	}
2185facf4a8dSllai1 
2186facf4a8dSllai1 	lens = 0;
2187facf4a8dSllai1 	for (i = 0; i < npaths; i++) {
2188facf4a8dSllai1 		lens += strlen(dirlist[i]) + 1;
2189facf4a8dSllai1 	}
2190facf4a8dSllai1 	lens++;		/* add one for double termination */
2191facf4a8dSllai1 
2192facf4a8dSllai1 	if (upaths) {
2193facf4a8dSllai1 		if (lens > ulens) {
2194facf4a8dSllai1 			ret = EAGAIN;
2195facf4a8dSllai1 			goto out;
2196facf4a8dSllai1 		}
2197facf4a8dSllai1 
2198facf4a8dSllai1 		paths = kmem_alloc(lens, KM_SLEEP);
2199facf4a8dSllai1 
2200facf4a8dSllai1 		p = paths;
2201facf4a8dSllai1 		for (i = 0; i < npaths; i++) {
2202facf4a8dSllai1 			n = strlen(dirlist[i]) + 1;
2203facf4a8dSllai1 			bcopy(dirlist[i], p, n);
2204facf4a8dSllai1 			p += n;
2205facf4a8dSllai1 		}
2206facf4a8dSllai1 		*p = 0;
2207facf4a8dSllai1 
2208facf4a8dSllai1 		if (copyout(paths, upaths, lens)) {
2209facf4a8dSllai1 			ret = EFAULT;
2210facf4a8dSllai1 			goto err;
2211facf4a8dSllai1 		}
2212facf4a8dSllai1 	}
2213facf4a8dSllai1 
2214facf4a8dSllai1 out:
2215facf4a8dSllai1 	/* copy out the amount of space needed to hold the paths */
2216facf4a8dSllai1 	if (copyout(&lens, ulensp, sizeof (lens)))
2217facf4a8dSllai1 		ret = EFAULT;
2218facf4a8dSllai1 
2219facf4a8dSllai1 err:
2220facf4a8dSllai1 	if (dirlist)
2221facf4a8dSllai1 		sdev_modctl_readdir_free(dirlist, npaths, npaths_alloc);
2222facf4a8dSllai1 	if (paths)
2223facf4a8dSllai1 		kmem_free(paths, lens);
2224facf4a8dSllai1 	kmem_free(dir, udirlen + 1);
2225facf4a8dSllai1 	return (ret);
2226facf4a8dSllai1 }
2227facf4a8dSllai1 
2228e37c6c37Scth static int
2229e37c6c37Scth modctl_devemptydir(const char *udir, int udirlen, int *uempty)
2230e37c6c37Scth {
2231e37c6c37Scth 	char	*dir;
2232e37c6c37Scth 	int	ret;
2233e37c6c37Scth 	char	**dirlist = NULL;
2234e37c6c37Scth 	int	npaths;
2235e37c6c37Scth 	int	npaths_alloc;
2236e37c6c37Scth 	int	empty;
2237e37c6c37Scth 
2238e37c6c37Scth 	/*
2239e37c6c37Scth 	 * copyin the /dev path including terminating null
2240e37c6c37Scth 	 */
2241e37c6c37Scth 	udirlen++;
2242e37c6c37Scth 	if (udirlen <= 1 || udirlen > MAXPATHLEN)
2243e37c6c37Scth 		return (EINVAL);
2244e37c6c37Scth 	dir = kmem_zalloc(udirlen + 1, KM_SLEEP);
2245e37c6c37Scth 	if ((ret = copyinstr(udir, dir, udirlen, NULL)) != 0)
2246e37c6c37Scth 		goto err;
2247e37c6c37Scth 
2248e37c6c37Scth 	if ((ret = sdev_modctl_readdir(dir, &dirlist,
2249e37c6c37Scth 	    &npaths, &npaths_alloc, 1)) != 0) {
2250e37c6c37Scth 		goto err;
2251e37c6c37Scth 	}
2252e37c6c37Scth 
2253e37c6c37Scth 	empty = npaths ? 0 : 1;
2254e37c6c37Scth 	if (copyout(&empty, uempty, sizeof (empty)))
2255e37c6c37Scth 		ret = EFAULT;
2256e37c6c37Scth 
2257e37c6c37Scth err:
2258e37c6c37Scth 	if (dirlist)
2259e37c6c37Scth 		sdev_modctl_readdir_free(dirlist, npaths, npaths_alloc);
2260e37c6c37Scth 	kmem_free(dir, udirlen + 1);
2261e37c6c37Scth 	return (ret);
2262e37c6c37Scth }
2263e37c6c37Scth 
2264*26947304SEvan Yan static int
2265*26947304SEvan Yan modctl_hp(int subcmd, const char *path, char *cn_name, uintptr_t arg,
2266*26947304SEvan Yan     uintptr_t rval)
2267*26947304SEvan Yan {
2268*26947304SEvan Yan 	int error = 0;
2269*26947304SEvan Yan 	size_t pathsz, namesz;
2270*26947304SEvan Yan 	char *devpath, *cn_name_str;
2271*26947304SEvan Yan 
2272*26947304SEvan Yan 	if (path == NULL)
2273*26947304SEvan Yan 		return (EINVAL);
2274*26947304SEvan Yan 
2275*26947304SEvan Yan 	devpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
2276*26947304SEvan Yan 	error = copyinstr(path, devpath, MAXPATHLEN, &pathsz);
2277*26947304SEvan Yan 	if (error != 0) {
2278*26947304SEvan Yan 		kmem_free(devpath, MAXPATHLEN);
2279*26947304SEvan Yan 		return (EFAULT);
2280*26947304SEvan Yan 	}
2281*26947304SEvan Yan 
2282*26947304SEvan Yan 	cn_name_str = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
2283*26947304SEvan Yan 	error = copyinstr(cn_name, cn_name_str, MAXNAMELEN, &namesz);
2284*26947304SEvan Yan 	if (error != 0) {
2285*26947304SEvan Yan 		kmem_free(devpath, MAXPATHLEN);
2286*26947304SEvan Yan 		kmem_free(cn_name_str, MAXNAMELEN);
2287*26947304SEvan Yan 
2288*26947304SEvan Yan 		return (EFAULT);
2289*26947304SEvan Yan 	}
2290*26947304SEvan Yan 
2291*26947304SEvan Yan 	switch (subcmd) {
2292*26947304SEvan Yan 	case MODHPOPS_CHANGE_STATE:
2293*26947304SEvan Yan 		error = ddihp_modctl(DDI_HPOP_CN_CHANGE_STATE, devpath,
2294*26947304SEvan Yan 		    cn_name_str, arg, NULL);
2295*26947304SEvan Yan 		break;
2296*26947304SEvan Yan 	case MODHPOPS_CREATE_PORT:
2297*26947304SEvan Yan 		/* Create an empty PORT */
2298*26947304SEvan Yan 		error = ddihp_modctl(DDI_HPOP_CN_CREATE_PORT, devpath,
2299*26947304SEvan Yan 		    cn_name_str, NULL, NULL);
2300*26947304SEvan Yan 		break;
2301*26947304SEvan Yan 	case MODHPOPS_REMOVE_PORT:
2302*26947304SEvan Yan 		/* Remove an empty PORT */
2303*26947304SEvan Yan 		error = ddihp_modctl(DDI_HPOP_CN_REMOVE_PORT, devpath,
2304*26947304SEvan Yan 		    cn_name_str, NULL, NULL);
2305*26947304SEvan Yan 		break;
2306*26947304SEvan Yan 	case MODHPOPS_BUS_GET:
2307*26947304SEvan Yan 		error = ddihp_modctl(DDI_HPOP_CN_GET_PROPERTY, devpath,
2308*26947304SEvan Yan 		    cn_name_str, arg, rval);
2309*26947304SEvan Yan 		break;
2310*26947304SEvan Yan 	case MODHPOPS_BUS_SET:
2311*26947304SEvan Yan 		error = ddihp_modctl(DDI_HPOP_CN_SET_PROPERTY, devpath,
2312*26947304SEvan Yan 		    cn_name_str, arg, rval);
2313*26947304SEvan Yan 		break;
2314*26947304SEvan Yan 	default:
2315*26947304SEvan Yan 		error = ENOTSUP;
2316*26947304SEvan Yan 		break;
2317*26947304SEvan Yan 	}
2318*26947304SEvan Yan 
2319*26947304SEvan Yan 	kmem_free(devpath, MAXPATHLEN);
2320*26947304SEvan Yan 	kmem_free(cn_name_str, MAXNAMELEN);
2321*26947304SEvan Yan 
2322*26947304SEvan Yan 	return (error);
2323*26947304SEvan Yan }
2324*26947304SEvan Yan 
2325facf4a8dSllai1 int
2326facf4a8dSllai1 modctl_moddevname(int subcmd, uintptr_t a1, uintptr_t a2)
2327facf4a8dSllai1 {
2328facf4a8dSllai1 	int error = 0;
2329facf4a8dSllai1 
2330facf4a8dSllai1 	switch (subcmd) {
2331facf4a8dSllai1 	case MODDEVNAME_LOOKUPDOOR:
23323c5e027bSEric Taylor 		error = devname_filename_register((char *)a1);
2333facf4a8dSllai1 		break;
2334facf4a8dSllai1 	case MODDEVNAME_PROFILE:
2335facf4a8dSllai1 		error = devname_profile_update((char *)a1, (size_t)a2);
2336facf4a8dSllai1 		break;
2337facf4a8dSllai1 	case MODDEVNAME_RECONFIG:
2338facf4a8dSllai1 		i_ddi_set_reconfig();
2339facf4a8dSllai1 		break;
2340facf4a8dSllai1 	case MODDEVNAME_SYSAVAIL:
2341facf4a8dSllai1 		i_ddi_set_sysavail();
2342facf4a8dSllai1 		break;
2343facf4a8dSllai1 	default:
2344facf4a8dSllai1 		error = EINVAL;
2345facf4a8dSllai1 		break;
2346facf4a8dSllai1 	}
2347facf4a8dSllai1 
2348facf4a8dSllai1 	return (error);
2349facf4a8dSllai1 }
2350facf4a8dSllai1 
23517c478bd9Sstevel@tonic-gate /*ARGSUSED5*/
23527c478bd9Sstevel@tonic-gate int
23537c478bd9Sstevel@tonic-gate modctl(int cmd, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4,
23547c478bd9Sstevel@tonic-gate     uintptr_t a5)
23557c478bd9Sstevel@tonic-gate {
23567c478bd9Sstevel@tonic-gate 	int	error = EINVAL;
23577c478bd9Sstevel@tonic-gate 	dev_t	dev;
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	if (secpolicy_modctl(CRED(), cmd) != 0)
23607c478bd9Sstevel@tonic-gate 		return (set_errno(EPERM));
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	switch (cmd) {
23637c478bd9Sstevel@tonic-gate 	case MODLOAD:		/* load a module */
23647c478bd9Sstevel@tonic-gate 		error = modctl_modload((int)a1, (char *)a2, (int *)a3);
23657c478bd9Sstevel@tonic-gate 		break;
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	case MODUNLOAD:		/* unload a module */
23687c478bd9Sstevel@tonic-gate 		error = modctl_modunload((modid_t)a1);
23697c478bd9Sstevel@tonic-gate 		break;
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	case MODINFO:		/* get module status */
23727c478bd9Sstevel@tonic-gate 		error = modctl_modinfo((modid_t)a1, (struct modinfo *)a2);
23737c478bd9Sstevel@tonic-gate 		break;
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	case MODRESERVED:	/* get last major number in range */
23767c478bd9Sstevel@tonic-gate 		error = modctl_modreserve((modid_t)a1, (int *)a2);
23777c478bd9Sstevel@tonic-gate 		break;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	case MODSETMINIROOT:	/* we are running in miniroot */
23807c478bd9Sstevel@tonic-gate 		isminiroot = 1;
23817c478bd9Sstevel@tonic-gate 		error = 0;
23827c478bd9Sstevel@tonic-gate 		break;
23837c478bd9Sstevel@tonic-gate 
23846532b960SJerry Gilliam 	case MODADDMAJBIND:	/* add major / driver alias bindings */
23856532b960SJerry Gilliam 		error = modctl_add_driver_aliases((int *)a2);
23867c478bd9Sstevel@tonic-gate 		break;
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 	case MODGETPATHLEN:	/* get modpath length */
23897c478bd9Sstevel@tonic-gate 		error = modctl_getmodpathlen((int *)a2);
23907c478bd9Sstevel@tonic-gate 		break;
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 	case MODGETPATH:	/* get modpath */
23937c478bd9Sstevel@tonic-gate 		error = modctl_getmodpath((char *)a2);
23947c478bd9Sstevel@tonic-gate 		break;
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	case MODREADSYSBIND:	/* read system call binding file */
23977c478bd9Sstevel@tonic-gate 		error = modctl_read_sysbinding_file();
23987c478bd9Sstevel@tonic-gate 		break;
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	case MODGETMAJBIND:	/* get major number for named device */
24017c478bd9Sstevel@tonic-gate 		error = modctl_getmaj((char *)a1, (uint_t)a2, (int *)a3);
24027c478bd9Sstevel@tonic-gate 		break;
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	case MODGETNAME:	/* get name of device given major number */
24057c478bd9Sstevel@tonic-gate 		error = modctl_getname((char *)a1, (uint_t)a2, (int *)a3);
24067c478bd9Sstevel@tonic-gate 		break;
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	case MODDEVT2INSTANCE:
24097c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
24107c478bd9Sstevel@tonic-gate 			dev = (dev_t)a1;
24117c478bd9Sstevel@tonic-gate 		}
24127c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24137c478bd9Sstevel@tonic-gate 		else {
24147c478bd9Sstevel@tonic-gate 			dev = expldev(a1);
24157c478bd9Sstevel@tonic-gate 		}
24167c478bd9Sstevel@tonic-gate #endif
24177c478bd9Sstevel@tonic-gate 		error = modctl_devt2instance(dev, (int *)a2);
24187c478bd9Sstevel@tonic-gate 		break;
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 	case MODSIZEOF_DEVID:	/* sizeof device id of device given dev_t */
24217c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
24227c478bd9Sstevel@tonic-gate 			dev = (dev_t)a1;
24237c478bd9Sstevel@tonic-gate 		}
24247c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24257c478bd9Sstevel@tonic-gate 		else {
24267c478bd9Sstevel@tonic-gate 			dev = expldev(a1);
24277c478bd9Sstevel@tonic-gate 		}
24287c478bd9Sstevel@tonic-gate #endif
24297c478bd9Sstevel@tonic-gate 		error = modctl_sizeof_devid(dev, (uint_t *)a2);
24307c478bd9Sstevel@tonic-gate 		break;
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 	case MODGETDEVID:	/* get device id of device given dev_t */
24337c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
24347c478bd9Sstevel@tonic-gate 			dev = (dev_t)a1;
24357c478bd9Sstevel@tonic-gate 		}
24367c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24377c478bd9Sstevel@tonic-gate 		else {
24387c478bd9Sstevel@tonic-gate 			dev = expldev(a1);
24397c478bd9Sstevel@tonic-gate 		}
24407c478bd9Sstevel@tonic-gate #endif
24417c478bd9Sstevel@tonic-gate 		error = modctl_get_devid(dev, (uint_t)a2, (ddi_devid_t)a3);
24427c478bd9Sstevel@tonic-gate 		break;
24437c478bd9Sstevel@tonic-gate 
2444a08731ecScth 	case MODSIZEOF_MINORNAME:	/* sizeof minor nm (dev_t,spectype) */
24457c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
24467c478bd9Sstevel@tonic-gate 			error = modctl_sizeof_minorname((dev_t)a1, (int)a2,
24477c478bd9Sstevel@tonic-gate 			    (uint_t *)a3);
24487c478bd9Sstevel@tonic-gate 		}
24497c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24507c478bd9Sstevel@tonic-gate 		else {
24517c478bd9Sstevel@tonic-gate 			error = modctl_sizeof_minorname(expldev(a1), (int)a2,
24527c478bd9Sstevel@tonic-gate 			    (uint_t *)a3);
24537c478bd9Sstevel@tonic-gate 		}
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate #endif
24567c478bd9Sstevel@tonic-gate 		break;
24577c478bd9Sstevel@tonic-gate 
2458a08731ecScth 	case MODGETMINORNAME:		/* get minor name of (dev_t,spectype) */
24597c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
24607c478bd9Sstevel@tonic-gate 			error = modctl_get_minorname((dev_t)a1, (int)a2,
24617c478bd9Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
24627c478bd9Sstevel@tonic-gate 		}
24637c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24647c478bd9Sstevel@tonic-gate 		else {
24657c478bd9Sstevel@tonic-gate 			error = modctl_get_minorname(expldev(a1), (int)a2,
24667c478bd9Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
24677c478bd9Sstevel@tonic-gate 		}
24687c478bd9Sstevel@tonic-gate #endif
24697c478bd9Sstevel@tonic-gate 		break;
24707c478bd9Sstevel@tonic-gate 
2471a08731ecScth 	case MODGETDEVFSPATH_LEN:	/* sizeof path nm of (dev_t,spectype) */
24727c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
24737c478bd9Sstevel@tonic-gate 			error = modctl_devfspath_len((dev_t)a1, (int)a2,
24747c478bd9Sstevel@tonic-gate 			    (uint_t *)a3);
24757c478bd9Sstevel@tonic-gate 		}
24767c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24777c478bd9Sstevel@tonic-gate 		else {
24787c478bd9Sstevel@tonic-gate 			error = modctl_devfspath_len(expldev(a1), (int)a2,
24797c478bd9Sstevel@tonic-gate 			    (uint_t *)a3);
24807c478bd9Sstevel@tonic-gate 		}
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate #endif
24837c478bd9Sstevel@tonic-gate 		break;
24847c478bd9Sstevel@tonic-gate 
2485a08731ecScth 	case MODGETDEVFSPATH:		/* get path name of (dev_t,spec) type */
24867c478bd9Sstevel@tonic-gate 		if (get_udatamodel() == DATAMODEL_NATIVE) {
24877c478bd9Sstevel@tonic-gate 			error = modctl_devfspath((dev_t)a1, (int)a2,
24887c478bd9Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
24897c478bd9Sstevel@tonic-gate 		}
24907c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
24917c478bd9Sstevel@tonic-gate 		else {
24927c478bd9Sstevel@tonic-gate 			error = modctl_devfspath(expldev(a1), (int)a2,
24937c478bd9Sstevel@tonic-gate 			    (uint_t)a3, (char *)a4);
24947c478bd9Sstevel@tonic-gate 		}
24957c478bd9Sstevel@tonic-gate #endif
24967c478bd9Sstevel@tonic-gate 		break;
24977c478bd9Sstevel@tonic-gate 
2498a08731ecScth 	case MODGETDEVFSPATH_MI_LEN:	/* sizeof path nm of (major,instance) */
2499a08731ecScth 		error = modctl_devfspath_mi_len((major_t)a1, (int)a2,
2500a08731ecScth 		    (uint_t *)a3);
2501a08731ecScth 		break;
2502a08731ecScth 
2503a08731ecScth 	case MODGETDEVFSPATH_MI:	/* get path name of (major,instance) */
2504a08731ecScth 		error = modctl_devfspath_mi((major_t)a1, (int)a2,
2505a08731ecScth 		    (uint_t)a3, (char *)a4);
2506a08731ecScth 		break;
2507a08731ecScth 
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 	case MODEVENTS:
25107c478bd9Sstevel@tonic-gate 		error = modctl_modevents((int)a1, a2, a3, a4, (uint_t)a5);
25117c478bd9Sstevel@tonic-gate 		break;
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate 	case MODGETFBNAME:	/* get the framebuffer name */
25147c478bd9Sstevel@tonic-gate 		error = modctl_get_fbname((char *)a1);
25157c478bd9Sstevel@tonic-gate 		break;
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	case MODREREADDACF:	/* reread dacf rule database from given file */
25187c478bd9Sstevel@tonic-gate 		error = modctl_reread_dacf((char *)a1);
25197c478bd9Sstevel@tonic-gate 		break;
25207c478bd9Sstevel@tonic-gate 
25217c478bd9Sstevel@tonic-gate 	case MODLOADDRVCONF:	/* load driver.conf file for major */
2522c9cc1492SJerry Gilliam 		error = modctl_load_drvconf((major_t)a1, (int)a2);
25237c478bd9Sstevel@tonic-gate 		break;
25247c478bd9Sstevel@tonic-gate 
25257c478bd9Sstevel@tonic-gate 	case MODUNLOADDRVCONF:	/* unload driver.conf file for major */
25267c478bd9Sstevel@tonic-gate 		error = modctl_unload_drvconf((major_t)a1);
25277c478bd9Sstevel@tonic-gate 		break;
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	case MODREMMAJBIND:	/* remove a major binding */
25307c478bd9Sstevel@tonic-gate 		error = modctl_rem_major((major_t)a1);
25317c478bd9Sstevel@tonic-gate 		break;
25327c478bd9Sstevel@tonic-gate 
25336532b960SJerry Gilliam 	case MODREMDRVALIAS:	/* remove a major/alias binding */
25346532b960SJerry Gilliam 		error = modctl_remove_driver_aliases((int *)a2);
25356532b960SJerry Gilliam 		break;
25366532b960SJerry Gilliam 
25377c478bd9Sstevel@tonic-gate 	case MODDEVID2PATHS:	/* get paths given devid */
25387c478bd9Sstevel@tonic-gate 		error = modctl_devid2paths((ddi_devid_t)a1, (char *)a2,
25397c478bd9Sstevel@tonic-gate 		    (uint_t)a3, (size_t *)a4, (char *)a5);
25407c478bd9Sstevel@tonic-gate 		break;
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	case MODSETDEVPOLICY:	/* establish device policy */
25437c478bd9Sstevel@tonic-gate 		error = devpolicy_load((int)a1, (size_t)a2, (devplcysys_t *)a3);
25447c478bd9Sstevel@tonic-gate 		break;
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 	case MODGETDEVPOLICY:	/* get device policy */
25477c478bd9Sstevel@tonic-gate 		error = devpolicy_get((int *)a1, (size_t)a2,
25487c478bd9Sstevel@tonic-gate 		    (devplcysys_t *)a3);
25497c478bd9Sstevel@tonic-gate 		break;
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	case MODALLOCPRIV:
25527c478bd9Sstevel@tonic-gate 		error = modctl_allocpriv((const char *)a1);
25537c478bd9Sstevel@tonic-gate 		break;
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate 	case MODGETDEVPOLICYBYNAME:
25567c478bd9Sstevel@tonic-gate 		error = devpolicy_getbyname((size_t)a1,
25577c478bd9Sstevel@tonic-gate 		    (devplcysys_t *)a2, (char *)a3);
25587c478bd9Sstevel@tonic-gate 		break;
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate 	case MODLOADMINORPERM:
25617c478bd9Sstevel@tonic-gate 	case MODADDMINORPERM:
25627c478bd9Sstevel@tonic-gate 	case MODREMMINORPERM:
25637c478bd9Sstevel@tonic-gate 		error = modctl_minorperm(cmd, (char *)a1, (size_t)a2);
25647c478bd9Sstevel@tonic-gate 		break;
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate 	case MODREMDRVCLEANUP:
25677c478bd9Sstevel@tonic-gate 		error = modctl_remdrv_cleanup((const char *)a1);
25687c478bd9Sstevel@tonic-gate 		break;
25697c478bd9Sstevel@tonic-gate 
2570facf4a8dSllai1 	case MODDEVEXISTS:	/* non-reconfiguring /dev lookup */
2571facf4a8dSllai1 		error = modctl_devexists((const char *)a1, (size_t)a2);
2572facf4a8dSllai1 		break;
2573facf4a8dSllai1 
2574facf4a8dSllai1 	case MODDEVREADDIR:	/* non-reconfiguring /dev readdir */
2575facf4a8dSllai1 		error = modctl_devreaddir((const char *)a1, (size_t)a2,
2576facf4a8dSllai1 		    (char *)a3, (int64_t *)a4);
2577facf4a8dSllai1 		break;
2578facf4a8dSllai1 
2579e37c6c37Scth 	case MODDEVEMPTYDIR:	/* non-reconfiguring /dev emptydir */
2580e37c6c37Scth 		error = modctl_devemptydir((const char *)a1, (size_t)a2,
2581e37c6c37Scth 		    (int *)a3);
2582e37c6c37Scth 		break;
2583e37c6c37Scth 
2584facf4a8dSllai1 	case MODDEVNAME:
2585facf4a8dSllai1 		error = modctl_moddevname((int)a1, a2, a3);
2586facf4a8dSllai1 		break;
2587facf4a8dSllai1 
258825e8c5aaSvikram 	case MODRETIRE:	/* retire device named by physpath a1 */
258925e8c5aaSvikram 		error = modctl_retire((char *)a1, (char *)a2, (size_t)a3);
259025e8c5aaSvikram 		break;
259125e8c5aaSvikram 
259225e8c5aaSvikram 	case MODISRETIRED:  /* check if a device is retired. */
259325e8c5aaSvikram 		error = modctl_is_retired((char *)a1, (int *)a2);
259425e8c5aaSvikram 		break;
259525e8c5aaSvikram 
259625e8c5aaSvikram 	case MODUNRETIRE:	/* unretire device named by physpath a1 */
259725e8c5aaSvikram 		error = modctl_unretire((char *)a1);
259825e8c5aaSvikram 		break;
259925e8c5aaSvikram 
2600*26947304SEvan Yan 	case MODHPOPS:	/* hotplug operations */
2601*26947304SEvan Yan 		/* device named by physpath a2 and Connection name a3 */
2602*26947304SEvan Yan 		error = modctl_hp((int)a1, (char *)a2, (char *)a3, a4, a5);
2603*26947304SEvan Yan 		break;
2604*26947304SEvan Yan 
26057c478bd9Sstevel@tonic-gate 	default:
26067c478bd9Sstevel@tonic-gate 		error = EINVAL;
26077c478bd9Sstevel@tonic-gate 		break;
26087c478bd9Sstevel@tonic-gate 	}
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 	return (error ? set_errno(error) : 0);
26117c478bd9Sstevel@tonic-gate }
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate /*
26147c478bd9Sstevel@tonic-gate  * Calls to kobj_load_module()() are handled off to this routine in a
26157c478bd9Sstevel@tonic-gate  * separate thread.
26167c478bd9Sstevel@tonic-gate  */
26177c478bd9Sstevel@tonic-gate static void
26187c478bd9Sstevel@tonic-gate modload_thread(struct loadmt *ltp)
26197c478bd9Sstevel@tonic-gate {
26207c478bd9Sstevel@tonic-gate 	/* load the module and signal the creator of this thread */
26217c478bd9Sstevel@tonic-gate 	kmutex_t	cpr_lk;
26227c478bd9Sstevel@tonic-gate 	callb_cpr_t	cpr_i;
26237c478bd9Sstevel@tonic-gate 
26247c478bd9Sstevel@tonic-gate 	mutex_init(&cpr_lk, NULL, MUTEX_DEFAULT, NULL);
26257c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cpr_i, &cpr_lk, callb_generic_cpr, "modload");
26267c478bd9Sstevel@tonic-gate 	/* borrow the devi lock from thread which invoked us */
26277c478bd9Sstevel@tonic-gate 	pm_borrow_lock(ltp->owner);
26287c478bd9Sstevel@tonic-gate 	ltp->retval = kobj_load_module(ltp->mp, ltp->usepath);
26297c478bd9Sstevel@tonic-gate 	pm_return_lock();
26307c478bd9Sstevel@tonic-gate 	sema_v(&ltp->sema);
26317c478bd9Sstevel@tonic-gate 	mutex_enter(&cpr_lk);
26327c478bd9Sstevel@tonic-gate 	CALLB_CPR_EXIT(&cpr_i);
26337c478bd9Sstevel@tonic-gate 	mutex_destroy(&cpr_lk);
26347c478bd9Sstevel@tonic-gate 	thread_exit();
26357c478bd9Sstevel@tonic-gate }
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate /*
26387c478bd9Sstevel@tonic-gate  * load a module, adding a reference if caller specifies rmodp.  If rmodp
26397c478bd9Sstevel@tonic-gate  * is specified then an errno is returned, otherwise a module index is
26407c478bd9Sstevel@tonic-gate  * returned (-1 on error).
26417c478bd9Sstevel@tonic-gate  */
26427c478bd9Sstevel@tonic-gate static int
264343d5cd3dSjohnlev modrload(const char *subdir, const char *filename, struct modctl **rmodp)
26447c478bd9Sstevel@tonic-gate {
26457c478bd9Sstevel@tonic-gate 	struct modctl *modp;
26467c478bd9Sstevel@tonic-gate 	size_t size;
26477c478bd9Sstevel@tonic-gate 	char *fullname;
26487c478bd9Sstevel@tonic-gate 	int retval = EINVAL;
26497c478bd9Sstevel@tonic-gate 	int id = -1;
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	if (rmodp)
26527c478bd9Sstevel@tonic-gate 		*rmodp = NULL;			/* avoid garbage */
26537c478bd9Sstevel@tonic-gate 
26547c478bd9Sstevel@tonic-gate 	if (subdir != NULL) {
26557c478bd9Sstevel@tonic-gate 		/*
26567c478bd9Sstevel@tonic-gate 		 * refuse / in filename to prevent "../" escapes.
26577c478bd9Sstevel@tonic-gate 		 */
26587c478bd9Sstevel@tonic-gate 		if (strchr(filename, '/') != NULL)
26597c478bd9Sstevel@tonic-gate 			return (rmodp ? retval : id);
26607c478bd9Sstevel@tonic-gate 
26617c478bd9Sstevel@tonic-gate 		/*
26627c478bd9Sstevel@tonic-gate 		 * allocate enough space for <subdir>/<filename><NULL>
26637c478bd9Sstevel@tonic-gate 		 */
26647c478bd9Sstevel@tonic-gate 		size = strlen(subdir) + strlen(filename) + 2;
26657c478bd9Sstevel@tonic-gate 		fullname = kmem_zalloc(size, KM_SLEEP);
26667c478bd9Sstevel@tonic-gate 		(void) sprintf(fullname, "%s/%s", subdir, filename);
26677c478bd9Sstevel@tonic-gate 	} else {
266843d5cd3dSjohnlev 		fullname = (char *)filename;
26697c478bd9Sstevel@tonic-gate 	}
26707c478bd9Sstevel@tonic-gate 
267120c794b3Sgavinm 	modp = mod_hold_installed_mod(fullname, 1, 0, &retval);
26727c478bd9Sstevel@tonic-gate 	if (modp != NULL) {
26737c478bd9Sstevel@tonic-gate 		id = modp->mod_id;
26747c478bd9Sstevel@tonic-gate 		if (rmodp) {
26757c478bd9Sstevel@tonic-gate 			/* add mod_ref and return *rmodp */
26767c478bd9Sstevel@tonic-gate 			mutex_enter(&mod_lock);
26777c478bd9Sstevel@tonic-gate 			modp->mod_ref++;
26787c478bd9Sstevel@tonic-gate 			mutex_exit(&mod_lock);
26797c478bd9Sstevel@tonic-gate 			*rmodp = modp;
26807c478bd9Sstevel@tonic-gate 		}
26817c478bd9Sstevel@tonic-gate 		mod_release_mod(modp);
26827c478bd9Sstevel@tonic-gate 		CPU_STATS_ADDQ(CPU, sys, modload, 1);
26837c478bd9Sstevel@tonic-gate 	}
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate done:	if (subdir != NULL)
26867c478bd9Sstevel@tonic-gate 		kmem_free(fullname, size);
26877c478bd9Sstevel@tonic-gate 	return (rmodp ? retval : id);
26887c478bd9Sstevel@tonic-gate }
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate /*
26917c478bd9Sstevel@tonic-gate  * This is the primary kernel interface to load a module. It loads and
26927c478bd9Sstevel@tonic-gate  * installs the named module.  It does not hold mod_ref of the module, so
26937c478bd9Sstevel@tonic-gate  * a module unload attempt can occur at any time - it is up to the
26947c478bd9Sstevel@tonic-gate  * _fini/mod_remove implementation to determine if unload will succeed.
26957c478bd9Sstevel@tonic-gate  */
26967c478bd9Sstevel@tonic-gate int
269743d5cd3dSjohnlev modload(const char *subdir, const char *filename)
26987c478bd9Sstevel@tonic-gate {
26997c478bd9Sstevel@tonic-gate 	return (modrload(subdir, filename, NULL));
27007c478bd9Sstevel@tonic-gate }
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate /*
27037aec1d6eScindi  * Load a module using a series of qualified names from most specific to least
27047aec1d6eScindi  * specific, e.g. for subdir "foo", p1 "bar", p2 "baz", we might try:
270520c794b3Sgavinm  *			Value returned in *chosen
270620c794b3Sgavinm  * foo/bar.baz.1.2.3	3
270720c794b3Sgavinm  * foo/bar.baz.1.2	2
270820c794b3Sgavinm  * foo/bar.baz.1	1
270920c794b3Sgavinm  * foo/bar.baz		0
27107aec1d6eScindi  *
271120c794b3Sgavinm  * Return the module ID on success; -1 if no module was loaded.  On success
271220c794b3Sgavinm  * and if 'chosen' is not NULL we also return the number of suffices that
271320c794b3Sgavinm  * were in the module we chose to load.
27147aec1d6eScindi  */
27157aec1d6eScindi int
27167aec1d6eScindi modload_qualified(const char *subdir, const char *p1,
271720c794b3Sgavinm     const char *p2, const char *delim, uint_t suffv[], int suffc, int *chosen)
27187aec1d6eScindi {
27197aec1d6eScindi 	char path[MOD_MAXPATH];
27207aec1d6eScindi 	size_t n, resid = sizeof (path);
27217aec1d6eScindi 	char *p = path;
27227aec1d6eScindi 
27237aec1d6eScindi 	char **dotv;
27247aec1d6eScindi 	int i, rc, id;
27257aec1d6eScindi 	modctl_t *mp;
27267aec1d6eScindi 
27277aec1d6eScindi 	if (p2 != NULL)
27287aec1d6eScindi 		n = snprintf(p, resid, "%s/%s%s%s", subdir, p1, delim, p2);
27297aec1d6eScindi 	else
27307aec1d6eScindi 		n = snprintf(p, resid, "%s/%s", subdir, p1);
27317aec1d6eScindi 
27327aec1d6eScindi 	if (n >= resid)
27337aec1d6eScindi 		return (-1);
27347aec1d6eScindi 
27357aec1d6eScindi 	p += n;
27367aec1d6eScindi 	resid -= n;
27377aec1d6eScindi 	dotv = kmem_alloc(sizeof (char *) * (suffc + 1), KM_SLEEP);
27387aec1d6eScindi 
27397aec1d6eScindi 	for (i = 0; i < suffc; i++) {
27407aec1d6eScindi 		dotv[i] = p;
27417aec1d6eScindi 		n = snprintf(p, resid, "%s%u", delim, suffv[i]);
27427aec1d6eScindi 
27437aec1d6eScindi 		if (n >= resid) {
27447aec1d6eScindi 			kmem_free(dotv, sizeof (char *) * (suffc + 1));
27457aec1d6eScindi 			return (-1);
27467aec1d6eScindi 		}
27477aec1d6eScindi 
27487aec1d6eScindi 		p += n;
27497aec1d6eScindi 		resid -= n;
27507aec1d6eScindi 	}
27517aec1d6eScindi 
27527aec1d6eScindi 	dotv[suffc] = p;
27537aec1d6eScindi 
27547aec1d6eScindi 	for (i = suffc; i >= 0; i--) {
27557aec1d6eScindi 		dotv[i][0] = '\0';
275620c794b3Sgavinm 		mp = mod_hold_installed_mod(path, 1, 1, &rc);
27577aec1d6eScindi 
27587aec1d6eScindi 		if (mp != NULL) {
27597aec1d6eScindi 			kmem_free(dotv, sizeof (char *) * (suffc + 1));
27607aec1d6eScindi 			id = mp->mod_id;
27617aec1d6eScindi 			mod_release_mod(mp);
276220c794b3Sgavinm 			if (chosen != NULL)
276320c794b3Sgavinm 				*chosen = i;
27647aec1d6eScindi 			return (id);
27657aec1d6eScindi 		}
27667aec1d6eScindi 	}
27677aec1d6eScindi 
27687aec1d6eScindi 	kmem_free(dotv, sizeof (char *) * (suffc + 1));
27697aec1d6eScindi 	return (-1);
27707aec1d6eScindi }
27717aec1d6eScindi 
27727aec1d6eScindi /*
27737c478bd9Sstevel@tonic-gate  * Load a module.
27747c478bd9Sstevel@tonic-gate  */
27757c478bd9Sstevel@tonic-gate int
277643d5cd3dSjohnlev modloadonly(const char *subdir, const char *filename)
27777c478bd9Sstevel@tonic-gate {
27787c478bd9Sstevel@tonic-gate 	struct modctl *modp;
27797c478bd9Sstevel@tonic-gate 	char *fullname;
27807c478bd9Sstevel@tonic-gate 	size_t size;
27817c478bd9Sstevel@tonic-gate 	int id, retval;
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 	if (subdir != NULL) {
27847c478bd9Sstevel@tonic-gate 		/*
27857c478bd9Sstevel@tonic-gate 		 * allocate enough space for <subdir>/<filename><NULL>
27867c478bd9Sstevel@tonic-gate 		 */
27877c478bd9Sstevel@tonic-gate 		size = strlen(subdir) + strlen(filename) + 2;
27887c478bd9Sstevel@tonic-gate 		fullname = kmem_zalloc(size, KM_SLEEP);
27897c478bd9Sstevel@tonic-gate 		(void) sprintf(fullname, "%s/%s", subdir, filename);
27907c478bd9Sstevel@tonic-gate 	} else {
279143d5cd3dSjohnlev 		fullname = (char *)filename;
27927c478bd9Sstevel@tonic-gate 	}
27937c478bd9Sstevel@tonic-gate 
27947c478bd9Sstevel@tonic-gate 	modp = mod_hold_loaded_mod(NULL, fullname, &retval);
27957c478bd9Sstevel@tonic-gate 	if (modp) {
27967c478bd9Sstevel@tonic-gate 		id = modp->mod_id;
27977c478bd9Sstevel@tonic-gate 		mod_release_mod(modp);
27987c478bd9Sstevel@tonic-gate 	}
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate 	if (subdir != NULL)
28017c478bd9Sstevel@tonic-gate 		kmem_free(fullname, size);
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 	if (retval == 0)
28047c478bd9Sstevel@tonic-gate 		return (id);
28057c478bd9Sstevel@tonic-gate 	return (-1);
28067c478bd9Sstevel@tonic-gate }
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate /*
28097c478bd9Sstevel@tonic-gate  * Try to uninstall and unload a module, removing a reference if caller
28107c478bd9Sstevel@tonic-gate  * specifies rmodp.
28117c478bd9Sstevel@tonic-gate  */
28127c478bd9Sstevel@tonic-gate static int
28137c478bd9Sstevel@tonic-gate modunrload(modid_t id, struct modctl **rmodp, int unload)
28147c478bd9Sstevel@tonic-gate {
28157c478bd9Sstevel@tonic-gate 	struct modctl	*modp;
28167c478bd9Sstevel@tonic-gate 	int		retval;
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 	if (rmodp)
28197c478bd9Sstevel@tonic-gate 		*rmodp = NULL;			/* avoid garbage */
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	if ((modp = mod_hold_by_id((modid_t)id)) == NULL)
28227c478bd9Sstevel@tonic-gate 		return (EINVAL);
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	if (rmodp) {
28257c478bd9Sstevel@tonic-gate 		mutex_enter(&mod_lock);
28267c478bd9Sstevel@tonic-gate 		modp->mod_ref--;
28277c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
28287c478bd9Sstevel@tonic-gate 		*rmodp = modp;
28297c478bd9Sstevel@tonic-gate 	}
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 	if (unload) {
28327c478bd9Sstevel@tonic-gate 		retval = moduninstall(modp);
28337c478bd9Sstevel@tonic-gate 		if (retval == 0) {
28347c478bd9Sstevel@tonic-gate 			mod_unload(modp);
28357c478bd9Sstevel@tonic-gate 			CPU_STATS_ADDQ(CPU, sys, modunload, 1);
28367c478bd9Sstevel@tonic-gate 		} else if (retval == EALREADY)
28377c478bd9Sstevel@tonic-gate 			retval = 0;	/* already unloaded, not an error */
28387c478bd9Sstevel@tonic-gate 	} else
28397c478bd9Sstevel@tonic-gate 		retval = 0;
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate 	mod_release_mod(modp);
28427c478bd9Sstevel@tonic-gate 	return (retval);
28437c478bd9Sstevel@tonic-gate }
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate /*
28467c478bd9Sstevel@tonic-gate  * Uninstall and unload a module.
28477c478bd9Sstevel@tonic-gate  */
28487c478bd9Sstevel@tonic-gate int
28497c478bd9Sstevel@tonic-gate modunload(modid_t id)
28507c478bd9Sstevel@tonic-gate {
2851a7aa4df7Scth 	int		retval;
2852a7aa4df7Scth 
2853a7aa4df7Scth 	/* synchronize with any active modunload_disable() */
2854a7aa4df7Scth 	modunload_begin();
2855a7aa4df7Scth 	if (ddi_root_node())
2856a7aa4df7Scth 		(void) devfs_clean(ddi_root_node(), NULL, 0);
2857a7aa4df7Scth 	retval = modunrload(id, NULL, 1);
2858a7aa4df7Scth 	modunload_end();
2859a7aa4df7Scth 	return (retval);
28607c478bd9Sstevel@tonic-gate }
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate /*
28637c478bd9Sstevel@tonic-gate  * Return status of a loaded module.
28647c478bd9Sstevel@tonic-gate  */
28657c478bd9Sstevel@tonic-gate static int
28667c478bd9Sstevel@tonic-gate modinfo(modid_t id, struct modinfo *modinfop)
28677c478bd9Sstevel@tonic-gate {
28687c478bd9Sstevel@tonic-gate 	struct modctl	*modp;
28697c478bd9Sstevel@tonic-gate 	modid_t		mid;
28707c478bd9Sstevel@tonic-gate 	int		i;
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	mid = modinfop->mi_id;
28737c478bd9Sstevel@tonic-gate 	if (modinfop->mi_info & MI_INFO_ALL) {
28747c478bd9Sstevel@tonic-gate 		while ((modp = mod_hold_next_by_id(mid++)) != NULL) {
28757c478bd9Sstevel@tonic-gate 			if ((modinfop->mi_info & MI_INFO_CNT) ||
28767c478bd9Sstevel@tonic-gate 			    modp->mod_installed)
28777c478bd9Sstevel@tonic-gate 				break;
28787c478bd9Sstevel@tonic-gate 			mod_release_mod(modp);
28797c478bd9Sstevel@tonic-gate 		}
28807c478bd9Sstevel@tonic-gate 		if (modp == NULL)
28817c478bd9Sstevel@tonic-gate 			return (EINVAL);
28827c478bd9Sstevel@tonic-gate 	} else {
28837c478bd9Sstevel@tonic-gate 		modp = mod_hold_by_id(id);
28847c478bd9Sstevel@tonic-gate 		if (modp == NULL)
28857c478bd9Sstevel@tonic-gate 			return (EINVAL);
28867c478bd9Sstevel@tonic-gate 		if (!(modinfop->mi_info & MI_INFO_CNT) &&
28877c478bd9Sstevel@tonic-gate 		    (modp->mod_installed == 0)) {
28887c478bd9Sstevel@tonic-gate 			mod_release_mod(modp);
28897c478bd9Sstevel@tonic-gate 			return (EINVAL);
28907c478bd9Sstevel@tonic-gate 		}
28917c478bd9Sstevel@tonic-gate 	}
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate 	modinfop->mi_rev = 0;
28947c478bd9Sstevel@tonic-gate 	modinfop->mi_state = 0;
28957c478bd9Sstevel@tonic-gate 	for (i = 0; i < MODMAXLINK; i++) {
28967c478bd9Sstevel@tonic-gate 		modinfop->mi_msinfo[i].msi_p0 = -1;
28977c478bd9Sstevel@tonic-gate 		modinfop->mi_msinfo[i].msi_linkinfo[0] = 0;
28987c478bd9Sstevel@tonic-gate 	}
28997c478bd9Sstevel@tonic-gate 	if (modp->mod_loaded) {
29007c478bd9Sstevel@tonic-gate 		modinfop->mi_state = MI_LOADED;
29017c478bd9Sstevel@tonic-gate 		kobj_getmodinfo(modp->mod_mp, modinfop);
29027c478bd9Sstevel@tonic-gate 	}
29037c478bd9Sstevel@tonic-gate 	if (modp->mod_installed) {
29047c478bd9Sstevel@tonic-gate 		modinfop->mi_state |= MI_INSTALLED;
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 		(void) mod_getinfo(modp, modinfop);
29077c478bd9Sstevel@tonic-gate 	}
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	modinfop->mi_id = modp->mod_id;
29107c478bd9Sstevel@tonic-gate 	modinfop->mi_loadcnt = modp->mod_loadcnt;
29117c478bd9Sstevel@tonic-gate 	(void) strcpy(modinfop->mi_name, modp->mod_modname);
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	mod_release_mod(modp);
29147c478bd9Sstevel@tonic-gate 	return (0);
29157c478bd9Sstevel@tonic-gate }
29167c478bd9Sstevel@tonic-gate 
29177c478bd9Sstevel@tonic-gate static char mod_stub_err[] = "mod_hold_stub: Couldn't load stub module %s";
29187c478bd9Sstevel@tonic-gate static char no_err[] = "No error function for weak stub %s";
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate /*
29217c478bd9Sstevel@tonic-gate  * used by the stubs themselves to load and hold a module.
29227c478bd9Sstevel@tonic-gate  * Returns  0 if the module is successfully held;
29237c478bd9Sstevel@tonic-gate  *	    the stub needs to call mod_release_stub().
29247c478bd9Sstevel@tonic-gate  *	    -1 if the stub should just call the err_fcn.
29257c478bd9Sstevel@tonic-gate  * Note that this code is stretched out so that we avoid subroutine calls
29267c478bd9Sstevel@tonic-gate  * and optimize for the most likely case.  That is, the case where the
29277c478bd9Sstevel@tonic-gate  * module is loaded and installed and not held.  In that case we just inc
29287c478bd9Sstevel@tonic-gate  * the mod_ref count and continue.
29297c478bd9Sstevel@tonic-gate  */
29307c478bd9Sstevel@tonic-gate int
29317c478bd9Sstevel@tonic-gate mod_hold_stub(struct mod_stub_info *stub)
29327c478bd9Sstevel@tonic-gate {
29337c478bd9Sstevel@tonic-gate 	struct modctl *mp;
29347c478bd9Sstevel@tonic-gate 	struct mod_modinfo *mip;
29357c478bd9Sstevel@tonic-gate 
29367c478bd9Sstevel@tonic-gate 	mip = stub->mods_modinfo;
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	/* we do mod_hold_by_modctl inline for speed */
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate mod_check_again:
29437c478bd9Sstevel@tonic-gate 	if ((mp = mip->mp) != NULL) {
29447c478bd9Sstevel@tonic-gate 		if (mp->mod_busy == 0) {
29457c478bd9Sstevel@tonic-gate 			if (mp->mod_installed) {
29467c478bd9Sstevel@tonic-gate 				/* increment the reference count */
29477c478bd9Sstevel@tonic-gate 				mp->mod_ref++;
29487c478bd9Sstevel@tonic-gate 				ASSERT(mp->mod_ref && mp->mod_installed);
29497c478bd9Sstevel@tonic-gate 				mutex_exit(&mod_lock);
29507c478bd9Sstevel@tonic-gate 				return (0);
29517c478bd9Sstevel@tonic-gate 			} else {
29527c478bd9Sstevel@tonic-gate 				mp->mod_busy = 1;
29537c478bd9Sstevel@tonic-gate 				mp->mod_inprogress_thread =
29547c478bd9Sstevel@tonic-gate 				    (curthread == NULL ?
29557c478bd9Sstevel@tonic-gate 				    (kthread_id_t)-1 : curthread);
29567c478bd9Sstevel@tonic-gate 			}
29577c478bd9Sstevel@tonic-gate 		} else {
29587c478bd9Sstevel@tonic-gate 			/*
29597c478bd9Sstevel@tonic-gate 			 * wait one time and then go see if someone
29607c478bd9Sstevel@tonic-gate 			 * else has resolved the stub (set mip->mp).
29617c478bd9Sstevel@tonic-gate 			 */
29627c478bd9Sstevel@tonic-gate 			if (mod_hold_by_modctl(mp,
29637c478bd9Sstevel@tonic-gate 			    MOD_WAIT_ONCE | MOD_LOCK_HELD))
29647c478bd9Sstevel@tonic-gate 				goto mod_check_again;
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate 			/*
29677c478bd9Sstevel@tonic-gate 			 * what we have now may have been unloaded!, in
29687c478bd9Sstevel@tonic-gate 			 * that case, mip->mp will be NULL, we'll hit this
29697c478bd9Sstevel@tonic-gate 			 * module and load again..
29707c478bd9Sstevel@tonic-gate 			 */
29717c478bd9Sstevel@tonic-gate 			cmn_err(CE_PANIC, "mod_hold_stub should have blocked");
29727c478bd9Sstevel@tonic-gate 		}
29737c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
29747c478bd9Sstevel@tonic-gate 	} else {
29757c478bd9Sstevel@tonic-gate 		/* first time we've hit this module */
29767c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
29777c478bd9Sstevel@tonic-gate 		mp = mod_hold_by_name(mip->modm_module_name);
29787c478bd9Sstevel@tonic-gate 		mip->mp = mp;
29797c478bd9Sstevel@tonic-gate 	}
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	/*
29827c478bd9Sstevel@tonic-gate 	 * If we are here, it means that the following conditions
29837c478bd9Sstevel@tonic-gate 	 * are satisfied.
29847c478bd9Sstevel@tonic-gate 	 *
29857c478bd9Sstevel@tonic-gate 	 * mip->mp != NULL
29867c478bd9Sstevel@tonic-gate 	 * this thread has set the mp->mod_busy = 1
29877c478bd9Sstevel@tonic-gate 	 * mp->mod_installed = 0
29887c478bd9Sstevel@tonic-gate 	 *
29897c478bd9Sstevel@tonic-gate 	 */
29907c478bd9Sstevel@tonic-gate 	ASSERT(mp != NULL);
29917c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy == 1);
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 	if (mp->mod_installed == 0) {
29947c478bd9Sstevel@tonic-gate 		/* Module not loaded, if weak stub don't load it */
29957c478bd9Sstevel@tonic-gate 		if (stub->mods_flag & MODS_WEAK) {
29967c478bd9Sstevel@tonic-gate 			if (stub->mods_errfcn == NULL) {
29977c478bd9Sstevel@tonic-gate 				mod_release_mod(mp);
29987c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC, no_err,
29997c478bd9Sstevel@tonic-gate 				    mip->modm_module_name);
30007c478bd9Sstevel@tonic-gate 			}
30017c478bd9Sstevel@tonic-gate 		} else {
30027c478bd9Sstevel@tonic-gate 			/* Not a weak stub so load the module */
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate 			if (mod_load(mp, 1) != 0 || modinstall(mp) != 0) {
30057c478bd9Sstevel@tonic-gate 				/*
30067c478bd9Sstevel@tonic-gate 				 * If mod_load() was successful
30077c478bd9Sstevel@tonic-gate 				 * and modinstall() failed, then
30087c478bd9Sstevel@tonic-gate 				 * unload the module.
30097c478bd9Sstevel@tonic-gate 				 */
30107c478bd9Sstevel@tonic-gate 				if (mp->mod_loaded)
30117c478bd9Sstevel@tonic-gate 					mod_unload(mp);
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate 				mod_release_mod(mp);
30147c478bd9Sstevel@tonic-gate 				if (stub->mods_errfcn == NULL) {
30157c478bd9Sstevel@tonic-gate 					cmn_err(CE_PANIC, mod_stub_err,
30167c478bd9Sstevel@tonic-gate 					    mip->modm_module_name);
30177c478bd9Sstevel@tonic-gate 				} else {
30187c478bd9Sstevel@tonic-gate 					return (-1);
30197c478bd9Sstevel@tonic-gate 				}
30207c478bd9Sstevel@tonic-gate 			}
30217c478bd9Sstevel@tonic-gate 		}
30227c478bd9Sstevel@tonic-gate 	}
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 	/*
30257c478bd9Sstevel@tonic-gate 	 * At this point module is held and loaded. Release
30267c478bd9Sstevel@tonic-gate 	 * the mod_busy and mod_inprogress_thread before
30277c478bd9Sstevel@tonic-gate 	 * returning. We actually call mod_release() here so
30287c478bd9Sstevel@tonic-gate 	 * that if another stub wants to access this module,
30297c478bd9Sstevel@tonic-gate 	 * it can do so. mod_ref is incremented before mod_release()
30307c478bd9Sstevel@tonic-gate 	 * is called to prevent someone else from snatching the
30317c478bd9Sstevel@tonic-gate 	 * module from this thread.
30327c478bd9Sstevel@tonic-gate 	 */
30337c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
30347c478bd9Sstevel@tonic-gate 	mp->mod_ref++;
30357c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_ref &&
30367c478bd9Sstevel@tonic-gate 	    (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
30377c478bd9Sstevel@tonic-gate 	mod_release(mp);
30387c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
30397c478bd9Sstevel@tonic-gate 	return (0);
30407c478bd9Sstevel@tonic-gate }
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate void
30437c478bd9Sstevel@tonic-gate mod_release_stub(struct mod_stub_info *stub)
30447c478bd9Sstevel@tonic-gate {
30457c478bd9Sstevel@tonic-gate 	struct modctl *mp = stub->mods_modinfo->mp;
30467c478bd9Sstevel@tonic-gate 
30477c478bd9Sstevel@tonic-gate 	/* inline mod_release_mod */
30487c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
30497c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_ref &&
30507c478bd9Sstevel@tonic-gate 	    (mp->mod_loaded || (stub->mods_flag & MODS_WEAK)));
30517c478bd9Sstevel@tonic-gate 	mp->mod_ref--;
30527c478bd9Sstevel@tonic-gate 	if (mp->mod_want) {
30537c478bd9Sstevel@tonic-gate 		mp->mod_want = 0;
30547c478bd9Sstevel@tonic-gate 		cv_broadcast(&mod_cv);
30557c478bd9Sstevel@tonic-gate 	}
30567c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
30577c478bd9Sstevel@tonic-gate }
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate static struct modctl *
30607c478bd9Sstevel@tonic-gate mod_hold_loaded_mod(struct modctl *dep, char *filename, int *status)
30617c478bd9Sstevel@tonic-gate {
30627c478bd9Sstevel@tonic-gate 	struct modctl *modp;
30637c478bd9Sstevel@tonic-gate 	int retval;
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	/*
30667c478bd9Sstevel@tonic-gate 	 * Hold the module.
30677c478bd9Sstevel@tonic-gate 	 */
30687c478bd9Sstevel@tonic-gate 	modp = mod_hold_by_name_requisite(dep, filename);
30697c478bd9Sstevel@tonic-gate 	if (modp) {
30707c478bd9Sstevel@tonic-gate 		retval = mod_load(modp, 1);
30717c478bd9Sstevel@tonic-gate 		if (retval != 0) {
30727c478bd9Sstevel@tonic-gate 			mod_release_mod(modp);
30737c478bd9Sstevel@tonic-gate 			modp = NULL;
30747c478bd9Sstevel@tonic-gate 		}
30757c478bd9Sstevel@tonic-gate 		*status = retval;
30767c478bd9Sstevel@tonic-gate 	} else {
30777c478bd9Sstevel@tonic-gate 		*status = ENOSPC;
30787c478bd9Sstevel@tonic-gate 	}
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate 	/*
30817c478bd9Sstevel@tonic-gate 	 * if dep is not NULL, clear the module dependency information.
30827c478bd9Sstevel@tonic-gate 	 * This information is set in mod_hold_by_name_common().
30837c478bd9Sstevel@tonic-gate 	 */
30847c478bd9Sstevel@tonic-gate 	if (dep != NULL && dep->mod_requisite_loading != NULL) {
30857c478bd9Sstevel@tonic-gate 		ASSERT(dep->mod_busy);
30867c478bd9Sstevel@tonic-gate 		dep->mod_requisite_loading = NULL;
30877c478bd9Sstevel@tonic-gate 	}
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 	return (modp);
30907c478bd9Sstevel@tonic-gate }
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate /*
30937c478bd9Sstevel@tonic-gate  * hold, load, and install the named module
30947c478bd9Sstevel@tonic-gate  */
30957c478bd9Sstevel@tonic-gate static struct modctl *
309620c794b3Sgavinm mod_hold_installed_mod(char *name, int usepath, int forcecheck, int *r)
30977c478bd9Sstevel@tonic-gate {
30987c478bd9Sstevel@tonic-gate 	struct modctl *modp;
30997c478bd9Sstevel@tonic-gate 	int retval;
31002bac1547Scth 
31012bac1547Scth 	/*
31022bac1547Scth 	 * Verify that that module in question actually exists on disk
31032bac1547Scth 	 * before allocation of module structure by mod_hold_by_name.
31042bac1547Scth 	 */
310520c794b3Sgavinm 	if (modrootloaded && swaploaded || forcecheck) {
31065c311300Scth 		if (!kobj_path_exists(name, usepath)) {
31072bac1547Scth 			*r = ENOENT;
31082bac1547Scth 			return (NULL);
31092bac1547Scth 		}
31102bac1547Scth 	}
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate 	/*
31137c478bd9Sstevel@tonic-gate 	 * Hold the module.
31147c478bd9Sstevel@tonic-gate 	 */
31157c478bd9Sstevel@tonic-gate 	modp = mod_hold_by_name(name);
31167c478bd9Sstevel@tonic-gate 	if (modp) {
31177c478bd9Sstevel@tonic-gate 		retval = mod_load(modp, usepath);
31187c478bd9Sstevel@tonic-gate 		if (retval != 0) {
31197c478bd9Sstevel@tonic-gate 			mod_release_mod(modp);
31207c478bd9Sstevel@tonic-gate 			modp = NULL;
31217c478bd9Sstevel@tonic-gate 			*r = retval;
31227c478bd9Sstevel@tonic-gate 		} else {
31237c478bd9Sstevel@tonic-gate 			if ((*r = modinstall(modp)) != 0) {
31247c478bd9Sstevel@tonic-gate 				/*
31257c478bd9Sstevel@tonic-gate 				 * We loaded it, but failed to _init() it.
31267c478bd9Sstevel@tonic-gate 				 * Be kind to developers -- force it
31277c478bd9Sstevel@tonic-gate 				 * out of memory now so that the next
31287c478bd9Sstevel@tonic-gate 				 * attempt to use the module will cause
31297c478bd9Sstevel@tonic-gate 				 * a reload.  See 1093793.
31307c478bd9Sstevel@tonic-gate 				 */
31317c478bd9Sstevel@tonic-gate 				mod_unload(modp);
31327c478bd9Sstevel@tonic-gate 				mod_release_mod(modp);
31337c478bd9Sstevel@tonic-gate 				modp = NULL;
31347c478bd9Sstevel@tonic-gate 			}
31357c478bd9Sstevel@tonic-gate 		}
31367c478bd9Sstevel@tonic-gate 	} else {
31377c478bd9Sstevel@tonic-gate 		*r = ENOSPC;
31387c478bd9Sstevel@tonic-gate 	}
31397c478bd9Sstevel@tonic-gate 	return (modp);
31407c478bd9Sstevel@tonic-gate }
31417c478bd9Sstevel@tonic-gate 
31427c478bd9Sstevel@tonic-gate static char mod_excl_msg[] =
31437c478bd9Sstevel@tonic-gate 	"module %s(%s) is EXCLUDED and will not be loaded\n";
31447c478bd9Sstevel@tonic-gate static char mod_init_msg[] = "loadmodule:%s(%s): _init() error %d\n";
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate /*
31477c478bd9Sstevel@tonic-gate  * This routine is needed for dependencies.  Users specify dependencies
31487c478bd9Sstevel@tonic-gate  * by declaring a character array initialized to filenames of dependents.
31497c478bd9Sstevel@tonic-gate  * So the code that handles dependents deals with filenames (and not
31507c478bd9Sstevel@tonic-gate  * module names) because that's all it has.  We load by filename and once
31517c478bd9Sstevel@tonic-gate  * we've loaded a file we can get the module name.
31527c478bd9Sstevel@tonic-gate  * Unfortunately there isn't a single unified filename/modulename namespace.
31537c478bd9Sstevel@tonic-gate  * C'est la vie.
31547c478bd9Sstevel@tonic-gate  *
31557c478bd9Sstevel@tonic-gate  * We allow the name being looked up to be prepended by an optional
31567c478bd9Sstevel@tonic-gate  * subdirectory e.g. we can lookup (NULL, "fs/ufs") or ("fs", "ufs")
31577c478bd9Sstevel@tonic-gate  */
31587c478bd9Sstevel@tonic-gate struct modctl *
31597c478bd9Sstevel@tonic-gate mod_find_by_filename(char *subdir, char *filename)
31607c478bd9Sstevel@tonic-gate {
31617c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
31627c478bd9Sstevel@tonic-gate 	size_t		sublen;
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&mod_lock));
31657c478bd9Sstevel@tonic-gate 	if (subdir != NULL)
31667c478bd9Sstevel@tonic-gate 		sublen = strlen(subdir);
31677c478bd9Sstevel@tonic-gate 	else
31687c478bd9Sstevel@tonic-gate 		sublen = 0;
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
31717c478bd9Sstevel@tonic-gate 	mp = &modules;
31727c478bd9Sstevel@tonic-gate 	do {
31737c478bd9Sstevel@tonic-gate 		if (sublen) {
31747c478bd9Sstevel@tonic-gate 			char *mod_filename = mp->mod_filename;
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 			if (strncmp(subdir, mod_filename, sublen) == 0 &&
31777c478bd9Sstevel@tonic-gate 			    mod_filename[sublen] == '/' &&
31787c478bd9Sstevel@tonic-gate 			    strcmp(filename, &mod_filename[sublen + 1]) == 0) {
31797c478bd9Sstevel@tonic-gate 				mutex_exit(&mod_lock);
31807c478bd9Sstevel@tonic-gate 				return (mp);
31817c478bd9Sstevel@tonic-gate 			}
31827c478bd9Sstevel@tonic-gate 		} else if (strcmp(filename, mp->mod_filename) == 0) {
31837c478bd9Sstevel@tonic-gate 			mutex_exit(&mod_lock);
31847c478bd9Sstevel@tonic-gate 			return (mp);
31857c478bd9Sstevel@tonic-gate 		}
31867c478bd9Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
31877c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
31887c478bd9Sstevel@tonic-gate 	return (NULL);
31897c478bd9Sstevel@tonic-gate }
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate /*
31927c478bd9Sstevel@tonic-gate  * Check for circular dependencies.  This is called from do_dependents()
31937c478bd9Sstevel@tonic-gate  * in kobj.c.  If we are the thread already loading this module, then
31947c478bd9Sstevel@tonic-gate  * we're trying to load a dependent that we're already loading which
31957c478bd9Sstevel@tonic-gate  * means the user specified circular dependencies.
31967c478bd9Sstevel@tonic-gate  */
31977c478bd9Sstevel@tonic-gate static int
31987c478bd9Sstevel@tonic-gate mod_circdep(struct modctl *modp)
31997c478bd9Sstevel@tonic-gate {
32007c478bd9Sstevel@tonic-gate 	struct modctl	*rmod;
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mod_lock));
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 	/*
32057c478bd9Sstevel@tonic-gate 	 * Check the mod_inprogress_thread first.
32067c478bd9Sstevel@tonic-gate 	 * mod_inprogress_thread is used in mod_hold_stub()
32077c478bd9Sstevel@tonic-gate 	 * directly to improve performance.
32087c478bd9Sstevel@tonic-gate 	 */
32097c478bd9Sstevel@tonic-gate 	if (modp->mod_inprogress_thread == curthread)
32107c478bd9Sstevel@tonic-gate 		return (1);
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 	/*
32137c478bd9Sstevel@tonic-gate 	 * Check the module circular dependencies.
32147c478bd9Sstevel@tonic-gate 	 */
32157c478bd9Sstevel@tonic-gate 	for (rmod = modp; rmod != NULL; rmod = rmod->mod_requisite_loading) {
32167c478bd9Sstevel@tonic-gate 		/*
32177c478bd9Sstevel@tonic-gate 		 * Check if there is a module circular dependency.
32187c478bd9Sstevel@tonic-gate 		 */
32197c478bd9Sstevel@tonic-gate 		if (rmod->mod_requisite_loading == modp)
32207c478bd9Sstevel@tonic-gate 			return (1);
32217c478bd9Sstevel@tonic-gate 	}
32227c478bd9Sstevel@tonic-gate 	return (0);
32237c478bd9Sstevel@tonic-gate }
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate static int
32267c478bd9Sstevel@tonic-gate mod_getinfo(struct modctl *modp, struct modinfo *modinfop)
32277c478bd9Sstevel@tonic-gate {
32287c478bd9Sstevel@tonic-gate 	int (*func)(struct modinfo *);
32297c478bd9Sstevel@tonic-gate 	int retval;
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	ASSERT(modp->mod_busy);
32327c478bd9Sstevel@tonic-gate 
32337c478bd9Sstevel@tonic-gate 	/* primary modules don't do getinfo */
32347c478bd9Sstevel@tonic-gate 	if (modp->mod_prim)
32357c478bd9Sstevel@tonic-gate 		return (0);
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 	func = (int (*)(struct modinfo *))kobj_lookup(modp->mod_mp, "_info");
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate 	if (kobj_addrcheck(modp->mod_mp, (caddr_t)func)) {
32407c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "_info() not defined properly in %s",
32417c478bd9Sstevel@tonic-gate 		    modp->mod_filename);
32427c478bd9Sstevel@tonic-gate 		/*
32437c478bd9Sstevel@tonic-gate 		 * The semantics of mod_info(9F) are that 0 is failure
32447c478bd9Sstevel@tonic-gate 		 * and non-zero is success.
32457c478bd9Sstevel@tonic-gate 		 */
32467c478bd9Sstevel@tonic-gate 		retval = 0;
32477c478bd9Sstevel@tonic-gate 	} else
32487c478bd9Sstevel@tonic-gate 		retval = (*func)(modinfop);	/* call _info() function */
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_USERDEBUG)
32517c478bd9Sstevel@tonic-gate 		printf("Returned from _info, retval = %x\n", retval);
32527c478bd9Sstevel@tonic-gate 
32537c478bd9Sstevel@tonic-gate 	return (retval);
32547c478bd9Sstevel@tonic-gate }
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate static void
32577c478bd9Sstevel@tonic-gate modadd(struct modctl *mp)
32587c478bd9Sstevel@tonic-gate {
32597c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mod_lock));
32607c478bd9Sstevel@tonic-gate 
32617c478bd9Sstevel@tonic-gate 	mp->mod_id = last_module_id++;
32627c478bd9Sstevel@tonic-gate 	mp->mod_next = &modules;
32637c478bd9Sstevel@tonic-gate 	mp->mod_prev = modules.mod_prev;
32647c478bd9Sstevel@tonic-gate 	modules.mod_prev->mod_next = mp;
32657c478bd9Sstevel@tonic-gate 	modules.mod_prev = mp;
32667c478bd9Sstevel@tonic-gate }
32677c478bd9Sstevel@tonic-gate 
32687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
32697c478bd9Sstevel@tonic-gate static struct modctl *
32707aec1d6eScindi allocate_modp(const char *filename, const char *modname)
32717c478bd9Sstevel@tonic-gate {
32727c478bd9Sstevel@tonic-gate 	struct modctl *mp;
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate 	mp = kobj_zalloc(sizeof (*mp), KM_SLEEP);
32757c478bd9Sstevel@tonic-gate 	mp->mod_modname = kobj_zalloc(strlen(modname) + 1, KM_SLEEP);
32767c478bd9Sstevel@tonic-gate 	(void) strcpy(mp->mod_modname, modname);
32777c478bd9Sstevel@tonic-gate 	return (mp);
32787c478bd9Sstevel@tonic-gate }
32797c478bd9Sstevel@tonic-gate 
32807c478bd9Sstevel@tonic-gate /*
32817c478bd9Sstevel@tonic-gate  * Get the value of a symbol.  This is a wrapper routine that
32827c478bd9Sstevel@tonic-gate  * calls kobj_getsymvalue().  kobj_getsymvalue() may go away but this
32837c478bd9Sstevel@tonic-gate  * wrapper will prevent callers from noticing.
32847c478bd9Sstevel@tonic-gate  */
32857c478bd9Sstevel@tonic-gate uintptr_t
32867c478bd9Sstevel@tonic-gate modgetsymvalue(char *name, int kernelonly)
32877c478bd9Sstevel@tonic-gate {
32887c478bd9Sstevel@tonic-gate 	return (kobj_getsymvalue(name, kernelonly));
32897c478bd9Sstevel@tonic-gate }
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate /*
32927c478bd9Sstevel@tonic-gate  * Get the symbol nearest an address.  This is a wrapper routine that
32937c478bd9Sstevel@tonic-gate  * calls kobj_getsymname().  kobj_getsymname() may go away but this
32947c478bd9Sstevel@tonic-gate  * wrapper will prevent callers from noticing.
32957c478bd9Sstevel@tonic-gate  */
32967c478bd9Sstevel@tonic-gate char *
32977c478bd9Sstevel@tonic-gate modgetsymname(uintptr_t value, ulong_t *offset)
32987c478bd9Sstevel@tonic-gate {
32997c478bd9Sstevel@tonic-gate 	return (kobj_getsymname(value, offset));
33007c478bd9Sstevel@tonic-gate }
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate /*
33037aec1d6eScindi  * Lookup a symbol in a specified module.  These are wrapper routines that
33047aec1d6eScindi  * call kobj_lookup().	kobj_lookup() may go away but these wrappers will
33057aec1d6eScindi  * prevent callers from noticing.
33067c478bd9Sstevel@tonic-gate  */
33077c478bd9Sstevel@tonic-gate uintptr_t
33087aec1d6eScindi modlookup(const char *modname, const char *symname)
33097c478bd9Sstevel@tonic-gate {
33107c478bd9Sstevel@tonic-gate 	struct modctl *modp;
33117c478bd9Sstevel@tonic-gate 	uintptr_t val;
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 	if ((modp = mod_hold_by_name(modname)) == NULL)
33147c478bd9Sstevel@tonic-gate 		return (0);
33157c478bd9Sstevel@tonic-gate 	val = kobj_lookup(modp->mod_mp, symname);
33167c478bd9Sstevel@tonic-gate 	mod_release_mod(modp);
33177c478bd9Sstevel@tonic-gate 	return (val);
33187c478bd9Sstevel@tonic-gate }
33197c478bd9Sstevel@tonic-gate 
33207aec1d6eScindi uintptr_t
33217aec1d6eScindi modlookup_by_modctl(modctl_t *modp, const char *symname)
33227aec1d6eScindi {
33237aec1d6eScindi 	ASSERT(modp->mod_ref > 0 || modp->mod_busy);
33247aec1d6eScindi 
33257aec1d6eScindi 	return (kobj_lookup(modp->mod_mp, symname));
33267aec1d6eScindi }
33277aec1d6eScindi 
33287c478bd9Sstevel@tonic-gate /*
33297c478bd9Sstevel@tonic-gate  * Ask the user for the name of the system file and the default path
33307c478bd9Sstevel@tonic-gate  * for modules.
33317c478bd9Sstevel@tonic-gate  */
33327c478bd9Sstevel@tonic-gate void
33337c478bd9Sstevel@tonic-gate mod_askparams()
33347c478bd9Sstevel@tonic-gate {
33357c478bd9Sstevel@tonic-gate 	static char s0[64];
33367c478bd9Sstevel@tonic-gate 	intptr_t fd;
33377c478bd9Sstevel@tonic-gate 
33387c478bd9Sstevel@tonic-gate 	if ((fd = kobj_open(systemfile)) != -1L)
33397c478bd9Sstevel@tonic-gate 		kobj_close(fd);
33407c478bd9Sstevel@tonic-gate 	else
33417c478bd9Sstevel@tonic-gate 		systemfile = NULL;
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate 	/*CONSTANTCONDITION*/
33447c478bd9Sstevel@tonic-gate 	while (1) {
33457c478bd9Sstevel@tonic-gate 		printf("Name of system file [%s]:  ",
33467c478bd9Sstevel@tonic-gate 		    systemfile ? systemfile : "/dev/null");
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 		console_gets(s0, sizeof (s0));
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 		if (s0[0] == '\0')
33517c478bd9Sstevel@tonic-gate 			break;
33527c478bd9Sstevel@tonic-gate 		else if (strcmp(s0, "/dev/null") == 0) {
33537c478bd9Sstevel@tonic-gate 			systemfile = NULL;
33547c478bd9Sstevel@tonic-gate 			break;
33557c478bd9Sstevel@tonic-gate 		} else {
33567c478bd9Sstevel@tonic-gate 			if ((fd = kobj_open(s0)) != -1L) {
33577c478bd9Sstevel@tonic-gate 				kobj_close(fd);
33587c478bd9Sstevel@tonic-gate 				systemfile = s0;
33597c478bd9Sstevel@tonic-gate 				break;
33607c478bd9Sstevel@tonic-gate 			}
33617c478bd9Sstevel@tonic-gate 		}
33627c478bd9Sstevel@tonic-gate 		printf("can't find file %s\n", s0);
33637c478bd9Sstevel@tonic-gate 	}
33647c478bd9Sstevel@tonic-gate }
33657c478bd9Sstevel@tonic-gate 
33667c478bd9Sstevel@tonic-gate static char loading_msg[] = "loading '%s' id %d\n";
33677c478bd9Sstevel@tonic-gate static char load_msg[] = "load '%s' id %d loaded @ 0x%p/0x%p size %d/%d\n";
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate /*
33707c478bd9Sstevel@tonic-gate  * Common code for loading a module (but not installing it).
3371da6c28aaSamw  * Handoff the task of module loading to a separate thread
33727c478bd9Sstevel@tonic-gate  * with a large stack if possible, since this code may recurse a few times.
33737c478bd9Sstevel@tonic-gate  * Return zero if there are no errors or an errno value.
33747c478bd9Sstevel@tonic-gate  */
33757c478bd9Sstevel@tonic-gate static int
33767c478bd9Sstevel@tonic-gate mod_load(struct modctl *mp, int usepath)
33777c478bd9Sstevel@tonic-gate {
33787c478bd9Sstevel@tonic-gate 	int		retval;
33797c478bd9Sstevel@tonic-gate 	struct modinfo	*modinfop = NULL;
33807c478bd9Sstevel@tonic-gate 	struct loadmt	lt;
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
33837c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
33847c478bd9Sstevel@tonic-gate 
33857c478bd9Sstevel@tonic-gate 	if (mp->mod_loaded)
33867c478bd9Sstevel@tonic-gate 		return (0);
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 	if (mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_modname) != 0 ||
33897c478bd9Sstevel@tonic-gate 	    mod_sysctl(SYS_CHECK_EXCLUDE, mp->mod_filename) != 0) {
33907c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG) {
33917c478bd9Sstevel@tonic-gate 			printf(mod_excl_msg, mp->mod_filename,
33927c478bd9Sstevel@tonic-gate 			    mp->mod_modname);
33937c478bd9Sstevel@tonic-gate 		}
33947c478bd9Sstevel@tonic-gate 		return (ENXIO);
33957c478bd9Sstevel@tonic-gate 	}
33967c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG2)
33977c478bd9Sstevel@tonic-gate 		printf(loading_msg, mp->mod_filename, mp->mod_id);
33987c478bd9Sstevel@tonic-gate 
33997c478bd9Sstevel@tonic-gate 	if (curthread != &t0) {
34007c478bd9Sstevel@tonic-gate 		lt.mp = mp;
34017c478bd9Sstevel@tonic-gate 		lt.usepath = usepath;
34027c478bd9Sstevel@tonic-gate 		lt.owner = curthread;
34037c478bd9Sstevel@tonic-gate 		sema_init(&lt.sema, 0, NULL, SEMA_DEFAULT, NULL);
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 		/* create thread to hand of call to */
34067c478bd9Sstevel@tonic-gate 		(void) thread_create(NULL, DEFAULTSTKSZ * 2,
34077c478bd9Sstevel@tonic-gate 		    modload_thread, &lt, 0, &p0, TS_RUN, maxclsyspri);
34087c478bd9Sstevel@tonic-gate 
34097c478bd9Sstevel@tonic-gate 		/* wait for thread to complete kobj_load_module */
34107c478bd9Sstevel@tonic-gate 		sema_p(&lt.sema);
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 		sema_destroy(&lt.sema);
34137c478bd9Sstevel@tonic-gate 		retval = lt.retval;
34147c478bd9Sstevel@tonic-gate 	} else
34157c478bd9Sstevel@tonic-gate 		retval = kobj_load_module(mp, usepath);
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate 	if (mp->mod_mp) {
34187c478bd9Sstevel@tonic-gate 		ASSERT(retval == 0);
34197c478bd9Sstevel@tonic-gate 		mp->mod_loaded = 1;
34207c478bd9Sstevel@tonic-gate 		mp->mod_loadcnt++;
34217c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG) {
34227c478bd9Sstevel@tonic-gate 			printf(load_msg, mp->mod_filename, mp->mod_id,
34237c478bd9Sstevel@tonic-gate 			    (void *)((struct module *)mp->mod_mp)->text,
34247c478bd9Sstevel@tonic-gate 			    (void *)((struct module *)mp->mod_mp)->data,
34257c478bd9Sstevel@tonic-gate 			    ((struct module *)mp->mod_mp)->text_size,
34267c478bd9Sstevel@tonic-gate 			    ((struct module *)mp->mod_mp)->data_size);
34277c478bd9Sstevel@tonic-gate 		}
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 		/*
34307c478bd9Sstevel@tonic-gate 		 * XXX - There should be a better way to get this.
34317c478bd9Sstevel@tonic-gate 		 */
34327c478bd9Sstevel@tonic-gate 		modinfop = kmem_zalloc(sizeof (struct modinfo), KM_SLEEP);
34337c478bd9Sstevel@tonic-gate 		modinfop->mi_info = MI_INFO_LINKAGE;
34347c478bd9Sstevel@tonic-gate 		if (mod_getinfo(mp, modinfop) == 0)
34357c478bd9Sstevel@tonic-gate 			mp->mod_linkage = NULL;
34367c478bd9Sstevel@tonic-gate 		else {
34377c478bd9Sstevel@tonic-gate 			mp->mod_linkage = (void *)modinfop->mi_base;
34387c478bd9Sstevel@tonic-gate 			ASSERT(mp->mod_linkage->ml_rev == MODREV_1);
34397c478bd9Sstevel@tonic-gate 		}
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate 		/*
34427c478bd9Sstevel@tonic-gate 		 * DCS: bootstrapping code. If the driver is loaded
34437c478bd9Sstevel@tonic-gate 		 * before root mount, it is assumed that the driver
34447c478bd9Sstevel@tonic-gate 		 * may be used before mounting root. In order to
34457c478bd9Sstevel@tonic-gate 		 * access mappings of global to local minor no.'s
34467c478bd9Sstevel@tonic-gate 		 * during installation/open of the driver, we load
34477c478bd9Sstevel@tonic-gate 		 * them into memory here while the BOP_interfaces
34487c478bd9Sstevel@tonic-gate 		 * are still up.
34497c478bd9Sstevel@tonic-gate 		 */
34507c478bd9Sstevel@tonic-gate 		if ((cluster_bootflags & CLUSTER_BOOTED) && !modrootloaded) {
34517c478bd9Sstevel@tonic-gate 			retval = clboot_modload(mp);
34527c478bd9Sstevel@tonic-gate 		}
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate 		kmem_free(modinfop, sizeof (struct modinfo));
34557c478bd9Sstevel@tonic-gate 		(void) mod_sysctl(SYS_SET_MVAR, (void *)mp);
34567c478bd9Sstevel@tonic-gate 		retval = install_stubs_by_name(mp, mp->mod_modname);
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 		/*
34597c478bd9Sstevel@tonic-gate 		 * Now that the module is loaded, we need to give DTrace
34607c478bd9Sstevel@tonic-gate 		 * a chance to notify its providers.  This is done via
34617c478bd9Sstevel@tonic-gate 		 * the dtrace_modload function pointer.
34627c478bd9Sstevel@tonic-gate 		 */
34637c478bd9Sstevel@tonic-gate 		if (strcmp(mp->mod_modname, "dtrace") != 0) {
34647c478bd9Sstevel@tonic-gate 			struct modctl *dmp = mod_hold_by_name("dtrace");
34657c478bd9Sstevel@tonic-gate 
34667c478bd9Sstevel@tonic-gate 			if (dmp != NULL && dtrace_modload != NULL)
34677c478bd9Sstevel@tonic-gate 				(*dtrace_modload)(mp);
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 			mod_release_mod(dmp);
34707c478bd9Sstevel@tonic-gate 		}
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate 	} else {
34737c478bd9Sstevel@tonic-gate 		/*
34747c478bd9Sstevel@tonic-gate 		 * If load failed then we need to release any requisites
34757c478bd9Sstevel@tonic-gate 		 * that we had established.
34767c478bd9Sstevel@tonic-gate 		 */
34777c478bd9Sstevel@tonic-gate 		ASSERT(retval);
34787c478bd9Sstevel@tonic-gate 		mod_release_requisites(mp);
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_ERRMSG)
34817c478bd9Sstevel@tonic-gate 			printf("error loading '%s', error %d\n",
34827c478bd9Sstevel@tonic-gate 			    mp->mod_filename, retval);
34837c478bd9Sstevel@tonic-gate 	}
34847c478bd9Sstevel@tonic-gate 	return (retval);
34857c478bd9Sstevel@tonic-gate }
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate static char unload_msg[] = "unloading %s, module id %d, loadcnt %d.\n";
34887c478bd9Sstevel@tonic-gate 
34897c478bd9Sstevel@tonic-gate static void
34907c478bd9Sstevel@tonic-gate mod_unload(struct modctl *mp)
34917c478bd9Sstevel@tonic-gate {
34927c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
34937c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
34947c478bd9Sstevel@tonic-gate 	ASSERT((mp->mod_loaded && (mp->mod_installed == 0)) &&
34957c478bd9Sstevel@tonic-gate 	    ((mp->mod_prim == 0) && (mp->mod_ref >= 0)));
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
34987c478bd9Sstevel@tonic-gate 		printf(unload_msg, mp->mod_modname,
34997c478bd9Sstevel@tonic-gate 		    mp->mod_id, mp->mod_loadcnt);
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 	/*
35027c478bd9Sstevel@tonic-gate 	 * If mod_ref is not zero, it means some modules might still refer
35037c478bd9Sstevel@tonic-gate 	 * to this module. Then you can't unload this module right now.
35047c478bd9Sstevel@tonic-gate 	 * Instead, set 1 to mod_delay_unload to notify the system of
35057c478bd9Sstevel@tonic-gate 	 * unloading this module later when it's not required any more.
35067c478bd9Sstevel@tonic-gate 	 */
35077c478bd9Sstevel@tonic-gate 	if (mp->mod_ref > 0) {
35087c478bd9Sstevel@tonic-gate 		mp->mod_delay_unload = 1;
35097c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG2) {
35107c478bd9Sstevel@tonic-gate 			printf("module %s not unloaded,"
35117c478bd9Sstevel@tonic-gate 			    " non-zero reference count (%d)",
35127c478bd9Sstevel@tonic-gate 			    mp->mod_modname, mp->mod_ref);
35137c478bd9Sstevel@tonic-gate 		}
35147c478bd9Sstevel@tonic-gate 		return;
35157c478bd9Sstevel@tonic-gate 	}
35167c478bd9Sstevel@tonic-gate 
35177c478bd9Sstevel@tonic-gate 	if (((mp->mod_loaded == 0) || mp->mod_installed) ||
35187c478bd9Sstevel@tonic-gate 	    (mp->mod_ref || mp->mod_prim)) {
35197c478bd9Sstevel@tonic-gate 		/*
35207c478bd9Sstevel@tonic-gate 		 * A DEBUG kernel would ASSERT panic above, the code is broken
35217c478bd9Sstevel@tonic-gate 		 * if we get this warning.
35227c478bd9Sstevel@tonic-gate 		 */
35237c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "mod_unload: %s in incorrect state: %d %d %d",
35247c478bd9Sstevel@tonic-gate 		    mp->mod_filename, mp->mod_installed, mp->mod_loaded,
35257c478bd9Sstevel@tonic-gate 		    mp->mod_ref);
35267c478bd9Sstevel@tonic-gate 		return;
35277c478bd9Sstevel@tonic-gate 	}
35287c478bd9Sstevel@tonic-gate 
35297c478bd9Sstevel@tonic-gate 	/* reset stub functions to call the binder again */
35307c478bd9Sstevel@tonic-gate 	reset_stubs(mp);
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 	/*
35337c478bd9Sstevel@tonic-gate 	 * mark module as unloaded before the modctl structure is freed.
35347c478bd9Sstevel@tonic-gate 	 * This is required not to reuse the modctl structure before
35357c478bd9Sstevel@tonic-gate 	 * the module is marked as unloaded.
35367c478bd9Sstevel@tonic-gate 	 */
35377c478bd9Sstevel@tonic-gate 	mp->mod_loaded = 0;
35387c478bd9Sstevel@tonic-gate 	mp->mod_linkage = NULL;
35397c478bd9Sstevel@tonic-gate 
35407c478bd9Sstevel@tonic-gate 	/* free the memory */
35417c478bd9Sstevel@tonic-gate 	kobj_unload_module(mp);
35427c478bd9Sstevel@tonic-gate 
35437c478bd9Sstevel@tonic-gate 	if (mp->mod_delay_unload) {
35447c478bd9Sstevel@tonic-gate 		mp->mod_delay_unload = 0;
35457c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG2) {
35467c478bd9Sstevel@tonic-gate 			printf("deferred unload of module %s"
35477c478bd9Sstevel@tonic-gate 			    " (id %d) successful",
35487c478bd9Sstevel@tonic-gate 			    mp->mod_modname, mp->mod_id);
35497c478bd9Sstevel@tonic-gate 		}
35507c478bd9Sstevel@tonic-gate 	}
35517c478bd9Sstevel@tonic-gate 
35527c478bd9Sstevel@tonic-gate 	/* release hold on requisites */
35537c478bd9Sstevel@tonic-gate 	mod_release_requisites(mp);
35547c478bd9Sstevel@tonic-gate 
35557c478bd9Sstevel@tonic-gate 	/*
35567c478bd9Sstevel@tonic-gate 	 * Now that the module is gone, we need to give DTrace a chance to
35577c478bd9Sstevel@tonic-gate 	 * remove any probes that it may have had in the module.  This is
35587c478bd9Sstevel@tonic-gate 	 * done via the dtrace_modunload function pointer.
35597c478bd9Sstevel@tonic-gate 	 */
35607c478bd9Sstevel@tonic-gate 	if (strcmp(mp->mod_modname, "dtrace") != 0) {
35617c478bd9Sstevel@tonic-gate 		struct modctl *dmp = mod_hold_by_name("dtrace");
35627c478bd9Sstevel@tonic-gate 
35637c478bd9Sstevel@tonic-gate 		if (dmp != NULL && dtrace_modunload != NULL)
35647c478bd9Sstevel@tonic-gate 			(*dtrace_modunload)(mp);
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate 		mod_release_mod(dmp);
35677c478bd9Sstevel@tonic-gate 	}
35687c478bd9Sstevel@tonic-gate }
35697c478bd9Sstevel@tonic-gate 
35707c478bd9Sstevel@tonic-gate static int
35717c478bd9Sstevel@tonic-gate modinstall(struct modctl *mp)
35727c478bd9Sstevel@tonic-gate {
35737c478bd9Sstevel@tonic-gate 	int val;
35747c478bd9Sstevel@tonic-gate 	int (*func)(void);
35757c478bd9Sstevel@tonic-gate 
35767c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
35777c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy && mp->mod_loaded);
35787c478bd9Sstevel@tonic-gate 
35797c478bd9Sstevel@tonic-gate 	if (mp->mod_installed)
35807c478bd9Sstevel@tonic-gate 		return (0);
35817c478bd9Sstevel@tonic-gate 	/*
35827c478bd9Sstevel@tonic-gate 	 * If mod_delay_unload is on, it means the system chose the deferred
35837c478bd9Sstevel@tonic-gate 	 * unload for this module. Then you can't install this module until
35847c478bd9Sstevel@tonic-gate 	 * it's unloaded from the system.
35857c478bd9Sstevel@tonic-gate 	 */
35867c478bd9Sstevel@tonic-gate 	if (mp->mod_delay_unload)
35877c478bd9Sstevel@tonic-gate 		return (ENXIO);
35887c478bd9Sstevel@tonic-gate 
35897c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG)
35907c478bd9Sstevel@tonic-gate 		printf("installing %s, module id %d.\n",
35917c478bd9Sstevel@tonic-gate 		    mp->mod_modname, mp->mod_id);
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_mp != NULL);
35947c478bd9Sstevel@tonic-gate 	if (mod_install_requisites(mp) != 0) {
35957c478bd9Sstevel@tonic-gate 		/*
35967c478bd9Sstevel@tonic-gate 		 * Note that we can't call mod_unload(mp) here since
35977c478bd9Sstevel@tonic-gate 		 * if modinstall() was called by mod_install_requisites(),
35987c478bd9Sstevel@tonic-gate 		 * we won't be able to hold the dependent modules
35997c478bd9Sstevel@tonic-gate 		 * (otherwise there would be a deadlock).
36007c478bd9Sstevel@tonic-gate 		 */
36017c478bd9Sstevel@tonic-gate 		return (ENXIO);
36027c478bd9Sstevel@tonic-gate 	}
36037c478bd9Sstevel@tonic-gate 
36047c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_ERRMSG) {
36057c478bd9Sstevel@tonic-gate 		printf("init '%s' id %d loaded @ 0x%p/0x%p size %lu/%lu\n",
36067c478bd9Sstevel@tonic-gate 		    mp->mod_filename, mp->mod_id,
36077c478bd9Sstevel@tonic-gate 		    (void *)((struct module *)mp->mod_mp)->text,
36087c478bd9Sstevel@tonic-gate 		    (void *)((struct module *)mp->mod_mp)->data,
36097c478bd9Sstevel@tonic-gate 		    ((struct module *)mp->mod_mp)->text_size,
36107c478bd9Sstevel@tonic-gate 		    ((struct module *)mp->mod_mp)->data_size);
36117c478bd9Sstevel@tonic-gate 	}
36127c478bd9Sstevel@tonic-gate 
36137c478bd9Sstevel@tonic-gate 	func = (int (*)())kobj_lookup(mp->mod_mp, "_init");
36147c478bd9Sstevel@tonic-gate 
36157c478bd9Sstevel@tonic-gate 	if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
36167c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "_init() not defined properly in %s",
36177c478bd9Sstevel@tonic-gate 		    mp->mod_filename);
36187c478bd9Sstevel@tonic-gate 		return (EFAULT);
36197c478bd9Sstevel@tonic-gate 	}
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_USERDEBUG) {
36227c478bd9Sstevel@tonic-gate 		printf("breakpoint before calling %s:_init()\n",
36237c478bd9Sstevel@tonic-gate 		    mp->mod_modname);
36247c478bd9Sstevel@tonic-gate 		if (DEBUGGER_PRESENT)
36257c478bd9Sstevel@tonic-gate 			debug_enter("_init");
36267c478bd9Sstevel@tonic-gate 	}
36277c478bd9Sstevel@tonic-gate 
36287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
36297c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy && mp->mod_loaded);
36307c478bd9Sstevel@tonic-gate 	val = (*func)();		/* call _init */
36317c478bd9Sstevel@tonic-gate 
36327c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_USERDEBUG)
36337c478bd9Sstevel@tonic-gate 		printf("Returned from _init, val = %x\n", val);
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 	if (val == 0) {
36367c478bd9Sstevel@tonic-gate 		/*
36377c478bd9Sstevel@tonic-gate 		 * Set the MODS_INSTALLED flag to enable this module
36387c478bd9Sstevel@tonic-gate 		 * being called now.
36397c478bd9Sstevel@tonic-gate 		 */
36407c478bd9Sstevel@tonic-gate 		install_stubs(mp);
36417c478bd9Sstevel@tonic-gate 		mp->mod_installed = 1;
36427c478bd9Sstevel@tonic-gate 	} else if (moddebug & MODDEBUG_ERRMSG)
36437c478bd9Sstevel@tonic-gate 		printf(mod_init_msg, mp->mod_filename, mp->mod_modname, val);
36447c478bd9Sstevel@tonic-gate 
36457c478bd9Sstevel@tonic-gate 	return (val);
36467c478bd9Sstevel@tonic-gate }
36477c478bd9Sstevel@tonic-gate 
3648a7aa4df7Scth int	detach_driver_unconfig = 0;
3649a7aa4df7Scth 
36507c478bd9Sstevel@tonic-gate static int
36517c478bd9Sstevel@tonic-gate detach_driver(char *name)
36527c478bd9Sstevel@tonic-gate {
36537c478bd9Sstevel@tonic-gate 	major_t major;
36547c478bd9Sstevel@tonic-gate 	int error;
36557c478bd9Sstevel@tonic-gate 
36567c478bd9Sstevel@tonic-gate 	/*
36577c478bd9Sstevel@tonic-gate 	 * If being called from mod_uninstall_all() then the appropriate
36587c478bd9Sstevel@tonic-gate 	 * driver detaches (leaf only) have already been done.
36597c478bd9Sstevel@tonic-gate 	 */
36607c478bd9Sstevel@tonic-gate 	if (mod_in_autounload())
36617c478bd9Sstevel@tonic-gate 		return (0);
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(name);
3664a204de77Scth 	if (major == DDI_MAJOR_T_NONE)
36657c478bd9Sstevel@tonic-gate 		return (0);
36667c478bd9Sstevel@tonic-gate 
36677c478bd9Sstevel@tonic-gate 	error = ndi_devi_unconfig_driver(ddi_root_node(),
3668a7aa4df7Scth 	    NDI_DETACH_DRIVER | detach_driver_unconfig, major);
36697c478bd9Sstevel@tonic-gate 	return (error == NDI_SUCCESS ? 0 : -1);
36707c478bd9Sstevel@tonic-gate }
36717c478bd9Sstevel@tonic-gate 
36727c478bd9Sstevel@tonic-gate static char finiret_msg[] = "Returned from _fini for %s, status = %x\n";
36737c478bd9Sstevel@tonic-gate 
36747c478bd9Sstevel@tonic-gate static int
36757c478bd9Sstevel@tonic-gate moduninstall(struct modctl *mp)
36767c478bd9Sstevel@tonic-gate {
36777c478bd9Sstevel@tonic-gate 	int status = 0;
36787c478bd9Sstevel@tonic-gate 	int (*func)(void);
36797c478bd9Sstevel@tonic-gate 
36807c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
36817c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
36827c478bd9Sstevel@tonic-gate 
36837c478bd9Sstevel@tonic-gate 	/*
36847c478bd9Sstevel@tonic-gate 	 * Verify that we need to do something and can uninstall the module.
36857c478bd9Sstevel@tonic-gate 	 *
36867c478bd9Sstevel@tonic-gate 	 * If we should not uninstall the module or if the module is not in
36877c478bd9Sstevel@tonic-gate 	 * the correct state to start an uninstall we return EBUSY to prevent
36887c478bd9Sstevel@tonic-gate 	 * us from progressing to mod_unload.  If the module has already been
36897c478bd9Sstevel@tonic-gate 	 * uninstalled and unloaded we return EALREADY.
36907c478bd9Sstevel@tonic-gate 	 */
36917c478bd9Sstevel@tonic-gate 	if (mp->mod_prim || mp->mod_ref || mp->mod_nenabled != 0)
36927c478bd9Sstevel@tonic-gate 		return (EBUSY);
36937c478bd9Sstevel@tonic-gate 	if ((mp->mod_installed == 0) || (mp->mod_loaded == 0))
36947c478bd9Sstevel@tonic-gate 		return (EALREADY);
36957c478bd9Sstevel@tonic-gate 
36967c478bd9Sstevel@tonic-gate 	/*
36977c478bd9Sstevel@tonic-gate 	 * To avoid devinfo / module deadlock we must release this module
36987c478bd9Sstevel@tonic-gate 	 * prior to initiating the detach_driver, otherwise the detach_driver
36997c478bd9Sstevel@tonic-gate 	 * might deadlock on a devinfo node held by another thread
37007c478bd9Sstevel@tonic-gate 	 * coming top down and involving the module we have locked.
37017c478bd9Sstevel@tonic-gate 	 *
37027c478bd9Sstevel@tonic-gate 	 * When we regrab the module we must reverify that it is OK
37037c478bd9Sstevel@tonic-gate 	 * to proceed with the uninstall operation.
37047c478bd9Sstevel@tonic-gate 	 */
37057c478bd9Sstevel@tonic-gate 	mod_release_mod(mp);
37067c478bd9Sstevel@tonic-gate 	status = detach_driver(mp->mod_modname);
37077c478bd9Sstevel@tonic-gate 	(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
37087c478bd9Sstevel@tonic-gate 
37097c478bd9Sstevel@tonic-gate 	/* check detach status and reverify state with lock */
37107c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
37117c478bd9Sstevel@tonic-gate 	if ((status != 0) || mp->mod_prim || mp->mod_ref) {
37127c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
37137c478bd9Sstevel@tonic-gate 		return (EBUSY);
37147c478bd9Sstevel@tonic-gate 	}
37157c478bd9Sstevel@tonic-gate 	if ((mp->mod_installed == 0) || (mp->mod_loaded == 0)) {
37167c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
37177c478bd9Sstevel@tonic-gate 		return (EALREADY);
37187c478bd9Sstevel@tonic-gate 	}
37197c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
37207c478bd9Sstevel@tonic-gate 
37217c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG2)
37227c478bd9Sstevel@tonic-gate 		printf("uninstalling %s\n", mp->mod_modname);
37237c478bd9Sstevel@tonic-gate 
37247c478bd9Sstevel@tonic-gate 	/*
37257c478bd9Sstevel@tonic-gate 	 * lookup _fini, return EBUSY if not defined.
37267c478bd9Sstevel@tonic-gate 	 *
37277c478bd9Sstevel@tonic-gate 	 * The MODDEBUG_FINI_EBUSY is usefull in resolving leaks in
37287c478bd9Sstevel@tonic-gate 	 * detach(9E) - it allows bufctl addresses to be resolved.
37297c478bd9Sstevel@tonic-gate 	 */
37307c478bd9Sstevel@tonic-gate 	func = (int (*)())kobj_lookup(mp->mod_mp, "_fini");
37317c478bd9Sstevel@tonic-gate 	if ((func == NULL) || (mp->mod_loadflags & MOD_NOUNLOAD) ||
37327c478bd9Sstevel@tonic-gate 	    (moddebug & MODDEBUG_FINI_EBUSY))
37337c478bd9Sstevel@tonic-gate 		return (EBUSY);
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate 	/* verify that _fini is in this module */
37367c478bd9Sstevel@tonic-gate 	if (kobj_addrcheck(mp->mod_mp, (caddr_t)func)) {
37377c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "_fini() not defined properly in %s",
37387c478bd9Sstevel@tonic-gate 		    mp->mod_filename);
37397c478bd9Sstevel@tonic-gate 		return (EFAULT);
37407c478bd9Sstevel@tonic-gate 	}
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate 	/* call _fini() */
37437c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
37447c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy && mp->mod_loaded && mp->mod_installed);
37457c478bd9Sstevel@tonic-gate 
37467c478bd9Sstevel@tonic-gate 	status = (*func)();
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate 	if (status == 0) {
37497c478bd9Sstevel@tonic-gate 		/* _fini returned success, the module is no longer installed */
37507c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_LOADMSG)
37517c478bd9Sstevel@tonic-gate 			printf("uninstalled %s\n", mp->mod_modname);
37527c478bd9Sstevel@tonic-gate 
37537c478bd9Sstevel@tonic-gate 		/*
37547c478bd9Sstevel@tonic-gate 		 * Even though we only set mod_installed to zero here, a zero
3755da6c28aaSamw 		 * return value means we are committed to a code path were
37567c478bd9Sstevel@tonic-gate 		 * mod_loaded will also end up as zero - we have no other
37577c478bd9Sstevel@tonic-gate 		 * way to get the module data and bss back to the pre _init
37587c478bd9Sstevel@tonic-gate 		 * state except a reload. To ensure this, after return,
37597c478bd9Sstevel@tonic-gate 		 * mod_busy must stay set until mod_loaded is cleared.
37607c478bd9Sstevel@tonic-gate 		 */
37617c478bd9Sstevel@tonic-gate 		mp->mod_installed = 0;
37627c478bd9Sstevel@tonic-gate 
37637c478bd9Sstevel@tonic-gate 		/*
37647c478bd9Sstevel@tonic-gate 		 * Clear the MODS_INSTALLED flag not to call functions
37657c478bd9Sstevel@tonic-gate 		 * in the module directly from now on.
37667c478bd9Sstevel@tonic-gate 		 */
37677c478bd9Sstevel@tonic-gate 		uninstall_stubs(mp);
37687c478bd9Sstevel@tonic-gate 	} else {
37697c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_USERDEBUG)
37707c478bd9Sstevel@tonic-gate 			printf(finiret_msg, mp->mod_filename, status);
37717c478bd9Sstevel@tonic-gate 		/*
37727c478bd9Sstevel@tonic-gate 		 * By definition _fini is only allowed to return EBUSY or the
37737c478bd9Sstevel@tonic-gate 		 * result of mod_remove (EBUSY or EINVAL).  In the off chance
37747c478bd9Sstevel@tonic-gate 		 * that a driver returns EALREADY we convert this to EINVAL
37757c478bd9Sstevel@tonic-gate 		 * since to our caller EALREADY means module was already
37767c478bd9Sstevel@tonic-gate 		 * removed.
37777c478bd9Sstevel@tonic-gate 		 */
37787c478bd9Sstevel@tonic-gate 		if (status == EALREADY)
37797c478bd9Sstevel@tonic-gate 			status = EINVAL;
37807c478bd9Sstevel@tonic-gate 	}
37817c478bd9Sstevel@tonic-gate 
37827c478bd9Sstevel@tonic-gate 	return (status);
37837c478bd9Sstevel@tonic-gate }
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate /*
37867c478bd9Sstevel@tonic-gate  * Uninstall all modules.
37877c478bd9Sstevel@tonic-gate  */
37887c478bd9Sstevel@tonic-gate static void
37897c478bd9Sstevel@tonic-gate mod_uninstall_all(void)
37907c478bd9Sstevel@tonic-gate {
37917c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
37927c478bd9Sstevel@tonic-gate 	modid_t		modid = 0;
37937c478bd9Sstevel@tonic-gate 
3794a7aa4df7Scth 	/* synchronize with any active modunload_disable() */
3795a7aa4df7Scth 	modunload_begin();
3796a7aa4df7Scth 
37977c478bd9Sstevel@tonic-gate 	/* mark this thread as doing autounloading */
37987c478bd9Sstevel@tonic-gate 	(void) tsd_set(mod_autounload_key, (void *)1);
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 	(void) devfs_clean(ddi_root_node(), NULL, 0);
38017c478bd9Sstevel@tonic-gate 	(void) ndi_devi_unconfig(ddi_root_node(), NDI_AUTODETACH);
38027c478bd9Sstevel@tonic-gate 
38037c478bd9Sstevel@tonic-gate 	while ((mp = mod_hold_next_by_id(modid)) != NULL) {
38047c478bd9Sstevel@tonic-gate 		modid = mp->mod_id;
38057c478bd9Sstevel@tonic-gate 		/*
38067c478bd9Sstevel@tonic-gate 		 * Skip modules with the MOD_NOAUTOUNLOAD flag set
38077c478bd9Sstevel@tonic-gate 		 */
38087c478bd9Sstevel@tonic-gate 		if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
38097c478bd9Sstevel@tonic-gate 			mod_release_mod(mp);
38107c478bd9Sstevel@tonic-gate 			continue;
38117c478bd9Sstevel@tonic-gate 		}
38127c478bd9Sstevel@tonic-gate 
38130b38a8bdSahl 		if (moduninstall(mp) == 0) {
38147c478bd9Sstevel@tonic-gate 			mod_unload(mp);
38150b38a8bdSahl 			CPU_STATS_ADDQ(CPU, sys, modunload, 1);
38160b38a8bdSahl 		}
38177c478bd9Sstevel@tonic-gate 		mod_release_mod(mp);
38187c478bd9Sstevel@tonic-gate 	}
38197c478bd9Sstevel@tonic-gate 
38207c478bd9Sstevel@tonic-gate 	(void) tsd_set(mod_autounload_key, NULL);
3821a7aa4df7Scth 	modunload_end();
38227c478bd9Sstevel@tonic-gate }
38237c478bd9Sstevel@tonic-gate 
3824a7aa4df7Scth /* wait for unloads that have begun before registering disable */
38257c478bd9Sstevel@tonic-gate void
38267c478bd9Sstevel@tonic-gate modunload_disable(void)
38277c478bd9Sstevel@tonic-gate {
3828a7aa4df7Scth 	mutex_enter(&modunload_wait_mutex);
3829a7aa4df7Scth 	while (modunload_active_count) {
3830a7aa4df7Scth 		modunload_wait++;
3831a7aa4df7Scth 		cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
3832a7aa4df7Scth 		modunload_wait--;
3833a7aa4df7Scth 	}
3834a7aa4df7Scth 	modunload_disable_count++;
3835a7aa4df7Scth 	mutex_exit(&modunload_wait_mutex);
38367c478bd9Sstevel@tonic-gate }
38377c478bd9Sstevel@tonic-gate 
3838a7aa4df7Scth /* mark end of disable and signal waiters */
38397c478bd9Sstevel@tonic-gate void
38407c478bd9Sstevel@tonic-gate modunload_enable(void)
38417c478bd9Sstevel@tonic-gate {
3842a7aa4df7Scth 	mutex_enter(&modunload_wait_mutex);
3843a7aa4df7Scth 	modunload_disable_count--;
3844a7aa4df7Scth 	if ((modunload_disable_count == 0) && modunload_wait)
3845a7aa4df7Scth 		cv_broadcast(&modunload_wait_cv);
3846a7aa4df7Scth 	mutex_exit(&modunload_wait_mutex);
3847a7aa4df7Scth }
3848a7aa4df7Scth 
3849a7aa4df7Scth /* wait for disables to complete before begining unload */
3850a7aa4df7Scth void
3851a7aa4df7Scth modunload_begin()
3852a7aa4df7Scth {
3853a7aa4df7Scth 	mutex_enter(&modunload_wait_mutex);
3854a7aa4df7Scth 	while (modunload_disable_count) {
3855a7aa4df7Scth 		modunload_wait++;
3856a7aa4df7Scth 		cv_wait(&modunload_wait_cv, &modunload_wait_mutex);
3857a7aa4df7Scth 		modunload_wait--;
3858a7aa4df7Scth 	}
3859a7aa4df7Scth 	modunload_active_count++;
3860a7aa4df7Scth 	mutex_exit(&modunload_wait_mutex);
3861a7aa4df7Scth }
3862a7aa4df7Scth 
3863a7aa4df7Scth /* mark end of unload and signal waiters */
3864a7aa4df7Scth void
3865a7aa4df7Scth modunload_end()
3866a7aa4df7Scth {
3867a7aa4df7Scth 	mutex_enter(&modunload_wait_mutex);
3868a7aa4df7Scth 	modunload_active_count--;
3869a7aa4df7Scth 	if ((modunload_active_count == 0) && modunload_wait)
3870a7aa4df7Scth 		cv_broadcast(&modunload_wait_cv);
3871a7aa4df7Scth 	mutex_exit(&modunload_wait_mutex);
38727c478bd9Sstevel@tonic-gate }
38737c478bd9Sstevel@tonic-gate 
38747c478bd9Sstevel@tonic-gate void
38757c478bd9Sstevel@tonic-gate mod_uninstall_daemon(void)
38767c478bd9Sstevel@tonic-gate {
38777c478bd9Sstevel@tonic-gate 	callb_cpr_t	cprinfo;
38787c478bd9Sstevel@tonic-gate 	clock_t		ticks = 0;
38797c478bd9Sstevel@tonic-gate 
38807c478bd9Sstevel@tonic-gate 	mod_aul_thread = curthread;
38817c478bd9Sstevel@tonic-gate 
38827c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &mod_uninstall_lock, callb_generic_cpr, "mud");
38837c478bd9Sstevel@tonic-gate 	for (;;) {
38847c478bd9Sstevel@tonic-gate 		mutex_enter(&mod_uninstall_lock);
38857c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
38867c478bd9Sstevel@tonic-gate 		/*
38877c478bd9Sstevel@tonic-gate 		 * In DEBUG kernels, unheld drivers are uninstalled periodically
38887c478bd9Sstevel@tonic-gate 		 * every mod_uninstall_interval seconds.  Periodic uninstall can
38897c478bd9Sstevel@tonic-gate 		 * be disabled by setting mod_uninstall_interval to 0 which is
38907c478bd9Sstevel@tonic-gate 		 * the default for a non-DEBUG kernel.
38917c478bd9Sstevel@tonic-gate 		 */
38927c478bd9Sstevel@tonic-gate 		if (mod_uninstall_interval) {
38937c478bd9Sstevel@tonic-gate 			ticks = ddi_get_lbolt() +
38947c478bd9Sstevel@tonic-gate 			    drv_usectohz(mod_uninstall_interval * 1000000);
38957c478bd9Sstevel@tonic-gate 			(void) cv_timedwait(&mod_uninstall_cv,
38967c478bd9Sstevel@tonic-gate 			    &mod_uninstall_lock, ticks);
38977c478bd9Sstevel@tonic-gate 		} else {
38987c478bd9Sstevel@tonic-gate 			cv_wait(&mod_uninstall_cv, &mod_uninstall_lock);
38997c478bd9Sstevel@tonic-gate 		}
39007c478bd9Sstevel@tonic-gate 		/*
39017c478bd9Sstevel@tonic-gate 		 * The whole daemon is safe for CPR except we don't want
39027c478bd9Sstevel@tonic-gate 		 * the daemon to run if FREEZE is issued and this daemon
39037c478bd9Sstevel@tonic-gate 		 * wakes up from the cv_wait above. In this case, it'll be
39047c478bd9Sstevel@tonic-gate 		 * blocked in CALLB_CPR_SAFE_END until THAW is issued.
39057c478bd9Sstevel@tonic-gate 		 *
39067c478bd9Sstevel@tonic-gate 		 * The reason of calling CALLB_CPR_SAFE_BEGIN twice is that
39077c478bd9Sstevel@tonic-gate 		 * mod_uninstall_lock is used to protect cprinfo and
39087c478bd9Sstevel@tonic-gate 		 * CALLB_CPR_SAFE_BEGIN assumes that this lock is held when
39097c478bd9Sstevel@tonic-gate 		 * called.
39107c478bd9Sstevel@tonic-gate 		 */
39117c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_END(&cprinfo, &mod_uninstall_lock);
39127c478bd9Sstevel@tonic-gate 		CALLB_CPR_SAFE_BEGIN(&cprinfo);
39137c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_uninstall_lock);
39147c478bd9Sstevel@tonic-gate 		if ((modunload_disable_count == 0) &&
39157c478bd9Sstevel@tonic-gate 		    ((moddebug & MODDEBUG_NOAUTOUNLOAD) == 0)) {
39167c478bd9Sstevel@tonic-gate 			mod_uninstall_all();
39177c478bd9Sstevel@tonic-gate 		}
39187c478bd9Sstevel@tonic-gate 	}
39197c478bd9Sstevel@tonic-gate }
39207c478bd9Sstevel@tonic-gate 
39217c478bd9Sstevel@tonic-gate /*
39227c478bd9Sstevel@tonic-gate  * Unload all uninstalled modules.
39237c478bd9Sstevel@tonic-gate  */
39247c478bd9Sstevel@tonic-gate void
39257c478bd9Sstevel@tonic-gate modreap(void)
39267c478bd9Sstevel@tonic-gate {
39277c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_uninstall_lock);
39287c478bd9Sstevel@tonic-gate 	cv_broadcast(&mod_uninstall_cv);
39297c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_uninstall_lock);
39307c478bd9Sstevel@tonic-gate }
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate /*
39337c478bd9Sstevel@tonic-gate  * Hold the specified module. This is the module holding primitive.
39347c478bd9Sstevel@tonic-gate  *
39357c478bd9Sstevel@tonic-gate  * If MOD_LOCK_HELD then the caller already holds the mod_lock.
39367c478bd9Sstevel@tonic-gate  *
39377c478bd9Sstevel@tonic-gate  * Return values:
39387c478bd9Sstevel@tonic-gate  *	 0 ==> the module is held
39397c478bd9Sstevel@tonic-gate  *	 1 ==> the module is not held and the MOD_WAIT_ONCE caller needs
39407c478bd9Sstevel@tonic-gate  *		to determine how to retry.
39417c478bd9Sstevel@tonic-gate  */
39427c478bd9Sstevel@tonic-gate int
39437c478bd9Sstevel@tonic-gate mod_hold_by_modctl(struct modctl *mp, int f)
39447c478bd9Sstevel@tonic-gate {
39457c478bd9Sstevel@tonic-gate 	ASSERT((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) &&
39467c478bd9Sstevel@tonic-gate 	    ((f & (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)) !=
39477c478bd9Sstevel@tonic-gate 	    (MOD_WAIT_ONCE | MOD_WAIT_FOREVER)));
39487c478bd9Sstevel@tonic-gate 	ASSERT((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) &&
39497c478bd9Sstevel@tonic-gate 	    ((f & (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)) !=
39507c478bd9Sstevel@tonic-gate 	    (MOD_LOCK_HELD | MOD_LOCK_NOT_HELD)));
39517c478bd9Sstevel@tonic-gate 	ASSERT((f & MOD_LOCK_NOT_HELD) || MUTEX_HELD(&mod_lock));
39527c478bd9Sstevel@tonic-gate 
39537c478bd9Sstevel@tonic-gate 	if (f & MOD_LOCK_NOT_HELD)
39547c478bd9Sstevel@tonic-gate 		mutex_enter(&mod_lock);
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate 	while (mp->mod_busy) {
39577c478bd9Sstevel@tonic-gate 		mp->mod_want = 1;
39587c478bd9Sstevel@tonic-gate 		cv_wait(&mod_cv, &mod_lock);
39597c478bd9Sstevel@tonic-gate 		/*
39607c478bd9Sstevel@tonic-gate 		 * Module may be unloaded by daemon.
39617c478bd9Sstevel@tonic-gate 		 * Nevertheless, modctl structure is still in linked list
39627c478bd9Sstevel@tonic-gate 		 * (i.e., off &modules), not freed!
39637c478bd9Sstevel@tonic-gate 		 * Caller is not supposed to assume "mp" is valid, but there
39647c478bd9Sstevel@tonic-gate 		 * is no reasonable way to detect this but using
39657c478bd9Sstevel@tonic-gate 		 * mp->mod_modinfo->mp == NULL check (follow the back pointer)
39667c478bd9Sstevel@tonic-gate 		 *   (or similar check depending on calling context)
39677c478bd9Sstevel@tonic-gate 		 * DON'T free modctl structure, it will be very very
39687c478bd9Sstevel@tonic-gate 		 * problematic.
39697c478bd9Sstevel@tonic-gate 		 */
39707c478bd9Sstevel@tonic-gate 		if (f & MOD_WAIT_ONCE) {
39717c478bd9Sstevel@tonic-gate 			if (f & MOD_LOCK_NOT_HELD)
39727c478bd9Sstevel@tonic-gate 				mutex_exit(&mod_lock);
39737c478bd9Sstevel@tonic-gate 			return (1);	/* caller decides how to retry */
39747c478bd9Sstevel@tonic-gate 		}
39757c478bd9Sstevel@tonic-gate 	}
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate 	mp->mod_busy = 1;
39787c478bd9Sstevel@tonic-gate 	mp->mod_inprogress_thread =
39797c478bd9Sstevel@tonic-gate 	    (curthread == NULL ? (kthread_id_t)-1 : curthread);
39807c478bd9Sstevel@tonic-gate 
39817c478bd9Sstevel@tonic-gate 	if (f & MOD_LOCK_NOT_HELD)
39827c478bd9Sstevel@tonic-gate 		mutex_exit(&mod_lock);
39837c478bd9Sstevel@tonic-gate 	return (0);
39847c478bd9Sstevel@tonic-gate }
39857c478bd9Sstevel@tonic-gate 
39867c478bd9Sstevel@tonic-gate static struct modctl *
39877aec1d6eScindi mod_hold_by_name_common(struct modctl *dep, const char *filename)
39887c478bd9Sstevel@tonic-gate {
39897aec1d6eScindi 	const char	*modname;
39907c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
39917c478bd9Sstevel@tonic-gate 	char		*curname, *newname;
39927c478bd9Sstevel@tonic-gate 	int		found = 0;
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate 	if ((modname = strrchr(filename, '/')) == NULL)
39977c478bd9Sstevel@tonic-gate 		modname = filename;
39987c478bd9Sstevel@tonic-gate 	else
39997c478bd9Sstevel@tonic-gate 		modname++;
40007c478bd9Sstevel@tonic-gate 
40017c478bd9Sstevel@tonic-gate 	mp = &modules;
40027c478bd9Sstevel@tonic-gate 	do {
40037c478bd9Sstevel@tonic-gate 		if (strcmp(modname, mp->mod_modname) == 0) {
40047c478bd9Sstevel@tonic-gate 			found = 1;
40057c478bd9Sstevel@tonic-gate 			break;
40067c478bd9Sstevel@tonic-gate 		}
40077c478bd9Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
40087c478bd9Sstevel@tonic-gate 
40097c478bd9Sstevel@tonic-gate 	if (found == 0) {
40107c478bd9Sstevel@tonic-gate 		mp = allocate_modp(filename, modname);
40117c478bd9Sstevel@tonic-gate 		modadd(mp);
40127c478bd9Sstevel@tonic-gate 	}
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate 	/*
40157c478bd9Sstevel@tonic-gate 	 * if dep is not NULL, set the mp in mod_requisite_loading for
40167c478bd9Sstevel@tonic-gate 	 * the module circular dependency check. This field is used in
40177c478bd9Sstevel@tonic-gate 	 * mod_circdep(), but it's cleard in mod_hold_loaded_mod().
40187c478bd9Sstevel@tonic-gate 	 */
40197c478bd9Sstevel@tonic-gate 	if (dep != NULL) {
40207c478bd9Sstevel@tonic-gate 		ASSERT(dep->mod_busy && dep->mod_requisite_loading == NULL);
40217c478bd9Sstevel@tonic-gate 		dep->mod_requisite_loading = mp;
40227c478bd9Sstevel@tonic-gate 	}
40237c478bd9Sstevel@tonic-gate 
40247c478bd9Sstevel@tonic-gate 	/*
40257c478bd9Sstevel@tonic-gate 	 * If the module was held, then it must be us who has it held.
40267c478bd9Sstevel@tonic-gate 	 */
40277c478bd9Sstevel@tonic-gate 	if (mod_circdep(mp))
40287c478bd9Sstevel@tonic-gate 		mp = NULL;
40297c478bd9Sstevel@tonic-gate 	else {
40307c478bd9Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
40317c478bd9Sstevel@tonic-gate 
40327c478bd9Sstevel@tonic-gate 		/*
40337c478bd9Sstevel@tonic-gate 		 * If the name hadn't been set or has changed, allocate
40347c478bd9Sstevel@tonic-gate 		 * space and set it.  Free space used by previous name.
40357c478bd9Sstevel@tonic-gate 		 *
40367c478bd9Sstevel@tonic-gate 		 * Do not change the name of primary modules, for primary
40377c478bd9Sstevel@tonic-gate 		 * modules the mod_filename was allocated in standalone mode:
40387c478bd9Sstevel@tonic-gate 		 * it is illegal to kobj_alloc in standalone mode and kobj_free
40397c478bd9Sstevel@tonic-gate 		 * in non-standalone mode.
40407c478bd9Sstevel@tonic-gate 		 */
40417c478bd9Sstevel@tonic-gate 		curname = mp->mod_filename;
40427c478bd9Sstevel@tonic-gate 		if (curname == NULL ||
40437c478bd9Sstevel@tonic-gate 		    ((mp->mod_prim == 0) &&
40447c478bd9Sstevel@tonic-gate 		    (curname != filename) &&
40457c478bd9Sstevel@tonic-gate 		    (modname != filename) &&
40467c478bd9Sstevel@tonic-gate 		    (strcmp(curname, filename) != 0))) {
40477c478bd9Sstevel@tonic-gate 			newname = kobj_zalloc(strlen(filename) + 1, KM_SLEEP);
40487c478bd9Sstevel@tonic-gate 			(void) strcpy(newname, filename);
40497c478bd9Sstevel@tonic-gate 			mp->mod_filename = newname;
40507c478bd9Sstevel@tonic-gate 			if (curname != NULL)
40517c478bd9Sstevel@tonic-gate 				kobj_free(curname, strlen(curname) + 1);
40527c478bd9Sstevel@tonic-gate 		}
40537c478bd9Sstevel@tonic-gate 	}
40547c478bd9Sstevel@tonic-gate 
40557c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
40567c478bd9Sstevel@tonic-gate 	if (mp && moddebug & MODDEBUG_LOADMSG2)
40577c478bd9Sstevel@tonic-gate 		printf("Holding %s\n", mp->mod_filename);
40587c478bd9Sstevel@tonic-gate 	if (mp == NULL && moddebug & MODDEBUG_LOADMSG2)
40597c478bd9Sstevel@tonic-gate 		printf("circular dependency loading %s\n", filename);
40607c478bd9Sstevel@tonic-gate 	return (mp);
40617c478bd9Sstevel@tonic-gate }
40627c478bd9Sstevel@tonic-gate 
40637c478bd9Sstevel@tonic-gate static struct modctl *
40647c478bd9Sstevel@tonic-gate mod_hold_by_name_requisite(struct modctl *dep, char *filename)
40657c478bd9Sstevel@tonic-gate {
40667c478bd9Sstevel@tonic-gate 	return (mod_hold_by_name_common(dep, filename));
40677c478bd9Sstevel@tonic-gate }
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate struct modctl *
40707aec1d6eScindi mod_hold_by_name(const char *filename)
40717c478bd9Sstevel@tonic-gate {
40727c478bd9Sstevel@tonic-gate 	return (mod_hold_by_name_common(NULL, filename));
40737c478bd9Sstevel@tonic-gate }
40747c478bd9Sstevel@tonic-gate 
40757aec1d6eScindi struct modctl *
40767c478bd9Sstevel@tonic-gate mod_hold_by_id(modid_t modid)
40777c478bd9Sstevel@tonic-gate {
40787c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
40797c478bd9Sstevel@tonic-gate 	int		found = 0;
40807c478bd9Sstevel@tonic-gate 
40817c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
40827c478bd9Sstevel@tonic-gate 	mp = &modules;
40837c478bd9Sstevel@tonic-gate 	do {
40847c478bd9Sstevel@tonic-gate 		if (mp->mod_id == modid) {
40857c478bd9Sstevel@tonic-gate 			found = 1;
40867c478bd9Sstevel@tonic-gate 			break;
40877c478bd9Sstevel@tonic-gate 		}
40887c478bd9Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate 	if ((found == 0) || mod_circdep(mp))
40917c478bd9Sstevel@tonic-gate 		mp = NULL;
40927c478bd9Sstevel@tonic-gate 	else
40937c478bd9Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
40967c478bd9Sstevel@tonic-gate 	return (mp);
40977c478bd9Sstevel@tonic-gate }
40987c478bd9Sstevel@tonic-gate 
40997c478bd9Sstevel@tonic-gate static struct modctl *
41007c478bd9Sstevel@tonic-gate mod_hold_next_by_id(modid_t modid)
41017c478bd9Sstevel@tonic-gate {
41027c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
41037c478bd9Sstevel@tonic-gate 	int		found = 0;
41047c478bd9Sstevel@tonic-gate 
41057c478bd9Sstevel@tonic-gate 	if (modid < -1)
41067c478bd9Sstevel@tonic-gate 		return (NULL);
41077c478bd9Sstevel@tonic-gate 
41087c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
41097c478bd9Sstevel@tonic-gate 
41107c478bd9Sstevel@tonic-gate 	mp = &modules;
41117c478bd9Sstevel@tonic-gate 	do {
41127c478bd9Sstevel@tonic-gate 		if (mp->mod_id > modid) {
41137c478bd9Sstevel@tonic-gate 			found = 1;
41147c478bd9Sstevel@tonic-gate 			break;
41157c478bd9Sstevel@tonic-gate 		}
41167c478bd9Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
41177c478bd9Sstevel@tonic-gate 
41187c478bd9Sstevel@tonic-gate 	if ((found == 0) || mod_circdep(mp))
41197c478bd9Sstevel@tonic-gate 		mp = NULL;
41207c478bd9Sstevel@tonic-gate 	else
41217c478bd9Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp, MOD_WAIT_FOREVER | MOD_LOCK_HELD);
41227c478bd9Sstevel@tonic-gate 
41237c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
41247c478bd9Sstevel@tonic-gate 	return (mp);
41257c478bd9Sstevel@tonic-gate }
41267c478bd9Sstevel@tonic-gate 
41277c478bd9Sstevel@tonic-gate static void
41287c478bd9Sstevel@tonic-gate mod_release(struct modctl *mp)
41297c478bd9Sstevel@tonic-gate {
41307c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&mod_lock));
41317c478bd9Sstevel@tonic-gate 	ASSERT(mp->mod_busy);
41327c478bd9Sstevel@tonic-gate 
41337c478bd9Sstevel@tonic-gate 	mp->mod_busy = 0;
41347c478bd9Sstevel@tonic-gate 	mp->mod_inprogress_thread = NULL;
41357c478bd9Sstevel@tonic-gate 	if (mp->mod_want) {
41367c478bd9Sstevel@tonic-gate 		mp->mod_want = 0;
41377c478bd9Sstevel@tonic-gate 		cv_broadcast(&mod_cv);
41387c478bd9Sstevel@tonic-gate 	}
41397c478bd9Sstevel@tonic-gate }
41407c478bd9Sstevel@tonic-gate 
41417c478bd9Sstevel@tonic-gate void
41427c478bd9Sstevel@tonic-gate mod_release_mod(struct modctl *mp)
41437c478bd9Sstevel@tonic-gate {
41447c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_LOADMSG2)
41457c478bd9Sstevel@tonic-gate 		printf("Releasing %s\n", mp->mod_filename);
41467c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
41477c478bd9Sstevel@tonic-gate 	mod_release(mp);
41487c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
41497c478bd9Sstevel@tonic-gate }
41507c478bd9Sstevel@tonic-gate 
41517c478bd9Sstevel@tonic-gate modid_t
41527c478bd9Sstevel@tonic-gate mod_name_to_modid(char *filename)
41537c478bd9Sstevel@tonic-gate {
41547c478bd9Sstevel@tonic-gate 	char		*modname;
41557c478bd9Sstevel@tonic-gate 	struct modctl	*mp;
41567c478bd9Sstevel@tonic-gate 
41577c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
41587c478bd9Sstevel@tonic-gate 
41597c478bd9Sstevel@tonic-gate 	if ((modname = strrchr(filename, '/')) == NULL)
41607c478bd9Sstevel@tonic-gate 		modname = filename;
41617c478bd9Sstevel@tonic-gate 	else
41627c478bd9Sstevel@tonic-gate 		modname++;
41637c478bd9Sstevel@tonic-gate 
41647c478bd9Sstevel@tonic-gate 	mp = &modules;
41657c478bd9Sstevel@tonic-gate 	do {
41667c478bd9Sstevel@tonic-gate 		if (strcmp(modname, mp->mod_modname) == 0) {
41677c478bd9Sstevel@tonic-gate 			mutex_exit(&mod_lock);
41687c478bd9Sstevel@tonic-gate 			return (mp->mod_id);
41697c478bd9Sstevel@tonic-gate 		}
41707c478bd9Sstevel@tonic-gate 	} while ((mp = mp->mod_next) != &modules);
41717c478bd9Sstevel@tonic-gate 
41727c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
41737c478bd9Sstevel@tonic-gate 	return (-1);
41747c478bd9Sstevel@tonic-gate }
41757c478bd9Sstevel@tonic-gate 
41767c478bd9Sstevel@tonic-gate 
41777c478bd9Sstevel@tonic-gate int
41787c478bd9Sstevel@tonic-gate mod_remove_by_name(char *name)
41797c478bd9Sstevel@tonic-gate {
41807c478bd9Sstevel@tonic-gate 	struct modctl *mp;
41817c478bd9Sstevel@tonic-gate 	int retval;
41827c478bd9Sstevel@tonic-gate 
41837c478bd9Sstevel@tonic-gate 	mp = mod_hold_by_name(name);
41847c478bd9Sstevel@tonic-gate 
41857c478bd9Sstevel@tonic-gate 	if (mp == NULL)
41867c478bd9Sstevel@tonic-gate 		return (EINVAL);
41877c478bd9Sstevel@tonic-gate 
41887c478bd9Sstevel@tonic-gate 	if (mp->mod_loadflags & MOD_NOAUTOUNLOAD) {
41897c478bd9Sstevel@tonic-gate 		/*
41907c478bd9Sstevel@tonic-gate 		 * Do not unload forceloaded modules
41917c478bd9Sstevel@tonic-gate 		 */
41927c478bd9Sstevel@tonic-gate 		mod_release_mod(mp);
41937c478bd9Sstevel@tonic-gate 		return (0);
41947c478bd9Sstevel@tonic-gate 	}
41957c478bd9Sstevel@tonic-gate 
41960b38a8bdSahl 	if ((retval = moduninstall(mp)) == 0) {
41977c478bd9Sstevel@tonic-gate 		mod_unload(mp);
41980b38a8bdSahl 		CPU_STATS_ADDQ(CPU, sys, modunload, 1);
41990b38a8bdSahl 	} else if (retval == EALREADY)
42007c478bd9Sstevel@tonic-gate 		retval = 0;		/* already unloaded, not an error */
42017c478bd9Sstevel@tonic-gate 	mod_release_mod(mp);
42027c478bd9Sstevel@tonic-gate 	return (retval);
42037c478bd9Sstevel@tonic-gate }
42047c478bd9Sstevel@tonic-gate 
42057c478bd9Sstevel@tonic-gate /*
42067c478bd9Sstevel@tonic-gate  * Record that module "dep" is dependent on module "on_mod."
42077c478bd9Sstevel@tonic-gate  */
42087c478bd9Sstevel@tonic-gate static void
42097c478bd9Sstevel@tonic-gate mod_make_requisite(struct modctl *dependent, struct modctl *on_mod)
42107c478bd9Sstevel@tonic-gate {
42117c478bd9Sstevel@tonic-gate 	struct modctl_list **pmlnp;	/* previous next pointer */
42127c478bd9Sstevel@tonic-gate 	struct modctl_list *mlp;
42137c478bd9Sstevel@tonic-gate 	struct modctl_list *new;
42147c478bd9Sstevel@tonic-gate 
42157c478bd9Sstevel@tonic-gate 	ASSERT(dependent->mod_busy && on_mod->mod_busy);
42167c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
42177c478bd9Sstevel@tonic-gate 
42187c478bd9Sstevel@tonic-gate 	/*
42197c478bd9Sstevel@tonic-gate 	 * Search dependent's requisite list to see if on_mod is recorded.
42207c478bd9Sstevel@tonic-gate 	 * List is ordered by id.
42217c478bd9Sstevel@tonic-gate 	 */
42227c478bd9Sstevel@tonic-gate 	for (pmlnp = &dependent->mod_requisites, mlp = *pmlnp;
42237c478bd9Sstevel@tonic-gate 	    mlp; pmlnp = &mlp->modl_next, mlp = *pmlnp)
42247c478bd9Sstevel@tonic-gate 		if (mlp->modl_modp->mod_id >= on_mod->mod_id)
42257c478bd9Sstevel@tonic-gate 			break;
42267c478bd9Sstevel@tonic-gate 
42277c478bd9Sstevel@tonic-gate 	/* Create and insert if not already recorded */
42287c478bd9Sstevel@tonic-gate 	if ((mlp == NULL) || (mlp->modl_modp->mod_id != on_mod->mod_id)) {
42297c478bd9Sstevel@tonic-gate 		new = kobj_zalloc(sizeof (*new), KM_SLEEP);
42307c478bd9Sstevel@tonic-gate 		new->modl_modp = on_mod;
42317c478bd9Sstevel@tonic-gate 		new->modl_next = mlp;
42327c478bd9Sstevel@tonic-gate 		*pmlnp = new;
42337c478bd9Sstevel@tonic-gate 
42347c478bd9Sstevel@tonic-gate 		/*
42357c478bd9Sstevel@tonic-gate 		 * Increment the mod_ref count in our new requisite module.
42367c478bd9Sstevel@tonic-gate 		 * This is what keeps a module that has other modules
42377c478bd9Sstevel@tonic-gate 		 * which are dependent on it from being uninstalled and
42387c478bd9Sstevel@tonic-gate 		 * unloaded. "on_mod"'s mod_ref count decremented in
42397c478bd9Sstevel@tonic-gate 		 * mod_release_requisites when the "dependent" module
42407c478bd9Sstevel@tonic-gate 		 * unload is complete.	"on_mod" must be loaded, but may not
42417c478bd9Sstevel@tonic-gate 		 * yet be installed.
42427c478bd9Sstevel@tonic-gate 		 */
42437c478bd9Sstevel@tonic-gate 		on_mod->mod_ref++;
42447c478bd9Sstevel@tonic-gate 		ASSERT(on_mod->mod_ref && on_mod->mod_loaded);
42457c478bd9Sstevel@tonic-gate 	}
42467c478bd9Sstevel@tonic-gate 
42477c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
42487c478bd9Sstevel@tonic-gate }
42497c478bd9Sstevel@tonic-gate 
42507c478bd9Sstevel@tonic-gate /*
42517c478bd9Sstevel@tonic-gate  * release the hold associated with mod_make_requisite mod_ref++
42527c478bd9Sstevel@tonic-gate  * as part of unload.
42537c478bd9Sstevel@tonic-gate  */
42547c478bd9Sstevel@tonic-gate void
42557c478bd9Sstevel@tonic-gate mod_release_requisites(struct modctl *modp)
42567c478bd9Sstevel@tonic-gate {
42577c478bd9Sstevel@tonic-gate 	struct modctl_list *modl;
42587c478bd9Sstevel@tonic-gate 	struct modctl_list *next;
42597c478bd9Sstevel@tonic-gate 	struct modctl *req;
42607c478bd9Sstevel@tonic-gate 	struct modctl_list *start = NULL, *mod_garbage;
42617c478bd9Sstevel@tonic-gate 
42627c478bd9Sstevel@tonic-gate 	ASSERT(modp->mod_busy);
42637c478bd9Sstevel@tonic-gate 	ASSERT(!MUTEX_HELD(&mod_lock));
42647c478bd9Sstevel@tonic-gate 
42657c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);		/* needed for manipulation of req */
42667c478bd9Sstevel@tonic-gate 	for (modl = modp->mod_requisites; modl; modl = next) {
42677c478bd9Sstevel@tonic-gate 		next = modl->modl_next;
42687c478bd9Sstevel@tonic-gate 		req = modl->modl_modp;
42697c478bd9Sstevel@tonic-gate 		ASSERT(req->mod_ref >= 1 && req->mod_loaded);
42707c478bd9Sstevel@tonic-gate 		req->mod_ref--;
42717c478bd9Sstevel@tonic-gate 
42727c478bd9Sstevel@tonic-gate 		/*
42737c478bd9Sstevel@tonic-gate 		 * Check if the module has to be unloaded or not.
42747c478bd9Sstevel@tonic-gate 		 */
42757c478bd9Sstevel@tonic-gate 		if (req->mod_ref == 0 && req->mod_delay_unload) {
42767c478bd9Sstevel@tonic-gate 			struct modctl_list *new;
42777c478bd9Sstevel@tonic-gate 			/*
42787c478bd9Sstevel@tonic-gate 			 * Allocate the modclt_list holding the garbage
42797c478bd9Sstevel@tonic-gate 			 * module which should be unloaded later.
42807c478bd9Sstevel@tonic-gate 			 */
42817c478bd9Sstevel@tonic-gate 			new = kobj_zalloc(sizeof (struct modctl_list),
42827c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
42837c478bd9Sstevel@tonic-gate 			new->modl_modp = req;
42847c478bd9Sstevel@tonic-gate 
42857c478bd9Sstevel@tonic-gate 			if (start == NULL)
42867c478bd9Sstevel@tonic-gate 				mod_garbage = start = new;
42877c478bd9Sstevel@tonic-gate 			else {
42887c478bd9Sstevel@tonic-gate 				mod_garbage->modl_next = new;
42897c478bd9Sstevel@tonic-gate 				mod_garbage = new;
42907c478bd9Sstevel@tonic-gate 			}
42917c478bd9Sstevel@tonic-gate 		}
42927c478bd9Sstevel@tonic-gate 
42937c478bd9Sstevel@tonic-gate 		/* free the list as we go */
42947c478bd9Sstevel@tonic-gate 		kobj_free(modl, sizeof (*modl));
42957c478bd9Sstevel@tonic-gate 	}
42967c478bd9Sstevel@tonic-gate 	modp->mod_requisites = NULL;
42977c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
42987c478bd9Sstevel@tonic-gate 
42997c478bd9Sstevel@tonic-gate 	/*
43007c478bd9Sstevel@tonic-gate 	 * Unload the garbage modules.
43017c478bd9Sstevel@tonic-gate 	 */
43027c478bd9Sstevel@tonic-gate 	for (mod_garbage = start; mod_garbage != NULL; /* nothing */) {
43037c478bd9Sstevel@tonic-gate 		struct modctl_list *old = mod_garbage;
43047c478bd9Sstevel@tonic-gate 		struct modctl *mp = mod_garbage->modl_modp;
43057c478bd9Sstevel@tonic-gate 		ASSERT(mp != NULL);
43067c478bd9Sstevel@tonic-gate 
43077c478bd9Sstevel@tonic-gate 		/*
43087c478bd9Sstevel@tonic-gate 		 * Hold this module until it's unloaded completely.
43097c478bd9Sstevel@tonic-gate 		 */
43107c478bd9Sstevel@tonic-gate 		(void) mod_hold_by_modctl(mp,
43117c478bd9Sstevel@tonic-gate 		    MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
43127c478bd9Sstevel@tonic-gate 		/*
43137c478bd9Sstevel@tonic-gate 		 * Check if the module is not unloaded yet and nobody requires
43147c478bd9Sstevel@tonic-gate 		 * the module. If it's unloaded already or somebody still
43157c478bd9Sstevel@tonic-gate 		 * requires the module, don't unload it now.
43167c478bd9Sstevel@tonic-gate 		 */
43177c478bd9Sstevel@tonic-gate 		if (mp->mod_loaded && mp->mod_ref == 0)
43187c478bd9Sstevel@tonic-gate 			mod_unload(mp);
43197c478bd9Sstevel@tonic-gate 		ASSERT((mp->mod_loaded == 0 && mp->mod_delay_unload == 0) ||
43207c478bd9Sstevel@tonic-gate 		    (mp->mod_ref > 0));
43217c478bd9Sstevel@tonic-gate 		mod_release_mod(mp);
43227c478bd9Sstevel@tonic-gate 
43237c478bd9Sstevel@tonic-gate 		mod_garbage = mod_garbage->modl_next;
43247c478bd9Sstevel@tonic-gate 		kobj_free(old, sizeof (struct modctl_list));
43257c478bd9Sstevel@tonic-gate 	}
43267c478bd9Sstevel@tonic-gate }
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate /*
43297c478bd9Sstevel@tonic-gate  * Process dependency of the module represented by "dep" on the
43307c478bd9Sstevel@tonic-gate  * module named by "on."
43317c478bd9Sstevel@tonic-gate  *
43327c478bd9Sstevel@tonic-gate  * Called from kobj_do_dependents() to load a module "on" on which
43337c478bd9Sstevel@tonic-gate  * "dep" depends.
43347c478bd9Sstevel@tonic-gate  */
43357c478bd9Sstevel@tonic-gate struct modctl *
43367c478bd9Sstevel@tonic-gate mod_load_requisite(struct modctl *dep, char *on)
43377c478bd9Sstevel@tonic-gate {
43387c478bd9Sstevel@tonic-gate 	struct modctl *on_mod;
43397c478bd9Sstevel@tonic-gate 	int retval;
43407c478bd9Sstevel@tonic-gate 
43417c478bd9Sstevel@tonic-gate 	if ((on_mod = mod_hold_loaded_mod(dep, on, &retval)) != NULL) {
43427c478bd9Sstevel@tonic-gate 		mod_make_requisite(dep, on_mod);
43437c478bd9Sstevel@tonic-gate 	} else if (moddebug & MODDEBUG_ERRMSG) {
43447c478bd9Sstevel@tonic-gate 		printf("error processing %s on which module %s depends\n",
43457c478bd9Sstevel@tonic-gate 		    on, dep->mod_modname);
43467c478bd9Sstevel@tonic-gate 	}
43477c478bd9Sstevel@tonic-gate 	return (on_mod);
43487c478bd9Sstevel@tonic-gate }
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate static int
43517c478bd9Sstevel@tonic-gate mod_install_requisites(struct modctl *modp)
43527c478bd9Sstevel@tonic-gate {
43537c478bd9Sstevel@tonic-gate 	struct modctl_list *modl;
43547c478bd9Sstevel@tonic-gate 	struct modctl *req;
43557c478bd9Sstevel@tonic-gate 	int status = 0;
43567c478bd9Sstevel@tonic-gate 
43577c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&mod_lock));
43587c478bd9Sstevel@tonic-gate 	ASSERT(modp->mod_busy);
43597c478bd9Sstevel@tonic-gate 
43607c478bd9Sstevel@tonic-gate 	for (modl = modp->mod_requisites; modl; modl = modl->modl_next) {
43617c478bd9Sstevel@tonic-gate 		req = modl->modl_modp;
43627c478bd9Sstevel@tonic-gate 		(void) mod_hold_by_modctl(req,
43637c478bd9Sstevel@tonic-gate 		    MOD_WAIT_FOREVER | MOD_LOCK_NOT_HELD);
43647c478bd9Sstevel@tonic-gate 		status = modinstall(req);
43657c478bd9Sstevel@tonic-gate 		mod_release_mod(req);
43667c478bd9Sstevel@tonic-gate 
43677c478bd9Sstevel@tonic-gate 		if (status != 0)
43687c478bd9Sstevel@tonic-gate 			break;
43697c478bd9Sstevel@tonic-gate 	}
43707c478bd9Sstevel@tonic-gate 	return (status);
43717c478bd9Sstevel@tonic-gate }
43727c478bd9Sstevel@tonic-gate 
43737c478bd9Sstevel@tonic-gate /*
43747c478bd9Sstevel@tonic-gate  * returns 1 if this thread is doing autounload, 0 otherwise.
43757c478bd9Sstevel@tonic-gate  * see mod_uninstall_all.
43767c478bd9Sstevel@tonic-gate  */
43777c478bd9Sstevel@tonic-gate int
43787c478bd9Sstevel@tonic-gate mod_in_autounload()
43797c478bd9Sstevel@tonic-gate {
43807c478bd9Sstevel@tonic-gate 	return ((int)(uintptr_t)tsd_get(mod_autounload_key));
43817c478bd9Sstevel@tonic-gate }
43827c478bd9Sstevel@tonic-gate 
43837c478bd9Sstevel@tonic-gate /*
43847c478bd9Sstevel@tonic-gate  * gmatch adapted from libc, stripping the wchar stuff
43857c478bd9Sstevel@tonic-gate  */
43861aef0e11Sjg #define	popchar(p, c)	{ \
43877c478bd9Sstevel@tonic-gate 		c = *p++; \
43881aef0e11Sjg 		if (c == 0) { \
43891aef0e11Sjg 			return (0); \
43901aef0e11Sjg 		} \
43911aef0e11Sjg 	}
43927c478bd9Sstevel@tonic-gate 
4393facf4a8dSllai1 int
43947c478bd9Sstevel@tonic-gate gmatch(const char *s, const char *p)
43957c478bd9Sstevel@tonic-gate {
43967c478bd9Sstevel@tonic-gate 	int c, sc;
43977c478bd9Sstevel@tonic-gate 	int ok, lc, notflag;
43987c478bd9Sstevel@tonic-gate 
43997c478bd9Sstevel@tonic-gate 	sc = *s++;
44007c478bd9Sstevel@tonic-gate 	c = *p++;
44017c478bd9Sstevel@tonic-gate 	if (c == 0)
44027c478bd9Sstevel@tonic-gate 		return (sc == c);	/* nothing matches nothing */
44037c478bd9Sstevel@tonic-gate 
44047c478bd9Sstevel@tonic-gate 	switch (c) {
44057c478bd9Sstevel@tonic-gate 	case '\\':
44067c478bd9Sstevel@tonic-gate 		/* skip to quoted character */
44071e1ddd6cScth 		popchar(p, c);
44087c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate 	default:
44117c478bd9Sstevel@tonic-gate 		/* straight comparison */
44127c478bd9Sstevel@tonic-gate 		if (c != sc)
44137c478bd9Sstevel@tonic-gate 			return (0);
44147c478bd9Sstevel@tonic-gate 		/*FALLTHRU*/
44157c478bd9Sstevel@tonic-gate 
44167c478bd9Sstevel@tonic-gate 	case '?':
44177c478bd9Sstevel@tonic-gate 		/* first char matches, move to remainder */
44187c478bd9Sstevel@tonic-gate 		return (sc != '\0' ? gmatch(s, p) : 0);
44197c478bd9Sstevel@tonic-gate 
44207c478bd9Sstevel@tonic-gate 
44217c478bd9Sstevel@tonic-gate 	case '*':
44227c478bd9Sstevel@tonic-gate 		while (*p == '*')
44237c478bd9Sstevel@tonic-gate 			p++;
44247c478bd9Sstevel@tonic-gate 
44257c478bd9Sstevel@tonic-gate 		/* * matches everything */
44267c478bd9Sstevel@tonic-gate 		if (*p == 0)
44277c478bd9Sstevel@tonic-gate 			return (1);
44287c478bd9Sstevel@tonic-gate 
44297c478bd9Sstevel@tonic-gate 		/* undo skip at the beginning & iterate over substrings */
44307c478bd9Sstevel@tonic-gate 		--s;
44317c478bd9Sstevel@tonic-gate 		while (*s) {
44327c478bd9Sstevel@tonic-gate 			if (gmatch(s, p))
44337c478bd9Sstevel@tonic-gate 				return (1);
44347c478bd9Sstevel@tonic-gate 			s++;
44357c478bd9Sstevel@tonic-gate 		}
44367c478bd9Sstevel@tonic-gate 		return (0);
44377c478bd9Sstevel@tonic-gate 
44387c478bd9Sstevel@tonic-gate 	case '[':
44397c478bd9Sstevel@tonic-gate 		/* match any char within [] */
44407c478bd9Sstevel@tonic-gate 		if (sc == 0)
44417c478bd9Sstevel@tonic-gate 			return (0);
44427c478bd9Sstevel@tonic-gate 
44437c478bd9Sstevel@tonic-gate 		ok = lc = notflag = 0;
44447c478bd9Sstevel@tonic-gate 
44457c478bd9Sstevel@tonic-gate 		if (*p == '!') {
44467c478bd9Sstevel@tonic-gate 			notflag = 1;
44477c478bd9Sstevel@tonic-gate 			p++;
44487c478bd9Sstevel@tonic-gate 		}
44491e1ddd6cScth 		popchar(p, c);
44507c478bd9Sstevel@tonic-gate 
44517c478bd9Sstevel@tonic-gate 		do {
44527c478bd9Sstevel@tonic-gate 			if (c == '-' && lc && *p != ']') {
44537c478bd9Sstevel@tonic-gate 				/* test sc against range [c1-c2] */
44541e1ddd6cScth 				popchar(p, c);
44557c478bd9Sstevel@tonic-gate 				if (c == '\\') {
44561e1ddd6cScth 					popchar(p, c);
44577c478bd9Sstevel@tonic-gate 				}
44587c478bd9Sstevel@tonic-gate 
44597c478bd9Sstevel@tonic-gate 				if (notflag) {
44607c478bd9Sstevel@tonic-gate 					/* return 0 on mismatch */
44617c478bd9Sstevel@tonic-gate 					if (lc <= sc && sc <= c)
44627c478bd9Sstevel@tonic-gate 						return (0);
44637c478bd9Sstevel@tonic-gate 					ok++;
44647c478bd9Sstevel@tonic-gate 				} else if (lc <= sc && sc <= c) {
44657c478bd9Sstevel@tonic-gate 					ok++;
44667c478bd9Sstevel@tonic-gate 				}
44677c478bd9Sstevel@tonic-gate 				/* keep going, may get a match next */
44687c478bd9Sstevel@tonic-gate 			} else if (c == '\\') {
44697c478bd9Sstevel@tonic-gate 				/* skip to quoted character */
44701e1ddd6cScth 				popchar(p, c);
44717c478bd9Sstevel@tonic-gate 			}
44727c478bd9Sstevel@tonic-gate 			lc = c;
44737c478bd9Sstevel@tonic-gate 			if (notflag) {
44747c478bd9Sstevel@tonic-gate 				if (sc == lc)
44757c478bd9Sstevel@tonic-gate 					return (0);
44767c478bd9Sstevel@tonic-gate 				ok++;
44777c478bd9Sstevel@tonic-gate 			} else if (sc == lc) {
44787c478bd9Sstevel@tonic-gate 				ok++;
44797c478bd9Sstevel@tonic-gate 			}
44801e1ddd6cScth 			popchar(p, c);
44817c478bd9Sstevel@tonic-gate 		} while (c != ']');
44827c478bd9Sstevel@tonic-gate 
44837c478bd9Sstevel@tonic-gate 		/* recurse on remainder of string */
44847c478bd9Sstevel@tonic-gate 		return (ok ? gmatch(s, p) : 0);
44857c478bd9Sstevel@tonic-gate 	}
44867c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
44877c478bd9Sstevel@tonic-gate }
44887c478bd9Sstevel@tonic-gate 
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate /*
44917c478bd9Sstevel@tonic-gate  * Get default perm for device from /etc/minor_perm. Return 0 if match found.
44927c478bd9Sstevel@tonic-gate  *
44937c478bd9Sstevel@tonic-gate  * Pure wild-carded patterns are handled separately so the ordering of
44947c478bd9Sstevel@tonic-gate  * these patterns doesn't matter.  We're still dependent on ordering
44957c478bd9Sstevel@tonic-gate  * however as the first matching entry is the one returned.
44967c478bd9Sstevel@tonic-gate  * Not ideal but all existing examples and usage do imply this
44977c478bd9Sstevel@tonic-gate  * ordering implicitly.
44987c478bd9Sstevel@tonic-gate  *
44997c478bd9Sstevel@tonic-gate  * Drivers using the clone driver are always good for some entertainment.
45007c478bd9Sstevel@tonic-gate  * Clone nodes under pseudo have the form clone@0:<driver>.  Some minor
45017c478bd9Sstevel@tonic-gate  * perm entries have the form clone:<driver>, others use <driver>:*
45027c478bd9Sstevel@tonic-gate  * Examples are clone:llc1 vs. llc2:*, for example.
45037c478bd9Sstevel@tonic-gate  *
45047c478bd9Sstevel@tonic-gate  * Minor perms in the clone:<driver> form are mapped to the drivers's
45057c478bd9Sstevel@tonic-gate  * mperm list, not the clone driver, as wildcard entries for clone
45067c478bd9Sstevel@tonic-gate  * reference only.  In other words, a clone wildcard will match
45077c478bd9Sstevel@tonic-gate  * references for clone@0:<driver> but never <driver>@<minor>.
45087c478bd9Sstevel@tonic-gate  *
45097c478bd9Sstevel@tonic-gate  * Additional minor perms in the standard form are also supported,
45107c478bd9Sstevel@tonic-gate  * for mixed usage, ie a node with an entry clone:<driver> could
45117c478bd9Sstevel@tonic-gate  * provide further entries <driver>:<minor>.
45127c478bd9Sstevel@tonic-gate  *
45137c478bd9Sstevel@tonic-gate  * Finally, some uses of clone use an alias as the minor name rather
45147c478bd9Sstevel@tonic-gate  * than the driver name, with the alias as the minor perm entry.
45157c478bd9Sstevel@tonic-gate  * This case is handled by attaching the driver to bring its
45167c478bd9Sstevel@tonic-gate  * minor list into existence, then discover the alias via DDI_ALIAS.
45177c478bd9Sstevel@tonic-gate  * The clone device's minor perm list can then be searched for
45187c478bd9Sstevel@tonic-gate  * that alias.
45197c478bd9Sstevel@tonic-gate  */
45207c478bd9Sstevel@tonic-gate 
45217c478bd9Sstevel@tonic-gate static int
45227c478bd9Sstevel@tonic-gate dev_alias_minorperm(dev_info_t *dip, char *minor_name, mperm_t *rmp)
45237c478bd9Sstevel@tonic-gate {
45247c478bd9Sstevel@tonic-gate 	major_t			major;
45257c478bd9Sstevel@tonic-gate 	struct devnames		*dnp;
45267c478bd9Sstevel@tonic-gate 	mperm_t			*mp;
45277c478bd9Sstevel@tonic-gate 	char			*alias = NULL;
45287c478bd9Sstevel@tonic-gate 	dev_info_t		*cdevi;
4529b9ccdc5aScth 	int			circ;
45307c478bd9Sstevel@tonic-gate 	struct ddi_minor_data	*dmd;
45317c478bd9Sstevel@tonic-gate 
45327c478bd9Sstevel@tonic-gate 	major = ddi_name_to_major(minor_name);
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate 	ASSERT(dip == clone_dip);
4535a204de77Scth 	ASSERT(major != DDI_MAJOR_T_NONE);
45367c478bd9Sstevel@tonic-gate 
45377c478bd9Sstevel@tonic-gate 	/*
45387c478bd9Sstevel@tonic-gate 	 * Attach the driver named by the minor node, then
45397c478bd9Sstevel@tonic-gate 	 * search its first instance's minor list for an
45407c478bd9Sstevel@tonic-gate 	 * alias node.
45417c478bd9Sstevel@tonic-gate 	 */
45427c478bd9Sstevel@tonic-gate 	if (ddi_hold_installed_driver(major) == NULL)
45437c478bd9Sstevel@tonic-gate 		return (1);
45447c478bd9Sstevel@tonic-gate 
45457c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
45467c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
45477c478bd9Sstevel@tonic-gate 
45487c478bd9Sstevel@tonic-gate 	if ((cdevi = dnp->dn_head) != NULL) {
4549b9ccdc5aScth 		ndi_devi_enter(cdevi, &circ);
45507c478bd9Sstevel@tonic-gate 		for (dmd = DEVI(cdevi)->devi_minor; dmd; dmd = dmd->next) {
45517c478bd9Sstevel@tonic-gate 			if (dmd->type == DDM_ALIAS) {
45527c478bd9Sstevel@tonic-gate 				alias = i_ddi_strdup(dmd->ddm_name, KM_SLEEP);
45537c478bd9Sstevel@tonic-gate 				break;
45547c478bd9Sstevel@tonic-gate 			}
45557c478bd9Sstevel@tonic-gate 		}
4556b9ccdc5aScth 		ndi_devi_exit(cdevi, circ);
45577c478bd9Sstevel@tonic-gate 	}
45587c478bd9Sstevel@tonic-gate 
45597c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
45607c478bd9Sstevel@tonic-gate 	ddi_rele_driver(major);
45617c478bd9Sstevel@tonic-gate 
45627c478bd9Sstevel@tonic-gate 	if (alias == NULL) {
45637c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM)
45647c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT, "dev_minorperm: "
45657c478bd9Sstevel@tonic-gate 			    "no alias for %s\n", minor_name);
45667c478bd9Sstevel@tonic-gate 		return (1);
45677c478bd9Sstevel@tonic-gate 	}
45687c478bd9Sstevel@tonic-gate 
45697c478bd9Sstevel@tonic-gate 	major = ddi_driver_major(clone_dip);
45707c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
45717c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
45727c478bd9Sstevel@tonic-gate 
45737c478bd9Sstevel@tonic-gate 	/*
45747c478bd9Sstevel@tonic-gate 	 * Go through the clone driver's mperm list looking
45757c478bd9Sstevel@tonic-gate 	 * for a match for the specified alias.
45767c478bd9Sstevel@tonic-gate 	 */
45777c478bd9Sstevel@tonic-gate 	for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
45787c478bd9Sstevel@tonic-gate 		if (strcmp(alias, mp->mp_minorname) == 0) {
45797c478bd9Sstevel@tonic-gate 			break;
45807c478bd9Sstevel@tonic-gate 		}
45817c478bd9Sstevel@tonic-gate 	}
45827c478bd9Sstevel@tonic-gate 
45837c478bd9Sstevel@tonic-gate 	if (mp) {
45847c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MP_MATCH) {
45857c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
45867c478bd9Sstevel@tonic-gate 			    "minor perm defaults: %s %s 0%o %d %d (aliased)\n",
45877c478bd9Sstevel@tonic-gate 			    minor_name, alias, mp->mp_mode,
45887c478bd9Sstevel@tonic-gate 			    mp->mp_uid, mp->mp_gid);
45897c478bd9Sstevel@tonic-gate 		}
45907c478bd9Sstevel@tonic-gate 		rmp->mp_uid = mp->mp_uid;
45917c478bd9Sstevel@tonic-gate 		rmp->mp_gid = mp->mp_gid;
45927c478bd9Sstevel@tonic-gate 		rmp->mp_mode = mp->mp_mode;
45937c478bd9Sstevel@tonic-gate 	}
45947c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
45957c478bd9Sstevel@tonic-gate 
45967c478bd9Sstevel@tonic-gate 	kmem_free(alias, strlen(alias)+1);
45977c478bd9Sstevel@tonic-gate 
45987c478bd9Sstevel@tonic-gate 	return (mp == NULL);
45997c478bd9Sstevel@tonic-gate }
46007c478bd9Sstevel@tonic-gate 
46017c478bd9Sstevel@tonic-gate int
46027c478bd9Sstevel@tonic-gate dev_minorperm(dev_info_t *dip, char *name, mperm_t *rmp)
46037c478bd9Sstevel@tonic-gate {
46047c478bd9Sstevel@tonic-gate 	major_t major;
46057c478bd9Sstevel@tonic-gate 	char *minor_name;
46067c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
46077c478bd9Sstevel@tonic-gate 	mperm_t *mp;
46087c478bd9Sstevel@tonic-gate 	int is_clone = 0;
46097c478bd9Sstevel@tonic-gate 
46107c478bd9Sstevel@tonic-gate 	if (!minorperm_loaded) {
46117c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MINORPERM)
46127c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
46137c478bd9Sstevel@tonic-gate 			    "%s: minor perm not yet loaded\n", name);
46147c478bd9Sstevel@tonic-gate 		return (1);
46157c478bd9Sstevel@tonic-gate 	}
46167c478bd9Sstevel@tonic-gate 
46177c478bd9Sstevel@tonic-gate 	minor_name = strchr(name, ':');
46187c478bd9Sstevel@tonic-gate 	if (minor_name == NULL)
46197c478bd9Sstevel@tonic-gate 		return (1);
46207c478bd9Sstevel@tonic-gate 	minor_name++;
46217c478bd9Sstevel@tonic-gate 
46227c478bd9Sstevel@tonic-gate 	/*
46237c478bd9Sstevel@tonic-gate 	 * If it's the clone driver, search the driver as named
46247c478bd9Sstevel@tonic-gate 	 * by the minor.  All clone minor perm entries other than
46257c478bd9Sstevel@tonic-gate 	 * alias nodes are actually installed on the real driver's list.
46267c478bd9Sstevel@tonic-gate 	 */
46277c478bd9Sstevel@tonic-gate 	if (dip == clone_dip) {
46287c478bd9Sstevel@tonic-gate 		major = ddi_name_to_major(minor_name);
4629a204de77Scth 		if (major == DDI_MAJOR_T_NONE) {
46307c478bd9Sstevel@tonic-gate 			if (moddebug & MODDEBUG_MINORPERM)
46317c478bd9Sstevel@tonic-gate 				cmn_err(CE_CONT, "dev_minorperm: "
46327c478bd9Sstevel@tonic-gate 				    "%s: no such driver\n", minor_name);
46337c478bd9Sstevel@tonic-gate 			return (1);
46347c478bd9Sstevel@tonic-gate 		}
46357c478bd9Sstevel@tonic-gate 		is_clone = 1;
46367c478bd9Sstevel@tonic-gate 	} else {
46377c478bd9Sstevel@tonic-gate 		major = ddi_driver_major(dip);
4638a204de77Scth 		ASSERT(major != DDI_MAJOR_T_NONE);
46397c478bd9Sstevel@tonic-gate 	}
46407c478bd9Sstevel@tonic-gate 
46417c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
46427c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
46437c478bd9Sstevel@tonic-gate 
46447c478bd9Sstevel@tonic-gate 	/*
46457c478bd9Sstevel@tonic-gate 	 * Go through the driver's mperm list looking for
46467c478bd9Sstevel@tonic-gate 	 * a match for the specified minor.  If there's
46477c478bd9Sstevel@tonic-gate 	 * no matching pattern, use the wild card.
46487c478bd9Sstevel@tonic-gate 	 * Defer to the clone wild for clone if specified,
46497c478bd9Sstevel@tonic-gate 	 * otherwise fall back to the normal form.
46507c478bd9Sstevel@tonic-gate 	 */
46517c478bd9Sstevel@tonic-gate 	for (mp = dnp->dn_mperm; mp; mp = mp->mp_next) {
46527c478bd9Sstevel@tonic-gate 		if (gmatch(minor_name, mp->mp_minorname) != 0) {
46537c478bd9Sstevel@tonic-gate 			break;
46547c478bd9Sstevel@tonic-gate 		}
46557c478bd9Sstevel@tonic-gate 	}
46567c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
46577c478bd9Sstevel@tonic-gate 		if (is_clone)
46587c478bd9Sstevel@tonic-gate 			mp = dnp->dn_mperm_clone;
46597c478bd9Sstevel@tonic-gate 		if (mp == NULL)
46607c478bd9Sstevel@tonic-gate 			mp = dnp->dn_mperm_wild;
46617c478bd9Sstevel@tonic-gate 	}
46627c478bd9Sstevel@tonic-gate 
46637c478bd9Sstevel@tonic-gate 	if (mp) {
46647c478bd9Sstevel@tonic-gate 		if (moddebug & MODDEBUG_MP_MATCH) {
46657c478bd9Sstevel@tonic-gate 			cmn_err(CE_CONT,
46667c478bd9Sstevel@tonic-gate 			    "minor perm defaults: %s %s 0%o %d %d\n",
46677c478bd9Sstevel@tonic-gate 			    name, mp->mp_minorname, mp->mp_mode,
46687c478bd9Sstevel@tonic-gate 			    mp->mp_uid, mp->mp_gid);
46697c478bd9Sstevel@tonic-gate 		}
46707c478bd9Sstevel@tonic-gate 		rmp->mp_uid = mp->mp_uid;
46717c478bd9Sstevel@tonic-gate 		rmp->mp_gid = mp->mp_gid;
46727c478bd9Sstevel@tonic-gate 		rmp->mp_mode = mp->mp_mode;
46737c478bd9Sstevel@tonic-gate 	}
46747c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
46757c478bd9Sstevel@tonic-gate 
46767c478bd9Sstevel@tonic-gate 	/*
46777c478bd9Sstevel@tonic-gate 	 * If no match can be found for a clone node,
46787c478bd9Sstevel@tonic-gate 	 * search for a possible match for an alias.
46797c478bd9Sstevel@tonic-gate 	 * One such example is /dev/ptmx -> /devices/pseudo/clone@0:ptm,
46807c478bd9Sstevel@tonic-gate 	 * with minor perm entry clone:ptmx.
46817c478bd9Sstevel@tonic-gate 	 */
46827c478bd9Sstevel@tonic-gate 	if (mp == NULL && is_clone) {
46837c478bd9Sstevel@tonic-gate 		return (dev_alias_minorperm(dip, minor_name, rmp));
46847c478bd9Sstevel@tonic-gate 	}
46857c478bd9Sstevel@tonic-gate 
46867c478bd9Sstevel@tonic-gate 	return (mp == NULL);
46877c478bd9Sstevel@tonic-gate }
46887c478bd9Sstevel@tonic-gate 
46897c478bd9Sstevel@tonic-gate /*
46907c478bd9Sstevel@tonic-gate  * dynamicaly reference load a dl module/library, returning handle
46917c478bd9Sstevel@tonic-gate  */
46927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
46937c478bd9Sstevel@tonic-gate ddi_modhandle_t
46947c478bd9Sstevel@tonic-gate ddi_modopen(const char *modname, int mode, int *errnop)
46957c478bd9Sstevel@tonic-gate {
46967c478bd9Sstevel@tonic-gate 	char		*subdir;
46977c478bd9Sstevel@tonic-gate 	char		*mod;
46987c478bd9Sstevel@tonic-gate 	int		subdirlen;
46997c478bd9Sstevel@tonic-gate 	struct modctl	*hmodp = NULL;
47007c478bd9Sstevel@tonic-gate 	int		retval = EINVAL;
47017c478bd9Sstevel@tonic-gate 
47027c478bd9Sstevel@tonic-gate 	ASSERT(modname && (mode == KRTLD_MODE_FIRST));
47037c478bd9Sstevel@tonic-gate 	if ((modname == NULL) || (mode != KRTLD_MODE_FIRST))
47047c478bd9Sstevel@tonic-gate 		goto out;
47057c478bd9Sstevel@tonic-gate 
47061e1ddd6cScth 	/* find last '/' in modname */
47071e1ddd6cScth 	mod = strrchr(modname, '/');
47087c478bd9Sstevel@tonic-gate 
47097c478bd9Sstevel@tonic-gate 	if (mod) {
47107c478bd9Sstevel@tonic-gate 		/* for subdir string without modification to argument */
47117c478bd9Sstevel@tonic-gate 		mod++;
47127c478bd9Sstevel@tonic-gate 		subdirlen = mod - modname;
47137c478bd9Sstevel@tonic-gate 		subdir = kmem_alloc(subdirlen, KM_SLEEP);
47147c478bd9Sstevel@tonic-gate 		(void) strlcpy(subdir, modname, subdirlen);
47157c478bd9Sstevel@tonic-gate 	} else {
47167c478bd9Sstevel@tonic-gate 		subdirlen = 0;
47177c478bd9Sstevel@tonic-gate 		subdir = "misc";
47187c478bd9Sstevel@tonic-gate 		mod = (char *)modname;
47197c478bd9Sstevel@tonic-gate 	}
47207c478bd9Sstevel@tonic-gate 
47217c478bd9Sstevel@tonic-gate 	/* reference load with errno return value */
47227c478bd9Sstevel@tonic-gate 	retval = modrload(subdir, mod, &hmodp);
47237c478bd9Sstevel@tonic-gate 
47247c478bd9Sstevel@tonic-gate 	if (subdirlen)
47257c478bd9Sstevel@tonic-gate 		kmem_free(subdir, subdirlen);
47267c478bd9Sstevel@tonic-gate 
47277c478bd9Sstevel@tonic-gate out:	if (errnop)
47287c478bd9Sstevel@tonic-gate 		*errnop = retval;
47297c478bd9Sstevel@tonic-gate 
47307c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_DDI_MOD)
47317c478bd9Sstevel@tonic-gate 		printf("ddi_modopen %s mode %x: %s %p %d\n",
47327c478bd9Sstevel@tonic-gate 		    modname ? modname : "<unknown>", mode,
47337c478bd9Sstevel@tonic-gate 		    hmodp ? hmodp->mod_filename : "<unknown>",
47347c478bd9Sstevel@tonic-gate 		    (void *)hmodp, retval);
47357c478bd9Sstevel@tonic-gate 
47367c478bd9Sstevel@tonic-gate 	return ((ddi_modhandle_t)hmodp);
47377c478bd9Sstevel@tonic-gate }
47387c478bd9Sstevel@tonic-gate 
47397c478bd9Sstevel@tonic-gate /* lookup "name" in open dl module/library */
47407c478bd9Sstevel@tonic-gate void *
47417c478bd9Sstevel@tonic-gate ddi_modsym(ddi_modhandle_t h, const char *name, int *errnop)
47427c478bd9Sstevel@tonic-gate {
47437c478bd9Sstevel@tonic-gate 	struct modctl	*hmodp = (struct modctl *)h;
47447c478bd9Sstevel@tonic-gate 	void		*f;
47457c478bd9Sstevel@tonic-gate 	int		retval;
47467c478bd9Sstevel@tonic-gate 
47477c478bd9Sstevel@tonic-gate 	ASSERT(hmodp && name && hmodp->mod_installed && (hmodp->mod_ref >= 1));
47487c478bd9Sstevel@tonic-gate 	if ((hmodp == NULL) || (name == NULL) ||
47497c478bd9Sstevel@tonic-gate 	    (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
47507c478bd9Sstevel@tonic-gate 		f = NULL;
47517c478bd9Sstevel@tonic-gate 		retval = EINVAL;
47527c478bd9Sstevel@tonic-gate 	} else {
47537c478bd9Sstevel@tonic-gate 		f = (void *)kobj_lookup(hmodp->mod_mp, (char *)name);
47547c478bd9Sstevel@tonic-gate 		if (f)
47557c478bd9Sstevel@tonic-gate 			retval = 0;
47567c478bd9Sstevel@tonic-gate 		else
47577c478bd9Sstevel@tonic-gate 			retval = ENOTSUP;
47587c478bd9Sstevel@tonic-gate 	}
47597c478bd9Sstevel@tonic-gate 
47607c478bd9Sstevel@tonic-gate 	if (moddebug & MODDEBUG_DDI_MOD)
47617c478bd9Sstevel@tonic-gate 		printf("ddi_modsym in %s of %s: %d %p\n",
47627c478bd9Sstevel@tonic-gate 		    hmodp ? hmodp->mod_modname : "<unknown>",
47637c478bd9Sstevel@tonic-gate 		    name ? name : "<unknown>", retval, f);
47647c478bd9Sstevel@tonic-gate 
47657c478bd9Sstevel@tonic-gate 	if (errnop)
47667c478bd9Sstevel@tonic-gate 		*errnop = retval;
47677c478bd9Sstevel@tonic-gate 	return (f);
47687c478bd9Sstevel@tonic-gate }
47697c478bd9Sstevel@tonic-gate 
47707c478bd9Sstevel@tonic-gate /* dynamic (un)reference unload of an open dl module/library */
47717c478bd9Sstevel@tonic-gate int
47727c478bd9Sstevel@tonic-gate ddi_modclose(ddi_modhandle_t h)
47737c478bd9Sstevel@tonic-gate {
47747c478bd9Sstevel@tonic-gate 	struct modctl	*hmodp = (struct modctl *)h;
47757c478bd9Sstevel@tonic-gate 	struct modctl	*modp = NULL;
47767c478bd9Sstevel@tonic-gate 	int		retval;
47777c478bd9Sstevel@tonic-gate 
47787c478bd9Sstevel@tonic-gate 	ASSERT(hmodp && hmodp->mod_installed && (hmodp->mod_ref >= 1));
47797c478bd9Sstevel@tonic-gate 	if ((hmodp == NULL) ||
47807c478bd9Sstevel@tonic-gate 	    (hmodp->mod_installed == 0) || (hmodp->mod_ref < 1)) {
47817c478bd9Sstevel@tonic-gate 		retval = EINVAL;
47827c478bd9Sstevel@tonic-gate 		goto out;
47837c478bd9Sstevel@tonic-gate 	}
47847c478bd9Sstevel@tonic-gate 
47857c478bd9Sstevel@tonic-gate 	retval = modunrload(hmodp->mod_id, &modp, ddi_modclose_unload);
47867c478bd9Sstevel@tonic-gate 	if (retval == EBUSY)
47877c478bd9Sstevel@tonic-gate 		retval = 0;	/* EBUSY is not an error */
47887c478bd9Sstevel@tonic-gate 
47897c478bd9Sstevel@tonic-gate 	if (retval == 0) {
47907c478bd9Sstevel@tonic-gate 		ASSERT(hmodp == modp);
47917c478bd9Sstevel@tonic-gate 		if (hmodp != modp)
47927c478bd9Sstevel@tonic-gate 			retval = EINVAL;
47937c478bd9Sstevel@tonic-gate 	}
47947c478bd9Sstevel@tonic-gate 
47957c478bd9Sstevel@tonic-gate out:	if (moddebug & MODDEBUG_DDI_MOD)
47967c478bd9Sstevel@tonic-gate 		printf("ddi_modclose %s: %d\n",
47977c478bd9Sstevel@tonic-gate 		    hmodp ? hmodp->mod_modname : "<unknown>", retval);
47987c478bd9Sstevel@tonic-gate 
47997c478bd9Sstevel@tonic-gate 	return (retval);
48007c478bd9Sstevel@tonic-gate }
4801