xref: /onnv-gate/usr/src/uts/common/fs/ctfs/ctfs_ctl.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
53898Srsb  * Common Development and Distribution License (the "License").
63898Srsb  * 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/param.h>
300Sstevel@tonic-gate #include <sys/time.h>
310Sstevel@tonic-gate #include <sys/cred.h>
320Sstevel@tonic-gate #include <sys/vfs.h>
333898Srsb #include <sys/vfs_opreg.h>
340Sstevel@tonic-gate #include <sys/gfs.h>
350Sstevel@tonic-gate #include <sys/vnode.h>
360Sstevel@tonic-gate #include <sys/systm.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #include <fs/fs_subr.h>
400Sstevel@tonic-gate #include <sys/contract.h>
410Sstevel@tonic-gate #include <sys/contract_impl.h>
420Sstevel@tonic-gate #include <sys/ctfs.h>
430Sstevel@tonic-gate #include <sys/ctfs_impl.h>
440Sstevel@tonic-gate #include <sys/file.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * CTFS routines for the /system/contract/<type>/<ctid>/ctl vnode.
480Sstevel@tonic-gate  * CTFS routines for the /system/contract/<type>/<ctid>/status vnode.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * ctfs_create_ctlnode
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * If necessary, creates a ctlnode for a ctl file and inserts it into
550Sstevel@tonic-gate  * the specified cdirnode's gfs_dir_t.  Returns either the existing
560Sstevel@tonic-gate  * vnode or the new one.
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate vnode_t *
ctfs_create_ctlnode(vnode_t * pvp)590Sstevel@tonic-gate ctfs_create_ctlnode(vnode_t *pvp)
600Sstevel@tonic-gate {
610Sstevel@tonic-gate 	ctfs_ctlnode_t *ctlnode;
620Sstevel@tonic-gate 	ctfs_cdirnode_t *cdirnode = pvp->v_data;
630Sstevel@tonic-gate 	vnode_t *vp;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_ctl);
660Sstevel@tonic-gate 	ctlnode = vp->v_data;
670Sstevel@tonic-gate 	/*
680Sstevel@tonic-gate 	 * We transitively have a hold on the contract through our
690Sstevel@tonic-gate 	 * parent directory.
700Sstevel@tonic-gate 	 */
710Sstevel@tonic-gate 	ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	return (vp);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * ctfs_ctl_access - VOP_ACCESS entry point
780Sstevel@tonic-gate  *
790Sstevel@tonic-gate  * You only get to access ctl files for contracts you own or were
800Sstevel@tonic-gate  * abandoned and inherited by your containing process contract.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate /* ARGSUSED */
830Sstevel@tonic-gate static int
ctfs_ctl_access(vnode_t * vp,int mode,int flags,cred_t * cr,caller_context_t * cct)84*5331Samw ctfs_ctl_access(
85*5331Samw 	vnode_t *vp,
86*5331Samw 	int mode,
87*5331Samw 	int flags,
88*5331Samw 	cred_t *cr,
89*5331Samw 	caller_context_t *cct)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	ctfs_ctlnode_t *ctlnode = vp->v_data;
920Sstevel@tonic-gate 	contract_t *ct = ctlnode->ctfs_ctl_contract;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	if (mode & (VEXEC | VREAD))
950Sstevel@tonic-gate 		return (EACCES);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
980Sstevel@tonic-gate 	if ((curproc == ct->ct_owner) ||
990Sstevel@tonic-gate 	    (ct->ct_owner == NULL && ct->ct_regent != NULL &&
1000Sstevel@tonic-gate 	    ct->ct_regent->ct_data == curproc->p_ct_process)) {
1010Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
1020Sstevel@tonic-gate 		return (0);
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
1060Sstevel@tonic-gate 	return (EACCES);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * ctfs_ctl_open - VOP_OPEN entry point
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  * Just checks to make sure the mode bits are set, and that the
1130Sstevel@tonic-gate  * constraints imposed by ctfs_ctl_access are met.
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate static int
ctfs_ctl_open(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)116*5331Samw ctfs_ctl_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	if (flag != (FWRITE | FOFFMAX))
1190Sstevel@tonic-gate 		return (EINVAL);
1200Sstevel@tonic-gate 
121*5331Samw 	return (ctfs_ctl_access(*vpp, VWRITE, 0, cr, ct));
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1254340Sacruz  * ctfs_ctl_common_getattr
126*5331Samw  * Implements functionality common to ctl and status ctfs VOP_GETATTR
1274340Sacruz  * entry points. It assumes vp->v_data is set
1280Sstevel@tonic-gate  */
1290Sstevel@tonic-gate static int
ctfs_ctl_common_getattr(vnode_t * vp,vattr_t * vap)1304347Sacruz ctfs_ctl_common_getattr(vnode_t *vp, vattr_t *vap)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	ctfs_ctlnode_t *ctlnode = vp->v_data;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	vap->va_type = VREG;
1350Sstevel@tonic-gate 	vap->va_nlink = 1;
1360Sstevel@tonic-gate 	vap->va_size = 0;
1370Sstevel@tonic-gate 	vap->va_ctime = ctlnode->ctfs_ctl_contract->ct_ctime;
1380Sstevel@tonic-gate 	mutex_enter(&ctlnode->ctfs_ctl_contract->ct_events.ctq_lock);
1390Sstevel@tonic-gate 	vap->va_atime = vap->va_mtime =
1400Sstevel@tonic-gate 	    ctlnode->ctfs_ctl_contract->ct_events.ctq_atime;
1410Sstevel@tonic-gate 	mutex_exit(&ctlnode->ctfs_ctl_contract->ct_events.ctq_lock);
1420Sstevel@tonic-gate 	ctfs_common_getattr(vp, vap);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	return (0);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1484340Sacruz  * ctfs_ctl_getattr - VOP_GETATTR entry point
1494340Sacruz  */
1504340Sacruz /* ARGSUSED */
1514340Sacruz static int
ctfs_ctl_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)152*5331Samw ctfs_ctl_getattr(vnode_t *vp, vattr_t *vap, int flags,
153*5331Samw     cred_t *cr, caller_context_t *ct)
1544340Sacruz {
1554340Sacruz 	vap->va_mode = 0222;
1564340Sacruz 
1574347Sacruz 	return (ctfs_ctl_common_getattr(vp, vap));
1584340Sacruz }
1594340Sacruz 
1604340Sacruz /*
1614340Sacruz  * ctfs_stat_getattr - VOP_GETATTR entry point
1624340Sacruz  */
1634340Sacruz /* ARGSUSED */
1644340Sacruz static int
ctfs_stat_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)165*5331Samw ctfs_stat_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
166*5331Samw     caller_context_t *ct)
1674340Sacruz {
1684340Sacruz 	vap->va_mode = 0444;
1694340Sacruz 
1704347Sacruz 	return (ctfs_ctl_common_getattr(vp, vap));
1714340Sacruz }
1724340Sacruz 
1734340Sacruz /*
1740Sstevel@tonic-gate  * ctfs_ctl_ioctl - VOP_IOCTL entry point
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  * All the ct_ctl_*(3contract) interfaces point here.
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate /* ARGSUSED */
1790Sstevel@tonic-gate static int
ctfs_ctl_ioctl(vnode_t * vp,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp,caller_context_t * cct)180*5331Samw ctfs_ctl_ioctl(
181*5331Samw 	vnode_t *vp,
182*5331Samw 	int cmd,
183*5331Samw 	intptr_t arg,
184*5331Samw 	int flag,
185*5331Samw 	cred_t *cr,
186*5331Samw 	int *rvalp,
187*5331Samw 	caller_context_t *cct)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	ctfs_ctlnode_t	*ctlnode = vp->v_data;
1900Sstevel@tonic-gate 	contract_t	*ct = ctlnode->ctfs_ctl_contract;
1910Sstevel@tonic-gate 	int		error = 0;
1920Sstevel@tonic-gate 	uint64_t	event;
1934845Svikram 	int		ack;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	switch (cmd) {
1960Sstevel@tonic-gate 	case CT_CABANDON:
1970Sstevel@tonic-gate 		error = contract_abandon(ct, curproc, 1);
1980Sstevel@tonic-gate 		break;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	case CT_CACK:
2014845Svikram 	case CT_CNACK:
2020Sstevel@tonic-gate 		if (copyin((void *)arg, &event, sizeof (uint64_t)))
2030Sstevel@tonic-gate 			return (EFAULT);
2044845Svikram 		ack = (cmd == CT_CACK) ? CT_ACK : CT_NACK;
2054845Svikram 		error = contract_ack(ct, event, ack);
2060Sstevel@tonic-gate 		break;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	case CT_CNEWCT:
2094845Svikram 		error = contract_newct(ct);
2100Sstevel@tonic-gate 		break;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	case CT_CQREQ:
2134845Svikram 		if (copyin((void *)arg, &event, sizeof (uint64_t)))
2144845Svikram 			return (EFAULT);
2154845Svikram 		error = contract_qack(ct, event);
2160Sstevel@tonic-gate 		break;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	case CT_CADOPT:
2190Sstevel@tonic-gate 		error = contract_adopt(ct, curproc);
2200Sstevel@tonic-gate 		break;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	default:
2230Sstevel@tonic-gate 		return (EINVAL);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	return (error);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_ctl[] = {
2303898Srsb 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_ctl_open } },
2313898Srsb 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
2323898Srsb 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_ctl_ioctl } },
2333898Srsb 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_ctl_getattr } },
2343898Srsb 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_ctl_access } },
2353898Srsb 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
2363898Srsb 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
2373898Srsb 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
2380Sstevel@tonic-gate 	{ NULL, NULL }
2390Sstevel@tonic-gate };
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * ctfs_create_statnode
2430Sstevel@tonic-gate  *
2440Sstevel@tonic-gate  * If necessary, creates a ctlnode for a status file and inserts it
2450Sstevel@tonic-gate  * into the specified cdirnode's gfs_dir_t.  Returns either the
2460Sstevel@tonic-gate  * existing vnode or the new one.
2470Sstevel@tonic-gate  */
2480Sstevel@tonic-gate vnode_t *
ctfs_create_statnode(vnode_t * pvp)2490Sstevel@tonic-gate ctfs_create_statnode(vnode_t *pvp)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate 	vnode_t *vp;
2520Sstevel@tonic-gate 	ctfs_cdirnode_t *cdirnode = pvp->v_data;
2530Sstevel@tonic-gate 	ctfs_ctlnode_t *ctlnode;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_stat);
2560Sstevel@tonic-gate 	ctlnode = vp->v_data;
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * We transitively have a hold on the contract through our
2590Sstevel@tonic-gate 	 * parent directory.
2600Sstevel@tonic-gate 	 */
2610Sstevel@tonic-gate 	ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	return (vp);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate  * ctfs_stat_ioctl - VOP_IOCTL entry point
2680Sstevel@tonic-gate  *
2690Sstevel@tonic-gate  * The kernel half of ct_status_read(3contract).
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate /* ARGSUSED */
2720Sstevel@tonic-gate static int
ctfs_stat_ioctl(vnode_t * vp,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp,caller_context_t * cct)273*5331Samw ctfs_stat_ioctl(
274*5331Samw 	vnode_t *vp,
275*5331Samw 	int cmd,
276*5331Samw 	intptr_t arg,
277*5331Samw 	int flag,
278*5331Samw 	cred_t *cr,
279*5331Samw 	int *rvalp,
280*5331Samw 	caller_context_t *cct)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	ctfs_ctlnode_t	*statnode = vp->v_data;
2830Sstevel@tonic-gate 	contract_t	*ct = statnode->ctfs_ctl_contract;
2840Sstevel@tonic-gate 	ct_type_t	*type = ct->ct_type;
2850Sstevel@tonic-gate 	STRUCT_DECL(ct_status, st);
2860Sstevel@tonic-gate 	nvlist_t	*foo;
2870Sstevel@tonic-gate 	char		*bufp = NULL;
2880Sstevel@tonic-gate 	size_t		len;
2890Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
2900Sstevel@tonic-gate 	uint_t		detail;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	STRUCT_INIT(st, mdl);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	if (cmd != CT_SSTATUS)
2950Sstevel@tonic-gate 		return (EINVAL);
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	if (copyin((void *)arg, STRUCT_BUF(st), STRUCT_SIZE(st)))
2980Sstevel@tonic-gate 		return (EFAULT);
2990Sstevel@tonic-gate 	detail = STRUCT_FGET(st, ctst_detail);
3000Sstevel@tonic-gate 	if (detail == CTD_COMMON) {
3010Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
302789Sahrens 		contract_status_common(ct, VTOZONE(vp), STRUCT_BUF(st), mdl);
3030Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
3040Sstevel@tonic-gate 	} else if (detail <= CTD_ALL) {
3050Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&foo, NV_UNIQUE_NAME, KM_SLEEP) == 0);
306789Sahrens 		type->ct_type_ops->contop_status(ct, VTOZONE(vp), detail, foo,
3070Sstevel@tonic-gate 		    STRUCT_BUF(st), mdl);
3080Sstevel@tonic-gate 		VERIFY(nvlist_pack(foo, &bufp, &len, NV_ENCODE_NATIVE,
3090Sstevel@tonic-gate 		    KM_SLEEP) == 0);
3100Sstevel@tonic-gate 		nvlist_free(foo);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		if ((len <= STRUCT_FGET(st, ctst_nbytes)) &&
3130Sstevel@tonic-gate 		    (copyout(bufp, STRUCT_FGETP(st, ctst_buffer), len) == -1)) {
3140Sstevel@tonic-gate 			kmem_free(bufp, len);
3150Sstevel@tonic-gate 			return (EFAULT);
3160Sstevel@tonic-gate 		}
3170Sstevel@tonic-gate 		kmem_free(bufp, len);
3180Sstevel@tonic-gate 		STRUCT_FSET(st, ctst_nbytes, len);
3190Sstevel@tonic-gate 	} else {
3200Sstevel@tonic-gate 		return (EINVAL);
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(st), (void *)arg, STRUCT_SIZE(st)))
3230Sstevel@tonic-gate 		return (EFAULT);
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	return (0);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_stat[] = {
3293898Srsb 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_open } },
3303898Srsb 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
3313898Srsb 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_stat_ioctl } },
3324340Sacruz 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_stat_getattr } },
3333898Srsb 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_readonly } },
3343898Srsb 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
3353898Srsb 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
3363898Srsb 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
3370Sstevel@tonic-gate 	{ NULL, NULL }
3380Sstevel@tonic-gate };
339