xref: /onnv-gate/usr/src/uts/common/fs/ctfs/ctfs_ctl.c (revision 4845:357e8e7542af)
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 *
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
840Sstevel@tonic-gate ctfs_ctl_access(vnode_t *vp, int mode, int flags, cred_t *cr)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	ctfs_ctlnode_t *ctlnode = vp->v_data;
870Sstevel@tonic-gate 	contract_t *ct = ctlnode->ctfs_ctl_contract;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (mode & (VEXEC | VREAD))
900Sstevel@tonic-gate 		return (EACCES);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
930Sstevel@tonic-gate 	if ((curproc == ct->ct_owner) ||
940Sstevel@tonic-gate 	    (ct->ct_owner == NULL && ct->ct_regent != NULL &&
950Sstevel@tonic-gate 	    ct->ct_regent->ct_data == curproc->p_ct_process)) {
960Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
970Sstevel@tonic-gate 		return (0);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
1010Sstevel@tonic-gate 	return (EACCES);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate  * ctfs_ctl_open - VOP_OPEN entry point
1060Sstevel@tonic-gate  *
1070Sstevel@tonic-gate  * Just checks to make sure the mode bits are set, and that the
1080Sstevel@tonic-gate  * constraints imposed by ctfs_ctl_access are met.
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate static int
1110Sstevel@tonic-gate ctfs_ctl_open(vnode_t **vpp, int flag, cred_t *cr)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	if (flag != (FWRITE | FOFFMAX))
1140Sstevel@tonic-gate 		return (EINVAL);
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	return (ctfs_ctl_access(*vpp, VWRITE, 0, cr));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1204340Sacruz  * ctfs_ctl_common_getattr
1214340Sacruz  * Implements fucntionality common to ctl and status ctfs VOP_GETATTR
1224340Sacruz  * entry points. It assumes vp->v_data is set
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate static int
1254347Sacruz ctfs_ctl_common_getattr(vnode_t *vp, vattr_t *vap)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	ctfs_ctlnode_t *ctlnode = vp->v_data;
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	vap->va_type = VREG;
1300Sstevel@tonic-gate 	vap->va_nlink = 1;
1310Sstevel@tonic-gate 	vap->va_size = 0;
1320Sstevel@tonic-gate 	vap->va_ctime = ctlnode->ctfs_ctl_contract->ct_ctime;
1330Sstevel@tonic-gate 	mutex_enter(&ctlnode->ctfs_ctl_contract->ct_events.ctq_lock);
1340Sstevel@tonic-gate 	vap->va_atime = vap->va_mtime =
1350Sstevel@tonic-gate 	    ctlnode->ctfs_ctl_contract->ct_events.ctq_atime;
1360Sstevel@tonic-gate 	mutex_exit(&ctlnode->ctfs_ctl_contract->ct_events.ctq_lock);
1370Sstevel@tonic-gate 	ctfs_common_getattr(vp, vap);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	return (0);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
1434340Sacruz  * ctfs_ctl_getattr - VOP_GETATTR entry point
1444340Sacruz  */
1454340Sacruz /* ARGSUSED */
1464340Sacruz static int
1474340Sacruz ctfs_ctl_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1484340Sacruz {
1494340Sacruz 	vap->va_mode = 0222;
1504340Sacruz 
1514347Sacruz 	return (ctfs_ctl_common_getattr(vp, vap));
1524340Sacruz }
1534340Sacruz 
1544340Sacruz /*
1554340Sacruz  * ctfs_stat_getattr - VOP_GETATTR entry point
1564340Sacruz  */
1574340Sacruz /* ARGSUSED */
1584340Sacruz static int
1594340Sacruz ctfs_stat_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1604340Sacruz {
1614340Sacruz 	vap->va_mode = 0444;
1624340Sacruz 
1634347Sacruz 	return (ctfs_ctl_common_getattr(vp, vap));
1644340Sacruz }
1654340Sacruz 
1664340Sacruz /*
1670Sstevel@tonic-gate  * ctfs_ctl_ioctl - VOP_IOCTL entry point
1680Sstevel@tonic-gate  *
1690Sstevel@tonic-gate  * All the ct_ctl_*(3contract) interfaces point here.
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate /* ARGSUSED */
1720Sstevel@tonic-gate static int
1730Sstevel@tonic-gate ctfs_ctl_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr,
1740Sstevel@tonic-gate     int *rvalp)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	ctfs_ctlnode_t	*ctlnode = vp->v_data;
1770Sstevel@tonic-gate 	contract_t	*ct = ctlnode->ctfs_ctl_contract;
1780Sstevel@tonic-gate 	int		error = 0;
1790Sstevel@tonic-gate 	uint64_t	event;
180*4845Svikram 	int		ack;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	switch (cmd) {
1830Sstevel@tonic-gate 	case CT_CABANDON:
1840Sstevel@tonic-gate 		error = contract_abandon(ct, curproc, 1);
1850Sstevel@tonic-gate 		break;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	case CT_CACK:
188*4845Svikram 	case CT_CNACK:
1890Sstevel@tonic-gate 		if (copyin((void *)arg, &event, sizeof (uint64_t)))
1900Sstevel@tonic-gate 			return (EFAULT);
191*4845Svikram 		ack = (cmd == CT_CACK) ? CT_ACK : CT_NACK;
192*4845Svikram 		error = contract_ack(ct, event, ack);
1930Sstevel@tonic-gate 		break;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	case CT_CNEWCT:
196*4845Svikram 		error = contract_newct(ct);
1970Sstevel@tonic-gate 		break;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	case CT_CQREQ:
200*4845Svikram 		if (copyin((void *)arg, &event, sizeof (uint64_t)))
201*4845Svikram 			return (EFAULT);
202*4845Svikram 		error = contract_qack(ct, event);
2030Sstevel@tonic-gate 		break;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	case CT_CADOPT:
2060Sstevel@tonic-gate 		error = contract_adopt(ct, curproc);
2070Sstevel@tonic-gate 		break;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	default:
2100Sstevel@tonic-gate 		return (EINVAL);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	return (error);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_ctl[] = {
2173898Srsb 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_ctl_open } },
2183898Srsb 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
2193898Srsb 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_ctl_ioctl } },
2203898Srsb 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_ctl_getattr } },
2213898Srsb 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_ctl_access } },
2223898Srsb 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
2233898Srsb 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
2243898Srsb 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
2250Sstevel@tonic-gate 	{ NULL, NULL }
2260Sstevel@tonic-gate };
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * ctfs_create_statnode
2300Sstevel@tonic-gate  *
2310Sstevel@tonic-gate  * If necessary, creates a ctlnode for a status file and inserts it
2320Sstevel@tonic-gate  * into the specified cdirnode's gfs_dir_t.  Returns either the
2330Sstevel@tonic-gate  * existing vnode or the new one.
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate vnode_t *
2360Sstevel@tonic-gate ctfs_create_statnode(vnode_t *pvp)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	vnode_t *vp;
2390Sstevel@tonic-gate 	ctfs_cdirnode_t *cdirnode = pvp->v_data;
2400Sstevel@tonic-gate 	ctfs_ctlnode_t *ctlnode;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_stat);
2430Sstevel@tonic-gate 	ctlnode = vp->v_data;
2440Sstevel@tonic-gate 	/*
2450Sstevel@tonic-gate 	 * We transitively have a hold on the contract through our
2460Sstevel@tonic-gate 	 * parent directory.
2470Sstevel@tonic-gate 	 */
2480Sstevel@tonic-gate 	ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	return (vp);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate /*
2540Sstevel@tonic-gate  * ctfs_stat_ioctl - VOP_IOCTL entry point
2550Sstevel@tonic-gate  *
2560Sstevel@tonic-gate  * The kernel half of ct_status_read(3contract).
2570Sstevel@tonic-gate  */
2580Sstevel@tonic-gate /* ARGSUSED */
2590Sstevel@tonic-gate static int
2600Sstevel@tonic-gate ctfs_stat_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr,
2610Sstevel@tonic-gate     int *rvalp)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	ctfs_ctlnode_t	*statnode = vp->v_data;
2640Sstevel@tonic-gate 	contract_t	*ct = statnode->ctfs_ctl_contract;
2650Sstevel@tonic-gate 	ct_type_t	*type = ct->ct_type;
2660Sstevel@tonic-gate 	STRUCT_DECL(ct_status, st);
2670Sstevel@tonic-gate 	nvlist_t	*foo;
2680Sstevel@tonic-gate 	char		*bufp = NULL;
2690Sstevel@tonic-gate 	size_t		len;
2700Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
2710Sstevel@tonic-gate 	uint_t		detail;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	STRUCT_INIT(st, mdl);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	if (cmd != CT_SSTATUS)
2760Sstevel@tonic-gate 		return (EINVAL);
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	if (copyin((void *)arg, STRUCT_BUF(st), STRUCT_SIZE(st)))
2790Sstevel@tonic-gate 		return (EFAULT);
2800Sstevel@tonic-gate 	detail = STRUCT_FGET(st, ctst_detail);
2810Sstevel@tonic-gate 	if (detail == CTD_COMMON) {
2820Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
283789Sahrens 		contract_status_common(ct, VTOZONE(vp), STRUCT_BUF(st), mdl);
2840Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
2850Sstevel@tonic-gate 	} else if (detail <= CTD_ALL) {
2860Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&foo, NV_UNIQUE_NAME, KM_SLEEP) == 0);
287789Sahrens 		type->ct_type_ops->contop_status(ct, VTOZONE(vp), detail, foo,
2880Sstevel@tonic-gate 		    STRUCT_BUF(st), mdl);
2890Sstevel@tonic-gate 		VERIFY(nvlist_pack(foo, &bufp, &len, NV_ENCODE_NATIVE,
2900Sstevel@tonic-gate 		    KM_SLEEP) == 0);
2910Sstevel@tonic-gate 		nvlist_free(foo);
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		if ((len <= STRUCT_FGET(st, ctst_nbytes)) &&
2940Sstevel@tonic-gate 		    (copyout(bufp, STRUCT_FGETP(st, ctst_buffer), len) == -1)) {
2950Sstevel@tonic-gate 			kmem_free(bufp, len);
2960Sstevel@tonic-gate 			return (EFAULT);
2970Sstevel@tonic-gate 		}
2980Sstevel@tonic-gate 		kmem_free(bufp, len);
2990Sstevel@tonic-gate 		STRUCT_FSET(st, ctst_nbytes, len);
3000Sstevel@tonic-gate 	} else {
3010Sstevel@tonic-gate 		return (EINVAL);
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(st), (void *)arg, STRUCT_SIZE(st)))
3040Sstevel@tonic-gate 		return (EFAULT);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	return (0);
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_stat[] = {
3103898Srsb 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_open } },
3113898Srsb 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
3123898Srsb 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_stat_ioctl } },
3134340Sacruz 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_stat_getattr } },
3143898Srsb 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_readonly } },
3153898Srsb 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
3163898Srsb 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
3173898Srsb 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
3180Sstevel@tonic-gate 	{ NULL, NULL }
3190Sstevel@tonic-gate };
320