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 /*
223898Srsb  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * vnode ops for the devfs
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  * For leaf vnode special files (VCHR|VBLK) specfs will always see the VOP
320Sstevel@tonic-gate  * first because dv_find always performs leaf vnode substitution, returning
330Sstevel@tonic-gate  * a specfs vnode with an s_realvp pointing to the devfs leaf vnode. This
340Sstevel@tonic-gate  * means that the only leaf special file VOP operations that devfs will see
350Sstevel@tonic-gate  * after VOP_LOOKUP are the ones that specfs forwards.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/param.h>
400Sstevel@tonic-gate #include <sys/t_lock.h>
410Sstevel@tonic-gate #include <sys/systm.h>
420Sstevel@tonic-gate #include <sys/sysmacros.h>
430Sstevel@tonic-gate #include <sys/user.h>
440Sstevel@tonic-gate #include <sys/time.h>
450Sstevel@tonic-gate #include <sys/vfs.h>
460Sstevel@tonic-gate #include <sys/vnode.h>
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
75*5331Samw devfs_open(struct vnode **vpp, int flag, struct cred *cred,
76*5331Samw     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,
92*5331Samw     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,
141*5331Samw     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
163*5331Samw devfs_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr,
164*5331Samw     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 
1830Sstevel@tonic-gate 	if (dv->dv_attr) {
1840Sstevel@tonic-gate 		/*
1850Sstevel@tonic-gate 		 * obtain from the memory version of attribute.
1860Sstevel@tonic-gate 		 * preserve mask for those that optimize.
1870Sstevel@tonic-gate 		 * devfs specific fields are already merged on creation.
1880Sstevel@tonic-gate 		 */
1890Sstevel@tonic-gate 		mask = vap->va_mask;
1900Sstevel@tonic-gate 		*vap = *dv->dv_attr;
1910Sstevel@tonic-gate 		vap->va_mask = mask;
1920Sstevel@tonic-gate 	} else {
1930Sstevel@tonic-gate 		/* obtain from attribute store and merge */
194*5331Samw 		error = VOP_GETATTR(dv->dv_attrvp, vap, flags, cr, ct);
1950Sstevel@tonic-gate 		dsysdebug(error, ("vop_getattr %s %d\n", dv->dv_name, error));
1960Sstevel@tonic-gate 		dv_vattr_merge(dv, vap);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	/*
2000Sstevel@tonic-gate 	 * Restrict the permissions of the node fronting the console
2010Sstevel@tonic-gate 	 * to 0600 with root as the owner.  This prevents a non-root
2020Sstevel@tonic-gate 	 * user from gaining access to a serial terminal (like /dev/term/a)
2030Sstevel@tonic-gate 	 * which is in reality serving as the console device (/dev/console).
2040Sstevel@tonic-gate 	 */
2050Sstevel@tonic-gate 	if (vp->v_rdev == rconsdev) {
2060Sstevel@tonic-gate 		mode_t	rconsmask = S_IXUSR|S_IRWXG|S_IRWXO;
2070Sstevel@tonic-gate 		vap->va_mode &= (~rconsmask);
2080Sstevel@tonic-gate 		vap->va_uid = 0;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	return (error);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate static int devfs_unlocked_access(void *, int, struct cred *);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate /*ARGSUSED4*/
2170Sstevel@tonic-gate static int
2180Sstevel@tonic-gate devfs_setattr_dir(
2190Sstevel@tonic-gate 	struct dv_node *dv,
2200Sstevel@tonic-gate 	struct vnode *vp,
2210Sstevel@tonic-gate 	struct vattr *vap,
2220Sstevel@tonic-gate 	int flags,
2230Sstevel@tonic-gate 	struct cred *cr)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	struct vattr	*map;
2260Sstevel@tonic-gate 	long int	mask;
2270Sstevel@tonic-gate 	int		error = 0;
2280Sstevel@tonic-gate 	struct vattr	vattr;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	ASSERT(dv->dv_attr || dv->dv_attrvp);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
2330Sstevel@tonic-gate 	ASSERT((dv->dv_flags & DV_NO_FSPERM) == 0);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (vap->va_mask & AT_NOSET)
2360Sstevel@tonic-gate 		return (EINVAL);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/* to ensure consistency, single thread setting of attributes */
2390Sstevel@tonic-gate 	rw_enter(&dv->dv_contents, RW_WRITER);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate again:	if (dv->dv_attr) {
2420Sstevel@tonic-gate 
243*5331Samw 		error = secpolicy_vnode_setattr(cr, vp, vap,
244*5331Samw 				dv->dv_attr, flags, devfs_unlocked_access, dv);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 		if (error)
2470Sstevel@tonic-gate 			goto out;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 		/*
2500Sstevel@tonic-gate 		 * Apply changes to the memory based attribute. This code
2510Sstevel@tonic-gate 		 * is modeled after the tmpfs implementation of memory
2520Sstevel@tonic-gate 		 * based vnodes
2530Sstevel@tonic-gate 		 */
2540Sstevel@tonic-gate 		map = dv->dv_attr;
2550Sstevel@tonic-gate 		mask = vap->va_mask;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 		/* Change file access modes. */
2580Sstevel@tonic-gate 		if (mask & AT_MODE) {
2590Sstevel@tonic-gate 			map->va_mode &= S_IFMT;
2600Sstevel@tonic-gate 			map->va_mode |= vap->va_mode & ~S_IFMT;
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 		if (mask & AT_UID)
2630Sstevel@tonic-gate 			map->va_uid = vap->va_uid;
2640Sstevel@tonic-gate 		if (mask & AT_GID)
2650Sstevel@tonic-gate 			map->va_gid = vap->va_gid;
2660Sstevel@tonic-gate 		if (mask & AT_ATIME)
2670Sstevel@tonic-gate 			map->va_atime = vap->va_atime;
2680Sstevel@tonic-gate 		if (mask & AT_MTIME)
2690Sstevel@tonic-gate 			map->va_mtime = vap->va_mtime;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		if (mask & (AT_MODE | AT_UID | AT_GID | AT_MTIME))
2720Sstevel@tonic-gate 			gethrestime(&map->va_ctime);
2730Sstevel@tonic-gate 	} else {
2740Sstevel@tonic-gate 		/* use the backing attribute store */
2750Sstevel@tonic-gate 		ASSERT(dv->dv_attrvp);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		/*
2780Sstevel@tonic-gate 		 * See if we are changing something we care about
2790Sstevel@tonic-gate 		 * the persistence of - return success if we don't care.
2800Sstevel@tonic-gate 		 */
2810Sstevel@tonic-gate 		if (vap->va_mask & (AT_MODE|AT_UID|AT_GID|AT_ATIME|AT_MTIME)) {
2820Sstevel@tonic-gate 			/* Set the attributes */
2830Sstevel@tonic-gate 			error = VOP_SETATTR(dv->dv_attrvp,
2840Sstevel@tonic-gate 				vap, flags, cr, NULL);
2850Sstevel@tonic-gate 			dsysdebug(error,
2860Sstevel@tonic-gate 				("vop_setattr %s %d\n", dv->dv_name, error));
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 			/*
2890Sstevel@tonic-gate 			 * Some file systems may return EROFS for a setattr
2900Sstevel@tonic-gate 			 * on a readonly file system.  In this case we create
2910Sstevel@tonic-gate 			 * our own memory based attribute.
2920Sstevel@tonic-gate 			 */
2930Sstevel@tonic-gate 			if (error == EROFS) {
2940Sstevel@tonic-gate 				/*
2950Sstevel@tonic-gate 				 * obtain attributes from existing file
2960Sstevel@tonic-gate 				 * that we will modify and switch to memory
2970Sstevel@tonic-gate 				 * based attribute until attribute store is
2980Sstevel@tonic-gate 				 * read/write.
2990Sstevel@tonic-gate 				 */
3000Sstevel@tonic-gate 				vattr = dv_vattr_dir;
301*5331Samw 				if (VOP_GETATTR(dv->dv_attrvp,
302*5331Samw 				    &vattr, flags, cr, NULL) == 0) {
3030Sstevel@tonic-gate 					dv->dv_attr = kmem_alloc(
3040Sstevel@tonic-gate 					    sizeof (struct vattr), KM_SLEEP);
3050Sstevel@tonic-gate 					*dv->dv_attr = vattr;
3060Sstevel@tonic-gate 					dv_vattr_merge(dv, dv->dv_attr);
3070Sstevel@tonic-gate 					goto again;
3080Sstevel@tonic-gate 				}
3090Sstevel@tonic-gate 			}
3100Sstevel@tonic-gate 		}
3110Sstevel@tonic-gate 	}
3120Sstevel@tonic-gate out:
3130Sstevel@tonic-gate 	rw_exit(&dv->dv_contents);
3140Sstevel@tonic-gate 	return (error);
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate  * Compare the uid/gid/mode changes requested for a setattr
3200Sstevel@tonic-gate  * operation with the same details of a node's default minor
3210Sstevel@tonic-gate  * perm information.  Return 0 if identical.
3220Sstevel@tonic-gate  */
3230Sstevel@tonic-gate static int
3240Sstevel@tonic-gate dv_setattr_cmp(struct vattr *map, mperm_t *mp)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	if ((map->va_mode & S_IAMB) != (mp->mp_mode & S_IAMB))
3270Sstevel@tonic-gate 		return (1);
3280Sstevel@tonic-gate 	if (map->va_uid != mp->mp_uid)
3290Sstevel@tonic-gate 		return (1);
3300Sstevel@tonic-gate 	if (map->va_gid != mp->mp_gid)
3310Sstevel@tonic-gate 		return (1);
3320Sstevel@tonic-gate 	return (0);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate /*ARGSUSED4*/
3370Sstevel@tonic-gate static int
3380Sstevel@tonic-gate devfs_setattr(
3390Sstevel@tonic-gate 	struct vnode *vp,
3400Sstevel@tonic-gate 	struct vattr *vap,
3410Sstevel@tonic-gate 	int flags,
3420Sstevel@tonic-gate 	struct cred *cr,
3430Sstevel@tonic-gate 	caller_context_t *ct)
3440Sstevel@tonic-gate {
3450Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
3460Sstevel@tonic-gate 	struct dv_node	*ddv;
3470Sstevel@tonic-gate 	struct vnode	*dvp;
3480Sstevel@tonic-gate 	struct vattr	*map;
3490Sstevel@tonic-gate 	long int	mask;
3500Sstevel@tonic-gate 	int		error = 0;
3510Sstevel@tonic-gate 	struct vattr	*free_vattr = NULL;
3520Sstevel@tonic-gate 	struct vattr	*vattrp = NULL;
3530Sstevel@tonic-gate 	mperm_t		mp;
3540Sstevel@tonic-gate 	int		persist;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	/*
3570Sstevel@tonic-gate 	 * Message goes to console only. Otherwise, the message
3580Sstevel@tonic-gate 	 * causes devfs_getattr to be invoked again... infinite loop
3590Sstevel@tonic-gate 	 */
3600Sstevel@tonic-gate 	dcmn_err2(("?devfs_setattr %s\n", dv->dv_name));
3610Sstevel@tonic-gate 	ASSERT(dv->dv_attr || dv->dv_attrvp);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	if (!(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK)) {
3640Sstevel@tonic-gate 		cmn_err(CE_WARN,	/* panic ? */
3650Sstevel@tonic-gate 		    "?%s: getattr on vnode type %d", dvnm, vp->v_type);
3660Sstevel@tonic-gate 		return (ENOENT);
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	if (vap->va_mask & AT_NOSET)
3700Sstevel@tonic-gate 		return (EINVAL);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	/*
3730Sstevel@tonic-gate 	 * If we are changing something we don't care about
3740Sstevel@tonic-gate 	 * the persistence of, return success.
3750Sstevel@tonic-gate 	 */
3760Sstevel@tonic-gate 	if ((vap->va_mask &
3770Sstevel@tonic-gate 	    (AT_MODE|AT_UID|AT_GID|AT_ATIME|AT_MTIME)) == 0)
3780Sstevel@tonic-gate 		return (0);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	/*
3810Sstevel@tonic-gate 	 * If driver overrides fs perm, disallow chmod
3820Sstevel@tonic-gate 	 * and do not create attribute nodes.
3830Sstevel@tonic-gate 	 */
3840Sstevel@tonic-gate 	if (dv->dv_flags & DV_NO_FSPERM) {
3850Sstevel@tonic-gate 		ASSERT(dv->dv_attr);
3860Sstevel@tonic-gate 		if (vap->va_mask & (AT_MODE | AT_UID | AT_GID))
3870Sstevel@tonic-gate 			return (EPERM);
3880Sstevel@tonic-gate 		if ((vap->va_mask & (AT_ATIME|AT_MTIME)) == 0)
3890Sstevel@tonic-gate 			return (0);
3900Sstevel@tonic-gate 		rw_enter(&dv->dv_contents, RW_WRITER);
3910Sstevel@tonic-gate 		if (vap->va_mask & AT_ATIME)
3920Sstevel@tonic-gate 			dv->dv_attr->va_atime = vap->va_atime;
3930Sstevel@tonic-gate 		if (vap->va_mask & AT_MTIME)
3940Sstevel@tonic-gate 			dv->dv_attr->va_mtime = vap->va_mtime;
3950Sstevel@tonic-gate 		rw_exit(&dv->dv_contents);
3960Sstevel@tonic-gate 		return (0);
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	/*
4000Sstevel@tonic-gate 	 * Directories are always created but device nodes are
4010Sstevel@tonic-gate 	 * only used to persist non-default permissions.
4020Sstevel@tonic-gate 	 */
4030Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
4040Sstevel@tonic-gate 		ASSERT(dv->dv_attr || dv->dv_attrvp);
4050Sstevel@tonic-gate 		return (devfs_setattr_dir(dv, vp, vap, flags, cr));
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	/*
4090Sstevel@tonic-gate 	 * Allocate now before we take any locks
4100Sstevel@tonic-gate 	 */
4110Sstevel@tonic-gate 	vattrp = kmem_zalloc(sizeof (*vattrp), KM_SLEEP);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	/* to ensure consistency, single thread setting of attributes */
4140Sstevel@tonic-gate 	rw_enter(&dv->dv_contents, RW_WRITER);
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/*
4170Sstevel@tonic-gate 	 * We don't need to create an attribute node
4180Sstevel@tonic-gate 	 * to persist access or modification times.
4190Sstevel@tonic-gate 	 */
4200Sstevel@tonic-gate 	persist = (vap->va_mask & (AT_MODE | AT_UID | AT_GID));
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	/*
4230Sstevel@tonic-gate 	 * If persisting something, get the default permissions
4240Sstevel@tonic-gate 	 * for this minor to compare against what the attributes
4250Sstevel@tonic-gate 	 * are now being set to.  Default ordering is:
4260Sstevel@tonic-gate 	 *	- minor_perm match for this minor
4270Sstevel@tonic-gate 	 *	- mode supplied by ddi_create_priv_minor_node
4280Sstevel@tonic-gate 	 *	- devfs defaults
4290Sstevel@tonic-gate 	 */
4300Sstevel@tonic-gate 	if (persist) {
4310Sstevel@tonic-gate 		if (dev_minorperm(dv->dv_devi, dv->dv_name, &mp) != 0) {
4320Sstevel@tonic-gate 			mp.mp_uid = dv_vattr_file.va_uid;
4330Sstevel@tonic-gate 			mp.mp_gid = dv_vattr_file.va_gid;
4340Sstevel@tonic-gate 			mp.mp_mode = dv_vattr_file.va_mode;
4350Sstevel@tonic-gate 			if (dv->dv_flags & DV_DFLT_MODE) {
4360Sstevel@tonic-gate 				ASSERT((dv->dv_dflt_mode & ~S_IAMB) == 0);
4370Sstevel@tonic-gate 				mp.mp_mode &= ~S_IAMB;
4380Sstevel@tonic-gate 				mp.mp_mode |= dv->dv_dflt_mode;
4390Sstevel@tonic-gate 				dcmn_err5(("%s: setattr priv default 0%o\n",
4400Sstevel@tonic-gate 				    dv->dv_name, mp.mp_mode));
4410Sstevel@tonic-gate 			} else {
4420Sstevel@tonic-gate 				dcmn_err5(("%s: setattr devfs default 0%o\n",
4430Sstevel@tonic-gate 				    dv->dv_name, mp.mp_mode));
4440Sstevel@tonic-gate 			}
4450Sstevel@tonic-gate 		} else {
4460Sstevel@tonic-gate 			dcmn_err5(("%s: setattr minor perm default 0%o\n",
4470Sstevel@tonic-gate 			    dv->dv_name, mp.mp_mode));
4480Sstevel@tonic-gate 		}
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	/*
4520Sstevel@tonic-gate 	 * If we don't have a vattr for this node, construct one.
4530Sstevel@tonic-gate 	 */
4540Sstevel@tonic-gate 	if (dv->dv_attr) {
4550Sstevel@tonic-gate 		free_vattr = vattrp;
4560Sstevel@tonic-gate 		vattrp = NULL;
4570Sstevel@tonic-gate 	} else {
4580Sstevel@tonic-gate 		ASSERT(dv->dv_attrvp);
4590Sstevel@tonic-gate 		ASSERT(vp->v_type != VDIR);
4600Sstevel@tonic-gate 		*vattrp = dv_vattr_file;
461*5331Samw 		error = VOP_GETATTR(dv->dv_attrvp, vattrp, 0, cr, ct);
4620Sstevel@tonic-gate 		dsysdebug(error, ("vop_getattr %s %d\n",
4630Sstevel@tonic-gate 			dv->dv_name, error));
4640Sstevel@tonic-gate 		if (error)
4650Sstevel@tonic-gate 			goto out;
4660Sstevel@tonic-gate 		dv->dv_attr = vattrp;
4670Sstevel@tonic-gate 		dv_vattr_merge(dv, dv->dv_attr);
4680Sstevel@tonic-gate 		vattrp = NULL;
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	error = secpolicy_vnode_setattr(cr, vp, vap, dv->dv_attr,
4720Sstevel@tonic-gate 					flags, devfs_unlocked_access, dv);
4730Sstevel@tonic-gate 	if (error) {
4740Sstevel@tonic-gate 		dsysdebug(error, ("devfs_setattr %s secpolicy error %d\n",
4750Sstevel@tonic-gate 			dv->dv_name, error));
4760Sstevel@tonic-gate 		goto out;
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * Apply changes to the memory based attribute. This code
4810Sstevel@tonic-gate 	 * is modeled after the tmpfs implementation of memory
4820Sstevel@tonic-gate 	 * based vnodes
4830Sstevel@tonic-gate 	 */
4840Sstevel@tonic-gate 	map = dv->dv_attr;
4850Sstevel@tonic-gate 	mask = vap->va_mask;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/* Change file access modes. */
4880Sstevel@tonic-gate 	if (mask & AT_MODE) {
4890Sstevel@tonic-gate 		map->va_mode &= S_IFMT;
4900Sstevel@tonic-gate 		map->va_mode |= vap->va_mode & ~S_IFMT;
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 	if (mask & AT_UID)
4930Sstevel@tonic-gate 		map->va_uid = vap->va_uid;
4940Sstevel@tonic-gate 	if (mask & AT_GID)
4950Sstevel@tonic-gate 		map->va_gid = vap->va_gid;
4960Sstevel@tonic-gate 	if (mask & AT_ATIME)
4970Sstevel@tonic-gate 		map->va_atime = vap->va_atime;
4980Sstevel@tonic-gate 	if (mask & AT_MTIME)
4990Sstevel@tonic-gate 		map->va_mtime = vap->va_mtime;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	if (mask & (AT_MODE | AT_UID | AT_GID | AT_MTIME)) {
5020Sstevel@tonic-gate 		gethrestime(&map->va_ctime);
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	/*
5060Sstevel@tonic-gate 	 * A setattr to defaults means we no longer need the
5070Sstevel@tonic-gate 	 * shadow node as a persistent store, unless there
5080Sstevel@tonic-gate 	 * are ACLs.  Otherwise create a shadow node if one
5090Sstevel@tonic-gate 	 * doesn't exist yet.
5100Sstevel@tonic-gate 	 */
5110Sstevel@tonic-gate 	if (persist) {
5120Sstevel@tonic-gate 		if ((dv_setattr_cmp(map, &mp) == 0) &&
5130Sstevel@tonic-gate 		    ((dv->dv_flags & DV_ACL) == 0)) {
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 			if (dv->dv_attrvp) {
5160Sstevel@tonic-gate 				ddv = dv->dv_dotdot;
5170Sstevel@tonic-gate 				ASSERT(ddv->dv_attrvp);
5180Sstevel@tonic-gate 				error = VOP_REMOVE(ddv->dv_attrvp,
519*5331Samw 				    dv->dv_name, cr, ct, 0);
5200Sstevel@tonic-gate 				dsysdebug(error,
5210Sstevel@tonic-gate 				    ("vop_remove %s %s %d\n",
5220Sstevel@tonic-gate 				    ddv->dv_name, dv->dv_name, error));
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 				if (error == EROFS)
5250Sstevel@tonic-gate 					error = 0;
5260Sstevel@tonic-gate 				VN_RELE(dv->dv_attrvp);
5270Sstevel@tonic-gate 				dv->dv_attrvp = NULL;
5280Sstevel@tonic-gate 			}
5290Sstevel@tonic-gate 			ASSERT(dv->dv_attr);
5300Sstevel@tonic-gate 		} else {
5310Sstevel@tonic-gate 			if (mask & AT_MODE)
5320Sstevel@tonic-gate 				dcmn_err5(("%s persisting mode 0%o\n",
5330Sstevel@tonic-gate 					dv->dv_name, vap->va_mode));
5340Sstevel@tonic-gate 			if (mask & AT_UID)
5350Sstevel@tonic-gate 				dcmn_err5(("%s persisting uid %d\n",
5360Sstevel@tonic-gate 					dv->dv_name, vap->va_uid));
5370Sstevel@tonic-gate 			if (mask & AT_GID)
5380Sstevel@tonic-gate 				dcmn_err5(("%s persisting gid %d\n",
5390Sstevel@tonic-gate 					dv->dv_name, vap->va_gid));
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 			if (dv->dv_attrvp == NULL) {
5420Sstevel@tonic-gate 				dvp = DVTOV(dv->dv_dotdot);
5430Sstevel@tonic-gate 				dv_shadow_node(dvp, dv->dv_name, vp,
5440Sstevel@tonic-gate 				    NULL, NULLVP, cr,
5450Sstevel@tonic-gate 				    DV_SHADOW_CREATE | DV_SHADOW_WRITE_HELD);
5460Sstevel@tonic-gate 			}
5470Sstevel@tonic-gate 			if (dv->dv_attrvp) {
5480Sstevel@tonic-gate 				error = VOP_SETATTR(dv->dv_attrvp,
5490Sstevel@tonic-gate 				    vap, flags, cr, NULL);
5500Sstevel@tonic-gate 				dsysdebug(error, ("vop_setattr %s %d\n",
5510Sstevel@tonic-gate 				    dv->dv_name, error));
5520Sstevel@tonic-gate 			}
5530Sstevel@tonic-gate 			/*
5540Sstevel@tonic-gate 			 * Some file systems may return EROFS for a setattr
5550Sstevel@tonic-gate 			 * on a readonly file system.  In this case save
5560Sstevel@tonic-gate 			 * as our own memory based attribute.
5570Sstevel@tonic-gate 			 * NOTE: ufs is NOT one of these (see ufs_iupdat).
5580Sstevel@tonic-gate 			 */
5590Sstevel@tonic-gate 			if (dv->dv_attr && dv->dv_attrvp && error == 0) {
5600Sstevel@tonic-gate 				vattrp = dv->dv_attr;
5610Sstevel@tonic-gate 				dv->dv_attr = NULL;
5620Sstevel@tonic-gate 			} else if (error == EROFS)
5630Sstevel@tonic-gate 				error = 0;
5640Sstevel@tonic-gate 		}
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate out:
5680Sstevel@tonic-gate 	rw_exit(&dv->dv_contents);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	if (vattrp)
5710Sstevel@tonic-gate 		kmem_free(vattrp, sizeof (*vattrp));
5720Sstevel@tonic-gate 	if (free_vattr)
5730Sstevel@tonic-gate 		kmem_free(free_vattr, sizeof (*free_vattr));
5740Sstevel@tonic-gate 	return (error);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate static int
578*5331Samw devfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
579*5331Samw     caller_context_t *ct)
5800Sstevel@tonic-gate {
5810Sstevel@tonic-gate 	switch (cmd) {
5820Sstevel@tonic-gate 	case _PC_ACL_ENABLED:
5830Sstevel@tonic-gate 		/*
5840Sstevel@tonic-gate 		 * We rely on the underlying filesystem for ACLs,
5850Sstevel@tonic-gate 		 * so direct the query for ACL support there.
5860Sstevel@tonic-gate 		 * ACL support isn't relative to the file
5870Sstevel@tonic-gate 		 * and we can't guarantee that the dv node
5880Sstevel@tonic-gate 		 * has an attribute node, so any valid
5890Sstevel@tonic-gate 		 * attribute node will suffice.
5900Sstevel@tonic-gate 		 */
5910Sstevel@tonic-gate 		ASSERT(dvroot);
5920Sstevel@tonic-gate 		ASSERT(dvroot->dv_attrvp);
593*5331Samw 		return (VOP_PATHCONF(dvroot->dv_attrvp, cmd, valp, cr, ct));
5940Sstevel@tonic-gate 		/*NOTREACHED*/
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
597*5331Samw 	return (fs_pathconf(vp, cmd, valp, cr, ct));
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate /*
6010Sstevel@tonic-gate  * Let avp handle security attributes (acl's).
6020Sstevel@tonic-gate  */
6030Sstevel@tonic-gate static int
6040Sstevel@tonic-gate devfs_getsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
605*5331Samw     struct cred *cr, caller_context_t *ct)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate 	dvnode_t *dv = VTODV(vp);
6080Sstevel@tonic-gate 	struct vnode *avp;
6090Sstevel@tonic-gate 	int	error;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	dcmn_err2(("devfs_getsecattr %s\n", dv->dv_name));
6120Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	rw_enter(&dv->dv_contents, RW_READER);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	avp = dv->dv_attrvp;
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	/* fabricate the acl */
6190Sstevel@tonic-gate 	if (avp == NULL) {
620*5331Samw 		error = fs_fab_acl(vp, vsap, flags, cr, ct);
6210Sstevel@tonic-gate 		rw_exit(&dv->dv_contents);
6220Sstevel@tonic-gate 		return (error);
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 
625*5331Samw 	error = VOP_GETSECATTR(avp, vsap, flags, cr, ct);
6260Sstevel@tonic-gate 	dsysdebug(error, ("vop_getsecattr %s %d\n", VTODV(vp)->dv_name, error));
6270Sstevel@tonic-gate 	rw_exit(&dv->dv_contents);
6280Sstevel@tonic-gate 	return (error);
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate /*
6320Sstevel@tonic-gate  * Set security attributes (acl's)
6330Sstevel@tonic-gate  *
6340Sstevel@tonic-gate  * Note that the dv_contents lock has already been acquired
6350Sstevel@tonic-gate  * by the caller's VOP_RWLOCK.
6360Sstevel@tonic-gate  */
6370Sstevel@tonic-gate static int
6380Sstevel@tonic-gate devfs_setsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
639*5331Samw     struct cred *cr, caller_context_t *ct)
6400Sstevel@tonic-gate {
6410Sstevel@tonic-gate 	dvnode_t *dv = VTODV(vp);
6420Sstevel@tonic-gate 	struct vnode *avp;
6430Sstevel@tonic-gate 	int	error;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	dcmn_err2(("devfs_setsecattr %s\n", dv->dv_name));
6460Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
6470Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&dv->dv_contents));
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	/*
6500Sstevel@tonic-gate 	 * Not a supported operation on drivers not providing
6510Sstevel@tonic-gate 	 * file system based permissions.
6520Sstevel@tonic-gate 	 */
6530Sstevel@tonic-gate 	if (dv->dv_flags & DV_NO_FSPERM)
6540Sstevel@tonic-gate 		return (ENOTSUP);
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	/*
6570Sstevel@tonic-gate 	 * To complete, the setsecattr requires an underlying attribute node.
6580Sstevel@tonic-gate 	 */
6590Sstevel@tonic-gate 	if (dv->dv_attrvp == NULL) {
6600Sstevel@tonic-gate 		ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
6610Sstevel@tonic-gate 		dv_shadow_node(DVTOV(dv->dv_dotdot), dv->dv_name, vp,
6620Sstevel@tonic-gate 		    NULL, NULLVP, cr, DV_SHADOW_CREATE | DV_SHADOW_WRITE_HELD);
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	if ((avp = dv->dv_attrvp) == NULL) {
6660Sstevel@tonic-gate 		dcmn_err2(("devfs_setsecattr %s: "
6670Sstevel@tonic-gate 		    "cannot construct attribute node\n", dv->dv_name));
6680Sstevel@tonic-gate 		return (fs_nosys());
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	/*
6720Sstevel@tonic-gate 	 * The acl(2) system call issues a VOP_RWLOCK before setting an ACL.
6730Sstevel@tonic-gate 	 * Since backing file systems expect the lock to be held before seeing
6740Sstevel@tonic-gate 	 * a VOP_SETSECATTR ACL, we need to issue the VOP_RWLOCK to the backing
6750Sstevel@tonic-gate 	 * store before forwarding the ACL.
6760Sstevel@tonic-gate 	 */
6770Sstevel@tonic-gate 	(void) VOP_RWLOCK(avp, V_WRITELOCK_TRUE, NULL);
678*5331Samw 	error = VOP_SETSECATTR(avp, vsap, flags, cr, ct);
6790Sstevel@tonic-gate 	dsysdebug(error, ("vop_setsecattr %s %d\n", VTODV(vp)->dv_name, error));
6800Sstevel@tonic-gate 	VOP_RWUNLOCK(avp, V_WRITELOCK_TRUE, NULL);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	/*
683789Sahrens 	 * Set DV_ACL if we have a non-trivial set of ACLs.  It is not
684789Sahrens 	 * necessary to hold VOP_RWLOCK since fs_acl_nontrivial only does
685789Sahrens 	 * VOP_GETSECATTR calls.
6860Sstevel@tonic-gate 	 */
687789Sahrens 	if (fs_acl_nontrivial(avp, cr))
6880Sstevel@tonic-gate 		dv->dv_flags |= DV_ACL;
6890Sstevel@tonic-gate 	return (error);
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate  * This function is used for secpolicy_setattr().  It must call an
6940Sstevel@tonic-gate  * access() like function while it is already holding the
6950Sstevel@tonic-gate  * dv_contents lock.  We only care about this when dv_attr != NULL;
6960Sstevel@tonic-gate  * so the unlocked access call only concerns itself with that
6970Sstevel@tonic-gate  * particular branch of devfs_access().
6980Sstevel@tonic-gate  */
6990Sstevel@tonic-gate static int
7000Sstevel@tonic-gate devfs_unlocked_access(void *vdv, int mode, struct cred *cr)
7010Sstevel@tonic-gate {
7020Sstevel@tonic-gate 	struct dv_node *dv = vdv;
7030Sstevel@tonic-gate 	int shift = 0;
7040Sstevel@tonic-gate 	uid_t owner = dv->dv_attr->va_uid;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/* Check access based on owner, group and public permissions. */
7070Sstevel@tonic-gate 	if (crgetuid(cr) != owner) {
7080Sstevel@tonic-gate 		shift += 3;
7090Sstevel@tonic-gate 		if (groupmember(dv->dv_attr->va_gid, cr) == 0)
7100Sstevel@tonic-gate 			shift += 3;
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	/* compute missing mode bits */
7140Sstevel@tonic-gate 	mode &= ~(dv->dv_attr->va_mode << shift);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if (mode == 0)
7170Sstevel@tonic-gate 		return (0);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	return (secpolicy_vnode_access(cr, DVTOV(dv), owner, mode));
7200Sstevel@tonic-gate }
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate static int
723*5331Samw devfs_access(struct vnode *vp, int mode, int flags, struct cred *cr,
724*5331Samw     caller_context_t *ct)
7250Sstevel@tonic-gate {
7260Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
7270Sstevel@tonic-gate 	int		res;
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	dcmn_err2(("devfs_access %s\n", dv->dv_name));
7300Sstevel@tonic-gate 	ASSERT(dv->dv_attr || dv->dv_attrvp);
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	/* restrict console access to privileged processes */
7330Sstevel@tonic-gate 	if ((vp->v_rdev == rconsdev) && secpolicy_console(cr) != 0) {
7340Sstevel@tonic-gate 		return (EACCES);
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if (dv->dv_attr && ((dv->dv_flags & DV_ACL) == 0)) {
7380Sstevel@tonic-gate 		rw_enter(&dv->dv_contents, RW_READER);
7390Sstevel@tonic-gate 		if (dv->dv_attr) {
7400Sstevel@tonic-gate 			res = devfs_unlocked_access(dv, mode, cr);
7410Sstevel@tonic-gate 			rw_exit(&dv->dv_contents);
7420Sstevel@tonic-gate 			return (res);
7430Sstevel@tonic-gate 		}
7440Sstevel@tonic-gate 		rw_exit(&dv->dv_contents);
7450Sstevel@tonic-gate 	}
746*5331Samw 	return (VOP_ACCESS(dv->dv_attrvp, mode, flags, cr, ct));
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate /*
7500Sstevel@tonic-gate  * Lookup
7510Sstevel@tonic-gate  *
7520Sstevel@tonic-gate  * Given the directory vnode and the name of the component, return
7530Sstevel@tonic-gate  * the corresponding held vnode for that component.
7540Sstevel@tonic-gate  *
7550Sstevel@tonic-gate  * Of course in these fictional filesystems, nothing's ever quite
7560Sstevel@tonic-gate  * -that- simple.
7570Sstevel@tonic-gate  *
7580Sstevel@tonic-gate  * devfs name	type		shadow (fs attributes)	type	comments
7590Sstevel@tonic-gate  * -------------------------------------------------------------------------
7600Sstevel@tonic-gate  * drv[@addr]	VDIR		drv[@addr]		VDIR	nexus driver
7610Sstevel@tonic-gate  * drv[@addr]:m	VCHR/VBLK	drv[@addr]:m		VREG	leaf driver
7620Sstevel@tonic-gate  * drv[@addr]	VCHR/VBLK	drv[@addr]:.default	VREG	leaf driver
7630Sstevel@tonic-gate  * -------------------------------------------------------------------------
7640Sstevel@tonic-gate  *
7650Sstevel@tonic-gate  * The following names are reserved for the attribute filesystem (which
7660Sstevel@tonic-gate  * could easily be another layer on top of this one - we simply need to
7670Sstevel@tonic-gate  * hold the vnode of the thing we're looking at)
7680Sstevel@tonic-gate  *
7690Sstevel@tonic-gate  * attr name	type		shadow (fs attributes)	type	comments
7700Sstevel@tonic-gate  * -------------------------------------------------------------------------
7710Sstevel@tonic-gate  * drv[@addr]	VDIR		-			-	attribute dir
7720Sstevel@tonic-gate  * minorname	VDIR		-			-	minorname
7730Sstevel@tonic-gate  * attribute	VREG		-			-	attribute
7740Sstevel@tonic-gate  * -------------------------------------------------------------------------
7750Sstevel@tonic-gate  *
7760Sstevel@tonic-gate  * Examples:
7770Sstevel@tonic-gate  *
7780Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:zero		VCHR
7790Sstevel@tonic-gate  *	shadow:/.devices/.../mm@0:zero		VREG, fs attrs
7800Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:/zero/attr	VREG, driver attribute
7810Sstevel@tonic-gate  *
7820Sstevel@tonic-gate  *	devfs:/devices/.../sd@0,0:a		VBLK
7830Sstevel@tonic-gate  *	shadow:/.devices/.../sd@0,0:a		VREG, fs attrs
7840Sstevel@tonic-gate  *	devfs:/devices/.../sd@0,0:/a/.type	VREG, "ddi_block:chan"
7850Sstevel@tonic-gate  *
7860Sstevel@tonic-gate  *	devfs:/devices/.../mm@0			VCHR
7870Sstevel@tonic-gate  *	shadow:/.devices/.../mm@0:.default	VREG, fs attrs
7880Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:/.default/attr	VREG, driver attribute
7890Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:/.default/.type	VREG, "ddi_pseudo"
7900Sstevel@tonic-gate  *
7910Sstevel@tonic-gate  *	devfs:/devices/.../obio			VDIR
7920Sstevel@tonic-gate  *	shadow:/devices/.../obio		VDIR, needed for fs attrs.
7930Sstevel@tonic-gate  *	devfs:/devices/.../obio:/.default/attr	VDIR, driver attribute
7940Sstevel@tonic-gate  *
7950Sstevel@tonic-gate  * We also need to be able deal with "old" devices that have gone away,
7960Sstevel@tonic-gate  * though I think that provided we return them with readdir, they can
7970Sstevel@tonic-gate  * be removed (i.e. they don't have to respond to lookup, though it might
7980Sstevel@tonic-gate  * be weird if they didn't ;-)
7990Sstevel@tonic-gate  *
8000Sstevel@tonic-gate  * Lookup has side-effects.
8010Sstevel@tonic-gate  *
8020Sstevel@tonic-gate  * - It will create directories and fs attribute files in the shadow hierarchy.
8030Sstevel@tonic-gate  * - It should cause non-SID devices to be probed (ask the parent nexi).
8040Sstevel@tonic-gate  */
8050Sstevel@tonic-gate /*ARGSUSED3*/
8060Sstevel@tonic-gate static int
8070Sstevel@tonic-gate devfs_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
808*5331Samw     struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred,
809*5331Samw     caller_context_t *ct, int *direntflags, pathname_t *realpnp)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate 	ASSERT(dvp->v_type == VDIR);
8120Sstevel@tonic-gate 	dcmn_err2(("devfs_lookup: %s\n", nm));
8130Sstevel@tonic-gate 	return (dv_find(VTODV(dvp), nm, vpp, pnp, rdir, cred, 0));
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate /*
8170Sstevel@tonic-gate  * devfs nodes can't really be created directly by userland - however,
8180Sstevel@tonic-gate  * we do allow creates to find existing nodes:
8190Sstevel@tonic-gate  *
8200Sstevel@tonic-gate  * - any create fails if the node doesn't exist - EROFS.
8210Sstevel@tonic-gate  * - creating an existing directory read-only succeeds, otherwise EISDIR.
8220Sstevel@tonic-gate  * - exclusive creates fail if the node already exists - EEXIST.
8230Sstevel@tonic-gate  * - failure to create the snode for an existing device - ENOSYS.
8240Sstevel@tonic-gate  */
8250Sstevel@tonic-gate /*ARGSUSED2*/
8260Sstevel@tonic-gate static int
8270Sstevel@tonic-gate devfs_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl,
828*5331Samw     int mode, struct vnode **vpp, struct cred *cred, int flag,
829*5331Samw     caller_context_t *ct, vsecattr_t *vsecp)
8300Sstevel@tonic-gate {
8310Sstevel@tonic-gate 	int error;
8320Sstevel@tonic-gate 	struct vnode *vp;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	dcmn_err2(("devfs_create %s\n", nm));
8350Sstevel@tonic-gate 	error = dv_find(VTODV(dvp), nm, &vp, NULL, NULLVP, cred, 0);
8360Sstevel@tonic-gate 	if (error == 0) {
8370Sstevel@tonic-gate 		if (excl == EXCL)
8380Sstevel@tonic-gate 			error = EEXIST;
8390Sstevel@tonic-gate 		else if (vp->v_type == VDIR && (mode & VWRITE))
8400Sstevel@tonic-gate 			error = EISDIR;
8410Sstevel@tonic-gate 		else
842*5331Samw 			error = VOP_ACCESS(vp, mode, 0, cred, ct);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 		if (error) {
8450Sstevel@tonic-gate 			VN_RELE(vp);
8460Sstevel@tonic-gate 		} else
8470Sstevel@tonic-gate 			*vpp = vp;
8480Sstevel@tonic-gate 	} else if (error == ENOENT)
8490Sstevel@tonic-gate 		error = EROFS;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	return (error);
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate /*
8550Sstevel@tonic-gate  * If DV_BUILD is set, we call into nexus driver to do a BUS_CONFIG_ALL.
8560Sstevel@tonic-gate  * Otherwise, simply return cached dv_node's. Hotplug code always call
8570Sstevel@tonic-gate  * devfs_clean() to invalid the dv_node cache.
8580Sstevel@tonic-gate  */
859*5331Samw /*ARGSUSED5*/
8600Sstevel@tonic-gate static int
861*5331Samw devfs_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred, int *eofp,
862*5331Samw     caller_context_t *ct, int flags)
8630Sstevel@tonic-gate {
8640Sstevel@tonic-gate 	struct dv_node *ddv, *dv;
8650Sstevel@tonic-gate 	struct dirent64 *de, *bufp;
8660Sstevel@tonic-gate 	offset_t diroff;
8670Sstevel@tonic-gate 	offset_t	soff;
8680Sstevel@tonic-gate 	size_t reclen, movesz;
8690Sstevel@tonic-gate 	int error;
8700Sstevel@tonic-gate 	struct vattr va;
8710Sstevel@tonic-gate 	size_t bufsz;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	ddv = VTODV(dvp);
8740Sstevel@tonic-gate 	dcmn_err2(("devfs_readdir %s: offset %lld len %ld\n",
8750Sstevel@tonic-gate 	    ddv->dv_name, uiop->uio_loffset, uiop->uio_iov->iov_len));
8760Sstevel@tonic-gate 	ASSERT(ddv->dv_attr || ddv->dv_attrvp);
8770Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&ddv->dv_contents));
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	if (uiop->uio_loffset >= MAXOFF_T) {
8800Sstevel@tonic-gate 		if (eofp)
8810Sstevel@tonic-gate 			*eofp = 1;
8820Sstevel@tonic-gate 		return (0);
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	if (uiop->uio_iovcnt != 1)
8860Sstevel@tonic-gate 		return (EINVAL);
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	if (dvp->v_type != VDIR)
8890Sstevel@tonic-gate 		return (ENOTDIR);
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	/* Load the initial contents */
8920Sstevel@tonic-gate 	if (ddv->dv_flags & DV_BUILD) {
8930Sstevel@tonic-gate 		if (!rw_tryupgrade(&ddv->dv_contents)) {
8940Sstevel@tonic-gate 			rw_exit(&ddv->dv_contents);
8950Sstevel@tonic-gate 			rw_enter(&ddv->dv_contents, RW_WRITER);
8960Sstevel@tonic-gate 		}
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 		/* recheck and fill */
8990Sstevel@tonic-gate 		if (ddv->dv_flags & DV_BUILD)
9000Sstevel@tonic-gate 			dv_filldir(ddv);
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 		rw_downgrade(&ddv->dv_contents);
9030Sstevel@tonic-gate 	}
9040Sstevel@tonic-gate 
9053133Sjg 	soff = uiop->uio_loffset;
9060Sstevel@tonic-gate 	bufsz = uiop->uio_iov->iov_len;
9070Sstevel@tonic-gate 	de = bufp = kmem_alloc(bufsz, KM_SLEEP);
9080Sstevel@tonic-gate 	movesz = 0;
9090Sstevel@tonic-gate 	dv = (struct dv_node *)-1;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	/*
9120Sstevel@tonic-gate 	 * Move as many entries into the uio structure as it will take.
9130Sstevel@tonic-gate 	 * Special case "." and "..".
9140Sstevel@tonic-gate 	 */
9150Sstevel@tonic-gate 	diroff = 0;
9160Sstevel@tonic-gate 	if (soff == 0) {				/* . */
9170Sstevel@tonic-gate 		reclen = DIRENT64_RECLEN(strlen("."));
9180Sstevel@tonic-gate 		if ((movesz + reclen) > bufsz)
9190Sstevel@tonic-gate 			goto full;
9200Sstevel@tonic-gate 		de->d_ino = (ino64_t)ddv->dv_ino;
9210Sstevel@tonic-gate 		de->d_off = (off64_t)diroff + 1;
9220Sstevel@tonic-gate 		de->d_reclen = (ushort_t)reclen;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 		(void) strncpy(de->d_name, ".", DIRENT64_NAMELEN(reclen));
9270Sstevel@tonic-gate 		movesz += reclen;
9283133Sjg 		de = (dirent64_t *)(intptr_t)((char *)de + reclen);
9290Sstevel@tonic-gate 		dcmn_err3(("devfs_readdir: A: diroff %lld, soff %lld: '%s' "
9300Sstevel@tonic-gate 		    "reclen %lu\n", diroff, soff, ".", reclen));
9310Sstevel@tonic-gate 	}
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	diroff++;
9340Sstevel@tonic-gate 	if (soff <= 1) {				/* .. */
9350Sstevel@tonic-gate 		reclen = DIRENT64_RECLEN(strlen(".."));
9360Sstevel@tonic-gate 		if ((movesz + reclen) > bufsz)
9370Sstevel@tonic-gate 			goto full;
9380Sstevel@tonic-gate 		de->d_ino = (ino64_t)ddv->dv_dotdot->dv_ino;
9390Sstevel@tonic-gate 		de->d_off = (off64_t)diroff + 1;
9400Sstevel@tonic-gate 		de->d_reclen = (ushort_t)reclen;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 		(void) strncpy(de->d_name, "..", DIRENT64_NAMELEN(reclen));
9450Sstevel@tonic-gate 		movesz += reclen;
9463133Sjg 		de = (dirent64_t *)(intptr_t)((char *)de + reclen);
9470Sstevel@tonic-gate 		dcmn_err3(("devfs_readdir: B: diroff %lld, soff %lld: '%s' "
9480Sstevel@tonic-gate 		    "reclen %lu\n", diroff, soff, "..", reclen));
9490Sstevel@tonic-gate 	}
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	diroff++;
9520Sstevel@tonic-gate 	for (dv = ddv->dv_dot; dv; dv = dv->dv_next, 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);
1005*5331Samw 		(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
1015*5331Samw devfs_fsync(struct vnode *vp, int syncflag, struct cred *cred,
1016*5331Samw     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
1036*5331Samw 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  */
1057*5331Samw /*ARGSUSED2*/
10580Sstevel@tonic-gate static int
1059*5331Samw 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
1108*5331Samw devfs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
1109*5331Samw     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