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