xref: /onnv-gate/usr/src/uts/common/fs/doorfs/door_vnops.c (revision 5331:3047ad28a67b)
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
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223898Srsb  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/vnode.h>
303898Srsb #include <sys/vfs_opreg.h>
310Sstevel@tonic-gate #include <sys/door.h>
320Sstevel@tonic-gate #include <sys/proc.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/debug.h>
350Sstevel@tonic-gate #include <sys/cmn_err.h>
360Sstevel@tonic-gate #include <fs/fs_subr.h>
371676Sjpk #include <sys/zone.h>
381676Sjpk #include <sys/tsol/label.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate kmutex_t	door_knob;
41*5331Samw static int	door_open(struct vnode **vpp, int flag, struct cred *cr,
42*5331Samw 			caller_context_t *ct);
430Sstevel@tonic-gate static int	door_close(struct vnode *vp, int flag, int count,
44*5331Samw 			offset_t offset, struct cred *cr, caller_context_t *ct);
450Sstevel@tonic-gate static int	door_getattr(struct vnode *vp, struct vattr *vap,
46*5331Samw 			int flags, struct cred *cr, caller_context_t *ct);
47*5331Samw static void	door_inactive(struct vnode *vp, struct cred *cr,
48*5331Samw 			caller_context_t *ct);
490Sstevel@tonic-gate static int	door_access(struct vnode *vp, int mode, int flags,
50*5331Samw 			struct cred *cr, caller_context_t *ct);
51*5331Samw static int	door_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate struct vfs door_vfs;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate struct vnodeops *door_vnodeops;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate const fs_operation_def_t door_vnodeops_template[] = {
583898Srsb 	VOPNAME_OPEN,		{ .vop_open = door_open },
593898Srsb 	VOPNAME_CLOSE,		{ .vop_close = door_close },
603898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = door_getattr },
613898Srsb 	VOPNAME_ACCESS,		{ .vop_access = door_access },
623898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = door_inactive },
633898Srsb 	VOPNAME_FRLOCK,		{ .error = fs_error },
643898Srsb 	VOPNAME_REALVP,		{ .vop_realvp = door_realvp },
653898Srsb 	VOPNAME_POLL,		{ .error = fs_error },
663898Srsb 	VOPNAME_PATHCONF,	{ .error = fs_error },
673898Srsb 	VOPNAME_DISPOSE,	{ .error = fs_error },
683898Srsb 	VOPNAME_GETSECATTR,	{ .error = fs_error },
693898Srsb 	VOPNAME_SHRLOCK,	{ .error = fs_error },
703898Srsb 	NULL,			NULL
710Sstevel@tonic-gate };
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* ARGSUSED */
740Sstevel@tonic-gate static int
door_open(struct vnode ** vpp,int flag,struct cred * cr,caller_context_t * ct)75*5331Samw door_open(struct vnode **vpp, int flag, struct cred *cr, caller_context_t *ct)
760Sstevel@tonic-gate {
771676Sjpk 	/*
781676Sjpk 	 * MAC policy for doors.  Restrict cross-zone open()s so that only
791676Sjpk 	 * door servers in the global zone can have clients from other zones.
801676Sjpk 	 * For other zones, client must be within the same zone as server.
811676Sjpk 	 */
821676Sjpk 	if (is_system_labeled()) {
831676Sjpk 		zone_t		*server_zone, *client_zone;
841676Sjpk 		door_node_t	*dp = VTOD((*vpp));
851676Sjpk 
861676Sjpk 		mutex_enter(&door_knob);
871676Sjpk 		if (DOOR_INVALID(dp)) {
881676Sjpk 			mutex_exit(&door_knob);
891676Sjpk 			return (0);
901676Sjpk 		}
911676Sjpk 		client_zone = curproc->p_zone;
921676Sjpk 		server_zone = dp->door_target->p_zone;
931676Sjpk 		mutex_exit(&door_knob);
941676Sjpk 		if (server_zone != global_zone &&
951676Sjpk 		    server_zone != client_zone)
961676Sjpk 			return (EACCES);
971676Sjpk 	}
980Sstevel@tonic-gate 	return (0);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /* ARGSUSED */
1020Sstevel@tonic-gate static int
door_close(struct vnode * vp,int flag,int count,offset_t offset,struct cred * cr,caller_context_t * ct)1030Sstevel@tonic-gate door_close(
1040Sstevel@tonic-gate 	struct vnode *vp,
1050Sstevel@tonic-gate 	int flag,
1060Sstevel@tonic-gate 	int count,
1070Sstevel@tonic-gate 	offset_t offset,
108*5331Samw 	struct cred *cr,
109*5331Samw 	caller_context_t *ct
1100Sstevel@tonic-gate )
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	door_node_t	*dp = VTOD(vp);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	/*
1150Sstevel@tonic-gate 	 * If this is being called from closeall on exit, any doors created
1160Sstevel@tonic-gate 	 * by this process should have been revoked already in door_exit.
1170Sstevel@tonic-gate 	 */
1180Sstevel@tonic-gate 	ASSERT(dp->door_target != curproc ||
1190Sstevel@tonic-gate 	    ((curthread->t_proc_flag & TP_LWPEXIT) == 0));
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	/*
1220Sstevel@tonic-gate 	 * Deliver an unref if needed.
1230Sstevel@tonic-gate 	 *
1240Sstevel@tonic-gate 	 * If the count is equal to 2, it means that I'm doing a VOP_CLOSE
1250Sstevel@tonic-gate 	 * on the next to last reference for *this* file struct. There may
1260Sstevel@tonic-gate 	 * be multiple files pointing to this vnode in which case the v_count
1270Sstevel@tonic-gate 	 * will be > 1.
1280Sstevel@tonic-gate 	 *
1290Sstevel@tonic-gate 	 * The door_active count is bumped during each invocation.
1300Sstevel@tonic-gate 	 */
1310Sstevel@tonic-gate 	if (count == 2 && vp->v_count == 1 &&
1320Sstevel@tonic-gate 	    (dp->door_flags & (DOOR_UNREF | DOOR_UNREF_MULTI))) {
1330Sstevel@tonic-gate 		mutex_enter(&door_knob);
1340Sstevel@tonic-gate 		if (dp->door_active == 0) {
1350Sstevel@tonic-gate 			/* o.k. to deliver unref now */
1360Sstevel@tonic-gate 			door_deliver_unref(dp);
1370Sstevel@tonic-gate 		} else {
1380Sstevel@tonic-gate 			/* do the unref later */
1390Sstevel@tonic-gate 			dp->door_flags |= DOOR_DELAY;
1400Sstevel@tonic-gate 		}
1410Sstevel@tonic-gate 		mutex_exit(&door_knob);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 	return (0);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /* ARGSUSED */
1470Sstevel@tonic-gate static int
door_getattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cr,caller_context_t * ct)148*5331Samw door_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr,
149*5331Samw 	caller_context_t *ct)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate 	static timestruc_t tzero = {0, 0};
1520Sstevel@tonic-gate 	extern dev_t doordev;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	vap->va_mask = 0;		/* bit-mask of attributes */
1550Sstevel@tonic-gate 	vap->va_type = vp->v_type;	/* vnode type (for create) */
1560Sstevel@tonic-gate 	vap->va_mode = 0777;		/* file access mode */
1570Sstevel@tonic-gate 	vap->va_uid = 0;		/* owner user id */
1580Sstevel@tonic-gate 	vap->va_gid = 0;		/* owner group id */
1590Sstevel@tonic-gate 	vap->va_fsid = doordev;		/* file system id (dev for now) */
1600Sstevel@tonic-gate 	vap->va_nodeid = (ino64_t)0;		/* node id */
1610Sstevel@tonic-gate 	vap->va_nlink = vp->v_count;	/* number of references to file */
1620Sstevel@tonic-gate 	vap->va_size = (u_offset_t)0;		/* file size in bytes */
1630Sstevel@tonic-gate 	vap->va_atime = tzero;		/* time of last access */
1640Sstevel@tonic-gate 	vap->va_mtime = tzero;		/* time of last modification */
1650Sstevel@tonic-gate 	vap->va_ctime = tzero;		/* time file ``created'' */
1660Sstevel@tonic-gate 	vap->va_rdev = doordev;		/* device the file represents */
1670Sstevel@tonic-gate 	vap->va_blksize = 0;		/* fundamental block size */
1680Sstevel@tonic-gate 	vap->va_nblocks = (fsblkcnt64_t)0;	/* # of blocks allocated */
1690Sstevel@tonic-gate 	vap->va_seq = 0;		/* sequence number */
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	return (0);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /* ARGSUSED */
1750Sstevel@tonic-gate static void
door_inactive(struct vnode * vp,struct cred * cr,caller_context_t * ct)176*5331Samw door_inactive(struct vnode *vp, struct cred *cr, caller_context_t *ct)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	door_node_t *dp = VTOD(vp);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Once the door_node is unreferenced, it stays unreferenced,
1830Sstevel@tonic-gate 	 * so we can simply return if there are active thread bindings;
1840Sstevel@tonic-gate 	 * the final door_unbind_thread() will re-invoke us.
1850Sstevel@tonic-gate 	 */
1860Sstevel@tonic-gate 	ASSERT(vp->v_count == 1);
1870Sstevel@tonic-gate 	if (dp->door_bound_threads > 0) {
1880Sstevel@tonic-gate 		vp->v_count--;
1890Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
1900Sstevel@tonic-gate 		return;
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	/* if not revoked, remove door from per-process list */
1950Sstevel@tonic-gate 	if (dp->door_target) {
1960Sstevel@tonic-gate 		mutex_enter(&door_knob);
1970Sstevel@tonic-gate 		if (dp->door_target)	/* recheck door_target under lock */
1980Sstevel@tonic-gate 			door_list_delete(dp);
1990Sstevel@tonic-gate 		mutex_exit(&door_knob);
2000Sstevel@tonic-gate 	}
2010Sstevel@tonic-gate 	vn_invalid(vp);
2020Sstevel@tonic-gate 	vn_free(vp);
2030Sstevel@tonic-gate 	kmem_free(dp, sizeof (door_node_t));
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * To avoid having bound threads interfere with unref processing, we
2080Sstevel@tonic-gate  * don't use VN_HOLD/VN_RELE to track threads bound to our private
2090Sstevel@tonic-gate  * pool.  Instead, we keep a separate counter, also under v_lock.
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate void
door_bind_thread(door_node_t * dp)2120Sstevel@tonic-gate door_bind_thread(door_node_t *dp)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate 	vnode_t *vp = DTOV(dp);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
2170Sstevel@tonic-gate 	dp->door_bound_threads++;
2180Sstevel@tonic-gate 	ASSERT(dp->door_bound_threads > 0 && vp->v_count > 0);
2190Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate void
door_unbind_thread(door_node_t * dp)2230Sstevel@tonic-gate door_unbind_thread(door_node_t *dp)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	vnode_t *vp = DTOV(dp);
2260Sstevel@tonic-gate 	int do_inactive = 0;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
2290Sstevel@tonic-gate 	ASSERT(dp->door_bound_threads > 0);
2300Sstevel@tonic-gate 	if (--dp->door_bound_threads == 0 && vp->v_count == 0) {
2310Sstevel@tonic-gate 		/* set up for inactive handling */
2320Sstevel@tonic-gate 		vp->v_count++;
2330Sstevel@tonic-gate 		do_inactive = 1;
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	if (do_inactive)
238*5331Samw 		door_inactive(vp, NULL, NULL);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /* ARGSUSED */
2420Sstevel@tonic-gate static int
door_access(struct vnode * vp,int mode,int flags,struct cred * cr,caller_context_t * ct)243*5331Samw door_access(struct vnode *vp, int mode, int flags, struct cred *cr,
244*5331Samw 	caller_context_t *ct)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	return (0);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /* ARGSUSED */
2500Sstevel@tonic-gate static int
door_realvp(vnode_t * vp,vnode_t ** vpp,caller_context_t * ct)251*5331Samw door_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	*vpp = vp;
2540Sstevel@tonic-gate 	return (0);
2550Sstevel@tonic-gate }
256