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