#include <sys/types.h>
#include <sys/param.h>
#include <sys/t_lock.h>
#include <sys/systm.h>
#include <sys/sysmacros.h>
#include <sys/user.h>
#include <sys/time.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/flock.h>
#include <sys/kmem.h>
#include <sys/uio.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/cred.h>
#include <sys/dirent.h>
#include <sys/pathname.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/modctl.h>
#include <fs/fs_subr.h>
#include <sys/fs/dv_node.h>
#include <sys/fs/snode.h>
#include <sys/sunndi.h>
#include <sys/sunmdi.h>
#include <sys/conf.h>
#ifdef DEBUG
int devfs_debug = 0x0;
#endif
const char dvnm[] = "devfs";
kmem_cache_t *dv_node_cache;
uint_t devfs_clean_key;
struct dv_node *dvroot;
vattr_t dv_vattr_dir = {
AT_TYPE|AT_MODE|AT_UID|AT_GID,
VDIR,
DV_DIRMODE_DEFAULT,
DV_UID_DEFAULT,
DV_GID_DEFAULT,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
};
vattr_t dv_vattr_file = {
AT_TYPE|AT_MODE|AT_SIZE|AT_UID|AT_GID|AT_RDEV,
0,
DV_DEVMODE_DEFAULT,
DV_UID_DEFAULT,
DV_GID_DEFAULT,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
};
vattr_t dv_vattr_priv = {
AT_TYPE|AT_MODE|AT_SIZE|AT_UID|AT_GID|AT_RDEV,
0,
DV_DEVMODE_PRIV,
DV_UID_DEFAULT,
DV_GID_DEFAULT,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
};
extern dev_info_t *clone_dip;
extern major_t clone_major;
extern struct dev_ops *ddi_hold_driver(major_t);
static int
i_dv_node_ctor(void *buf, void *cfarg, int flag)
{
_NOTE(ARGUNUSED(cfarg, flag))
struct dv_node *dv = (struct dv_node *)buf;
struct vnode *vp;
bzero(buf, sizeof (struct dv_node));
vp = dv->dv_vnode = vn_alloc(flag);
if (vp == NULL) {
return (-1);
}
vp->v_data = dv;
rw_init(&dv->dv_contents, NULL, RW_DEFAULT, NULL);
return (0);
}
static void
i_dv_node_dtor(void *buf, void *arg)
{
_NOTE(ARGUNUSED(arg))
struct dv_node *dv = (struct dv_node *)buf;
struct vnode *vp = DVTOV(dv);
rw_destroy(&dv->dv_contents);
vn_invalid(vp);
vn_free(vp);
}
void
dv_node_cache_init()
{
ASSERT(dv_node_cache == NULL);
dv_node_cache = kmem_cache_create("dv_node_cache",
sizeof (struct dv_node), 0, i_dv_node_ctor, i_dv_node_dtor,
NULL, NULL, NULL, 0);
tsd_create(&devfs_clean_key, NULL);
}
void
dv_node_cache_fini()
{
ASSERT(dv_node_cache != NULL);
kmem_cache_destroy(dv_node_cache);
dv_node_cache = NULL;
tsd_destroy(&devfs_clean_key);
}
static ino_t
dv_mkino(dev_info_t *devi, vtype_t typ, dev_t dev)
{
major_t major;
minor_t minor;
ino_t ino;
static int warn;
if (typ == VDIR) {
major = ((L_MAXMAJ32 + 1) >> 1) + DEVI(devi)->devi_major;
minor = ddi_get_instance(devi);
ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32));
major = DEVI(devi)->devi_major;
} else {
major = getmajor(dev);
minor = getminor(dev);
ino = (ino_t)((major << L_BITSMINOR32) | (minor & L_MAXMIN32));
ino <<= 1;
if (typ == VCHR)
ino |= 1;
}
ino += DV_ROOTINO + 1;
if ((major >= (L_MAXMAJ32 >> 1)) && (warn == 0)) {
warn = 1;
cmn_err(CE_WARN, "%s: inode numbers are not unique", dvnm);
}
return (ino);
}
static int
dv_compare_nodes(const struct dv_node *dv1, const struct dv_node *dv2)
{
int rv;
if ((rv = strcmp(dv1->dv_name, dv2->dv_name)) == 0)
return (0);
return ((rv < 0) ? -1 : 1);
}
struct dv_node *
dv_mkroot(struct vfs *vfsp, dev_t devfsdev)
{
struct dv_node *dv;
struct vnode *vp;
ASSERT(ddi_root_node() != NULL);
ASSERT(dv_node_cache != NULL);
dcmn_err3(("dv_mkroot\n"));
dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
vp = DVTOV(dv);
vn_reinit(vp);
vp->v_flag = VROOT;
vp->v_vfsp = vfsp;
vp->v_type = VDIR;
vp->v_rdev = devfsdev;
vn_setops(vp, dv_vnodeops);
vn_exists(vp);
dvroot = dv;
dv->dv_name = NULL;
dv->dv_namelen = 0;
dv->dv_devi = ddi_root_node();
dv->dv_ino = DV_ROOTINO;
dv->dv_nlink = 2;
dv->dv_dotdot = dv;
dv->dv_attrvp = NULLVP;
dv->dv_attr = NULL;
dv->dv_flags = DV_BUILD;
dv->dv_priv = NULL;
dv->dv_busy = 0;
dv->dv_dflt_mode = 0;
avl_create(&dv->dv_entries,
(int (*)(const void *, const void *))dv_compare_nodes,
sizeof (struct dv_node), offsetof(struct dv_node, dv_avllink));
return (dv);
}
struct dv_node *
dv_mkdir(struct dv_node *ddv, dev_info_t *devi, char *nm)
{
struct dv_node *dv;
struct vnode *vp;
size_t nmlen;
ASSERT((devi));
dcmn_err4(("dv_mkdir: %s\n", nm));
dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
nmlen = strlen(nm) + 1;
dv->dv_name = kmem_alloc(nmlen, KM_SLEEP);
bcopy(nm, dv->dv_name, nmlen);
dv->dv_namelen = nmlen - 1;
vp = DVTOV(dv);
vn_reinit(vp);
vp->v_flag = 0;
vp->v_vfsp = DVTOV(ddv)->v_vfsp;
vp->v_type = VDIR;
vp->v_rdev = DVTOV(ddv)->v_rdev;
vn_setops(vp, vn_getops(DVTOV(ddv)));
vn_exists(vp);
dv->dv_devi = devi;
ndi_hold_devi(devi);
dv->dv_ino = dv_mkino(devi, VDIR, NODEV);
dv->dv_nlink = 0;
dv->dv_dotdot = ddv;
dv->dv_attrvp = NULLVP;
dv->dv_attr = NULL;
dv->dv_flags = DV_BUILD;
dv->dv_priv = NULL;
dv->dv_busy = 0;
dv->dv_dflt_mode = 0;
avl_create(&dv->dv_entries,
(int (*)(const void *, const void *))dv_compare_nodes,
sizeof (struct dv_node), offsetof(struct dv_node, dv_avllink));
return (dv);
}
static struct dv_node *
dv_mknod(struct dv_node *ddv, dev_info_t *devi, char *nm,
struct ddi_minor_data *dmd)
{
struct dv_node *dv;
struct vnode *vp;
size_t nmlen;
dcmn_err4(("dv_mknod: %s\n", nm));
dv = kmem_cache_alloc(dv_node_cache, KM_SLEEP);
nmlen = strlen(nm) + 1;
dv->dv_name = kmem_alloc(nmlen, KM_SLEEP);
bcopy(nm, dv->dv_name, nmlen);
dv->dv_namelen = nmlen - 1;
vp = DVTOV(dv);
vn_reinit(vp);
vp->v_flag = 0;
vp->v_vfsp = DVTOV(ddv)->v_vfsp;
vp->v_type = dmd->ddm_spec_type == S_IFCHR ? VCHR : VBLK;
vp->v_rdev = dmd->ddm_dev;
vn_setops(vp, vn_getops(DVTOV(ddv)));
vn_exists(vp);
ASSERT(DEVI_BUSY_OWNED(devi));
mutex_enter(&DEVI(devi)->devi_lock);
dv->dv_devi = devi;
DEVI(devi)->devi_ref++;
mutex_exit(&DEVI(devi)->devi_lock);
dv->dv_ino = dv_mkino(devi, vp->v_type, vp->v_rdev);
dv->dv_nlink = 0;
dv->dv_dotdot = ddv;
dv->dv_attrvp = NULLVP;
dv->dv_attr = NULL;
dv->dv_flags = 0;
if (dmd->type == DDM_INTERNAL_PATH)
dv->dv_flags |= DV_INTERNAL;
if (dmd->ddm_flags & DM_NO_FSPERM)
dv->dv_flags |= DV_NO_FSPERM;
dv->dv_priv = dmd->ddm_node_priv;
if (dv->dv_priv)
dphold(dv->dv_priv);
if (dv->dv_priv || dv->dv_flags & DV_NO_FSPERM) {
dcmn_err5(("%s: dv_mknod default priv mode 0%o\n",
dv->dv_name, dmd->ddm_priv_mode));
dv->dv_flags |= DV_DFLT_MODE;
dv->dv_dflt_mode = dmd->ddm_priv_mode & S_IAMB;
}
return (dv);
}
void
dv_destroy(struct dv_node *dv, uint_t flags)
{
vnode_t *vp = DVTOV(dv);
ASSERT(dv->dv_nlink == 0);
dcmn_err4(("dv_destroy: %s\n", dv->dv_name));
if (vp->v_count != 0) {
ASSERT(vp->v_type == VDIR);
ASSERT(flags & DV_CLEAN_FORCE);
ASSERT(DV_STALE(dv));
return;
}
if (vp->v_type == VDIR) {
ASSERT(DV_FIRST_ENTRY(dv) == NULL);
avl_destroy(&dv->dv_entries);
}
if (dv->dv_attrvp != NULLVP)
VN_RELE(dv->dv_attrvp);
if (dv->dv_attr != NULL)
kmem_free(dv->dv_attr, sizeof (struct vattr));
if (dv->dv_name != NULL)
kmem_free(dv->dv_name, dv->dv_namelen + 1);
if (dv->dv_devi != NULL) {
ndi_rele_devi(dv->dv_devi);
}
if (dv->dv_priv != NULL) {
dpfree(dv->dv_priv);
}
kmem_cache_free(dv_node_cache, dv);
}
static struct dv_node *
dv_findbyname(struct dv_node *ddv, char *nm)
{
struct dv_node *dv;
avl_index_t where;
struct dv_node dvtmp;
ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
dcmn_err3(("dv_findbyname: %s\n", nm));
dvtmp.dv_name = nm;
dv = avl_find(&ddv->dv_entries, &dvtmp, &where);
if (dv) {
ASSERT(dv->dv_dotdot == ddv);
ASSERT(strcmp(dv->dv_name, nm) == 0);
VN_HOLD(DVTOV(dv));
return (dv);
}
return (NULL);
}
void
dv_insert(struct dv_node *ddv, struct dv_node *dv)
{
avl_index_t where;
ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
ASSERT(DVTOV(ddv)->v_type == VDIR);
ASSERT(ddv->dv_nlink >= 2);
ASSERT(dv->dv_nlink == 0);
dcmn_err3(("dv_insert: %s\n", dv->dv_name));
dv->dv_dotdot = ddv;
if (DVTOV(dv)->v_type == VDIR) {
ddv->dv_nlink++;
dv->dv_nlink = 2;
} else {
dv->dv_nlink = 1;
}
VERIFY(avl_find(&ddv->dv_entries, dv, &where) == NULL);
avl_insert(&ddv->dv_entries, dv, where);
}
void
dv_unlink(struct dv_node *ddv, struct dv_node *dv)
{
ASSERT(ddv && dv);
ASSERT(dv->dv_dotdot == ddv);
ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
ASSERT(DVTOV(ddv)->v_type == VDIR);
dcmn_err3(("dv_unlink: %s\n", dv->dv_name));
if (DVTOV(dv)->v_type == VDIR) {
ddv->dv_nlink--;
dv->dv_nlink -= 2;
} else {
dv->dv_nlink -= 1;
}
ASSERT(ddv->dv_nlink >= 2);
ASSERT(dv->dv_nlink == 0);
dv->dv_dotdot = NULL;
avl_remove(&ddv->dv_entries, dv);
}
void
dv_vattr_merge(struct dv_node *dv, struct vattr *vap)
{
struct vnode *vp = DVTOV(dv);
vap->va_nodeid = dv->dv_ino;
vap->va_nlink = dv->dv_nlink;
if (vp->v_type == VDIR) {
vap->va_rdev = 0;
vap->va_fsid = vp->v_rdev;
} else {
vap->va_rdev = vp->v_rdev;
vap->va_fsid = DVTOV(dv->dv_dotdot)->v_rdev;
vap->va_type = vp->v_type;
vap->va_mode &= ~S_IFMT;
if (vap->va_type == VCHR)
vap->va_mode |= S_IFCHR;
else
vap->va_mode |= S_IFBLK;
}
}
void
devfs_get_defattr(struct vnode *vp, struct vattr *vap, int *no_fs_perm)
{
mperm_t mp;
struct dv_node *dv;
if (!vn_matchops(vp, dv_vnodeops)) {
if (no_fs_perm)
*no_fs_perm = 0;
*vap = dv_vattr_file;
return;
}
dv = VTODV(vp);
if (vp->v_type == VDIR) {
*vap = dv_vattr_dir;
} else if (dv->dv_flags & DV_NO_FSPERM) {
if (no_fs_perm)
*no_fs_perm = 1;
*vap = dv_vattr_priv;
} else {
*vap = dv_vattr_file;
if (dev_minorperm(dv->dv_devi, dv->dv_name, &mp) == 0) {
VATTR_MP_MERGE((*vap), mp);
dcmn_err5(("%s: minor perm mode 0%o\n",
dv->dv_name, vap->va_mode));
} else if (dv->dv_flags & DV_DFLT_MODE) {
ASSERT((dv->dv_dflt_mode & ~S_IAMB) == 0);
vap->va_mode &= ~S_IAMB;
vap->va_mode |= dv->dv_dflt_mode;
dcmn_err5(("%s: priv mode 0%o\n",
dv->dv_name, vap->va_mode));
}
}
}
void
dv_shadow_node(
struct vnode *dvp,
char *nm,
struct vnode *vp,
struct pathname *pnp,
struct vnode *rdir,
struct cred *cred,
int flags)
{
struct dv_node *dv;
struct vnode *rdvp;
struct vnode *rvp;
struct vnode *rrvp;
struct vattr vattr;
int create_tried;
int error;
ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
dv = VTODV(vp);
dcmn_err3(("dv_shadow_node: name %s attr %p\n",
nm, (void *)dv->dv_attrvp));
if ((flags & DV_SHADOW_WRITE_HELD) == 0) {
ASSERT(RW_READ_HELD(&dv->dv_contents));
if (dv->dv_attrvp != NULLVP)
return;
if (!rw_tryupgrade(&dv->dv_contents)) {
rw_exit(&dv->dv_contents);
rw_enter(&dv->dv_contents, RW_WRITER);
if (dv->dv_attrvp != NULLVP) {
rw_downgrade(&dv->dv_contents);
return;
}
}
} else {
ASSERT(RW_WRITE_HELD(&dv->dv_contents));
if (dv->dv_attrvp != NULLVP)
return;
}
ASSERT(RW_WRITE_HELD(&dv->dv_contents) && dv->dv_attrvp == NULL);
rdvp = VTODV(dvp)->dv_attrvp;
create_tried = 0;
lookup:
if (rdvp && (dv->dv_flags & DV_NO_FSPERM) == 0) {
error = VOP_LOOKUP(rdvp, nm, &rvp, pnp, LOOKUP_DIR, rdir, cred,
NULL, NULL, NULL);
if ((error == 0) && (VOP_REALVP(rvp, &rrvp, NULL) == 0)) {
VN_HOLD(rrvp);
VN_RELE(rvp);
rvp = rrvp;
}
} else
error = EROFS;
if (error == 0) {
dv->dv_attrvp = rvp;
dv->dv_flags &= ~DV_ACL;
if (fs_acl_nontrivial(rvp, cred))
dv->dv_flags |= DV_ACL;
if (rvp && dv->dv_attr) {
kmem_free(dv->dv_attr, sizeof (struct vattr));
dv->dv_attr = NULL;
}
if ((flags & DV_SHADOW_WRITE_HELD) == 0)
rw_downgrade(&dv->dv_contents);
ASSERT(RW_LOCK_HELD(&dv->dv_contents));
return;
}
devfs_get_defattr(vp, &vattr, NULL);
dv_vattr_merge(dv, &vattr);
gethrestime(&vattr.va_atime);
vattr.va_mtime = vattr.va_atime;
vattr.va_ctime = vattr.va_atime;
if ((error == ENOENT) && !create_tried) {
switch (vp->v_type) {
case VDIR:
error = VOP_MKDIR(rdvp, nm, &vattr, &rvp, kcred,
NULL, 0, NULL);
dsysdebug(error, ("vop_mkdir %s %s %d\n",
VTODV(dvp)->dv_name, nm, error));
create_tried = 1;
break;
case VCHR:
case VBLK:
if (flags & DV_SHADOW_CREATE) {
error = VOP_CREATE(rdvp, nm, &vattr, NONEXCL,
VREAD|VWRITE, &rvp, kcred, 0, NULL, NULL);
dsysdebug(error, ("vop_create %s %s %d\n",
VTODV(dvp)->dv_name, nm, error));
create_tried = 1;
}
break;
default:
cmn_err(CE_PANIC, "devfs: %s: create", dvnm);
}
if (create_tried &&
(error == 0) || (error == EEXIST)) {
VN_RELE(rvp);
goto lookup;
}
}
if (dv->dv_attr == NULL) {
dv->dv_attr = kmem_alloc(sizeof (struct vattr), KM_SLEEP);
*(dv->dv_attr) = vattr;
}
if ((flags & DV_SHADOW_WRITE_HELD) == 0)
rw_downgrade(&dv->dv_contents);
ASSERT(RW_LOCK_HELD(&dv->dv_contents));
}
static int
dv_find_leafnode(dev_info_t *devi, char *minor_nm, struct ddi_minor_data *r_mi)
{
struct ddi_minor_data *dmd;
ASSERT(i_ddi_devi_attached(devi));
dcmn_err3(("dv_find_leafnode: %s\n", minor_nm));
ASSERT(DEVI_BUSY_OWNED(devi));
for (dmd = DEVI(devi)->devi_minor; dmd; dmd = dmd->next) {
if ((dmd->type == DDM_ALIAS) || (dmd->ddm_name == NULL))
continue;
dcmn_err4(("dv_find_leafnode: (%s,%s)\n",
minor_nm, dmd->ddm_name));
if (strcmp(minor_nm, dmd->ddm_name) == 0) {
r_mi->ddm_dev = dmd->ddm_dev;
r_mi->ddm_spec_type = dmd->ddm_spec_type;
r_mi->type = dmd->type;
r_mi->ddm_flags = dmd->ddm_flags;
r_mi->ddm_node_priv = dmd->ddm_node_priv;
r_mi->ddm_priv_mode = dmd->ddm_priv_mode;
if (r_mi->ddm_node_priv)
dphold(r_mi->ddm_node_priv);
return (0);
}
}
dcmn_err3(("dv_find_leafnode: %s: ENOENT\n", minor_nm));
return (ENOENT);
}
static struct dv_node *
dv_clone_mknod(struct dv_node *ddv, char *drvname)
{
major_t major;
struct dv_node *dvp;
char *devnm;
struct ddi_minor_data *dmd;
major = ddi_name_to_major(drvname);
if (major == DDI_MAJOR_T_NONE)
return (NULL);
if (ddi_hold_driver(major) == NULL)
return (NULL);
if (STREAMSTAB(major) == NULL) {
ddi_rele_driver(major);
return (NULL);
}
ddi_rele_driver(major);
devnm = kmem_alloc(MAXNAMELEN, KM_SLEEP);
(void) snprintf(devnm, MAXNAMELEN, "clone@0:%s", drvname);
dmd = kmem_zalloc(sizeof (*dmd), KM_SLEEP);
dmd->ddm_dev = makedevice(clone_major, (minor_t)major);
dmd->ddm_spec_type = S_IFCHR;
dvp = dv_mknod(ddv, clone_dip, devnm, dmd);
kmem_free(dmd, sizeof (*dmd));
kmem_free(devnm, MAXNAMELEN);
return (dvp);
}
int
dv_find(struct dv_node *ddv, char *nm, struct vnode **vpp, struct pathname *pnp,
struct vnode *rdir, struct cred *cred, uint_t ndi_flags)
{
extern int isminiroot;
int rv = 0, was_busy = 0, nmlen, write_held = 0;
struct vnode *vp;
struct dv_node *dv, *dup;
dev_info_t *pdevi, *devi = NULL;
char *mnm;
struct ddi_minor_data *dmd;
dcmn_err3(("dv_find %s\n", nm));
if (!rw_tryenter(&ddv->dv_contents, RW_READER)) {
if (tsd_get(devfs_clean_key))
return (EBUSY);
rw_enter(&ddv->dv_contents, RW_READER);
}
start:
if (DV_STALE(ddv)) {
rw_exit(&ddv->dv_contents);
return (ESTALE);
}
nmlen = strlen(nm);
if ((nmlen == 0) || ((nmlen == 1) && (nm[0] == '.'))) {
*vpp = DVTOV(ddv);
rw_exit(&ddv->dv_contents);
VN_HOLD(*vpp);
return (0);
}
if ((nmlen == 2) && (strcmp(nm, "..") == 0)) {
*vpp = DVTOV(ddv->dv_dotdot);
rw_exit(&ddv->dv_contents);
VN_HOLD(*vpp);
return (0);
}
if (nm[0] == '@' || nm[0] == ':') {
dcmn_err3(("devfs: no driver '%s'\n", nm));
rw_exit(&ddv->dv_contents);
return (ENOENT);
}
if ((dv = dv_findbyname(ddv, nm)) != NULL) {
founddv:
ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
if (!rw_tryenter(&dv->dv_contents, RW_READER)) {
if (tsd_get(devfs_clean_key)) {
VN_RELE(DVTOV(dv));
rw_exit(&ddv->dv_contents);
return (EBUSY);
}
rw_enter(&dv->dv_contents, RW_READER);
}
vp = DVTOV(dv);
if ((dv->dv_attrvp != NULLVP) ||
(vp->v_type != VDIR && dv->dv_attr != NULL)) {
rw_exit(&dv->dv_contents);
rw_exit(&ddv->dv_contents);
goto found;
}
if (tsd_get(devfs_clean_key)) {
if (!rw_tryupgrade(&dv->dv_contents)) {
VN_RELE(DVTOV(dv));
rw_exit(&dv->dv_contents);
rw_exit(&ddv->dv_contents);
return (EBUSY);
}
write_held = DV_SHADOW_WRITE_HELD;
}
dv_shadow_node(DVTOV(ddv), nm, vp, pnp, rdir, cred,
write_held);
rw_exit(&dv->dv_contents);
rw_exit(&ddv->dv_contents);
goto found;
}
if (tsd_get(devfs_clean_key)) {
rw_exit(&ddv->dv_contents);
return (ENOENT);
}
ASSERT(RW_LOCK_HELD(&ddv->dv_contents));
if (rw_read_locked(&ddv->dv_contents) &&
!rw_tryupgrade(&ddv->dv_contents)) {
rw_exit(&ddv->dv_contents);
rw_enter(&ddv->dv_contents, RW_WRITER);
goto start;
}
ddv->dv_busy++;
was_busy++;
rw_exit(&ddv->dv_contents);
pdevi = ddv->dv_devi;
ASSERT(pdevi != NULL);
mnm = strchr(nm, ':');
if (mnm)
*mnm = (char)0;
ASSERT((ndi_flags & NDI_CONFIG) == 0);
rv = ndi_devi_config_one(pdevi, nm, &devi, ndi_flags | NDI_NO_EVENT);
if (mnm)
*mnm = ':';
if (rv != NDI_SUCCESS) {
rv = ENOENT;
goto notfound;
}
ASSERT(devi);
if (ddi_aliases_present == B_TRUE && ddi_get_parent(devi) != pdevi) {
char *curr = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(devi, curr);
vp = NULL;
if (devfs_lookupname(curr, NULL, &vp) == 0 && vp) {
dv = VTODV(vp);
kmem_free(curr, MAXPATHLEN);
goto found;
}
kmem_free(curr, MAXPATHLEN);
}
if (ndi_dev_is_hidden_node(devi)) {
ndi_rele_devi(devi);
rv = ENOENT;
goto notfound;
}
if (isminiroot == 0 && ddi_get_parent(devi) != pdevi) {
ndi_rele_devi(devi);
rv = ENOENT;
goto notfound;
}
ASSERT(devi && i_ddi_devi_attached(devi));
rw_enter(&ddv->dv_contents, RW_WRITER);
ddv->dv_flags |= DV_BUILD;
rw_exit(&ddv->dv_contents);
if (mnm == NULL) {
dv = dv_mkdir(ddv, devi, nm);
} else {
dmd = kmem_zalloc(sizeof (*dmd), KM_SLEEP);
ndi_devi_enter(devi);
if (devi == clone_dip) {
dv = dv_clone_mknod(ddv, mnm + 1);
} else {
if (dv_find_leafnode(devi, mnm + 1, dmd) == 0) {
dv = dv_mknod(ddv, devi, nm, dmd);
if (dmd->ddm_node_priv)
dpfree(dmd->ddm_node_priv);
}
}
ndi_devi_exit(devi);
kmem_free(dmd, sizeof (*dmd));
}
ndi_rele_devi(devi);
if (dv == NULL) {
rv = ENOENT;
goto notfound;
}
rw_enter(&ddv->dv_contents, RW_WRITER);
if ((dup = dv_findbyname(ddv, nm)) == NULL) {
dv_insert(ddv, dv);
} else {
VN_RELE(DVTOV(dv));
dv_destroy(dv, 0);
dv = dup;
}
goto founddv;
found:
if (dv->dv_devi && ndi_dev_is_hidden_node(dv->dv_devi)) {
dcmn_err2(("dv_find: nm %s failed: hidden/removed\n", nm));
VN_RELE(vp);
rv = ENOENT;
goto notfound;
}
if ((dv->dv_flags & DV_INTERNAL) && (cred != kcred)) {
dcmn_err2(("dv_find: nm %s failed: internal\n", nm));
VN_RELE(vp);
rv = ENOENT;
goto notfound;
}
dcmn_err2(("dv_find: returning vp for nm %s\n", nm));
if (vp->v_type == VCHR || vp->v_type == VBLK) {
*vpp = specvp_devfs(vp, vp->v_rdev, vp->v_type, cred,
dv->dv_devi);
VN_RELE(vp);
if (*vpp == NULLVP)
rv = ENOSYS;
} else
*vpp = vp;
notfound:
if (was_busy) {
rw_enter(&ddv->dv_contents, RW_WRITER);
ddv->dv_busy--;
rw_exit(&ddv->dv_contents);
}
return (rv);
}
void
dv_filldir(struct dv_node *ddv)
{
struct dv_node *dv;
dev_info_t *devi, *pdevi;
struct ddi_minor_data *dmd;
char devnm[MAXNAMELEN];
ASSERT(DVTOV(ddv)->v_type == VDIR);
ASSERT(RW_WRITE_HELD(&ddv->dv_contents));
ASSERT(ddv->dv_flags & DV_BUILD);
dcmn_err3(("dv_filldir: %s\n", ddv->dv_name));
if (DV_STALE(ddv))
return;
pdevi = ddv->dv_devi;
if (ndi_devi_config(pdevi, NDI_NO_EVENT) != NDI_SUCCESS) {
dcmn_err3(("dv_filldir: config error %s\n", ddv->dv_name));
}
ndi_devi_enter(pdevi);
for (devi = ddi_get_child(pdevi); devi;
devi = ddi_get_next_sibling(devi)) {
if (i_ddi_node_state(devi) < DS_ATTACHED)
continue;
if (ndi_dev_is_hidden_node(devi))
continue;
dcmn_err3(("dv_filldir: node %s\n", ddi_node_name(devi)));
ndi_devi_enter(devi);
for (dmd = DEVI(devi)->devi_minor; dmd; dmd = dmd->next) {
char *addr;
if ((dmd->type == DDM_ALIAS) ||
(dmd->type == DDM_INTERNAL_PATH) ||
(dmd->ddm_name == NULL))
continue;
addr = ddi_get_name_addr(devi);
if (addr && *addr)
(void) sprintf(devnm, "%s@%s:%s",
ddi_node_name(devi), addr, dmd->ddm_name);
else
(void) sprintf(devnm, "%s:%s",
ddi_node_name(devi), dmd->ddm_name);
if ((dv = dv_findbyname(ddv, devnm)) != NULL) {
VN_RELE(DVTOV(dv));
continue;
}
dv = dv_mknod(ddv, devi, devnm, dmd);
dv_insert(ddv, dv);
VN_RELE(DVTOV(dv));
}
ndi_devi_exit(devi);
(void) ddi_deviname(devi, devnm);
if ((dv = dv_findbyname(ddv, devnm + 1)) == NULL) {
dv = dv_mkdir(ddv, devi, devnm + 1);
dv_insert(ddv, dv);
}
VN_RELE(DVTOV(dv));
}
ndi_devi_exit(pdevi);
ddv->dv_flags &= ~DV_BUILD;
}
int
dv_cleandir(struct dv_node *ddv, char *devnm, uint_t flags)
{
struct dv_node *dv;
struct dv_node *next;
struct vnode *vp;
int busy = 0;
ASSERT(tsd_get(devfs_clean_key));
dcmn_err3(("dv_cleandir: %s\n", ddv->dv_name));
if (!(flags & DV_CLEANDIR_LCK) &&
!rw_tryenter(&ddv->dv_contents, RW_WRITER))
return (EBUSY);
for (dv = DV_FIRST_ENTRY(ddv); dv; dv = next) {
next = DV_NEXT_ENTRY(ddv, dv);
if (devnm &&
(strncmp(devnm, dv->dv_name, strlen(devnm)) ||
(dv->dv_name[strlen(devnm)] != ':' &&
dv->dv_name[strlen(devnm)] != '\0')))
continue;
vp = DVTOV(dv);
if (vp->v_type == VDIR) {
rw_enter(&dv->dv_contents, RW_WRITER);
if (dv_cleandir(dv, NULL,
flags | DV_CLEANDIR_LCK) == EBUSY) {
rw_exit(&dv->dv_contents);
goto set_busy;
}
ASSERT(dv->dv_nlink == 2);
mutex_enter(&vp->v_lock);
if (vp->v_count > 0) {
if (dv->dv_busy || !(flags & DV_CLEAN_FORCE)) {
mutex_exit(&vp->v_lock);
rw_exit(&dv->dv_contents);
goto set_busy;
}
ASSERT(!DV_STALE(dv));
ndi_rele_devi(dv->dv_devi);
dv->dv_devi = NULL;
}
} else {
ASSERT((vp->v_type == VCHR) || (vp->v_type == VBLK));
ASSERT(dv->dv_nlink == 1);
mutex_enter(&vp->v_lock);
if (vp->v_count > 0) {
mutex_exit(&vp->v_lock);
goto set_busy;
}
}
dv_unlink(ddv, dv);
mutex_exit(&vp->v_lock);
if (vp->v_type == VDIR)
rw_exit(&dv->dv_contents);
if (vp->v_count == 0)
dv_destroy(dv, flags);
continue;
set_busy: busy++;
if (devnm)
break;
}
ddv->dv_flags |= DV_BUILD;
if (!(flags & DV_CLEANDIR_LCK))
rw_exit(&ddv->dv_contents);
return (busy ? EBUSY : 0);
}
static int
dv_reset_perm_dir(struct dv_node *ddv, uint_t flags)
{
struct dv_node *dv;
struct vnode *vp;
int retval = 0;
struct vattr *attrp;
mperm_t mp;
char *nm;
uid_t old_uid;
gid_t old_gid;
mode_t old_mode;
rw_enter(&ddv->dv_contents, RW_WRITER);
for (dv = DV_FIRST_ENTRY(ddv); dv; dv = DV_NEXT_ENTRY(ddv, dv)) {
int error = 0;
nm = dv->dv_name;
rw_enter(&dv->dv_contents, RW_READER);
vp = DVTOV(dv);
if (vp->v_type == VDIR) {
rw_exit(&dv->dv_contents);
if (dv_reset_perm_dir(dv, flags) != 0) {
error = EBUSY;
}
} else {
ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
rw_exit(&dv->dv_contents);
if (dev_minorperm(dv->dv_devi, nm, &mp) != 0)
continue;
rw_enter(&dv->dv_contents, RW_READER);
if (dv->dv_attrvp == NULLVP) {
dv_shadow_node(DVTOV(ddv), nm, vp,
NULL, NULLVP, kcred, 0);
}
if (dv->dv_attrvp != NULLVP || dv->dv_attr == NULL) {
rw_exit(&dv->dv_contents);
continue;
}
attrp = dv->dv_attr;
if (VATTRP_MP_CMP(attrp, mp) == 0) {
dcmn_err5(("%s: no perm change: "
"%d %d 0%o\n", nm, attrp->va_uid,
attrp->va_gid, attrp->va_mode));
rw_exit(&dv->dv_contents);
continue;
}
old_uid = attrp->va_uid;
old_gid = attrp->va_gid;
old_mode = attrp->va_mode;
VATTRP_MP_MERGE(attrp, mp);
mutex_enter(&vp->v_lock);
if (vp->v_count > 0) {
error = EBUSY;
}
mutex_exit(&vp->v_lock);
dcmn_err5(("%s: perm %d/%d/0%o -> %d/%d/0%o (%d)\n",
nm, old_uid, old_gid, old_mode, attrp->va_uid,
attrp->va_gid, attrp->va_mode, error));
rw_exit(&dv->dv_contents);
}
if (error != 0) {
retval = error;
}
}
ddv->dv_flags |= DV_BUILD;
rw_exit(&ddv->dv_contents);
return (retval);
}
int
devfs_reset_perm(uint_t flags)
{
struct dv_node *dvp;
int rval;
if ((dvp = devfs_dip_to_dvnode(ddi_root_node())) == NULL)
return (0);
VN_HOLD(DVTOV(dvp));
rval = dv_reset_perm_dir(dvp, flags);
VN_RELE(DVTOV(dvp));
return (rval);
}
static int
devfs_remdrv_rmdir(vnode_t *dirvp, const char *dir, vnode_t *rvp)
{
int error;
vnode_t *vp;
int eof;
struct iovec iov;
struct uio uio;
struct dirent64 *dp;
dirent64_t *dbuf;
size_t dlen;
size_t dbuflen;
int ndirents = 64;
char *nm;
VN_HOLD(dirvp);
dlen = ndirents * (sizeof (*dbuf));
dbuf = kmem_alloc(dlen, KM_SLEEP);
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
uio.uio_segflg = UIO_SYSSPACE;
uio.uio_fmode = 0;
uio.uio_extflg = UIO_COPY_CACHED;
uio.uio_loffset = 0;
uio.uio_llimit = MAXOFFSET_T;
eof = 0;
error = 0;
while (!error && !eof) {
uio.uio_resid = dlen;
iov.iov_base = (char *)dbuf;
iov.iov_len = dlen;
(void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
error = VOP_READDIR(dirvp, &uio, kcred, &eof, NULL, 0);
VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
dbuflen = dlen - uio.uio_resid;
if (error || dbuflen == 0)
break;
for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
nm = dp->d_name;
if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
continue;
error = VOP_LOOKUP(dirvp, nm,
&vp, NULL, 0, NULL, kcred, NULL, NULL, NULL);
dsysdebug(error,
("rem_drv %s/%s lookup (%d)\n",
dir, nm, error));
if (error)
continue;
ASSERT(vp->v_type == VDIR ||
vp->v_type == VCHR || vp->v_type == VBLK);
if (vp->v_type == VDIR) {
error = devfs_remdrv_rmdir(vp, nm, rvp);
if (error == 0) {
error = VOP_RMDIR(dirvp,
(char *)nm, rvp, kcred, NULL, 0);
dsysdebug(error,
("rem_drv %s/%s rmdir (%d)\n",
dir, nm, error));
}
} else {
error = VOP_REMOVE(dirvp, (char *)nm, kcred,
NULL, 0);
dsysdebug(error,
("rem_drv %s/%s remove (%d)\n",
dir, nm, error));
}
VN_RELE(vp);
if (error) {
goto exit;
}
}
}
exit:
VN_RELE(dirvp);
kmem_free(dbuf, dlen);
return (error);
}
int
devfs_remdrv_cleanup(const char *dir, const char *nodename)
{
int error;
vnode_t *vp;
vnode_t *dirvp;
int eof;
struct iovec iov;
struct uio uio;
struct dirent64 *dp;
dirent64_t *dbuf;
size_t dlen;
size_t dbuflen;
int ndirents = 64;
int nodenamelen = strlen(nodename);
char *nm;
struct pathname pn;
vnode_t *rvp;
dcmn_err5(("devfs_remdrv_cleanup: %s %s\n", dir, nodename));
if (error = pn_get((char *)dir, UIO_SYSSPACE, &pn))
return (0);
rvp = dvroot->dv_attrvp;
ASSERT(rvp != NULL);
VN_HOLD(rvp);
pn_skipslash(&pn);
dirvp = rvp;
VN_HOLD(dirvp);
nm = kmem_alloc(MAXNAMELEN, KM_SLEEP);
while (pn_pathleft(&pn)) {
ASSERT(dirvp->v_type == VDIR);
(void) pn_getcomponent(&pn, nm);
ASSERT((strcmp(nm, ".") != 0) && (strcmp(nm, "..") != 0));
error = VOP_LOOKUP(dirvp, nm, &vp, NULL, 0, rvp, kcred,
NULL, NULL, NULL);
if (error) {
dcmn_err5(("remdrv_cleanup %s lookup error %d\n",
nm, error));
VN_RELE(dirvp);
if (dirvp != rvp)
VN_RELE(rvp);
pn_free(&pn);
kmem_free(nm, MAXNAMELEN);
return (0);
}
VN_RELE(dirvp);
dirvp = vp;
pn_skipslash(&pn);
}
ASSERT(dirvp->v_type == VDIR);
if (dirvp != rvp)
VN_RELE(rvp);
pn_free(&pn);
kmem_free(nm, MAXNAMELEN);
dlen = ndirents * (sizeof (*dbuf));
dbuf = kmem_alloc(dlen, KM_SLEEP);
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;
uio.uio_segflg = UIO_SYSSPACE;
uio.uio_fmode = 0;
uio.uio_extflg = UIO_COPY_CACHED;
uio.uio_loffset = 0;
uio.uio_llimit = MAXOFFSET_T;
eof = 0;
error = 0;
while (!error && !eof) {
uio.uio_resid = dlen;
iov.iov_base = (char *)dbuf;
iov.iov_len = dlen;
(void) VOP_RWLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
error = VOP_READDIR(dirvp, &uio, kcred, &eof, NULL, 0);
VOP_RWUNLOCK(dirvp, V_WRITELOCK_FALSE, NULL);
dbuflen = dlen - uio.uio_resid;
if (error || dbuflen == 0)
break;
for (dp = dbuf; ((intptr_t)dp < (intptr_t)dbuf + dbuflen);
dp = (dirent64_t *)((intptr_t)dp + dp->d_reclen)) {
nm = dp->d_name;
if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
continue;
if (strncmp(nm, nodename, nodenamelen) != 0)
continue;
error = VOP_LOOKUP(dirvp, nm, &vp,
NULL, 0, NULL, kcred, NULL, NULL, NULL);
dsysdebug(error,
("rem_drv %s/%s lookup (%d)\n",
dir, nm, error));
if (error)
continue;
ASSERT(vp->v_type == VDIR ||
vp->v_type == VCHR || vp->v_type == VBLK);
if (vp->v_type == VDIR) {
error = devfs_remdrv_rmdir(vp, nm, rvp);
if (error == 0) {
error = VOP_RMDIR(dirvp, (char *)nm,
rvp, kcred, NULL, 0);
dsysdebug(error,
("rem_drv %s/%s rmdir (%d)\n",
dir, nm, error));
}
} else {
error = VOP_REMOVE(dirvp, (char *)nm, kcred,
NULL, 0);
dsysdebug(error,
("rem_drv %s/%s remove (%d)\n",
dir, nm, error));
}
VN_RELE(vp);
if (error)
goto exit;
}
}
exit:
VN_RELE(dirvp);
kmem_free(dbuf, dlen);
return (0);
}
struct dv_list {
struct dv_node *dv;
struct dv_list *next;
};
void
dv_walk(
struct dv_node *ddv,
char *devnm,
void (*callback)(struct dv_node *, void *),
void *arg)
{
struct vnode *dvp;
struct dv_node *dv;
struct dv_list *head, *tail, *next;
int len;
dcmn_err3(("dv_walk: ddv = %s, devnm = %s\n",
ddv->dv_name, devnm ? devnm : "<null>"));
dvp = DVTOV(ddv);
ASSERT(dvp->v_type == VDIR);
head = tail = next = NULL;
rw_enter(&ddv->dv_contents, RW_READER);
mutex_enter(&dvp->v_lock);
for (dv = DV_FIRST_ENTRY(ddv); dv; dv = DV_NEXT_ENTRY(ddv, dv)) {
if (devnm && (len = strlen(devnm)) &&
(strncmp(devnm, dv->dv_name, len) ||
(dv->dv_name[len] != ':' && dv->dv_name[len] != '\0')))
continue;
callback(dv, arg);
if (DVTOV(dv)->v_type != VDIR)
continue;
next = kmem_zalloc(sizeof (*next), KM_SLEEP);
next->dv = dv;
if (tail)
tail->next = next;
else
head = next;
tail = next;
}
while (head) {
dv_walk(head->dv, NULL, callback, arg);
next = head->next;
kmem_free(head, sizeof (*head));
head = next;
}
rw_exit(&ddv->dv_contents);
mutex_exit(&dvp->v_lock);
}