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
53133Sjg  * Common Development and Distribution License (the "License").
63133Sjg  * 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 /*
22*3898Srsb  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * vnode ops for the devfs
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * For leaf vnode special files (VCHR|VBLK) specfs will always see the VOP
320Sstevel@tonic-gate  * first because dv_find always performs leaf vnode substitution, returning
330Sstevel@tonic-gate  * a specfs vnode with an s_realvp pointing to the devfs leaf vnode. This
340Sstevel@tonic-gate  * means that the only leaf special file VOP operations that devfs will see
350Sstevel@tonic-gate  * after VOP_LOOKUP are the ones that specfs forwards.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/param.h>
400Sstevel@tonic-gate #include <sys/t_lock.h>
410Sstevel@tonic-gate #include <sys/systm.h>
420Sstevel@tonic-gate #include <sys/sysmacros.h>
430Sstevel@tonic-gate #include <sys/user.h>
440Sstevel@tonic-gate #include <sys/time.h>
450Sstevel@tonic-gate #include <sys/vfs.h>
460Sstevel@tonic-gate #include <sys/vnode.h>
47*3898Srsb #include <sys/vfs_opreg.h>
480Sstevel@tonic-gate #include <sys/file.h>
490Sstevel@tonic-gate #include <sys/fcntl.h>
500Sstevel@tonic-gate #include <sys/flock.h>
510Sstevel@tonic-gate #include <sys/kmem.h>
520Sstevel@tonic-gate #include <sys/uio.h>
530Sstevel@tonic-gate #include <sys/errno.h>
540Sstevel@tonic-gate #include <sys/stat.h>
550Sstevel@tonic-gate #include <sys/cred.h>
560Sstevel@tonic-gate #include <sys/dirent.h>
570Sstevel@tonic-gate #include <sys/pathname.h>
580Sstevel@tonic-gate #include <sys/cmn_err.h>
590Sstevel@tonic-gate #include <sys/debug.h>
600Sstevel@tonic-gate #include <sys/policy.h>
610Sstevel@tonic-gate #include <sys/modctl.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <fs/fs_subr.h>
640Sstevel@tonic-gate #include <sys/fs/dv_node.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate extern struct vattr	dv_vattr_dir, dv_vattr_file;
670Sstevel@tonic-gate extern dev_t rconsdev;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * Open of devices (leaf nodes) is handled by specfs.
710Sstevel@tonic-gate  * There is nothing to do to open a directory
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate /*ARGSUSED*/
740Sstevel@tonic-gate static int
750Sstevel@tonic-gate devfs_open(struct vnode **vpp, int flag, struct cred *cred)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(*vpp);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	dcmn_err2(("devfs_open %s\n", dv->dv_name));
800Sstevel@tonic-gate 	ASSERT((*vpp)->v_type == VDIR);
810Sstevel@tonic-gate 	return (0);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Close of devices (leaf nodes) is handled by specfs.
860Sstevel@tonic-gate  * There is nothing much to do inorder to close a directory.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate /*ARGSUSED1*/
890Sstevel@tonic-gate static int
900Sstevel@tonic-gate devfs_close(struct vnode *vp, int flag, int count,
910Sstevel@tonic-gate     offset_t offset, struct cred *cred)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	dcmn_err2(("devfs_close %s\n", dv->dv_name));
960Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
990Sstevel@tonic-gate 	cleanshares(vp, ttoproc(curthread)->p_pid);
1000Sstevel@tonic-gate 	return (0);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * Read of devices (leaf nodes) is handled by specfs.
1050Sstevel@tonic-gate  * Read of directories is not supported.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate /*ARGSUSED*/
1080Sstevel@tonic-gate static int
1090Sstevel@tonic-gate devfs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
1100Sstevel@tonic-gate 	struct caller_context *ct)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	dcmn_err2(("devfs_read %s\n", VTODV(vp)->dv_name));
1130Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
1140Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&VTODV(vp)->dv_contents));
1150Sstevel@tonic-gate 	return (EISDIR);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * Write of devices (leaf nodes) is handled by specfs.
1200Sstevel@tonic-gate  * Write of directories is not supported.
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate /*ARGSUSED*/
1230Sstevel@tonic-gate static int
1240Sstevel@tonic-gate devfs_write(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
1250Sstevel@tonic-gate 	struct caller_context *ct)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	dcmn_err2(("devfs_write %s\n", VTODV(vp)->dv_name));
1280Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
1290Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&VTODV(vp)->dv_contents));
1300Sstevel@tonic-gate 	return (EISDIR);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate  * Ioctls to device (leaf nodes) is handled by specfs.
1350Sstevel@tonic-gate  * Ioctl to directories is not supported.
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate /*ARGSUSED*/
1380Sstevel@tonic-gate static int
1390Sstevel@tonic-gate devfs_ioctl(struct vnode *vp, int cmd, intptr_t arg, int flag,
1400Sstevel@tonic-gate     struct cred *cred, int *rvalp)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	dcmn_err2(("devfs_ioctl %s\n", VTODV(vp)->dv_name));
1430Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	return (ENOTTY);	/* no ioctls supported */
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * We can be asked directly about the attributes of directories, or
1500Sstevel@tonic-gate  * (via sp->s_realvp) about the filesystem attributes of special files.
1510Sstevel@tonic-gate  *
1520Sstevel@tonic-gate  * For directories, we just believe the attribute store
1530Sstevel@tonic-gate  * though we mangle the nodeid, fsid, and rdev to convince userland we
1540Sstevel@tonic-gate  * really are a different filesystem.
1550Sstevel@tonic-gate  *
1560Sstevel@tonic-gate  * For special files, a little more fakery is required.
1570Sstevel@tonic-gate  *
1580Sstevel@tonic-gate  * If the attribute store is not there (read only root), we believe our
1590Sstevel@tonic-gate  * memory based attributes.
1600Sstevel@tonic-gate  */
1610Sstevel@tonic-gate static int
1620Sstevel@tonic-gate devfs_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
1650Sstevel@tonic-gate 	int		error = 0;
1660Sstevel@tonic-gate 	uint_t		mask;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/*
1690Sstevel@tonic-gate 	 * Message goes to console only. Otherwise, the message
1700Sstevel@tonic-gate 	 * causes devfs_getattr to be invoked again... infinite loop
1710Sstevel@tonic-gate 	 */
1720Sstevel@tonic-gate 	dcmn_err2(("?devfs_getattr %s\n", dv->dv_name));
1730Sstevel@tonic-gate 	ASSERT(dv->dv_attr || dv->dv_attrvp);
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	if (!(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK)) {
1760Sstevel@tonic-gate 		cmn_err(CE_WARN,	/* panic ? */
1770Sstevel@tonic-gate 		    "?%s: getattr on vnode type %d", dvnm, vp->v_type);
1780Sstevel@tonic-gate 		return (ENOENT);
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (dv->dv_attr) {
1820Sstevel@tonic-gate 		/*
1830Sstevel@tonic-gate 		 * obtain from the memory version of attribute.
1840Sstevel@tonic-gate 		 * preserve mask for those that optimize.
1850Sstevel@tonic-gate 		 * devfs specific fields are already merged on creation.
1860Sstevel@tonic-gate 		 */
1870Sstevel@tonic-gate 		mask = vap->va_mask;
1880Sstevel@tonic-gate 		*vap = *dv->dv_attr;
1890Sstevel@tonic-gate 		vap->va_mask = mask;
1900Sstevel@tonic-gate 	} else {
1910Sstevel@tonic-gate 		/* obtain from attribute store and merge */
1920Sstevel@tonic-gate 		error = VOP_GETATTR(dv->dv_attrvp, vap, flags, cr);
1930Sstevel@tonic-gate 		dsysdebug(error, ("vop_getattr %s %d\n", dv->dv_name, error));
1940Sstevel@tonic-gate 		dv_vattr_merge(dv, vap);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/*
1980Sstevel@tonic-gate 	 * Restrict the permissions of the node fronting the console
1990Sstevel@tonic-gate 	 * to 0600 with root as the owner.  This prevents a non-root
2000Sstevel@tonic-gate 	 * user from gaining access to a serial terminal (like /dev/term/a)
2010Sstevel@tonic-gate 	 * which is in reality serving as the console device (/dev/console).
2020Sstevel@tonic-gate 	 */
2030Sstevel@tonic-gate 	if (vp->v_rdev == rconsdev) {
2040Sstevel@tonic-gate 		mode_t	rconsmask = S_IXUSR|S_IRWXG|S_IRWXO;
2050Sstevel@tonic-gate 		vap->va_mode &= (~rconsmask);
2060Sstevel@tonic-gate 		vap->va_uid = 0;
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	return (error);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate static int devfs_unlocked_access(void *, int, struct cred *);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*ARGSUSED4*/
2150Sstevel@tonic-gate static int
2160Sstevel@tonic-gate devfs_setattr_dir(
2170Sstevel@tonic-gate 	struct dv_node *dv,
2180Sstevel@tonic-gate 	struct vnode *vp,
2190Sstevel@tonic-gate 	struct vattr *vap,
2200Sstevel@tonic-gate 	int flags,
2210Sstevel@tonic-gate 	struct cred *cr)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate 	struct vattr	*map;
2240Sstevel@tonic-gate 	long int	mask;
2250Sstevel@tonic-gate 	int		error = 0;
2260Sstevel@tonic-gate 	struct vattr	vattr;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	ASSERT(dv->dv_attr || dv->dv_attrvp);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
2310Sstevel@tonic-gate 	ASSERT((dv->dv_flags & DV_NO_FSPERM) == 0);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if (vap->va_mask & AT_NOSET)
2340Sstevel@tonic-gate 		return (EINVAL);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	/* to ensure consistency, single thread setting of attributes */
2370Sstevel@tonic-gate 	rw_enter(&dv->dv_contents, RW_WRITER);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate again:	if (dv->dv_attr) {
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 		error = secpolicy_vnode_setattr(cr, vp, vap, dv->dv_attr,
2420Sstevel@tonic-gate 					flags, devfs_unlocked_access, dv);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		if (error)
2450Sstevel@tonic-gate 			goto out;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 		/*
2480Sstevel@tonic-gate 		 * Apply changes to the memory based attribute. This code
2490Sstevel@tonic-gate 		 * is modeled after the tmpfs implementation of memory
2500Sstevel@tonic-gate 		 * based vnodes
2510Sstevel@tonic-gate 		 */
2520Sstevel@tonic-gate 		map = dv->dv_attr;
2530Sstevel@tonic-gate 		mask = vap->va_mask;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 		/* Change file access modes. */
2560Sstevel@tonic-gate 		if (mask & AT_MODE) {
2570Sstevel@tonic-gate 			map->va_mode &= S_IFMT;
2580Sstevel@tonic-gate 			map->va_mode |= vap->va_mode & ~S_IFMT;
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 		if (mask & AT_UID)
2610Sstevel@tonic-gate 			map->va_uid = vap->va_uid;
2620Sstevel@tonic-gate 		if (mask & AT_GID)
2630Sstevel@tonic-gate 			map->va_gid = vap->va_gid;
2640Sstevel@tonic-gate 		if (mask & AT_ATIME)
2650Sstevel@tonic-gate 			map->va_atime = vap->va_atime;
2660Sstevel@tonic-gate 		if (mask & AT_MTIME)
2670Sstevel@tonic-gate 			map->va_mtime = vap->va_mtime;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 		if (mask & (AT_MODE | AT_UID | AT_GID | AT_MTIME))
2700Sstevel@tonic-gate 			gethrestime(&map->va_ctime);
2710Sstevel@tonic-gate 	} else {
2720Sstevel@tonic-gate 		/* use the backing attribute store */
2730Sstevel@tonic-gate 		ASSERT(dv->dv_attrvp);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 		/*
2760Sstevel@tonic-gate 		 * See if we are changing something we care about
2770Sstevel@tonic-gate 		 * the persistence of - return success if we don't care.
2780Sstevel@tonic-gate 		 */
2790Sstevel@tonic-gate 		if (vap->va_mask & (AT_MODE|AT_UID|AT_GID|AT_ATIME|AT_MTIME)) {
2800Sstevel@tonic-gate 			/* Set the attributes */
2810Sstevel@tonic-gate 			error = VOP_SETATTR(dv->dv_attrvp,
2820Sstevel@tonic-gate 				vap, flags, cr, NULL);
2830Sstevel@tonic-gate 			dsysdebug(error,
2840Sstevel@tonic-gate 				("vop_setattr %s %d\n", dv->dv_name, error));
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 			/*
2870Sstevel@tonic-gate 			 * Some file systems may return EROFS for a setattr
2880Sstevel@tonic-gate 			 * on a readonly file system.  In this case we create
2890Sstevel@tonic-gate 			 * our own memory based attribute.
2900Sstevel@tonic-gate 			 */
2910Sstevel@tonic-gate 			if (error == EROFS) {
2920Sstevel@tonic-gate 				/*
2930Sstevel@tonic-gate 				 * obtain attributes from existing file
2940Sstevel@tonic-gate 				 * that we will modify and switch to memory
2950Sstevel@tonic-gate 				 * based attribute until attribute store is
2960Sstevel@tonic-gate 				 * read/write.
2970Sstevel@tonic-gate 				 */
2980Sstevel@tonic-gate 				vattr = dv_vattr_dir;
2990Sstevel@tonic-gate 				if (VOP_GETATTR(dv->dv_attrvp, &vattr,
3000Sstevel@tonic-gate 				    flags, cr) == 0) {
3010Sstevel@tonic-gate 					dv->dv_attr = kmem_alloc(
3020Sstevel@tonic-gate 					    sizeof (struct vattr), KM_SLEEP);
3030Sstevel@tonic-gate 					*dv->dv_attr = vattr;
3040Sstevel@tonic-gate 					dv_vattr_merge(dv, dv->dv_attr);
3050Sstevel@tonic-gate 					goto again;
3060Sstevel@tonic-gate 				}
3070Sstevel@tonic-gate 			}
3080Sstevel@tonic-gate 		}
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate out:
3110Sstevel@tonic-gate 	rw_exit(&dv->dv_contents);
3120Sstevel@tonic-gate 	return (error);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate  * Compare the uid/gid/mode changes requested for a setattr
3180Sstevel@tonic-gate  * operation with the same details of a node's default minor
3190Sstevel@tonic-gate  * perm information.  Return 0 if identical.
3200Sstevel@tonic-gate  */
3210Sstevel@tonic-gate static int
3220Sstevel@tonic-gate dv_setattr_cmp(struct vattr *map, mperm_t *mp)
3230Sstevel@tonic-gate {
3240Sstevel@tonic-gate 	if ((map->va_mode & S_IAMB) != (mp->mp_mode & S_IAMB))
3250Sstevel@tonic-gate 		return (1);
3260Sstevel@tonic-gate 	if (map->va_uid != mp->mp_uid)
3270Sstevel@tonic-gate 		return (1);
3280Sstevel@tonic-gate 	if (map->va_gid != mp->mp_gid)
3290Sstevel@tonic-gate 		return (1);
3300Sstevel@tonic-gate 	return (0);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate /*ARGSUSED4*/
3350Sstevel@tonic-gate static int
3360Sstevel@tonic-gate devfs_setattr(
3370Sstevel@tonic-gate 	struct vnode *vp,
3380Sstevel@tonic-gate 	struct vattr *vap,
3390Sstevel@tonic-gate 	int flags,
3400Sstevel@tonic-gate 	struct cred *cr,
3410Sstevel@tonic-gate 	caller_context_t *ct)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
3440Sstevel@tonic-gate 	struct dv_node	*ddv;
3450Sstevel@tonic-gate 	struct vnode	*dvp;
3460Sstevel@tonic-gate 	struct vattr	*map;
3470Sstevel@tonic-gate 	long int	mask;
3480Sstevel@tonic-gate 	int		error = 0;
3490Sstevel@tonic-gate 	struct vattr	*free_vattr = NULL;
3500Sstevel@tonic-gate 	struct vattr	*vattrp = NULL;
3510Sstevel@tonic-gate 	mperm_t		mp;
3520Sstevel@tonic-gate 	int		persist;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/*
3550Sstevel@tonic-gate 	 * Message goes to console only. Otherwise, the message
3560Sstevel@tonic-gate 	 * causes devfs_getattr to be invoked again... infinite loop
3570Sstevel@tonic-gate 	 */
3580Sstevel@tonic-gate 	dcmn_err2(("?devfs_setattr %s\n", dv->dv_name));
3590Sstevel@tonic-gate 	ASSERT(dv->dv_attr || dv->dv_attrvp);
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if (!(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK)) {
3620Sstevel@tonic-gate 		cmn_err(CE_WARN,	/* panic ? */
3630Sstevel@tonic-gate 		    "?%s: getattr on vnode type %d", dvnm, vp->v_type);
3640Sstevel@tonic-gate 		return (ENOENT);
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	if (vap->va_mask & AT_NOSET)
3680Sstevel@tonic-gate 		return (EINVAL);
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	/*
3710Sstevel@tonic-gate 	 * If we are changing something we don't care about
3720Sstevel@tonic-gate 	 * the persistence of, return success.
3730Sstevel@tonic-gate 	 */
3740Sstevel@tonic-gate 	if ((vap->va_mask &
3750Sstevel@tonic-gate 	    (AT_MODE|AT_UID|AT_GID|AT_ATIME|AT_MTIME)) == 0)
3760Sstevel@tonic-gate 		return (0);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	/*
3790Sstevel@tonic-gate 	 * If driver overrides fs perm, disallow chmod
3800Sstevel@tonic-gate 	 * and do not create attribute nodes.
3810Sstevel@tonic-gate 	 */
3820Sstevel@tonic-gate 	if (dv->dv_flags & DV_NO_FSPERM) {
3830Sstevel@tonic-gate 		ASSERT(dv->dv_attr);
3840Sstevel@tonic-gate 		if (vap->va_mask & (AT_MODE | AT_UID | AT_GID))
3850Sstevel@tonic-gate 			return (EPERM);
3860Sstevel@tonic-gate 		if ((vap->va_mask & (AT_ATIME|AT_MTIME)) == 0)
3870Sstevel@tonic-gate 			return (0);
3880Sstevel@tonic-gate 		rw_enter(&dv->dv_contents, RW_WRITER);
3890Sstevel@tonic-gate 		if (vap->va_mask & AT_ATIME)
3900Sstevel@tonic-gate 			dv->dv_attr->va_atime = vap->va_atime;
3910Sstevel@tonic-gate 		if (vap->va_mask & AT_MTIME)
3920Sstevel@tonic-gate 			dv->dv_attr->va_mtime = vap->va_mtime;
3930Sstevel@tonic-gate 		rw_exit(&dv->dv_contents);
3940Sstevel@tonic-gate 		return (0);
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * Directories are always created but device nodes are
3990Sstevel@tonic-gate 	 * only used to persist non-default permissions.
4000Sstevel@tonic-gate 	 */
4010Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
4020Sstevel@tonic-gate 		ASSERT(dv->dv_attr || dv->dv_attrvp);
4030Sstevel@tonic-gate 		return (devfs_setattr_dir(dv, vp, vap, flags, cr));
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	/*
4070Sstevel@tonic-gate 	 * Allocate now before we take any locks
4080Sstevel@tonic-gate 	 */
4090Sstevel@tonic-gate 	vattrp = kmem_zalloc(sizeof (*vattrp), KM_SLEEP);
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	/* to ensure consistency, single thread setting of attributes */
4120Sstevel@tonic-gate 	rw_enter(&dv->dv_contents, RW_WRITER);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	/*
4150Sstevel@tonic-gate 	 * We don't need to create an attribute node
4160Sstevel@tonic-gate 	 * to persist access or modification times.
4170Sstevel@tonic-gate 	 */
4180Sstevel@tonic-gate 	persist = (vap->va_mask & (AT_MODE | AT_UID | AT_GID));
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/*
4210Sstevel@tonic-gate 	 * If persisting something, get the default permissions
4220Sstevel@tonic-gate 	 * for this minor to compare against what the attributes
4230Sstevel@tonic-gate 	 * are now being set to.  Default ordering is:
4240Sstevel@tonic-gate 	 *	- minor_perm match for this minor
4250Sstevel@tonic-gate 	 *	- mode supplied by ddi_create_priv_minor_node
4260Sstevel@tonic-gate 	 *	- devfs defaults
4270Sstevel@tonic-gate 	 */
4280Sstevel@tonic-gate 	if (persist) {
4290Sstevel@tonic-gate 		if (dev_minorperm(dv->dv_devi, dv->dv_name, &mp) != 0) {
4300Sstevel@tonic-gate 			mp.mp_uid = dv_vattr_file.va_uid;
4310Sstevel@tonic-gate 			mp.mp_gid = dv_vattr_file.va_gid;
4320Sstevel@tonic-gate 			mp.mp_mode = dv_vattr_file.va_mode;
4330Sstevel@tonic-gate 			if (dv->dv_flags & DV_DFLT_MODE) {
4340Sstevel@tonic-gate 				ASSERT((dv->dv_dflt_mode & ~S_IAMB) == 0);
4350Sstevel@tonic-gate 				mp.mp_mode &= ~S_IAMB;
4360Sstevel@tonic-gate 				mp.mp_mode |= dv->dv_dflt_mode;
4370Sstevel@tonic-gate 				dcmn_err5(("%s: setattr priv default 0%o\n",
4380Sstevel@tonic-gate 				    dv->dv_name, mp.mp_mode));
4390Sstevel@tonic-gate 			} else {
4400Sstevel@tonic-gate 				dcmn_err5(("%s: setattr devfs default 0%o\n",
4410Sstevel@tonic-gate 				    dv->dv_name, mp.mp_mode));
4420Sstevel@tonic-gate 			}
4430Sstevel@tonic-gate 		} else {
4440Sstevel@tonic-gate 			dcmn_err5(("%s: setattr minor perm default 0%o\n",
4450Sstevel@tonic-gate 			    dv->dv_name, mp.mp_mode));
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 	}
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	/*
4500Sstevel@tonic-gate 	 * If we don't have a vattr for this node, construct one.
4510Sstevel@tonic-gate 	 */
4520Sstevel@tonic-gate 	if (dv->dv_attr) {
4530Sstevel@tonic-gate 		free_vattr = vattrp;
4540Sstevel@tonic-gate 		vattrp = NULL;
4550Sstevel@tonic-gate 	} else {
4560Sstevel@tonic-gate 		ASSERT(dv->dv_attrvp);
4570Sstevel@tonic-gate 		ASSERT(vp->v_type != VDIR);
4580Sstevel@tonic-gate 		*vattrp = dv_vattr_file;
4590Sstevel@tonic-gate 		error = VOP_GETATTR(dv->dv_attrvp, vattrp, 0, cr);
4600Sstevel@tonic-gate 		dsysdebug(error, ("vop_getattr %s %d\n",
4610Sstevel@tonic-gate 			dv->dv_name, error));
4620Sstevel@tonic-gate 		if (error)
4630Sstevel@tonic-gate 			goto out;
4640Sstevel@tonic-gate 		dv->dv_attr = vattrp;
4650Sstevel@tonic-gate 		dv_vattr_merge(dv, dv->dv_attr);
4660Sstevel@tonic-gate 		vattrp = NULL;
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	error = secpolicy_vnode_setattr(cr, vp, vap, dv->dv_attr,
4700Sstevel@tonic-gate 					flags, devfs_unlocked_access, dv);
4710Sstevel@tonic-gate 	if (error) {
4720Sstevel@tonic-gate 		dsysdebug(error, ("devfs_setattr %s secpolicy error %d\n",
4730Sstevel@tonic-gate 			dv->dv_name, error));
4740Sstevel@tonic-gate 		goto out;
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	/*
4780Sstevel@tonic-gate 	 * Apply changes to the memory based attribute. This code
4790Sstevel@tonic-gate 	 * is modeled after the tmpfs implementation of memory
4800Sstevel@tonic-gate 	 * based vnodes
4810Sstevel@tonic-gate 	 */
4820Sstevel@tonic-gate 	map = dv->dv_attr;
4830Sstevel@tonic-gate 	mask = vap->va_mask;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	/* Change file access modes. */
4860Sstevel@tonic-gate 	if (mask & AT_MODE) {
4870Sstevel@tonic-gate 		map->va_mode &= S_IFMT;
4880Sstevel@tonic-gate 		map->va_mode |= vap->va_mode & ~S_IFMT;
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 	if (mask & AT_UID)
4910Sstevel@tonic-gate 		map->va_uid = vap->va_uid;
4920Sstevel@tonic-gate 	if (mask & AT_GID)
4930Sstevel@tonic-gate 		map->va_gid = vap->va_gid;
4940Sstevel@tonic-gate 	if (mask & AT_ATIME)
4950Sstevel@tonic-gate 		map->va_atime = vap->va_atime;
4960Sstevel@tonic-gate 	if (mask & AT_MTIME)
4970Sstevel@tonic-gate 		map->va_mtime = vap->va_mtime;
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if (mask & (AT_MODE | AT_UID | AT_GID | AT_MTIME)) {
5000Sstevel@tonic-gate 		gethrestime(&map->va_ctime);
5010Sstevel@tonic-gate 	}
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	/*
5040Sstevel@tonic-gate 	 * A setattr to defaults means we no longer need the
5050Sstevel@tonic-gate 	 * shadow node as a persistent store, unless there
5060Sstevel@tonic-gate 	 * are ACLs.  Otherwise create a shadow node if one
5070Sstevel@tonic-gate 	 * doesn't exist yet.
5080Sstevel@tonic-gate 	 */
5090Sstevel@tonic-gate 	if (persist) {
5100Sstevel@tonic-gate 		if ((dv_setattr_cmp(map, &mp) == 0) &&
5110Sstevel@tonic-gate 		    ((dv->dv_flags & DV_ACL) == 0)) {
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 			if (dv->dv_attrvp) {
5140Sstevel@tonic-gate 				ddv = dv->dv_dotdot;
5150Sstevel@tonic-gate 				ASSERT(ddv->dv_attrvp);
5160Sstevel@tonic-gate 				error = VOP_REMOVE(ddv->dv_attrvp,
5170Sstevel@tonic-gate 				    dv->dv_name, cr);
5180Sstevel@tonic-gate 				dsysdebug(error,
5190Sstevel@tonic-gate 				    ("vop_remove %s %s %d\n",
5200Sstevel@tonic-gate 				    ddv->dv_name, dv->dv_name, error));
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 				if (error == EROFS)
5230Sstevel@tonic-gate 					error = 0;
5240Sstevel@tonic-gate 				VN_RELE(dv->dv_attrvp);
5250Sstevel@tonic-gate 				dv->dv_attrvp = NULL;
5260Sstevel@tonic-gate 			}
5270Sstevel@tonic-gate 			ASSERT(dv->dv_attr);
5280Sstevel@tonic-gate 		} else {
5290Sstevel@tonic-gate 			if (mask & AT_MODE)
5300Sstevel@tonic-gate 				dcmn_err5(("%s persisting mode 0%o\n",
5310Sstevel@tonic-gate 					dv->dv_name, vap->va_mode));
5320Sstevel@tonic-gate 			if (mask & AT_UID)
5330Sstevel@tonic-gate 				dcmn_err5(("%s persisting uid %d\n",
5340Sstevel@tonic-gate 					dv->dv_name, vap->va_uid));
5350Sstevel@tonic-gate 			if (mask & AT_GID)
5360Sstevel@tonic-gate 				dcmn_err5(("%s persisting gid %d\n",
5370Sstevel@tonic-gate 					dv->dv_name, vap->va_gid));
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 			if (dv->dv_attrvp == NULL) {
5400Sstevel@tonic-gate 				dvp = DVTOV(dv->dv_dotdot);
5410Sstevel@tonic-gate 				dv_shadow_node(dvp, dv->dv_name, vp,
5420Sstevel@tonic-gate 				    NULL, NULLVP, cr,
5430Sstevel@tonic-gate 				    DV_SHADOW_CREATE | DV_SHADOW_WRITE_HELD);
5440Sstevel@tonic-gate 			}
5450Sstevel@tonic-gate 			if (dv->dv_attrvp) {
5460Sstevel@tonic-gate 				error = VOP_SETATTR(dv->dv_attrvp,
5470Sstevel@tonic-gate 				    vap, flags, cr, NULL);
5480Sstevel@tonic-gate 				dsysdebug(error, ("vop_setattr %s %d\n",
5490Sstevel@tonic-gate 				    dv->dv_name, error));
5500Sstevel@tonic-gate 			}
5510Sstevel@tonic-gate 			/*
5520Sstevel@tonic-gate 			 * Some file systems may return EROFS for a setattr
5530Sstevel@tonic-gate 			 * on a readonly file system.  In this case save
5540Sstevel@tonic-gate 			 * as our own memory based attribute.
5550Sstevel@tonic-gate 			 * NOTE: ufs is NOT one of these (see ufs_iupdat).
5560Sstevel@tonic-gate 			 */
5570Sstevel@tonic-gate 			if (dv->dv_attr && dv->dv_attrvp && error == 0) {
5580Sstevel@tonic-gate 				vattrp = dv->dv_attr;
5590Sstevel@tonic-gate 				dv->dv_attr = NULL;
5600Sstevel@tonic-gate 			} else if (error == EROFS)
5610Sstevel@tonic-gate 				error = 0;
5620Sstevel@tonic-gate 		}
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate out:
5660Sstevel@tonic-gate 	rw_exit(&dv->dv_contents);
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	if (vattrp)
5690Sstevel@tonic-gate 		kmem_free(vattrp, sizeof (*vattrp));
5700Sstevel@tonic-gate 	if (free_vattr)
5710Sstevel@tonic-gate 		kmem_free(free_vattr, sizeof (*free_vattr));
5720Sstevel@tonic-gate 	return (error);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate static int
5760Sstevel@tonic-gate devfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	switch (cmd) {
5790Sstevel@tonic-gate 	case _PC_ACL_ENABLED:
5800Sstevel@tonic-gate 		/*
5810Sstevel@tonic-gate 		 * We rely on the underlying filesystem for ACLs,
5820Sstevel@tonic-gate 		 * so direct the query for ACL support there.
5830Sstevel@tonic-gate 		 * ACL support isn't relative to the file
5840Sstevel@tonic-gate 		 * and we can't guarantee that the dv node
5850Sstevel@tonic-gate 		 * has an attribute node, so any valid
5860Sstevel@tonic-gate 		 * attribute node will suffice.
5870Sstevel@tonic-gate 		 */
5880Sstevel@tonic-gate 		ASSERT(dvroot);
5890Sstevel@tonic-gate 		ASSERT(dvroot->dv_attrvp);
5900Sstevel@tonic-gate 		return (VOP_PATHCONF(dvroot->dv_attrvp, cmd, valp, cr));
5910Sstevel@tonic-gate 		/*NOTREACHED*/
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	return (fs_pathconf(vp, cmd, valp, cr));
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate /*
5980Sstevel@tonic-gate  * Let avp handle security attributes (acl's).
5990Sstevel@tonic-gate  */
6000Sstevel@tonic-gate static int
6010Sstevel@tonic-gate devfs_getsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
6020Sstevel@tonic-gate     struct cred *cr)
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate 	dvnode_t *dv = VTODV(vp);
6050Sstevel@tonic-gate 	struct vnode *avp;
6060Sstevel@tonic-gate 	int	error;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	dcmn_err2(("devfs_getsecattr %s\n", dv->dv_name));
6090Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	rw_enter(&dv->dv_contents, RW_READER);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	avp = dv->dv_attrvp;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/* fabricate the acl */
6160Sstevel@tonic-gate 	if (avp == NULL) {
6170Sstevel@tonic-gate 		error = fs_fab_acl(vp, vsap, flags, cr);
6180Sstevel@tonic-gate 		rw_exit(&dv->dv_contents);
6190Sstevel@tonic-gate 		return (error);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	error = VOP_GETSECATTR(avp, vsap, flags, cr);
6230Sstevel@tonic-gate 	dsysdebug(error, ("vop_getsecattr %s %d\n", VTODV(vp)->dv_name, error));
6240Sstevel@tonic-gate 	rw_exit(&dv->dv_contents);
6250Sstevel@tonic-gate 	return (error);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate /*
6290Sstevel@tonic-gate  * Set security attributes (acl's)
6300Sstevel@tonic-gate  *
6310Sstevel@tonic-gate  * Note that the dv_contents lock has already been acquired
6320Sstevel@tonic-gate  * by the caller's VOP_RWLOCK.
6330Sstevel@tonic-gate  */
6340Sstevel@tonic-gate static int
6350Sstevel@tonic-gate devfs_setsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
6360Sstevel@tonic-gate     struct cred *cr)
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate 	dvnode_t *dv = VTODV(vp);
6390Sstevel@tonic-gate 	struct vnode *avp;
6400Sstevel@tonic-gate 	int	error;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	dcmn_err2(("devfs_setsecattr %s\n", dv->dv_name));
6430Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
6440Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&dv->dv_contents));
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	/*
6470Sstevel@tonic-gate 	 * Not a supported operation on drivers not providing
6480Sstevel@tonic-gate 	 * file system based permissions.
6490Sstevel@tonic-gate 	 */
6500Sstevel@tonic-gate 	if (dv->dv_flags & DV_NO_FSPERM)
6510Sstevel@tonic-gate 		return (ENOTSUP);
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	/*
6540Sstevel@tonic-gate 	 * To complete, the setsecattr requires an underlying attribute node.
6550Sstevel@tonic-gate 	 */
6560Sstevel@tonic-gate 	if (dv->dv_attrvp == NULL) {
6570Sstevel@tonic-gate 		ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
6580Sstevel@tonic-gate 		dv_shadow_node(DVTOV(dv->dv_dotdot), dv->dv_name, vp,
6590Sstevel@tonic-gate 		    NULL, NULLVP, cr, DV_SHADOW_CREATE | DV_SHADOW_WRITE_HELD);
6600Sstevel@tonic-gate 	}
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	if ((avp = dv->dv_attrvp) == NULL) {
6630Sstevel@tonic-gate 		dcmn_err2(("devfs_setsecattr %s: "
6640Sstevel@tonic-gate 		    "cannot construct attribute node\n", dv->dv_name));
6650Sstevel@tonic-gate 		return (fs_nosys());
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	/*
6690Sstevel@tonic-gate 	 * The acl(2) system call issues a VOP_RWLOCK before setting an ACL.
6700Sstevel@tonic-gate 	 * Since backing file systems expect the lock to be held before seeing
6710Sstevel@tonic-gate 	 * a VOP_SETSECATTR ACL, we need to issue the VOP_RWLOCK to the backing
6720Sstevel@tonic-gate 	 * store before forwarding the ACL.
6730Sstevel@tonic-gate 	 */
6740Sstevel@tonic-gate 	(void) VOP_RWLOCK(avp, V_WRITELOCK_TRUE, NULL);
6750Sstevel@tonic-gate 	error = VOP_SETSECATTR(avp, vsap, flags, cr);
6760Sstevel@tonic-gate 	dsysdebug(error, ("vop_setsecattr %s %d\n", VTODV(vp)->dv_name, error));
6770Sstevel@tonic-gate 	VOP_RWUNLOCK(avp, V_WRITELOCK_TRUE, NULL);
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	/*
680789Sahrens 	 * Set DV_ACL if we have a non-trivial set of ACLs.  It is not
681789Sahrens 	 * necessary to hold VOP_RWLOCK since fs_acl_nontrivial only does
682789Sahrens 	 * VOP_GETSECATTR calls.
6830Sstevel@tonic-gate 	 */
684789Sahrens 	if (fs_acl_nontrivial(avp, cr))
6850Sstevel@tonic-gate 		dv->dv_flags |= DV_ACL;
6860Sstevel@tonic-gate 	return (error);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate /*
6900Sstevel@tonic-gate  * This function is used for secpolicy_setattr().  It must call an
6910Sstevel@tonic-gate  * access() like function while it is already holding the
6920Sstevel@tonic-gate  * dv_contents lock.  We only care about this when dv_attr != NULL;
6930Sstevel@tonic-gate  * so the unlocked access call only concerns itself with that
6940Sstevel@tonic-gate  * particular branch of devfs_access().
6950Sstevel@tonic-gate  */
6960Sstevel@tonic-gate static int
6970Sstevel@tonic-gate devfs_unlocked_access(void *vdv, int mode, struct cred *cr)
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate 	struct dv_node *dv = vdv;
7000Sstevel@tonic-gate 	int shift = 0;
7010Sstevel@tonic-gate 	uid_t owner = dv->dv_attr->va_uid;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	/* Check access based on owner, group and public permissions. */
7040Sstevel@tonic-gate 	if (crgetuid(cr) != owner) {
7050Sstevel@tonic-gate 		shift += 3;
7060Sstevel@tonic-gate 		if (groupmember(dv->dv_attr->va_gid, cr) == 0)
7070Sstevel@tonic-gate 			shift += 3;
7080Sstevel@tonic-gate 	}
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	/* compute missing mode bits */
7110Sstevel@tonic-gate 	mode &= ~(dv->dv_attr->va_mode << shift);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (mode == 0)
7140Sstevel@tonic-gate 		return (0);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	return (secpolicy_vnode_access(cr, DVTOV(dv), owner, mode));
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate static int
7200Sstevel@tonic-gate devfs_access(struct vnode *vp, int mode, int flags, struct cred *cr)
7210Sstevel@tonic-gate {
7220Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
7230Sstevel@tonic-gate 	int		res;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	dcmn_err2(("devfs_access %s\n", dv->dv_name));
7260Sstevel@tonic-gate 	ASSERT(dv->dv_attr || dv->dv_attrvp);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	/* restrict console access to privileged processes */
7290Sstevel@tonic-gate 	if ((vp->v_rdev == rconsdev) && secpolicy_console(cr) != 0) {
7300Sstevel@tonic-gate 		return (EACCES);
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	if (dv->dv_attr && ((dv->dv_flags & DV_ACL) == 0)) {
7340Sstevel@tonic-gate 		rw_enter(&dv->dv_contents, RW_READER);
7350Sstevel@tonic-gate 		if (dv->dv_attr) {
7360Sstevel@tonic-gate 			res = devfs_unlocked_access(dv, mode, cr);
7370Sstevel@tonic-gate 			rw_exit(&dv->dv_contents);
7380Sstevel@tonic-gate 			return (res);
7390Sstevel@tonic-gate 		}
7400Sstevel@tonic-gate 		rw_exit(&dv->dv_contents);
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate 	return (VOP_ACCESS(dv->dv_attrvp, mode, flags, cr));
7430Sstevel@tonic-gate }
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate /*
7460Sstevel@tonic-gate  * Lookup
7470Sstevel@tonic-gate  *
7480Sstevel@tonic-gate  * Given the directory vnode and the name of the component, return
7490Sstevel@tonic-gate  * the corresponding held vnode for that component.
7500Sstevel@tonic-gate  *
7510Sstevel@tonic-gate  * Of course in these fictional filesystems, nothing's ever quite
7520Sstevel@tonic-gate  * -that- simple.
7530Sstevel@tonic-gate  *
7540Sstevel@tonic-gate  * devfs name	type		shadow (fs attributes)	type	comments
7550Sstevel@tonic-gate  * -------------------------------------------------------------------------
7560Sstevel@tonic-gate  * drv[@addr]	VDIR		drv[@addr]		VDIR	nexus driver
7570Sstevel@tonic-gate  * drv[@addr]:m	VCHR/VBLK	drv[@addr]:m		VREG	leaf driver
7580Sstevel@tonic-gate  * drv[@addr]	VCHR/VBLK	drv[@addr]:.default	VREG	leaf driver
7590Sstevel@tonic-gate  * -------------------------------------------------------------------------
7600Sstevel@tonic-gate  *
7610Sstevel@tonic-gate  * The following names are reserved for the attribute filesystem (which
7620Sstevel@tonic-gate  * could easily be another layer on top of this one - we simply need to
7630Sstevel@tonic-gate  * hold the vnode of the thing we're looking at)
7640Sstevel@tonic-gate  *
7650Sstevel@tonic-gate  * attr name	type		shadow (fs attributes)	type	comments
7660Sstevel@tonic-gate  * -------------------------------------------------------------------------
7670Sstevel@tonic-gate  * drv[@addr]	VDIR		-			-	attribute dir
7680Sstevel@tonic-gate  * minorname	VDIR		-			-	minorname
7690Sstevel@tonic-gate  * attribute	VREG		-			-	attribute
7700Sstevel@tonic-gate  * -------------------------------------------------------------------------
7710Sstevel@tonic-gate  *
7720Sstevel@tonic-gate  * Examples:
7730Sstevel@tonic-gate  *
7740Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:zero		VCHR
7750Sstevel@tonic-gate  *	shadow:/.devices/.../mm@0:zero		VREG, fs attrs
7760Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:/zero/attr	VREG, driver attribute
7770Sstevel@tonic-gate  *
7780Sstevel@tonic-gate  *	devfs:/devices/.../sd@0,0:a		VBLK
7790Sstevel@tonic-gate  *	shadow:/.devices/.../sd@0,0:a		VREG, fs attrs
7800Sstevel@tonic-gate  *	devfs:/devices/.../sd@0,0:/a/.type	VREG, "ddi_block:chan"
7810Sstevel@tonic-gate  *
7820Sstevel@tonic-gate  *	devfs:/devices/.../mm@0			VCHR
7830Sstevel@tonic-gate  *	shadow:/.devices/.../mm@0:.default	VREG, fs attrs
7840Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:/.default/attr	VREG, driver attribute
7850Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:/.default/.type	VREG, "ddi_pseudo"
7860Sstevel@tonic-gate  *
7870Sstevel@tonic-gate  *	devfs:/devices/.../obio			VDIR
7880Sstevel@tonic-gate  *	shadow:/devices/.../obio		VDIR, needed for fs attrs.
7890Sstevel@tonic-gate  *	devfs:/devices/.../obio:/.default/attr	VDIR, driver attribute
7900Sstevel@tonic-gate  *
7910Sstevel@tonic-gate  * We also need to be able deal with "old" devices that have gone away,
7920Sstevel@tonic-gate  * though I think that provided we return them with readdir, they can
7930Sstevel@tonic-gate  * be removed (i.e. they don't have to respond to lookup, though it might
7940Sstevel@tonic-gate  * be weird if they didn't ;-)
7950Sstevel@tonic-gate  *
7960Sstevel@tonic-gate  * Lookup has side-effects.
7970Sstevel@tonic-gate  *
7980Sstevel@tonic-gate  * - It will create directories and fs attribute files in the shadow hierarchy.
7990Sstevel@tonic-gate  * - It should cause non-SID devices to be probed (ask the parent nexi).
8000Sstevel@tonic-gate  */
8010Sstevel@tonic-gate /*ARGSUSED3*/
8020Sstevel@tonic-gate static int
8030Sstevel@tonic-gate devfs_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
8040Sstevel@tonic-gate     struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred)
8050Sstevel@tonic-gate {
8060Sstevel@tonic-gate 	ASSERT(dvp->v_type == VDIR);
8070Sstevel@tonic-gate 	dcmn_err2(("devfs_lookup: %s\n", nm));
8080Sstevel@tonic-gate 	return (dv_find(VTODV(dvp), nm, vpp, pnp, rdir, cred, 0));
8090Sstevel@tonic-gate }
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate /*
8120Sstevel@tonic-gate  * devfs nodes can't really be created directly by userland - however,
8130Sstevel@tonic-gate  * we do allow creates to find existing nodes:
8140Sstevel@tonic-gate  *
8150Sstevel@tonic-gate  * - any create fails if the node doesn't exist - EROFS.
8160Sstevel@tonic-gate  * - creating an existing directory read-only succeeds, otherwise EISDIR.
8170Sstevel@tonic-gate  * - exclusive creates fail if the node already exists - EEXIST.
8180Sstevel@tonic-gate  * - failure to create the snode for an existing device - ENOSYS.
8190Sstevel@tonic-gate  */
8200Sstevel@tonic-gate /*ARGSUSED2*/
8210Sstevel@tonic-gate static int
8220Sstevel@tonic-gate devfs_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl,
8230Sstevel@tonic-gate     int mode, struct vnode **vpp, struct cred *cred, int flag)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate 	int error;
8260Sstevel@tonic-gate 	struct vnode *vp;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	dcmn_err2(("devfs_create %s\n", nm));
8290Sstevel@tonic-gate 	error = dv_find(VTODV(dvp), nm, &vp, NULL, NULLVP, cred, 0);
8300Sstevel@tonic-gate 	if (error == 0) {
8310Sstevel@tonic-gate 		if (excl == EXCL)
8320Sstevel@tonic-gate 			error = EEXIST;
8330Sstevel@tonic-gate 		else if (vp->v_type == VDIR && (mode & VWRITE))
8340Sstevel@tonic-gate 			error = EISDIR;
8350Sstevel@tonic-gate 		else
8360Sstevel@tonic-gate 			error = VOP_ACCESS(vp, mode, 0, cred);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		if (error) {
8390Sstevel@tonic-gate 			VN_RELE(vp);
8400Sstevel@tonic-gate 		} else
8410Sstevel@tonic-gate 			*vpp = vp;
8420Sstevel@tonic-gate 	} else if (error == ENOENT)
8430Sstevel@tonic-gate 		error = EROFS;
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	return (error);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate /*
8490Sstevel@tonic-gate  * If DV_BUILD is set, we call into nexus driver to do a BUS_CONFIG_ALL.
8500Sstevel@tonic-gate  * Otherwise, simply return cached dv_node's. Hotplug code always call
8510Sstevel@tonic-gate  * devfs_clean() to invalid the dv_node cache.
8520Sstevel@tonic-gate  */
8530Sstevel@tonic-gate static int
8540Sstevel@tonic-gate devfs_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred, int *eofp)
8550Sstevel@tonic-gate {
8560Sstevel@tonic-gate 	struct dv_node *ddv, *dv;
8570Sstevel@tonic-gate 	struct dirent64 *de, *bufp;
8580Sstevel@tonic-gate 	offset_t diroff;
8590Sstevel@tonic-gate 	offset_t	soff;
8600Sstevel@tonic-gate 	size_t reclen, movesz;
8610Sstevel@tonic-gate 	int error;
8620Sstevel@tonic-gate 	struct vattr va;
8630Sstevel@tonic-gate 	size_t bufsz;
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	ddv = VTODV(dvp);
8660Sstevel@tonic-gate 	dcmn_err2(("devfs_readdir %s: offset %lld len %ld\n",
8670Sstevel@tonic-gate 	    ddv->dv_name, uiop->uio_loffset, uiop->uio_iov->iov_len));
8680Sstevel@tonic-gate 	ASSERT(ddv->dv_attr || ddv->dv_attrvp);
8690Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&ddv->dv_contents));
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	if (uiop->uio_loffset >= MAXOFF_T) {
8720Sstevel@tonic-gate 		if (eofp)
8730Sstevel@tonic-gate 			*eofp = 1;
8740Sstevel@tonic-gate 		return (0);
8750Sstevel@tonic-gate 	}
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	if (uiop->uio_iovcnt != 1)
8780Sstevel@tonic-gate 		return (EINVAL);
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	if (dvp->v_type != VDIR)
8810Sstevel@tonic-gate 		return (ENOTDIR);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	/* Load the initial contents */
8840Sstevel@tonic-gate 	if (ddv->dv_flags & DV_BUILD) {
8850Sstevel@tonic-gate 		if (!rw_tryupgrade(&ddv->dv_contents)) {
8860Sstevel@tonic-gate 			rw_exit(&ddv->dv_contents);
8870Sstevel@tonic-gate 			rw_enter(&ddv->dv_contents, RW_WRITER);
8880Sstevel@tonic-gate 		}
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 		/* recheck and fill */
8910Sstevel@tonic-gate 		if (ddv->dv_flags & DV_BUILD)
8920Sstevel@tonic-gate 			dv_filldir(ddv);
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 		rw_downgrade(&ddv->dv_contents);
8950Sstevel@tonic-gate 	}
8960Sstevel@tonic-gate 
8973133Sjg 	soff = uiop->uio_loffset;
8980Sstevel@tonic-gate 	bufsz = uiop->uio_iov->iov_len;
8990Sstevel@tonic-gate 	de = bufp = kmem_alloc(bufsz, KM_SLEEP);
9000Sstevel@tonic-gate 	movesz = 0;
9010Sstevel@tonic-gate 	dv = (struct dv_node *)-1;
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	/*
9040Sstevel@tonic-gate 	 * Move as many entries into the uio structure as it will take.
9050Sstevel@tonic-gate 	 * Special case "." and "..".
9060Sstevel@tonic-gate 	 */
9070Sstevel@tonic-gate 	diroff = 0;
9080Sstevel@tonic-gate 	if (soff == 0) {				/* . */
9090Sstevel@tonic-gate 		reclen = DIRENT64_RECLEN(strlen("."));
9100Sstevel@tonic-gate 		if ((movesz + reclen) > bufsz)
9110Sstevel@tonic-gate 			goto full;
9120Sstevel@tonic-gate 		de->d_ino = (ino64_t)ddv->dv_ino;
9130Sstevel@tonic-gate 		de->d_off = (off64_t)diroff + 1;
9140Sstevel@tonic-gate 		de->d_reclen = (ushort_t)reclen;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		(void) strncpy(de->d_name, ".", DIRENT64_NAMELEN(reclen));
9190Sstevel@tonic-gate 		movesz += reclen;
9203133Sjg 		de = (dirent64_t *)(intptr_t)((char *)de + reclen);
9210Sstevel@tonic-gate 		dcmn_err3(("devfs_readdir: A: diroff %lld, soff %lld: '%s' "
9220Sstevel@tonic-gate 		    "reclen %lu\n", diroff, soff, ".", reclen));
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	diroff++;
9260Sstevel@tonic-gate 	if (soff <= 1) {				/* .. */
9270Sstevel@tonic-gate 		reclen = DIRENT64_RECLEN(strlen(".."));
9280Sstevel@tonic-gate 		if ((movesz + reclen) > bufsz)
9290Sstevel@tonic-gate 			goto full;
9300Sstevel@tonic-gate 		de->d_ino = (ino64_t)ddv->dv_dotdot->dv_ino;
9310Sstevel@tonic-gate 		de->d_off = (off64_t)diroff + 1;
9320Sstevel@tonic-gate 		de->d_reclen = (ushort_t)reclen;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 		(void) strncpy(de->d_name, "..", DIRENT64_NAMELEN(reclen));
9370Sstevel@tonic-gate 		movesz += reclen;
9383133Sjg 		de = (dirent64_t *)(intptr_t)((char *)de + reclen);
9390Sstevel@tonic-gate 		dcmn_err3(("devfs_readdir: B: diroff %lld, soff %lld: '%s' "
9400Sstevel@tonic-gate 		    "reclen %lu\n", diroff, soff, "..", reclen));
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	diroff++;
9440Sstevel@tonic-gate 	for (dv = ddv->dv_dot; dv; dv = dv->dv_next, diroff++) {
9450Sstevel@tonic-gate 		/*
9460Sstevel@tonic-gate 		 * although DDM_INTERNAL_PATH minor nodes are skipped for
9470Sstevel@tonic-gate 		 * readdirs outside the kernel, they still occupy directory
9480Sstevel@tonic-gate 		 * offsets
9490Sstevel@tonic-gate 		 */
9500Sstevel@tonic-gate 		if (diroff < soff ||
9510Sstevel@tonic-gate 		    ((dv->dv_flags & DV_INTERNAL) && (cred != kcred)))
9520Sstevel@tonic-gate 			continue;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 		reclen = DIRENT64_RECLEN(strlen(dv->dv_name));
9550Sstevel@tonic-gate 		if ((movesz + reclen) > bufsz) {
9560Sstevel@tonic-gate 			dcmn_err3(("devfs_readdir: C: diroff "
9570Sstevel@tonic-gate 			    "%lld, soff %lld: '%s' reclen %lu\n",
9580Sstevel@tonic-gate 			    diroff, soff, dv->dv_name, reclen));
9590Sstevel@tonic-gate 			goto full;
9600Sstevel@tonic-gate 		}
9610Sstevel@tonic-gate 		de->d_ino = (ino64_t)dv->dv_ino;
9620Sstevel@tonic-gate 		de->d_off = (off64_t)diroff + 1;
9630Sstevel@tonic-gate 		de->d_reclen = (ushort_t)reclen;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 		ASSERT(strlen(dv->dv_name) + 1 <=
9680Sstevel@tonic-gate 		    DIRENT64_NAMELEN(reclen));
9690Sstevel@tonic-gate 		(void) strncpy(de->d_name, dv->dv_name,
9700Sstevel@tonic-gate 		    DIRENT64_NAMELEN(reclen));
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 		movesz += reclen;
9733133Sjg 		de = (dirent64_t *)(intptr_t)((char *)de + reclen);
9740Sstevel@tonic-gate 		dcmn_err4(("devfs_readdir: D: diroff "
9750Sstevel@tonic-gate 		    "%lld, soff %lld: '%s' reclen %lu\n", diroff, soff,
9760Sstevel@tonic-gate 		    dv->dv_name, reclen));
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	/* the buffer is full, or we exhausted everything */
9800Sstevel@tonic-gate full:	dcmn_err3(("devfs_readdir: moving %lu bytes: "
9810Sstevel@tonic-gate 	    "diroff %lld, soff %lld, dv %p\n",
9820Sstevel@tonic-gate 	    movesz, diroff, soff, (void *)dv));
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	if ((movesz == 0) && dv)
9850Sstevel@tonic-gate 		error = EINVAL;		/* cannot be represented */
9860Sstevel@tonic-gate 	else {
9870Sstevel@tonic-gate 		error = uiomove(bufp, movesz, UIO_READ, uiop);
9880Sstevel@tonic-gate 		if (error == 0) {
9890Sstevel@tonic-gate 			if (eofp)
9900Sstevel@tonic-gate 				*eofp = dv ? 0 : 1;
9913133Sjg 			uiop->uio_loffset = diroff;
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		va.va_mask = AT_ATIME;
9950Sstevel@tonic-gate 		gethrestime(&va.va_atime);
9960Sstevel@tonic-gate 		rw_exit(&ddv->dv_contents);
9970Sstevel@tonic-gate 		(void) devfs_setattr(dvp, &va, 0, cred, NULL);
9980Sstevel@tonic-gate 		rw_enter(&ddv->dv_contents, RW_READER);
9990Sstevel@tonic-gate 	}
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	kmem_free(bufp, bufsz);
10020Sstevel@tonic-gate 	return (error);
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate /*ARGSUSED*/
10060Sstevel@tonic-gate static int
10070Sstevel@tonic-gate devfs_fsync(struct vnode *vp, int syncflag, struct cred *cred)
10080Sstevel@tonic-gate {
10090Sstevel@tonic-gate 	/*
10100Sstevel@tonic-gate 	 * Message goes to console only. Otherwise, the message
10110Sstevel@tonic-gate 	 * causes devfs_fsync to be invoked again... infinite loop
10120Sstevel@tonic-gate 	 */
10130Sstevel@tonic-gate 	dcmn_err2(("devfs_fsync %s\n", VTODV(vp)->dv_name));
10140Sstevel@tonic-gate 	return (0);
10150Sstevel@tonic-gate }
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate /*
10180Sstevel@tonic-gate  * Normally, we leave the dv_node here at count of 0.
10190Sstevel@tonic-gate  * The node will be destroyed when dv_cleandir() is called.
10200Sstevel@tonic-gate  *
10210Sstevel@tonic-gate  * Stale dv_node's are already unlinked from the fs tree,
10220Sstevel@tonic-gate  * so dv_cleandir() won't find them. We destroy such nodes
10230Sstevel@tonic-gate  * immediately.
10240Sstevel@tonic-gate  */
10250Sstevel@tonic-gate /*ARGSUSED1*/
10260Sstevel@tonic-gate static void
10270Sstevel@tonic-gate devfs_inactive(struct vnode *vp, struct cred *cred)
10280Sstevel@tonic-gate {
10290Sstevel@tonic-gate 	int destroy;
10300Sstevel@tonic-gate 	struct dv_node *dv = VTODV(vp);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	dcmn_err2(("devfs_inactive: %s\n", dv->dv_name));
10330Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
10340Sstevel@tonic-gate 	ASSERT(vp->v_count >= 1);
10350Sstevel@tonic-gate 	--vp->v_count;
10360Sstevel@tonic-gate 	destroy = (DV_STALE(dv) && vp->v_count == 0);
10370Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	/* stale nodes cannot be rediscovered, destroy it here */
10400Sstevel@tonic-gate 	if (destroy)
10410Sstevel@tonic-gate 		dv_destroy(dv, 0);
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate /*
10450Sstevel@tonic-gate  * XXX Why do we need this?  NFS mounted /dev directories?
10460Sstevel@tonic-gate  * XXX Talk to peter staubach about this.
10470Sstevel@tonic-gate  */
10480Sstevel@tonic-gate static int
10490Sstevel@tonic-gate devfs_fid(struct vnode *vp, struct fid *fidp)
10500Sstevel@tonic-gate {
10510Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
10520Sstevel@tonic-gate 	struct dv_fid	*dv_fid;
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	if (fidp->fid_len < (sizeof (struct dv_fid) - sizeof (ushort_t))) {
10550Sstevel@tonic-gate 		fidp->fid_len = sizeof (struct dv_fid) - sizeof (ushort_t);
10560Sstevel@tonic-gate 		return (ENOSPC);
10570Sstevel@tonic-gate 	}
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	dv_fid = (struct dv_fid *)fidp;
10600Sstevel@tonic-gate 	bzero(dv_fid, sizeof (struct dv_fid));
10610Sstevel@tonic-gate 	dv_fid->dvfid_len = (int)sizeof (struct dv_fid) - sizeof (ushort_t);
10620Sstevel@tonic-gate 	dv_fid->dvfid_ino = dv->dv_ino;
10630Sstevel@tonic-gate 	/* dv_fid->dvfid_gen = dv->tn_gen; XXX ? */
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	return (0);
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate /*
10690Sstevel@tonic-gate  * This pair of routines bracket all VOP_READ, VOP_WRITE
10700Sstevel@tonic-gate  * and VOP_READDIR requests.  The contents lock stops things
10710Sstevel@tonic-gate  * moving around while we're looking at them.
10720Sstevel@tonic-gate  *
10730Sstevel@tonic-gate  * Also used by file and record locking.
10740Sstevel@tonic-gate  */
10750Sstevel@tonic-gate /*ARGSUSED2*/
10760Sstevel@tonic-gate static int
10770Sstevel@tonic-gate devfs_rwlock(struct vnode *vp, int write_flag, caller_context_t *ct)
10780Sstevel@tonic-gate {
10790Sstevel@tonic-gate 	dcmn_err2(("devfs_rwlock %s\n", VTODV(vp)->dv_name));
10800Sstevel@tonic-gate 	rw_enter(&VTODV(vp)->dv_contents, write_flag ? RW_WRITER : RW_READER);
10810Sstevel@tonic-gate 	return (write_flag);
10820Sstevel@tonic-gate }
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate /*ARGSUSED1*/
10850Sstevel@tonic-gate static void
10860Sstevel@tonic-gate devfs_rwunlock(struct vnode *vp, int write_flag, caller_context_t *ct)
10870Sstevel@tonic-gate {
10880Sstevel@tonic-gate 	dcmn_err2(("devfs_rwunlock %s\n", VTODV(vp)->dv_name));
10890Sstevel@tonic-gate 	rw_exit(&VTODV(vp)->dv_contents);
10900Sstevel@tonic-gate }
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate /*
10930Sstevel@tonic-gate  * XXX	Should probably do a better job of computing the maximum
10940Sstevel@tonic-gate  *	offset available in the directory.
10950Sstevel@tonic-gate  */
10960Sstevel@tonic-gate /*ARGSUSED1*/
10970Sstevel@tonic-gate static int
10980Sstevel@tonic-gate devfs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp)
10990Sstevel@tonic-gate {
11000Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
11010Sstevel@tonic-gate 	dcmn_err2(("devfs_seek %s\n", VTODV(vp)->dv_name));
11020Sstevel@tonic-gate 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
11030Sstevel@tonic-gate }
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate vnodeops_t *dv_vnodeops;
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate const fs_operation_def_t dv_vnodeops_template[] = {
1108*3898Srsb 	VOPNAME_OPEN,		{ .vop_open = devfs_open },
1109*3898Srsb 	VOPNAME_CLOSE,		{ .vop_close = devfs_close },
1110*3898Srsb 	VOPNAME_READ,		{ .vop_read = devfs_read },
1111*3898Srsb 	VOPNAME_WRITE,		{ .vop_write = devfs_write },
1112*3898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = devfs_ioctl },
1113*3898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = devfs_getattr },
1114*3898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = devfs_setattr },
1115*3898Srsb 	VOPNAME_ACCESS,		{ .vop_access = devfs_access },
1116*3898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = devfs_lookup },
1117*3898Srsb 	VOPNAME_CREATE,		{ .vop_create = devfs_create },
1118*3898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = devfs_readdir },
1119*3898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = devfs_fsync },
1120*3898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = devfs_inactive },
1121*3898Srsb 	VOPNAME_FID,		{ .vop_fid = devfs_fid },
1122*3898Srsb 	VOPNAME_RWLOCK,		{ .vop_rwlock = devfs_rwlock },
1123*3898Srsb 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = devfs_rwunlock },
1124*3898Srsb 	VOPNAME_SEEK,		{ .vop_seek = devfs_seek },
1125*3898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = devfs_pathconf },
1126*3898Srsb 	VOPNAME_DISPOSE,	{ .error = fs_error },
1127*3898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = devfs_setsecattr },
1128*3898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = devfs_getsecattr },
1129*3898Srsb 	NULL,			NULL
11300Sstevel@tonic-gate };
1131