xref: /onnv-gate/usr/src/uts/common/fs/ctfs/ctfs_ctl.c (revision 4340:3c0d0e161a41)
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 /*
120*4340Sacruz  * ctfs_ctl_common_getattr
121*4340Sacruz  * Implements fucntionality common to ctl and status ctfs VOP_GETATTR
122*4340Sacruz  * entry points. It assumes vp->v_data is set
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate static int
125*4340Sacruz ctfs_ctl_common_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
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 /*
143*4340Sacruz  * ctfs_ctl_getattr - VOP_GETATTR entry point
144*4340Sacruz  */
145*4340Sacruz /* ARGSUSED */
146*4340Sacruz static int
147*4340Sacruz ctfs_ctl_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
148*4340Sacruz {
149*4340Sacruz 	vap->va_mode = 0222;
150*4340Sacruz 
151*4340Sacruz 	return (ctfs_ctl_common_getattr(vp, vap, flags, cr));
152*4340Sacruz }
153*4340Sacruz 
154*4340Sacruz /*
155*4340Sacruz  * ctfs_stat_getattr - VOP_GETATTR entry point
156*4340Sacruz  */
157*4340Sacruz /* ARGSUSED */
158*4340Sacruz static int
159*4340Sacruz ctfs_stat_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
160*4340Sacruz {
161*4340Sacruz 	vap->va_mode = 0444;
162*4340Sacruz 
163*4340Sacruz 	return (ctfs_ctl_common_getattr(vp, vap, flags, cr));
164*4340Sacruz }
165*4340Sacruz 
166*4340Sacruz /*
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;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	switch (cmd) {
1820Sstevel@tonic-gate 	case CT_CABANDON:
1830Sstevel@tonic-gate 		error = contract_abandon(ct, curproc, 1);
1840Sstevel@tonic-gate 		break;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	case CT_CACK:
1870Sstevel@tonic-gate 		if (copyin((void *)arg, &event, sizeof (uint64_t)))
1880Sstevel@tonic-gate 			return (EFAULT);
1890Sstevel@tonic-gate 		error = contract_ack(ct, event);
1900Sstevel@tonic-gate 		break;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	case CT_CNEWCT:
1930Sstevel@tonic-gate 		break;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	case CT_CQREQ:
1960Sstevel@tonic-gate 		break;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	case CT_CADOPT:
1990Sstevel@tonic-gate 		error = contract_adopt(ct, curproc);
2000Sstevel@tonic-gate 		break;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	default:
2030Sstevel@tonic-gate 		return (EINVAL);
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	return (error);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_ctl[] = {
2103898Srsb 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_ctl_open } },
2113898Srsb 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
2123898Srsb 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_ctl_ioctl } },
2133898Srsb 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_ctl_getattr } },
2143898Srsb 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_ctl_access } },
2153898Srsb 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
2163898Srsb 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
2173898Srsb 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
2180Sstevel@tonic-gate 	{ NULL, NULL }
2190Sstevel@tonic-gate };
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate  * ctfs_create_statnode
2230Sstevel@tonic-gate  *
2240Sstevel@tonic-gate  * If necessary, creates a ctlnode for a status file and inserts it
2250Sstevel@tonic-gate  * into the specified cdirnode's gfs_dir_t.  Returns either the
2260Sstevel@tonic-gate  * existing vnode or the new one.
2270Sstevel@tonic-gate  */
2280Sstevel@tonic-gate vnode_t *
2290Sstevel@tonic-gate ctfs_create_statnode(vnode_t *pvp)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 	vnode_t *vp;
2320Sstevel@tonic-gate 	ctfs_cdirnode_t *cdirnode = pvp->v_data;
2330Sstevel@tonic-gate 	ctfs_ctlnode_t *ctlnode;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_stat);
2360Sstevel@tonic-gate 	ctlnode = vp->v_data;
2370Sstevel@tonic-gate 	/*
2380Sstevel@tonic-gate 	 * We transitively have a hold on the contract through our
2390Sstevel@tonic-gate 	 * parent directory.
2400Sstevel@tonic-gate 	 */
2410Sstevel@tonic-gate 	ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	return (vp);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * ctfs_stat_ioctl - VOP_IOCTL entry point
2480Sstevel@tonic-gate  *
2490Sstevel@tonic-gate  * The kernel half of ct_status_read(3contract).
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate /* ARGSUSED */
2520Sstevel@tonic-gate static int
2530Sstevel@tonic-gate ctfs_stat_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr,
2540Sstevel@tonic-gate     int *rvalp)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	ctfs_ctlnode_t	*statnode = vp->v_data;
2570Sstevel@tonic-gate 	contract_t	*ct = statnode->ctfs_ctl_contract;
2580Sstevel@tonic-gate 	ct_type_t	*type = ct->ct_type;
2590Sstevel@tonic-gate 	STRUCT_DECL(ct_status, st);
2600Sstevel@tonic-gate 	nvlist_t	*foo;
2610Sstevel@tonic-gate 	char		*bufp = NULL;
2620Sstevel@tonic-gate 	size_t		len;
2630Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
2640Sstevel@tonic-gate 	uint_t		detail;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	STRUCT_INIT(st, mdl);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if (cmd != CT_SSTATUS)
2690Sstevel@tonic-gate 		return (EINVAL);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	if (copyin((void *)arg, STRUCT_BUF(st), STRUCT_SIZE(st)))
2720Sstevel@tonic-gate 		return (EFAULT);
2730Sstevel@tonic-gate 	detail = STRUCT_FGET(st, ctst_detail);
2740Sstevel@tonic-gate 	if (detail == CTD_COMMON) {
2750Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
276789Sahrens 		contract_status_common(ct, VTOZONE(vp), STRUCT_BUF(st), mdl);
2770Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
2780Sstevel@tonic-gate 	} else if (detail <= CTD_ALL) {
2790Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&foo, NV_UNIQUE_NAME, KM_SLEEP) == 0);
280789Sahrens 		type->ct_type_ops->contop_status(ct, VTOZONE(vp), detail, foo,
2810Sstevel@tonic-gate 		    STRUCT_BUF(st), mdl);
2820Sstevel@tonic-gate 		VERIFY(nvlist_pack(foo, &bufp, &len, NV_ENCODE_NATIVE,
2830Sstevel@tonic-gate 		    KM_SLEEP) == 0);
2840Sstevel@tonic-gate 		nvlist_free(foo);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 		if ((len <= STRUCT_FGET(st, ctst_nbytes)) &&
2870Sstevel@tonic-gate 		    (copyout(bufp, STRUCT_FGETP(st, ctst_buffer), len) == -1)) {
2880Sstevel@tonic-gate 			kmem_free(bufp, len);
2890Sstevel@tonic-gate 			return (EFAULT);
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate 		kmem_free(bufp, len);
2920Sstevel@tonic-gate 		STRUCT_FSET(st, ctst_nbytes, len);
2930Sstevel@tonic-gate 	} else {
2940Sstevel@tonic-gate 		return (EINVAL);
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(st), (void *)arg, STRUCT_SIZE(st)))
2970Sstevel@tonic-gate 		return (EFAULT);
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	return (0);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_stat[] = {
3033898Srsb 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_open } },
3043898Srsb 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
3053898Srsb 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_stat_ioctl } },
306*4340Sacruz 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_stat_getattr } },
3073898Srsb 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_readonly } },
3083898Srsb 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
3093898Srsb 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
3103898Srsb 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
3110Sstevel@tonic-gate 	{ NULL, NULL }
3120Sstevel@tonic-gate };
313