xref: /titanic_44/usr/src/uts/common/fs/devfs/devfs_vnops.c (revision 134a1f4e3289b54e0f980e9cf05352e419a60bee)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5bc1009abSjg  * Common Development and Distribution License (the "License").
6bc1009abSjg  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*134a1f4eSCasper H.S. Dik  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * vnode ops for the devfs
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * For leaf vnode special files (VCHR|VBLK) specfs will always see the VOP
297c478bd9Sstevel@tonic-gate  * first because dv_find always performs leaf vnode substitution, returning
307c478bd9Sstevel@tonic-gate  * a specfs vnode with an s_realvp pointing to the devfs leaf vnode. This
317c478bd9Sstevel@tonic-gate  * means that the only leaf special file VOP operations that devfs will see
327c478bd9Sstevel@tonic-gate  * after VOP_LOOKUP are the ones that specfs forwards.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
387c478bd9Sstevel@tonic-gate #include <sys/systm.h>
397c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
407c478bd9Sstevel@tonic-gate #include <sys/user.h>
417c478bd9Sstevel@tonic-gate #include <sys/time.h>
427c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
437c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
44aa59c4cbSrsb #include <sys/vfs_opreg.h>
457c478bd9Sstevel@tonic-gate #include <sys/file.h>
467c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
477c478bd9Sstevel@tonic-gate #include <sys/flock.h>
487c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
497c478bd9Sstevel@tonic-gate #include <sys/uio.h>
507c478bd9Sstevel@tonic-gate #include <sys/errno.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <sys/cred.h>
537c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
547c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
557c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
567c478bd9Sstevel@tonic-gate #include <sys/debug.h>
577c478bd9Sstevel@tonic-gate #include <sys/policy.h>
587c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
594c06356bSdh142964 #include <sys/sunndi.h>
607c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
617c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate extern struct vattr	dv_vattr_dir, dv_vattr_file;
647c478bd9Sstevel@tonic-gate extern dev_t rconsdev;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * Open of devices (leaf nodes) is handled by specfs.
687c478bd9Sstevel@tonic-gate  * There is nothing to do to open a directory
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate /*ARGSUSED*/
717c478bd9Sstevel@tonic-gate static int
devfs_open(struct vnode ** vpp,int flag,struct cred * cred,caller_context_t * ct)72da6c28aaSamw devfs_open(struct vnode **vpp, int flag, struct cred *cred,
73da6c28aaSamw     caller_context_t *ct)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(*vpp);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_open %s\n", dv->dv_name));
787c478bd9Sstevel@tonic-gate 	ASSERT((*vpp)->v_type == VDIR);
797c478bd9Sstevel@tonic-gate 	return (0);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Close of devices (leaf nodes) is handled by specfs.
847c478bd9Sstevel@tonic-gate  * There is nothing much to do inorder to close a directory.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
877c478bd9Sstevel@tonic-gate static int
devfs_close(struct vnode * vp,int flag,int count,offset_t offset,struct cred * cred,caller_context_t * ct)887c478bd9Sstevel@tonic-gate devfs_close(struct vnode *vp, int flag, int count,
89da6c28aaSamw     offset_t offset, struct cred *cred, caller_context_t *ct)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_close %s\n", dv->dv_name));
947c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
977c478bd9Sstevel@tonic-gate 	cleanshares(vp, ttoproc(curthread)->p_pid);
987c478bd9Sstevel@tonic-gate 	return (0);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * Read of devices (leaf nodes) is handled by specfs.
1037c478bd9Sstevel@tonic-gate  * Read of directories is not supported.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1067c478bd9Sstevel@tonic-gate static int
devfs_read(struct vnode * vp,struct uio * uiop,int ioflag,struct cred * cred,struct caller_context * ct)1077c478bd9Sstevel@tonic-gate devfs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
1087c478bd9Sstevel@tonic-gate 	struct caller_context *ct)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_read %s\n", VTODV(vp)->dv_name));
1117c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
1127c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&VTODV(vp)->dv_contents));
1137c478bd9Sstevel@tonic-gate 	return (EISDIR);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Write of devices (leaf nodes) is handled by specfs.
1187c478bd9Sstevel@tonic-gate  * Write of directories is not supported.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1217c478bd9Sstevel@tonic-gate static int
devfs_write(struct vnode * vp,struct uio * uiop,int ioflag,struct cred * cred,struct caller_context * ct)1227c478bd9Sstevel@tonic-gate devfs_write(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
1237c478bd9Sstevel@tonic-gate 	struct caller_context *ct)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_write %s\n", VTODV(vp)->dv_name));
1267c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
1277c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&VTODV(vp)->dv_contents));
1287c478bd9Sstevel@tonic-gate 	return (EISDIR);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * Ioctls to device (leaf nodes) is handled by specfs.
1337c478bd9Sstevel@tonic-gate  * Ioctl to directories is not supported.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1367c478bd9Sstevel@tonic-gate static int
devfs_ioctl(struct vnode * vp,int cmd,intptr_t arg,int flag,struct cred * cred,int * rvalp,caller_context_t * ct)1377c478bd9Sstevel@tonic-gate devfs_ioctl(struct vnode *vp, int cmd, intptr_t arg, int flag,
138da6c28aaSamw     struct cred *cred, int *rvalp, caller_context_t *ct)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_ioctl %s\n", VTODV(vp)->dv_name));
1417c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	return (ENOTTY);	/* no ioctls supported */
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * We can be asked directly about the attributes of directories, or
1487c478bd9Sstevel@tonic-gate  * (via sp->s_realvp) about the filesystem attributes of special files.
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  * For directories, we just believe the attribute store
1517c478bd9Sstevel@tonic-gate  * though we mangle the nodeid, fsid, and rdev to convince userland we
1527c478bd9Sstevel@tonic-gate  * really are a different filesystem.
1537c478bd9Sstevel@tonic-gate  *
1547c478bd9Sstevel@tonic-gate  * For special files, a little more fakery is required.
1557c478bd9Sstevel@tonic-gate  *
1567c478bd9Sstevel@tonic-gate  * If the attribute store is not there (read only root), we believe our
1577c478bd9Sstevel@tonic-gate  * memory based attributes.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate static int
devfs_getattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cr,caller_context_t * ct)160da6c28aaSamw devfs_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr,
161da6c28aaSamw     caller_context_t *ct)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
1647c478bd9Sstevel@tonic-gate 	int		error = 0;
1657c478bd9Sstevel@tonic-gate 	uint_t		mask;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 * Message goes to console only. Otherwise, the message
1697c478bd9Sstevel@tonic-gate 	 * causes devfs_getattr to be invoked again... infinite loop
1707c478bd9Sstevel@tonic-gate 	 */
1717c478bd9Sstevel@tonic-gate 	dcmn_err2(("?devfs_getattr %s\n", dv->dv_name));
1727c478bd9Sstevel@tonic-gate 	ASSERT(dv->dv_attr || dv->dv_attrvp);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (!(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK)) {
1757c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,	/* panic ? */
1767c478bd9Sstevel@tonic-gate 		    "?%s: getattr on vnode type %d", dvnm, vp->v_type);
1777c478bd9Sstevel@tonic-gate 		return (ENOENT);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 
180e37c6c37Scth 	rw_enter(&dv->dv_contents, RW_READER);
1817c478bd9Sstevel@tonic-gate 	if (dv->dv_attr) {
1827c478bd9Sstevel@tonic-gate 		/*
1837c478bd9Sstevel@tonic-gate 		 * obtain from the memory version of attribute.
1847c478bd9Sstevel@tonic-gate 		 * preserve mask for those that optimize.
1857c478bd9Sstevel@tonic-gate 		 * devfs specific fields are already merged on creation.
1867c478bd9Sstevel@tonic-gate 		 */
1877c478bd9Sstevel@tonic-gate 		mask = vap->va_mask;
1887c478bd9Sstevel@tonic-gate 		*vap = *dv->dv_attr;
1897c478bd9Sstevel@tonic-gate 		vap->va_mask = mask;
1907c478bd9Sstevel@tonic-gate 	} else {
1917c478bd9Sstevel@tonic-gate 		/* obtain from attribute store and merge */
192da6c28aaSamw 		error = VOP_GETATTR(dv->dv_attrvp, vap, flags, cr, ct);
1937c478bd9Sstevel@tonic-gate 		dsysdebug(error, ("vop_getattr %s %d\n", dv->dv_name, error));
1947c478bd9Sstevel@tonic-gate 		dv_vattr_merge(dv, vap);
1957c478bd9Sstevel@tonic-gate 	}
196e37c6c37Scth 	rw_exit(&dv->dv_contents);
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/*
1997c478bd9Sstevel@tonic-gate 	 * Restrict the permissions of the node fronting the console
2007c478bd9Sstevel@tonic-gate 	 * to 0600 with root as the owner.  This prevents a non-root
2017c478bd9Sstevel@tonic-gate 	 * user from gaining access to a serial terminal (like /dev/term/a)
2027c478bd9Sstevel@tonic-gate 	 * which is in reality serving as the console device (/dev/console).
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	if (vp->v_rdev == rconsdev) {
2057c478bd9Sstevel@tonic-gate 		mode_t	rconsmask = S_IXUSR|S_IRWXG|S_IRWXO;
2067c478bd9Sstevel@tonic-gate 		vap->va_mode &= (~rconsmask);
2077c478bd9Sstevel@tonic-gate 		vap->va_uid = 0;
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	return (error);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate static int devfs_unlocked_access(void *, int, struct cred *);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*ARGSUSED4*/
2167c478bd9Sstevel@tonic-gate static int
devfs_setattr_dir(struct dv_node * dv,struct vnode * vp,struct vattr * vap,int flags,struct cred * cr)2177c478bd9Sstevel@tonic-gate devfs_setattr_dir(
2187c478bd9Sstevel@tonic-gate 	struct dv_node *dv,
2197c478bd9Sstevel@tonic-gate 	struct vnode *vp,
2207c478bd9Sstevel@tonic-gate 	struct vattr *vap,
2217c478bd9Sstevel@tonic-gate 	int flags,
2227c478bd9Sstevel@tonic-gate 	struct cred *cr)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	struct vattr	*map;
22532207c10Scth 	uint_t		mask;
2267c478bd9Sstevel@tonic-gate 	int		error = 0;
2277c478bd9Sstevel@tonic-gate 	struct vattr	vattr;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	ASSERT(dv->dv_attr || dv->dv_attrvp);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
2327c478bd9Sstevel@tonic-gate 	ASSERT((dv->dv_flags & DV_NO_FSPERM) == 0);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (vap->va_mask & AT_NOSET)
2357c478bd9Sstevel@tonic-gate 		return (EINVAL);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* to ensure consistency, single thread setting of attributes */
2387c478bd9Sstevel@tonic-gate 	rw_enter(&dv->dv_contents, RW_WRITER);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate again:	if (dv->dv_attr) {
2417c478bd9Sstevel@tonic-gate 
242da6c28aaSamw 		error = secpolicy_vnode_setattr(cr, vp, vap,
243da6c28aaSamw 		    dv->dv_attr, flags, devfs_unlocked_access, dv);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 		if (error)
2467c478bd9Sstevel@tonic-gate 			goto out;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		/*
2497c478bd9Sstevel@tonic-gate 		 * Apply changes to the memory based attribute. This code
2507c478bd9Sstevel@tonic-gate 		 * is modeled after the tmpfs implementation of memory
2517c478bd9Sstevel@tonic-gate 		 * based vnodes
2527c478bd9Sstevel@tonic-gate 		 */
2537c478bd9Sstevel@tonic-gate 		map = dv->dv_attr;
2547c478bd9Sstevel@tonic-gate 		mask = vap->va_mask;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		/* Change file access modes. */
2577c478bd9Sstevel@tonic-gate 		if (mask & AT_MODE) {
2587c478bd9Sstevel@tonic-gate 			map->va_mode &= S_IFMT;
2597c478bd9Sstevel@tonic-gate 			map->va_mode |= vap->va_mode & ~S_IFMT;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 		if (mask & AT_UID)
2627c478bd9Sstevel@tonic-gate 			map->va_uid = vap->va_uid;
2637c478bd9Sstevel@tonic-gate 		if (mask & AT_GID)
2647c478bd9Sstevel@tonic-gate 			map->va_gid = vap->va_gid;
2657c478bd9Sstevel@tonic-gate 		if (mask & AT_ATIME)
2667c478bd9Sstevel@tonic-gate 			map->va_atime = vap->va_atime;
2677c478bd9Sstevel@tonic-gate 		if (mask & AT_MTIME)
2687c478bd9Sstevel@tonic-gate 			map->va_mtime = vap->va_mtime;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		if (mask & (AT_MODE | AT_UID | AT_GID | AT_MTIME))
2717c478bd9Sstevel@tonic-gate 			gethrestime(&map->va_ctime);
2727c478bd9Sstevel@tonic-gate 	} else {
2737c478bd9Sstevel@tonic-gate 		/* use the backing attribute store */
2747c478bd9Sstevel@tonic-gate 		ASSERT(dv->dv_attrvp);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 		/*
2777c478bd9Sstevel@tonic-gate 		 * See if we are changing something we care about
2787c478bd9Sstevel@tonic-gate 		 * the persistence of - return success if we don't care.
2797c478bd9Sstevel@tonic-gate 		 */
2807c478bd9Sstevel@tonic-gate 		if (vap->va_mask & (AT_MODE|AT_UID|AT_GID|AT_ATIME|AT_MTIME)) {
2817c478bd9Sstevel@tonic-gate 			/* Set the attributes */
2827c478bd9Sstevel@tonic-gate 			error = VOP_SETATTR(dv->dv_attrvp,
2837c478bd9Sstevel@tonic-gate 			    vap, flags, cr, NULL);
2847c478bd9Sstevel@tonic-gate 			dsysdebug(error,
2857c478bd9Sstevel@tonic-gate 			    ("vop_setattr %s %d\n", dv->dv_name, error));
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			/*
2887c478bd9Sstevel@tonic-gate 			 * Some file systems may return EROFS for a setattr
2897c478bd9Sstevel@tonic-gate 			 * on a readonly file system.  In this case we create
2907c478bd9Sstevel@tonic-gate 			 * our own memory based attribute.
2917c478bd9Sstevel@tonic-gate 			 */
2927c478bd9Sstevel@tonic-gate 			if (error == EROFS) {
2937c478bd9Sstevel@tonic-gate 				/*
2947c478bd9Sstevel@tonic-gate 				 * obtain attributes from existing file
2957c478bd9Sstevel@tonic-gate 				 * that we will modify and switch to memory
2967c478bd9Sstevel@tonic-gate 				 * based attribute until attribute store is
2977c478bd9Sstevel@tonic-gate 				 * read/write.
2987c478bd9Sstevel@tonic-gate 				 */
2997c478bd9Sstevel@tonic-gate 				vattr = dv_vattr_dir;
300da6c28aaSamw 				if (VOP_GETATTR(dv->dv_attrvp,
301da6c28aaSamw 				    &vattr, flags, cr, NULL) == 0) {
3027c478bd9Sstevel@tonic-gate 					dv->dv_attr = kmem_alloc(
3037c478bd9Sstevel@tonic-gate 					    sizeof (struct vattr), KM_SLEEP);
3047c478bd9Sstevel@tonic-gate 					*dv->dv_attr = vattr;
3057c478bd9Sstevel@tonic-gate 					dv_vattr_merge(dv, dv->dv_attr);
3067c478bd9Sstevel@tonic-gate 					goto again;
3077c478bd9Sstevel@tonic-gate 				}
3087c478bd9Sstevel@tonic-gate 			}
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate out:
3127c478bd9Sstevel@tonic-gate 	rw_exit(&dv->dv_contents);
3137c478bd9Sstevel@tonic-gate 	return (error);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate  * Compare the uid/gid/mode changes requested for a setattr
3197c478bd9Sstevel@tonic-gate  * operation with the same details of a node's default minor
3207c478bd9Sstevel@tonic-gate  * perm information.  Return 0 if identical.
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate static int
dv_setattr_cmp(struct vattr * map,mperm_t * mp)3237c478bd9Sstevel@tonic-gate dv_setattr_cmp(struct vattr *map, mperm_t *mp)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	if ((map->va_mode & S_IAMB) != (mp->mp_mode & S_IAMB))
3267c478bd9Sstevel@tonic-gate 		return (1);
3277c478bd9Sstevel@tonic-gate 	if (map->va_uid != mp->mp_uid)
3287c478bd9Sstevel@tonic-gate 		return (1);
3297c478bd9Sstevel@tonic-gate 	if (map->va_gid != mp->mp_gid)
3307c478bd9Sstevel@tonic-gate 		return (1);
3317c478bd9Sstevel@tonic-gate 	return (0);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*ARGSUSED4*/
3367c478bd9Sstevel@tonic-gate static int
devfs_setattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cr,caller_context_t * ct)3377c478bd9Sstevel@tonic-gate devfs_setattr(
3387c478bd9Sstevel@tonic-gate 	struct vnode *vp,
3397c478bd9Sstevel@tonic-gate 	struct vattr *vap,
3407c478bd9Sstevel@tonic-gate 	int flags,
3417c478bd9Sstevel@tonic-gate 	struct cred *cr,
3427c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
3457c478bd9Sstevel@tonic-gate 	struct dv_node	*ddv;
3467c478bd9Sstevel@tonic-gate 	struct vnode	*dvp;
3477c478bd9Sstevel@tonic-gate 	struct vattr	*map;
34832207c10Scth 	uint_t		mask;
3497c478bd9Sstevel@tonic-gate 	int		error = 0;
3507c478bd9Sstevel@tonic-gate 	struct vattr	*free_vattr = NULL;
3517c478bd9Sstevel@tonic-gate 	struct vattr	*vattrp = NULL;
3527c478bd9Sstevel@tonic-gate 	mperm_t		mp;
3537c478bd9Sstevel@tonic-gate 	int		persist;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	/*
3567c478bd9Sstevel@tonic-gate 	 * Message goes to console only. Otherwise, the message
3577c478bd9Sstevel@tonic-gate 	 * causes devfs_getattr to be invoked again... infinite loop
3587c478bd9Sstevel@tonic-gate 	 */
3597c478bd9Sstevel@tonic-gate 	dcmn_err2(("?devfs_setattr %s\n", dv->dv_name));
3607c478bd9Sstevel@tonic-gate 	ASSERT(dv->dv_attr || dv->dv_attrvp);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (!(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK)) {
3637c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,	/* panic ? */
3647c478bd9Sstevel@tonic-gate 		    "?%s: getattr on vnode type %d", dvnm, vp->v_type);
3657c478bd9Sstevel@tonic-gate 		return (ENOENT);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (vap->va_mask & AT_NOSET)
3697c478bd9Sstevel@tonic-gate 		return (EINVAL);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	/*
3727c478bd9Sstevel@tonic-gate 	 * If we are changing something we don't care about
3737c478bd9Sstevel@tonic-gate 	 * the persistence of, return success.
3747c478bd9Sstevel@tonic-gate 	 */
3757c478bd9Sstevel@tonic-gate 	if ((vap->va_mask &
3767c478bd9Sstevel@tonic-gate 	    (AT_MODE|AT_UID|AT_GID|AT_ATIME|AT_MTIME)) == 0)
3777c478bd9Sstevel@tonic-gate 		return (0);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/*
3807c478bd9Sstevel@tonic-gate 	 * If driver overrides fs perm, disallow chmod
3817c478bd9Sstevel@tonic-gate 	 * and do not create attribute nodes.
3827c478bd9Sstevel@tonic-gate 	 */
3837c478bd9Sstevel@tonic-gate 	if (dv->dv_flags & DV_NO_FSPERM) {
3847c478bd9Sstevel@tonic-gate 		ASSERT(dv->dv_attr);
3857c478bd9Sstevel@tonic-gate 		if (vap->va_mask & (AT_MODE | AT_UID | AT_GID))
3867c478bd9Sstevel@tonic-gate 			return (EPERM);
3877c478bd9Sstevel@tonic-gate 		if ((vap->va_mask & (AT_ATIME|AT_MTIME)) == 0)
3887c478bd9Sstevel@tonic-gate 			return (0);
3897c478bd9Sstevel@tonic-gate 		rw_enter(&dv->dv_contents, RW_WRITER);
3907c478bd9Sstevel@tonic-gate 		if (vap->va_mask & AT_ATIME)
3917c478bd9Sstevel@tonic-gate 			dv->dv_attr->va_atime = vap->va_atime;
3927c478bd9Sstevel@tonic-gate 		if (vap->va_mask & AT_MTIME)
3937c478bd9Sstevel@tonic-gate 			dv->dv_attr->va_mtime = vap->va_mtime;
3947c478bd9Sstevel@tonic-gate 		rw_exit(&dv->dv_contents);
3957c478bd9Sstevel@tonic-gate 		return (0);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/*
3997c478bd9Sstevel@tonic-gate 	 * Directories are always created but device nodes are
4007c478bd9Sstevel@tonic-gate 	 * only used to persist non-default permissions.
4017c478bd9Sstevel@tonic-gate 	 */
4027c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
4037c478bd9Sstevel@tonic-gate 		ASSERT(dv->dv_attr || dv->dv_attrvp);
4047c478bd9Sstevel@tonic-gate 		return (devfs_setattr_dir(dv, vp, vap, flags, cr));
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/*
4087c478bd9Sstevel@tonic-gate 	 * Allocate now before we take any locks
4097c478bd9Sstevel@tonic-gate 	 */
4107c478bd9Sstevel@tonic-gate 	vattrp = kmem_zalloc(sizeof (*vattrp), KM_SLEEP);
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/* to ensure consistency, single thread setting of attributes */
4137c478bd9Sstevel@tonic-gate 	rw_enter(&dv->dv_contents, RW_WRITER);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/*
4167c478bd9Sstevel@tonic-gate 	 * We don't need to create an attribute node
4177c478bd9Sstevel@tonic-gate 	 * to persist access or modification times.
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	persist = (vap->va_mask & (AT_MODE | AT_UID | AT_GID));
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 * If persisting something, get the default permissions
4237c478bd9Sstevel@tonic-gate 	 * for this minor to compare against what the attributes
4247c478bd9Sstevel@tonic-gate 	 * are now being set to.  Default ordering is:
4257c478bd9Sstevel@tonic-gate 	 *	- minor_perm match for this minor
4267c478bd9Sstevel@tonic-gate 	 *	- mode supplied by ddi_create_priv_minor_node
4277c478bd9Sstevel@tonic-gate 	 *	- devfs defaults
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	if (persist) {
4307c478bd9Sstevel@tonic-gate 		if (dev_minorperm(dv->dv_devi, dv->dv_name, &mp) != 0) {
4317c478bd9Sstevel@tonic-gate 			mp.mp_uid = dv_vattr_file.va_uid;
4327c478bd9Sstevel@tonic-gate 			mp.mp_gid = dv_vattr_file.va_gid;
4337c478bd9Sstevel@tonic-gate 			mp.mp_mode = dv_vattr_file.va_mode;
4347c478bd9Sstevel@tonic-gate 			if (dv->dv_flags & DV_DFLT_MODE) {
4357c478bd9Sstevel@tonic-gate 				ASSERT((dv->dv_dflt_mode & ~S_IAMB) == 0);
4367c478bd9Sstevel@tonic-gate 				mp.mp_mode &= ~S_IAMB;
4377c478bd9Sstevel@tonic-gate 				mp.mp_mode |= dv->dv_dflt_mode;
4387c478bd9Sstevel@tonic-gate 				dcmn_err5(("%s: setattr priv default 0%o\n",
4397c478bd9Sstevel@tonic-gate 				    dv->dv_name, mp.mp_mode));
4407c478bd9Sstevel@tonic-gate 			} else {
4417c478bd9Sstevel@tonic-gate 				dcmn_err5(("%s: setattr devfs default 0%o\n",
4427c478bd9Sstevel@tonic-gate 				    dv->dv_name, mp.mp_mode));
4437c478bd9Sstevel@tonic-gate 			}
4447c478bd9Sstevel@tonic-gate 		} else {
4457c478bd9Sstevel@tonic-gate 			dcmn_err5(("%s: setattr minor perm default 0%o\n",
4467c478bd9Sstevel@tonic-gate 			    dv->dv_name, mp.mp_mode));
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	/*
4517c478bd9Sstevel@tonic-gate 	 * If we don't have a vattr for this node, construct one.
4527c478bd9Sstevel@tonic-gate 	 */
4537c478bd9Sstevel@tonic-gate 	if (dv->dv_attr) {
4547c478bd9Sstevel@tonic-gate 		free_vattr = vattrp;
4557c478bd9Sstevel@tonic-gate 		vattrp = NULL;
4567c478bd9Sstevel@tonic-gate 	} else {
4577c478bd9Sstevel@tonic-gate 		ASSERT(dv->dv_attrvp);
4587c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_type != VDIR);
4597c478bd9Sstevel@tonic-gate 		*vattrp = dv_vattr_file;
460da6c28aaSamw 		error = VOP_GETATTR(dv->dv_attrvp, vattrp, 0, cr, ct);
461e37c6c37Scth 		dsysdebug(error, ("vop_getattr %s %d\n", dv->dv_name, error));
4627c478bd9Sstevel@tonic-gate 		if (error)
4637c478bd9Sstevel@tonic-gate 			goto out;
4647c478bd9Sstevel@tonic-gate 		dv->dv_attr = vattrp;
4657c478bd9Sstevel@tonic-gate 		dv_vattr_merge(dv, dv->dv_attr);
4667c478bd9Sstevel@tonic-gate 		vattrp = NULL;
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	error = secpolicy_vnode_setattr(cr, vp, vap, dv->dv_attr,
4707c478bd9Sstevel@tonic-gate 	    flags, devfs_unlocked_access, dv);
4717c478bd9Sstevel@tonic-gate 	if (error) {
4727c478bd9Sstevel@tonic-gate 		dsysdebug(error, ("devfs_setattr %s secpolicy error %d\n",
4737c478bd9Sstevel@tonic-gate 		    dv->dv_name, error));
4747c478bd9Sstevel@tonic-gate 		goto out;
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/*
4787c478bd9Sstevel@tonic-gate 	 * Apply changes to the memory based attribute. This code
4797c478bd9Sstevel@tonic-gate 	 * is modeled after the tmpfs implementation of memory
4807c478bd9Sstevel@tonic-gate 	 * based vnodes
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 	map = dv->dv_attr;
4837c478bd9Sstevel@tonic-gate 	mask = vap->va_mask;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/* Change file access modes. */
4867c478bd9Sstevel@tonic-gate 	if (mask & AT_MODE) {
4877c478bd9Sstevel@tonic-gate 		map->va_mode &= S_IFMT;
4887c478bd9Sstevel@tonic-gate 		map->va_mode |= vap->va_mode & ~S_IFMT;
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 	if (mask & AT_UID)
4917c478bd9Sstevel@tonic-gate 		map->va_uid = vap->va_uid;
4927c478bd9Sstevel@tonic-gate 	if (mask & AT_GID)
4937c478bd9Sstevel@tonic-gate 		map->va_gid = vap->va_gid;
4947c478bd9Sstevel@tonic-gate 	if (mask & AT_ATIME)
4957c478bd9Sstevel@tonic-gate 		map->va_atime = vap->va_atime;
4967c478bd9Sstevel@tonic-gate 	if (mask & AT_MTIME)
4977c478bd9Sstevel@tonic-gate 		map->va_mtime = vap->va_mtime;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	if (mask & (AT_MODE | AT_UID | AT_GID | AT_MTIME)) {
5007c478bd9Sstevel@tonic-gate 		gethrestime(&map->va_ctime);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	/*
5047c478bd9Sstevel@tonic-gate 	 * A setattr to defaults means we no longer need the
5057c478bd9Sstevel@tonic-gate 	 * shadow node as a persistent store, unless there
5067c478bd9Sstevel@tonic-gate 	 * are ACLs.  Otherwise create a shadow node if one
5077c478bd9Sstevel@tonic-gate 	 * doesn't exist yet.
5087c478bd9Sstevel@tonic-gate 	 */
5097c478bd9Sstevel@tonic-gate 	if (persist) {
5107c478bd9Sstevel@tonic-gate 		if ((dv_setattr_cmp(map, &mp) == 0) &&
5117c478bd9Sstevel@tonic-gate 		    ((dv->dv_flags & DV_ACL) == 0)) {
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 			if (dv->dv_attrvp) {
5147c478bd9Sstevel@tonic-gate 				ddv = dv->dv_dotdot;
5157c478bd9Sstevel@tonic-gate 				ASSERT(ddv->dv_attrvp);
5167c478bd9Sstevel@tonic-gate 				error = VOP_REMOVE(ddv->dv_attrvp,
517da6c28aaSamw 				    dv->dv_name, cr, ct, 0);
5187c478bd9Sstevel@tonic-gate 				dsysdebug(error,
5197c478bd9Sstevel@tonic-gate 				    ("vop_remove %s %s %d\n",
5207c478bd9Sstevel@tonic-gate 				    ddv->dv_name, dv->dv_name, error));
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 				if (error == EROFS)
5237c478bd9Sstevel@tonic-gate 					error = 0;
5247c478bd9Sstevel@tonic-gate 				VN_RELE(dv->dv_attrvp);
5257c478bd9Sstevel@tonic-gate 				dv->dv_attrvp = NULL;
5267c478bd9Sstevel@tonic-gate 			}
5277c478bd9Sstevel@tonic-gate 			ASSERT(dv->dv_attr);
5287c478bd9Sstevel@tonic-gate 		} else {
5297c478bd9Sstevel@tonic-gate 			if (mask & AT_MODE)
5307c478bd9Sstevel@tonic-gate 				dcmn_err5(("%s persisting mode 0%o\n",
5317c478bd9Sstevel@tonic-gate 				    dv->dv_name, vap->va_mode));
5327c478bd9Sstevel@tonic-gate 			if (mask & AT_UID)
5337c478bd9Sstevel@tonic-gate 				dcmn_err5(("%s persisting uid %d\n",
5347c478bd9Sstevel@tonic-gate 				    dv->dv_name, vap->va_uid));
5357c478bd9Sstevel@tonic-gate 			if (mask & AT_GID)
5367c478bd9Sstevel@tonic-gate 				dcmn_err5(("%s persisting gid %d\n",
5377c478bd9Sstevel@tonic-gate 				    dv->dv_name, vap->va_gid));
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 			if (dv->dv_attrvp == NULL) {
5407c478bd9Sstevel@tonic-gate 				dvp = DVTOV(dv->dv_dotdot);
5417c478bd9Sstevel@tonic-gate 				dv_shadow_node(dvp, dv->dv_name, vp,
5427c478bd9Sstevel@tonic-gate 				    NULL, NULLVP, cr,
5437c478bd9Sstevel@tonic-gate 				    DV_SHADOW_CREATE | DV_SHADOW_WRITE_HELD);
5447c478bd9Sstevel@tonic-gate 			}
5457c478bd9Sstevel@tonic-gate 			if (dv->dv_attrvp) {
54632207c10Scth 				/* If map still valid do TIME for free. */
54732207c10Scth 				if (dv->dv_attr == map) {
54832207c10Scth 					mask = map->va_mask;
54932207c10Scth 					map->va_mask =
55032207c10Scth 					    vap->va_mask | AT_ATIME | AT_MTIME;
55132207c10Scth 					error = VOP_SETATTR(dv->dv_attrvp, map,
55232207c10Scth 					    flags, cr, NULL);
55332207c10Scth 					map->va_mask = mask;
55432207c10Scth 				} else {
5557c478bd9Sstevel@tonic-gate 					error = VOP_SETATTR(dv->dv_attrvp,
5567c478bd9Sstevel@tonic-gate 					    vap, flags, cr, NULL);
55732207c10Scth 				}
5587c478bd9Sstevel@tonic-gate 				dsysdebug(error, ("vop_setattr %s %d\n",
5597c478bd9Sstevel@tonic-gate 				    dv->dv_name, error));
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 			/*
5627c478bd9Sstevel@tonic-gate 			 * Some file systems may return EROFS for a setattr
5637c478bd9Sstevel@tonic-gate 			 * on a readonly file system.  In this case save
5647c478bd9Sstevel@tonic-gate 			 * as our own memory based attribute.
5657c478bd9Sstevel@tonic-gate 			 * NOTE: ufs is NOT one of these (see ufs_iupdat).
5667c478bd9Sstevel@tonic-gate 			 */
5677c478bd9Sstevel@tonic-gate 			if (dv->dv_attr && dv->dv_attrvp && error == 0) {
5687c478bd9Sstevel@tonic-gate 				vattrp = dv->dv_attr;
5697c478bd9Sstevel@tonic-gate 				dv->dv_attr = NULL;
5707c478bd9Sstevel@tonic-gate 			} else if (error == EROFS)
5717c478bd9Sstevel@tonic-gate 				error = 0;
5727c478bd9Sstevel@tonic-gate 		}
5737c478bd9Sstevel@tonic-gate 	}
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate out:
5767c478bd9Sstevel@tonic-gate 	rw_exit(&dv->dv_contents);
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (vattrp)
5797c478bd9Sstevel@tonic-gate 		kmem_free(vattrp, sizeof (*vattrp));
5807c478bd9Sstevel@tonic-gate 	if (free_vattr)
5817c478bd9Sstevel@tonic-gate 		kmem_free(free_vattr, sizeof (*free_vattr));
5827c478bd9Sstevel@tonic-gate 	return (error);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate static int
devfs_pathconf(vnode_t * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)586da6c28aaSamw devfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
587da6c28aaSamw     caller_context_t *ct)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	switch (cmd) {
5907c478bd9Sstevel@tonic-gate 	case _PC_ACL_ENABLED:
5917c478bd9Sstevel@tonic-gate 		/*
5927c478bd9Sstevel@tonic-gate 		 * We rely on the underlying filesystem for ACLs,
5937c478bd9Sstevel@tonic-gate 		 * so direct the query for ACL support there.
5947c478bd9Sstevel@tonic-gate 		 * ACL support isn't relative to the file
5957c478bd9Sstevel@tonic-gate 		 * and we can't guarantee that the dv node
5967c478bd9Sstevel@tonic-gate 		 * has an attribute node, so any valid
5977c478bd9Sstevel@tonic-gate 		 * attribute node will suffice.
5987c478bd9Sstevel@tonic-gate 		 */
5997c478bd9Sstevel@tonic-gate 		ASSERT(dvroot);
6007c478bd9Sstevel@tonic-gate 		ASSERT(dvroot->dv_attrvp);
601da6c28aaSamw 		return (VOP_PATHCONF(dvroot->dv_attrvp, cmd, valp, cr, ct));
6027c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 
605da6c28aaSamw 	return (fs_pathconf(vp, cmd, valp, cr, ct));
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate /*
6097c478bd9Sstevel@tonic-gate  * Let avp handle security attributes (acl's).
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate static int
devfs_getsecattr(struct vnode * vp,struct vsecattr * vsap,int flags,struct cred * cr,caller_context_t * ct)6127c478bd9Sstevel@tonic-gate devfs_getsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
613da6c28aaSamw     struct cred *cr, caller_context_t *ct)
6147c478bd9Sstevel@tonic-gate {
6157c478bd9Sstevel@tonic-gate 	dvnode_t *dv = VTODV(vp);
6167c478bd9Sstevel@tonic-gate 	struct vnode *avp;
6177c478bd9Sstevel@tonic-gate 	int	error;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_getsecattr %s\n", dv->dv_name));
6207c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	rw_enter(&dv->dv_contents, RW_READER);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	avp = dv->dv_attrvp;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	/* fabricate the acl */
6277c478bd9Sstevel@tonic-gate 	if (avp == NULL) {
628da6c28aaSamw 		error = fs_fab_acl(vp, vsap, flags, cr, ct);
6297c478bd9Sstevel@tonic-gate 		rw_exit(&dv->dv_contents);
6307c478bd9Sstevel@tonic-gate 		return (error);
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 
633da6c28aaSamw 	error = VOP_GETSECATTR(avp, vsap, flags, cr, ct);
6347c478bd9Sstevel@tonic-gate 	dsysdebug(error, ("vop_getsecattr %s %d\n", VTODV(vp)->dv_name, error));
6357c478bd9Sstevel@tonic-gate 	rw_exit(&dv->dv_contents);
6367c478bd9Sstevel@tonic-gate 	return (error);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate  * Set security attributes (acl's)
6417c478bd9Sstevel@tonic-gate  *
6427c478bd9Sstevel@tonic-gate  * Note that the dv_contents lock has already been acquired
6437c478bd9Sstevel@tonic-gate  * by the caller's VOP_RWLOCK.
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate static int
devfs_setsecattr(struct vnode * vp,struct vsecattr * vsap,int flags,struct cred * cr,caller_context_t * ct)6467c478bd9Sstevel@tonic-gate devfs_setsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
647da6c28aaSamw     struct cred *cr, caller_context_t *ct)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	dvnode_t *dv = VTODV(vp);
6507c478bd9Sstevel@tonic-gate 	struct vnode *avp;
6517c478bd9Sstevel@tonic-gate 	int	error;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_setsecattr %s\n", dv->dv_name));
6547c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR || vp->v_type == VCHR || vp->v_type == VBLK);
6557c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&dv->dv_contents));
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/*
6587c478bd9Sstevel@tonic-gate 	 * Not a supported operation on drivers not providing
6597c478bd9Sstevel@tonic-gate 	 * file system based permissions.
6607c478bd9Sstevel@tonic-gate 	 */
6617c478bd9Sstevel@tonic-gate 	if (dv->dv_flags & DV_NO_FSPERM)
6627c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/*
6657c478bd9Sstevel@tonic-gate 	 * To complete, the setsecattr requires an underlying attribute node.
6667c478bd9Sstevel@tonic-gate 	 */
6677c478bd9Sstevel@tonic-gate 	if (dv->dv_attrvp == NULL) {
6687c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
6697c478bd9Sstevel@tonic-gate 		dv_shadow_node(DVTOV(dv->dv_dotdot), dv->dv_name, vp,
6707c478bd9Sstevel@tonic-gate 		    NULL, NULLVP, cr, DV_SHADOW_CREATE | DV_SHADOW_WRITE_HELD);
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if ((avp = dv->dv_attrvp) == NULL) {
6747c478bd9Sstevel@tonic-gate 		dcmn_err2(("devfs_setsecattr %s: "
6757c478bd9Sstevel@tonic-gate 		    "cannot construct attribute node\n", dv->dv_name));
6767c478bd9Sstevel@tonic-gate 		return (fs_nosys());
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/*
6807c478bd9Sstevel@tonic-gate 	 * The acl(2) system call issues a VOP_RWLOCK before setting an ACL.
6817c478bd9Sstevel@tonic-gate 	 * Since backing file systems expect the lock to be held before seeing
6827c478bd9Sstevel@tonic-gate 	 * a VOP_SETSECATTR ACL, we need to issue the VOP_RWLOCK to the backing
6837c478bd9Sstevel@tonic-gate 	 * store before forwarding the ACL.
6847c478bd9Sstevel@tonic-gate 	 */
6857c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(avp, V_WRITELOCK_TRUE, NULL);
686da6c28aaSamw 	error = VOP_SETSECATTR(avp, vsap, flags, cr, ct);
6877c478bd9Sstevel@tonic-gate 	dsysdebug(error, ("vop_setsecattr %s %d\n", VTODV(vp)->dv_name, error));
6887c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(avp, V_WRITELOCK_TRUE, NULL);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/*
691fa9e4066Sahrens 	 * Set DV_ACL if we have a non-trivial set of ACLs.  It is not
692fa9e4066Sahrens 	 * necessary to hold VOP_RWLOCK since fs_acl_nontrivial only does
693fa9e4066Sahrens 	 * VOP_GETSECATTR calls.
6947c478bd9Sstevel@tonic-gate 	 */
695fa9e4066Sahrens 	if (fs_acl_nontrivial(avp, cr))
6967c478bd9Sstevel@tonic-gate 		dv->dv_flags |= DV_ACL;
6977c478bd9Sstevel@tonic-gate 	return (error);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate /*
7017c478bd9Sstevel@tonic-gate  * This function is used for secpolicy_setattr().  It must call an
7027c478bd9Sstevel@tonic-gate  * access() like function while it is already holding the
7037c478bd9Sstevel@tonic-gate  * dv_contents lock.  We only care about this when dv_attr != NULL;
7047c478bd9Sstevel@tonic-gate  * so the unlocked access call only concerns itself with that
7057c478bd9Sstevel@tonic-gate  * particular branch of devfs_access().
7067c478bd9Sstevel@tonic-gate  */
7077c478bd9Sstevel@tonic-gate static int
devfs_unlocked_access(void * vdv,int mode,struct cred * cr)7087c478bd9Sstevel@tonic-gate devfs_unlocked_access(void *vdv, int mode, struct cred *cr)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	struct dv_node *dv = vdv;
7117c478bd9Sstevel@tonic-gate 	int shift = 0;
7127c478bd9Sstevel@tonic-gate 	uid_t owner = dv->dv_attr->va_uid;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	/* Check access based on owner, group and public permissions. */
7157c478bd9Sstevel@tonic-gate 	if (crgetuid(cr) != owner) {
7167c478bd9Sstevel@tonic-gate 		shift += 3;
7177c478bd9Sstevel@tonic-gate 		if (groupmember(dv->dv_attr->va_gid, cr) == 0)
7187c478bd9Sstevel@tonic-gate 			shift += 3;
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
721*134a1f4eSCasper H.S. Dik 	return (secpolicy_vnode_access2(cr, DVTOV(dv), owner,
722*134a1f4eSCasper H.S. Dik 	    dv->dv_attr->va_mode << shift, mode));
7237c478bd9Sstevel@tonic-gate }
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate static int
devfs_access(struct vnode * vp,int mode,int flags,struct cred * cr,caller_context_t * ct)726da6c28aaSamw devfs_access(struct vnode *vp, int mode, int flags, struct cred *cr,
727da6c28aaSamw     caller_context_t *ct)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
7307c478bd9Sstevel@tonic-gate 	int		res;
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_access %s\n", dv->dv_name));
7337c478bd9Sstevel@tonic-gate 	ASSERT(dv->dv_attr || dv->dv_attrvp);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	/* restrict console access to privileged processes */
7367c478bd9Sstevel@tonic-gate 	if ((vp->v_rdev == rconsdev) && secpolicy_console(cr) != 0) {
7377c478bd9Sstevel@tonic-gate 		return (EACCES);
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	rw_enter(&dv->dv_contents, RW_READER);
741e37c6c37Scth 	if (dv->dv_attr && ((dv->dv_flags & DV_ACL) == 0)) {
7427c478bd9Sstevel@tonic-gate 		res = devfs_unlocked_access(dv, mode, cr);
743e37c6c37Scth 	} else {
744e37c6c37Scth 		res = VOP_ACCESS(dv->dv_attrvp, mode, flags, cr, ct);
745e37c6c37Scth 	}
7467c478bd9Sstevel@tonic-gate 	rw_exit(&dv->dv_contents);
7477c478bd9Sstevel@tonic-gate 	return (res);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate /*
7517c478bd9Sstevel@tonic-gate  * Lookup
7527c478bd9Sstevel@tonic-gate  *
7537c478bd9Sstevel@tonic-gate  * Given the directory vnode and the name of the component, return
7547c478bd9Sstevel@tonic-gate  * the corresponding held vnode for that component.
7557c478bd9Sstevel@tonic-gate  *
7567c478bd9Sstevel@tonic-gate  * Of course in these fictional filesystems, nothing's ever quite
7577c478bd9Sstevel@tonic-gate  * -that- simple.
7587c478bd9Sstevel@tonic-gate  *
7597c478bd9Sstevel@tonic-gate  * devfs name	type		shadow (fs attributes)	type	comments
7607c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
7617c478bd9Sstevel@tonic-gate  * drv[@addr]	VDIR		drv[@addr]		VDIR	nexus driver
7627c478bd9Sstevel@tonic-gate  * drv[@addr]:m	VCHR/VBLK	drv[@addr]:m		VREG	leaf driver
7637c478bd9Sstevel@tonic-gate  * drv[@addr]	VCHR/VBLK	drv[@addr]:.default	VREG	leaf driver
7647c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
7657c478bd9Sstevel@tonic-gate  *
7667c478bd9Sstevel@tonic-gate  * The following names are reserved for the attribute filesystem (which
7677c478bd9Sstevel@tonic-gate  * could easily be another layer on top of this one - we simply need to
7687c478bd9Sstevel@tonic-gate  * hold the vnode of the thing we're looking at)
7697c478bd9Sstevel@tonic-gate  *
7707c478bd9Sstevel@tonic-gate  * attr name	type		shadow (fs attributes)	type	comments
7717c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
7727c478bd9Sstevel@tonic-gate  * drv[@addr]	VDIR		-			-	attribute dir
7737c478bd9Sstevel@tonic-gate  * minorname	VDIR		-			-	minorname
7747c478bd9Sstevel@tonic-gate  * attribute	VREG		-			-	attribute
7757c478bd9Sstevel@tonic-gate  * -------------------------------------------------------------------------
7767c478bd9Sstevel@tonic-gate  *
7777c478bd9Sstevel@tonic-gate  * Examples:
7787c478bd9Sstevel@tonic-gate  *
7797c478bd9Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:zero		VCHR
7807c478bd9Sstevel@tonic-gate  *	shadow:/.devices/.../mm@0:zero		VREG, fs attrs
7817c478bd9Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:/zero/attr	VREG, driver attribute
7827c478bd9Sstevel@tonic-gate  *
7837c478bd9Sstevel@tonic-gate  *	devfs:/devices/.../sd@0,0:a		VBLK
7847c478bd9Sstevel@tonic-gate  *	shadow:/.devices/.../sd@0,0:a		VREG, fs attrs
7857c478bd9Sstevel@tonic-gate  *	devfs:/devices/.../sd@0,0:/a/.type	VREG, "ddi_block:chan"
7867c478bd9Sstevel@tonic-gate  *
7877c478bd9Sstevel@tonic-gate  *	devfs:/devices/.../mm@0			VCHR
7887c478bd9Sstevel@tonic-gate  *	shadow:/.devices/.../mm@0:.default	VREG, fs attrs
7897c478bd9Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:/.default/attr	VREG, driver attribute
7907c478bd9Sstevel@tonic-gate  *	devfs:/devices/.../mm@0:/.default/.type	VREG, "ddi_pseudo"
7917c478bd9Sstevel@tonic-gate  *
7927c478bd9Sstevel@tonic-gate  *	devfs:/devices/.../obio			VDIR
7937c478bd9Sstevel@tonic-gate  *	shadow:/devices/.../obio		VDIR, needed for fs attrs.
7947c478bd9Sstevel@tonic-gate  *	devfs:/devices/.../obio:/.default/attr	VDIR, driver attribute
7957c478bd9Sstevel@tonic-gate  *
7967c478bd9Sstevel@tonic-gate  * We also need to be able deal with "old" devices that have gone away,
7977c478bd9Sstevel@tonic-gate  * though I think that provided we return them with readdir, they can
7987c478bd9Sstevel@tonic-gate  * be removed (i.e. they don't have to respond to lookup, though it might
7997c478bd9Sstevel@tonic-gate  * be weird if they didn't ;-)
8007c478bd9Sstevel@tonic-gate  *
8017c478bd9Sstevel@tonic-gate  * Lookup has side-effects.
8027c478bd9Sstevel@tonic-gate  *
8037c478bd9Sstevel@tonic-gate  * - It will create directories and fs attribute files in the shadow hierarchy.
8047c478bd9Sstevel@tonic-gate  * - It should cause non-SID devices to be probed (ask the parent nexi).
8057c478bd9Sstevel@tonic-gate  */
8067c478bd9Sstevel@tonic-gate /*ARGSUSED3*/
8077c478bd9Sstevel@tonic-gate static int
devfs_lookup(struct vnode * dvp,char * nm,struct vnode ** vpp,struct pathname * pnp,int flags,struct vnode * rdir,struct cred * cred,caller_context_t * ct,int * direntflags,pathname_t * realpnp)8087c478bd9Sstevel@tonic-gate devfs_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
809da6c28aaSamw     struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred,
810da6c28aaSamw     caller_context_t *ct, int *direntflags, pathname_t *realpnp)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	ASSERT(dvp->v_type == VDIR);
8137c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_lookup: %s\n", nm));
8147c478bd9Sstevel@tonic-gate 	return (dv_find(VTODV(dvp), nm, vpp, pnp, rdir, cred, 0));
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate /*
8187c478bd9Sstevel@tonic-gate  * devfs nodes can't really be created directly by userland - however,
8197c478bd9Sstevel@tonic-gate  * we do allow creates to find existing nodes:
8207c478bd9Sstevel@tonic-gate  *
8217c478bd9Sstevel@tonic-gate  * - any create fails if the node doesn't exist - EROFS.
8227c478bd9Sstevel@tonic-gate  * - creating an existing directory read-only succeeds, otherwise EISDIR.
8237c478bd9Sstevel@tonic-gate  * - exclusive creates fail if the node already exists - EEXIST.
8247c478bd9Sstevel@tonic-gate  * - failure to create the snode for an existing device - ENOSYS.
8257c478bd9Sstevel@tonic-gate  */
8267c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
8277c478bd9Sstevel@tonic-gate static int
devfs_create(struct vnode * dvp,char * nm,struct vattr * vap,vcexcl_t excl,int mode,struct vnode ** vpp,struct cred * cred,int flag,caller_context_t * ct,vsecattr_t * vsecp)8287c478bd9Sstevel@tonic-gate devfs_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl,
829da6c28aaSamw     int mode, struct vnode **vpp, struct cred *cred, int flag,
830da6c28aaSamw     caller_context_t *ct, vsecattr_t *vsecp)
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	int error;
8337c478bd9Sstevel@tonic-gate 	struct vnode *vp;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_create %s\n", nm));
8367c478bd9Sstevel@tonic-gate 	error = dv_find(VTODV(dvp), nm, &vp, NULL, NULLVP, cred, 0);
8377c478bd9Sstevel@tonic-gate 	if (error == 0) {
8387c478bd9Sstevel@tonic-gate 		if (excl == EXCL)
8397c478bd9Sstevel@tonic-gate 			error = EEXIST;
8407c478bd9Sstevel@tonic-gate 		else if (vp->v_type == VDIR && (mode & VWRITE))
8417c478bd9Sstevel@tonic-gate 			error = EISDIR;
8427c478bd9Sstevel@tonic-gate 		else
843da6c28aaSamw 			error = VOP_ACCESS(vp, mode, 0, cred, ct);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		if (error) {
8467c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
8477c478bd9Sstevel@tonic-gate 		} else
8487c478bd9Sstevel@tonic-gate 			*vpp = vp;
8497c478bd9Sstevel@tonic-gate 	} else if (error == ENOENT)
8507c478bd9Sstevel@tonic-gate 		error = EROFS;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	return (error);
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate /*
8567c478bd9Sstevel@tonic-gate  * If DV_BUILD is set, we call into nexus driver to do a BUS_CONFIG_ALL.
8577c478bd9Sstevel@tonic-gate  * Otherwise, simply return cached dv_node's. Hotplug code always call
8587c478bd9Sstevel@tonic-gate  * devfs_clean() to invalid the dv_node cache.
8597c478bd9Sstevel@tonic-gate  */
860da6c28aaSamw /*ARGSUSED5*/
8617c478bd9Sstevel@tonic-gate static int
devfs_readdir(struct vnode * dvp,struct uio * uiop,struct cred * cred,int * eofp,caller_context_t * ct,int flags)862da6c28aaSamw devfs_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred, int *eofp,
863da6c28aaSamw     caller_context_t *ct, int flags)
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate 	struct dv_node *ddv, *dv;
8667c478bd9Sstevel@tonic-gate 	struct dirent64 *de, *bufp;
8677c478bd9Sstevel@tonic-gate 	offset_t diroff;
8687c478bd9Sstevel@tonic-gate 	offset_t	soff;
8697c478bd9Sstevel@tonic-gate 	size_t reclen, movesz;
8707c478bd9Sstevel@tonic-gate 	int error;
8717c478bd9Sstevel@tonic-gate 	struct vattr va;
8727c478bd9Sstevel@tonic-gate 	size_t bufsz;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	ddv = VTODV(dvp);
8757c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_readdir %s: offset %lld len %ld\n",
8767c478bd9Sstevel@tonic-gate 	    ddv->dv_name, uiop->uio_loffset, uiop->uio_iov->iov_len));
8777c478bd9Sstevel@tonic-gate 	ASSERT(ddv->dv_attr || ddv->dv_attrvp);
8787c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&ddv->dv_contents));
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	if (uiop->uio_loffset >= MAXOFF_T) {
8817c478bd9Sstevel@tonic-gate 		if (eofp)
8827c478bd9Sstevel@tonic-gate 			*eofp = 1;
8837c478bd9Sstevel@tonic-gate 		return (0);
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	if (uiop->uio_iovcnt != 1)
8877c478bd9Sstevel@tonic-gate 		return (EINVAL);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	if (dvp->v_type != VDIR)
8907c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	/* Load the initial contents */
8937c478bd9Sstevel@tonic-gate 	if (ddv->dv_flags & DV_BUILD) {
8947c478bd9Sstevel@tonic-gate 		if (!rw_tryupgrade(&ddv->dv_contents)) {
8957c478bd9Sstevel@tonic-gate 			rw_exit(&ddv->dv_contents);
8967c478bd9Sstevel@tonic-gate 			rw_enter(&ddv->dv_contents, RW_WRITER);
8977c478bd9Sstevel@tonic-gate 		}
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 		/* recheck and fill */
9007c478bd9Sstevel@tonic-gate 		if (ddv->dv_flags & DV_BUILD)
9017c478bd9Sstevel@tonic-gate 			dv_filldir(ddv);
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 		rw_downgrade(&ddv->dv_contents);
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
906bc1009abSjg 	soff = uiop->uio_loffset;
9077c478bd9Sstevel@tonic-gate 	bufsz = uiop->uio_iov->iov_len;
9087c478bd9Sstevel@tonic-gate 	de = bufp = kmem_alloc(bufsz, KM_SLEEP);
9097c478bd9Sstevel@tonic-gate 	movesz = 0;
9107c478bd9Sstevel@tonic-gate 	dv = (struct dv_node *)-1;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	/*
9137c478bd9Sstevel@tonic-gate 	 * Move as many entries into the uio structure as it will take.
9147c478bd9Sstevel@tonic-gate 	 * Special case "." and "..".
9157c478bd9Sstevel@tonic-gate 	 */
9167c478bd9Sstevel@tonic-gate 	diroff = 0;
9177c478bd9Sstevel@tonic-gate 	if (soff == 0) {				/* . */
9187c478bd9Sstevel@tonic-gate 		reclen = DIRENT64_RECLEN(strlen("."));
9197c478bd9Sstevel@tonic-gate 		if ((movesz + reclen) > bufsz)
9207c478bd9Sstevel@tonic-gate 			goto full;
9217c478bd9Sstevel@tonic-gate 		de->d_ino = (ino64_t)ddv->dv_ino;
9227c478bd9Sstevel@tonic-gate 		de->d_off = (off64_t)diroff + 1;
9237c478bd9Sstevel@tonic-gate 		de->d_reclen = (ushort_t)reclen;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		(void) strncpy(de->d_name, ".", DIRENT64_NAMELEN(reclen));
9287c478bd9Sstevel@tonic-gate 		movesz += reclen;
929bc1009abSjg 		de = (dirent64_t *)(intptr_t)((char *)de + reclen);
9307c478bd9Sstevel@tonic-gate 		dcmn_err3(("devfs_readdir: A: diroff %lld, soff %lld: '%s' "
9317c478bd9Sstevel@tonic-gate 		    "reclen %lu\n", diroff, soff, ".", reclen));
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	diroff++;
9357c478bd9Sstevel@tonic-gate 	if (soff <= 1) {				/* .. */
9367c478bd9Sstevel@tonic-gate 		reclen = DIRENT64_RECLEN(strlen(".."));
9377c478bd9Sstevel@tonic-gate 		if ((movesz + reclen) > bufsz)
9387c478bd9Sstevel@tonic-gate 			goto full;
9397c478bd9Sstevel@tonic-gate 		de->d_ino = (ino64_t)ddv->dv_dotdot->dv_ino;
9407c478bd9Sstevel@tonic-gate 		de->d_off = (off64_t)diroff + 1;
9417c478bd9Sstevel@tonic-gate 		de->d_reclen = (ushort_t)reclen;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 		(void) strncpy(de->d_name, "..", DIRENT64_NAMELEN(reclen));
9467c478bd9Sstevel@tonic-gate 		movesz += reclen;
947bc1009abSjg 		de = (dirent64_t *)(intptr_t)((char *)de + reclen);
9487c478bd9Sstevel@tonic-gate 		dcmn_err3(("devfs_readdir: B: diroff %lld, soff %lld: '%s' "
9497c478bd9Sstevel@tonic-gate 		    "reclen %lu\n", diroff, soff, "..", reclen));
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	diroff++;
953aac43a5fSjg 	for (dv = DV_FIRST_ENTRY(ddv); dv;
954aac43a5fSjg 	    dv = DV_NEXT_ENTRY(ddv, dv), diroff++) {
9554c06356bSdh142964 		/* skip entries until at correct directory offset */
9564c06356bSdh142964 		if (diroff < soff)
9574c06356bSdh142964 			continue;
9584c06356bSdh142964 
9597c478bd9Sstevel@tonic-gate 		/*
9604c06356bSdh142964 		 * hidden nodes are skipped (but they still occupy a
9614c06356bSdh142964 		 * directory offset).
9627c478bd9Sstevel@tonic-gate 		 */
9634c06356bSdh142964 		if (dv->dv_devi && ndi_dev_is_hidden_node(dv->dv_devi))
9644c06356bSdh142964 			continue;
9654c06356bSdh142964 
9664c06356bSdh142964 		/*
9674c06356bSdh142964 		 * DDM_INTERNAL_PATH minor nodes are skipped for readdirs
9684c06356bSdh142964 		 * outside the kernel (but they still occupy a directory
9694c06356bSdh142964 		 * offset).
9704c06356bSdh142964 		 */
9714c06356bSdh142964 		if ((dv->dv_flags & DV_INTERNAL) && (cred != kcred))
9727c478bd9Sstevel@tonic-gate 			continue;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		reclen = DIRENT64_RECLEN(strlen(dv->dv_name));
9757c478bd9Sstevel@tonic-gate 		if ((movesz + reclen) > bufsz) {
9767c478bd9Sstevel@tonic-gate 			dcmn_err3(("devfs_readdir: C: diroff "
9777c478bd9Sstevel@tonic-gate 			    "%lld, soff %lld: '%s' reclen %lu\n",
9787c478bd9Sstevel@tonic-gate 			    diroff, soff, dv->dv_name, reclen));
9797c478bd9Sstevel@tonic-gate 			goto full;
9807c478bd9Sstevel@tonic-gate 		}
9817c478bd9Sstevel@tonic-gate 		de->d_ino = (ino64_t)dv->dv_ino;
9827c478bd9Sstevel@tonic-gate 		de->d_off = (off64_t)diroff + 1;
9837c478bd9Sstevel@tonic-gate 		de->d_reclen = (ushort_t)reclen;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		/* use strncpy(9f) to zero out uninitialized bytes */
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 		ASSERT(strlen(dv->dv_name) + 1 <=
9887c478bd9Sstevel@tonic-gate 		    DIRENT64_NAMELEN(reclen));
9897c478bd9Sstevel@tonic-gate 		(void) strncpy(de->d_name, dv->dv_name,
9907c478bd9Sstevel@tonic-gate 		    DIRENT64_NAMELEN(reclen));
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 		movesz += reclen;
993bc1009abSjg 		de = (dirent64_t *)(intptr_t)((char *)de + reclen);
9947c478bd9Sstevel@tonic-gate 		dcmn_err4(("devfs_readdir: D: diroff "
9957c478bd9Sstevel@tonic-gate 		    "%lld, soff %lld: '%s' reclen %lu\n", diroff, soff,
9967c478bd9Sstevel@tonic-gate 		    dv->dv_name, reclen));
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	/* the buffer is full, or we exhausted everything */
10007c478bd9Sstevel@tonic-gate full:	dcmn_err3(("devfs_readdir: moving %lu bytes: "
10017c478bd9Sstevel@tonic-gate 	    "diroff %lld, soff %lld, dv %p\n",
10027c478bd9Sstevel@tonic-gate 	    movesz, diroff, soff, (void *)dv));
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	if ((movesz == 0) && dv)
10057c478bd9Sstevel@tonic-gate 		error = EINVAL;		/* cannot be represented */
10067c478bd9Sstevel@tonic-gate 	else {
10077c478bd9Sstevel@tonic-gate 		error = uiomove(bufp, movesz, UIO_READ, uiop);
10087c478bd9Sstevel@tonic-gate 		if (error == 0) {
10097c478bd9Sstevel@tonic-gate 			if (eofp)
10107c478bd9Sstevel@tonic-gate 				*eofp = dv ? 0 : 1;
1011bc1009abSjg 			uiop->uio_loffset = diroff;
10127c478bd9Sstevel@tonic-gate 		}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 		va.va_mask = AT_ATIME;
10157c478bd9Sstevel@tonic-gate 		gethrestime(&va.va_atime);
10167c478bd9Sstevel@tonic-gate 		rw_exit(&ddv->dv_contents);
1017da6c28aaSamw 		(void) devfs_setattr(dvp, &va, 0, cred, ct);
10187c478bd9Sstevel@tonic-gate 		rw_enter(&ddv->dv_contents, RW_READER);
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	kmem_free(bufp, bufsz);
10227c478bd9Sstevel@tonic-gate 	return (error);
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10267c478bd9Sstevel@tonic-gate static int
devfs_fsync(struct vnode * vp,int syncflag,struct cred * cred,caller_context_t * ct)1027da6c28aaSamw devfs_fsync(struct vnode *vp, int syncflag, struct cred *cred,
1028da6c28aaSamw     caller_context_t *ct)
10297c478bd9Sstevel@tonic-gate {
10307c478bd9Sstevel@tonic-gate 	/*
10317c478bd9Sstevel@tonic-gate 	 * Message goes to console only. Otherwise, the message
10327c478bd9Sstevel@tonic-gate 	 * causes devfs_fsync to be invoked again... infinite loop
10337c478bd9Sstevel@tonic-gate 	 */
10347c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_fsync %s\n", VTODV(vp)->dv_name));
10357c478bd9Sstevel@tonic-gate 	return (0);
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate /*
10397c478bd9Sstevel@tonic-gate  * Normally, we leave the dv_node here at count of 0.
10407c478bd9Sstevel@tonic-gate  * The node will be destroyed when dv_cleandir() is called.
10417c478bd9Sstevel@tonic-gate  *
10427c478bd9Sstevel@tonic-gate  * Stale dv_node's are already unlinked from the fs tree,
10437c478bd9Sstevel@tonic-gate  * so dv_cleandir() won't find them. We destroy such nodes
10447c478bd9Sstevel@tonic-gate  * immediately.
10457c478bd9Sstevel@tonic-gate  */
10467c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
10477c478bd9Sstevel@tonic-gate static void
devfs_inactive(struct vnode * vp,struct cred * cred,caller_context_t * ct)1048da6c28aaSamw devfs_inactive(struct vnode *vp, struct cred *cred, caller_context_t *ct)
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate 	int destroy;
10517c478bd9Sstevel@tonic-gate 	struct dv_node *dv = VTODV(vp);
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_inactive: %s\n", dv->dv_name));
10547c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
10557c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_count >= 1);
10567c478bd9Sstevel@tonic-gate 	--vp->v_count;
10577c478bd9Sstevel@tonic-gate 	destroy = (DV_STALE(dv) && vp->v_count == 0);
10587c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	/* stale nodes cannot be rediscovered, destroy it here */
10617c478bd9Sstevel@tonic-gate 	if (destroy)
10627c478bd9Sstevel@tonic-gate 		dv_destroy(dv, 0);
10637c478bd9Sstevel@tonic-gate }
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate /*
10667c478bd9Sstevel@tonic-gate  * XXX Why do we need this?  NFS mounted /dev directories?
10677c478bd9Sstevel@tonic-gate  * XXX Talk to peter staubach about this.
10687c478bd9Sstevel@tonic-gate  */
1069da6c28aaSamw /*ARGSUSED2*/
10707c478bd9Sstevel@tonic-gate static int
devfs_fid(struct vnode * vp,struct fid * fidp,caller_context_t * ct)1071da6c28aaSamw devfs_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate 	struct dv_node	*dv = VTODV(vp);
10747c478bd9Sstevel@tonic-gate 	struct dv_fid	*dv_fid;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	if (fidp->fid_len < (sizeof (struct dv_fid) - sizeof (ushort_t))) {
10777c478bd9Sstevel@tonic-gate 		fidp->fid_len = sizeof (struct dv_fid) - sizeof (ushort_t);
10787c478bd9Sstevel@tonic-gate 		return (ENOSPC);
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	dv_fid = (struct dv_fid *)fidp;
10827c478bd9Sstevel@tonic-gate 	bzero(dv_fid, sizeof (struct dv_fid));
10837c478bd9Sstevel@tonic-gate 	dv_fid->dvfid_len = (int)sizeof (struct dv_fid) - sizeof (ushort_t);
10847c478bd9Sstevel@tonic-gate 	dv_fid->dvfid_ino = dv->dv_ino;
10857c478bd9Sstevel@tonic-gate 	/* dv_fid->dvfid_gen = dv->tn_gen; XXX ? */
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	return (0);
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate /*
10917c478bd9Sstevel@tonic-gate  * This pair of routines bracket all VOP_READ, VOP_WRITE
10927c478bd9Sstevel@tonic-gate  * and VOP_READDIR requests.  The contents lock stops things
10937c478bd9Sstevel@tonic-gate  * moving around while we're looking at them.
10947c478bd9Sstevel@tonic-gate  *
10957c478bd9Sstevel@tonic-gate  * Also used by file and record locking.
10967c478bd9Sstevel@tonic-gate  */
10977c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
10987c478bd9Sstevel@tonic-gate static int
devfs_rwlock(struct vnode * vp,int write_flag,caller_context_t * ct)10997c478bd9Sstevel@tonic-gate devfs_rwlock(struct vnode *vp, int write_flag, caller_context_t *ct)
11007c478bd9Sstevel@tonic-gate {
11017c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_rwlock %s\n", VTODV(vp)->dv_name));
11027c478bd9Sstevel@tonic-gate 	rw_enter(&VTODV(vp)->dv_contents, write_flag ? RW_WRITER : RW_READER);
11037c478bd9Sstevel@tonic-gate 	return (write_flag);
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
11077c478bd9Sstevel@tonic-gate static void
devfs_rwunlock(struct vnode * vp,int write_flag,caller_context_t * ct)11087c478bd9Sstevel@tonic-gate devfs_rwunlock(struct vnode *vp, int write_flag, caller_context_t *ct)
11097c478bd9Sstevel@tonic-gate {
11107c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_rwunlock %s\n", VTODV(vp)->dv_name));
11117c478bd9Sstevel@tonic-gate 	rw_exit(&VTODV(vp)->dv_contents);
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate /*
11157c478bd9Sstevel@tonic-gate  * XXX	Should probably do a better job of computing the maximum
11167c478bd9Sstevel@tonic-gate  *	offset available in the directory.
11177c478bd9Sstevel@tonic-gate  */
11187c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
11197c478bd9Sstevel@tonic-gate static int
devfs_seek(struct vnode * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)1120da6c28aaSamw devfs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
1121da6c28aaSamw     caller_context_t *ct)
11227c478bd9Sstevel@tonic-gate {
11237c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VDIR);
11247c478bd9Sstevel@tonic-gate 	dcmn_err2(("devfs_seek %s\n", VTODV(vp)->dv_name));
11257c478bd9Sstevel@tonic-gate 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate vnodeops_t *dv_vnodeops;
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate const fs_operation_def_t dv_vnodeops_template[] = {
1131aa59c4cbSrsb 	VOPNAME_OPEN,		{ .vop_open = devfs_open },
1132aa59c4cbSrsb 	VOPNAME_CLOSE,		{ .vop_close = devfs_close },
1133aa59c4cbSrsb 	VOPNAME_READ,		{ .vop_read = devfs_read },
1134aa59c4cbSrsb 	VOPNAME_WRITE,		{ .vop_write = devfs_write },
1135aa59c4cbSrsb 	VOPNAME_IOCTL,		{ .vop_ioctl = devfs_ioctl },
1136aa59c4cbSrsb 	VOPNAME_GETATTR,	{ .vop_getattr = devfs_getattr },
1137aa59c4cbSrsb 	VOPNAME_SETATTR,	{ .vop_setattr = devfs_setattr },
1138aa59c4cbSrsb 	VOPNAME_ACCESS,		{ .vop_access = devfs_access },
1139aa59c4cbSrsb 	VOPNAME_LOOKUP,		{ .vop_lookup = devfs_lookup },
1140aa59c4cbSrsb 	VOPNAME_CREATE,		{ .vop_create = devfs_create },
1141aa59c4cbSrsb 	VOPNAME_READDIR,	{ .vop_readdir = devfs_readdir },
1142aa59c4cbSrsb 	VOPNAME_FSYNC,		{ .vop_fsync = devfs_fsync },
1143aa59c4cbSrsb 	VOPNAME_INACTIVE,	{ .vop_inactive = devfs_inactive },
1144aa59c4cbSrsb 	VOPNAME_FID,		{ .vop_fid = devfs_fid },
1145aa59c4cbSrsb 	VOPNAME_RWLOCK,		{ .vop_rwlock = devfs_rwlock },
1146aa59c4cbSrsb 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = devfs_rwunlock },
1147aa59c4cbSrsb 	VOPNAME_SEEK,		{ .vop_seek = devfs_seek },
1148aa59c4cbSrsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = devfs_pathconf },
1149aa59c4cbSrsb 	VOPNAME_DISPOSE,	{ .error = fs_error },
1150aa59c4cbSrsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = devfs_setsecattr },
1151aa59c4cbSrsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = devfs_getsecattr },
11527c478bd9Sstevel@tonic-gate 	NULL,			NULL
11537c478bd9Sstevel@tonic-gate };
1154