xref: /onnv-gate/usr/src/uts/common/fs/ctfs/ctfs_ctl.c (revision 789:b348f31ed315)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/time.h>
320Sstevel@tonic-gate #include <sys/cred.h>
330Sstevel@tonic-gate #include <sys/vfs.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 /*
1200Sstevel@tonic-gate  * ctfs_ctl_getattr - VOP_GETATTR entry point
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate /* ARGSUSED */
1230Sstevel@tonic-gate static int
1240Sstevel@tonic-gate ctfs_ctl_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	ctfs_ctlnode_t *ctlnode = vp->v_data;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	vap->va_type = VREG;
1290Sstevel@tonic-gate 	vap->va_mode = 0222;
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 /*
1430Sstevel@tonic-gate  * ctfs_ctl_ioctl - VOP_IOCTL entry point
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  * All the ct_ctl_*(3contract) interfaces point here.
1460Sstevel@tonic-gate  */
1470Sstevel@tonic-gate /* ARGSUSED */
1480Sstevel@tonic-gate static int
1490Sstevel@tonic-gate ctfs_ctl_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr,
1500Sstevel@tonic-gate     int *rvalp)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	ctfs_ctlnode_t	*ctlnode = vp->v_data;
1530Sstevel@tonic-gate 	contract_t	*ct = ctlnode->ctfs_ctl_contract;
1540Sstevel@tonic-gate 	int		error = 0;
1550Sstevel@tonic-gate 	uint64_t	event;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	switch (cmd) {
1580Sstevel@tonic-gate 	case CT_CABANDON:
1590Sstevel@tonic-gate 		error = contract_abandon(ct, curproc, 1);
1600Sstevel@tonic-gate 		break;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	case CT_CACK:
1630Sstevel@tonic-gate 		if (copyin((void *)arg, &event, sizeof (uint64_t)))
1640Sstevel@tonic-gate 			return (EFAULT);
1650Sstevel@tonic-gate 		error = contract_ack(ct, event);
1660Sstevel@tonic-gate 		break;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	case CT_CNEWCT:
1690Sstevel@tonic-gate 		break;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	case CT_CQREQ:
1720Sstevel@tonic-gate 		break;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	case CT_CADOPT:
1750Sstevel@tonic-gate 		error = contract_adopt(ct, curproc);
1760Sstevel@tonic-gate 		break;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	default:
1790Sstevel@tonic-gate 		return (EINVAL);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	return (error);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_ctl[] = {
1860Sstevel@tonic-gate 	{ VOPNAME_OPEN,		ctfs_ctl_open },
1870Sstevel@tonic-gate 	{ VOPNAME_CLOSE,	ctfs_close },
1880Sstevel@tonic-gate 	{ VOPNAME_IOCTL,	ctfs_ctl_ioctl },
1890Sstevel@tonic-gate 	{ VOPNAME_GETATTR,	ctfs_ctl_getattr },
1900Sstevel@tonic-gate 	{ VOPNAME_ACCESS,	ctfs_ctl_access },
1910Sstevel@tonic-gate 	{ VOPNAME_READDIR,	fs_notdir },
1920Sstevel@tonic-gate 	{ VOPNAME_LOOKUP,	fs_notdir },
1930Sstevel@tonic-gate 	{ VOPNAME_INACTIVE,	(fs_generic_func_p) gfs_vop_inactive },
1940Sstevel@tonic-gate 	{ NULL, NULL }
1950Sstevel@tonic-gate };
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate /*
1980Sstevel@tonic-gate  * ctfs_create_statnode
1990Sstevel@tonic-gate  *
2000Sstevel@tonic-gate  * If necessary, creates a ctlnode for a status file and inserts it
2010Sstevel@tonic-gate  * into the specified cdirnode's gfs_dir_t.  Returns either the
2020Sstevel@tonic-gate  * existing vnode or the new one.
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate vnode_t *
2050Sstevel@tonic-gate ctfs_create_statnode(vnode_t *pvp)
2060Sstevel@tonic-gate {
2070Sstevel@tonic-gate 	vnode_t *vp;
2080Sstevel@tonic-gate 	ctfs_cdirnode_t *cdirnode = pvp->v_data;
2090Sstevel@tonic-gate 	ctfs_ctlnode_t *ctlnode;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_stat);
2120Sstevel@tonic-gate 	ctlnode = vp->v_data;
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * We transitively have a hold on the contract through our
2150Sstevel@tonic-gate 	 * parent directory.
2160Sstevel@tonic-gate 	 */
2170Sstevel@tonic-gate 	ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	return (vp);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate  * ctfs_stat_ioctl - VOP_IOCTL entry point
2240Sstevel@tonic-gate  *
2250Sstevel@tonic-gate  * The kernel half of ct_status_read(3contract).
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate /* ARGSUSED */
2280Sstevel@tonic-gate static int
2290Sstevel@tonic-gate ctfs_stat_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr,
2300Sstevel@tonic-gate     int *rvalp)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate 	ctfs_ctlnode_t	*statnode = vp->v_data;
2330Sstevel@tonic-gate 	contract_t	*ct = statnode->ctfs_ctl_contract;
2340Sstevel@tonic-gate 	ct_type_t	*type = ct->ct_type;
2350Sstevel@tonic-gate 	STRUCT_DECL(ct_status, st);
2360Sstevel@tonic-gate 	nvlist_t	*foo;
2370Sstevel@tonic-gate 	char		*bufp = NULL;
2380Sstevel@tonic-gate 	size_t		len;
2390Sstevel@tonic-gate 	model_t		mdl = get_udatamodel();
2400Sstevel@tonic-gate 	uint_t		detail;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	STRUCT_INIT(st, mdl);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	if (cmd != CT_SSTATUS)
2450Sstevel@tonic-gate 		return (EINVAL);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	if (copyin((void *)arg, STRUCT_BUF(st), STRUCT_SIZE(st)))
2480Sstevel@tonic-gate 		return (EFAULT);
2490Sstevel@tonic-gate 	detail = STRUCT_FGET(st, ctst_detail);
2500Sstevel@tonic-gate 	if (detail == CTD_COMMON) {
2510Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
252*789Sahrens 		contract_status_common(ct, VTOZONE(vp), STRUCT_BUF(st), mdl);
2530Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
2540Sstevel@tonic-gate 	} else if (detail <= CTD_ALL) {
2550Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&foo, NV_UNIQUE_NAME, KM_SLEEP) == 0);
256*789Sahrens 		type->ct_type_ops->contop_status(ct, VTOZONE(vp), detail, foo,
2570Sstevel@tonic-gate 		    STRUCT_BUF(st), mdl);
2580Sstevel@tonic-gate 		VERIFY(nvlist_pack(foo, &bufp, &len, NV_ENCODE_NATIVE,
2590Sstevel@tonic-gate 		    KM_SLEEP) == 0);
2600Sstevel@tonic-gate 		nvlist_free(foo);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 		if ((len <= STRUCT_FGET(st, ctst_nbytes)) &&
2630Sstevel@tonic-gate 		    (copyout(bufp, STRUCT_FGETP(st, ctst_buffer), len) == -1)) {
2640Sstevel@tonic-gate 			kmem_free(bufp, len);
2650Sstevel@tonic-gate 			return (EFAULT);
2660Sstevel@tonic-gate 		}
2670Sstevel@tonic-gate 		kmem_free(bufp, len);
2680Sstevel@tonic-gate 		STRUCT_FSET(st, ctst_nbytes, len);
2690Sstevel@tonic-gate 	} else {
2700Sstevel@tonic-gate 		return (EINVAL);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(st), (void *)arg, STRUCT_SIZE(st)))
2730Sstevel@tonic-gate 		return (EFAULT);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	return (0);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_stat[] = {
2790Sstevel@tonic-gate 	{ VOPNAME_OPEN,		ctfs_open },
2800Sstevel@tonic-gate 	{ VOPNAME_CLOSE,	ctfs_close },
2810Sstevel@tonic-gate 	{ VOPNAME_IOCTL,	ctfs_stat_ioctl },
2820Sstevel@tonic-gate 	{ VOPNAME_GETATTR,	ctfs_ctl_getattr },
2830Sstevel@tonic-gate 	{ VOPNAME_ACCESS,	ctfs_access_readonly },
2840Sstevel@tonic-gate 	{ VOPNAME_READDIR,	fs_notdir },
2850Sstevel@tonic-gate 	{ VOPNAME_LOOKUP,	fs_notdir },
2860Sstevel@tonic-gate 	{ VOPNAME_INACTIVE,	(fs_generic_func_p) gfs_vop_inactive },
2870Sstevel@tonic-gate 	{ NULL, NULL }
2880Sstevel@tonic-gate };
289