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
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * 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 /*
22aa59c4cbSrsb  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25*ade42b55SSebastien Roy /*
26*ade42b55SSebastien Roy  * Copyright (c) 2017 by Delphix. All rights reserved.
27*ade42b55SSebastien Roy  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
31aa59c4cbSrsb #include <sys/vfs_opreg.h>
327c478bd9Sstevel@tonic-gate #include <sys/door.h>
337c478bd9Sstevel@tonic-gate #include <sys/proc.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
377c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
3845916cd2Sjpk #include <sys/zone.h>
3945916cd2Sjpk #include <sys/tsol/label.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate kmutex_t	door_knob;
42da6c28aaSamw static int	door_open(struct vnode **vpp, int flag, struct cred *cr,
43da6c28aaSamw 			caller_context_t *ct);
447c478bd9Sstevel@tonic-gate static int	door_close(struct vnode *vp, int flag, int count,
45da6c28aaSamw 			offset_t offset, struct cred *cr, caller_context_t *ct);
467c478bd9Sstevel@tonic-gate static int	door_getattr(struct vnode *vp, struct vattr *vap,
47da6c28aaSamw 			int flags, struct cred *cr, caller_context_t *ct);
48da6c28aaSamw static void	door_inactive(struct vnode *vp, struct cred *cr,
49da6c28aaSamw 			caller_context_t *ct);
507c478bd9Sstevel@tonic-gate static int	door_access(struct vnode *vp, int mode, int flags,
51da6c28aaSamw 			struct cred *cr, caller_context_t *ct);
52da6c28aaSamw static int	door_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate struct vfs door_vfs;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate struct vnodeops *door_vnodeops;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate const fs_operation_def_t door_vnodeops_template[] = {
59aa59c4cbSrsb 	VOPNAME_OPEN,		{ .vop_open = door_open },
60aa59c4cbSrsb 	VOPNAME_CLOSE,		{ .vop_close = door_close },
61aa59c4cbSrsb 	VOPNAME_GETATTR,	{ .vop_getattr = door_getattr },
62aa59c4cbSrsb 	VOPNAME_ACCESS,		{ .vop_access = door_access },
63aa59c4cbSrsb 	VOPNAME_INACTIVE,	{ .vop_inactive = door_inactive },
64aa59c4cbSrsb 	VOPNAME_FRLOCK,		{ .error = fs_error },
65aa59c4cbSrsb 	VOPNAME_REALVP,		{ .vop_realvp = door_realvp },
66aa59c4cbSrsb 	VOPNAME_POLL,		{ .error = fs_error },
67aa59c4cbSrsb 	VOPNAME_PATHCONF,	{ .error = fs_error },
68aa59c4cbSrsb 	VOPNAME_DISPOSE,	{ .error = fs_error },
69aa59c4cbSrsb 	VOPNAME_GETSECATTR,	{ .error = fs_error },
70aa59c4cbSrsb 	VOPNAME_SHRLOCK,	{ .error = fs_error },
71aa59c4cbSrsb 	NULL,			NULL
727c478bd9Sstevel@tonic-gate };
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* ARGSUSED */
757c478bd9Sstevel@tonic-gate static int
door_open(struct vnode ** vpp,int flag,struct cred * cr,caller_context_t * ct)76da6c28aaSamw door_open(struct vnode **vpp, int flag, struct cred *cr, caller_context_t *ct)
777c478bd9Sstevel@tonic-gate {
7845916cd2Sjpk 	/*
7945916cd2Sjpk 	 * MAC policy for doors.  Restrict cross-zone open()s so that only
8045916cd2Sjpk 	 * door servers in the global zone can have clients from other zones.
8145916cd2Sjpk 	 * For other zones, client must be within the same zone as server.
8245916cd2Sjpk 	 */
8345916cd2Sjpk 	if (is_system_labeled()) {
8445916cd2Sjpk 		zone_t		*server_zone, *client_zone;
8545916cd2Sjpk 		door_node_t	*dp = VTOD((*vpp));
8645916cd2Sjpk 
8745916cd2Sjpk 		mutex_enter(&door_knob);
8845916cd2Sjpk 		if (DOOR_INVALID(dp)) {
8945916cd2Sjpk 			mutex_exit(&door_knob);
9045916cd2Sjpk 			return (0);
9145916cd2Sjpk 		}
9245916cd2Sjpk 		client_zone = curproc->p_zone;
9345916cd2Sjpk 		server_zone = dp->door_target->p_zone;
9445916cd2Sjpk 		mutex_exit(&door_knob);
9545916cd2Sjpk 		if (server_zone != global_zone &&
9645916cd2Sjpk 		    server_zone != client_zone)
9745916cd2Sjpk 			return (EACCES);
9845916cd2Sjpk 	}
997c478bd9Sstevel@tonic-gate 	return (0);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /* ARGSUSED */
1037c478bd9Sstevel@tonic-gate static int
door_close(struct vnode * vp,int flag,int count,offset_t offset,struct cred * cr,caller_context_t * ct)104*ade42b55SSebastien Roy door_close(struct vnode *vp, int flag, int count, offset_t offset,
105*ade42b55SSebastien Roy     struct cred *cr, caller_context_t *ct)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	door_node_t	*dp = VTOD(vp);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/*
1107c478bd9Sstevel@tonic-gate 	 * If this is being called from closeall on exit, any doors created
1117c478bd9Sstevel@tonic-gate 	 * by this process should have been revoked already in door_exit.
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	ASSERT(dp->door_target != curproc ||
1147c478bd9Sstevel@tonic-gate 	    ((curthread->t_proc_flag & TP_LWPEXIT) == 0));
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/*
1177c478bd9Sstevel@tonic-gate 	 * Deliver an unref if needed.
1187c478bd9Sstevel@tonic-gate 	 *
1197c478bd9Sstevel@tonic-gate 	 * If the count is equal to 2, it means that I'm doing a VOP_CLOSE
1207c478bd9Sstevel@tonic-gate 	 * on the next to last reference for *this* file struct. There may
1217c478bd9Sstevel@tonic-gate 	 * be multiple files pointing to this vnode in which case the v_count
1227c478bd9Sstevel@tonic-gate 	 * will be > 1.
1237c478bd9Sstevel@tonic-gate 	 *
1247c478bd9Sstevel@tonic-gate 	 * The door_active count is bumped during each invocation.
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 	if (count == 2 && vp->v_count == 1 &&
1277c478bd9Sstevel@tonic-gate 	    (dp->door_flags & (DOOR_UNREF | DOOR_UNREF_MULTI))) {
1287c478bd9Sstevel@tonic-gate 		mutex_enter(&door_knob);
1297c478bd9Sstevel@tonic-gate 		if (dp->door_active == 0) {
1307c478bd9Sstevel@tonic-gate 			/* o.k. to deliver unref now */
1317c478bd9Sstevel@tonic-gate 			door_deliver_unref(dp);
1327c478bd9Sstevel@tonic-gate 		} else {
1337c478bd9Sstevel@tonic-gate 			/* do the unref later */
1347c478bd9Sstevel@tonic-gate 			dp->door_flags |= DOOR_DELAY;
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		mutex_exit(&door_knob);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	return (0);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /* ARGSUSED */
1427c478bd9Sstevel@tonic-gate static int
door_getattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cr,caller_context_t * ct)143da6c28aaSamw door_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr,
144*ade42b55SSebastien Roy     caller_context_t *ct)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	static timestruc_t tzero = {0, 0};
1477c478bd9Sstevel@tonic-gate 	extern dev_t doordev;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	vap->va_mask = 0;		/* bit-mask of attributes */
1507c478bd9Sstevel@tonic-gate 	vap->va_type = vp->v_type;	/* vnode type (for create) */
1517c478bd9Sstevel@tonic-gate 	vap->va_mode = 0777;		/* file access mode */
1527c478bd9Sstevel@tonic-gate 	vap->va_uid = 0;		/* owner user id */
1537c478bd9Sstevel@tonic-gate 	vap->va_gid = 0;		/* owner group id */
1547c478bd9Sstevel@tonic-gate 	vap->va_fsid = doordev;		/* file system id (dev for now) */
1557c478bd9Sstevel@tonic-gate 	vap->va_nodeid = (ino64_t)0;		/* node id */
1567c478bd9Sstevel@tonic-gate 	vap->va_nlink = vp->v_count;	/* number of references to file */
1577c478bd9Sstevel@tonic-gate 	vap->va_size = (u_offset_t)0;		/* file size in bytes */
1587c478bd9Sstevel@tonic-gate 	vap->va_atime = tzero;		/* time of last access */
1597c478bd9Sstevel@tonic-gate 	vap->va_mtime = tzero;		/* time of last modification */
1607c478bd9Sstevel@tonic-gate 	vap->va_ctime = tzero;		/* time file ``created'' */
1617c478bd9Sstevel@tonic-gate 	vap->va_rdev = doordev;		/* device the file represents */
1627c478bd9Sstevel@tonic-gate 	vap->va_blksize = 0;		/* fundamental block size */
1637c478bd9Sstevel@tonic-gate 	vap->va_nblocks = (fsblkcnt64_t)0;	/* # of blocks allocated */
1647c478bd9Sstevel@tonic-gate 	vap->va_seq = 0;		/* sequence number */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	return (0);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /* ARGSUSED */
1707c478bd9Sstevel@tonic-gate static void
door_inactive(struct vnode * vp,struct cred * cr,caller_context_t * ct)171da6c28aaSamw door_inactive(struct vnode *vp, struct cred *cr, caller_context_t *ct)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	door_node_t *dp = VTOD(vp);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * Once the door_node is unreferenced, it stays unreferenced,
1787c478bd9Sstevel@tonic-gate 	 * so we can simply return if there are active thread bindings;
1797c478bd9Sstevel@tonic-gate 	 * the final door_unbind_thread() will re-invoke us.
1807c478bd9Sstevel@tonic-gate 	 */
1817c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_count == 1);
1827c478bd9Sstevel@tonic-gate 	if (dp->door_bound_threads > 0) {
183*ade42b55SSebastien Roy 		VN_RELE_LOCKED(vp);
1847c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
1857c478bd9Sstevel@tonic-gate 		return;
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	/* if not revoked, remove door from per-process list */
1907c478bd9Sstevel@tonic-gate 	if (dp->door_target) {
1917c478bd9Sstevel@tonic-gate 		mutex_enter(&door_knob);
1927c478bd9Sstevel@tonic-gate 		if (dp->door_target)	/* recheck door_target under lock */
1937c478bd9Sstevel@tonic-gate 			door_list_delete(dp);
1947c478bd9Sstevel@tonic-gate 		mutex_exit(&door_knob);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	vn_invalid(vp);
1977c478bd9Sstevel@tonic-gate 	vn_free(vp);
1987c478bd9Sstevel@tonic-gate 	kmem_free(dp, sizeof (door_node_t));
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * To avoid having bound threads interfere with unref processing, we
2037c478bd9Sstevel@tonic-gate  * don't use VN_HOLD/VN_RELE to track threads bound to our private
2047c478bd9Sstevel@tonic-gate  * pool.  Instead, we keep a separate counter, also under v_lock.
2057c478bd9Sstevel@tonic-gate  */
2067c478bd9Sstevel@tonic-gate void
door_bind_thread(door_node_t * dp)2077c478bd9Sstevel@tonic-gate door_bind_thread(door_node_t *dp)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	vnode_t *vp = DTOV(dp);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
2127c478bd9Sstevel@tonic-gate 	dp->door_bound_threads++;
2137c478bd9Sstevel@tonic-gate 	ASSERT(dp->door_bound_threads > 0 && vp->v_count > 0);
2147c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate void
door_unbind_thread(door_node_t * dp)2187c478bd9Sstevel@tonic-gate door_unbind_thread(door_node_t *dp)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	vnode_t *vp = DTOV(dp);
2217c478bd9Sstevel@tonic-gate 	int do_inactive = 0;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
2247c478bd9Sstevel@tonic-gate 	ASSERT(dp->door_bound_threads > 0);
2257c478bd9Sstevel@tonic-gate 	if (--dp->door_bound_threads == 0 && vp->v_count == 0) {
2267c478bd9Sstevel@tonic-gate 		/* set up for inactive handling */
227*ade42b55SSebastien Roy 		VN_HOLD_LOCKED(vp);
2287c478bd9Sstevel@tonic-gate 		do_inactive = 1;
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (do_inactive)
233da6c28aaSamw 		door_inactive(vp, NULL, NULL);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /* ARGSUSED */
2377c478bd9Sstevel@tonic-gate static int
door_access(struct vnode * vp,int mode,int flags,struct cred * cr,caller_context_t * ct)238da6c28aaSamw door_access(struct vnode *vp, int mode, int flags, struct cred *cr,
239*ade42b55SSebastien Roy     caller_context_t *ct)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	return (0);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /* ARGSUSED */
2457c478bd9Sstevel@tonic-gate static int
door_realvp(vnode_t * vp,vnode_t ** vpp,caller_context_t * ct)246da6c28aaSamw door_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	*vpp = vp;
2497c478bd9Sstevel@tonic-gate 	return (0);
2507c478bd9Sstevel@tonic-gate }
251