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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*1333Scth * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * miscellaneous routines for the devfs 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <sys/types.h> 340Sstevel@tonic-gate #include <sys/param.h> 350Sstevel@tonic-gate #include <sys/t_lock.h> 360Sstevel@tonic-gate #include <sys/systm.h> 370Sstevel@tonic-gate #include <sys/sysmacros.h> 380Sstevel@tonic-gate #include <sys/user.h> 390Sstevel@tonic-gate #include <sys/time.h> 400Sstevel@tonic-gate #include <sys/vfs.h> 410Sstevel@tonic-gate #include <sys/vnode.h> 420Sstevel@tonic-gate #include <sys/file.h> 430Sstevel@tonic-gate #include <sys/fcntl.h> 440Sstevel@tonic-gate #include <sys/flock.h> 450Sstevel@tonic-gate #include <sys/kmem.h> 460Sstevel@tonic-gate #include <sys/uio.h> 470Sstevel@tonic-gate #include <sys/errno.h> 480Sstevel@tonic-gate #include <sys/stat.h> 490Sstevel@tonic-gate #include <sys/cred.h> 500Sstevel@tonic-gate #include <sys/dirent.h> 510Sstevel@tonic-gate #include <sys/pathname.h> 520Sstevel@tonic-gate #include <sys/cmn_err.h> 530Sstevel@tonic-gate #include <sys/debug.h> 540Sstevel@tonic-gate #include <sys/modctl.h> 550Sstevel@tonic-gate #include <fs/fs_subr.h> 560Sstevel@tonic-gate #include <sys/fs/dv_node.h> 570Sstevel@tonic-gate #include <sys/fs/snode.h> 580Sstevel@tonic-gate #include <sys/sunndi.h> 590Sstevel@tonic-gate #include <sys/sunmdi.h> 600Sstevel@tonic-gate #include <sys/conf.h> 610Sstevel@tonic-gate 620Sstevel@tonic-gate #ifdef DEBUG 630Sstevel@tonic-gate int devfs_debug = 0x0; 640Sstevel@tonic-gate #endif 650Sstevel@tonic-gate 660Sstevel@tonic-gate const char dvnm[] = "devfs"; 670Sstevel@tonic-gate kmem_cache_t *dv_node_cache; /* dv_node cache */ 680Sstevel@tonic-gate uint_t devfs_clean_key; 690Sstevel@tonic-gate struct dv_node *dvroot; 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* prototype memory vattrs */ 720Sstevel@tonic-gate vattr_t dv_vattr_dir = { 730Sstevel@tonic-gate AT_TYPE|AT_MODE|AT_UID|AT_GID, /* va_mask */ 740Sstevel@tonic-gate VDIR, /* va_type */ 750Sstevel@tonic-gate DV_DIRMODE_DEFAULT, /* va_mode */ 760Sstevel@tonic-gate DV_UID_DEFAULT, /* va_uid */ 770Sstevel@tonic-gate DV_GID_DEFAULT, /* va_gid */ 780Sstevel@tonic-gate 0, /* va_fsid; */ 790Sstevel@tonic-gate 0, /* va_nodeid; */ 800Sstevel@tonic-gate 0, /* va_nlink; */ 810Sstevel@tonic-gate 0, /* va_size; */ 820Sstevel@tonic-gate 0, /* va_atime; */ 830Sstevel@tonic-gate 0, /* va_mtime; */ 840Sstevel@tonic-gate 0, /* va_ctime; */ 850Sstevel@tonic-gate 0, /* va_rdev; */ 860Sstevel@tonic-gate 0, /* va_blksize; */ 870Sstevel@tonic-gate 0, /* va_nblocks; */ 880Sstevel@tonic-gate 0, /* va_seq; */ 890Sstevel@tonic-gate }; 900Sstevel@tonic-gate 910Sstevel@tonic-gate vattr_t dv_vattr_file = { 920Sstevel@tonic-gate AT_TYPE|AT_MODE|AT_SIZE|AT_UID|AT_GID|AT_RDEV, /* va_mask */ 930Sstevel@tonic-gate 0, /* va_type */ 940Sstevel@tonic-gate DV_DEVMODE_DEFAULT, /* va_mode */ 950Sstevel@tonic-gate DV_UID_DEFAULT, /* va_uid */ 960Sstevel@tonic-gate DV_GID_DEFAULT, /* va_gid */ 970Sstevel@tonic-gate 0, /* va_fsid; */ 980Sstevel@tonic-gate 0, /* va_nodeid; */ 990Sstevel@tonic-gate 0, /* va_nlink; */ 1000Sstevel@tonic-gate 0, /* va_size; */ 1010Sstevel@tonic-gate 0, /* va_atime; */ 1020Sstevel@tonic-gate 0, /* va_mtime; */ 1030Sstevel@tonic-gate 0, /* va_ctime; */ 1040Sstevel@tonic-gate 0, /* va_rdev; */ 1050Sstevel@tonic-gate 0, /* va_blksize; */ 1060Sstevel@tonic-gate 0, /* va_nblocks; */ 1070Sstevel@tonic-gate 0, /* va_seq; */ 1080Sstevel@tonic-gate }; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate vattr_t dv_vattr_priv = { 1110Sstevel@tonic-gate AT_TYPE|AT_MODE|AT_SIZE|AT_UID|AT_GID|AT_RDEV, /* va_mask */ 1120Sstevel@tonic-gate 0, /* va_type */ 1130Sstevel@tonic-gate DV_DEVMODE_PRIV, /* va_mode */ 1140Sstevel@tonic-gate DV_UID_DEFAULT, /* va_uid */ 1150Sstevel@tonic-gate DV_GID_DEFAULT, /* va_gid */ 1160Sstevel@tonic-gate 0, /* va_fsid; */ 1170Sstevel@tonic-gate 0, /* va_nodeid; */ 1180Sstevel@tonic-gate 0, /* va_nlink; */ 1190Sstevel@tonic-gate 0, /* va_size; */ 1200Sstevel@tonic-gate 0, /* va_atime; */ 1210Sstevel@tonic-gate 0, /* va_mtime; */ 1220Sstevel@tonic-gate 0, /* va_ctime; */ 1230Sstevel@tonic-gate 0, /* va_rdev; */ 1240Sstevel@tonic-gate 0, /* va_blksize; */ 1250Sstevel@tonic-gate 0, /* va_nblocks; */ 1260Sstevel@tonic-gate 0, /* va_seq; */ 1270Sstevel@tonic-gate }; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate extern dev_info_t *clone_dip; 1300Sstevel@tonic-gate extern major_t clone_major; 1310Sstevel@tonic-gate extern struct dev_ops *ddi_hold_driver(major_t); 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate /* 1340Sstevel@tonic-gate * dv_node cache constructor, destructor, can cache creation 1350Sstevel@tonic-gate */ 1360Sstevel@tonic-gate /*ARGSUSED1*/ 1370Sstevel@tonic-gate static int 1380Sstevel@tonic-gate i_dv_node_ctor(void *buf, void *cfarg, int flag) 1390Sstevel@tonic-gate { 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)); 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate /* initialize persistent parts of dv_node */ 1460Sstevel@tonic-gate rw_init(&dv->dv_contents, NULL, RW_DEFAULT, NULL); 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate /* allocate vnode and initialize link back to dv_node */ 1490Sstevel@tonic-gate dv->dv_vnode = vn_alloc(KM_SLEEP); 1500Sstevel@tonic-gate vp = DVTOV(dv); 1510Sstevel@tonic-gate vp->v_data = (caddr_t)dv; 1520Sstevel@tonic-gate return (0); 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate /* dev_info node destructor for kmem cache */ 1560Sstevel@tonic-gate /*ARGSUSED1*/ 1570Sstevel@tonic-gate static void 1580Sstevel@tonic-gate i_dv_node_dtor(void *buf, void *arg) 1590Sstevel@tonic-gate { 1600Sstevel@tonic-gate struct dv_node *dv = (struct dv_node *)buf; 1610Sstevel@tonic-gate struct vnode *vp = DVTOV(dv); 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate rw_destroy(&dv->dv_contents); 1640Sstevel@tonic-gate vn_invalid(vp); 1650Sstevel@tonic-gate vn_free(vp); 1660Sstevel@tonic-gate } 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate /* initialize dev_info node cache */ 1700Sstevel@tonic-gate void 1710Sstevel@tonic-gate dv_node_cache_init() 1720Sstevel@tonic-gate { 1730Sstevel@tonic-gate ASSERT(dv_node_cache == NULL); 1740Sstevel@tonic-gate dv_node_cache = kmem_cache_create("dv_node_cache", 1750Sstevel@tonic-gate sizeof (struct dv_node), 0, i_dv_node_ctor, i_dv_node_dtor, 1760Sstevel@tonic-gate NULL, NULL, NULL, 0); 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate tsd_create(&devfs_clean_key, NULL); 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate /* initialize dev_info node cache */ 1820Sstevel@tonic-gate void 1830Sstevel@tonic-gate dv_node_cache_fini() 1840Sstevel@tonic-gate { 1850Sstevel@tonic-gate ASSERT(dv_node_cache != NULL); 1860Sstevel@tonic-gate kmem_cache_destroy(dv_node_cache); 1870Sstevel@tonic-gate dv_node_cache = NULL; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate tsd_destroy(&devfs_clean_key); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * dv_mkino - Generate a unique inode number for devfs nodes. 1940Sstevel@tonic-gate * 1950Sstevel@tonic-gate * Although ino_t is 64 bits, the inode number is truncated to 32 bits for 32 1960Sstevel@tonic-gate * bit non-LARGEFILE applications. This means that there is a requirement to 1970Sstevel@tonic-gate * maintain the inode number as a 32 bit value or applications will have 1980Sstevel@tonic-gate * stat(2) calls fail with EOVERFLOW. We form a 32 bit inode number from the 1990Sstevel@tonic-gate * dev_t. but if the minor number is larger than L_MAXMIN32 we fold extra minor 2000Sstevel@tonic-gate * 2010Sstevel@tonic-gate * To generate inode numbers for directories, we assume that we will never use 2020Sstevel@tonic-gate * more than half the major space - this allows for ~8190 drivers. We use this 2030Sstevel@tonic-gate * upper major number space to allocate inode numbers for directories by 2040Sstevel@tonic-gate * encoding the major and instance into this space. 2050Sstevel@tonic-gate * 2060Sstevel@tonic-gate * We also skew the result so that inode 2 is reserved for the root of the file 2070Sstevel@tonic-gate * system. 2080Sstevel@tonic-gate * 2090Sstevel@tonic-gate * As part of the future support for 64-bit dev_t APIs, the upper minor bits 2100Sstevel@tonic-gate * should be folded into the high inode bits by adding the following code 2110Sstevel@tonic-gate * after "ino |= 1": 2120Sstevel@tonic-gate * 2130Sstevel@tonic-gate * #if (L_BITSMINOR32 != L_BITSMINOR) 2140Sstevel@tonic-gate * |* fold overflow minor bits into high bits of inode number *| 2150Sstevel@tonic-gate * ino |= ((ino_t)(minor >> L_BITSMINOR32)) << L_BITSMINOR; 2160Sstevel@tonic-gate * #endif |* (L_BITSMINOR32 != L_BITSMINOR) *| 2170Sstevel@tonic-gate * 2180Sstevel@tonic-gate * This way only applications that use devices that overflow their minor 2190Sstevel@tonic-gate * space will have an application level impact. 2200Sstevel@tonic-gate */ 2210Sstevel@tonic-gate static ino_t 2220Sstevel@tonic-gate dv_mkino(dev_info_t *devi, vtype_t typ, dev_t dev) 2230Sstevel@tonic-gate { 2240Sstevel@tonic-gate major_t major; 2250Sstevel@tonic-gate minor_t minor; 2260Sstevel@tonic-gate ino_t ino; 2270Sstevel@tonic-gate static int warn; 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate if (typ == VDIR) { 2300Sstevel@tonic-gate major = ((L_MAXMAJ32 + 1) >> 1) + DEVI(devi)->devi_major; 2310Sstevel@tonic-gate minor = ddi_get_instance(devi); 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate /* makedevice32 in high half of major number space */ 2340Sstevel@tonic-gate ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32)); 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate major = DEVI(devi)->devi_major; 2370Sstevel@tonic-gate } else { 2380Sstevel@tonic-gate major = getmajor(dev); 2390Sstevel@tonic-gate minor = getminor(dev); 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate /* makedevice32 */ 2420Sstevel@tonic-gate ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32)); 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* make ino for VCHR different than VBLK */ 2450Sstevel@tonic-gate ino <<= 1; 2460Sstevel@tonic-gate if (typ == VCHR) 2470Sstevel@tonic-gate ino |= 1; 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate ino += DV_ROOTINO + 1; /* skew */ 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * diagnose things a little early because adding the skew to a large 2540Sstevel@tonic-gate * minor number could roll over the major. 2550Sstevel@tonic-gate */ 2560Sstevel@tonic-gate if ((major >= (L_MAXMAJ32 >> 1)) && (warn == 0)) { 2570Sstevel@tonic-gate warn = 1; 2580Sstevel@tonic-gate cmn_err(CE_WARN, "%s: inode numbers are not unique", dvnm); 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate return (ino); 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate /* 2650Sstevel@tonic-gate * dv_mkroot 2660Sstevel@tonic-gate * 2670Sstevel@tonic-gate * Build the first VDIR dv_node. 2680Sstevel@tonic-gate */ 2690Sstevel@tonic-gate struct dv_node * 2700Sstevel@tonic-gate dv_mkroot(struct vfs *vfsp, dev_t devfsdev) 2710Sstevel@tonic-gate { 2720Sstevel@tonic-gate struct dv_node *dv; 2730Sstevel@tonic-gate struct vnode *vp; 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate ASSERT(ddi_root_node() != NULL); 2760Sstevel@tonic-gate ASSERT(dv_node_cache != NULL); 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate dcmn_err3(("dv_mkroot\n")); 2790Sstevel@tonic-gate dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP); 2800Sstevel@tonic-gate vp = DVTOV(dv); 2810Sstevel@tonic-gate vn_reinit(vp); 2820Sstevel@tonic-gate vp->v_flag = VROOT; 2830Sstevel@tonic-gate vp->v_vfsp = vfsp; 2840Sstevel@tonic-gate vp->v_type = VDIR; 2850Sstevel@tonic-gate vp->v_rdev = devfsdev; 2860Sstevel@tonic-gate vn_setops(vp, dv_vnodeops); 2870Sstevel@tonic-gate vn_exists(vp); 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate dvroot = dv; 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate dv->dv_name = NULL; /* not needed */ 2920Sstevel@tonic-gate dv->dv_namelen = 0; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate dv->dv_devi = ddi_root_node(); 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate dv->dv_ino = DV_ROOTINO; 2970Sstevel@tonic-gate dv->dv_nlink = 2; /* name + . (no dv_insert) */ 2980Sstevel@tonic-gate dv->dv_dotdot = dv; /* .. == self */ 2990Sstevel@tonic-gate dv->dv_attrvp = NULLVP; 3000Sstevel@tonic-gate dv->dv_attr = NULL; 3010Sstevel@tonic-gate dv->dv_flags = DV_BUILD; 3020Sstevel@tonic-gate dv->dv_priv = NULL; 3030Sstevel@tonic-gate dv->dv_busy = 0; 3040Sstevel@tonic-gate dv->dv_dflt_mode = 0; 3050Sstevel@tonic-gate 3060Sstevel@tonic-gate return (dv); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate /* 3100Sstevel@tonic-gate * dv_mkdir 3110Sstevel@tonic-gate * 3120Sstevel@tonic-gate * Given an probed or attached nexus node, create a VDIR dv_node. 3130Sstevel@tonic-gate * No dv_attrvp is created at this point. 3140Sstevel@tonic-gate */ 3150Sstevel@tonic-gate struct dv_node * 3160Sstevel@tonic-gate dv_mkdir(struct dv_node *ddv, dev_info_t *devi, char *nm) 3170Sstevel@tonic-gate { 3180Sstevel@tonic-gate struct dv_node *dv; 3190Sstevel@tonic-gate struct vnode *vp; 3200Sstevel@tonic-gate size_t nmlen; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate ASSERT((devi)); 3230Sstevel@tonic-gate dcmn_err4(("dv_mkdir: %s\n", nm)); 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP); 3260Sstevel@tonic-gate nmlen = strlen(nm) + 1; 3270Sstevel@tonic-gate dv->dv_name = kmem_alloc(nmlen, KM_SLEEP); 3280Sstevel@tonic-gate bcopy(nm, dv->dv_name, nmlen); 3290Sstevel@tonic-gate dv->dv_namelen = nmlen - 1; /* '\0' not included */ 3300Sstevel@tonic-gate vp = DVTOV(dv); 3310Sstevel@tonic-gate vn_reinit(vp); 3320Sstevel@tonic-gate vp->v_flag = 0; 3330Sstevel@tonic-gate vp->v_vfsp = DVTOV(ddv)->v_vfsp; 3340Sstevel@tonic-gate vp->v_type = VDIR; 3350Sstevel@tonic-gate vp->v_rdev = DVTOV(ddv)->v_rdev; 3360Sstevel@tonic-gate vn_setops(vp, vn_getops(DVTOV(ddv))); 3370Sstevel@tonic-gate vn_exists(vp); 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate dv->dv_devi = devi; 3400Sstevel@tonic-gate ndi_hold_devi(devi); 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate dv->dv_ino = dv_mkino(devi, VDIR, NODEV); 3430Sstevel@tonic-gate dv->dv_nlink = 0; /* updated on insert */ 3440Sstevel@tonic-gate dv->dv_dotdot = ddv; 3450Sstevel@tonic-gate dv->dv_attrvp = NULLVP; 3460Sstevel@tonic-gate dv->dv_attr = NULL; 3470Sstevel@tonic-gate dv->dv_flags = DV_BUILD; 3480Sstevel@tonic-gate dv->dv_priv = NULL; 3490Sstevel@tonic-gate dv->dv_busy = 0; 3500Sstevel@tonic-gate dv->dv_dflt_mode = 0; 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate return (dv); 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate /* 3560Sstevel@tonic-gate * dv_mknod 3570Sstevel@tonic-gate * 3580Sstevel@tonic-gate * Given a minor node, create a VCHR or VBLK dv_node. 3590Sstevel@tonic-gate * No dv_attrvp is created at this point. 3600Sstevel@tonic-gate */ 3610Sstevel@tonic-gate static struct dv_node * 3620Sstevel@tonic-gate dv_mknod(struct dv_node *ddv, dev_info_t *devi, char *nm, 3630Sstevel@tonic-gate struct ddi_minor_data *dmd) 3640Sstevel@tonic-gate { 3650Sstevel@tonic-gate struct dv_node *dv; 3660Sstevel@tonic-gate struct vnode *vp; 3670Sstevel@tonic-gate size_t nmlen; 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate dcmn_err4(("dv_mknod: %s\n", nm)); 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP); 3720Sstevel@tonic-gate nmlen = strlen(nm) + 1; 3730Sstevel@tonic-gate dv->dv_name = kmem_alloc(nmlen, KM_SLEEP); 3740Sstevel@tonic-gate bcopy(nm, dv->dv_name, nmlen); 3750Sstevel@tonic-gate dv->dv_namelen = nmlen - 1; /* no '\0' */ 3760Sstevel@tonic-gate vp = DVTOV(dv); 3770Sstevel@tonic-gate vn_reinit(vp); 3780Sstevel@tonic-gate vp->v_flag = 0; 3790Sstevel@tonic-gate vp->v_vfsp = DVTOV(ddv)->v_vfsp; 3800Sstevel@tonic-gate vp->v_type = dmd->ddm_spec_type == S_IFCHR ? VCHR : VBLK; 3810Sstevel@tonic-gate vp->v_rdev = dmd->ddm_dev; 3820Sstevel@tonic-gate vn_setops(vp, vn_getops(DVTOV(ddv))); 3830Sstevel@tonic-gate vn_exists(vp); 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate ASSERT(MUTEX_HELD(&DEVI(devi)->devi_lock)); 3860Sstevel@tonic-gate dv->dv_devi = devi; 3870Sstevel@tonic-gate DEVI(devi)->devi_ref++; 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate dv->dv_ino = dv_mkino(devi, vp->v_type, vp->v_rdev); 3900Sstevel@tonic-gate dv->dv_nlink = 0; /* updated on insert */ 3910Sstevel@tonic-gate dv->dv_dotdot = ddv; 3920Sstevel@tonic-gate dv->dv_attrvp = NULLVP; 3930Sstevel@tonic-gate dv->dv_attr = NULL; 3940Sstevel@tonic-gate dv->dv_flags = 0; 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate if (dmd->type == DDM_INTERNAL_PATH) 3970Sstevel@tonic-gate dv->dv_flags |= DV_INTERNAL; 3980Sstevel@tonic-gate if (dmd->ddm_flags & DM_NO_FSPERM) 3990Sstevel@tonic-gate dv->dv_flags |= DV_NO_FSPERM; 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate dv->dv_priv = dmd->ddm_node_priv; 4020Sstevel@tonic-gate if (dv->dv_priv) 4030Sstevel@tonic-gate dphold(dv->dv_priv); 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate /* 4060Sstevel@tonic-gate * Minors created with ddi_create_priv_minor_node can specify 4070Sstevel@tonic-gate * a default mode permission other than the devfs default. 4080Sstevel@tonic-gate */ 4090Sstevel@tonic-gate if (dv->dv_priv || dv->dv_flags & DV_NO_FSPERM) { 4100Sstevel@tonic-gate dcmn_err5(("%s: dv_mknod default priv mode 0%o\n", 4110Sstevel@tonic-gate dv->dv_name, dmd->ddm_priv_mode)); 4120Sstevel@tonic-gate dv->dv_flags |= DV_DFLT_MODE; 4130Sstevel@tonic-gate dv->dv_dflt_mode = dmd->ddm_priv_mode & S_IAMB; 4140Sstevel@tonic-gate } 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate return (dv); 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate /* 4200Sstevel@tonic-gate * dv_destroy 4210Sstevel@tonic-gate * 4220Sstevel@tonic-gate * Destroy what we created in dv_mkdir or dv_mknod. 4230Sstevel@tonic-gate * In the case of a *referenced* directory, do nothing. 4240Sstevel@tonic-gate */ 4250Sstevel@tonic-gate /*ARGSUSED1*/ 4260Sstevel@tonic-gate void 4270Sstevel@tonic-gate dv_destroy(struct dv_node *dv, uint_t flags) 4280Sstevel@tonic-gate { 4290Sstevel@tonic-gate vnode_t *vp = DVTOV(dv); 4300Sstevel@tonic-gate ASSERT(dv->dv_nlink == 0); /* no references */ 4310Sstevel@tonic-gate ASSERT(dv->dv_next == NULL); /* unlinked from directory */ 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate dcmn_err4(("dv_destroy: %s\n", dv->dv_name)); 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate /* 4360Sstevel@tonic-gate * We may be asked to unlink referenced directories. 4370Sstevel@tonic-gate * In this case, there is nothing to be done. 4380Sstevel@tonic-gate * The eventual memory free will be done in 4390Sstevel@tonic-gate * devfs_inactive. 4400Sstevel@tonic-gate */ 4410Sstevel@tonic-gate if (vp->v_count != 0) { 4420Sstevel@tonic-gate ASSERT(vp->v_type == VDIR); 4430Sstevel@tonic-gate ASSERT(flags & DV_CLEAN_FORCE); 4440Sstevel@tonic-gate ASSERT(DV_STALE(dv)); 4450Sstevel@tonic-gate return; 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate if (dv->dv_attrvp != NULLVP) 4490Sstevel@tonic-gate VN_RELE(dv->dv_attrvp); 4500Sstevel@tonic-gate if (dv->dv_attr != NULL) 4510Sstevel@tonic-gate kmem_free(dv->dv_attr, sizeof (struct vattr)); 4520Sstevel@tonic-gate if (dv->dv_name != NULL) 4530Sstevel@tonic-gate kmem_free(dv->dv_name, dv->dv_namelen + 1); 4540Sstevel@tonic-gate if (dv->dv_devi != NULL) { 4550Sstevel@tonic-gate ndi_rele_devi(dv->dv_devi); 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate if (dv->dv_priv != NULL) { 4580Sstevel@tonic-gate dpfree(dv->dv_priv); 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate kmem_cache_free(dv_node_cache, dv); 4620Sstevel@tonic-gate } 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate /* 4650Sstevel@tonic-gate * Find and hold dv_node by name 4660Sstevel@tonic-gate */ 4670Sstevel@tonic-gate struct dv_node * 4680Sstevel@tonic-gate dv_findbyname(struct dv_node *ddv, char *nm) 4690Sstevel@tonic-gate { 4700Sstevel@tonic-gate struct dv_node *dv; 4710Sstevel@tonic-gate size_t nmlen = strlen(nm); 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate ASSERT(RW_LOCK_HELD(&ddv->dv_contents)); 4740Sstevel@tonic-gate dcmn_err3(("dv_findbyname: %s\n", nm)); 4750Sstevel@tonic-gate for (dv = ddv->dv_dot; dv; dv = dv->dv_next) { 4760Sstevel@tonic-gate if (dv->dv_namelen != nmlen) 4770Sstevel@tonic-gate continue; 4780Sstevel@tonic-gate if (strcmp(dv->dv_name, nm) == 0) { 4790Sstevel@tonic-gate VN_HOLD(DVTOV(dv)); 4800Sstevel@tonic-gate return (dv); 4810Sstevel@tonic-gate } 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate return (NULL); 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* 4870Sstevel@tonic-gate * Inserts a new dv_node in a parent directory 4880Sstevel@tonic-gate */ 4890Sstevel@tonic-gate void 4900Sstevel@tonic-gate dv_insert(struct dv_node *ddv, struct dv_node *dv) 4910Sstevel@tonic-gate { 4920Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ddv->dv_contents)); 4930Sstevel@tonic-gate ASSERT(DVTOV(ddv)->v_type == VDIR); 4940Sstevel@tonic-gate ASSERT(ddv->dv_nlink >= 2); 4950Sstevel@tonic-gate ASSERT(dv->dv_nlink == 0); 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate dcmn_err3(("dv_insert: %s\n", dv->dv_name)); 4980Sstevel@tonic-gate 4990Sstevel@tonic-gate dv->dv_dotdot = ddv; 5000Sstevel@tonic-gate dv->dv_next = ddv->dv_dot; 5010Sstevel@tonic-gate ddv->dv_dot = dv; 5020Sstevel@tonic-gate if (DVTOV(dv)->v_type == VDIR) { 5030Sstevel@tonic-gate ddv->dv_nlink++; /* .. to containing directory */ 5040Sstevel@tonic-gate dv->dv_nlink = 2; /* name + . */ 5050Sstevel@tonic-gate } else { 5060Sstevel@tonic-gate dv->dv_nlink = 1; /* name */ 5070Sstevel@tonic-gate } 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* 511647Scth * Unlink a dv_node from a perent directory 512647Scth */ 513647Scth void 514647Scth dv_unlink(struct dv_node *ddv, struct dv_node *dv, struct dv_node **dv_pprev) 515647Scth { 516647Scth /* verify linkage of arguments */ 517647Scth ASSERT(ddv && dv && dv_pprev); 518647Scth ASSERT(dv->dv_dotdot == ddv); 519647Scth ASSERT(*dv_pprev == dv); 520647Scth ASSERT(RW_WRITE_HELD(&ddv->dv_contents)); 521647Scth ASSERT(DVTOV(ddv)->v_type == VDIR); 522647Scth 523647Scth dcmn_err3(("dv_unlink: %s\n", dv->dv_name)); 524647Scth 525647Scth if (DVTOV(dv)->v_type == VDIR) { 526647Scth ddv->dv_nlink--; /* .. to containing directory */ 527647Scth dv->dv_nlink -= 2; /* name + . */ 528647Scth } else { 529647Scth dv->dv_nlink -= 1; /* name */ 530647Scth } 531647Scth ASSERT(ddv->dv_nlink >= 2); 532647Scth ASSERT(dv->dv_nlink == 0); 533647Scth 534647Scth /* update ddv->dv_dot/dv_next */ 535647Scth *dv_pprev = dv->dv_next; 536647Scth 537647Scth dv->dv_dotdot = NULL; 538647Scth dv->dv_next = NULL; 539647Scth dv->dv_dot = NULL; 540647Scth } 541647Scth 542647Scth /* 5430Sstevel@tonic-gate * Merge devfs node specific information into an attribute structure. 5440Sstevel@tonic-gate * 5450Sstevel@tonic-gate * NOTE: specfs provides ATIME,MTIME,CTIME,SIZE,BLKSIZE,NBLOCKS on leaf node. 5460Sstevel@tonic-gate */ 5470Sstevel@tonic-gate void 5480Sstevel@tonic-gate dv_vattr_merge(struct dv_node *dv, struct vattr *vap) 5490Sstevel@tonic-gate { 5500Sstevel@tonic-gate struct vnode *vp = DVTOV(dv); 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate vap->va_nodeid = dv->dv_ino; 5530Sstevel@tonic-gate vap->va_nlink = dv->dv_nlink; 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate if (vp->v_type == VDIR) { 5560Sstevel@tonic-gate vap->va_rdev = 0; 5570Sstevel@tonic-gate vap->va_fsid = vp->v_rdev; 5580Sstevel@tonic-gate } else { 5590Sstevel@tonic-gate vap->va_rdev = vp->v_rdev; 5600Sstevel@tonic-gate vap->va_fsid = DVTOV(dv->dv_dotdot)->v_rdev; 5610Sstevel@tonic-gate vap->va_type = vp->v_type; 5620Sstevel@tonic-gate /* don't trust the shadow file type */ 5630Sstevel@tonic-gate vap->va_mode &= ~S_IFMT; 5640Sstevel@tonic-gate if (vap->va_type == VCHR) 5650Sstevel@tonic-gate vap->va_mode |= S_IFCHR; 5660Sstevel@tonic-gate else 5670Sstevel@tonic-gate vap->va_mode |= S_IFBLK; 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate /* 5720Sstevel@tonic-gate * dv_shadow_node 5730Sstevel@tonic-gate * 5740Sstevel@tonic-gate * Given a VDIR dv_node, find/create the associated VDIR 5750Sstevel@tonic-gate * node in the shadow attribute filesystem. 5760Sstevel@tonic-gate * 5770Sstevel@tonic-gate * Given a VCHR/VBLK dv_node, find the associated VREG 5780Sstevel@tonic-gate * node in the shadow attribute filesystem. These nodes 5790Sstevel@tonic-gate * are only created to persist non-default attributes. 5800Sstevel@tonic-gate * Lack of such a node implies the default permissions 5810Sstevel@tonic-gate * are sufficient. 5820Sstevel@tonic-gate * 5830Sstevel@tonic-gate * Managing the attribute file entries is slightly tricky (mostly 5840Sstevel@tonic-gate * because we can't intercept VN_HOLD and VN_RELE except on the last 5850Sstevel@tonic-gate * release). 5860Sstevel@tonic-gate * 5870Sstevel@tonic-gate * We assert that if the dv_attrvp pointer is non-NULL, it points 5880Sstevel@tonic-gate * to a singly-held (by us) vnode that represents the shadow entry 5890Sstevel@tonic-gate * in the underlying filesystem. To avoid store-ordering issues, 5900Sstevel@tonic-gate * we assert that the pointer can only be tested under the dv_contents 5910Sstevel@tonic-gate * READERS lock. 5920Sstevel@tonic-gate */ 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate void 5950Sstevel@tonic-gate dv_shadow_node( 5960Sstevel@tonic-gate struct vnode *dvp, /* devfs parent directory vnode */ 5970Sstevel@tonic-gate char *nm, /* name component */ 5980Sstevel@tonic-gate struct vnode *vp, /* devfs vnode */ 5990Sstevel@tonic-gate struct pathname *pnp, /* the path .. */ 6000Sstevel@tonic-gate struct vnode *rdir, /* the root .. */ 6010Sstevel@tonic-gate struct cred *cred, /* who's asking? */ 6020Sstevel@tonic-gate int flags) /* optionally create shadow node */ 6030Sstevel@tonic-gate { 6040Sstevel@tonic-gate struct dv_node *dv; /* dv_node of named directory */ 6050Sstevel@tonic-gate struct vnode *rdvp; /* shadow parent directory vnode */ 6060Sstevel@tonic-gate struct vnode *rvp; /* shadow vnode */ 6070Sstevel@tonic-gate struct vnode *rrvp; /* realvp of shadow vnode */ 6080Sstevel@tonic-gate struct vattr vattr; 6090Sstevel@tonic-gate int create_tried; 6100Sstevel@tonic-gate int error; 6110Sstevel@tonic-gate mperm_t mp; 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK); 6140Sstevel@tonic-gate dv = VTODV(vp); 6150Sstevel@tonic-gate dcmn_err3(("dv_shadow_node: name %s attr %p\n", 6160Sstevel@tonic-gate nm, (void *)dv->dv_attrvp)); 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate if ((flags & DV_SHADOW_WRITE_HELD) == 0) { 6190Sstevel@tonic-gate ASSERT(RW_READ_HELD(&dv->dv_contents)); 6200Sstevel@tonic-gate if (dv->dv_attrvp != NULLVP) 6210Sstevel@tonic-gate return; 6220Sstevel@tonic-gate if (!rw_tryupgrade(&dv->dv_contents)) { 6230Sstevel@tonic-gate rw_exit(&dv->dv_contents); 6240Sstevel@tonic-gate rw_enter(&dv->dv_contents, RW_WRITER); 6250Sstevel@tonic-gate if (dv->dv_attrvp != NULLVP) { 6260Sstevel@tonic-gate rw_downgrade(&dv->dv_contents); 6270Sstevel@tonic-gate return; 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate } else { 6310Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&dv->dv_contents)); 6320Sstevel@tonic-gate if (dv->dv_attrvp != NULLVP) 6330Sstevel@tonic-gate return; 6340Sstevel@tonic-gate } 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&dv->dv_contents) && dv->dv_attrvp == NULL); 6370Sstevel@tonic-gate 6380Sstevel@tonic-gate rdvp = VTODV(dvp)->dv_attrvp; 6390Sstevel@tonic-gate create_tried = 0; 6400Sstevel@tonic-gate lookup: 6410Sstevel@tonic-gate if (rdvp && (dv->dv_flags & DV_NO_FSPERM) == 0) { 6420Sstevel@tonic-gate error = VOP_LOOKUP(rdvp, nm, &rvp, pnp, LOOKUP_DIR, rdir, cred); 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate /* factor out the snode since we only want the attribute node */ 6450Sstevel@tonic-gate if ((error == 0) && (VOP_REALVP(rvp, &rrvp) == 0)) { 6460Sstevel@tonic-gate VN_HOLD(rrvp); 6470Sstevel@tonic-gate VN_RELE(rvp); 6480Sstevel@tonic-gate rvp = rrvp; 6490Sstevel@tonic-gate } 6500Sstevel@tonic-gate } else 6510Sstevel@tonic-gate error = EROFS; /* no parent, no entry */ 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate /* 6540Sstevel@tonic-gate * All we want is the permissions (and maybe ACLs and 6550Sstevel@tonic-gate * extended attributes), and we want to perform lookups 6560Sstevel@tonic-gate * by name. Drivers occasionally change their minor 6570Sstevel@tonic-gate * number space. If something changes, there's no 6580Sstevel@tonic-gate * much we can do about it here. 6590Sstevel@tonic-gate */ 6600Sstevel@tonic-gate 6610Sstevel@tonic-gate /* The shadow node checks out. We are done */ 6620Sstevel@tonic-gate if (error == 0) { 6630Sstevel@tonic-gate dv->dv_attrvp = rvp; /* with one hold */ 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate /* 666789Sahrens * Determine if we have non-trivial ACLs on this node. 667789Sahrens * It is not necessary to VOP_RWLOCK since fs_acl_nontrivial 668789Sahrens * only does VOP_GETSECATTR. 6690Sstevel@tonic-gate */ 6700Sstevel@tonic-gate dv->dv_flags &= ~DV_ACL; 671789Sahrens 672789Sahrens if (fs_acl_nontrivial(rvp, cred)) 673789Sahrens dv->dv_flags |= DV_ACL; 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate /* 6760Sstevel@tonic-gate * If we have synced out the memory attributes, free 6770Sstevel@tonic-gate * them and switch back to using the persistent store. 6780Sstevel@tonic-gate */ 6790Sstevel@tonic-gate if (rvp && dv->dv_attr) { 6800Sstevel@tonic-gate kmem_free(dv->dv_attr, sizeof (struct vattr)); 6810Sstevel@tonic-gate dv->dv_attr = NULL; 6820Sstevel@tonic-gate } 6830Sstevel@tonic-gate if ((flags & DV_SHADOW_WRITE_HELD) == 0) 6840Sstevel@tonic-gate rw_downgrade(&dv->dv_contents); 6850Sstevel@tonic-gate ASSERT(RW_LOCK_HELD(&dv->dv_contents)); 6860Sstevel@tonic-gate return; 6870Sstevel@tonic-gate } 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate /* 6900Sstevel@tonic-gate * Failed to find attribute in persistent backing store, 6910Sstevel@tonic-gate * get default permission bits. For minors not created by 6920Sstevel@tonic-gate * ddi_create_priv_minor_node(), use devfs defaults. 6930Sstevel@tonic-gate */ 6940Sstevel@tonic-gate if (vp->v_type == VDIR) { 6950Sstevel@tonic-gate vattr = dv_vattr_dir; 6960Sstevel@tonic-gate } else if (dv->dv_flags & DV_NO_FSPERM) { 6970Sstevel@tonic-gate vattr = dv_vattr_priv; 6980Sstevel@tonic-gate } else { 6990Sstevel@tonic-gate /* 7000Sstevel@tonic-gate * look up perm bits from minor_perm 7010Sstevel@tonic-gate */ 7020Sstevel@tonic-gate vattr = dv_vattr_file; 7030Sstevel@tonic-gate if (dev_minorperm(dv->dv_devi, dv->dv_name, &mp) == 0) { 7040Sstevel@tonic-gate VATTR_MP_MERGE(vattr, mp); 7050Sstevel@tonic-gate dcmn_err5(("%s: minor perm mode 0%o\n", 7060Sstevel@tonic-gate dv->dv_name, vattr.va_mode)); 7070Sstevel@tonic-gate } else if (dv->dv_flags & DV_DFLT_MODE) { 7080Sstevel@tonic-gate ASSERT((dv->dv_dflt_mode & ~S_IAMB) == 0); 7090Sstevel@tonic-gate vattr.va_mode &= ~S_IAMB; 7100Sstevel@tonic-gate vattr.va_mode |= dv->dv_dflt_mode; 7110Sstevel@tonic-gate dcmn_err5(("%s: priv mode 0%o\n", 7120Sstevel@tonic-gate dv->dv_name, vattr.va_mode)); 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate } 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate dv_vattr_merge(dv, &vattr); 7170Sstevel@tonic-gate gethrestime(&vattr.va_atime); 7180Sstevel@tonic-gate vattr.va_mtime = vattr.va_atime; 7190Sstevel@tonic-gate vattr.va_ctime = vattr.va_atime; 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate /* 7220Sstevel@tonic-gate * Try to create shadow dir. This is necessary in case 7230Sstevel@tonic-gate * we need to create a shadow leaf node later, when user 7240Sstevel@tonic-gate * executes chmod. 7250Sstevel@tonic-gate */ 7260Sstevel@tonic-gate if ((error == ENOENT) && !create_tried) { 7270Sstevel@tonic-gate switch (vp->v_type) { 7280Sstevel@tonic-gate case VDIR: 7290Sstevel@tonic-gate error = VOP_MKDIR(rdvp, nm, &vattr, &rvp, kcred); 7300Sstevel@tonic-gate dsysdebug(error, ("vop_mkdir %s %s %d\n", 7310Sstevel@tonic-gate VTODV(dvp)->dv_name, nm, error)); 7320Sstevel@tonic-gate create_tried = 1; 7330Sstevel@tonic-gate break; 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate case VCHR: 7360Sstevel@tonic-gate case VBLK: 7370Sstevel@tonic-gate /* 7380Sstevel@tonic-gate * Shadow nodes are only created on demand 7390Sstevel@tonic-gate */ 7400Sstevel@tonic-gate if (flags & DV_SHADOW_CREATE) { 7410Sstevel@tonic-gate error = VOP_CREATE(rdvp, nm, &vattr, NONEXCL, 7420Sstevel@tonic-gate VREAD|VWRITE, &rvp, kcred, 0); 7430Sstevel@tonic-gate dsysdebug(error, ("vop_create %s %s %d\n", 7440Sstevel@tonic-gate VTODV(dvp)->dv_name, nm, error)); 7450Sstevel@tonic-gate create_tried = 1; 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate break; 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate default: 7500Sstevel@tonic-gate cmn_err(CE_PANIC, "devfs: %s: create", dvnm); 7510Sstevel@tonic-gate /*NOTREACHED*/ 7520Sstevel@tonic-gate } 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate if (create_tried && 7550Sstevel@tonic-gate (error == 0) || (error == EEXIST)) { 7560Sstevel@tonic-gate VN_RELE(rvp); 7570Sstevel@tonic-gate goto lookup; 7580Sstevel@tonic-gate } 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate 7610Sstevel@tonic-gate /* Store attribute in memory */ 7620Sstevel@tonic-gate if (dv->dv_attr == NULL) { 7630Sstevel@tonic-gate dv->dv_attr = kmem_alloc(sizeof (struct vattr), KM_SLEEP); 7640Sstevel@tonic-gate *(dv->dv_attr) = vattr; 7650Sstevel@tonic-gate } 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate if ((flags & DV_SHADOW_WRITE_HELD) == 0) 7680Sstevel@tonic-gate rw_downgrade(&dv->dv_contents); 7690Sstevel@tonic-gate ASSERT(RW_LOCK_HELD(&dv->dv_contents)); 7700Sstevel@tonic-gate } 7710Sstevel@tonic-gate 7720Sstevel@tonic-gate /* 7730Sstevel@tonic-gate * Given a devinfo node, and a name, returns the appropriate 7740Sstevel@tonic-gate * minor information for that named node, if it exists. 7750Sstevel@tonic-gate */ 7760Sstevel@tonic-gate static int 7770Sstevel@tonic-gate dv_find_leafnode(dev_info_t *devi, char *minor_nm, struct ddi_minor_data *r_mi) 7780Sstevel@tonic-gate { 7790Sstevel@tonic-gate struct ddi_minor_data *dmd; 7800Sstevel@tonic-gate 781*1333Scth ASSERT(i_ddi_devi_attached(devi)); 7820Sstevel@tonic-gate ASSERT(MUTEX_HELD(&DEVI(devi)->devi_lock)); 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate dcmn_err3(("dv_find_leafnode: %s\n", minor_nm)); 7850Sstevel@tonic-gate for (dmd = DEVI(devi)->devi_minor; dmd; dmd = dmd->next) { 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate /* 7880Sstevel@tonic-gate * Skip alias nodes and nodes without a name. 7890Sstevel@tonic-gate */ 7900Sstevel@tonic-gate if ((dmd->type == DDM_ALIAS) || (dmd->ddm_name == NULL)) 7910Sstevel@tonic-gate continue; 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate dcmn_err4(("dv_find_leafnode: (%s,%s)\n", 7940Sstevel@tonic-gate minor_nm, dmd->ddm_name)); 7950Sstevel@tonic-gate if (strcmp(minor_nm, dmd->ddm_name) == 0) { 7960Sstevel@tonic-gate r_mi->ddm_dev = dmd->ddm_dev; 7970Sstevel@tonic-gate r_mi->ddm_spec_type = dmd->ddm_spec_type; 7980Sstevel@tonic-gate r_mi->type = dmd->type; 7990Sstevel@tonic-gate r_mi->ddm_flags = dmd->ddm_flags; 8000Sstevel@tonic-gate r_mi->ddm_node_priv = dmd->ddm_node_priv; 8010Sstevel@tonic-gate r_mi->ddm_priv_mode = dmd->ddm_priv_mode; 8020Sstevel@tonic-gate if (r_mi->ddm_node_priv) 8030Sstevel@tonic-gate dphold(r_mi->ddm_node_priv); 8040Sstevel@tonic-gate return (0); 8050Sstevel@tonic-gate } 8060Sstevel@tonic-gate } 8070Sstevel@tonic-gate 8080Sstevel@tonic-gate dcmn_err3(("dv_find_leafnode: %s: ENOENT\n", minor_nm)); 8090Sstevel@tonic-gate return (ENOENT); 8100Sstevel@tonic-gate } 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate /* 8130Sstevel@tonic-gate * Special handling for clone node: 8140Sstevel@tonic-gate * Clone minor name is a driver name, the minor number will 8150Sstevel@tonic-gate * be the major number of the driver. There is no minor 8160Sstevel@tonic-gate * node under the clone driver, so we'll manufacture the 8170Sstevel@tonic-gate * dev_t. 8180Sstevel@tonic-gate */ 8190Sstevel@tonic-gate static struct dv_node * 8200Sstevel@tonic-gate dv_clone_mknod(struct dv_node *ddv, char *drvname) 8210Sstevel@tonic-gate { 8220Sstevel@tonic-gate major_t major; 8230Sstevel@tonic-gate struct dv_node *dvp; 8240Sstevel@tonic-gate char *devnm; 8250Sstevel@tonic-gate struct ddi_minor_data *dmd; 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate /* 8280Sstevel@tonic-gate * Make sure drvname is a STREAMS driver. We load the driver, 8290Sstevel@tonic-gate * but don't attach to any instances. This makes stat(2) 8300Sstevel@tonic-gate * relatively cheap. 8310Sstevel@tonic-gate */ 8320Sstevel@tonic-gate major = ddi_name_to_major(drvname); 8330Sstevel@tonic-gate if (major == (major_t)-1) 8340Sstevel@tonic-gate return (NULL); 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate if (ddi_hold_driver(major) == NULL) 8370Sstevel@tonic-gate return (NULL); 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate if (STREAMSTAB(major) == NULL) { 8400Sstevel@tonic-gate ddi_rele_driver(major); 8410Sstevel@tonic-gate return (NULL); 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate ddi_rele_driver(major); 8450Sstevel@tonic-gate devnm = kmem_alloc(MAXNAMELEN, KM_SLEEP); 8460Sstevel@tonic-gate (void) snprintf(devnm, MAXNAMELEN, "clone@0:%s", drvname); 8470Sstevel@tonic-gate dmd = kmem_zalloc(sizeof (*dmd), KM_SLEEP); 8480Sstevel@tonic-gate dmd->ddm_dev = makedevice(clone_major, (minor_t)major); 8490Sstevel@tonic-gate dmd->ddm_spec_type = S_IFCHR; 8500Sstevel@tonic-gate dvp = dv_mknod(ddv, clone_dip, devnm, dmd); 8510Sstevel@tonic-gate kmem_free(dmd, sizeof (*dmd)); 8520Sstevel@tonic-gate kmem_free(devnm, MAXNAMELEN); 8530Sstevel@tonic-gate return (dvp); 8540Sstevel@tonic-gate } 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate /* 8570Sstevel@tonic-gate * Given the parent directory node, and a name in it, returns the 8580Sstevel@tonic-gate * named dv_node to the caller (as a vnode). 8590Sstevel@tonic-gate * 8600Sstevel@tonic-gate * (We need pnp and rdir for doing shadow lookups; they can be NULL) 8610Sstevel@tonic-gate */ 8620Sstevel@tonic-gate int 8630Sstevel@tonic-gate dv_find(struct dv_node *ddv, char *nm, struct vnode **vpp, struct pathname *pnp, 8640Sstevel@tonic-gate struct vnode *rdir, struct cred *cred, uint_t ndi_flags) 8650Sstevel@tonic-gate { 8660Sstevel@tonic-gate extern int isminiroot; /* see modctl.c */ 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate int rv = 0, was_busy = 0, nmlen; 8690Sstevel@tonic-gate struct vnode *vp; 8700Sstevel@tonic-gate struct dv_node *dv, *dup; 8710Sstevel@tonic-gate dev_info_t *pdevi, *devi = NULL; 8720Sstevel@tonic-gate char *mnm; 8730Sstevel@tonic-gate struct ddi_minor_data *dmd; 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate dcmn_err3(("dv_find %s\n", nm)); 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate rw_enter(&ddv->dv_contents, RW_READER); 8780Sstevel@tonic-gate start: 8790Sstevel@tonic-gate if (DV_STALE(ddv)) { 8800Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 8810Sstevel@tonic-gate return (ESTALE); 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate /* 8850Sstevel@tonic-gate * Empty name or ., return node itself. 8860Sstevel@tonic-gate */ 8870Sstevel@tonic-gate nmlen = strlen(nm); 8880Sstevel@tonic-gate if ((nmlen == 0) || ((nmlen == 1) && (nm[0] == '.'))) { 8890Sstevel@tonic-gate *vpp = DVTOV(ddv); 8900Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 8910Sstevel@tonic-gate VN_HOLD(*vpp); 8920Sstevel@tonic-gate return (0); 8930Sstevel@tonic-gate } 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate /* 8960Sstevel@tonic-gate * .., return the parent directory 8970Sstevel@tonic-gate */ 8980Sstevel@tonic-gate if ((nmlen == 2) && (strcmp(nm, "..") == 0)) { 8990Sstevel@tonic-gate *vpp = DVTOV(ddv->dv_dotdot); 9000Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 9010Sstevel@tonic-gate VN_HOLD(*vpp); 9020Sstevel@tonic-gate return (0); 9030Sstevel@tonic-gate } 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate /* 9060Sstevel@tonic-gate * Fail anything without a valid device name component 9070Sstevel@tonic-gate */ 9080Sstevel@tonic-gate if (nm[0] == '@' || nm[0] == ':') { 9090Sstevel@tonic-gate dcmn_err3(("devfs: no driver '%s'\n", nm)); 9100Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 9110Sstevel@tonic-gate return (ENOENT); 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate /* 9150Sstevel@tonic-gate * So, now we have to deal with the trickier stuff. 9160Sstevel@tonic-gate * 9170Sstevel@tonic-gate * (a) search the existing list of dv_nodes on this directory 9180Sstevel@tonic-gate */ 9190Sstevel@tonic-gate if ((dv = dv_findbyname(ddv, nm)) != NULL) { 9200Sstevel@tonic-gate founddv: 9210Sstevel@tonic-gate ASSERT(RW_LOCK_HELD(&ddv->dv_contents)); 9220Sstevel@tonic-gate rw_enter(&dv->dv_contents, RW_READER); 9230Sstevel@tonic-gate vp = DVTOV(dv); 9240Sstevel@tonic-gate if ((dv->dv_attrvp != NULLVP) || 9250Sstevel@tonic-gate (vp->v_type != VDIR && dv->dv_attr != NULL)) { 9260Sstevel@tonic-gate /* 9270Sstevel@tonic-gate * Common case - we already have attributes 9280Sstevel@tonic-gate */ 9290Sstevel@tonic-gate rw_exit(&dv->dv_contents); 9300Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 9310Sstevel@tonic-gate goto found; 9320Sstevel@tonic-gate } 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate /* 9350Sstevel@tonic-gate * No attribute vp, try and build one. 9360Sstevel@tonic-gate */ 9370Sstevel@tonic-gate dv_shadow_node(DVTOV(ddv), nm, vp, pnp, rdir, cred, 0); 9380Sstevel@tonic-gate rw_exit(&dv->dv_contents); 9390Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 9400Sstevel@tonic-gate goto found; 9410Sstevel@tonic-gate } 9420Sstevel@tonic-gate 9430Sstevel@tonic-gate /* 9440Sstevel@tonic-gate * (b) Search the child devinfo nodes of our parent directory, 9450Sstevel@tonic-gate * looking for the named node. If we find it, build a new 9460Sstevel@tonic-gate * node, then grab the writers lock, search the directory 9470Sstevel@tonic-gate * if it's still not there, then insert it. 9480Sstevel@tonic-gate * 9490Sstevel@tonic-gate * We drop the devfs locks before accessing the device tree. 9500Sstevel@tonic-gate * Take care to mark the node BUSY so that a forced devfs_clean 9510Sstevel@tonic-gate * doesn't mark the directory node stale. 9520Sstevel@tonic-gate * 9530Sstevel@tonic-gate * Also, check if we are called as part of devfs_clean or 9540Sstevel@tonic-gate * reset_perm. If so, simply return not found because there 9550Sstevel@tonic-gate * is nothing to clean. 9560Sstevel@tonic-gate */ 9570Sstevel@tonic-gate if (tsd_get(devfs_clean_key)) { 9580Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 9590Sstevel@tonic-gate return (ENOENT); 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate /* 9630Sstevel@tonic-gate * We could be either READ or WRITE locked at 9640Sstevel@tonic-gate * this point. Upgrade if we are read locked. 9650Sstevel@tonic-gate */ 9660Sstevel@tonic-gate ASSERT(RW_LOCK_HELD(&ddv->dv_contents)); 9670Sstevel@tonic-gate if (rw_read_locked(&ddv->dv_contents) && 9680Sstevel@tonic-gate !rw_tryupgrade(&ddv->dv_contents)) { 9690Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 9700Sstevel@tonic-gate rw_enter(&ddv->dv_contents, RW_WRITER); 9710Sstevel@tonic-gate /* 9720Sstevel@tonic-gate * Things may have changed when we dropped 9730Sstevel@tonic-gate * the contents lock, so start from top again 9740Sstevel@tonic-gate */ 9750Sstevel@tonic-gate goto start; 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate ddv->dv_busy++; /* mark busy before dropping lock */ 9780Sstevel@tonic-gate was_busy++; 9790Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 9800Sstevel@tonic-gate 9810Sstevel@tonic-gate pdevi = ddv->dv_devi; 9820Sstevel@tonic-gate ASSERT(pdevi != NULL); 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate mnm = strchr(nm, ':'); 9850Sstevel@tonic-gate if (mnm) 9860Sstevel@tonic-gate *mnm = (char)0; 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate /* 9890Sstevel@tonic-gate * Configure one nexus child, will call nexus's bus_ops 9900Sstevel@tonic-gate * If successful, devi is held upon returning. 9910Sstevel@tonic-gate * Note: devfs lookup should not be configuring grandchildren. 9920Sstevel@tonic-gate */ 9930Sstevel@tonic-gate ASSERT((ndi_flags & NDI_CONFIG) == 0); 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate rv = ndi_devi_config_one(pdevi, nm, &devi, ndi_flags | NDI_NO_EVENT); 9960Sstevel@tonic-gate if (mnm) 9970Sstevel@tonic-gate *mnm = ':'; 9980Sstevel@tonic-gate if (rv != NDI_SUCCESS) { 9990Sstevel@tonic-gate rv = ENOENT; 10000Sstevel@tonic-gate goto notfound; 10010Sstevel@tonic-gate } 10020Sstevel@tonic-gate 10030Sstevel@tonic-gate /* 10040Sstevel@tonic-gate * Don't make vhci clients visible under phci, unless we 10050Sstevel@tonic-gate * are in miniroot. 10060Sstevel@tonic-gate */ 10070Sstevel@tonic-gate if (isminiroot == 0 && ddi_get_parent(devi) != pdevi) { 10080Sstevel@tonic-gate ndi_rele_devi(devi); 10090Sstevel@tonic-gate rv = ENOENT; 10100Sstevel@tonic-gate goto notfound; 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate 1013*1333Scth ASSERT(devi && i_ddi_devi_attached(devi)); 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate /* 10160Sstevel@tonic-gate * Invalidate cache to notice newly created minor nodes. 10170Sstevel@tonic-gate */ 10180Sstevel@tonic-gate rw_enter(&ddv->dv_contents, RW_WRITER); 10190Sstevel@tonic-gate ddv->dv_flags |= DV_BUILD; 10200Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate /* 10230Sstevel@tonic-gate * mkdir for nexus drivers and leaf nodes as well. If we are racing 10240Sstevel@tonic-gate * and create a duplicate, the duplicate will be destroyed below. 10250Sstevel@tonic-gate */ 10260Sstevel@tonic-gate if (mnm == NULL) { 10270Sstevel@tonic-gate dv = dv_mkdir(ddv, devi, nm); 10280Sstevel@tonic-gate } else { 10290Sstevel@tonic-gate /* 10300Sstevel@tonic-gate * For clone minors, load the driver indicated by minor name. 10310Sstevel@tonic-gate */ 10320Sstevel@tonic-gate mutex_enter(&DEVI(devi)->devi_lock); 10330Sstevel@tonic-gate if (devi == clone_dip) { 10340Sstevel@tonic-gate dv = dv_clone_mknod(ddv, mnm + 1); 10350Sstevel@tonic-gate } else { 10360Sstevel@tonic-gate /* 10370Sstevel@tonic-gate * Find minor node and make a dv_node 10380Sstevel@tonic-gate */ 10390Sstevel@tonic-gate dmd = kmem_zalloc(sizeof (*dmd), KM_SLEEP); 10400Sstevel@tonic-gate if (dv_find_leafnode(devi, mnm + 1, dmd) == 0) { 10410Sstevel@tonic-gate dv = dv_mknod(ddv, devi, nm, dmd); 10420Sstevel@tonic-gate if (dmd->ddm_node_priv) 10430Sstevel@tonic-gate dpfree(dmd->ddm_node_priv); 10440Sstevel@tonic-gate } 10450Sstevel@tonic-gate kmem_free(dmd, sizeof (*dmd)); 10460Sstevel@tonic-gate } 10470Sstevel@tonic-gate mutex_exit(&DEVI(devi)->devi_lock); 10480Sstevel@tonic-gate } 10490Sstevel@tonic-gate /* 10500Sstevel@tonic-gate * Release hold from ndi_devi_config_one() 10510Sstevel@tonic-gate */ 10520Sstevel@tonic-gate ndi_rele_devi(devi); 10530Sstevel@tonic-gate 10540Sstevel@tonic-gate if (dv == NULL) { 10550Sstevel@tonic-gate rv = ENOENT; 10560Sstevel@tonic-gate goto notfound; 10570Sstevel@tonic-gate } 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate /* 10600Sstevel@tonic-gate * We have released the dv_contents lock, need to check 10610Sstevel@tonic-gate * if another thread already created a duplicate node 10620Sstevel@tonic-gate */ 10630Sstevel@tonic-gate rw_enter(&ddv->dv_contents, RW_WRITER); 10640Sstevel@tonic-gate if ((dup = dv_findbyname(ddv, nm)) == NULL) { 10650Sstevel@tonic-gate dv_insert(ddv, dv); 10660Sstevel@tonic-gate } else { 10670Sstevel@tonic-gate /* 10680Sstevel@tonic-gate * Duplicate found, use the existing node 10690Sstevel@tonic-gate */ 10700Sstevel@tonic-gate VN_RELE(DVTOV(dv)); 10710Sstevel@tonic-gate dv_destroy(dv, 0); 10720Sstevel@tonic-gate dv = dup; 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate goto founddv; 10750Sstevel@tonic-gate /*NOTREACHED*/ 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate found: 10780Sstevel@tonic-gate /* 10790Sstevel@tonic-gate * Skip non-kernel lookups of internal nodes. 10800Sstevel@tonic-gate * This use of kcred to distinguish between user and 10810Sstevel@tonic-gate * internal kernel lookups is unfortunate. The information 10820Sstevel@tonic-gate * provided by the seg argument to lookupnameat should 10830Sstevel@tonic-gate * evolve into a lookup flag for filesystems that need 10840Sstevel@tonic-gate * this distinction. 10850Sstevel@tonic-gate */ 10860Sstevel@tonic-gate if ((dv->dv_flags & DV_INTERNAL) && (cred != kcred)) { 10870Sstevel@tonic-gate VN_RELE(vp); 10880Sstevel@tonic-gate rv = ENOENT; 10890Sstevel@tonic-gate goto notfound; 10900Sstevel@tonic-gate } 10910Sstevel@tonic-gate 10920Sstevel@tonic-gate dcmn_err2(("dv_find: returning vp for nm %s\n", nm)); 10930Sstevel@tonic-gate if (vp->v_type == VCHR || vp->v_type == VBLK) { 10940Sstevel@tonic-gate /* 10950Sstevel@tonic-gate * If vnode is a device, return special vnode instead 10960Sstevel@tonic-gate * (though it knows all about -us- via sp->s_realvp, 10970Sstevel@tonic-gate * sp->s_devvp, and sp->s_dip) 10980Sstevel@tonic-gate */ 10990Sstevel@tonic-gate *vpp = specvp_devfs(vp, vp->v_rdev, vp->v_type, cred, 11000Sstevel@tonic-gate dv->dv_devi); 11010Sstevel@tonic-gate VN_RELE(vp); 11020Sstevel@tonic-gate if (*vpp == NULLVP) 11030Sstevel@tonic-gate rv = ENOSYS; 11040Sstevel@tonic-gate } else 11050Sstevel@tonic-gate *vpp = vp; 11060Sstevel@tonic-gate 11070Sstevel@tonic-gate notfound: 11080Sstevel@tonic-gate rw_enter(&ddv->dv_contents, RW_WRITER); 11090Sstevel@tonic-gate if (was_busy) 11100Sstevel@tonic-gate ddv->dv_busy--; 11110Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 11120Sstevel@tonic-gate return (rv); 11130Sstevel@tonic-gate } 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate /* 11160Sstevel@tonic-gate * The given directory node is out-of-date; that is, it has been 11170Sstevel@tonic-gate * marked as needing to be rebuilt, possibly because some new devinfo 11180Sstevel@tonic-gate * node has come into existence, or possibly because this is the first 11190Sstevel@tonic-gate * time we've been here. 11200Sstevel@tonic-gate */ 11210Sstevel@tonic-gate void 11220Sstevel@tonic-gate dv_filldir(struct dv_node *ddv) 11230Sstevel@tonic-gate { 11240Sstevel@tonic-gate struct dv_node *dv; 11250Sstevel@tonic-gate dev_info_t *devi, *pdevi; 11260Sstevel@tonic-gate struct ddi_minor_data *dmd; 11270Sstevel@tonic-gate char devnm[MAXNAMELEN]; 11280Sstevel@tonic-gate int circ; 11290Sstevel@tonic-gate 11300Sstevel@tonic-gate ASSERT(DVTOV(ddv)->v_type == VDIR); 11310Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&ddv->dv_contents)); 11320Sstevel@tonic-gate ASSERT(ddv->dv_flags & DV_BUILD); 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate dcmn_err3(("dv_filldir: %s\n", ddv->dv_name)); 11350Sstevel@tonic-gate if (DV_STALE(ddv)) 11360Sstevel@tonic-gate return; 11370Sstevel@tonic-gate pdevi = ddv->dv_devi; 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate if (ndi_devi_config(pdevi, NDI_NO_EVENT) != NDI_SUCCESS) { 11400Sstevel@tonic-gate dcmn_err3(("dv_filldir: config error %s\n", 11410Sstevel@tonic-gate ddv->dv_name)); 11420Sstevel@tonic-gate } 11430Sstevel@tonic-gate 11440Sstevel@tonic-gate ndi_devi_enter(pdevi, &circ); 11450Sstevel@tonic-gate for (devi = ddi_get_child(pdevi); devi; 11460Sstevel@tonic-gate devi = ddi_get_next_sibling(devi)) { 11470Sstevel@tonic-gate if (i_ddi_node_state(devi) < DS_PROBED) 11480Sstevel@tonic-gate continue; 11490Sstevel@tonic-gate 11500Sstevel@tonic-gate dcmn_err3(("dv_filldir: node %s\n", ddi_node_name(devi))); 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate mutex_enter(&DEVI(devi)->devi_lock); 11530Sstevel@tonic-gate for (dmd = DEVI(devi)->devi_minor; dmd; dmd = dmd->next) { 11540Sstevel@tonic-gate char *addr; 11550Sstevel@tonic-gate 11560Sstevel@tonic-gate /* 11570Sstevel@tonic-gate * Skip alias nodes, internal nodes, and nodes 11580Sstevel@tonic-gate * without a name. We allow DDM_DEFAULT nodes 11590Sstevel@tonic-gate * to appear in readdir. 11600Sstevel@tonic-gate */ 11610Sstevel@tonic-gate if ((dmd->type == DDM_ALIAS) || 11620Sstevel@tonic-gate (dmd->type == DDM_INTERNAL_PATH) || 11630Sstevel@tonic-gate (dmd->ddm_name == NULL)) 11640Sstevel@tonic-gate continue; 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate addr = ddi_get_name_addr(devi); 11670Sstevel@tonic-gate if (addr && *addr) 11680Sstevel@tonic-gate (void) sprintf(devnm, "%s@%s:%s", 11690Sstevel@tonic-gate ddi_node_name(devi), addr, dmd->ddm_name); 11700Sstevel@tonic-gate else 11710Sstevel@tonic-gate (void) sprintf(devnm, "%s:%s", 11720Sstevel@tonic-gate ddi_node_name(devi), dmd->ddm_name); 11730Sstevel@tonic-gate 11740Sstevel@tonic-gate if ((dv = dv_findbyname(ddv, devnm)) != NULL) { 11750Sstevel@tonic-gate /* dv_node already exists */ 11760Sstevel@tonic-gate VN_RELE(DVTOV(dv)); 11770Sstevel@tonic-gate continue; 11780Sstevel@tonic-gate } 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate dv = dv_mknod(ddv, devi, devnm, dmd); 11810Sstevel@tonic-gate dv_insert(ddv, dv); 11820Sstevel@tonic-gate VN_RELE(DVTOV(dv)); 11830Sstevel@tonic-gate } 11840Sstevel@tonic-gate mutex_exit(&DEVI(devi)->devi_lock); 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate (void) ddi_deviname(devi, devnm); 11870Sstevel@tonic-gate if ((dv = dv_findbyname(ddv, devnm + 1)) == NULL) { 11880Sstevel@tonic-gate /* directory doesn't exist */ 11890Sstevel@tonic-gate dv = dv_mkdir(ddv, devi, devnm + 1); 11900Sstevel@tonic-gate dv_insert(ddv, dv); 11910Sstevel@tonic-gate } 11920Sstevel@tonic-gate VN_RELE(DVTOV(dv)); 11930Sstevel@tonic-gate } 11940Sstevel@tonic-gate ndi_devi_exit(pdevi, circ); 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate ddv->dv_flags &= ~DV_BUILD; 11970Sstevel@tonic-gate } 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate /* 12000Sstevel@tonic-gate * Given a directory node, clean out all the nodes beneath. 12010Sstevel@tonic-gate * 12020Sstevel@tonic-gate * VDIR: Reinvoke to clean them, then delete the directory. 12030Sstevel@tonic-gate * VCHR, VBLK: Just blow them away. 12040Sstevel@tonic-gate * 12050Sstevel@tonic-gate * Mark the directories touched as in need of a rebuild, in case 12060Sstevel@tonic-gate * we fall over part way through. When DV_CLEAN_FORCE is specified, 12070Sstevel@tonic-gate * we mark referenced empty directories as stale to facilitate DR. 12080Sstevel@tonic-gate */ 12090Sstevel@tonic-gate int 12100Sstevel@tonic-gate dv_cleandir(struct dv_node *ddv, char *devnm, uint_t flags) 12110Sstevel@tonic-gate { 1212647Scth struct dv_node *dv; 1213647Scth struct dv_node **pprev, **npprev; 12140Sstevel@tonic-gate struct vnode *vp; 1215647Scth int busy = 0; 12160Sstevel@tonic-gate 12170Sstevel@tonic-gate dcmn_err3(("dv_cleandir: %s\n", ddv->dv_name)); 12180Sstevel@tonic-gate 1219647Scth if (!(flags & DV_CLEANDIR_LCK)) 1220647Scth rw_enter(&ddv->dv_contents, RW_WRITER); 1221647Scth for (pprev = &ddv->dv_dot, dv = *pprev; dv; 1222647Scth pprev = npprev, dv = *pprev) { 1223647Scth npprev = &dv->dv_next; 12240Sstevel@tonic-gate 1225647Scth /* 1226647Scth * If devnm is specified, the non-minor portion of the 1227647Scth * name must match devnm. 1228647Scth */ 1229647Scth if (devnm && 1230647Scth (strncmp(devnm, dv->dv_name, strlen(devnm)) || 12310Sstevel@tonic-gate (dv->dv_name[strlen(devnm)] != ':' && 12320Sstevel@tonic-gate dv->dv_name[strlen(devnm)] != '\0'))) 12330Sstevel@tonic-gate continue; 12340Sstevel@tonic-gate 1235647Scth /* check type of what we are cleaning */ 12360Sstevel@tonic-gate vp = DVTOV(dv); 12370Sstevel@tonic-gate if (vp->v_type == VDIR) { 1238647Scth /* recurse on directories */ 1239647Scth rw_enter(&dv->dv_contents, RW_WRITER); 1240647Scth if (dv_cleandir(dv, NULL, 1241647Scth flags | DV_CLEANDIR_LCK) == EBUSY) { 1242647Scth rw_exit(&dv->dv_contents); 1243647Scth goto set_busy; 1244647Scth } 1245647Scth 1246647Scth /* A clean directory is an empty directory... */ 1247647Scth ASSERT(dv->dv_nlink == 2); 1248647Scth mutex_enter(&vp->v_lock); 1249647Scth if (vp->v_count > 0) { 12500Sstevel@tonic-gate /* 1251647Scth * ... but an empty directory can still have 1252647Scth * references to it. If we have dv_busy or 1253647Scth * DV_CLEAN_FORCE is *not* specified then a 1254647Scth * referenced directory is considered busy. 12550Sstevel@tonic-gate */ 1256647Scth if (dv->dv_busy || !(flags & DV_CLEAN_FORCE)) { 1257647Scth mutex_exit(&vp->v_lock); 12580Sstevel@tonic-gate rw_exit(&dv->dv_contents); 1259647Scth goto set_busy; 12600Sstevel@tonic-gate } 1261647Scth 1262647Scth /* 1263647Scth * Mark referenced directory stale so that DR 1264647Scth * will succeed even if a shell has 1265647Scth * /devices/xxx as current directory (causing 1266647Scth * VN_HOLD reference to an empty directory). 1267647Scth */ 1268647Scth ASSERT(!DV_STALE(dv)); 1269647Scth ndi_rele_devi(dv->dv_devi); 1270647Scth dv->dv_devi = NULL; /* mark DV_STALE */ 12710Sstevel@tonic-gate } 12720Sstevel@tonic-gate } else { 1273647Scth ASSERT((vp->v_type == VCHR) || (vp->v_type == VBLK)); 1274647Scth ASSERT(dv->dv_nlink == 1); /* no hard links */ 12750Sstevel@tonic-gate mutex_enter(&vp->v_lock); 12760Sstevel@tonic-gate if (vp->v_count > 0) { 1277647Scth mutex_exit(&vp->v_lock); 1278647Scth goto set_busy; 12790Sstevel@tonic-gate } 12800Sstevel@tonic-gate } 12810Sstevel@tonic-gate 12820Sstevel@tonic-gate /* unlink from directory */ 1283647Scth dv_unlink(ddv, dv, pprev); 12840Sstevel@tonic-gate 1285647Scth /* drop locks */ 1286647Scth mutex_exit(&vp->v_lock); 1287647Scth if (vp->v_type == VDIR) 1288647Scth rw_exit(&dv->dv_contents); 1289647Scth 1290647Scth /* destroy vnode if ref count is zero */ 1291647Scth if (vp->v_count == 0) 1292647Scth dv_destroy(dv, flags); 12930Sstevel@tonic-gate 1294647Scth /* pointer to previous stays unchanged */ 1295647Scth npprev = pprev; 1296647Scth continue; 1297647Scth 1298647Scth /* 1299647Scth * If devnm is not NULL we return immediately on busy, 1300647Scth * otherwise we continue destroying unused dv_node's. 1301647Scth */ 1302647Scth set_busy: busy++; 1303647Scth if (devnm) 1304647Scth break; 13050Sstevel@tonic-gate } 1306647Scth 13070Sstevel@tonic-gate /* 13080Sstevel@tonic-gate * This code may be invoked to inform devfs that a new node has 13090Sstevel@tonic-gate * been created in the kernel device tree. So we always set 13100Sstevel@tonic-gate * the DV_BUILD flag to allow the next dv_filldir() to pick 13110Sstevel@tonic-gate * the new devinfo nodes. 13120Sstevel@tonic-gate */ 13130Sstevel@tonic-gate ddv->dv_flags |= DV_BUILD; 13140Sstevel@tonic-gate 1315647Scth if (!(flags & DV_CLEANDIR_LCK)) 1316647Scth rw_exit(&ddv->dv_contents); 13170Sstevel@tonic-gate 1318647Scth return (busy ? EBUSY : 0); 13190Sstevel@tonic-gate } 13200Sstevel@tonic-gate 13210Sstevel@tonic-gate /* 13220Sstevel@tonic-gate * Walk through the devfs hierarchy, correcting the permissions of 13230Sstevel@tonic-gate * devices with default permissions that do not match those specified 13240Sstevel@tonic-gate * by minor perm. This can only be done for all drivers for now. 13250Sstevel@tonic-gate */ 13260Sstevel@tonic-gate static int 13270Sstevel@tonic-gate dv_reset_perm_dir(struct dv_node *ddv, uint_t flags) 13280Sstevel@tonic-gate { 13290Sstevel@tonic-gate struct dv_node *dv, *next = NULL; 13300Sstevel@tonic-gate struct vnode *vp; 13310Sstevel@tonic-gate int retval = 0; 13320Sstevel@tonic-gate struct vattr *attrp; 13330Sstevel@tonic-gate mperm_t mp; 13340Sstevel@tonic-gate char *nm; 13350Sstevel@tonic-gate uid_t old_uid; 13360Sstevel@tonic-gate gid_t old_gid; 13370Sstevel@tonic-gate mode_t old_mode; 13380Sstevel@tonic-gate 13390Sstevel@tonic-gate rw_enter(&ddv->dv_contents, RW_WRITER); 13400Sstevel@tonic-gate for (dv = ddv->dv_dot; dv; dv = next) { 13410Sstevel@tonic-gate int error = 0; 13420Sstevel@tonic-gate next = dv->dv_next; 13430Sstevel@tonic-gate nm = dv->dv_name; 13440Sstevel@tonic-gate 13450Sstevel@tonic-gate rw_enter(&dv->dv_contents, RW_READER); 13460Sstevel@tonic-gate vp = DVTOV(dv); 13470Sstevel@tonic-gate if (vp->v_type == VDIR) { 13480Sstevel@tonic-gate rw_exit(&dv->dv_contents); 13490Sstevel@tonic-gate if (dv_reset_perm_dir(dv, flags) != 0) { 13500Sstevel@tonic-gate error = EBUSY; 13510Sstevel@tonic-gate } 13520Sstevel@tonic-gate } else { 13530Sstevel@tonic-gate ASSERT(vp->v_type == VCHR || vp->v_type == VBLK); 13540Sstevel@tonic-gate 13550Sstevel@tonic-gate /* 13560Sstevel@tonic-gate * Check for permissions from minor_perm 13570Sstevel@tonic-gate * If there are none, we're done 13580Sstevel@tonic-gate */ 13590Sstevel@tonic-gate rw_exit(&dv->dv_contents); 13600Sstevel@tonic-gate if (dev_minorperm(dv->dv_devi, nm, &mp) != 0) 13610Sstevel@tonic-gate continue; 13620Sstevel@tonic-gate 13630Sstevel@tonic-gate rw_enter(&dv->dv_contents, RW_READER); 13640Sstevel@tonic-gate 13650Sstevel@tonic-gate /* 13660Sstevel@tonic-gate * Allow a node's permissions to be altered 13670Sstevel@tonic-gate * permanently from the defaults by chmod, 13680Sstevel@tonic-gate * using the shadow node as backing store. 13690Sstevel@tonic-gate * Otherwise, update node to minor_perm permissions. 13700Sstevel@tonic-gate */ 13710Sstevel@tonic-gate if (dv->dv_attrvp == NULLVP) { 13720Sstevel@tonic-gate /* 13730Sstevel@tonic-gate * No attribute vp, try to find one. 13740Sstevel@tonic-gate */ 13750Sstevel@tonic-gate dv_shadow_node(DVTOV(ddv), nm, vp, 13760Sstevel@tonic-gate NULL, NULLVP, kcred, 0); 13770Sstevel@tonic-gate } 13780Sstevel@tonic-gate if (dv->dv_attrvp != NULLVP || dv->dv_attr == NULL) { 13790Sstevel@tonic-gate rw_exit(&dv->dv_contents); 13800Sstevel@tonic-gate continue; 13810Sstevel@tonic-gate } 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate attrp = dv->dv_attr; 13840Sstevel@tonic-gate 13850Sstevel@tonic-gate if (VATTRP_MP_CMP(attrp, mp) == 0) { 13860Sstevel@tonic-gate dcmn_err5(("%s: no perm change: " 13870Sstevel@tonic-gate "%d %d 0%o\n", nm, attrp->va_uid, 13880Sstevel@tonic-gate attrp->va_gid, attrp->va_mode)); 13890Sstevel@tonic-gate rw_exit(&dv->dv_contents); 13900Sstevel@tonic-gate continue; 13910Sstevel@tonic-gate } 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate old_uid = attrp->va_uid; 13940Sstevel@tonic-gate old_gid = attrp->va_gid; 13950Sstevel@tonic-gate old_mode = attrp->va_mode; 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate VATTRP_MP_MERGE(attrp, mp); 13980Sstevel@tonic-gate mutex_enter(&vp->v_lock); 13990Sstevel@tonic-gate if (vp->v_count > 0) { 14000Sstevel@tonic-gate error = EBUSY; 14010Sstevel@tonic-gate } 14020Sstevel@tonic-gate mutex_exit(&vp->v_lock); 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate dcmn_err5(("%s: perm %d/%d/0%o -> %d/%d/0%o (%d)\n", 14050Sstevel@tonic-gate nm, old_uid, old_gid, old_mode, attrp->va_uid, 14060Sstevel@tonic-gate attrp->va_gid, attrp->va_mode, error)); 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate rw_exit(&dv->dv_contents); 14090Sstevel@tonic-gate } 14100Sstevel@tonic-gate 14110Sstevel@tonic-gate if (error != 0) { 14120Sstevel@tonic-gate retval = error; 14130Sstevel@tonic-gate } 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate 14160Sstevel@tonic-gate ddv->dv_flags |= DV_BUILD; 14170Sstevel@tonic-gate 14180Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 14190Sstevel@tonic-gate 14200Sstevel@tonic-gate return (retval); 14210Sstevel@tonic-gate } 14220Sstevel@tonic-gate 14230Sstevel@tonic-gate int 14240Sstevel@tonic-gate devfs_reset_perm(uint_t flags) 14250Sstevel@tonic-gate { 14260Sstevel@tonic-gate struct dv_node *dvp; 14270Sstevel@tonic-gate int rval; 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate if ((dvp = devfs_dip_to_dvnode(ddi_root_node())) == NULL) 14300Sstevel@tonic-gate return (0); 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate VN_HOLD(DVTOV(dvp)); 14330Sstevel@tonic-gate rval = dv_reset_perm_dir(dvp, flags); 14340Sstevel@tonic-gate VN_RELE(DVTOV(dvp)); 14350Sstevel@tonic-gate return (rval); 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate /* 14390Sstevel@tonic-gate * Clean up dangling devfs shadow nodes for removed 14400Sstevel@tonic-gate * drivers so that, in the event the driver is re-added 14410Sstevel@tonic-gate * to the system, newly created nodes won't incorrectly 14420Sstevel@tonic-gate * pick up these stale shadow node permissions. 14430Sstevel@tonic-gate * 14440Sstevel@tonic-gate * This is accomplished by walking down the pathname 14450Sstevel@tonic-gate * to the directory, starting at the root's attribute 14460Sstevel@tonic-gate * node, then removing all minors matching the specified 14470Sstevel@tonic-gate * node name. Care must be taken to remove all entries 14480Sstevel@tonic-gate * in a directory before the directory itself, so that 14490Sstevel@tonic-gate * the clean-up associated with rem_drv'ing a nexus driver 14500Sstevel@tonic-gate * does not inadvertently result in an inconsistent 14510Sstevel@tonic-gate * filesystem underlying devfs. 14520Sstevel@tonic-gate */ 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate static int 1455720Sjg devfs_remdrv_rmdir(vnode_t *dirvp, const char *dir, vnode_t *rvp) 14560Sstevel@tonic-gate { 14570Sstevel@tonic-gate int error; 14580Sstevel@tonic-gate vnode_t *vp; 14590Sstevel@tonic-gate int eof; 14600Sstevel@tonic-gate struct iovec iov; 14610Sstevel@tonic-gate struct uio uio; 14620Sstevel@tonic-gate struct dirent64 *dp; 14630Sstevel@tonic-gate dirent64_t *dbuf; 14640Sstevel@tonic-gate size_t dlen; 14650Sstevel@tonic-gate size_t dbuflen; 14660Sstevel@tonic-gate int ndirents = 64; 14670Sstevel@tonic-gate char *nm; 14680Sstevel@tonic-gate 14690Sstevel@tonic-gate VN_HOLD(dirvp); 14700Sstevel@tonic-gate 14710Sstevel@tonic-gate dlen = ndirents * (sizeof (*dbuf)); 14720Sstevel@tonic-gate dbuf = kmem_alloc(dlen, KM_SLEEP); 14730Sstevel@tonic-gate 14740Sstevel@tonic-gate uio.uio_iov = &iov; 14750Sstevel@tonic-gate uio.uio_iovcnt = 1; 14760Sstevel@tonic-gate uio.uio_segflg = UIO_SYSSPACE; 14770Sstevel@tonic-gate uio.uio_fmode = 0; 14780Sstevel@tonic-gate uio.uio_extflg = UIO_COPY_CACHED; 14790Sstevel@tonic-gate uio.uio_loffset = 0; 14800Sstevel@tonic-gate uio.uio_llimit = MAXOFFSET_T; 14810Sstevel@tonic-gate 14820Sstevel@tonic-gate eof = 0; 14830Sstevel@tonic-gate error = 0; 14840Sstevel@tonic-gate while (!error && !eof) { 14850Sstevel@tonic-gate uio.uio_resid = dlen; 14860Sstevel@tonic-gate iov.iov_base = (char *)dbuf; 14870Sstevel@tonic-gate iov.iov_len = dlen; 14880Sstevel@tonic-gate 14890Sstevel@tonic-gate (void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL); 14900Sstevel@tonic-gate error = VOP_READDIR(dirvp, &uio, kcred, &eof); 14910Sstevel@tonic-gate VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL); 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate dbuflen = dlen - uio.uio_resid; 14940Sstevel@tonic-gate 14950Sstevel@tonic-gate if (error || dbuflen == 0) 14960Sstevel@tonic-gate break; 14970Sstevel@tonic-gate 14980Sstevel@tonic-gate for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen); 14990Sstevel@tonic-gate dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) { 15000Sstevel@tonic-gate 15010Sstevel@tonic-gate nm = dp->d_name; 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0) 15040Sstevel@tonic-gate continue; 15050Sstevel@tonic-gate 15060Sstevel@tonic-gate error = VOP_LOOKUP(dirvp, 15070Sstevel@tonic-gate nm, &vp, NULL, 0, NULL, kcred); 15080Sstevel@tonic-gate 15090Sstevel@tonic-gate dsysdebug(error, 15100Sstevel@tonic-gate ("rem_drv %s/%s lookup (%d)\n", 15110Sstevel@tonic-gate dir, nm, error)); 15120Sstevel@tonic-gate 15130Sstevel@tonic-gate if (error) 15140Sstevel@tonic-gate continue; 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate ASSERT(vp->v_type == VDIR || 15170Sstevel@tonic-gate vp->v_type == VCHR || vp->v_type == VBLK); 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate if (vp->v_type == VDIR) { 1520720Sjg error = devfs_remdrv_rmdir(vp, nm, rvp); 15210Sstevel@tonic-gate if (error == 0) { 15220Sstevel@tonic-gate error = VOP_RMDIR(dirvp, 1523720Sjg (char *)nm, rvp, kcred); 15240Sstevel@tonic-gate dsysdebug(error, 15250Sstevel@tonic-gate ("rem_drv %s/%s rmdir (%d)\n", 15260Sstevel@tonic-gate dir, nm, error)); 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate } else { 15290Sstevel@tonic-gate error = VOP_REMOVE(dirvp, (char *)nm, kcred); 15300Sstevel@tonic-gate dsysdebug(error, 15310Sstevel@tonic-gate ("rem_drv %s/%s remove (%d)\n", 15320Sstevel@tonic-gate dir, nm, error)); 15330Sstevel@tonic-gate } 15340Sstevel@tonic-gate 15350Sstevel@tonic-gate VN_RELE(vp); 15360Sstevel@tonic-gate if (error) { 15370Sstevel@tonic-gate goto exit; 15380Sstevel@tonic-gate } 15390Sstevel@tonic-gate } 15400Sstevel@tonic-gate } 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate exit: 15430Sstevel@tonic-gate VN_RELE(dirvp); 15440Sstevel@tonic-gate kmem_free(dbuf, dlen); 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate return (error); 15470Sstevel@tonic-gate } 15480Sstevel@tonic-gate 15490Sstevel@tonic-gate int 15500Sstevel@tonic-gate devfs_remdrv_cleanup(const char *dir, const char *nodename) 15510Sstevel@tonic-gate { 15520Sstevel@tonic-gate int error; 15530Sstevel@tonic-gate vnode_t *vp; 15540Sstevel@tonic-gate vnode_t *dirvp; 15550Sstevel@tonic-gate int eof; 15560Sstevel@tonic-gate struct iovec iov; 15570Sstevel@tonic-gate struct uio uio; 15580Sstevel@tonic-gate struct dirent64 *dp; 15590Sstevel@tonic-gate dirent64_t *dbuf; 15600Sstevel@tonic-gate size_t dlen; 15610Sstevel@tonic-gate size_t dbuflen; 15620Sstevel@tonic-gate int ndirents = 64; 15630Sstevel@tonic-gate int nodenamelen = strlen(nodename); 15640Sstevel@tonic-gate char *nm; 15650Sstevel@tonic-gate struct pathname pn; 1566720Sjg vnode_t *rvp; /* root node of the underlying attribute fs */ 15670Sstevel@tonic-gate 15680Sstevel@tonic-gate dcmn_err5(("devfs_remdrv_cleanup: %s %s\n", dir, nodename)); 15690Sstevel@tonic-gate 15700Sstevel@tonic-gate if (error = pn_get((char *)dir, UIO_SYSSPACE, &pn)) 15710Sstevel@tonic-gate return (0); 15720Sstevel@tonic-gate 1573720Sjg rvp = dvroot->dv_attrvp; 1574720Sjg ASSERT(rvp != NULL); 1575720Sjg VN_HOLD(rvp); 15760Sstevel@tonic-gate 15770Sstevel@tonic-gate pn_skipslash(&pn); 1578720Sjg dirvp = rvp; 15790Sstevel@tonic-gate VN_HOLD(dirvp); 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate nm = kmem_alloc(MAXNAMELEN, KM_SLEEP); 15820Sstevel@tonic-gate 15830Sstevel@tonic-gate while (pn_pathleft(&pn)) { 15840Sstevel@tonic-gate ASSERT(dirvp->v_type == VDIR); 15850Sstevel@tonic-gate (void) pn_getcomponent(&pn, nm); 15860Sstevel@tonic-gate ASSERT((strcmp(nm, ".") != 0) && (strcmp(nm, "..") != 0)); 1587720Sjg error = VOP_LOOKUP(dirvp, nm, &vp, NULL, 0, rvp, kcred); 15880Sstevel@tonic-gate if (error) { 15890Sstevel@tonic-gate dcmn_err5(("remdrv_cleanup %s lookup error %d\n", 15900Sstevel@tonic-gate nm, error)); 15910Sstevel@tonic-gate VN_RELE(dirvp); 1592720Sjg if (dirvp != rvp) 1593720Sjg VN_RELE(rvp); 15940Sstevel@tonic-gate pn_free(&pn); 15950Sstevel@tonic-gate kmem_free(nm, MAXNAMELEN); 15960Sstevel@tonic-gate return (0); 15970Sstevel@tonic-gate } 15980Sstevel@tonic-gate VN_RELE(dirvp); 15990Sstevel@tonic-gate dirvp = vp; 16000Sstevel@tonic-gate pn_skipslash(&pn); 16010Sstevel@tonic-gate } 16020Sstevel@tonic-gate 16030Sstevel@tonic-gate ASSERT(dirvp->v_type == VDIR); 1604720Sjg if (dirvp != rvp) 1605720Sjg VN_RELE(rvp); 16060Sstevel@tonic-gate pn_free(&pn); 16070Sstevel@tonic-gate kmem_free(nm, MAXNAMELEN); 16080Sstevel@tonic-gate 16090Sstevel@tonic-gate dlen = ndirents * (sizeof (*dbuf)); 16100Sstevel@tonic-gate dbuf = kmem_alloc(dlen, KM_SLEEP); 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate uio.uio_iov = &iov; 16130Sstevel@tonic-gate uio.uio_iovcnt = 1; 16140Sstevel@tonic-gate uio.uio_segflg = UIO_SYSSPACE; 16150Sstevel@tonic-gate uio.uio_fmode = 0; 16160Sstevel@tonic-gate uio.uio_extflg = UIO_COPY_CACHED; 16170Sstevel@tonic-gate uio.uio_loffset = 0; 16180Sstevel@tonic-gate uio.uio_llimit = MAXOFFSET_T; 16190Sstevel@tonic-gate 16200Sstevel@tonic-gate eof = 0; 16210Sstevel@tonic-gate error = 0; 16220Sstevel@tonic-gate while (!error && !eof) { 16230Sstevel@tonic-gate uio.uio_resid = dlen; 16240Sstevel@tonic-gate iov.iov_base = (char *)dbuf; 16250Sstevel@tonic-gate iov.iov_len = dlen; 16260Sstevel@tonic-gate 16270Sstevel@tonic-gate (void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL); 16280Sstevel@tonic-gate error = VOP_READDIR(dirvp, &uio, kcred, &eof); 16290Sstevel@tonic-gate VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL); 16300Sstevel@tonic-gate 16310Sstevel@tonic-gate dbuflen = dlen - uio.uio_resid; 16320Sstevel@tonic-gate 16330Sstevel@tonic-gate if (error || dbuflen == 0) 16340Sstevel@tonic-gate break; 16350Sstevel@tonic-gate 16360Sstevel@tonic-gate for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen); 16370Sstevel@tonic-gate dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) { 16380Sstevel@tonic-gate 16390Sstevel@tonic-gate nm = dp->d_name; 16400Sstevel@tonic-gate 16410Sstevel@tonic-gate if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0) 16420Sstevel@tonic-gate continue; 16430Sstevel@tonic-gate 16440Sstevel@tonic-gate if (strncmp(nm, nodename, nodenamelen) != 0) 16450Sstevel@tonic-gate continue; 16460Sstevel@tonic-gate 16470Sstevel@tonic-gate error = VOP_LOOKUP(dirvp, nm, &vp, 16480Sstevel@tonic-gate NULL, 0, NULL, kcred); 16490Sstevel@tonic-gate 16500Sstevel@tonic-gate dsysdebug(error, 16510Sstevel@tonic-gate ("rem_drv %s/%s lookup (%d)\n", 16520Sstevel@tonic-gate dir, nm, error)); 16530Sstevel@tonic-gate 16540Sstevel@tonic-gate if (error) 16550Sstevel@tonic-gate continue; 16560Sstevel@tonic-gate 16570Sstevel@tonic-gate ASSERT(vp->v_type == VDIR || 16580Sstevel@tonic-gate vp->v_type == VCHR || vp->v_type == VBLK); 16590Sstevel@tonic-gate 16600Sstevel@tonic-gate if (vp->v_type == VDIR) { 1661720Sjg error = devfs_remdrv_rmdir(vp, nm, rvp); 16620Sstevel@tonic-gate if (error == 0) { 16630Sstevel@tonic-gate error = VOP_RMDIR(dirvp, 1664720Sjg (char *)nm, rvp, kcred); 16650Sstevel@tonic-gate dsysdebug(error, 16660Sstevel@tonic-gate ("rem_drv %s/%s rmdir (%d)\n", 16670Sstevel@tonic-gate dir, nm, error)); 16680Sstevel@tonic-gate } 16690Sstevel@tonic-gate } else { 16700Sstevel@tonic-gate error = VOP_REMOVE(dirvp, (char *)nm, kcred); 16710Sstevel@tonic-gate dsysdebug(error, 16720Sstevel@tonic-gate ("rem_drv %s/%s remove (%d)\n", 16730Sstevel@tonic-gate dir, nm, error)); 16740Sstevel@tonic-gate } 16750Sstevel@tonic-gate 16760Sstevel@tonic-gate VN_RELE(vp); 16770Sstevel@tonic-gate if (error) 16780Sstevel@tonic-gate goto exit; 16790Sstevel@tonic-gate } 16800Sstevel@tonic-gate } 16810Sstevel@tonic-gate 16820Sstevel@tonic-gate exit: 16830Sstevel@tonic-gate VN_RELE(dirvp); 16840Sstevel@tonic-gate 16850Sstevel@tonic-gate kmem_free(dbuf, dlen); 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate return (0); 16880Sstevel@tonic-gate } 16890Sstevel@tonic-gate 16900Sstevel@tonic-gate struct dv_list { 16910Sstevel@tonic-gate struct dv_node *dv; 16920Sstevel@tonic-gate struct dv_list *next; 16930Sstevel@tonic-gate }; 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate void 16960Sstevel@tonic-gate dv_walk( 16970Sstevel@tonic-gate struct dv_node *ddv, 16980Sstevel@tonic-gate char *devnm, 16990Sstevel@tonic-gate void (*callback)(struct dv_node *, void *), 17000Sstevel@tonic-gate void *arg) 17010Sstevel@tonic-gate { 17020Sstevel@tonic-gate struct vnode *dvp; 17030Sstevel@tonic-gate struct dv_node *dv; 17040Sstevel@tonic-gate struct dv_list *head, *tail, *next; 1705647Scth int len; 17060Sstevel@tonic-gate 17070Sstevel@tonic-gate dcmn_err3(("dv_walk: ddv = %s, devnm = %s\n", 17080Sstevel@tonic-gate ddv->dv_name, devnm ? devnm : "<null>")); 17090Sstevel@tonic-gate 17100Sstevel@tonic-gate dvp = DVTOV(ddv); 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate ASSERT(dvp->v_type == VDIR); 17130Sstevel@tonic-gate 17140Sstevel@tonic-gate head = tail = next = NULL; 17150Sstevel@tonic-gate 1716647Scth rw_enter(&ddv->dv_contents, RW_READER); 17170Sstevel@tonic-gate mutex_enter(&dvp->v_lock); 17180Sstevel@tonic-gate for (dv = ddv->dv_dot; dv; dv = dv->dv_next) { 17190Sstevel@tonic-gate /* 17200Sstevel@tonic-gate * If devnm is not NULL and is not the empty string, 17210Sstevel@tonic-gate * select only dv_nodes with matching non-minor name 17220Sstevel@tonic-gate */ 17230Sstevel@tonic-gate if (devnm && (len = strlen(devnm)) && 17240Sstevel@tonic-gate (strncmp(devnm, dv->dv_name, len) || 17250Sstevel@tonic-gate (dv->dv_name[len] != ':' && dv->dv_name[len] != '\0'))) 17260Sstevel@tonic-gate continue; 17270Sstevel@tonic-gate 17280Sstevel@tonic-gate callback(dv, arg); 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate if (DVTOV(dv)->v_type != VDIR) 17310Sstevel@tonic-gate continue; 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate next = kmem_zalloc(sizeof (*next), KM_SLEEP); 17340Sstevel@tonic-gate next->dv = dv; 17350Sstevel@tonic-gate 17360Sstevel@tonic-gate if (tail) 17370Sstevel@tonic-gate tail->next = next; 17380Sstevel@tonic-gate else 17390Sstevel@tonic-gate head = next; 17400Sstevel@tonic-gate 17410Sstevel@tonic-gate tail = next; 17420Sstevel@tonic-gate } 17430Sstevel@tonic-gate 17440Sstevel@tonic-gate while (head) { 17450Sstevel@tonic-gate dv_walk(head->dv, NULL, callback, arg); 17460Sstevel@tonic-gate next = head->next; 17470Sstevel@tonic-gate kmem_free(head, sizeof (*head)); 17480Sstevel@tonic-gate head = next; 17490Sstevel@tonic-gate } 17500Sstevel@tonic-gate rw_exit(&ddv->dv_contents); 17510Sstevel@tonic-gate mutex_exit(&dvp->v_lock); 17520Sstevel@tonic-gate } 1753