xref: /onnv-gate/usr/src/uts/common/fs/devfs/devfs_subr.c (revision 12569:06d420824c4b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52621Sllai1  * Common Development and Distribution License (the "License").
62621Sllai1  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212116SVikram.Hegde@Sun.COM  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * miscellaneous routines for the devfs
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/t_lock.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate #include <sys/user.h>
350Sstevel@tonic-gate #include <sys/time.h>
360Sstevel@tonic-gate #include <sys/vfs.h>
370Sstevel@tonic-gate #include <sys/vnode.h>
380Sstevel@tonic-gate #include <sys/file.h>
390Sstevel@tonic-gate #include <sys/fcntl.h>
400Sstevel@tonic-gate #include <sys/flock.h>
410Sstevel@tonic-gate #include <sys/kmem.h>
420Sstevel@tonic-gate #include <sys/uio.h>
430Sstevel@tonic-gate #include <sys/errno.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate #include <sys/cred.h>
460Sstevel@tonic-gate #include <sys/dirent.h>
470Sstevel@tonic-gate #include <sys/pathname.h>
480Sstevel@tonic-gate #include <sys/cmn_err.h>
490Sstevel@tonic-gate #include <sys/debug.h>
500Sstevel@tonic-gate #include <sys/modctl.h>
510Sstevel@tonic-gate #include <fs/fs_subr.h>
520Sstevel@tonic-gate #include <sys/fs/dv_node.h>
530Sstevel@tonic-gate #include <sys/fs/snode.h>
540Sstevel@tonic-gate #include <sys/sunndi.h>
550Sstevel@tonic-gate #include <sys/sunmdi.h>
560Sstevel@tonic-gate #include <sys/conf.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #ifdef DEBUG
590Sstevel@tonic-gate int devfs_debug = 0x0;
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate 
620Sstevel@tonic-gate const char	dvnm[] = "devfs";
630Sstevel@tonic-gate kmem_cache_t	*dv_node_cache;	/* dv_node cache */
644895Sdf125853 
654895Sdf125853 /*
664895Sdf125853  * The devfs_clean_key is taken during a devfs_clean operation: it is used to
674895Sdf125853  * prevent unnecessary code execution and for detection of potential deadlocks.
684895Sdf125853  */
690Sstevel@tonic-gate uint_t		devfs_clean_key;
704895Sdf125853 
710Sstevel@tonic-gate struct dv_node *dvroot;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* prototype memory vattrs */
740Sstevel@tonic-gate vattr_t dv_vattr_dir = {
750Sstevel@tonic-gate 	AT_TYPE|AT_MODE|AT_UID|AT_GID, 		/* va_mask */
760Sstevel@tonic-gate 	VDIR,					/* va_type */
770Sstevel@tonic-gate 	DV_DIRMODE_DEFAULT,			/* va_mode */
780Sstevel@tonic-gate 	DV_UID_DEFAULT,				/* va_uid */
790Sstevel@tonic-gate 	DV_GID_DEFAULT,				/* va_gid */
800Sstevel@tonic-gate 	0,					/* va_fsid; */
810Sstevel@tonic-gate 	0,					/* va_nodeid; */
820Sstevel@tonic-gate 	0,					/* va_nlink; */
830Sstevel@tonic-gate 	0,					/* va_size; */
840Sstevel@tonic-gate 	0,					/* va_atime; */
850Sstevel@tonic-gate 	0,					/* va_mtime; */
860Sstevel@tonic-gate 	0,					/* va_ctime; */
870Sstevel@tonic-gate 	0,					/* va_rdev; */
880Sstevel@tonic-gate 	0,					/* va_blksize; */
890Sstevel@tonic-gate 	0,					/* va_nblocks; */
900Sstevel@tonic-gate 	0,					/* va_seq; */
910Sstevel@tonic-gate };
920Sstevel@tonic-gate 
930Sstevel@tonic-gate vattr_t dv_vattr_file = {
940Sstevel@tonic-gate 	AT_TYPE|AT_MODE|AT_SIZE|AT_UID|AT_GID|AT_RDEV,	/* va_mask */
950Sstevel@tonic-gate 	0,					/* va_type */
960Sstevel@tonic-gate 	DV_DEVMODE_DEFAULT,			/* va_mode */
970Sstevel@tonic-gate 	DV_UID_DEFAULT,				/* va_uid */
980Sstevel@tonic-gate 	DV_GID_DEFAULT,				/* va_gid */
990Sstevel@tonic-gate 	0,					/* va_fsid; */
1000Sstevel@tonic-gate 	0,					/* va_nodeid; */
1010Sstevel@tonic-gate 	0,					/* va_nlink; */
1020Sstevel@tonic-gate 	0,					/* va_size; */
1030Sstevel@tonic-gate 	0,					/* va_atime; */
1040Sstevel@tonic-gate 	0,					/* va_mtime; */
1050Sstevel@tonic-gate 	0,					/* va_ctime; */
1060Sstevel@tonic-gate 	0,					/* va_rdev; */
1070Sstevel@tonic-gate 	0,					/* va_blksize; */
1080Sstevel@tonic-gate 	0,					/* va_nblocks; */
1090Sstevel@tonic-gate 	0,					/* va_seq; */
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate vattr_t dv_vattr_priv = {
1130Sstevel@tonic-gate 	AT_TYPE|AT_MODE|AT_SIZE|AT_UID|AT_GID|AT_RDEV,	/* va_mask */
1140Sstevel@tonic-gate 	0,					/* va_type */
1150Sstevel@tonic-gate 	DV_DEVMODE_PRIV,			/* va_mode */
1160Sstevel@tonic-gate 	DV_UID_DEFAULT,				/* va_uid */
1170Sstevel@tonic-gate 	DV_GID_DEFAULT,				/* va_gid */
1180Sstevel@tonic-gate 	0,					/* va_fsid; */
1190Sstevel@tonic-gate 	0,					/* va_nodeid; */
1200Sstevel@tonic-gate 	0,					/* va_nlink; */
1210Sstevel@tonic-gate 	0,					/* va_size; */
1220Sstevel@tonic-gate 	0,					/* va_atime; */
1230Sstevel@tonic-gate 	0,					/* va_mtime; */
1240Sstevel@tonic-gate 	0,					/* va_ctime; */
1250Sstevel@tonic-gate 	0,					/* va_rdev; */
1260Sstevel@tonic-gate 	0,					/* va_blksize; */
1270Sstevel@tonic-gate 	0,					/* va_nblocks; */
1280Sstevel@tonic-gate 	0,					/* va_seq; */
1290Sstevel@tonic-gate };
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate extern dev_info_t	*clone_dip;
1320Sstevel@tonic-gate extern major_t		clone_major;
1330Sstevel@tonic-gate extern struct dev_ops	*ddi_hold_driver(major_t);
1340Sstevel@tonic-gate 
1358912SChris.Horne@Sun.COM /* dv_node node constructor for kmem cache */
1360Sstevel@tonic-gate static int
i_dv_node_ctor(void * buf,void * cfarg,int flag)1370Sstevel@tonic-gate i_dv_node_ctor(void *buf, void *cfarg, int flag)
1380Sstevel@tonic-gate {
1398912SChris.Horne@Sun.COM 	_NOTE(ARGUNUSED(cfarg, flag))
1400Sstevel@tonic-gate 	struct dv_node	*dv = (struct dv_node *)buf;
1410Sstevel@tonic-gate 	struct vnode	*vp;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	bzero(buf, sizeof (struct dv_node));
1446712Stomee 	vp = dv->dv_vnode = vn_alloc(flag);
1456712Stomee 	if (vp == NULL) {
1466712Stomee 		return (-1);
1476712Stomee 	}
1486712Stomee 	vp->v_data = dv;
1490Sstevel@tonic-gate 	rw_init(&dv->dv_contents, NULL, RW_DEFAULT, NULL);
1500Sstevel@tonic-gate 	return (0);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1538912SChris.Horne@Sun.COM /* dv_node node destructor for kmem cache */
1540Sstevel@tonic-gate static void
i_dv_node_dtor(void * buf,void * arg)1550Sstevel@tonic-gate i_dv_node_dtor(void *buf, void *arg)
1560Sstevel@tonic-gate {
1578912SChris.Horne@Sun.COM 	_NOTE(ARGUNUSED(arg))
1580Sstevel@tonic-gate 	struct dv_node	*dv = (struct dv_node *)buf;
1590Sstevel@tonic-gate 	struct vnode	*vp = DVTOV(dv);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	rw_destroy(&dv->dv_contents);
1620Sstevel@tonic-gate 	vn_invalid(vp);
1630Sstevel@tonic-gate 	vn_free(vp);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 
1678912SChris.Horne@Sun.COM /* initialize dv_node node cache */
1680Sstevel@tonic-gate void
dv_node_cache_init()1690Sstevel@tonic-gate dv_node_cache_init()
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	ASSERT(dv_node_cache == NULL);
1720Sstevel@tonic-gate 	dv_node_cache = kmem_cache_create("dv_node_cache",
1730Sstevel@tonic-gate 	    sizeof (struct dv_node), 0, i_dv_node_ctor, i_dv_node_dtor,
1740Sstevel@tonic-gate 	    NULL, NULL, NULL, 0);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	tsd_create(&devfs_clean_key, NULL);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1798912SChris.Horne@Sun.COM /* destroy dv_node node cache */
1800Sstevel@tonic-gate void
dv_node_cache_fini()1810Sstevel@tonic-gate dv_node_cache_fini()
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	ASSERT(dv_node_cache != NULL);
1840Sstevel@tonic-gate 	kmem_cache_destroy(dv_node_cache);
1850Sstevel@tonic-gate 	dv_node_cache = NULL;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	tsd_destroy(&devfs_clean_key);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate  * dv_mkino - Generate a unique inode number for devfs nodes.
1920Sstevel@tonic-gate  *
1930Sstevel@tonic-gate  * Although ino_t is 64 bits, the inode number is truncated to 32 bits for 32
1940Sstevel@tonic-gate  * bit non-LARGEFILE applications. This means that there is a requirement to
1950Sstevel@tonic-gate  * maintain the inode number as a 32 bit value or applications will have
1960Sstevel@tonic-gate  * stat(2) calls fail with EOVERFLOW.  We form a 32 bit inode number from the
1970Sstevel@tonic-gate  * dev_t. but if the minor number is larger than L_MAXMIN32 we fold extra minor
1980Sstevel@tonic-gate  *
1990Sstevel@tonic-gate  * To generate inode numbers for directories, we assume that we will never use
2000Sstevel@tonic-gate  * more than half the major space - this allows for ~8190 drivers. We use this
2010Sstevel@tonic-gate  * upper major number space to allocate inode numbers for directories by
2020Sstevel@tonic-gate  * encoding the major and instance into this space.
2030Sstevel@tonic-gate  *
2040Sstevel@tonic-gate  * We also skew the result so that inode 2 is reserved for the root of the file
2050Sstevel@tonic-gate  * system.
2060Sstevel@tonic-gate  *
2070Sstevel@tonic-gate  * As part of the future support for 64-bit dev_t APIs, the upper minor bits
2080Sstevel@tonic-gate  * should be folded into the high inode bits by adding the following code
2090Sstevel@tonic-gate  * after "ino |= 1":
2100Sstevel@tonic-gate  *
2110Sstevel@tonic-gate  * #if (L_BITSMINOR32 != L_BITSMINOR)
2120Sstevel@tonic-gate  *		|* fold overflow minor bits into high bits of inode number *|
2130Sstevel@tonic-gate  *		ino |= ((ino_t)(minor >> L_BITSMINOR32)) << L_BITSMINOR;
2140Sstevel@tonic-gate  * #endif |* (L_BITSMINOR32 != L_BITSMINOR) *|
2150Sstevel@tonic-gate  *
2160Sstevel@tonic-gate  * This way only applications that use devices that overflow their minor
2170Sstevel@tonic-gate  * space will have an application level impact.
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate static ino_t
dv_mkino(dev_info_t * devi,vtype_t typ,dev_t dev)2200Sstevel@tonic-gate dv_mkino(dev_info_t *devi, vtype_t typ, dev_t dev)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate 	major_t		major;
2230Sstevel@tonic-gate 	minor_t		minor;
2240Sstevel@tonic-gate 	ino_t		ino;
2250Sstevel@tonic-gate 	static int	warn;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (typ == VDIR) {
2280Sstevel@tonic-gate 		major = ((L_MAXMAJ32 + 1) >> 1) + DEVI(devi)->devi_major;
2290Sstevel@tonic-gate 		minor = ddi_get_instance(devi);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 		/* makedevice32 in high half of major number space */
2320Sstevel@tonic-gate 		ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32));
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		major = DEVI(devi)->devi_major;
2350Sstevel@tonic-gate 	} else {
2360Sstevel@tonic-gate 		major = getmajor(dev);
2370Sstevel@tonic-gate 		minor = getminor(dev);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		/* makedevice32 */
2400Sstevel@tonic-gate 		ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32));
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		/* make ino for VCHR different than VBLK */
2430Sstevel@tonic-gate 		ino <<= 1;
2440Sstevel@tonic-gate 		if (typ == VCHR)
2450Sstevel@tonic-gate 			ino |= 1;
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	ino += DV_ROOTINO + 1;		/* skew */
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	/*
2510Sstevel@tonic-gate 	 * diagnose things a little early because adding the skew to a large
2520Sstevel@tonic-gate 	 * minor number could roll over the major.
2530Sstevel@tonic-gate 	 */
2540Sstevel@tonic-gate 	if ((major >= (L_MAXMAJ32 >> 1)) && (warn == 0)) {
2550Sstevel@tonic-gate 		warn = 1;
2560Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s: inode numbers are not unique", dvnm);
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	return (ino);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate /*
2636260Sjg  * Compare two nodes lexographically to balance avl tree
2646260Sjg  */
2656260Sjg static int
dv_compare_nodes(const struct dv_node * dv1,const struct dv_node * dv2)2666260Sjg dv_compare_nodes(const struct dv_node *dv1, const struct dv_node *dv2)
2676260Sjg {
2687224Scth 	int	rv;
2697224Scth 
2706260Sjg 	if ((rv = strcmp(dv1->dv_name, dv2->dv_name)) == 0)
2716260Sjg 		return (0);
2726260Sjg 	return ((rv < 0) ? -1 : 1);
2736260Sjg }
2746260Sjg 
2756260Sjg /*
2760Sstevel@tonic-gate  * dv_mkroot
2770Sstevel@tonic-gate  *
2780Sstevel@tonic-gate  * Build the first VDIR dv_node.
2790Sstevel@tonic-gate  */
2800Sstevel@tonic-gate struct dv_node *
dv_mkroot(struct vfs * vfsp,dev_t devfsdev)2810Sstevel@tonic-gate dv_mkroot(struct vfs *vfsp, dev_t devfsdev)
2820Sstevel@tonic-gate {
2837224Scth 	struct dv_node	*dv;
2847224Scth 	struct vnode	*vp;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	ASSERT(ddi_root_node() != NULL);
2870Sstevel@tonic-gate 	ASSERT(dv_node_cache != NULL);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	dcmn_err3(("dv_mkroot\n"));
2900Sstevel@tonic-gate 	dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
2910Sstevel@tonic-gate 	vp = DVTOV(dv);
2920Sstevel@tonic-gate 	vn_reinit(vp);
2930Sstevel@tonic-gate 	vp->v_flag = VROOT;
2940Sstevel@tonic-gate 	vp->v_vfsp = vfsp;
2950Sstevel@tonic-gate 	vp->v_type = VDIR;
2960Sstevel@tonic-gate 	vp->v_rdev = devfsdev;
2970Sstevel@tonic-gate 	vn_setops(vp, dv_vnodeops);
2980Sstevel@tonic-gate 	vn_exists(vp);
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	dvroot = dv;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	dv->dv_name = NULL;		/* not needed */
3030Sstevel@tonic-gate 	dv->dv_namelen = 0;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	dv->dv_devi = ddi_root_node();
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	dv->dv_ino = DV_ROOTINO;
3080Sstevel@tonic-gate 	dv->dv_nlink = 2;		/* name + . (no dv_insert) */
3090Sstevel@tonic-gate 	dv->dv_dotdot = dv;		/* .. == self */
3100Sstevel@tonic-gate 	dv->dv_attrvp = NULLVP;
3110Sstevel@tonic-gate 	dv->dv_attr = NULL;
3120Sstevel@tonic-gate 	dv->dv_flags = DV_BUILD;
3130Sstevel@tonic-gate 	dv->dv_priv = NULL;
3140Sstevel@tonic-gate 	dv->dv_busy = 0;
3150Sstevel@tonic-gate 	dv->dv_dflt_mode = 0;
3160Sstevel@tonic-gate 
3176260Sjg 	avl_create(&dv->dv_entries,
3186260Sjg 	    (int (*)(const void *, const void *))dv_compare_nodes,
3196260Sjg 	    sizeof (struct dv_node), offsetof(struct dv_node, dv_avllink));
3206260Sjg 
3210Sstevel@tonic-gate 	return (dv);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate /*
3250Sstevel@tonic-gate  * dv_mkdir
3260Sstevel@tonic-gate  *
3270Sstevel@tonic-gate  * Given an probed or attached nexus node, create a VDIR dv_node.
3280Sstevel@tonic-gate  * No dv_attrvp is created at this point.
3290Sstevel@tonic-gate  */
3300Sstevel@tonic-gate struct dv_node *
dv_mkdir(struct dv_node * ddv,dev_info_t * devi,char * nm)3310Sstevel@tonic-gate dv_mkdir(struct dv_node *ddv, dev_info_t *devi, char *nm)
3320Sstevel@tonic-gate {
3337224Scth 	struct dv_node	*dv;
3347224Scth 	struct vnode	*vp;
3357224Scth 	size_t		nmlen;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	ASSERT((devi));
3380Sstevel@tonic-gate 	dcmn_err4(("dv_mkdir: %s\n", nm));
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
3410Sstevel@tonic-gate 	nmlen = strlen(nm) + 1;
3420Sstevel@tonic-gate 	dv->dv_name = kmem_alloc(nmlen, KM_SLEEP);
3430Sstevel@tonic-gate 	bcopy(nm, dv->dv_name, nmlen);
3440Sstevel@tonic-gate 	dv->dv_namelen = nmlen - 1;	/* '\0' not included */
3456260Sjg 
3460Sstevel@tonic-gate 	vp = DVTOV(dv);
3470Sstevel@tonic-gate 	vn_reinit(vp);
3480Sstevel@tonic-gate 	vp->v_flag = 0;
3490Sstevel@tonic-gate 	vp->v_vfsp = DVTOV(ddv)->v_vfsp;
3500Sstevel@tonic-gate 	vp->v_type = VDIR;
3510Sstevel@tonic-gate 	vp->v_rdev = DVTOV(ddv)->v_rdev;
3520Sstevel@tonic-gate 	vn_setops(vp, vn_getops(DVTOV(ddv)));
3530Sstevel@tonic-gate 	vn_exists(vp);
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	dv->dv_devi = devi;
3560Sstevel@tonic-gate 	ndi_hold_devi(devi);
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	dv->dv_ino = dv_mkino(devi, VDIR, NODEV);
3590Sstevel@tonic-gate 	dv->dv_nlink = 0;		/* updated on insert */
3600Sstevel@tonic-gate 	dv->dv_dotdot = ddv;
3610Sstevel@tonic-gate 	dv->dv_attrvp = NULLVP;
3620Sstevel@tonic-gate 	dv->dv_attr = NULL;
3630Sstevel@tonic-gate 	dv->dv_flags = DV_BUILD;
3640Sstevel@tonic-gate 	dv->dv_priv = NULL;
3650Sstevel@tonic-gate 	dv->dv_busy = 0;
3660Sstevel@tonic-gate 	dv->dv_dflt_mode = 0;
3670Sstevel@tonic-gate 
3686260Sjg 	avl_create(&dv->dv_entries,
3696260Sjg 	    (int (*)(const void *, const void *))dv_compare_nodes,
3706260Sjg 	    sizeof (struct dv_node), offsetof(struct dv_node, dv_avllink));
3716260Sjg 
3720Sstevel@tonic-gate 	return (dv);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate  * dv_mknod
3770Sstevel@tonic-gate  *
3780Sstevel@tonic-gate  * Given a minor node, create a VCHR or VBLK dv_node.
3790Sstevel@tonic-gate  * No dv_attrvp is created at this point.
3800Sstevel@tonic-gate  */
3810Sstevel@tonic-gate static struct dv_node *
dv_mknod(struct dv_node * ddv,dev_info_t * devi,char * nm,struct ddi_minor_data * dmd)3820Sstevel@tonic-gate dv_mknod(struct dv_node *ddv, dev_info_t *devi, char *nm,
3830Sstevel@tonic-gate 	struct ddi_minor_data *dmd)
3840Sstevel@tonic-gate {
3857224Scth 	struct dv_node	*dv;
3867224Scth 	struct vnode	*vp;
3877224Scth 	size_t		nmlen;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	dcmn_err4(("dv_mknod: %s\n", nm));
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
3920Sstevel@tonic-gate 	nmlen = strlen(nm) + 1;
3930Sstevel@tonic-gate 	dv->dv_name = kmem_alloc(nmlen, KM_SLEEP);
3940Sstevel@tonic-gate 	bcopy(nm, dv->dv_name, nmlen);
3950Sstevel@tonic-gate 	dv->dv_namelen = nmlen - 1;	/* no '\0' */
3966260Sjg 
3970Sstevel@tonic-gate 	vp = DVTOV(dv);
3980Sstevel@tonic-gate 	vn_reinit(vp);
3990Sstevel@tonic-gate 	vp->v_flag = 0;
4000Sstevel@tonic-gate 	vp->v_vfsp = DVTOV(ddv)->v_vfsp;
4010Sstevel@tonic-gate 	vp->v_type = dmd->ddm_spec_type == S_IFCHR ? VCHR : VBLK;
4020Sstevel@tonic-gate 	vp->v_rdev = dmd->ddm_dev;
4030Sstevel@tonic-gate 	vn_setops(vp, vn_getops(DVTOV(ddv)));
4040Sstevel@tonic-gate 	vn_exists(vp);
4050Sstevel@tonic-gate 
4067224Scth 	/* increment dev_ref with devi_lock held */
4077224Scth 	ASSERT(DEVI_BUSY_OWNED(devi));
4087224Scth 	mutex_enter(&DEVI(devi)->devi_lock);
4090Sstevel@tonic-gate 	dv->dv_devi = devi;
4108912SChris.Horne@Sun.COM 	DEVI(devi)->devi_ref++;		/* ndi_hold_devi(dip) */
4117224Scth 	mutex_exit(&DEVI(devi)->devi_lock);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	dv->dv_ino = dv_mkino(devi, vp->v_type, vp->v_rdev);
4140Sstevel@tonic-gate 	dv->dv_nlink = 0;		/* updated on insert */
4150Sstevel@tonic-gate 	dv->dv_dotdot = ddv;
4160Sstevel@tonic-gate 	dv->dv_attrvp = NULLVP;
4170Sstevel@tonic-gate 	dv->dv_attr = NULL;
4180Sstevel@tonic-gate 	dv->dv_flags = 0;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (dmd->type == DDM_INTERNAL_PATH)
4210Sstevel@tonic-gate 		dv->dv_flags |= DV_INTERNAL;
4220Sstevel@tonic-gate 	if (dmd->ddm_flags & DM_NO_FSPERM)
4230Sstevel@tonic-gate 		dv->dv_flags |= DV_NO_FSPERM;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	dv->dv_priv = dmd->ddm_node_priv;
4260Sstevel@tonic-gate 	if (dv->dv_priv)
4270Sstevel@tonic-gate 		dphold(dv->dv_priv);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	/*
4300Sstevel@tonic-gate 	 * Minors created with ddi_create_priv_minor_node can specify
4310Sstevel@tonic-gate 	 * a default mode permission other than the devfs default.
4320Sstevel@tonic-gate 	 */
4330Sstevel@tonic-gate 	if (dv->dv_priv || dv->dv_flags & DV_NO_FSPERM) {
4340Sstevel@tonic-gate 		dcmn_err5(("%s: dv_mknod default priv mode 0%o\n",
4350Sstevel@tonic-gate 		    dv->dv_name, dmd->ddm_priv_mode));
4360Sstevel@tonic-gate 		dv->dv_flags |= DV_DFLT_MODE;
4370Sstevel@tonic-gate 		dv->dv_dflt_mode = dmd->ddm_priv_mode & S_IAMB;
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	return (dv);
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate /*
4440Sstevel@tonic-gate  * dv_destroy
4450Sstevel@tonic-gate  *
4460Sstevel@tonic-gate  * Destroy what we created in dv_mkdir or dv_mknod.
4470Sstevel@tonic-gate  * In the case of a *referenced* directory, do nothing.
4480Sstevel@tonic-gate  */
4490Sstevel@tonic-gate void
dv_destroy(struct dv_node * dv,uint_t flags)4500Sstevel@tonic-gate dv_destroy(struct dv_node *dv, uint_t flags)
4510Sstevel@tonic-gate {
4520Sstevel@tonic-gate 	vnode_t *vp = DVTOV(dv);
4530Sstevel@tonic-gate 	ASSERT(dv->dv_nlink == 0);		/* no references */
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	dcmn_err4(("dv_destroy: %s\n", dv->dv_name));
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	/*
4580Sstevel@tonic-gate 	 * We may be asked to unlink referenced directories.
4590Sstevel@tonic-gate 	 * In this case, there is nothing to be done.
4600Sstevel@tonic-gate 	 * The eventual memory free will be done in
4610Sstevel@tonic-gate 	 * devfs_inactive.
4620Sstevel@tonic-gate 	 */
4630Sstevel@tonic-gate 	if (vp->v_count != 0) {
4640Sstevel@tonic-gate 		ASSERT(vp->v_type == VDIR);
4650Sstevel@tonic-gate 		ASSERT(flags & DV_CLEAN_FORCE);
4660Sstevel@tonic-gate 		ASSERT(DV_STALE(dv));
4670Sstevel@tonic-gate 		return;
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4706260Sjg 	if (vp->v_type == VDIR) {
4716260Sjg 		ASSERT(DV_FIRST_ENTRY(dv) == NULL);
4726260Sjg 		avl_destroy(&dv->dv_entries);
4736260Sjg 	}
4746260Sjg 
4750Sstevel@tonic-gate 	if (dv->dv_attrvp != NULLVP)
4760Sstevel@tonic-gate 		VN_RELE(dv->dv_attrvp);
4770Sstevel@tonic-gate 	if (dv->dv_attr != NULL)
4780Sstevel@tonic-gate 		kmem_free(dv->dv_attr, sizeof (struct vattr));
4790Sstevel@tonic-gate 	if (dv->dv_name != NULL)
4800Sstevel@tonic-gate 		kmem_free(dv->dv_name, dv->dv_namelen + 1);
4810Sstevel@tonic-gate 	if (dv->dv_devi != NULL) {
4820Sstevel@tonic-gate 		ndi_rele_devi(dv->dv_devi);
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 	if (dv->dv_priv != NULL) {
4850Sstevel@tonic-gate 		dpfree(dv->dv_priv);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	kmem_cache_free(dv_node_cache, dv);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate /*
4920Sstevel@tonic-gate  * Find and hold dv_node by name
4930Sstevel@tonic-gate  */
4946260Sjg static struct dv_node *
dv_findbyname(struct dv_node * ddv,char * nm)4950Sstevel@tonic-gate dv_findbyname(struct dv_node *ddv, char *nm)
4960Sstevel@tonic-gate {
4976260Sjg 	struct dv_node  *dv;
4986260Sjg 	avl_index_t	where;
4996260Sjg 	struct dv_node	dvtmp;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
5020Sstevel@tonic-gate 	dcmn_err3(("dv_findbyname: %s\n", nm));
5036260Sjg 
5046260Sjg 	dvtmp.dv_name = nm;
5056260Sjg 	dv = avl_find(&ddv->dv_entries, &dvtmp, &where);
5066260Sjg 	if (dv) {
5076260Sjg 		ASSERT(dv->dv_dotdot == ddv);
5086260Sjg 		ASSERT(strcmp(dv->dv_name, nm) == 0);
5096260Sjg 		VN_HOLD(DVTOV(dv));
5106260Sjg 		return (dv);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 	return (NULL);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate /*
5160Sstevel@tonic-gate  * Inserts a new dv_node in a parent directory
5170Sstevel@tonic-gate  */
5180Sstevel@tonic-gate void
dv_insert(struct dv_node * ddv,struct dv_node * dv)5190Sstevel@tonic-gate dv_insert(struct dv_node *ddv, struct dv_node *dv)
5200Sstevel@tonic-gate {
5217224Scth 	avl_index_t	where;
5226260Sjg 
5230Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
5240Sstevel@tonic-gate 	ASSERT(DVTOV(ddv)->v_type == VDIR);
5250Sstevel@tonic-gate 	ASSERT(ddv->dv_nlink >= 2);
5260Sstevel@tonic-gate 	ASSERT(dv->dv_nlink == 0);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	dcmn_err3(("dv_insert: %s\n", dv->dv_name));
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	dv->dv_dotdot = ddv;
5310Sstevel@tonic-gate 	if (DVTOV(dv)->v_type == VDIR) {
5320Sstevel@tonic-gate 		ddv->dv_nlink++;	/* .. to containing directory */
5330Sstevel@tonic-gate 		dv->dv_nlink = 2;	/* name + . */
5340Sstevel@tonic-gate 	} else {
5350Sstevel@tonic-gate 		dv->dv_nlink = 1;	/* name */
5360Sstevel@tonic-gate 	}
5376260Sjg 
5386260Sjg 	/* enter node in the avl tree */
5396260Sjg 	VERIFY(avl_find(&ddv->dv_entries, dv, &where) == NULL);
5406260Sjg 	avl_insert(&ddv->dv_entries, dv, where);
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate /*
544647Scth  * Unlink a dv_node from a perent directory
545647Scth  */
546647Scth void
dv_unlink(struct dv_node * ddv,struct dv_node * dv)5476260Sjg dv_unlink(struct dv_node *ddv, struct dv_node *dv)
548647Scth {
549647Scth 	/* verify linkage of arguments */
5506260Sjg 	ASSERT(ddv && dv);
551647Scth 	ASSERT(dv->dv_dotdot == ddv);
552647Scth 	ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
553647Scth 	ASSERT(DVTOV(ddv)->v_type == VDIR);
554647Scth 
555647Scth 	dcmn_err3(("dv_unlink: %s\n", dv->dv_name));
556647Scth 
557647Scth 	if (DVTOV(dv)->v_type == VDIR) {
558647Scth 		ddv->dv_nlink--;	/* .. to containing directory */
559647Scth 		dv->dv_nlink -= 2;	/* name + . */
560647Scth 	} else {
561647Scth 		dv->dv_nlink -= 1;	/* name */
562647Scth 	}
563647Scth 	ASSERT(ddv->dv_nlink >= 2);
564647Scth 	ASSERT(dv->dv_nlink == 0);
565647Scth 
5666260Sjg 	dv->dv_dotdot = NULL;
567647Scth 
5686260Sjg 	/* remove from avl tree */
5696260Sjg 	avl_remove(&ddv->dv_entries, dv);
570647Scth }
571647Scth 
572647Scth /*
5730Sstevel@tonic-gate  * Merge devfs node specific information into an attribute structure.
5740Sstevel@tonic-gate  *
5750Sstevel@tonic-gate  * NOTE: specfs provides ATIME,MTIME,CTIME,SIZE,BLKSIZE,NBLOCKS on leaf node.
5760Sstevel@tonic-gate  */
5770Sstevel@tonic-gate void
dv_vattr_merge(struct dv_node * dv,struct vattr * vap)5780Sstevel@tonic-gate dv_vattr_merge(struct dv_node *dv, struct vattr *vap)
5790Sstevel@tonic-gate {
5807224Scth 	struct vnode	*vp = DVTOV(dv);
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	vap->va_nodeid = dv->dv_ino;
5830Sstevel@tonic-gate 	vap->va_nlink = dv->dv_nlink;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
5860Sstevel@tonic-gate 		vap->va_rdev = 0;
5870Sstevel@tonic-gate 		vap->va_fsid = vp->v_rdev;
5880Sstevel@tonic-gate 	} else {
5890Sstevel@tonic-gate 		vap->va_rdev = vp->v_rdev;
5900Sstevel@tonic-gate 		vap->va_fsid = DVTOV(dv->dv_dotdot)->v_rdev;
5910Sstevel@tonic-gate 		vap->va_type = vp->v_type;
5920Sstevel@tonic-gate 		/* don't trust the shadow file type */
5930Sstevel@tonic-gate 		vap->va_mode &= ~S_IFMT;
5940Sstevel@tonic-gate 		if (vap->va_type == VCHR)
5950Sstevel@tonic-gate 			vap->va_mode |= S_IFCHR;
5960Sstevel@tonic-gate 		else
5970Sstevel@tonic-gate 			vap->va_mode |= S_IFBLK;
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate /*
6022621Sllai1  * Get default device permission by consulting rules in
6032621Sllai1  * privilege specification in minor node and /etc/minor_perm.
6042621Sllai1  *
6052621Sllai1  * This function is called from the devname filesystem to get default
6062621Sllai1  * permissions for a device exported to a non-global zone.
6072621Sllai1  */
6082621Sllai1 void
devfs_get_defattr(struct vnode * vp,struct vattr * vap,int * no_fs_perm)6092621Sllai1 devfs_get_defattr(struct vnode *vp, struct vattr *vap, int *no_fs_perm)
6102621Sllai1 {
6117224Scth 	mperm_t		mp;
6127224Scth 	struct dv_node	*dv;
6132621Sllai1 
6142621Sllai1 	/* If vp isn't a dv_node, return something sensible */
6152621Sllai1 	if (!vn_matchops(vp, dv_vnodeops)) {
6162621Sllai1 		if (no_fs_perm)
6172621Sllai1 			*no_fs_perm = 0;
6182621Sllai1 		*vap = dv_vattr_file;
6192621Sllai1 		return;
6202621Sllai1 	}
6212621Sllai1 
6222621Sllai1 	/*
6232621Sllai1 	 * For minors not created by ddi_create_priv_minor_node(),
6242621Sllai1 	 * use devfs defaults.
6252621Sllai1 	 */
6262621Sllai1 	dv = VTODV(vp);
6272621Sllai1 	if (vp->v_type == VDIR) {
6282621Sllai1 		*vap = dv_vattr_dir;
6292621Sllai1 	} else if (dv->dv_flags & DV_NO_FSPERM) {
6302621Sllai1 		if (no_fs_perm)
6312621Sllai1 			*no_fs_perm = 1;
6322621Sllai1 		*vap = dv_vattr_priv;
6332621Sllai1 	} else {
6342621Sllai1 		/*
6352621Sllai1 		 * look up perm bits from minor_perm
6362621Sllai1 		 */
6372621Sllai1 		*vap = dv_vattr_file;
6382621Sllai1 		if (dev_minorperm(dv->dv_devi, dv->dv_name, &mp) == 0) {
6392621Sllai1 			VATTR_MP_MERGE((*vap), mp);
6402621Sllai1 			dcmn_err5(("%s: minor perm mode 0%o\n",
6412621Sllai1 			    dv->dv_name, vap->va_mode));
6422621Sllai1 		} else if (dv->dv_flags & DV_DFLT_MODE) {
6432621Sllai1 			ASSERT((dv->dv_dflt_mode & ~S_IAMB) == 0);
6442621Sllai1 			vap->va_mode &= ~S_IAMB;
6452621Sllai1 			vap->va_mode |= dv->dv_dflt_mode;
6462621Sllai1 			dcmn_err5(("%s: priv mode 0%o\n",
6472621Sllai1 			    dv->dv_name, vap->va_mode));
6482621Sllai1 		}
6492621Sllai1 	}
6502621Sllai1 }
6512621Sllai1 
6522621Sllai1 /*
6530Sstevel@tonic-gate  * dv_shadow_node
6540Sstevel@tonic-gate  *
6550Sstevel@tonic-gate  * Given a VDIR dv_node, find/create the associated VDIR
6560Sstevel@tonic-gate  * node in the shadow attribute filesystem.
6570Sstevel@tonic-gate  *
6580Sstevel@tonic-gate  * Given a VCHR/VBLK dv_node, find the associated VREG
6590Sstevel@tonic-gate  * node in the shadow attribute filesystem.  These nodes
6600Sstevel@tonic-gate  * are only created to persist non-default attributes.
6610Sstevel@tonic-gate  * Lack of such a node implies the default permissions
6620Sstevel@tonic-gate  * are sufficient.
6630Sstevel@tonic-gate  *
6640Sstevel@tonic-gate  * Managing the attribute file entries is slightly tricky (mostly
6650Sstevel@tonic-gate  * because we can't intercept VN_HOLD and VN_RELE except on the last
6660Sstevel@tonic-gate  * release).
6670Sstevel@tonic-gate  *
6680Sstevel@tonic-gate  * We assert that if the dv_attrvp pointer is non-NULL, it points
6690Sstevel@tonic-gate  * to a singly-held (by us) vnode that represents the shadow entry
6700Sstevel@tonic-gate  * in the underlying filesystem.  To avoid store-ordering issues,
6710Sstevel@tonic-gate  * we assert that the pointer can only be tested under the dv_contents
6720Sstevel@tonic-gate  * READERS lock.
6730Sstevel@tonic-gate  */
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate void
dv_shadow_node(struct vnode * dvp,char * nm,struct vnode * vp,struct pathname * pnp,struct vnode * rdir,struct cred * cred,int flags)6760Sstevel@tonic-gate dv_shadow_node(
6770Sstevel@tonic-gate 	struct vnode *dvp,	/* devfs parent directory vnode */
6780Sstevel@tonic-gate 	char *nm,		/* name component */
6790Sstevel@tonic-gate 	struct vnode *vp,	/* devfs vnode */
6800Sstevel@tonic-gate 	struct pathname *pnp,	/* the path .. */
6810Sstevel@tonic-gate 	struct vnode *rdir,	/* the root .. */
6820Sstevel@tonic-gate 	struct cred *cred,	/* who's asking? */
6830Sstevel@tonic-gate 	int flags)		/* optionally create shadow node */
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	struct dv_node	*dv;	/* dv_node of named directory */
6860Sstevel@tonic-gate 	struct vnode	*rdvp;	/* shadow parent directory vnode */
6870Sstevel@tonic-gate 	struct vnode	*rvp;	/* shadow vnode */
6880Sstevel@tonic-gate 	struct vnode	*rrvp;	/* realvp of shadow vnode */
6890Sstevel@tonic-gate 	struct vattr	vattr;
6900Sstevel@tonic-gate 	int		create_tried;
6910Sstevel@tonic-gate 	int		error;
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
6940Sstevel@tonic-gate 	dv = VTODV(vp);
6950Sstevel@tonic-gate 	dcmn_err3(("dv_shadow_node: name %s attr %p\n",
6960Sstevel@tonic-gate 	    nm, (void *)dv->dv_attrvp));
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	if ((flags & DV_SHADOW_WRITE_HELD) == 0) {
6990Sstevel@tonic-gate 		ASSERT(RW_READ_HELD(&dv->dv_contents));
7000Sstevel@tonic-gate 		if (dv->dv_attrvp != NULLVP)
7010Sstevel@tonic-gate 			return;
7020Sstevel@tonic-gate 		if (!rw_tryupgrade(&dv->dv_contents)) {
7030Sstevel@tonic-gate 			rw_exit(&dv->dv_contents);
7040Sstevel@tonic-gate 			rw_enter(&dv->dv_contents, RW_WRITER);
7050Sstevel@tonic-gate 			if (dv->dv_attrvp != NULLVP) {
7060Sstevel@tonic-gate 				rw_downgrade(&dv->dv_contents);
7070Sstevel@tonic-gate 				return;
7080Sstevel@tonic-gate 			}
7090Sstevel@tonic-gate 		}
7100Sstevel@tonic-gate 	} else {
7110Sstevel@tonic-gate 		ASSERT(RW_WRITE_HELD(&dv->dv_contents));
7120Sstevel@tonic-gate 		if (dv->dv_attrvp != NULLVP)
7130Sstevel@tonic-gate 			return;
7140Sstevel@tonic-gate 	}
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&dv->dv_contents) && dv->dv_attrvp == NULL);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	rdvp = VTODV(dvp)->dv_attrvp;
7190Sstevel@tonic-gate 	create_tried = 0;
7200Sstevel@tonic-gate lookup:
7210Sstevel@tonic-gate 	if (rdvp && (dv->dv_flags & DV_NO_FSPERM) == 0) {
7225331Samw 		error = VOP_LOOKUP(rdvp, nm, &rvp, pnp, LOOKUP_DIR, rdir, cred,
7235331Samw 		    NULL, NULL, NULL);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 		/* factor out the snode since we only want the attribute node */
7265331Samw 		if ((error == 0) && (VOP_REALVP(rvp, &rrvp, NULL) == 0)) {
7270Sstevel@tonic-gate 			VN_HOLD(rrvp);
7280Sstevel@tonic-gate 			VN_RELE(rvp);
7290Sstevel@tonic-gate 			rvp = rrvp;
7300Sstevel@tonic-gate 		}
7310Sstevel@tonic-gate 	} else
7320Sstevel@tonic-gate 		error = EROFS;		/* no parent, no entry */
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	/*
7350Sstevel@tonic-gate 	 * All we want is the permissions (and maybe ACLs and
7360Sstevel@tonic-gate 	 * extended attributes), and we want to perform lookups
7370Sstevel@tonic-gate 	 * by name.  Drivers occasionally change their minor
7380Sstevel@tonic-gate 	 * number space.  If something changes, there's no
7390Sstevel@tonic-gate 	 * much we can do about it here.
7400Sstevel@tonic-gate 	 */
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	/* The shadow node checks out. We are done */
7430Sstevel@tonic-gate 	if (error == 0) {
7440Sstevel@tonic-gate 		dv->dv_attrvp = rvp;	/* with one hold */
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 		/*
747789Sahrens 		 * Determine if we have non-trivial ACLs on this node.
748789Sahrens 		 * It is not necessary to VOP_RWLOCK since fs_acl_nontrivial
749789Sahrens 		 * only does VOP_GETSECATTR.
7500Sstevel@tonic-gate 		 */
7510Sstevel@tonic-gate 		dv->dv_flags &= ~DV_ACL;
752789Sahrens 
753789Sahrens 		if (fs_acl_nontrivial(rvp, cred))
754789Sahrens 			dv->dv_flags |= DV_ACL;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 		/*
7570Sstevel@tonic-gate 		 * If we have synced out the memory attributes, free
7580Sstevel@tonic-gate 		 * them and switch back to using the persistent store.
7590Sstevel@tonic-gate 		 */
7600Sstevel@tonic-gate 		if (rvp && dv->dv_attr) {
7610Sstevel@tonic-gate 			kmem_free(dv->dv_attr, sizeof (struct vattr));
7620Sstevel@tonic-gate 			dv->dv_attr = NULL;
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 		if ((flags & DV_SHADOW_WRITE_HELD) == 0)
7650Sstevel@tonic-gate 			rw_downgrade(&dv->dv_contents);
7660Sstevel@tonic-gate 		ASSERT(RW_LOCK_HELD(&dv->dv_contents));
7670Sstevel@tonic-gate 		return;
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	/*
7710Sstevel@tonic-gate 	 * Failed to find attribute in persistent backing store,
7722621Sllai1 	 * get default permission bits.
7730Sstevel@tonic-gate 	 */
7742621Sllai1 	devfs_get_defattr(vp, &vattr, NULL);
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	dv_vattr_merge(dv, &vattr);
7770Sstevel@tonic-gate 	gethrestime(&vattr.va_atime);
7780Sstevel@tonic-gate 	vattr.va_mtime = vattr.va_atime;
7790Sstevel@tonic-gate 	vattr.va_ctime = vattr.va_atime;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	/*
7820Sstevel@tonic-gate 	 * Try to create shadow dir. This is necessary in case
7830Sstevel@tonic-gate 	 * we need to create a shadow leaf node later, when user
7840Sstevel@tonic-gate 	 * executes chmod.
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	if ((error == ENOENT) && !create_tried) {
7870Sstevel@tonic-gate 		switch (vp->v_type) {
7880Sstevel@tonic-gate 		case VDIR:
7895331Samw 			error = VOP_MKDIR(rdvp, nm, &vattr, &rvp, kcred,
7905331Samw 			    NULL, 0, NULL);
7910Sstevel@tonic-gate 			dsysdebug(error, ("vop_mkdir %s %s %d\n",
7920Sstevel@tonic-gate 			    VTODV(dvp)->dv_name, nm, error));
7930Sstevel@tonic-gate 			create_tried = 1;
7940Sstevel@tonic-gate 			break;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 		case VCHR:
7970Sstevel@tonic-gate 		case VBLK:
7980Sstevel@tonic-gate 			/*
7990Sstevel@tonic-gate 			 * Shadow nodes are only created on demand
8000Sstevel@tonic-gate 			 */
8010Sstevel@tonic-gate 			if (flags & DV_SHADOW_CREATE) {
8020Sstevel@tonic-gate 				error = VOP_CREATE(rdvp, nm, &vattr, NONEXCL,
8035331Samw 				    VREAD|VWRITE, &rvp, kcred, 0, NULL, NULL);
8040Sstevel@tonic-gate 				dsysdebug(error, ("vop_create %s %s %d\n",
8050Sstevel@tonic-gate 				    VTODV(dvp)->dv_name, nm, error));
8060Sstevel@tonic-gate 				create_tried = 1;
8070Sstevel@tonic-gate 			}
8080Sstevel@tonic-gate 			break;
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 		default:
8110Sstevel@tonic-gate 			cmn_err(CE_PANIC, "devfs: %s: create", dvnm);
8120Sstevel@tonic-gate 			/*NOTREACHED*/
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 		if (create_tried &&
8160Sstevel@tonic-gate 		    (error == 0) || (error == EEXIST)) {
8170Sstevel@tonic-gate 			VN_RELE(rvp);
8180Sstevel@tonic-gate 			goto lookup;
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	/* Store attribute in memory */
8230Sstevel@tonic-gate 	if (dv->dv_attr == NULL) {
8240Sstevel@tonic-gate 		dv->dv_attr = kmem_alloc(sizeof (struct vattr), KM_SLEEP);
8250Sstevel@tonic-gate 		*(dv->dv_attr) = vattr;
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	if ((flags & DV_SHADOW_WRITE_HELD) == 0)
8290Sstevel@tonic-gate 		rw_downgrade(&dv->dv_contents);
8300Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&dv->dv_contents));
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate /*
8340Sstevel@tonic-gate  * Given a devinfo node, and a name, returns the appropriate
8350Sstevel@tonic-gate  * minor information for that named node, if it exists.
8360Sstevel@tonic-gate  */
8370Sstevel@tonic-gate static int
dv_find_leafnode(dev_info_t * devi,char * minor_nm,struct ddi_minor_data * r_mi)8380Sstevel@tonic-gate dv_find_leafnode(dev_info_t *devi, char *minor_nm, struct ddi_minor_data *r_mi)
8390Sstevel@tonic-gate {
8407224Scth 	struct ddi_minor_data	*dmd;
8410Sstevel@tonic-gate 
8421333Scth 	ASSERT(i_ddi_devi_attached(devi));
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	dcmn_err3(("dv_find_leafnode: %s\n", minor_nm));
8457224Scth 	ASSERT(DEVI_BUSY_OWNED(devi));
8460Sstevel@tonic-gate 	for (dmd = DEVI(devi)->devi_minor; dmd; dmd = dmd->next) {
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 		/*
8490Sstevel@tonic-gate 		 * Skip alias nodes and nodes without a name.
8500Sstevel@tonic-gate 		 */
8510Sstevel@tonic-gate 		if ((dmd->type == DDM_ALIAS) || (dmd->ddm_name == NULL))
8526260Sjg 			continue;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 		dcmn_err4(("dv_find_leafnode: (%s,%s)\n",
8556260Sjg 		    minor_nm, dmd->ddm_name));
8560Sstevel@tonic-gate 		if (strcmp(minor_nm, dmd->ddm_name) == 0) {
8570Sstevel@tonic-gate 			r_mi->ddm_dev = dmd->ddm_dev;
8580Sstevel@tonic-gate 			r_mi->ddm_spec_type = dmd->ddm_spec_type;
8590Sstevel@tonic-gate 			r_mi->type = dmd->type;
8600Sstevel@tonic-gate 			r_mi->ddm_flags = dmd->ddm_flags;
8610Sstevel@tonic-gate 			r_mi->ddm_node_priv = dmd->ddm_node_priv;
8620Sstevel@tonic-gate 			r_mi->ddm_priv_mode = dmd->ddm_priv_mode;
8630Sstevel@tonic-gate 			if (r_mi->ddm_node_priv)
8640Sstevel@tonic-gate 				dphold(r_mi->ddm_node_priv);
8650Sstevel@tonic-gate 			return (0);
8660Sstevel@tonic-gate 		}
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	dcmn_err3(("dv_find_leafnode: %s: ENOENT\n", minor_nm));
8700Sstevel@tonic-gate 	return (ENOENT);
8710Sstevel@tonic-gate }
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate /*
8740Sstevel@tonic-gate  * Special handling for clone node:
8750Sstevel@tonic-gate  *	Clone minor name is a driver name, the minor number will
8760Sstevel@tonic-gate  *	be the major number of the driver. There is no minor
8770Sstevel@tonic-gate  *	node under the clone driver, so we'll manufacture the
8780Sstevel@tonic-gate  *	dev_t.
8790Sstevel@tonic-gate  */
8800Sstevel@tonic-gate static struct dv_node *
dv_clone_mknod(struct dv_node * ddv,char * drvname)8810Sstevel@tonic-gate dv_clone_mknod(struct dv_node *ddv, char *drvname)
8820Sstevel@tonic-gate {
8837224Scth 	major_t			major;
8847224Scth 	struct dv_node		*dvp;
8857224Scth 	char			*devnm;
8867224Scth 	struct ddi_minor_data	*dmd;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	/*
8890Sstevel@tonic-gate 	 * Make sure drvname is a STREAMS driver. We load the driver,
8900Sstevel@tonic-gate 	 * but don't attach to any instances. This makes stat(2)
8910Sstevel@tonic-gate 	 * relatively cheap.
8920Sstevel@tonic-gate 	 */
8930Sstevel@tonic-gate 	major = ddi_name_to_major(drvname);
8947009Scth 	if (major == DDI_MAJOR_T_NONE)
8950Sstevel@tonic-gate 		return (NULL);
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	if (ddi_hold_driver(major) == NULL)
8980Sstevel@tonic-gate 		return (NULL);
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	if (STREAMSTAB(major) == NULL) {
9010Sstevel@tonic-gate 		ddi_rele_driver(major);
9020Sstevel@tonic-gate 		return (NULL);
9030Sstevel@tonic-gate 	}
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	ddi_rele_driver(major);
9060Sstevel@tonic-gate 	devnm = kmem_alloc(MAXNAMELEN, KM_SLEEP);
9070Sstevel@tonic-gate 	(void) snprintf(devnm, MAXNAMELEN, "clone@0:%s", drvname);
9080Sstevel@tonic-gate 	dmd = kmem_zalloc(sizeof (*dmd), KM_SLEEP);
9090Sstevel@tonic-gate 	dmd->ddm_dev = makedevice(clone_major, (minor_t)major);
9100Sstevel@tonic-gate 	dmd->ddm_spec_type = S_IFCHR;
9110Sstevel@tonic-gate 	dvp = dv_mknod(ddv, clone_dip, devnm, dmd);
9120Sstevel@tonic-gate 	kmem_free(dmd, sizeof (*dmd));
9130Sstevel@tonic-gate 	kmem_free(devnm, MAXNAMELEN);
9140Sstevel@tonic-gate 	return (dvp);
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate /*
9180Sstevel@tonic-gate  * Given the parent directory node, and a name in it, returns the
9190Sstevel@tonic-gate  * named dv_node to the caller (as a vnode).
9200Sstevel@tonic-gate  *
9210Sstevel@tonic-gate  * (We need pnp and rdir for doing shadow lookups; they can be NULL)
9220Sstevel@tonic-gate  */
9230Sstevel@tonic-gate int
dv_find(struct dv_node * ddv,char * nm,struct vnode ** vpp,struct pathname * pnp,struct vnode * rdir,struct cred * cred,uint_t ndi_flags)9240Sstevel@tonic-gate dv_find(struct dv_node *ddv, char *nm, struct vnode **vpp, struct pathname *pnp,
9250Sstevel@tonic-gate 	struct vnode *rdir, struct cred *cred, uint_t ndi_flags)
9260Sstevel@tonic-gate {
9270Sstevel@tonic-gate 	extern int isminiroot;	/* see modctl.c */
9280Sstevel@tonic-gate 
9297224Scth 	int			circ;
9307224Scth 	int			rv = 0, was_busy = 0, nmlen, write_held = 0;
9317224Scth 	struct vnode		*vp;
9327224Scth 	struct dv_node		*dv, *dup;
9337224Scth 	dev_info_t		*pdevi, *devi = NULL;
9347224Scth 	char			*mnm;
9357224Scth 	struct ddi_minor_data	*dmd;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	dcmn_err3(("dv_find %s\n", nm));
9380Sstevel@tonic-gate 
939*12569SJakub.Jermar@Sun.COM 	if (!rw_tryenter(&ddv->dv_contents, RW_READER)) {
940*12569SJakub.Jermar@Sun.COM 		if (tsd_get(devfs_clean_key))
941*12569SJakub.Jermar@Sun.COM 			return (EBUSY);
942*12569SJakub.Jermar@Sun.COM 		rw_enter(&ddv->dv_contents, RW_READER);
943*12569SJakub.Jermar@Sun.COM 	}
9440Sstevel@tonic-gate start:
9450Sstevel@tonic-gate 	if (DV_STALE(ddv)) {
9460Sstevel@tonic-gate 		rw_exit(&ddv->dv_contents);
9470Sstevel@tonic-gate 		return (ESTALE);
9480Sstevel@tonic-gate 	}
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	/*
9510Sstevel@tonic-gate 	 * Empty name or ., return node itself.
9520Sstevel@tonic-gate 	 */
9530Sstevel@tonic-gate 	nmlen = strlen(nm);
9540Sstevel@tonic-gate 	if ((nmlen == 0) || ((nmlen == 1) && (nm[0] == '.'))) {
9550Sstevel@tonic-gate 		*vpp = DVTOV(ddv);
9560Sstevel@tonic-gate 		rw_exit(&ddv->dv_contents);
9570Sstevel@tonic-gate 		VN_HOLD(*vpp);
9580Sstevel@tonic-gate 		return (0);
9590Sstevel@tonic-gate 	}
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	/*
9620Sstevel@tonic-gate 	 * .., return the parent directory
9630Sstevel@tonic-gate 	 */
9640Sstevel@tonic-gate 	if ((nmlen == 2) && (strcmp(nm, "..") == 0)) {
9650Sstevel@tonic-gate 		*vpp = DVTOV(ddv->dv_dotdot);
9660Sstevel@tonic-gate 		rw_exit(&ddv->dv_contents);
9670Sstevel@tonic-gate 		VN_HOLD(*vpp);
9680Sstevel@tonic-gate 		return (0);
9690Sstevel@tonic-gate 	}
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	/*
9720Sstevel@tonic-gate 	 * Fail anything without a valid device name component
9730Sstevel@tonic-gate 	 */
9740Sstevel@tonic-gate 	if (nm[0] == '@' || nm[0] == ':') {
9750Sstevel@tonic-gate 		dcmn_err3(("devfs: no driver '%s'\n", nm));
9760Sstevel@tonic-gate 		rw_exit(&ddv->dv_contents);
9770Sstevel@tonic-gate 		return (ENOENT);
9780Sstevel@tonic-gate 	}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	/*
9810Sstevel@tonic-gate 	 * So, now we have to deal with the trickier stuff.
9820Sstevel@tonic-gate 	 *
9830Sstevel@tonic-gate 	 * (a) search the existing list of dv_nodes on this directory
9840Sstevel@tonic-gate 	 */
9850Sstevel@tonic-gate 	if ((dv = dv_findbyname(ddv, nm)) != NULL) {
9860Sstevel@tonic-gate founddv:
9870Sstevel@tonic-gate 		ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
9884895Sdf125853 
9894895Sdf125853 		if (!rw_tryenter(&dv->dv_contents, RW_READER)) {
9904895Sdf125853 			if (tsd_get(devfs_clean_key)) {
9914895Sdf125853 				VN_RELE(DVTOV(dv));
9924895Sdf125853 				rw_exit(&ddv->dv_contents);
9934895Sdf125853 				return (EBUSY);
9944895Sdf125853 			}
9954895Sdf125853 			rw_enter(&dv->dv_contents, RW_READER);
9964895Sdf125853 		}
9974895Sdf125853 
9980Sstevel@tonic-gate 		vp = DVTOV(dv);
9990Sstevel@tonic-gate 		if ((dv->dv_attrvp != NULLVP) ||
10000Sstevel@tonic-gate 		    (vp->v_type != VDIR && dv->dv_attr != NULL)) {
10010Sstevel@tonic-gate 			/*
10020Sstevel@tonic-gate 			 * Common case - we already have attributes
10030Sstevel@tonic-gate 			 */
10040Sstevel@tonic-gate 			rw_exit(&dv->dv_contents);
10050Sstevel@tonic-gate 			rw_exit(&ddv->dv_contents);
10060Sstevel@tonic-gate 			goto found;
10070Sstevel@tonic-gate 		}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 		/*
10100Sstevel@tonic-gate 		 * No attribute vp, try and build one.
10114895Sdf125853 		 *
10124895Sdf125853 		 * dv_shadow_node() can briefly drop &dv->dv_contents lock
10134895Sdf125853 		 * if it is unable to upgrade it to a write lock. If the
10144895Sdf125853 		 * current thread has come in through the bottom-up device
10154895Sdf125853 		 * configuration devfs_clean() path, we may deadlock against
10164895Sdf125853 		 * a thread performing top-down device configuration if it
10174895Sdf125853 		 * grabs the contents lock. To avoid this, when we are on the
10184895Sdf125853 		 * devfs_clean() path we attempt to upgrade the dv_contents
10194895Sdf125853 		 * lock before we call dv_shadow_node().
10200Sstevel@tonic-gate 		 */
10214895Sdf125853 		if (tsd_get(devfs_clean_key)) {
10224895Sdf125853 			if (!rw_tryupgrade(&dv->dv_contents)) {
10234895Sdf125853 				VN_RELE(DVTOV(dv));
10244895Sdf125853 				rw_exit(&dv->dv_contents);
10254895Sdf125853 				rw_exit(&ddv->dv_contents);
10264895Sdf125853 				return (EBUSY);
10274895Sdf125853 			}
10284895Sdf125853 
10294895Sdf125853 			write_held = DV_SHADOW_WRITE_HELD;
10304895Sdf125853 		}
10314895Sdf125853 
10324895Sdf125853 		dv_shadow_node(DVTOV(ddv), nm, vp, pnp, rdir, cred,
10334895Sdf125853 		    write_held);
10344895Sdf125853 
10350Sstevel@tonic-gate 		rw_exit(&dv->dv_contents);
10360Sstevel@tonic-gate 		rw_exit(&ddv->dv_contents);
10370Sstevel@tonic-gate 		goto found;
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	/*
10410Sstevel@tonic-gate 	 * (b) Search the child devinfo nodes of our parent directory,
10420Sstevel@tonic-gate 	 * looking for the named node.  If we find it, build a new
10430Sstevel@tonic-gate 	 * node, then grab the writers lock, search the directory
10440Sstevel@tonic-gate 	 * if it's still not there, then insert it.
10450Sstevel@tonic-gate 	 *
10460Sstevel@tonic-gate 	 * We drop the devfs locks before accessing the device tree.
10470Sstevel@tonic-gate 	 * Take care to mark the node BUSY so that a forced devfs_clean
10480Sstevel@tonic-gate 	 * doesn't mark the directory node stale.
10490Sstevel@tonic-gate 	 *
10500Sstevel@tonic-gate 	 * Also, check if we are called as part of devfs_clean or
10510Sstevel@tonic-gate 	 * reset_perm. If so, simply return not found because there
10520Sstevel@tonic-gate 	 * is nothing to clean.
10530Sstevel@tonic-gate 	 */
10540Sstevel@tonic-gate 	if (tsd_get(devfs_clean_key)) {
10550Sstevel@tonic-gate 		rw_exit(&ddv->dv_contents);
10560Sstevel@tonic-gate 		return (ENOENT);
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	/*
10600Sstevel@tonic-gate 	 * We could be either READ or WRITE locked at
10610Sstevel@tonic-gate 	 * this point. Upgrade if we are read locked.
10620Sstevel@tonic-gate 	 */
10630Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
10640Sstevel@tonic-gate 	if (rw_read_locked(&ddv->dv_contents) &&
10650Sstevel@tonic-gate 	    !rw_tryupgrade(&ddv->dv_contents)) {
10660Sstevel@tonic-gate 		rw_exit(&ddv->dv_contents);
10670Sstevel@tonic-gate 		rw_enter(&ddv->dv_contents, RW_WRITER);
10680Sstevel@tonic-gate 		/*
10690Sstevel@tonic-gate 		 * Things may have changed when we dropped
10700Sstevel@tonic-gate 		 * the contents lock, so start from top again
10710Sstevel@tonic-gate 		 */
10720Sstevel@tonic-gate 		goto start;
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 	ddv->dv_busy++;		/* mark busy before dropping lock */
10750Sstevel@tonic-gate 	was_busy++;
10760Sstevel@tonic-gate 	rw_exit(&ddv->dv_contents);
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 	pdevi = ddv->dv_devi;
10790Sstevel@tonic-gate 	ASSERT(pdevi != NULL);
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 	mnm = strchr(nm, ':');
10820Sstevel@tonic-gate 	if (mnm)
10830Sstevel@tonic-gate 		*mnm = (char)0;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 	/*
10860Sstevel@tonic-gate 	 * Configure one nexus child, will call nexus's bus_ops
10870Sstevel@tonic-gate 	 * If successful, devi is held upon returning.
10880Sstevel@tonic-gate 	 * Note: devfs lookup should not be configuring grandchildren.
10890Sstevel@tonic-gate 	 */
10900Sstevel@tonic-gate 	ASSERT((ndi_flags & NDI_CONFIG) == 0);
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 	rv = ndi_devi_config_one(pdevi, nm, &devi, ndi_flags | NDI_NO_EVENT);
10930Sstevel@tonic-gate 	if (mnm)
10940Sstevel@tonic-gate 		*mnm = ':';
10950Sstevel@tonic-gate 	if (rv != NDI_SUCCESS) {
10960Sstevel@tonic-gate 		rv = ENOENT;
10970Sstevel@tonic-gate 		goto notfound;
10980Sstevel@tonic-gate 	}
10990Sstevel@tonic-gate 
110012116SVikram.Hegde@Sun.COM 	ASSERT(devi);
110112116SVikram.Hegde@Sun.COM 
110212116SVikram.Hegde@Sun.COM 	/* Check if this is a path alias */
110312116SVikram.Hegde@Sun.COM 	if (ddi_aliases_present == B_TRUE && ddi_get_parent(devi) != pdevi) {
110412116SVikram.Hegde@Sun.COM 		char *curr = kmem_alloc(MAXPATHLEN, KM_SLEEP);
110512116SVikram.Hegde@Sun.COM 
110612116SVikram.Hegde@Sun.COM 		(void) ddi_pathname(devi, curr);
110712116SVikram.Hegde@Sun.COM 
110812116SVikram.Hegde@Sun.COM 		vp = NULL;
110912116SVikram.Hegde@Sun.COM 		if (devfs_lookupname(curr, NULL, &vp) == 0 && vp) {
111012116SVikram.Hegde@Sun.COM 			dv = VTODV(vp);
111112116SVikram.Hegde@Sun.COM 			kmem_free(curr, MAXPATHLEN);
111212116SVikram.Hegde@Sun.COM 			goto found;
111312116SVikram.Hegde@Sun.COM 		}
111412116SVikram.Hegde@Sun.COM 		kmem_free(curr, MAXPATHLEN);
111512116SVikram.Hegde@Sun.COM 	}
111612116SVikram.Hegde@Sun.COM 
11170Sstevel@tonic-gate 	/*
11188912SChris.Horne@Sun.COM 	 * If we configured a hidden node, consider it notfound.
11198912SChris.Horne@Sun.COM 	 */
11208912SChris.Horne@Sun.COM 	if (ndi_dev_is_hidden_node(devi)) {
11218912SChris.Horne@Sun.COM 		ndi_rele_devi(devi);
11228912SChris.Horne@Sun.COM 		rv = ENOENT;
11238912SChris.Horne@Sun.COM 		goto notfound;
11248912SChris.Horne@Sun.COM 	}
11258912SChris.Horne@Sun.COM 
11268912SChris.Horne@Sun.COM 	/*
11270Sstevel@tonic-gate 	 * Don't make vhci clients visible under phci, unless we
11280Sstevel@tonic-gate 	 * are in miniroot.
11290Sstevel@tonic-gate 	 */
11300Sstevel@tonic-gate 	if (isminiroot == 0 && ddi_get_parent(devi) != pdevi) {
11310Sstevel@tonic-gate 		ndi_rele_devi(devi);
11320Sstevel@tonic-gate 		rv = ENOENT;
11330Sstevel@tonic-gate 		goto notfound;
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 
11361333Scth 	ASSERT(devi && i_ddi_devi_attached(devi));
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	/*
11390Sstevel@tonic-gate 	 * Invalidate cache to notice newly created minor nodes.
11400Sstevel@tonic-gate 	 */
11410Sstevel@tonic-gate 	rw_enter(&ddv->dv_contents, RW_WRITER);
11420Sstevel@tonic-gate 	ddv->dv_flags |= DV_BUILD;
11430Sstevel@tonic-gate 	rw_exit(&ddv->dv_contents);
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	/*
11460Sstevel@tonic-gate 	 * mkdir for nexus drivers and leaf nodes as well.  If we are racing
11470Sstevel@tonic-gate 	 * and create a duplicate, the duplicate will be destroyed below.
11480Sstevel@tonic-gate 	 */
11490Sstevel@tonic-gate 	if (mnm == NULL) {
11500Sstevel@tonic-gate 		dv = dv_mkdir(ddv, devi, nm);
11510Sstevel@tonic-gate 	} else {
11520Sstevel@tonic-gate 		/*
11537224Scth 		 * Allocate dmd first to avoid KM_SLEEP with active
11547224Scth 		 * ndi_devi_enter.
11550Sstevel@tonic-gate 		 */
11567224Scth 		dmd = kmem_zalloc(sizeof (*dmd), KM_SLEEP);
11577224Scth 		ndi_devi_enter(devi, &circ);
11580Sstevel@tonic-gate 		if (devi == clone_dip) {
11597224Scth 			/*
11607224Scth 			 * For clone minors, load the driver indicated by
11617224Scth 			 * minor name.
11627224Scth 			 */
11630Sstevel@tonic-gate 			dv = dv_clone_mknod(ddv, mnm + 1);
11640Sstevel@tonic-gate 		} else {
11650Sstevel@tonic-gate 			/*
11660Sstevel@tonic-gate 			 * Find minor node and make a dv_node
11670Sstevel@tonic-gate 			 */
11680Sstevel@tonic-gate 			if (dv_find_leafnode(devi, mnm + 1, dmd) == 0) {
11690Sstevel@tonic-gate 				dv = dv_mknod(ddv, devi, nm, dmd);
11700Sstevel@tonic-gate 				if (dmd->ddm_node_priv)
11710Sstevel@tonic-gate 					dpfree(dmd->ddm_node_priv);
11720Sstevel@tonic-gate 			}
11730Sstevel@tonic-gate 		}
11747224Scth 		ndi_devi_exit(devi, circ);
11757224Scth 		kmem_free(dmd, sizeof (*dmd));
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 	/*
11780Sstevel@tonic-gate 	 * Release hold from ndi_devi_config_one()
11790Sstevel@tonic-gate 	 */
11800Sstevel@tonic-gate 	ndi_rele_devi(devi);
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	if (dv == NULL) {
11830Sstevel@tonic-gate 		rv = ENOENT;
11840Sstevel@tonic-gate 		goto notfound;
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 	/*
11880Sstevel@tonic-gate 	 * We have released the dv_contents lock, need to check
11890Sstevel@tonic-gate 	 * if another thread already created a duplicate node
11900Sstevel@tonic-gate 	 */
11910Sstevel@tonic-gate 	rw_enter(&ddv->dv_contents, RW_WRITER);
11920Sstevel@tonic-gate 	if ((dup = dv_findbyname(ddv, nm)) == NULL) {
11930Sstevel@tonic-gate 		dv_insert(ddv, dv);
11940Sstevel@tonic-gate 	} else {
11950Sstevel@tonic-gate 		/*
11960Sstevel@tonic-gate 		 * Duplicate found, use the existing node
11970Sstevel@tonic-gate 		 */
11980Sstevel@tonic-gate 		VN_RELE(DVTOV(dv));
11990Sstevel@tonic-gate 		dv_destroy(dv, 0);
12000Sstevel@tonic-gate 		dv = dup;
12010Sstevel@tonic-gate 	}
12020Sstevel@tonic-gate 	goto founddv;
12030Sstevel@tonic-gate 	/*NOTREACHED*/
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate found:
12060Sstevel@tonic-gate 	/*
120710696SDavid.Hollister@Sun.COM 	 * Fail lookup of device that has now become hidden (typically via
120810696SDavid.Hollister@Sun.COM 	 * hot removal of open device).
120910696SDavid.Hollister@Sun.COM 	 */
121010696SDavid.Hollister@Sun.COM 	if (dv->dv_devi && ndi_dev_is_hidden_node(dv->dv_devi)) {
121110696SDavid.Hollister@Sun.COM 		dcmn_err2(("dv_find: nm %s failed: hidden/removed\n", nm));
121210696SDavid.Hollister@Sun.COM 		VN_RELE(vp);
121310696SDavid.Hollister@Sun.COM 		rv = ENOENT;
121410696SDavid.Hollister@Sun.COM 		goto notfound;
121510696SDavid.Hollister@Sun.COM 	}
121610696SDavid.Hollister@Sun.COM 
121710696SDavid.Hollister@Sun.COM 	/*
12180Sstevel@tonic-gate 	 * Skip non-kernel lookups of internal nodes.
12190Sstevel@tonic-gate 	 * This use of kcred to distinguish between user and
12200Sstevel@tonic-gate 	 * internal kernel lookups is unfortunate.  The information
12210Sstevel@tonic-gate 	 * provided by the seg argument to lookupnameat should
12220Sstevel@tonic-gate 	 * evolve into a lookup flag for filesystems that need
12230Sstevel@tonic-gate 	 * this distinction.
12240Sstevel@tonic-gate 	 */
12250Sstevel@tonic-gate 	if ((dv->dv_flags & DV_INTERNAL) && (cred != kcred)) {
122610696SDavid.Hollister@Sun.COM 		dcmn_err2(("dv_find: nm %s failed: internal\n", nm));
12270Sstevel@tonic-gate 		VN_RELE(vp);
12280Sstevel@tonic-gate 		rv = ENOENT;
12290Sstevel@tonic-gate 		goto notfound;
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	dcmn_err2(("dv_find: returning vp for nm %s\n", nm));
12330Sstevel@tonic-gate 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
12340Sstevel@tonic-gate 		/*
12350Sstevel@tonic-gate 		 * If vnode is a device, return special vnode instead
12360Sstevel@tonic-gate 		 * (though it knows all about -us- via sp->s_realvp,
12370Sstevel@tonic-gate 		 * sp->s_devvp, and sp->s_dip)
12380Sstevel@tonic-gate 		 */
12390Sstevel@tonic-gate 		*vpp = specvp_devfs(vp, vp->v_rdev, vp->v_type, cred,
12406260Sjg 		    dv->dv_devi);
12410Sstevel@tonic-gate 		VN_RELE(vp);
12420Sstevel@tonic-gate 		if (*vpp == NULLVP)
12430Sstevel@tonic-gate 			rv = ENOSYS;
12440Sstevel@tonic-gate 	} else
12450Sstevel@tonic-gate 		*vpp = vp;
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate notfound:
1248*12569SJakub.Jermar@Sun.COM 	if (was_busy) {
1249*12569SJakub.Jermar@Sun.COM 		/*
1250*12569SJakub.Jermar@Sun.COM 		 * Non-zero was_busy tells us that we are not in the
1251*12569SJakub.Jermar@Sun.COM 		 * devfs_clean() path which in turn means that we can afford
1252*12569SJakub.Jermar@Sun.COM 		 * to take the contents lock unconditionally.
1253*12569SJakub.Jermar@Sun.COM 		 */
1254*12569SJakub.Jermar@Sun.COM 		rw_enter(&ddv->dv_contents, RW_WRITER);
12550Sstevel@tonic-gate 		ddv->dv_busy--;
1256*12569SJakub.Jermar@Sun.COM 		rw_exit(&ddv->dv_contents);
1257*12569SJakub.Jermar@Sun.COM 	}
12580Sstevel@tonic-gate 	return (rv);
12590Sstevel@tonic-gate }
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate /*
12620Sstevel@tonic-gate  * The given directory node is out-of-date; that is, it has been
12630Sstevel@tonic-gate  * marked as needing to be rebuilt, possibly because some new devinfo
12640Sstevel@tonic-gate  * node has come into existence, or possibly because this is the first
12650Sstevel@tonic-gate  * time we've been here.
12660Sstevel@tonic-gate  */
12670Sstevel@tonic-gate void
dv_filldir(struct dv_node * ddv)12680Sstevel@tonic-gate dv_filldir(struct dv_node *ddv)
12690Sstevel@tonic-gate {
12707224Scth 	struct dv_node		*dv;
12717224Scth 	dev_info_t		*devi, *pdevi;
12727224Scth 	struct ddi_minor_data	*dmd;
12737224Scth 	char			devnm[MAXNAMELEN];
12747224Scth 	int			circ, ccirc;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	ASSERT(DVTOV(ddv)->v_type == VDIR);
12770Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
12780Sstevel@tonic-gate 	ASSERT(ddv->dv_flags & DV_BUILD);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	dcmn_err3(("dv_filldir: %s\n", ddv->dv_name));
12810Sstevel@tonic-gate 	if (DV_STALE(ddv))
12820Sstevel@tonic-gate 		return;
12830Sstevel@tonic-gate 	pdevi = ddv->dv_devi;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	if (ndi_devi_config(pdevi, NDI_NO_EVENT) != NDI_SUCCESS) {
12866712Stomee 		dcmn_err3(("dv_filldir: config error %s\n", ddv->dv_name));
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	ndi_devi_enter(pdevi, &circ);
12900Sstevel@tonic-gate 	for (devi = ddi_get_child(pdevi); devi;
12910Sstevel@tonic-gate 	    devi = ddi_get_next_sibling(devi)) {
129211719SMark.Logan@Sun.COM 		/*
129311719SMark.Logan@Sun.COM 		 * While we know enough to create a directory at DS_INITIALIZED,
129411719SMark.Logan@Sun.COM 		 * the directory will be empty until DS_ATTACHED. The existence
129511719SMark.Logan@Sun.COM 		 * of an empty directory dv_node will cause a devi_ref, which
129611719SMark.Logan@Sun.COM 		 * has caused problems for existing code paths doing offline/DR
129711719SMark.Logan@Sun.COM 		 * type operations - making devfs_clean coordination even more
129811719SMark.Logan@Sun.COM 		 * sensitive and error prone. Given this, the 'continue' below
129911719SMark.Logan@Sun.COM 		 * is checking for DS_ATTACHED instead of DS_INITIALIZED.
130011719SMark.Logan@Sun.COM 		 */
130111719SMark.Logan@Sun.COM 		if (i_ddi_node_state(devi) < DS_ATTACHED)
13020Sstevel@tonic-gate 			continue;
13030Sstevel@tonic-gate 
13048912SChris.Horne@Sun.COM 		/* skip hidden nodes */
13058912SChris.Horne@Sun.COM 		if (ndi_dev_is_hidden_node(devi))
13068912SChris.Horne@Sun.COM 			continue;
13078912SChris.Horne@Sun.COM 
13080Sstevel@tonic-gate 		dcmn_err3(("dv_filldir: node %s\n", ddi_node_name(devi)));
13090Sstevel@tonic-gate 
13107224Scth 		ndi_devi_enter(devi, &ccirc);
13110Sstevel@tonic-gate 		for (dmd = DEVI(devi)->devi_minor; dmd; dmd = dmd->next) {
13120Sstevel@tonic-gate 			char *addr;
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 			/*
13150Sstevel@tonic-gate 			 * Skip alias nodes, internal nodes, and nodes
13160Sstevel@tonic-gate 			 * without a name.  We allow DDM_DEFAULT nodes
13170Sstevel@tonic-gate 			 * to appear in readdir.
13180Sstevel@tonic-gate 			 */
13190Sstevel@tonic-gate 			if ((dmd->type == DDM_ALIAS) ||
13200Sstevel@tonic-gate 			    (dmd->type == DDM_INTERNAL_PATH) ||
13210Sstevel@tonic-gate 			    (dmd->ddm_name == NULL))
13220Sstevel@tonic-gate 				continue;
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 			addr = ddi_get_name_addr(devi);
13250Sstevel@tonic-gate 			if (addr && *addr)
13260Sstevel@tonic-gate 				(void) sprintf(devnm, "%s@%s:%s",
13270Sstevel@tonic-gate 				    ddi_node_name(devi), addr, dmd->ddm_name);
13280Sstevel@tonic-gate 			else
13290Sstevel@tonic-gate 				(void) sprintf(devnm, "%s:%s",
13300Sstevel@tonic-gate 				    ddi_node_name(devi), dmd->ddm_name);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 			if ((dv = dv_findbyname(ddv, devnm)) != NULL) {
13330Sstevel@tonic-gate 				/* dv_node already exists */
13340Sstevel@tonic-gate 				VN_RELE(DVTOV(dv));
13350Sstevel@tonic-gate 				continue;
13360Sstevel@tonic-gate 			}
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 			dv = dv_mknod(ddv, devi, devnm, dmd);
13390Sstevel@tonic-gate 			dv_insert(ddv, dv);
13400Sstevel@tonic-gate 			VN_RELE(DVTOV(dv));
13410Sstevel@tonic-gate 		}
13427224Scth 		ndi_devi_exit(devi, ccirc);
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 		(void) ddi_deviname(devi, devnm);
13450Sstevel@tonic-gate 		if ((dv = dv_findbyname(ddv, devnm + 1)) == NULL) {
13460Sstevel@tonic-gate 			/* directory doesn't exist */
13470Sstevel@tonic-gate 			dv = dv_mkdir(ddv, devi, devnm + 1);
13480Sstevel@tonic-gate 			dv_insert(ddv, dv);
13490Sstevel@tonic-gate 		}
13500Sstevel@tonic-gate 		VN_RELE(DVTOV(dv));
13510Sstevel@tonic-gate 	}
13520Sstevel@tonic-gate 	ndi_devi_exit(pdevi, circ);
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	ddv->dv_flags &= ~DV_BUILD;
13550Sstevel@tonic-gate }
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate /*
13580Sstevel@tonic-gate  * Given a directory node, clean out all the nodes beneath.
13590Sstevel@tonic-gate  *
13600Sstevel@tonic-gate  * VDIR:	Reinvoke to clean them, then delete the directory.
13610Sstevel@tonic-gate  * VCHR, VBLK:	Just blow them away.
13620Sstevel@tonic-gate  *
13630Sstevel@tonic-gate  * Mark the directories touched as in need of a rebuild, in case
13640Sstevel@tonic-gate  * we fall over part way through. When DV_CLEAN_FORCE is specified,
13650Sstevel@tonic-gate  * we mark referenced empty directories as stale to facilitate DR.
13660Sstevel@tonic-gate  */
13670Sstevel@tonic-gate int
dv_cleandir(struct dv_node * ddv,char * devnm,uint_t flags)13680Sstevel@tonic-gate dv_cleandir(struct dv_node *ddv, char *devnm, uint_t flags)
13690Sstevel@tonic-gate {
13707224Scth 	struct dv_node	*dv;
13717224Scth 	struct dv_node	*next;
13727224Scth 	struct vnode	*vp;
13737224Scth 	int		busy = 0;
13740Sstevel@tonic-gate 
13754895Sdf125853 	/*
13764895Sdf125853 	 * We should always be holding the tsd_clean_key here: dv_cleandir()
13774895Sdf125853 	 * will be called as a result of a devfs_clean request and the
13784895Sdf125853 	 * tsd_clean_key will be set in either in devfs_clean() itself or in
13794895Sdf125853 	 * devfs_clean_vhci().
13804895Sdf125853 	 *
13814895Sdf125853 	 * Since we are on the devfs_clean path, we return EBUSY if we cannot
13824895Sdf125853 	 * get the contents lock: if we blocked here we might deadlock against
13834895Sdf125853 	 * a thread performing top-down device configuration.
13844895Sdf125853 	 */
13854895Sdf125853 	ASSERT(tsd_get(devfs_clean_key));
13864895Sdf125853 
13870Sstevel@tonic-gate 	dcmn_err3(("dv_cleandir: %s\n", ddv->dv_name));
13880Sstevel@tonic-gate 
13894895Sdf125853 	if (!(flags & DV_CLEANDIR_LCK) &&
13904895Sdf125853 	    !rw_tryenter(&ddv->dv_contents, RW_WRITER))
13914895Sdf125853 		return (EBUSY);
13924895Sdf125853 
13936260Sjg 	for (dv = DV_FIRST_ENTRY(ddv); dv; dv = next) {
13946260Sjg 		next = DV_NEXT_ENTRY(ddv, dv);
13950Sstevel@tonic-gate 
1396647Scth 		/*
1397647Scth 		 * If devnm is specified, the non-minor portion of the
1398647Scth 		 * name must match devnm.
1399647Scth 		 */
1400647Scth 		if (devnm &&
1401647Scth 		    (strncmp(devnm, dv->dv_name, strlen(devnm)) ||
14020Sstevel@tonic-gate 		    (dv->dv_name[strlen(devnm)] != ':' &&
14030Sstevel@tonic-gate 		    dv->dv_name[strlen(devnm)] != '\0')))
14040Sstevel@tonic-gate 			continue;
14050Sstevel@tonic-gate 
1406647Scth 		/* check type of what we are cleaning */
14070Sstevel@tonic-gate 		vp = DVTOV(dv);
14080Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
1409647Scth 			/* recurse on directories */
1410647Scth 			rw_enter(&dv->dv_contents, RW_WRITER);
1411647Scth 			if (dv_cleandir(dv, NULL,
1412647Scth 			    flags | DV_CLEANDIR_LCK) == EBUSY) {
1413647Scth 				rw_exit(&dv->dv_contents);
1414647Scth 				goto set_busy;
1415647Scth 			}
1416647Scth 
1417647Scth 			/* A clean directory is an empty directory... */
1418647Scth 			ASSERT(dv->dv_nlink == 2);
1419647Scth 			mutex_enter(&vp->v_lock);
1420647Scth 			if (vp->v_count > 0) {
14210Sstevel@tonic-gate 				/*
1422647Scth 				 * ... but an empty directory can still have
1423647Scth 				 * references to it. If we have dv_busy or
1424647Scth 				 * DV_CLEAN_FORCE is *not* specified then a
1425647Scth 				 * referenced directory is considered busy.
14260Sstevel@tonic-gate 				 */
1427647Scth 				if (dv->dv_busy || !(flags & DV_CLEAN_FORCE)) {
1428647Scth 					mutex_exit(&vp->v_lock);
14290Sstevel@tonic-gate 					rw_exit(&dv->dv_contents);
1430647Scth 					goto set_busy;
14310Sstevel@tonic-gate 				}
1432647Scth 
1433647Scth 				/*
1434647Scth 				 * Mark referenced directory stale so that DR
1435647Scth 				 * will succeed even if a shell has
1436647Scth 				 * /devices/xxx as current directory (causing
1437647Scth 				 * VN_HOLD reference to an empty directory).
1438647Scth 				 */
1439647Scth 				ASSERT(!DV_STALE(dv));
1440647Scth 				ndi_rele_devi(dv->dv_devi);
1441647Scth 				dv->dv_devi = NULL;	/* mark DV_STALE */
14420Sstevel@tonic-gate 			}
14430Sstevel@tonic-gate 		} else {
1444647Scth 			ASSERT((vp->v_type == VCHR) || (vp->v_type == VBLK));
1445647Scth 			ASSERT(dv->dv_nlink == 1);	/* no hard links */
14460Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
14470Sstevel@tonic-gate 			if (vp->v_count > 0) {
1448647Scth 				mutex_exit(&vp->v_lock);
1449647Scth 				goto set_busy;
14500Sstevel@tonic-gate 			}
14510Sstevel@tonic-gate 		}
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 		/* unlink from directory */
14546260Sjg 		dv_unlink(ddv, dv);
14550Sstevel@tonic-gate 
1456647Scth 		/* drop locks */
1457647Scth 		mutex_exit(&vp->v_lock);
1458647Scth 		if (vp->v_type == VDIR)
1459647Scth 			rw_exit(&dv->dv_contents);
1460647Scth 
1461647Scth 		/* destroy vnode if ref count is zero */
1462647Scth 		if (vp->v_count == 0)
1463647Scth 			dv_destroy(dv, flags);
14640Sstevel@tonic-gate 
1465647Scth 		continue;
1466647Scth 
1467647Scth 		/*
1468647Scth 		 * If devnm is not NULL we return immediately on busy,
1469647Scth 		 * otherwise we continue destroying unused dv_node's.
1470647Scth 		 */
1471647Scth set_busy:	busy++;
1472647Scth 		if (devnm)
1473647Scth 			break;
14740Sstevel@tonic-gate 	}
1475647Scth 
14760Sstevel@tonic-gate 	/*
14770Sstevel@tonic-gate 	 * This code may be invoked to inform devfs that a new node has
14780Sstevel@tonic-gate 	 * been created in the kernel device tree. So we always set
14790Sstevel@tonic-gate 	 * the DV_BUILD flag to allow the next dv_filldir() to pick
14800Sstevel@tonic-gate 	 * the new devinfo nodes.
14810Sstevel@tonic-gate 	 */
14820Sstevel@tonic-gate 	ddv->dv_flags |= DV_BUILD;
14830Sstevel@tonic-gate 
1484647Scth 	if (!(flags & DV_CLEANDIR_LCK))
1485647Scth 		rw_exit(&ddv->dv_contents);
14860Sstevel@tonic-gate 
1487647Scth 	return (busy ? EBUSY : 0);
14880Sstevel@tonic-gate }
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate /*
14910Sstevel@tonic-gate  * Walk through the devfs hierarchy, correcting the permissions of
14920Sstevel@tonic-gate  * devices with default permissions that do not match those specified
14930Sstevel@tonic-gate  * by minor perm.  This can only be done for all drivers for now.
14940Sstevel@tonic-gate  */
14950Sstevel@tonic-gate static int
dv_reset_perm_dir(struct dv_node * ddv,uint_t flags)14960Sstevel@tonic-gate dv_reset_perm_dir(struct dv_node *ddv, uint_t flags)
14970Sstevel@tonic-gate {
14987224Scth 	struct dv_node	*dv;
14997224Scth 	struct vnode	*vp;
15007224Scth 	int		retval = 0;
15017224Scth 	struct vattr	*attrp;
15027224Scth 	mperm_t		mp;
15037224Scth 	char		*nm;
15047224Scth 	uid_t		old_uid;
15057224Scth 	gid_t		old_gid;
15067224Scth 	mode_t		old_mode;
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	rw_enter(&ddv->dv_contents, RW_WRITER);
15096260Sjg 	for (dv = DV_FIRST_ENTRY(ddv); dv; dv = DV_NEXT_ENTRY(ddv, dv)) {
15100Sstevel@tonic-gate 		int error = 0;
15110Sstevel@tonic-gate 		nm = dv->dv_name;
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 		rw_enter(&dv->dv_contents, RW_READER);
15140Sstevel@tonic-gate 		vp = DVTOV(dv);
15150Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
15160Sstevel@tonic-gate 			rw_exit(&dv->dv_contents);
15170Sstevel@tonic-gate 			if (dv_reset_perm_dir(dv, flags) != 0) {
15180Sstevel@tonic-gate 				error = EBUSY;
15190Sstevel@tonic-gate 			}
15200Sstevel@tonic-gate 		} else {
15210Sstevel@tonic-gate 			ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 			/*
15240Sstevel@tonic-gate 			 * Check for permissions from minor_perm
15250Sstevel@tonic-gate 			 * If there are none, we're done
15260Sstevel@tonic-gate 			 */
15270Sstevel@tonic-gate 			rw_exit(&dv->dv_contents);
15280Sstevel@tonic-gate 			if (dev_minorperm(dv->dv_devi, nm, &mp) != 0)
15290Sstevel@tonic-gate 				continue;
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate 			rw_enter(&dv->dv_contents, RW_READER);
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 			/*
15340Sstevel@tonic-gate 			 * Allow a node's permissions to be altered
15350Sstevel@tonic-gate 			 * permanently from the defaults by chmod,
15360Sstevel@tonic-gate 			 * using the shadow node as backing store.
15370Sstevel@tonic-gate 			 * Otherwise, update node to minor_perm permissions.
15380Sstevel@tonic-gate 			 */
15390Sstevel@tonic-gate 			if (dv->dv_attrvp == NULLVP) {
15400Sstevel@tonic-gate 				/*
15410Sstevel@tonic-gate 				 * No attribute vp, try to find one.
15420Sstevel@tonic-gate 				 */
15430Sstevel@tonic-gate 				dv_shadow_node(DVTOV(ddv), nm, vp,
15446260Sjg 				    NULL, NULLVP, kcred, 0);
15450Sstevel@tonic-gate 			}
15460Sstevel@tonic-gate 			if (dv->dv_attrvp != NULLVP || dv->dv_attr == NULL) {
15470Sstevel@tonic-gate 				rw_exit(&dv->dv_contents);
15480Sstevel@tonic-gate 				continue;
15490Sstevel@tonic-gate 			}
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 			attrp = dv->dv_attr;
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 			if (VATTRP_MP_CMP(attrp, mp) == 0) {
15540Sstevel@tonic-gate 				dcmn_err5(("%s: no perm change: "
15550Sstevel@tonic-gate 				    "%d %d 0%o\n", nm, attrp->va_uid,
15560Sstevel@tonic-gate 				    attrp->va_gid, attrp->va_mode));
15570Sstevel@tonic-gate 				rw_exit(&dv->dv_contents);
15580Sstevel@tonic-gate 				continue;
15590Sstevel@tonic-gate 			}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 			old_uid = attrp->va_uid;
15620Sstevel@tonic-gate 			old_gid = attrp->va_gid;
15630Sstevel@tonic-gate 			old_mode = attrp->va_mode;
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 			VATTRP_MP_MERGE(attrp, mp);
15660Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
15670Sstevel@tonic-gate 			if (vp->v_count > 0) {
15680Sstevel@tonic-gate 				error = EBUSY;
15690Sstevel@tonic-gate 			}
15700Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 			dcmn_err5(("%s: perm %d/%d/0%o -> %d/%d/0%o (%d)\n",
15730Sstevel@tonic-gate 			    nm, old_uid, old_gid, old_mode, attrp->va_uid,
15740Sstevel@tonic-gate 			    attrp->va_gid, attrp->va_mode, error));
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 			rw_exit(&dv->dv_contents);
15770Sstevel@tonic-gate 		}
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 		if (error != 0) {
15800Sstevel@tonic-gate 			retval = error;
15810Sstevel@tonic-gate 		}
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	ddv->dv_flags |= DV_BUILD;
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 	rw_exit(&ddv->dv_contents);
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	return (retval);
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate int
devfs_reset_perm(uint_t flags)15920Sstevel@tonic-gate devfs_reset_perm(uint_t flags)
15930Sstevel@tonic-gate {
15947224Scth 	struct dv_node	*dvp;
15957224Scth 	int		rval;
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	if ((dvp = devfs_dip_to_dvnode(ddi_root_node())) == NULL)
15980Sstevel@tonic-gate 		return (0);
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	VN_HOLD(DVTOV(dvp));
16010Sstevel@tonic-gate 	rval = dv_reset_perm_dir(dvp, flags);
16020Sstevel@tonic-gate 	VN_RELE(DVTOV(dvp));
16030Sstevel@tonic-gate 	return (rval);
16040Sstevel@tonic-gate }
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate /*
16070Sstevel@tonic-gate  * Clean up dangling devfs shadow nodes for removed
16080Sstevel@tonic-gate  * drivers so that, in the event the driver is re-added
16090Sstevel@tonic-gate  * to the system, newly created nodes won't incorrectly
16100Sstevel@tonic-gate  * pick up these stale shadow node permissions.
16110Sstevel@tonic-gate  *
16120Sstevel@tonic-gate  * This is accomplished by walking down the pathname
16130Sstevel@tonic-gate  * to the directory, starting at the root's attribute
16140Sstevel@tonic-gate  * node, then removing all minors matching the specified
16150Sstevel@tonic-gate  * node name.  Care must be taken to remove all entries
16160Sstevel@tonic-gate  * in a directory before the directory itself, so that
16170Sstevel@tonic-gate  * the clean-up associated with rem_drv'ing a nexus driver
16180Sstevel@tonic-gate  * does not inadvertently result in an inconsistent
16190Sstevel@tonic-gate  * filesystem underlying devfs.
16200Sstevel@tonic-gate  */
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate static int
devfs_remdrv_rmdir(vnode_t * dirvp,const char * dir,vnode_t * rvp)1623720Sjg devfs_remdrv_rmdir(vnode_t *dirvp, const char *dir, vnode_t *rvp)
16240Sstevel@tonic-gate {
16257224Scth 	int		error;
16267224Scth 	vnode_t		*vp;
16277224Scth 	int		eof;
16287224Scth 	struct iovec	iov;
16297224Scth 	struct uio	uio;
16307224Scth 	struct dirent64	*dp;
16317224Scth 	dirent64_t	*dbuf;
16327224Scth 	size_t		dlen;
16337224Scth 	size_t		dbuflen;
16347224Scth 	int		ndirents = 64;
16357224Scth 	char		*nm;
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate 	VN_HOLD(dirvp);
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 	dlen = ndirents * (sizeof (*dbuf));
16400Sstevel@tonic-gate 	dbuf = kmem_alloc(dlen, KM_SLEEP);
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	uio.uio_iov = &iov;
16430Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
16440Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
16450Sstevel@tonic-gate 	uio.uio_fmode = 0;
16460Sstevel@tonic-gate 	uio.uio_extflg = UIO_COPY_CACHED;
16470Sstevel@tonic-gate 	uio.uio_loffset = 0;
16480Sstevel@tonic-gate 	uio.uio_llimit = MAXOFFSET_T;
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	eof = 0;
16510Sstevel@tonic-gate 	error = 0;
16520Sstevel@tonic-gate 	while (!error && !eof) {
16530Sstevel@tonic-gate 		uio.uio_resid = dlen;
16540Sstevel@tonic-gate 		iov.iov_base = (char *)dbuf;
16550Sstevel@tonic-gate 		iov.iov_len = dlen;
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 		(void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
16585331Samw 		error = VOP_READDIR(dirvp, &uio, kcred, &eof, NULL, 0);
16590Sstevel@tonic-gate 		VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 		dbuflen = dlen - uio.uio_resid;
16620Sstevel@tonic-gate 
16630Sstevel@tonic-gate 		if (error || dbuflen == 0)
16640Sstevel@tonic-gate 			break;
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 		for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
16676260Sjg 		    dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 			nm = dp->d_name;
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 			if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
16720Sstevel@tonic-gate 				continue;
16730Sstevel@tonic-gate 
16745331Samw 			error = VOP_LOOKUP(dirvp, nm,
16755331Samw 			    &vp, NULL, 0, NULL, kcred, NULL, NULL, NULL);
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 			dsysdebug(error,
16780Sstevel@tonic-gate 			    ("rem_drv %s/%s lookup (%d)\n",
16790Sstevel@tonic-gate 			    dir, nm, error));
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 			if (error)
16820Sstevel@tonic-gate 				continue;
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 			ASSERT(vp->v_type == VDIR ||
16856260Sjg 			    vp->v_type == VCHR || vp->v_type == VBLK);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 			if (vp->v_type == VDIR) {
1688720Sjg 				error = devfs_remdrv_rmdir(vp, nm, rvp);
16890Sstevel@tonic-gate 				if (error == 0) {
16900Sstevel@tonic-gate 					error = VOP_RMDIR(dirvp,
16915331Samw 					    (char *)nm, rvp, kcred, NULL, 0);
16920Sstevel@tonic-gate 					dsysdebug(error,
16930Sstevel@tonic-gate 					    ("rem_drv %s/%s rmdir (%d)\n",
16940Sstevel@tonic-gate 					    dir, nm, error));
16950Sstevel@tonic-gate 				}
16960Sstevel@tonic-gate 			} else {
16975331Samw 				error = VOP_REMOVE(dirvp, (char *)nm, kcred,
16985331Samw 				    NULL, 0);
16990Sstevel@tonic-gate 				dsysdebug(error,
17000Sstevel@tonic-gate 				    ("rem_drv %s/%s remove (%d)\n",
17010Sstevel@tonic-gate 				    dir, nm, error));
17020Sstevel@tonic-gate 			}
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 			VN_RELE(vp);
17050Sstevel@tonic-gate 			if (error) {
17060Sstevel@tonic-gate 				goto exit;
17070Sstevel@tonic-gate 			}
17080Sstevel@tonic-gate 		}
17090Sstevel@tonic-gate 	}
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate exit:
17120Sstevel@tonic-gate 	VN_RELE(dirvp);
17130Sstevel@tonic-gate 	kmem_free(dbuf, dlen);
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	return (error);
17160Sstevel@tonic-gate }
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate int
devfs_remdrv_cleanup(const char * dir,const char * nodename)17190Sstevel@tonic-gate devfs_remdrv_cleanup(const char *dir, const char *nodename)
17200Sstevel@tonic-gate {
17217224Scth 	int		error;
17227224Scth 	vnode_t		*vp;
17237224Scth 	vnode_t		*dirvp;
17247224Scth 	int		eof;
17257224Scth 	struct iovec	iov;
17267224Scth 	struct uio	uio;
17277224Scth 	struct dirent64	*dp;
17287224Scth 	dirent64_t	*dbuf;
17297224Scth 	size_t		dlen;
17307224Scth 	size_t		dbuflen;
17317224Scth 	int		ndirents = 64;
17327224Scth 	int		nodenamelen = strlen(nodename);
17337224Scth 	char		*nm;
17347224Scth 	struct pathname	pn;
17357224Scth 	vnode_t		*rvp;	/* root node of the underlying attribute fs */
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	dcmn_err5(("devfs_remdrv_cleanup: %s %s\n", dir, nodename));
17380Sstevel@tonic-gate 
17390Sstevel@tonic-gate 	if (error = pn_get((char *)dir, UIO_SYSSPACE, &pn))
17400Sstevel@tonic-gate 		return (0);
17410Sstevel@tonic-gate 
1742720Sjg 	rvp = dvroot->dv_attrvp;
1743720Sjg 	ASSERT(rvp != NULL);
1744720Sjg 	VN_HOLD(rvp);
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 	pn_skipslash(&pn);
1747720Sjg 	dirvp = rvp;
17480Sstevel@tonic-gate 	VN_HOLD(dirvp);
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	nm = kmem_alloc(MAXNAMELEN, KM_SLEEP);
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	while (pn_pathleft(&pn)) {
17530Sstevel@tonic-gate 		ASSERT(dirvp->v_type == VDIR);
17540Sstevel@tonic-gate 		(void) pn_getcomponent(&pn, nm);
17550Sstevel@tonic-gate 		ASSERT((strcmp(nm, ".") != 0) && (strcmp(nm, "..") != 0));
17565331Samw 		error = VOP_LOOKUP(dirvp, nm, &vp, NULL, 0, rvp, kcred,
17575331Samw 		    NULL, NULL, NULL);
17580Sstevel@tonic-gate 		if (error) {
17590Sstevel@tonic-gate 			dcmn_err5(("remdrv_cleanup %s lookup error %d\n",
17600Sstevel@tonic-gate 			    nm, error));
17610Sstevel@tonic-gate 			VN_RELE(dirvp);
1762720Sjg 			if (dirvp != rvp)
1763720Sjg 				VN_RELE(rvp);
17640Sstevel@tonic-gate 			pn_free(&pn);
17650Sstevel@tonic-gate 			kmem_free(nm, MAXNAMELEN);
17660Sstevel@tonic-gate 			return (0);
17670Sstevel@tonic-gate 		}
17680Sstevel@tonic-gate 		VN_RELE(dirvp);
17690Sstevel@tonic-gate 		dirvp = vp;
17700Sstevel@tonic-gate 		pn_skipslash(&pn);
17710Sstevel@tonic-gate 	}
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 	ASSERT(dirvp->v_type == VDIR);
1774720Sjg 	if (dirvp != rvp)
1775720Sjg 		VN_RELE(rvp);
17760Sstevel@tonic-gate 	pn_free(&pn);
17770Sstevel@tonic-gate 	kmem_free(nm, MAXNAMELEN);
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	dlen = ndirents * (sizeof (*dbuf));
17800Sstevel@tonic-gate 	dbuf = kmem_alloc(dlen, KM_SLEEP);
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	uio.uio_iov = &iov;
17830Sstevel@tonic-gate 	uio.uio_iovcnt = 1;
17840Sstevel@tonic-gate 	uio.uio_segflg = UIO_SYSSPACE;
17850Sstevel@tonic-gate 	uio.uio_fmode = 0;
17860Sstevel@tonic-gate 	uio.uio_extflg = UIO_COPY_CACHED;
17870Sstevel@tonic-gate 	uio.uio_loffset = 0;
17880Sstevel@tonic-gate 	uio.uio_llimit = MAXOFFSET_T;
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 	eof = 0;
17910Sstevel@tonic-gate 	error = 0;
17920Sstevel@tonic-gate 	while (!error && !eof) {
17930Sstevel@tonic-gate 		uio.uio_resid = dlen;
17940Sstevel@tonic-gate 		iov.iov_base = (char *)dbuf;
17950Sstevel@tonic-gate 		iov.iov_len = dlen;
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 		(void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
17985331Samw 		error = VOP_READDIR(dirvp, &uio, kcred, &eof, NULL, 0);
17990Sstevel@tonic-gate 		VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 		dbuflen = dlen - uio.uio_resid;
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 		if (error || dbuflen == 0)
18040Sstevel@tonic-gate 			break;
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 		for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
18076260Sjg 		    dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 			nm = dp->d_name;
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 			if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
18120Sstevel@tonic-gate 				continue;
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate 			if (strncmp(nm, nodename, nodenamelen) != 0)
18150Sstevel@tonic-gate 				continue;
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate 			error = VOP_LOOKUP(dirvp, nm, &vp,
18185331Samw 			    NULL, 0, NULL, kcred, NULL, NULL, NULL);
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 			dsysdebug(error,
18210Sstevel@tonic-gate 			    ("rem_drv %s/%s lookup (%d)\n",
18220Sstevel@tonic-gate 			    dir, nm, error));
18230Sstevel@tonic-gate 
18240Sstevel@tonic-gate 			if (error)
18250Sstevel@tonic-gate 				continue;
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 			ASSERT(vp->v_type == VDIR ||
18286260Sjg 			    vp->v_type == VCHR || vp->v_type == VBLK);
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 			if (vp->v_type == VDIR) {
1831720Sjg 				error = devfs_remdrv_rmdir(vp, nm, rvp);
18320Sstevel@tonic-gate 				if (error == 0) {
18335331Samw 					error = VOP_RMDIR(dirvp, (char *)nm,
18345331Samw 					    rvp, kcred, NULL, 0);
18350Sstevel@tonic-gate 					dsysdebug(error,
18360Sstevel@tonic-gate 					    ("rem_drv %s/%s rmdir (%d)\n",
18370Sstevel@tonic-gate 					    dir, nm, error));
18380Sstevel@tonic-gate 				}
18390Sstevel@tonic-gate 			} else {
18405331Samw 				error = VOP_REMOVE(dirvp, (char *)nm, kcred,
18415331Samw 				    NULL, 0);
18420Sstevel@tonic-gate 				dsysdebug(error,
18430Sstevel@tonic-gate 				    ("rem_drv %s/%s remove (%d)\n",
18440Sstevel@tonic-gate 				    dir, nm, error));
18450Sstevel@tonic-gate 			}
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 			VN_RELE(vp);
18480Sstevel@tonic-gate 			if (error)
18490Sstevel@tonic-gate 				goto exit;
18500Sstevel@tonic-gate 		}
18510Sstevel@tonic-gate 	}
18520Sstevel@tonic-gate 
18530Sstevel@tonic-gate exit:
18540Sstevel@tonic-gate 	VN_RELE(dirvp);
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	kmem_free(dbuf, dlen);
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	return (0);
18590Sstevel@tonic-gate }
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate struct dv_list {
18620Sstevel@tonic-gate 	struct dv_node	*dv;
18630Sstevel@tonic-gate 	struct dv_list	*next;
18640Sstevel@tonic-gate };
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate void
dv_walk(struct dv_node * ddv,char * devnm,void (* callback)(struct dv_node *,void *),void * arg)18670Sstevel@tonic-gate dv_walk(
18680Sstevel@tonic-gate 	struct dv_node	*ddv,
18690Sstevel@tonic-gate 	char		*devnm,
18700Sstevel@tonic-gate 	void		(*callback)(struct dv_node *, void *),
18710Sstevel@tonic-gate 	void		*arg)
18720Sstevel@tonic-gate {
18730Sstevel@tonic-gate 	struct vnode	*dvp;
18740Sstevel@tonic-gate 	struct dv_node	*dv;
18750Sstevel@tonic-gate 	struct dv_list	*head, *tail, *next;
1876647Scth 	int		len;
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	dcmn_err3(("dv_walk: ddv = %s, devnm = %s\n",
18790Sstevel@tonic-gate 	    ddv->dv_name, devnm ? devnm : "<null>"));
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 	dvp = DVTOV(ddv);
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 	ASSERT(dvp->v_type == VDIR);
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	head = tail = next = NULL;
18860Sstevel@tonic-gate 
1887647Scth 	rw_enter(&ddv->dv_contents, RW_READER);
18880Sstevel@tonic-gate 	mutex_enter(&dvp->v_lock);
18896260Sjg 	for (dv = DV_FIRST_ENTRY(ddv); dv; dv = DV_NEXT_ENTRY(ddv, dv)) {
18900Sstevel@tonic-gate 		/*
18910Sstevel@tonic-gate 		 * If devnm is not NULL and is not the empty string,
18920Sstevel@tonic-gate 		 * select only dv_nodes with matching non-minor name
18930Sstevel@tonic-gate 		 */
18940Sstevel@tonic-gate 		if (devnm && (len = strlen(devnm)) &&
18950Sstevel@tonic-gate 		    (strncmp(devnm, dv->dv_name, len) ||
18960Sstevel@tonic-gate 		    (dv->dv_name[len] != ':' && dv->dv_name[len] != '\0')))
18970Sstevel@tonic-gate 			continue;
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 		callback(dv, arg);
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate 		if (DVTOV(dv)->v_type != VDIR)
19020Sstevel@tonic-gate 			continue;
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 		next = kmem_zalloc(sizeof (*next), KM_SLEEP);
19050Sstevel@tonic-gate 		next->dv = dv;
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 		if (tail)
19080Sstevel@tonic-gate 			tail->next = next;
19090Sstevel@tonic-gate 		else
19100Sstevel@tonic-gate 			head = next;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 		tail = next;
19130Sstevel@tonic-gate 	}
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 	while (head) {
19160Sstevel@tonic-gate 		dv_walk(head->dv, NULL, callback, arg);
19170Sstevel@tonic-gate 		next = head->next;
19180Sstevel@tonic-gate 		kmem_free(head, sizeof (*head));
19190Sstevel@tonic-gate 		head = next;
19200Sstevel@tonic-gate 	}
19210Sstevel@tonic-gate 	rw_exit(&ddv->dv_contents);
19220Sstevel@tonic-gate 	mutex_exit(&dvp->v_lock);
19230Sstevel@tonic-gate }
1924