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