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