xref: /onnv-gate/usr/src/uts/common/fs/dev/sdev_vnops.c (revision 10588:dc03f981ea18)
12621Sllai1 /*
22621Sllai1  * CDDL HEADER START
32621Sllai1  *
42621Sllai1  * The contents of this file are subject to the terms of the
52621Sllai1  * Common Development and Distribution License (the "License").
62621Sllai1  * You may not use this file except in compliance with the License.
72621Sllai1  *
82621Sllai1  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92621Sllai1  * or http://www.opensolaris.org/os/licensing.
102621Sllai1  * See the License for the specific language governing permissions
112621Sllai1  * and limitations under the License.
122621Sllai1  *
132621Sllai1  * When distributing Covered Code, include this CDDL HEADER in each
142621Sllai1  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152621Sllai1  * If applicable, add the following below this CDDL HEADER, with the
162621Sllai1  * fields enclosed by brackets "[]" replaced with your own identifying
172621Sllai1  * information: Portions Copyright [yyyy] [name of copyright owner]
182621Sllai1  *
192621Sllai1  * CDDL HEADER END
202621Sllai1  */
212621Sllai1 /*
229198SJerry.Gilliam@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
232621Sllai1  * Use is subject to license terms.
242621Sllai1  */
252621Sllai1 
262621Sllai1 /*
272621Sllai1  * vnode ops for the /dev filesystem
282621Sllai1  *
292621Sllai1  * - VDIR, VCHR, CBLK, and VLNK are considered must supported files
302621Sllai1  * - VREG and VDOOR are used for some internal implementations in
312621Sllai1  *    the global zone, e.g. devname and devfsadm communication
322621Sllai1  * - other file types are unusual in this namespace and
332621Sllai1  *    not supported for now
342621Sllai1  */
352621Sllai1 
362621Sllai1 #include <sys/types.h>
372621Sllai1 #include <sys/param.h>
382621Sllai1 #include <sys/t_lock.h>
392621Sllai1 #include <sys/systm.h>
402621Sllai1 #include <sys/sysmacros.h>
412621Sllai1 #include <sys/user.h>
422621Sllai1 #include <sys/time.h>
432621Sllai1 #include <sys/vfs.h>
442621Sllai1 #include <sys/vnode.h>
453898Srsb #include <sys/vfs_opreg.h>
462621Sllai1 #include <sys/file.h>
472621Sllai1 #include <sys/fcntl.h>
482621Sllai1 #include <sys/flock.h>
492621Sllai1 #include <sys/kmem.h>
502621Sllai1 #include <sys/uio.h>
512621Sllai1 #include <sys/errno.h>
522621Sllai1 #include <sys/stat.h>
532621Sllai1 #include <sys/cred.h>
542621Sllai1 #include <sys/cred_impl.h>
552621Sllai1 #include <sys/dirent.h>
562621Sllai1 #include <sys/pathname.h>
572621Sllai1 #include <sys/cmn_err.h>
582621Sllai1 #include <sys/debug.h>
592621Sllai1 #include <sys/policy.h>
602621Sllai1 #include <vm/hat.h>
612621Sllai1 #include <vm/seg_vn.h>
622621Sllai1 #include <vm/seg_map.h>
632621Sllai1 #include <vm/seg.h>
642621Sllai1 #include <vm/as.h>
652621Sllai1 #include <vm/page.h>
662621Sllai1 #include <sys/proc.h>
672621Sllai1 #include <sys/mode.h>
682621Sllai1 #include <sys/sunndi.h>
692621Sllai1 #include <sys/ptms.h>
702621Sllai1 #include <fs/fs_subr.h>
712621Sllai1 #include <sys/fs/dv_node.h>
722621Sllai1 #include <sys/fs/sdev_impl.h>
732621Sllai1 
742621Sllai1 /*ARGSUSED*/
752621Sllai1 static int
765331Samw sdev_open(struct vnode **vpp, int flag, struct cred *cred, caller_context_t *ct)
772621Sllai1 {
782621Sllai1 	struct sdev_node *dv = VTOSDEV(*vpp);
792621Sllai1 	struct sdev_node *ddv = dv->sdev_dotdot;
802621Sllai1 	int error = 0;
812621Sllai1 
822621Sllai1 	if ((*vpp)->v_type == VDIR)
832621Sllai1 		return (0);
842621Sllai1 
852621Sllai1 	if (!SDEV_IS_GLOBAL(dv))
862621Sllai1 		return (ENOTSUP);
872621Sllai1 
88*10588SEric.Taylor@Sun.COM 	if ((*vpp)->v_type == VLNK)
89*10588SEric.Taylor@Sun.COM 		return (ENOENT);
902621Sllai1 	ASSERT((*vpp)->v_type == VREG);
912621Sllai1 	if ((*vpp)->v_type != VREG)
922621Sllai1 		return (ENOTSUP);
932621Sllai1 
942621Sllai1 	ASSERT(ddv);
952621Sllai1 	rw_enter(&ddv->sdev_contents, RW_READER);
962621Sllai1 	if (dv->sdev_attrvp == NULL) {
972621Sllai1 		rw_exit(&ddv->sdev_contents);
982621Sllai1 		return (ENOENT);
992621Sllai1 	}
1005331Samw 	error = VOP_OPEN(&(dv->sdev_attrvp), flag, cred, ct);
1012621Sllai1 	rw_exit(&ddv->sdev_contents);
1022621Sllai1 	return (error);
1032621Sllai1 }
1042621Sllai1 
1052621Sllai1 /*ARGSUSED1*/
1062621Sllai1 static int
1072621Sllai1 sdev_close(struct vnode *vp, int flag, int count,
1085331Samw     offset_t offset, struct cred *cred, caller_context_t *ct)
1092621Sllai1 {
1102621Sllai1 	struct sdev_node *dv = VTOSDEV(vp);
1112621Sllai1 
1122621Sllai1 	if (vp->v_type == VDIR) {
1132621Sllai1 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
1142621Sllai1 		cleanshares(vp, ttoproc(curthread)->p_pid);
1152621Sllai1 		return (0);
1162621Sllai1 	}
1172621Sllai1 
1182621Sllai1 	if (!SDEV_IS_GLOBAL(dv))
1192621Sllai1 		return (ENOTSUP);
1202621Sllai1 
1212621Sllai1 	ASSERT(vp->v_type == VREG);
1222621Sllai1 	if (vp->v_type != VREG)
1232621Sllai1 		return (ENOTSUP);
1242621Sllai1 
1252621Sllai1 	ASSERT(dv->sdev_attrvp);
1265331Samw 	return (VOP_CLOSE(dv->sdev_attrvp, flag, count, offset, cred, ct));
1272621Sllai1 }
1282621Sllai1 
1292621Sllai1 /*ARGSUSED*/
1302621Sllai1 static int
1312621Sllai1 sdev_read(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred,
1322621Sllai1 	struct caller_context *ct)
1332621Sllai1 {
1342621Sllai1 	struct sdev_node *dv = (struct sdev_node *)VTOSDEV(vp);
1352621Sllai1 	int	error;
1362621Sllai1 
1372621Sllai1 	if (!SDEV_IS_GLOBAL(dv))
1382621Sllai1 		return (EINVAL);
1392621Sllai1 
1402621Sllai1 	if (vp->v_type == VDIR)
1412621Sllai1 		return (EISDIR);
1422621Sllai1 
1432621Sllai1 	/* only supporting regular files in /dev */
1442621Sllai1 	ASSERT(vp->v_type == VREG);
1452621Sllai1 	if (vp->v_type != VREG)
1462621Sllai1 		return (EINVAL);
1472621Sllai1 
1482621Sllai1 	ASSERT(RW_READ_HELD(&VTOSDEV(vp)->sdev_contents));
1492621Sllai1 	ASSERT(dv->sdev_attrvp);
1505331Samw 	(void) VOP_RWLOCK(dv->sdev_attrvp, 0, ct);
1512621Sllai1 	error = VOP_READ(dv->sdev_attrvp, uio, ioflag, cred, ct);
1525331Samw 	VOP_RWUNLOCK(dv->sdev_attrvp, 0, ct);
1532621Sllai1 	return (error);
1542621Sllai1 }
1552621Sllai1 
1562621Sllai1 /*ARGSUSED*/
1572621Sllai1 static int
1582621Sllai1 sdev_write(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred,
1592621Sllai1 	struct caller_context *ct)
1602621Sllai1 {
1612621Sllai1 	struct sdev_node *dv = VTOSDEV(vp);
1622621Sllai1 	int	error = 0;
1632621Sllai1 
1642621Sllai1 	if (!SDEV_IS_GLOBAL(dv))
1652621Sllai1 		return (EINVAL);
1662621Sllai1 
1672621Sllai1 	if (vp->v_type == VDIR)
1682621Sllai1 		return (EISDIR);
1692621Sllai1 
1702621Sllai1 	/* only supporting regular files in /dev */
1712621Sllai1 	ASSERT(vp->v_type == VREG);
1722621Sllai1 	if (vp->v_type != VREG)
1732621Sllai1 		return (EINVAL);
1742621Sllai1 
1752621Sllai1 	ASSERT(dv->sdev_attrvp);
1762621Sllai1 
1775331Samw 	(void) VOP_RWLOCK(dv->sdev_attrvp, 1, ct);
1782621Sllai1 	error = VOP_WRITE(dv->sdev_attrvp, uio, ioflag, cred, ct);
1795331Samw 	VOP_RWUNLOCK(dv->sdev_attrvp, 1, ct);
1802621Sllai1 	if (error == 0) {
1812621Sllai1 		sdev_update_timestamps(dv->sdev_attrvp, kcred,
1822621Sllai1 		    AT_MTIME);
1832621Sllai1 	}
1842621Sllai1 	return (error);
1852621Sllai1 }
1862621Sllai1 
1872621Sllai1 /*ARGSUSED*/
1882621Sllai1 static int
1892621Sllai1 sdev_ioctl(struct vnode *vp, int cmd, intptr_t arg, int flag,
1905331Samw     struct cred *cred, int *rvalp,  caller_context_t *ct)
1912621Sllai1 {
1922621Sllai1 	struct sdev_node *dv = VTOSDEV(vp);
1932621Sllai1 
1942621Sllai1 	if (!SDEV_IS_GLOBAL(dv) || (vp->v_type == VDIR))
1952621Sllai1 		return (ENOTTY);
1962621Sllai1 
1972621Sllai1 	ASSERT(vp->v_type == VREG);
1982621Sllai1 	if (vp->v_type != VREG)
1992621Sllai1 		return (EINVAL);
2002621Sllai1 
2012621Sllai1 	ASSERT(dv->sdev_attrvp);
2025331Samw 	return (VOP_IOCTL(dv->sdev_attrvp, cmd, arg, flag, cred, rvalp, ct));
2032621Sllai1 }
2042621Sllai1 
2052621Sllai1 static int
2065331Samw sdev_getattr(struct vnode *vp, struct vattr *vap, int flags,
2075331Samw     struct cred *cr, caller_context_t *ct)
2082621Sllai1 {
2092621Sllai1 	int			error = 0;
2102621Sllai1 	struct sdev_node	*dv = VTOSDEV(vp);
2112621Sllai1 	struct sdev_node	*parent = dv->sdev_dotdot;
2122621Sllai1 
2132621Sllai1 	ASSERT(parent);
2142621Sllai1 
2152621Sllai1 	rw_enter(&parent->sdev_contents, RW_READER);
2162621Sllai1 	ASSERT(dv->sdev_attr || dv->sdev_attrvp);
2172621Sllai1 
2182621Sllai1 	/*
2192621Sllai1 	 * search order:
2202621Sllai1 	 * 	- for persistent nodes (SDEV_PERSIST): backstore
2212621Sllai1 	 *	- for non-persistent nodes: module ops if global, then memory
2222621Sllai1 	 */
2232621Sllai1 	if (dv->sdev_attrvp) {
2242621Sllai1 		rw_exit(&parent->sdev_contents);
2255331Samw 		error = VOP_GETATTR(dv->sdev_attrvp, vap, flags, cr, ct);
2262621Sllai1 		sdev_vattr_merge(dv, vap);
2272621Sllai1 	} else {
2282621Sllai1 		ASSERT(dv->sdev_attr);
2292621Sllai1 		*vap = *dv->sdev_attr;
2302621Sllai1 		sdev_vattr_merge(dv, vap);
2312621Sllai1 		rw_exit(&parent->sdev_contents);
2322621Sllai1 	}
2332621Sllai1 
2342621Sllai1 	return (error);
2352621Sllai1 }
2362621Sllai1 
2373748Sjg /*ARGSUSED4*/
2382621Sllai1 static int
2393748Sjg sdev_setattr(struct vnode *vp, struct vattr *vap, int flags,
2403748Sjg     struct cred *cred, caller_context_t *ctp)
2412621Sllai1 {
2422621Sllai1 	return (devname_setattr_func(vp, vap, flags, cred, NULL, 0));
2432621Sllai1 }
2442621Sllai1 
2452621Sllai1 static int
2462621Sllai1 sdev_getsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
2475331Samw     struct cred *cr, caller_context_t *ct)
2482621Sllai1 {
2492621Sllai1 	int	error;
2502621Sllai1 	struct sdev_node *dv = VTOSDEV(vp);
2512621Sllai1 	struct vnode *avp = dv->sdev_attrvp;
2522621Sllai1 
2532621Sllai1 	if (avp == NULL) {
2542621Sllai1 		/* return fs_fab_acl() if flavor matches, else do nothing */
2552621Sllai1 		if ((SDEV_ACL_FLAVOR(vp) == _ACL_ACLENT_ENABLED &&
2562621Sllai1 		    (vsap->vsa_mask & (VSA_ACLCNT | VSA_DFACLCNT))) ||
2572621Sllai1 		    (SDEV_ACL_FLAVOR(vp) == _ACL_ACE_ENABLED &&
2582621Sllai1 		    (vsap->vsa_mask & (VSA_ACECNT | VSA_ACE))))
2595331Samw 			return (fs_fab_acl(vp, vsap, flags, cr, ct));
2602621Sllai1 
2612621Sllai1 		return (ENOSYS);
2622621Sllai1 	}
2632621Sllai1 
2645331Samw 	(void) VOP_RWLOCK(avp, 1, ct);
2655331Samw 	error = VOP_GETSECATTR(avp, vsap, flags, cr, ct);
2665331Samw 	VOP_RWUNLOCK(avp, 1, ct);
2672621Sllai1 	return (error);
2682621Sllai1 }
2692621Sllai1 
2702621Sllai1 static int
2712621Sllai1 sdev_setsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
2725331Samw     struct cred *cr, caller_context_t *ct)
2732621Sllai1 {
2742621Sllai1 	int	error;
2752621Sllai1 	struct sdev_node *dv = VTOSDEV(vp);
2762621Sllai1 	struct vnode *avp = dv->sdev_attrvp;
2772621Sllai1 
2782621Sllai1 	if (dv->sdev_state == SDEV_ZOMBIE)
2792621Sllai1 		return (0);
2802621Sllai1 
2812621Sllai1 	if (avp == NULL) {
2822621Sllai1 		if (SDEV_IS_GLOBAL(dv) && !SDEV_IS_PERSIST(dv))
2832621Sllai1 			return (fs_nosys());
2842621Sllai1 		ASSERT(dv->sdev_attr);
2852621Sllai1 		/*
2862621Sllai1 		 * if coming in directly, the acl system call will
2872621Sllai1 		 * have held the read-write lock via VOP_RWLOCK()
2882621Sllai1 		 * If coming in via specfs, specfs will have
2892621Sllai1 		 * held the rw lock on the realvp i.e. us.
2902621Sllai1 		 */
2912621Sllai1 		ASSERT(RW_WRITE_HELD(&dv->sdev_contents));
2922621Sllai1 		sdev_vattr_merge(dv, dv->sdev_attr);
2935331Samw 		error = sdev_shadow_node(dv, cr);
2942621Sllai1 		if (error) {
2952621Sllai1 			return (fs_nosys());
2962621Sllai1 		}
2972621Sllai1 
2982621Sllai1 		ASSERT(dv->sdev_attrvp);
2992621Sllai1 		/* clean out the memory copy if any */
3002621Sllai1 		if (dv->sdev_attr) {
3012621Sllai1 			kmem_free(dv->sdev_attr, sizeof (struct vattr));
3022621Sllai1 			dv->sdev_attr = NULL;
3032621Sllai1 		}
3042621Sllai1 		avp = dv->sdev_attrvp;
3052621Sllai1 	}
3062621Sllai1 	ASSERT(avp);
3072621Sllai1 
3085331Samw 	(void) VOP_RWLOCK(avp, V_WRITELOCK_TRUE, ct);
3095331Samw 	error = VOP_SETSECATTR(avp, vsap, flags, cr, ct);
3105331Samw 	VOP_RWUNLOCK(avp, V_WRITELOCK_TRUE, ct);
3112621Sllai1 	return (error);
3122621Sllai1 }
3132621Sllai1 
3142621Sllai1 int
3152621Sllai1 sdev_unlocked_access(void *vdv, int mode, struct cred *cr)
3162621Sllai1 {
3172621Sllai1 	struct sdev_node	*dv = vdv;
3182621Sllai1 	int			shift = 0;
3192621Sllai1 	uid_t			owner = dv->sdev_attr->va_uid;
3202621Sllai1 
3212621Sllai1 	if (crgetuid(cr) != owner) {
3222621Sllai1 		shift += 3;
3232621Sllai1 		if (groupmember(dv->sdev_attr->va_gid, cr) == 0)
3242621Sllai1 			shift += 3;
3252621Sllai1 	}
3262621Sllai1 
3272621Sllai1 	mode &= ~(dv->sdev_attr->va_mode << shift);
3282621Sllai1 	if (mode == 0)
3292621Sllai1 		return (0);
3302621Sllai1 
3312621Sllai1 	return (secpolicy_vnode_access(cr, SDEVTOV(dv), owner, mode));
3322621Sllai1 }
3332621Sllai1 
3342621Sllai1 static int
3355331Samw sdev_access(struct vnode *vp, int mode, int flags, struct cred *cr,
3365331Samw     caller_context_t *ct)
3372621Sllai1 {
3382621Sllai1 	struct sdev_node	*dv = VTOSDEV(vp);
3392621Sllai1 	int ret = 0;
3402621Sllai1 
3412621Sllai1 	ASSERT(dv->sdev_attr || dv->sdev_attrvp);
3422621Sllai1 
3432621Sllai1 	if (dv->sdev_attrvp) {
3445331Samw 		ret = VOP_ACCESS(dv->sdev_attrvp, mode, flags, cr, ct);
3452621Sllai1 	} else if (dv->sdev_attr) {
3462621Sllai1 		rw_enter(&dv->sdev_contents, RW_READER);
3472621Sllai1 		ret = sdev_unlocked_access(dv, mode, cr);
3482621Sllai1 		if (ret)
3492621Sllai1 			ret = EACCES;
3502621Sllai1 		rw_exit(&dv->sdev_contents);
3512621Sllai1 	}
3522621Sllai1 
3532621Sllai1 	return (ret);
3542621Sllai1 }
3552621Sllai1 
3562621Sllai1 /*
3572621Sllai1  * Lookup
3582621Sllai1  */
3592621Sllai1 /*ARGSUSED3*/
3602621Sllai1 static int
3612621Sllai1 sdev_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
3625331Samw     struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred,
3635331Samw     caller_context_t *ct, int *direntflags, pathname_t *realpnp)
3642621Sllai1 {
3653843Sjg 	struct sdev_node *parent;
3663843Sjg 	int error;
3672621Sllai1 
3682621Sllai1 	parent = VTOSDEV(dvp);
3692621Sllai1 	ASSERT(parent);
3702621Sllai1 
3713843Sjg 	/* execute access is required to search the directory */
3725331Samw 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0)
3733843Sjg 		return (error);
3743843Sjg 
3752621Sllai1 	if (!SDEV_IS_GLOBAL(parent))
3762621Sllai1 		return (prof_lookup(dvp, nm, vpp, cred));
3772621Sllai1 	return (devname_lookup_func(parent, nm, vpp, cred, NULL, 0));
3782621Sllai1 }
3792621Sllai1 
3802621Sllai1 /*ARGSUSED2*/
3812621Sllai1 static int
3822621Sllai1 sdev_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl,
3835331Samw     int mode, struct vnode **vpp, struct cred *cred, int flag,
3845331Samw     caller_context_t *ct, vsecattr_t *vsecp)
3852621Sllai1 {
3862621Sllai1 	struct vnode		*vp = NULL;
3872621Sllai1 	struct vnode		*avp;
3882621Sllai1 	struct sdev_node	*parent;
3892621Sllai1 	struct sdev_node	*self = NULL;
3902621Sllai1 	int			error = 0;
3912621Sllai1 	vtype_t			type = vap->va_type;
3922621Sllai1 
3932729Sllai1 	ASSERT(type != VNON && type != VBAD);
3942621Sllai1 
3952621Sllai1 	if ((type == VFIFO) || (type == VSOCK) ||
3962621Sllai1 	    (type == VPROC) || (type == VPORT))
3972621Sllai1 		return (ENOTSUP);
3982621Sllai1 
3992621Sllai1 	parent = VTOSDEV(dvp);
4002621Sllai1 	ASSERT(parent);
4012621Sllai1 
4022621Sllai1 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
4032621Sllai1 	if (parent->sdev_state == SDEV_ZOMBIE) {
4042621Sllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
4052621Sllai1 		return (ENOENT);
4062621Sllai1 	}
4072621Sllai1 
4082621Sllai1 	/* non-global do not allow pure node creation */
4092621Sllai1 	if (!SDEV_IS_GLOBAL(parent)) {
4102621Sllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
4112621Sllai1 		return (prof_lookup(dvp, nm, vpp, cred));
4122621Sllai1 	}
4132621Sllai1 	rw_exit(&parent->sdev_dotdot->sdev_contents);
4142621Sllai1 
4153843Sjg 	/* execute access is required to search the directory */
4169198SJerry.Gilliam@Sun.COM 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0)
4173843Sjg 		return (error);
4183843Sjg 
4192621Sllai1 	/* check existing name */
4205331Samw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
4215331Samw 	error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL);
4222621Sllai1 
4232621Sllai1 	/* name found */
4242621Sllai1 	if (error == 0) {
4252621Sllai1 		ASSERT(vp);
4262621Sllai1 		if (excl == EXCL) {
4272621Sllai1 			error = EEXIST;
4282621Sllai1 		} else if ((vp->v_type == VDIR) && (mode & VWRITE)) {
4292621Sllai1 			/* allowing create/read-only an existing directory */
4302621Sllai1 			error = EISDIR;
4312621Sllai1 		} else {
4325686Sjg 			error = VOP_ACCESS(vp, mode, 0, cred, ct);
4332621Sllai1 		}
4342621Sllai1 
4352621Sllai1 		if (error) {
4362621Sllai1 			VN_RELE(vp);
4372621Sllai1 			return (error);
4382621Sllai1 		}
4392621Sllai1 
4402621Sllai1 		/* truncation first */
4412621Sllai1 		if ((vp->v_type == VREG) && (vap->va_mask & AT_SIZE) &&
4422621Sllai1 		    (vap->va_size == 0)) {
4432621Sllai1 			ASSERT(parent->sdev_attrvp);
4442621Sllai1 			error = VOP_CREATE(parent->sdev_attrvp,
4455331Samw 			    nm, vap, excl, mode, &avp, cred, flag, ct, vsecp);
4462621Sllai1 
4472621Sllai1 			if (error) {
4482621Sllai1 				VN_RELE(vp);
4492621Sllai1 				return (error);
4502621Sllai1 			}
4512621Sllai1 		}
4522621Sllai1 
4532621Sllai1 		sdev_update_timestamps(vp, kcred,
4542621Sllai1 		    AT_CTIME|AT_MTIME|AT_ATIME);
4552621Sllai1 		*vpp = vp;
4562621Sllai1 		return (0);
4572621Sllai1 	}
4582621Sllai1 
4592621Sllai1 	/* bail out early */
4602621Sllai1 	if (error != ENOENT)
4612621Sllai1 		return (error);
4622621Sllai1 
4639198SJerry.Gilliam@Sun.COM 	/* verify write access - compliance specifies ENXIO */
4649198SJerry.Gilliam@Sun.COM 	if ((error = VOP_ACCESS(dvp, VEXEC|VWRITE, 0, cred, ct)) != 0) {
4659198SJerry.Gilliam@Sun.COM 		if (error == EACCES)
4669198SJerry.Gilliam@Sun.COM 			error = ENXIO;
4679198SJerry.Gilliam@Sun.COM 		return (error);
4689198SJerry.Gilliam@Sun.COM 	}
4699198SJerry.Gilliam@Sun.COM 
4702621Sllai1 	/*
4712621Sllai1 	 * For memory-based (ROFS) directory:
4722621Sllai1 	 * 	- either disallow node creation;
4732621Sllai1 	 *	- or implement VOP_CREATE of its own
4742621Sllai1 	 */
4752621Sllai1 	rw_enter(&parent->sdev_contents, RW_WRITER);
4762621Sllai1 	if (!SDEV_IS_PERSIST(parent)) {
4772621Sllai1 		rw_exit(&parent->sdev_contents);
4782621Sllai1 		return (ENOTSUP);
4792621Sllai1 	}
4802621Sllai1 	ASSERT(parent->sdev_attrvp);
4812621Sllai1 	error = sdev_mknode(parent, nm, &self, vap, NULL, NULL,
4822621Sllai1 	    cred, SDEV_READY);
4832621Sllai1 	if (error) {
4842621Sllai1 		rw_exit(&parent->sdev_contents);
4852621Sllai1 		if (self)
4862621Sllai1 			SDEV_RELE(self);
4872621Sllai1 		return (error);
4882621Sllai1 	}
4892621Sllai1 	rw_exit(&parent->sdev_contents);
4902621Sllai1 
4912621Sllai1 	ASSERT(self);
4922621Sllai1 	/* take care the timestamps for the node and its parent */
4932621Sllai1 	sdev_update_timestamps(SDEVTOV(self), kcred,
4942621Sllai1 	    AT_CTIME|AT_MTIME|AT_ATIME);
4952621Sllai1 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
4962621Sllai1 	if (SDEV_IS_GLOBAL(parent))
4972621Sllai1 		atomic_inc_ulong(&parent->sdev_gdir_gen);
4982621Sllai1 
4992621Sllai1 	/* wake up other threads blocked on looking up this node */
5002621Sllai1 	mutex_enter(&self->sdev_lookup_lock);
5012621Sllai1 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
5022621Sllai1 	mutex_exit(&self->sdev_lookup_lock);
5032621Sllai1 	error = sdev_to_vp(self, vpp);
5042621Sllai1 	return (error);
5052621Sllai1 }
5062621Sllai1 
5072621Sllai1 static int
5085331Samw sdev_remove(struct vnode *dvp, char *nm, struct cred *cred,
5095331Samw     caller_context_t *ct, int flags)
5102621Sllai1 {
5112621Sllai1 	int	error;
5122621Sllai1 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
5132621Sllai1 	struct vnode *vp = NULL;
5142621Sllai1 	struct sdev_node *dv = NULL;
5152621Sllai1 	int len;
5162621Sllai1 	int bkstore = 0;
5172621Sllai1 
5182621Sllai1 	/* bail out early */
5192621Sllai1 	len = strlen(nm);
5202621Sllai1 	if (nm[0] == '.') {
5212621Sllai1 		if (len == 1) {
5222621Sllai1 			return (EINVAL);
5232621Sllai1 		} else if (len == 2 && nm[1] == '.') {
5242621Sllai1 			return (EEXIST);
5252621Sllai1 		}
5262621Sllai1 	}
5272621Sllai1 
5282621Sllai1 	ASSERT(parent);
5292621Sllai1 	rw_enter(&parent->sdev_contents, RW_READER);
5302621Sllai1 	if (!SDEV_IS_GLOBAL(parent)) {
5312621Sllai1 		rw_exit(&parent->sdev_contents);
5322621Sllai1 		return (ENOTSUP);
5332621Sllai1 	}
5342621Sllai1 
5353843Sjg 	/* execute access is required to search the directory */
5365331Samw 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) {
5373843Sjg 		rw_exit(&parent->sdev_contents);
5383843Sjg 		return (error);
5393843Sjg 	}
5403843Sjg 
5412621Sllai1 	/* check existence first */
5422621Sllai1 	dv = sdev_cache_lookup(parent, nm);
5432621Sllai1 	if (dv == NULL) {
5442621Sllai1 		rw_exit(&parent->sdev_contents);
5452621Sllai1 		return (ENOENT);
5462621Sllai1 	}
5472621Sllai1 
5482621Sllai1 	vp = SDEVTOV(dv);
5492621Sllai1 	if ((dv->sdev_state == SDEV_INIT) ||
5502621Sllai1 	    (dv->sdev_state == SDEV_ZOMBIE)) {
5512621Sllai1 		rw_exit(&parent->sdev_contents);
5522621Sllai1 		VN_RELE(vp);
5532621Sllai1 		return (ENOENT);
5542621Sllai1 	}
5552621Sllai1 
5563843Sjg 	/* write access is required to remove an entry */
5575331Samw 	if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0) {
5583843Sjg 		rw_exit(&parent->sdev_contents);
5593843Sjg 		VN_RELE(vp);
5603843Sjg 		return (error);
5613843Sjg 	}
5623843Sjg 
5632621Sllai1 	/*
5642621Sllai1 	 * sdev_dirdelete does the real job of:
5652621Sllai1 	 *  - make sure no open ref count
5662621Sllai1 	 *  - destroying the sdev_node
5672621Sllai1 	 *  - releasing the hold on attrvp
5682621Sllai1 	 */
5692621Sllai1 	bkstore = SDEV_IS_PERSIST(dv) ? 1 : 0;
5702621Sllai1 	if (!rw_tryupgrade(&parent->sdev_contents)) {
5712621Sllai1 		rw_exit(&parent->sdev_contents);
5722621Sllai1 		rw_enter(&parent->sdev_contents, RW_WRITER);
5732621Sllai1 	}
5742621Sllai1 	error = sdev_cache_update(parent, &dv, nm, SDEV_CACHE_DELETE);
5752621Sllai1 	rw_exit(&parent->sdev_contents);
5762621Sllai1 
5772621Sllai1 	sdcmn_err2(("sdev_remove: cache_update error %d\n", error));
5782621Sllai1 	if (error && (error != EBUSY)) {
5792621Sllai1 		/* report errors other than EBUSY */
5802621Sllai1 		VN_RELE(vp);
5812621Sllai1 	} else {
5822621Sllai1 		sdcmn_err2(("sdev_remove: cleaning node %s from cache "
5832621Sllai1 		    " with error %d\n", nm, error));
5842621Sllai1 
5852621Sllai1 		/*
5862621Sllai1 		 * best efforts clean up the backing store
5872621Sllai1 		 */
5882621Sllai1 		if (bkstore) {
5892621Sllai1 			ASSERT(parent->sdev_attrvp);
5905331Samw 			error = VOP_REMOVE(parent->sdev_attrvp, nm, cred,
5915331Samw 			    ct, flags);
5922621Sllai1 			/*
5932621Sllai1 			 * do not report BUSY error
5942621Sllai1 			 * because the backing store ref count is released
5952621Sllai1 			 * when the last ref count on the sdev_node is
5962621Sllai1 			 * released.
5972621Sllai1 			 */
5982621Sllai1 			if (error == EBUSY) {
5992621Sllai1 				sdcmn_err2(("sdev_remove: device %s is still on"
6002621Sllai1 				    "disk %s\n", nm, parent->sdev_path));
6012621Sllai1 				error = 0;
6022621Sllai1 			}
6032621Sllai1 		}
6042621Sllai1 
6052621Sllai1 		if (error == EBUSY)
6062621Sllai1 			error = 0;
6072621Sllai1 	}
6082621Sllai1 
6092621Sllai1 	return (error);
6102621Sllai1 }
6112621Sllai1 
6122621Sllai1 /*
6132621Sllai1  * Some restrictions for this file system:
6142621Sllai1  *  - both oldnm and newnm are in the scope of /dev file system,
6152621Sllai1  *    to simply the namespace management model.
6162621Sllai1  */
6175331Samw /*ARGSUSED6*/
6182621Sllai1 static int
6192621Sllai1 sdev_rename(struct vnode *odvp, char *onm, struct vnode *ndvp, char *nnm,
6205331Samw     struct cred *cred, caller_context_t *ct, int flags)
6212621Sllai1 {
6222621Sllai1 	struct sdev_node	*fromparent = NULL;
6232621Sllai1 	struct vattr		vattr;
6242621Sllai1 	struct sdev_node	*toparent;
6252621Sllai1 	struct sdev_node	*fromdv = NULL;	/* source node */
6262729Sllai1 	struct vnode 		*ovp = NULL;	/* source vnode */
6272621Sllai1 	struct sdev_node	*todv = NULL;	/* destination node */
6282729Sllai1 	struct vnode 		*nvp = NULL;	/* destination vnode */
6292621Sllai1 	int			samedir = 0;	/* set if odvp == ndvp */
6302621Sllai1 	struct vnode		*realvp;
6312621Sllai1 	int error = 0;
6322621Sllai1 	dev_t fsid;
6332621Sllai1 	int bkstore = 0;
6342729Sllai1 	vtype_t type;
6352621Sllai1 
6362621Sllai1 	/* prevent modifying "." and ".." */
6372621Sllai1 	if ((onm[0] == '.' &&
6382729Sllai1 	    (onm[1] == '\0' || (onm[1] == '.' && onm[2] == '\0'))) ||
6392729Sllai1 	    (nnm[0] == '.' &&
6402729Sllai1 	    (nnm[1] == '\0' || (nnm[1] == '.' && nnm[2] == '\0')))) {
6412621Sllai1 		return (EINVAL);
6422621Sllai1 	}
6432621Sllai1 
6442621Sllai1 	fromparent = VTOSDEV(odvp);
6452621Sllai1 	toparent = VTOSDEV(ndvp);
6462621Sllai1 
6472621Sllai1 	/* ZOMBIE parent doesn't allow new node creation */
6482621Sllai1 	rw_enter(&fromparent->sdev_dotdot->sdev_contents, RW_READER);
6492621Sllai1 	if (fromparent->sdev_state == SDEV_ZOMBIE) {
6502621Sllai1 		rw_exit(&fromparent->sdev_dotdot->sdev_contents);
6512621Sllai1 		return (ENOENT);
6522621Sllai1 	}
6532621Sllai1 
6542621Sllai1 	/* renaming only supported for global device nodes */
6552621Sllai1 	if (!SDEV_IS_GLOBAL(fromparent)) {
6562621Sllai1 		rw_exit(&fromparent->sdev_dotdot->sdev_contents);
6572621Sllai1 		return (ENOTSUP);
6582621Sllai1 	}
6592621Sllai1 	rw_exit(&fromparent->sdev_dotdot->sdev_contents);
6602621Sllai1 
6612621Sllai1 	rw_enter(&toparent->sdev_dotdot->sdev_contents, RW_READER);
6622621Sllai1 	if (toparent->sdev_state == SDEV_ZOMBIE) {
6632621Sllai1 		rw_exit(&toparent->sdev_dotdot->sdev_contents);
6642621Sllai1 		return (ENOENT);
6652621Sllai1 	}
6662621Sllai1 	rw_exit(&toparent->sdev_dotdot->sdev_contents);
6672621Sllai1 
6682729Sllai1 	/*
6693843Sjg 	 * acquire the global lock to prevent
6702729Sllai1 	 * mount/unmount/other rename activities.
6712729Sllai1 	 */
6722729Sllai1 	mutex_enter(&sdev_lock);
6732729Sllai1 
6742621Sllai1 	/* check existence of the source node */
6755331Samw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
6765331Samw 	error = VOP_LOOKUP(odvp, onm, &ovp, NULL, 0, NULL, cred, ct,
6775331Samw 	    NULL, NULL);
6782621Sllai1 	if (error) {
6792621Sllai1 		sdcmn_err2(("sdev_rename: the source node %s exists\n",
6802621Sllai1 		    onm));
6812729Sllai1 		mutex_exit(&sdev_lock);
6822621Sllai1 		return (error);
6832621Sllai1 	}
6842621Sllai1 
6855331Samw 	if (VOP_REALVP(ovp, &realvp, ct) == 0) {
6862621Sllai1 		VN_HOLD(realvp);
6872621Sllai1 		VN_RELE(ovp);
6882621Sllai1 		ovp = realvp;
6892621Sllai1 	}
6902621Sllai1 
6912621Sllai1 	/* check existence of destination */
6925331Samw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
6935331Samw 	error = VOP_LOOKUP(ndvp, nnm, &nvp, NULL, 0, NULL, cred, ct,
6945331Samw 	    NULL, NULL);
6952621Sllai1 	if (error && (error != ENOENT)) {
6962729Sllai1 		mutex_exit(&sdev_lock);
6972621Sllai1 		VN_RELE(ovp);
6982621Sllai1 		return (error);
6992621Sllai1 	}
7002621Sllai1 
7015331Samw 	if (nvp && (VOP_REALVP(nvp, &realvp, ct) == 0)) {
7022621Sllai1 		VN_HOLD(realvp);
7032621Sllai1 		VN_RELE(nvp);
7042621Sllai1 		nvp = realvp;
7052621Sllai1 	}
7062621Sllai1 
7072621Sllai1 	/*
7082729Sllai1 	 * make sure the source and the destination are
7092729Sllai1 	 * in the same dev filesystem
7102621Sllai1 	 */
7112621Sllai1 	if (odvp != ndvp) {
7122621Sllai1 		vattr.va_mask = AT_FSID;
7135331Samw 		if (error = VOP_GETATTR(odvp, &vattr, 0, cred, ct)) {
7142729Sllai1 			mutex_exit(&sdev_lock);
7152621Sllai1 			VN_RELE(ovp);
7162621Sllai1 			return (error);
7172621Sllai1 		}
7182621Sllai1 		fsid = vattr.va_fsid;
7192621Sllai1 		vattr.va_mask = AT_FSID;
7205331Samw 		if (error = VOP_GETATTR(ndvp, &vattr, 0, cred, ct)) {
7212729Sllai1 			mutex_exit(&sdev_lock);
7222621Sllai1 			VN_RELE(ovp);
7232621Sllai1 			return (error);
7242621Sllai1 		}
7252621Sllai1 		if (fsid != vattr.va_fsid) {
7262729Sllai1 			mutex_exit(&sdev_lock);
7272621Sllai1 			VN_RELE(ovp);
7282621Sllai1 			return (EXDEV);
7292621Sllai1 		}
7302621Sllai1 	}
7312621Sllai1 
7322621Sllai1 	/* make sure the old entry can be deleted */
7335331Samw 	error = VOP_ACCESS(odvp, VWRITE, 0, cred, ct);
7342621Sllai1 	if (error) {
7352729Sllai1 		mutex_exit(&sdev_lock);
7362621Sllai1 		VN_RELE(ovp);
7372621Sllai1 		return (error);
7382621Sllai1 	}
7392621Sllai1 
7402621Sllai1 	/* make sure the destination allows creation */
7412621Sllai1 	samedir = (fromparent == toparent);
7422621Sllai1 	if (!samedir) {
7435331Samw 		error = VOP_ACCESS(ndvp, VEXEC|VWRITE, 0, cred, ct);
7442621Sllai1 		if (error) {
7452729Sllai1 			mutex_exit(&sdev_lock);
7462621Sllai1 			VN_RELE(ovp);
7472621Sllai1 			return (error);
7482621Sllai1 		}
7492621Sllai1 	}
7502621Sllai1 
7512621Sllai1 	fromdv = VTOSDEV(ovp);
7522621Sllai1 	ASSERT(fromdv);
7532621Sllai1 
7542729Sllai1 	/* destination file exists */
7552621Sllai1 	if (nvp) {
7562729Sllai1 		todv = VTOSDEV(nvp);
7572729Sllai1 		ASSERT(todv);
7582621Sllai1 	}
7592621Sllai1 
7602621Sllai1 	/*
7612621Sllai1 	 * link source to new target in the memory
7622621Sllai1 	 */
7632729Sllai1 	error = sdev_rnmnode(fromparent, fromdv, toparent, &todv, nnm, cred);
7642621Sllai1 	if (error) {
7652621Sllai1 		sdcmn_err2(("sdev_rename: renaming %s to %s failed "
7662621Sllai1 		    " with error %d\n", onm, nnm, error));
7672729Sllai1 		mutex_exit(&sdev_lock);
7682729Sllai1 		if (nvp)
7692729Sllai1 			VN_RELE(nvp);
7702621Sllai1 		VN_RELE(ovp);
7712729Sllai1 		return (error);
7722729Sllai1 	}
7732729Sllai1 
7742729Sllai1 	/*
7752729Sllai1 	 * unlink from source
7762729Sllai1 	 */
7772729Sllai1 	rw_enter(&fromparent->sdev_contents, RW_READER);
7782729Sllai1 	fromdv = sdev_cache_lookup(fromparent, onm);
7792729Sllai1 	if (fromdv == NULL) {
7802729Sllai1 		rw_exit(&fromparent->sdev_contents);
7812729Sllai1 		mutex_exit(&sdev_lock);
7822729Sllai1 		sdcmn_err2(("sdev_rename: the source is deleted already\n"));
7832729Sllai1 		return (0);
7842621Sllai1 	}
7852621Sllai1 
7862729Sllai1 	if (fromdv->sdev_state == SDEV_ZOMBIE) {
7872729Sllai1 		rw_exit(&fromparent->sdev_contents);
7882729Sllai1 		mutex_exit(&sdev_lock);
7892729Sllai1 		VN_RELE(SDEVTOV(fromdv));
7902729Sllai1 		sdcmn_err2(("sdev_rename: the source is being deleted\n"));
7912729Sllai1 		return (0);
7922729Sllai1 	}
7932729Sllai1 	rw_exit(&fromparent->sdev_contents);
7942729Sllai1 	ASSERT(SDEVTOV(fromdv) == ovp);
7952729Sllai1 	VN_RELE(ovp);
7962729Sllai1 
7972729Sllai1 	/* clean out the directory contents before it can be removed */
7982729Sllai1 	type = SDEVTOV(fromdv)->v_type;
7992729Sllai1 	if (type == VDIR) {
8002729Sllai1 		error = sdev_cleandir(fromdv, NULL, 0);
8012729Sllai1 		sdcmn_err2(("sdev_rename: cleandir finished with %d\n",
8022729Sllai1 		    error));
8032729Sllai1 		if (error == EBUSY)
8042729Sllai1 			error = 0;
8052729Sllai1 	}
8062729Sllai1 
8072729Sllai1 	rw_enter(&fromparent->sdev_contents, RW_WRITER);
8082729Sllai1 	bkstore = SDEV_IS_PERSIST(fromdv) ? 1 : 0;
8092729Sllai1 	error = sdev_cache_update(fromparent, &fromdv, onm,
8102729Sllai1 	    SDEV_CACHE_DELETE);
8112729Sllai1 
8122729Sllai1 	/* best effforts clean up the backing store */
8132729Sllai1 	if (bkstore) {
8142729Sllai1 		ASSERT(fromparent->sdev_attrvp);
8152729Sllai1 		if (type != VDIR) {
8165331Samw /* XXXci - We may need to translate the C-I flags on VOP_REMOVE */
8172729Sllai1 			error = VOP_REMOVE(fromparent->sdev_attrvp,
8185331Samw 			    onm, kcred, ct, 0);
8192729Sllai1 		} else {
8205331Samw /* XXXci - We may need to translate the C-I flags on VOP_RMDIR */
8212729Sllai1 			error = VOP_RMDIR(fromparent->sdev_attrvp,
8225331Samw 			    onm, fromparent->sdev_attrvp, kcred, ct, 0);
8232729Sllai1 		}
8242729Sllai1 
8252729Sllai1 		if (error) {
8262729Sllai1 			sdcmn_err2(("sdev_rename: device %s is "
8272729Sllai1 			    "still on disk %s\n", onm,
8282729Sllai1 			    fromparent->sdev_path));
8292729Sllai1 			error = 0;
8302729Sllai1 		}
8312729Sllai1 	}
8322729Sllai1 	rw_exit(&fromparent->sdev_contents);
8332729Sllai1 	mutex_exit(&sdev_lock);
8342729Sllai1 
8352729Sllai1 	/* once reached to this point, the rename is regarded successful */
8362729Sllai1 	return (0);
8372621Sllai1 }
8382621Sllai1 
8392621Sllai1 /*
8402621Sllai1  * dev-fs version of "ln -s path dev-name"
8412621Sllai1  *	tnm - path, e.g. /devices/... or /dev/...
8422621Sllai1  *	lnm - dev_name
8432621Sllai1  */
8445331Samw /*ARGSUSED6*/
8452621Sllai1 static int
8462621Sllai1 sdev_symlink(struct vnode *dvp, char *lnm, struct vattr *tva,
8475331Samw     char *tnm, struct cred *cred, caller_context_t *ct, int flags)
8482621Sllai1 {
8492621Sllai1 	int error;
8502621Sllai1 	struct vnode *vp = NULL;
8512621Sllai1 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
8522621Sllai1 	struct sdev_node *self = (struct sdev_node *)NULL;
8532621Sllai1 
8542621Sllai1 	ASSERT(parent);
8552621Sllai1 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
8562621Sllai1 	if (parent->sdev_state == SDEV_ZOMBIE) {
8572621Sllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
8582621Sllai1 		sdcmn_err2(("sdev_symlink: parent %s is ZOMBIED \n",
8592621Sllai1 		    parent->sdev_name));
8602621Sllai1 		return (ENOENT);
8612621Sllai1 	}
8622621Sllai1 
8632621Sllai1 	if (!SDEV_IS_GLOBAL(parent)) {
8642621Sllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
8652621Sllai1 		return (ENOTSUP);
8662621Sllai1 	}
8672621Sllai1 	rw_exit(&parent->sdev_dotdot->sdev_contents);
8682621Sllai1 
8693843Sjg 	/* execute access is required to search a directory */
8705331Samw 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0)
8713843Sjg 		return (error);
8723843Sjg 
8732621Sllai1 	/* find existing name */
8745331Samw /* XXXci - We may need to translate the C-I flags here */
8755331Samw 	error = VOP_LOOKUP(dvp, lnm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL);
8762621Sllai1 	if (error == 0) {
8772621Sllai1 		ASSERT(vp);
8782621Sllai1 		VN_RELE(vp);
8792621Sllai1 		sdcmn_err2(("sdev_symlink: node %s already exists\n", lnm));
8802621Sllai1 		return (EEXIST);
8812621Sllai1 	}
8823843Sjg 	if (error != ENOENT)
8833843Sjg 		return (error);
8842621Sllai1 
8853843Sjg 	/* write access is required to create a symlink */
8865331Samw 	if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0)
8872621Sllai1 		return (error);
8882621Sllai1 
8892621Sllai1 	/* put it into memory cache */
8902621Sllai1 	rw_enter(&parent->sdev_contents, RW_WRITER);
8912621Sllai1 	error = sdev_mknode(parent, lnm, &self, tva, NULL, (void *)tnm,
8922621Sllai1 	    cred, SDEV_READY);
8932621Sllai1 	if (error) {
8942621Sllai1 		rw_exit(&parent->sdev_contents);
8952621Sllai1 		sdcmn_err2(("sdev_symlink: node %s creation failed\n", lnm));
8962621Sllai1 		if (self)
8972621Sllai1 			SDEV_RELE(self);
8982621Sllai1 
8992621Sllai1 		return (error);
9002621Sllai1 	}
9012621Sllai1 	ASSERT(self && (self->sdev_state == SDEV_READY));
9022621Sllai1 	rw_exit(&parent->sdev_contents);
9032621Sllai1 
9042621Sllai1 	/* take care the timestamps for the node and its parent */
9052621Sllai1 	sdev_update_timestamps(SDEVTOV(self), kcred,
9062621Sllai1 	    AT_CTIME|AT_MTIME|AT_ATIME);
9072621Sllai1 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
9082621Sllai1 	if (SDEV_IS_GLOBAL(parent))
9092621Sllai1 		atomic_inc_ulong(&parent->sdev_gdir_gen);
9102621Sllai1 
9112621Sllai1 	/* wake up other threads blocked on looking up this node */
9122621Sllai1 	mutex_enter(&self->sdev_lookup_lock);
9132621Sllai1 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
9142621Sllai1 	mutex_exit(&self->sdev_lookup_lock);
9152621Sllai1 	SDEV_RELE(self);	/* don't return with vnode held */
9162621Sllai1 	return (0);
9172621Sllai1 }
9182621Sllai1 
9195331Samw /*ARGSUSED6*/
9202621Sllai1 static int
9212621Sllai1 sdev_mkdir(struct vnode *dvp, char *nm, struct vattr *va, struct vnode **vpp,
9225331Samw     struct cred *cred, caller_context_t *ct, int flags, vsecattr_t *vsecp)
9232621Sllai1 {
9242621Sllai1 	int error;
9252621Sllai1 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
9262621Sllai1 	struct sdev_node *self = NULL;
9272621Sllai1 	struct vnode	*vp = NULL;
9282621Sllai1 
9292621Sllai1 	ASSERT(parent && parent->sdev_dotdot);
9302621Sllai1 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
9312621Sllai1 	if (parent->sdev_state == SDEV_ZOMBIE) {
9322621Sllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
9332621Sllai1 		return (ENOENT);
9342621Sllai1 	}
9352621Sllai1 
9362621Sllai1 	/* non-global do not allow pure directory creation */
9372621Sllai1 	if (!SDEV_IS_GLOBAL(parent)) {
9382621Sllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
9392621Sllai1 		return (prof_lookup(dvp, nm, vpp, cred));
9402621Sllai1 	}
9412621Sllai1 	rw_exit(&parent->sdev_dotdot->sdev_contents);
9422621Sllai1 
9433843Sjg 	/* execute access is required to search the directory */
9445331Samw 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) {
9453843Sjg 		return (error);
9463843Sjg 	}
9473843Sjg 
9482621Sllai1 	/* find existing name */
9495331Samw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
9505331Samw 	error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL);
9512621Sllai1 	if (error == 0) {
9522621Sllai1 		VN_RELE(vp);
9532621Sllai1 		return (EEXIST);
9542621Sllai1 	}
9552621Sllai1 	if (error != ENOENT)
9562621Sllai1 		return (error);
9572621Sllai1 
9583843Sjg 	/* require write access to create a directory */
9595331Samw 	if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0) {
9603843Sjg 		return (error);
9613843Sjg 	}
9623843Sjg 
9632621Sllai1 	/* put it into memory */
9642621Sllai1 	rw_enter(&parent->sdev_contents, RW_WRITER);
9652621Sllai1 	error = sdev_mknode(parent, nm, &self,
9662621Sllai1 	    va, NULL, NULL, cred, SDEV_READY);
9672621Sllai1 	if (error) {
9682621Sllai1 		rw_exit(&parent->sdev_contents);
9692621Sllai1 		if (self)
9702621Sllai1 			SDEV_RELE(self);
9712621Sllai1 		return (error);
9722621Sllai1 	}
9732621Sllai1 	ASSERT(self && (self->sdev_state == SDEV_READY));
9742621Sllai1 	rw_exit(&parent->sdev_contents);
9752621Sllai1 
9762621Sllai1 	/* take care the timestamps for the node and its parent */
9772621Sllai1 	sdev_update_timestamps(SDEVTOV(self), kcred,
9782621Sllai1 	    AT_CTIME|AT_MTIME|AT_ATIME);
9792621Sllai1 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
9802621Sllai1 	if (SDEV_IS_GLOBAL(parent))
9812621Sllai1 		atomic_inc_ulong(&parent->sdev_gdir_gen);
9822621Sllai1 
9832621Sllai1 	/* wake up other threads blocked on looking up this node */
9842621Sllai1 	mutex_enter(&self->sdev_lookup_lock);
9852621Sllai1 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
9862621Sllai1 	mutex_exit(&self->sdev_lookup_lock);
9872621Sllai1 	*vpp = SDEVTOV(self);
9882621Sllai1 	return (0);
9892621Sllai1 }
9902621Sllai1 
9912621Sllai1 /*
9922621Sllai1  * allowing removing an empty directory under /dev
9932621Sllai1  */
9942621Sllai1 /*ARGSUSED*/
9952621Sllai1 static int
9965331Samw sdev_rmdir(struct vnode *dvp, char *nm, struct vnode *cdir, struct cred *cred,
9975331Samw     caller_context_t *ct, int flags)
9982621Sllai1 {
9992621Sllai1 	int error = 0;
10002621Sllai1 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
10012621Sllai1 	struct sdev_node *self = NULL;
10022621Sllai1 	struct vnode *vp = NULL;
10032621Sllai1 
10042621Sllai1 	/* bail out early */
10052621Sllai1 	if (strcmp(nm, ".") == 0)
10062621Sllai1 		return (EINVAL);
10072621Sllai1 	if (strcmp(nm, "..") == 0)
10082621Sllai1 		return (EEXIST); /* should be ENOTEMPTY */
10092621Sllai1 
10102621Sllai1 	/* no destruction of non-global node */
10112621Sllai1 	ASSERT(parent && parent->sdev_dotdot);
10122621Sllai1 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
10132621Sllai1 	if (!SDEV_IS_GLOBAL(parent)) {
10142621Sllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
10152621Sllai1 		return (ENOTSUP);
10162621Sllai1 	}
10172621Sllai1 	rw_exit(&parent->sdev_dotdot->sdev_contents);
10182621Sllai1 
10193843Sjg 	/* execute access is required to search the directory */
1020*10588SEric.Taylor@Sun.COM 	if ((error = VOP_ACCESS(dvp, VEXEC|VWRITE, 0, cred, ct)) != 0)
10213843Sjg 		return (error);
10223843Sjg 
10232621Sllai1 	/* check existing name */
10242621Sllai1 	rw_enter(&parent->sdev_contents, RW_WRITER);
10252621Sllai1 	self = sdev_cache_lookup(parent, nm);
10262621Sllai1 	if (self == NULL) {
10272621Sllai1 		rw_exit(&parent->sdev_contents);
10282621Sllai1 		return (ENOENT);
10292621Sllai1 	}
10302621Sllai1 
10312621Sllai1 	vp = SDEVTOV(self);
10322621Sllai1 	if ((self->sdev_state == SDEV_INIT) ||
10332621Sllai1 	    (self->sdev_state == SDEV_ZOMBIE)) {
10342621Sllai1 		rw_exit(&parent->sdev_contents);
10352621Sllai1 		VN_RELE(vp);
10362621Sllai1 		return (ENOENT);
10372621Sllai1 	}
10382621Sllai1 
10392621Sllai1 	/* some sanity checks */
10402621Sllai1 	if (vp == dvp || vp == cdir) {
10412621Sllai1 		rw_exit(&parent->sdev_contents);
10422621Sllai1 		VN_RELE(vp);
10432621Sllai1 		return (EINVAL);
10442621Sllai1 	}
10452621Sllai1 
10462621Sllai1 	if (vp->v_type != VDIR) {
10472621Sllai1 		rw_exit(&parent->sdev_contents);
10482621Sllai1 		VN_RELE(vp);
10492621Sllai1 		return (ENOTDIR);
10502621Sllai1 	}
10512621Sllai1 
10522621Sllai1 	if (vn_vfswlock(vp)) {
10532621Sllai1 		rw_exit(&parent->sdev_contents);
10542621Sllai1 		VN_RELE(vp);
10552621Sllai1 		return (EBUSY);
10562621Sllai1 	}
10572621Sllai1 
10582621Sllai1 	if (vn_mountedvfs(vp) != NULL) {
10592621Sllai1 		rw_exit(&parent->sdev_contents);
10602621Sllai1 		vn_vfsunlock(vp);
10612621Sllai1 		VN_RELE(vp);
10622621Sllai1 		return (EBUSY);
10632621Sllai1 	}
10642621Sllai1 
10652621Sllai1 	self = VTOSDEV(vp);
10662621Sllai1 	/* bail out on a non-empty directory */
10672621Sllai1 	rw_enter(&self->sdev_contents, RW_READER);
10682621Sllai1 	if (self->sdev_nlink > 2) {
10692621Sllai1 		rw_exit(&self->sdev_contents);
10702621Sllai1 		rw_exit(&parent->sdev_contents);
10712621Sllai1 		vn_vfsunlock(vp);
10722621Sllai1 		VN_RELE(vp);
10732621Sllai1 		return (ENOTEMPTY);
10742621Sllai1 	}
10752621Sllai1 	rw_exit(&self->sdev_contents);
10762621Sllai1 
10772621Sllai1 	/* unlink it from the directory cache */
10782621Sllai1 	error = sdev_cache_update(parent, &self, nm, SDEV_CACHE_DELETE);
10792621Sllai1 	rw_exit(&parent->sdev_contents);
10802621Sllai1 	vn_vfsunlock(vp);
10812621Sllai1 
10822621Sllai1 	if (error && (error != EBUSY)) {
10832621Sllai1 		VN_RELE(vp);
10842621Sllai1 	} else {
10852621Sllai1 		sdcmn_err2(("sdev_rmdir: cleaning node %s from directory "
10862621Sllai1 		    " cache with error %d\n", nm, error));
10872621Sllai1 
10882621Sllai1 		/* best effort to clean up the backing store */
10892621Sllai1 		if (SDEV_IS_PERSIST(parent)) {
10902621Sllai1 			ASSERT(parent->sdev_attrvp);
10912621Sllai1 			error = VOP_RMDIR(parent->sdev_attrvp, nm,
10925331Samw 			    parent->sdev_attrvp, kcred, ct, flags);
10932621Sllai1 			sdcmn_err2(("sdev_rmdir: cleaning device %s is on"
10942621Sllai1 			    " disk error %d\n", parent->sdev_path, error));
10952621Sllai1 		}
10962621Sllai1 
10972621Sllai1 		if (error == EBUSY)
10982621Sllai1 			error = 0;
10992621Sllai1 	}
11002621Sllai1 
11012621Sllai1 	return (error);
11022621Sllai1 }
11032621Sllai1 
11042621Sllai1 /*
11052621Sllai1  * read the contents of a symbolic link
11062621Sllai1  */
11072621Sllai1 static int
11085331Samw sdev_readlink(struct vnode *vp, struct uio *uiop, struct cred *cred,
11095331Samw     caller_context_t *ct)
11102621Sllai1 {
11112621Sllai1 	struct sdev_node *dv;
11122621Sllai1 	int	error = 0;
11132621Sllai1 
11142621Sllai1 	ASSERT(vp->v_type == VLNK);
11152621Sllai1 
11162621Sllai1 	dv = VTOSDEV(vp);
11172621Sllai1 
11182621Sllai1 	if (dv->sdev_attrvp) {
11192621Sllai1 		/* non-NULL attrvp implys a persisted node at READY state */
11205331Samw 		return (VOP_READLINK(dv->sdev_attrvp, uiop, cred, ct));
11212621Sllai1 	} else if (dv->sdev_symlink != NULL) {
11222621Sllai1 		/* memory nodes, e.g. local nodes */
11232621Sllai1 		rw_enter(&dv->sdev_contents, RW_READER);
11242621Sllai1 		sdcmn_err2(("sdev_readlink link is %s\n", dv->sdev_symlink));
11252621Sllai1 		error = uiomove(dv->sdev_symlink, strlen(dv->sdev_symlink),
11262621Sllai1 		    UIO_READ, uiop);
11272621Sllai1 		rw_exit(&dv->sdev_contents);
11282621Sllai1 		return (error);
11292621Sllai1 	}
11302621Sllai1 
11312621Sllai1 	return (ENOENT);
11322621Sllai1 }
11332621Sllai1 
11345331Samw /*ARGSUSED4*/
11352621Sllai1 static int
11365331Samw sdev_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred, int *eofp,
11375331Samw     caller_context_t *ct, int flags)
11382621Sllai1 {
11392679Sszhou 	struct sdev_node *parent = VTOSDEV(dvp);
11403843Sjg 	int error;
11413843Sjg 
11423843Sjg 	/* execute access is required to search the directory */
11435331Samw 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0)
11443843Sjg 		return (error);
11452679Sszhou 
11462679Sszhou 	ASSERT(parent);
11472679Sszhou 	if (!SDEV_IS_GLOBAL(parent))
11482679Sszhou 		prof_filldir(parent);
11492621Sllai1 	return (devname_readdir_func(dvp, uiop, cred, eofp, SDEV_BROWSE));
11502621Sllai1 }
11512621Sllai1 
11522621Sllai1 /*ARGSUSED1*/
11532621Sllai1 static void
11545331Samw sdev_inactive(struct vnode *vp, struct cred *cred, caller_context_t *ct)
11552621Sllai1 {
11565895Syz147064 	devname_inactive_func(vp, cred, NULL);
11572621Sllai1 }
11582621Sllai1 
11595331Samw /*ARGSUSED2*/
11602621Sllai1 static int
11615331Samw sdev_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
11622621Sllai1 {
11632621Sllai1 	struct sdev_node	*dv = VTOSDEV(vp);
11642621Sllai1 	struct sdev_fid	*sdev_fid;
11652621Sllai1 
11662621Sllai1 	if (fidp->fid_len < (sizeof (struct sdev_fid) - sizeof (ushort_t))) {
11672621Sllai1 		fidp->fid_len = sizeof (struct sdev_fid) - sizeof (ushort_t);
11682621Sllai1 		return (ENOSPC);
11692621Sllai1 	}
11702621Sllai1 
11712621Sllai1 	sdev_fid = (struct sdev_fid *)fidp;
11722621Sllai1 	bzero(sdev_fid, sizeof (struct sdev_fid));
11732621Sllai1 	sdev_fid->sdevfid_len =
11742621Sllai1 	    (int)sizeof (struct sdev_fid) - sizeof (ushort_t);
11752621Sllai1 	sdev_fid->sdevfid_ino = dv->sdev_ino;
11762621Sllai1 
11772621Sllai1 	return (0);
11782621Sllai1 }
11792621Sllai1 
11802621Sllai1 /*
11812621Sllai1  * This pair of routines bracket all VOP_READ, VOP_WRITE
11822621Sllai1  * and VOP_READDIR requests.  The contents lock stops things
11832621Sllai1  * moving around while we're looking at them.
11842621Sllai1  */
11853748Sjg /*ARGSUSED2*/
11863748Sjg static int
11873748Sjg sdev_rwlock(struct vnode *vp, int write_flag, caller_context_t *ctp)
11882621Sllai1 {
11893748Sjg 	rw_enter(&VTOSDEV(vp)->sdev_contents,
11903748Sjg 	    write_flag ? RW_WRITER : RW_READER);
11913748Sjg 	return (write_flag ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE);
11922621Sllai1 }
11932621Sllai1 
11942621Sllai1 /*ARGSUSED1*/
11952621Sllai1 static void
11963748Sjg sdev_rwunlock(struct vnode *vp, int write_flag, caller_context_t *ctp)
11972621Sllai1 {
11982621Sllai1 	rw_exit(&VTOSDEV(vp)->sdev_contents);
11992621Sllai1 }
12002621Sllai1 
12012621Sllai1 /*ARGSUSED1*/
12022621Sllai1 static int
12035331Samw sdev_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
12045331Samw     caller_context_t *ct)
12052621Sllai1 {
12062621Sllai1 	struct vnode *attrvp = VTOSDEV(vp)->sdev_attrvp;
12072621Sllai1 
12082621Sllai1 	ASSERT(vp->v_type != VCHR &&
12092621Sllai1 	    vp->v_type != VBLK && vp->v_type != VLNK);
12102621Sllai1 
12112621Sllai1 	if (vp->v_type == VDIR)
12125331Samw 		return (fs_seek(vp, ooff, noffp, ct));
12132621Sllai1 
12142621Sllai1 	ASSERT(attrvp);
12155331Samw 	return (VOP_SEEK(attrvp, ooff, noffp, ct));
12162621Sllai1 }
12172621Sllai1 
12182621Sllai1 /*ARGSUSED1*/
12192621Sllai1 static int
12202621Sllai1 sdev_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
12215331Samw     offset_t offset, struct flk_callback *flk_cbp, struct cred *cr,
12225331Samw     caller_context_t *ct)
12232621Sllai1 {
12242621Sllai1 	int error;
12252621Sllai1 	struct sdev_node *dv = VTOSDEV(vp);
12262621Sllai1 
12272621Sllai1 	ASSERT(dv);
12282621Sllai1 	ASSERT(dv->sdev_attrvp);
12292621Sllai1 	error = VOP_FRLOCK(dv->sdev_attrvp, cmd, bfp, flag, offset,
12305331Samw 	    flk_cbp, cr, ct);
12312621Sllai1 
12322621Sllai1 	return (error);
12332621Sllai1 }
12342621Sllai1 
12352621Sllai1 static int
12365331Samw sdev_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
12375331Samw     caller_context_t *ct)
12382621Sllai1 {
12392621Sllai1 	switch (cmd) {
12402621Sllai1 	case _PC_ACL_ENABLED:
12412621Sllai1 		*valp = SDEV_ACL_FLAVOR(vp);
12422621Sllai1 		return (0);
12432621Sllai1 	}
12442621Sllai1 
12455331Samw 	return (fs_pathconf(vp, cmd, valp, cr, ct));
12462621Sllai1 }
12472621Sllai1 
12482621Sllai1 vnodeops_t *sdev_vnodeops;
12492621Sllai1 
12502621Sllai1 const fs_operation_def_t sdev_vnodeops_tbl[] = {
12513898Srsb 	VOPNAME_OPEN,		{ .vop_open = sdev_open },
12523898Srsb 	VOPNAME_CLOSE,		{ .vop_close = sdev_close },
12533898Srsb 	VOPNAME_READ,		{ .vop_read = sdev_read },
12543898Srsb 	VOPNAME_WRITE,		{ .vop_write = sdev_write },
12553898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = sdev_ioctl },
12563898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = sdev_getattr },
12573898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = sdev_setattr },
12583898Srsb 	VOPNAME_ACCESS,		{ .vop_access = sdev_access },
12593898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = sdev_lookup },
12603898Srsb 	VOPNAME_CREATE,		{ .vop_create = sdev_create },
12613898Srsb 	VOPNAME_RENAME,		{ .vop_rename = sdev_rename },
12623898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = sdev_remove },
12633898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = sdev_mkdir },
12643898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = sdev_rmdir },
12653898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = sdev_readdir },
12663898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = sdev_symlink },
12673898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = sdev_readlink },
12683898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = sdev_inactive },
12693898Srsb 	VOPNAME_FID,		{ .vop_fid = sdev_fid },
12703898Srsb 	VOPNAME_RWLOCK,		{ .vop_rwlock = sdev_rwlock },
12713898Srsb 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = sdev_rwunlock },
12723898Srsb 	VOPNAME_SEEK,		{ .vop_seek = sdev_seek },
12733898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = sdev_frlock },
12743898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = sdev_pathconf },
12753898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = sdev_setsecattr },
12763898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = sdev_getsecattr },
12773898Srsb 	NULL,			NULL
12782621Sllai1 };
12792621Sllai1 
12802621Sllai1 int sdev_vnodeops_tbl_size = sizeof (sdev_vnodeops_tbl);
1281