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 52155Scth * Common Development and Distribution License (the "License"). 62155Scth * 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 /* 223898Srsb * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * This is the device filesystem. 300Sstevel@tonic-gate * 310Sstevel@tonic-gate * It is a combination of a namer to drive autoconfiguration, 320Sstevel@tonic-gate * plus the access methods for the device drivers of the system. 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * The prototype is fairly dependent on specfs for the latter part 350Sstevel@tonic-gate * of its implementation, though a final version would integrate the two. 360Sstevel@tonic-gate */ 370Sstevel@tonic-gate #include <sys/types.h> 380Sstevel@tonic-gate #include <sys/param.h> 390Sstevel@tonic-gate #include <sys/sysmacros.h> 400Sstevel@tonic-gate #include <sys/systm.h> 410Sstevel@tonic-gate #include <sys/kmem.h> 420Sstevel@tonic-gate #include <sys/time.h> 430Sstevel@tonic-gate #include <sys/pathname.h> 440Sstevel@tonic-gate #include <sys/vfs.h> 453898Srsb #include <sys/vfs_opreg.h> 460Sstevel@tonic-gate #include <sys/vnode.h> 470Sstevel@tonic-gate #include <sys/stat.h> 480Sstevel@tonic-gate #include <sys/uio.h> 490Sstevel@tonic-gate #include <sys/stat.h> 500Sstevel@tonic-gate #include <sys/errno.h> 510Sstevel@tonic-gate #include <sys/cmn_err.h> 520Sstevel@tonic-gate #include <sys/cred.h> 530Sstevel@tonic-gate #include <sys/statvfs.h> 540Sstevel@tonic-gate #include <sys/mount.h> 550Sstevel@tonic-gate #include <sys/debug.h> 560Sstevel@tonic-gate #include <sys/modctl.h> 570Sstevel@tonic-gate #include <fs/fs_subr.h> 580Sstevel@tonic-gate #include <sys/fs/dv_node.h> 590Sstevel@tonic-gate #include <sys/fs/snode.h> 600Sstevel@tonic-gate #include <sys/sunndi.h> 610Sstevel@tonic-gate #include <sys/policy.h> 622155Scth #include <sys/sunmdi.h> 630Sstevel@tonic-gate 640Sstevel@tonic-gate /* 650Sstevel@tonic-gate * devfs vfs operations. 660Sstevel@tonic-gate */ 670Sstevel@tonic-gate static int devfs_mount(struct vfs *, struct vnode *, struct mounta *, 680Sstevel@tonic-gate struct cred *); 690Sstevel@tonic-gate static int devfs_unmount(struct vfs *, int, struct cred *); 700Sstevel@tonic-gate static int devfs_root(struct vfs *, struct vnode **); 710Sstevel@tonic-gate static int devfs_statvfs(struct vfs *, struct statvfs64 *); 720Sstevel@tonic-gate static int devfs_mountroot(struct vfs *, enum whymountroot); 730Sstevel@tonic-gate 740Sstevel@tonic-gate static int devfsinit(int, char *); 750Sstevel@tonic-gate 760Sstevel@tonic-gate static vfsdef_t devfs_vfssw = { 770Sstevel@tonic-gate VFSDEF_VERSION, 780Sstevel@tonic-gate "devfs", /* type name string */ 790Sstevel@tonic-gate devfsinit, /* init routine */ 800Sstevel@tonic-gate 0, /* flags */ 810Sstevel@tonic-gate NULL /* mount options table prototype */ 820Sstevel@tonic-gate }; 830Sstevel@tonic-gate 840Sstevel@tonic-gate static kmutex_t devfs_lock; /* protects global data */ 850Sstevel@tonic-gate static int devfstype; /* fstype */ 860Sstevel@tonic-gate static dev_t devfsdev; /* the fictious 'device' we live on */ 870Sstevel@tonic-gate static struct devfs_data *devfs_mntinfo; /* linked list of instances */ 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * Module linkage information 910Sstevel@tonic-gate */ 920Sstevel@tonic-gate static struct modlfs modlfs = { 930Sstevel@tonic-gate &mod_fsops, "devices filesystem %I%", &devfs_vfssw 940Sstevel@tonic-gate }; 950Sstevel@tonic-gate 960Sstevel@tonic-gate static struct modlinkage modlinkage = { 970Sstevel@tonic-gate MODREV_1, (void *)&modlfs, NULL 980Sstevel@tonic-gate }; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate int 1010Sstevel@tonic-gate _init(void) 1020Sstevel@tonic-gate { 1030Sstevel@tonic-gate int e; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate mutex_init(&devfs_lock, "devfs lock", MUTEX_DEFAULT, NULL); 1060Sstevel@tonic-gate dv_node_cache_init(); 1070Sstevel@tonic-gate if ((e = mod_install(&modlinkage)) != 0) { 1080Sstevel@tonic-gate dv_node_cache_fini(); 1090Sstevel@tonic-gate mutex_destroy(&devfs_lock); 1100Sstevel@tonic-gate return (e); 1110Sstevel@tonic-gate } 1120Sstevel@tonic-gate dcmn_err(("devfs loaded\n")); 1130Sstevel@tonic-gate return (0); 1140Sstevel@tonic-gate } 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate int 1170Sstevel@tonic-gate _fini(void) 1180Sstevel@tonic-gate { 1190Sstevel@tonic-gate return (EBUSY); 1200Sstevel@tonic-gate } 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate int 1230Sstevel@tonic-gate _info(struct modinfo *modinfop) 1240Sstevel@tonic-gate { 1250Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /*ARGSUSED1*/ 1290Sstevel@tonic-gate static int 1300Sstevel@tonic-gate devfsinit(int fstype, char *name) 1310Sstevel@tonic-gate { 1320Sstevel@tonic-gate static const fs_operation_def_t devfs_vfsops_template[] = { 1333898Srsb VFSNAME_MOUNT, { .vfs_mount = devfs_mount }, 1343898Srsb VFSNAME_UNMOUNT, { .vfs_unmount = devfs_unmount }, 1353898Srsb VFSNAME_ROOT, { .vfs_root = devfs_root }, 1363898Srsb VFSNAME_STATVFS, { .vfs_statvfs = devfs_statvfs }, 1373898Srsb VFSNAME_SYNC, { .vfs_sync = fs_sync }, 1383898Srsb VFSNAME_MOUNTROOT, { .vfs_mountroot = devfs_mountroot }, 1393898Srsb NULL, NULL 1400Sstevel@tonic-gate }; 1410Sstevel@tonic-gate int error; 1420Sstevel@tonic-gate int dev; 1430Sstevel@tonic-gate extern major_t getudev(void); /* gack - what a function */ 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate devfstype = fstype; 1460Sstevel@tonic-gate /* 1470Sstevel@tonic-gate * Associate VFS ops vector with this fstype 1480Sstevel@tonic-gate */ 1490Sstevel@tonic-gate error = vfs_setfsops(fstype, devfs_vfsops_template, NULL); 1500Sstevel@tonic-gate if (error != 0) { 1510Sstevel@tonic-gate cmn_err(CE_WARN, "devfsinit: bad vfs ops template"); 1520Sstevel@tonic-gate return (error); 1530Sstevel@tonic-gate } 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate error = vn_make_ops("dev fs", dv_vnodeops_template, &dv_vnodeops); 1560Sstevel@tonic-gate if (error != 0) { 1570Sstevel@tonic-gate (void) vfs_freevfsops_by_type(fstype); 1580Sstevel@tonic-gate cmn_err(CE_WARN, "devfsinit: bad vnode ops template"); 1590Sstevel@tonic-gate return (error); 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* 1630Sstevel@tonic-gate * Invent a dev_t (sigh). 1640Sstevel@tonic-gate */ 1650Sstevel@tonic-gate if ((dev = getudev()) == (major_t)-1) { 1660Sstevel@tonic-gate cmn_err(CE_NOTE, "%s: can't get unique dev", devfs_vfssw.name); 1670Sstevel@tonic-gate dev = 0; 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate devfsdev = makedevice(dev, 0); 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate return (0); 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate /* 1750Sstevel@tonic-gate * The name of the mount point and the name of the attribute 1760Sstevel@tonic-gate * filesystem are passed down from userland for now. 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate static int 1790Sstevel@tonic-gate devfs_mount(struct vfs *vfsp, struct vnode *mvp, struct mounta *uap, 1800Sstevel@tonic-gate struct cred *cr) 1810Sstevel@tonic-gate { 1820Sstevel@tonic-gate struct devfs_data *devfs_data; 1830Sstevel@tonic-gate struct vnode *avp; 1840Sstevel@tonic-gate struct dv_node *dv; 1850Sstevel@tonic-gate struct vattr va; 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate dcmn_err(("devfs_mount\n")); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate if (secpolicy_fs_mount(cr, mvp, vfsp) != 0) 1900Sstevel@tonic-gate return (EPERM); 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate /* 1930Sstevel@tonic-gate * check that the mount point is sane 1940Sstevel@tonic-gate */ 1950Sstevel@tonic-gate if (mvp->v_type != VDIR) 1960Sstevel@tonic-gate return (ENOTDIR); 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate ASSERT(uap->flags & MS_SYSSPACE); 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * Devfs can only be mounted from kernel during boot. 2010Sstevel@tonic-gate * avp is the existing /devices, the same as the mount point. 2020Sstevel@tonic-gate */ 2030Sstevel@tonic-gate avp = mvp; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate /* 2060Sstevel@tonic-gate * Create and initialize the vfs-private data. 2070Sstevel@tonic-gate * This includes a hand-crafted root vnode (we build 2080Sstevel@tonic-gate * this here mostly so that traverse() doesn't sleep 2090Sstevel@tonic-gate * in VFS_ROOT()). 2100Sstevel@tonic-gate */ 2110Sstevel@tonic-gate mutex_enter(&devfs_lock); 2120Sstevel@tonic-gate ASSERT(devfs_mntinfo == NULL); 2130Sstevel@tonic-gate dv = dv_mkroot(vfsp, devfsdev); 2140Sstevel@tonic-gate dv->dv_attrvp = avp; /* attribute root vp */ 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate ASSERT(dv == dv->dv_dotdot); 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate devfs_data = kmem_zalloc(sizeof (struct devfs_data), KM_SLEEP); 2190Sstevel@tonic-gate devfs_data->devfs_vfsp = vfsp; 2200Sstevel@tonic-gate devfs_data->devfs_root = dv; 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate vfsp->vfs_data = (caddr_t)devfs_data; 2230Sstevel@tonic-gate vfsp->vfs_fstype = devfstype; 2240Sstevel@tonic-gate vfsp->vfs_dev = devfsdev; 2250Sstevel@tonic-gate vfsp->vfs_bsize = DEV_BSIZE; 2260Sstevel@tonic-gate vfsp->vfs_mtime = ddi_get_time(); 2270Sstevel@tonic-gate vfs_make_fsid(&vfsp->vfs_fsid, vfsp->vfs_dev, devfstype); 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /* We're there. */ 2300Sstevel@tonic-gate devfs_mntinfo = devfs_data; 2310Sstevel@tonic-gate mutex_exit(&devfs_lock); 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate va.va_mask = AT_ATIME|AT_MTIME; 2340Sstevel@tonic-gate gethrestime(&va.va_atime); 2350Sstevel@tonic-gate gethrestime(&va.va_mtime); 2360Sstevel@tonic-gate (void) VOP_SETATTR(DVTOV(dv), &va, 0, cr, NULL); 2370Sstevel@tonic-gate return (0); 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate /* 2420Sstevel@tonic-gate * We never unmount devfs in a real production system. 2430Sstevel@tonic-gate */ 2440Sstevel@tonic-gate /*ARGSUSED*/ 2450Sstevel@tonic-gate static int 2460Sstevel@tonic-gate devfs_unmount(struct vfs *vfsp, int flag, struct cred *cr) 2470Sstevel@tonic-gate { 2480Sstevel@tonic-gate return (EBUSY); 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* 2520Sstevel@tonic-gate * return root vnode for given vfs 2530Sstevel@tonic-gate */ 2540Sstevel@tonic-gate static int 2550Sstevel@tonic-gate devfs_root(struct vfs *vfsp, struct vnode **vpp) 2560Sstevel@tonic-gate { 2570Sstevel@tonic-gate dcmn_err(("devfs_root\n")); 2580Sstevel@tonic-gate *vpp = DVTOV(VFSTODVFS(vfsp)->devfs_root); 2590Sstevel@tonic-gate VN_HOLD(*vpp); 2600Sstevel@tonic-gate return (0); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * return 'generic superblock' information to userland. 2650Sstevel@tonic-gate * 2660Sstevel@tonic-gate * not much that we can usefully admit to here 2670Sstevel@tonic-gate */ 2680Sstevel@tonic-gate static int 2690Sstevel@tonic-gate devfs_statvfs(struct vfs *vfsp, struct statvfs64 *sbp) 2700Sstevel@tonic-gate { 2710Sstevel@tonic-gate extern kmem_cache_t *dv_node_cache; 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate dev32_t d32; 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate dcmn_err(("devfs_statvfs\n")); 2760Sstevel@tonic-gate bzero(sbp, sizeof (*sbp)); 2770Sstevel@tonic-gate sbp->f_frsize = sbp->f_bsize = vfsp->vfs_bsize; 2780Sstevel@tonic-gate /* 2790Sstevel@tonic-gate * We could compute the number of devfsnodes here .. but since 2800Sstevel@tonic-gate * it's dynamic anyway, it's not clear how useful this is. 2810Sstevel@tonic-gate */ 2820Sstevel@tonic-gate sbp->f_files = kmem_cache_stat(dv_node_cache, "alloc"); 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate /* no illusions that free/avail files is relevant to devfs */ 2850Sstevel@tonic-gate sbp->f_ffree = 0; 2860Sstevel@tonic-gate sbp->f_favail = 0; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate /* no illusions that blocks are relevant to devfs */ 2890Sstevel@tonic-gate sbp->f_bfree = 0; 2900Sstevel@tonic-gate sbp->f_bavail = 0; 2910Sstevel@tonic-gate sbp->f_blocks = 0; 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate (void) cmpldev(&d32, vfsp->vfs_dev); 2940Sstevel@tonic-gate sbp->f_fsid = d32; 2950Sstevel@tonic-gate (void) strcpy(sbp->f_basetype, vfssw[devfstype].vsw_name); 2960Sstevel@tonic-gate sbp->f_flag = vf_to_stf(vfsp->vfs_flag); 2970Sstevel@tonic-gate sbp->f_namemax = MAXNAMELEN - 1; 2980Sstevel@tonic-gate (void) strcpy(sbp->f_fstr, "devices"); 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate return (0); 3010Sstevel@tonic-gate } 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate /* 3040Sstevel@tonic-gate * devfs always mount after root is mounted, so this should never 3050Sstevel@tonic-gate * be invoked. 3060Sstevel@tonic-gate */ 3070Sstevel@tonic-gate /*ARGSUSED*/ 3080Sstevel@tonic-gate static int 3090Sstevel@tonic-gate devfs_mountroot(struct vfs *vfsp, enum whymountroot why) 3100Sstevel@tonic-gate { 3110Sstevel@tonic-gate dcmn_err(("devfs_mountroot\n")); 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate return (EINVAL); 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate struct dv_node * 3170Sstevel@tonic-gate devfs_dip_to_dvnode(dev_info_t *dip) 3180Sstevel@tonic-gate { 3190Sstevel@tonic-gate char *dirpath; 3200Sstevel@tonic-gate struct vnode *dirvp; 3210Sstevel@tonic-gate 3220Sstevel@tonic-gate ASSERT(dip != NULL); 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* no-op if devfs not mounted yet */ 3250Sstevel@tonic-gate if (devfs_mntinfo == NULL) 3260Sstevel@tonic-gate return (NULL); 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* 3290Sstevel@tonic-gate * The lookupname below only looks up cached dv_nodes 3300Sstevel@tonic-gate * because devfs_clean_key is set in thread specific data. 3310Sstevel@tonic-gate */ 3320Sstevel@tonic-gate dirpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 3330Sstevel@tonic-gate (void) ddi_pathname(dip, dirpath); 3340Sstevel@tonic-gate if (devfs_lookupname(dirpath, NULLVPP, &dirvp)) { 3350Sstevel@tonic-gate dcmn_err(("directory %s not found\n", dirpath)); 3360Sstevel@tonic-gate kmem_free(dirpath, MAXPATHLEN); 3370Sstevel@tonic-gate return (NULL); 3380Sstevel@tonic-gate } 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate kmem_free(dirpath, MAXPATHLEN); 3410Sstevel@tonic-gate return (VTODV(dirvp)); 3420Sstevel@tonic-gate } 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate /* 3452155Scth * If DV_CLEAN_FORCE devfs_clean is issued with a dip that is not the root 3462155Scth * and not a vHCI we also need to clean any vHCI branches because they 3472155Scth * may contain pHCI nodes. A detach_node() of a pHCI will fail if its 3482155Scth * mdi_devi_offline() fails, and the mdi_devi_offline() of the last 3492155Scth * pHCI will fail unless an ndi_devi_offline() of the Client nodes under 3502155Scth * the vHCI is successful - which requires a clean vHCI branch to removed 3512155Scth * the devi_refs associated with devfs vnodes. 3522155Scth */ 3532155Scth static int 3542155Scth devfs_clean_vhci(dev_info_t *dip, void *args) 3552155Scth { 3562155Scth struct dv_node *dvp; 3572155Scth uint_t flags = (uint_t)(uintptr_t)args; 3582155Scth 3592155Scth (void) tsd_set(devfs_clean_key, (void *)1); 3602155Scth dvp = devfs_dip_to_dvnode(dip); 3612155Scth (void) tsd_set(devfs_clean_key, NULL); 3622155Scth if (dvp) { 3632155Scth (void) dv_cleandir(dvp, NULL, flags); 3642155Scth VN_RELE(DVTOV(dvp)); 3652155Scth } 3662155Scth return (DDI_WALK_CONTINUE); 3672155Scth } 3682155Scth 3692155Scth /* 3700Sstevel@tonic-gate * devfs_clean() 3710Sstevel@tonic-gate * 3720Sstevel@tonic-gate * Destroy unreferenced dv_node's and detach devices. 373*4411Svikram * 374*4411Svikram * devfs_clean will try its best to clean up unused nodes. It is 375*4411Svikram * no longer valid to assume that just because devfs_clean fails, 376*4411Svikram * the device is not removable. This is because device contracts 377*4411Svikram * can result in userland processes releasing a device during the 378*4411Svikram * device offline process in the kernel. Thus it is no longer 379*4411Svikram * correct to fail an offline just because devfs_clean finds 380*4411Svikram * referenced dv_nodes. To enforce this, devfs_clean() always 381*4411Svikram * returns success i.e. 0. 3820Sstevel@tonic-gate * 3830Sstevel@tonic-gate * devfs caches unreferenced dv_node to speed by the performance 3840Sstevel@tonic-gate * of ls, find, etc. devfs_clean() is invoked to cleanup cached 3850Sstevel@tonic-gate * dv_nodes to reclaim memory as well as to facilitate device 3860Sstevel@tonic-gate * removal (dv_node reference devinfo nodes, which prevents driver 3870Sstevel@tonic-gate * detach). 3880Sstevel@tonic-gate * 3890Sstevel@tonic-gate * If a shell parks in a /devices directory, the dv_node will be 3900Sstevel@tonic-gate * held, preventing the corresponding device to be detached. 3910Sstevel@tonic-gate * This would be a denial of service against DR. To prevent this, 3920Sstevel@tonic-gate * DR code calls devfs_clean() with the DV_CLEAN_FORCE flag. 3930Sstevel@tonic-gate * The dv_cleandir() implementation does the right thing to ensure 3940Sstevel@tonic-gate * successful DR. 3950Sstevel@tonic-gate */ 3960Sstevel@tonic-gate int 3970Sstevel@tonic-gate devfs_clean(dev_info_t *dip, char *devnm, uint_t flags) 3980Sstevel@tonic-gate { 3992155Scth struct dv_node *dvp; 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate dcmn_err(("devfs_unconfigure: dip = 0x%p, flags = 0x%x", 4020Sstevel@tonic-gate (void *)dip, flags)); 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* avoid recursion back into the device tree */ 4050Sstevel@tonic-gate (void) tsd_set(devfs_clean_key, (void *)1); 4060Sstevel@tonic-gate dvp = devfs_dip_to_dvnode(dip); 4070Sstevel@tonic-gate (void) tsd_set(devfs_clean_key, NULL); 4080Sstevel@tonic-gate if (dvp == NULL) 4090Sstevel@tonic-gate return (0); 4100Sstevel@tonic-gate 411*4411Svikram (void) dv_cleandir(dvp, devnm, flags); 4122155Scth VN_RELE(DVTOV(dvp)); 4132155Scth 4142155Scth /* 4152155Scth * If we are doing a DV_CLEAN_FORCE, and we did not start at the 4162155Scth * root, and we did not start at a vHCI node then clean vHCI 4172155Scth * branches too. Failure to clean vHCI branch does not cause EBUSY. 4182155Scth * 4192155Scth * Also, to accommodate nexus callers that clean 'self' to DR 'child' 4202155Scth * (like pcihp) we clean vHCIs even when dv_cleandir() of dip branch 4212155Scth * above fails - this prevents a busy DR 'child' sibling from causing 4222155Scth * the DR of 'child' to fail because a vHCI branch was not cleaned. 4232155Scth */ 4242155Scth if ((flags & DV_CLEAN_FORCE) && (dip != ddi_root_node()) && 4252155Scth (mdi_component_is_vhci(dip, NULL) != MDI_SUCCESS)) { 4262155Scth /* 4272155Scth * NOTE: for backport the following is recommended 4282155Scth * (void) devfs_clean_vhci(scsi_vhci_dip, 4292155Scth * (void *)(uintptr_t)flags); 4302155Scth */ 4312155Scth mdi_walk_vhcis(devfs_clean_vhci, (void *)(uintptr_t)flags); 4320Sstevel@tonic-gate } 4332155Scth 434*4411Svikram return (0); 4350Sstevel@tonic-gate } 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate /* 4380Sstevel@tonic-gate * lookup a devfs relative pathname, returning held vnodes for the final 4390Sstevel@tonic-gate * component and the containing directory (if requested). 4400Sstevel@tonic-gate * 4410Sstevel@tonic-gate * NOTE: We can't use lookupname because this would use the current 4420Sstevel@tonic-gate * processes credentials (CRED) in the call lookuppnvp instead 4430Sstevel@tonic-gate * of kcred. It also does not give you the flexibility so 4440Sstevel@tonic-gate * specify the directory to start the resolution in (devicesdir). 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate int 4470Sstevel@tonic-gate devfs_lookupname( 4480Sstevel@tonic-gate char *pathname, /* user pathname */ 4490Sstevel@tonic-gate vnode_t **dirvpp, /* ret for ptr to parent dir vnode */ 4500Sstevel@tonic-gate vnode_t **compvpp) /* ret for ptr to component vnode */ 4510Sstevel@tonic-gate { 4520Sstevel@tonic-gate struct pathname pn; 4530Sstevel@tonic-gate int error; 4540Sstevel@tonic-gate 4550Sstevel@tonic-gate ASSERT(devicesdir); /* devfs must be initialized */ 4560Sstevel@tonic-gate ASSERT(pathname); /* must have some path */ 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate if (error = pn_get(pathname, UIO_SYSSPACE, &pn)) 4590Sstevel@tonic-gate return (error); 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate /* make the path relative to /devices. */ 4620Sstevel@tonic-gate pn_skipslash(&pn); 4630Sstevel@tonic-gate if (pn_pathleft(&pn) == 0) { 4640Sstevel@tonic-gate /* all we had was "\0" or "/" (which skipslash skiped) */ 4650Sstevel@tonic-gate if (dirvpp) 4660Sstevel@tonic-gate *dirvpp = NULL; 4670Sstevel@tonic-gate if (compvpp) { 4680Sstevel@tonic-gate VN_HOLD(devicesdir); 4690Sstevel@tonic-gate *compvpp = devicesdir; 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate } else { 4720Sstevel@tonic-gate /* 4730Sstevel@tonic-gate * Use devfs lookup to resolve pathname to the vnode for 4740Sstevel@tonic-gate * the device via relative lookup in devfs. Extra holds for 4750Sstevel@tonic-gate * using devicesdir as directory we are searching and for 4760Sstevel@tonic-gate * being our root without being == rootdir. 4770Sstevel@tonic-gate */ 4780Sstevel@tonic-gate VN_HOLD(devicesdir); 4790Sstevel@tonic-gate VN_HOLD(devicesdir); 4800Sstevel@tonic-gate error = lookuppnvp(&pn, NULL, FOLLOW, dirvpp, compvpp, 4810Sstevel@tonic-gate devicesdir, devicesdir, kcred); 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate pn_free(&pn); 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate return (error); 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate /* 4890Sstevel@tonic-gate * Given a devfs path (without the /devices prefix), walk 4900Sstevel@tonic-gate * the dv_node sub-tree rooted at the path. 4910Sstevel@tonic-gate */ 4920Sstevel@tonic-gate int 4930Sstevel@tonic-gate devfs_walk( 4940Sstevel@tonic-gate char *path, 4950Sstevel@tonic-gate void (*callback)(struct dv_node *, void *), 4960Sstevel@tonic-gate void *arg) 4970Sstevel@tonic-gate { 4980Sstevel@tonic-gate char *dirpath, *devnm; 4990Sstevel@tonic-gate struct vnode *dirvp; 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate ASSERT(path && callback); 5020Sstevel@tonic-gate 5030Sstevel@tonic-gate if (*path != '/' || devfs_mntinfo == NULL) 5040Sstevel@tonic-gate return (ENXIO); 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate dcmn_err(("devfs_walk: path = %s", path)); 5070Sstevel@tonic-gate 5080Sstevel@tonic-gate dirpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate (void) snprintf(dirpath, MAXPATHLEN, "/devices%s", path); 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate devnm = strrchr(dirpath, '/'); 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate ASSERT(devnm); 5150Sstevel@tonic-gate 5160Sstevel@tonic-gate *devnm++ = '\0'; 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate if (lookupname(dirpath, UIO_SYSSPACE, 0, NULL, &dirvp)) { 5190Sstevel@tonic-gate dcmn_err(("directory %s not found\n", dirpath)); 5200Sstevel@tonic-gate kmem_free(dirpath, MAXPATHLEN); 5210Sstevel@tonic-gate return (ENXIO); 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate /* 5250Sstevel@tonic-gate * if path == "/", visit the root dv_node 5260Sstevel@tonic-gate */ 5270Sstevel@tonic-gate if (*devnm == '\0') { 5280Sstevel@tonic-gate callback(VTODV(dirvp), arg); 5290Sstevel@tonic-gate devnm = NULL; 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate 5320Sstevel@tonic-gate dv_walk(VTODV(dirvp), devnm, callback, arg); 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate VN_RELE(dirvp); 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate kmem_free(dirpath, MAXPATHLEN); 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate return (0); 5390Sstevel@tonic-gate } 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate int 5420Sstevel@tonic-gate devfs_devpolicy(vnode_t *vp, devplcy_t **dpp) 5430Sstevel@tonic-gate { 5440Sstevel@tonic-gate struct vnode *rvp; 5450Sstevel@tonic-gate struct dv_node *dvp; 5460Sstevel@tonic-gate int rval = -1; 5470Sstevel@tonic-gate 5480Sstevel@tonic-gate /* fail if devfs not mounted yet */ 5490Sstevel@tonic-gate if (devfs_mntinfo == NULL) 5500Sstevel@tonic-gate return (rval); 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate if (VOP_REALVP(vp, &rvp) == 0 && vn_matchops(rvp, dv_vnodeops)) { 5530Sstevel@tonic-gate dvp = VTODV(rvp); 5540Sstevel@tonic-gate rw_enter(&dvp->dv_contents, RW_READER); 5550Sstevel@tonic-gate if (dvp->dv_priv) { 5560Sstevel@tonic-gate dphold(dvp->dv_priv); 5570Sstevel@tonic-gate *dpp = dvp->dv_priv; 5580Sstevel@tonic-gate rval = 0; 5590Sstevel@tonic-gate } 5600Sstevel@tonic-gate rw_exit(&dvp->dv_contents); 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate return (rval); 5630Sstevel@tonic-gate } 564