xref: /onnv-gate/usr/src/uts/common/fs/ctfs/ctfs_tmpl.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>/template vnode.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  * ctfs_create_tmplnode
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * Creates a new template and tdirnode, and returns the tdirnode.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate vnode_t *
560Sstevel@tonic-gate ctfs_create_tmplnode(vnode_t *pvp)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	vnode_t *vp;
590Sstevel@tonic-gate 	ctfs_tmplnode_t *tmplnode;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	ASSERT(gfs_file_index(pvp) < ct_ntypes);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	vp = gfs_file_create(sizeof (ctfs_tmplnode_t), pvp, ctfs_ops_tmpl);
640Sstevel@tonic-gate 	tmplnode = vp->v_data;
650Sstevel@tonic-gate 	tmplnode->ctfs_tmn_tmpl =
660Sstevel@tonic-gate 	    ct_types[gfs_file_index(pvp)]->ct_type_default();
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	return (vp);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /*
720Sstevel@tonic-gate  * ctfs_tmpl_open - VOP_OPEN entry point
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * Just ensures the right mode bits are set.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate /* ARGSUSED */
770Sstevel@tonic-gate static int
78*5331Samw ctfs_tmpl_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	if (flag != (FREAD | FWRITE | FOFFMAX))
810Sstevel@tonic-gate 		return (EINVAL);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	return (0);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * ctfs_tmpl_getattr - VOP_GETATTR entry point
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate /* ARGSUSED */
900Sstevel@tonic-gate static int
91*5331Samw ctfs_tmpl_getattr(
92*5331Samw 	vnode_t *vp,
93*5331Samw 	vattr_t *vap,
94*5331Samw 	int flags,
95*5331Samw 	cred_t *cr,
96*5331Samw 	caller_context_t *ct)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	vap->va_type = VREG;
990Sstevel@tonic-gate 	vap->va_mode = 0666;
1000Sstevel@tonic-gate 	vap->va_nlink = 1;
1010Sstevel@tonic-gate 	vap->va_size = 0;
1020Sstevel@tonic-gate 	vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
1030Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = 0;
1040Sstevel@tonic-gate 	vap->va_atime = vap->va_mtime = vap->va_ctime;
1050Sstevel@tonic-gate 	ctfs_common_getattr(vp, vap);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	return (0);
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /*
1110Sstevel@tonic-gate  * ctfs_tmpl_ioctl - VOP_IOCTL entry point
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  * All the ct_tmpl_*(3contract) interfaces point here.
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate /* ARGSUSED */
1160Sstevel@tonic-gate static int
117*5331Samw ctfs_tmpl_ioctl(
118*5331Samw 	vnode_t *vp,
119*5331Samw 	int cmd,
120*5331Samw 	intptr_t arg,
121*5331Samw 	int flag,
122*5331Samw 	cred_t *cr,
123*5331Samw 	int *rvalp,
124*5331Samw 	caller_context_t *ct)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	ctfs_tmplnode_t	*tmplnode = vp->v_data;
1270Sstevel@tonic-gate 	ct_param_t param;
1284845Svikram 	ctid_t ctid;
1290Sstevel@tonic-gate 	int error;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	switch (cmd) {
1320Sstevel@tonic-gate 	case CT_TACTIVATE:
1330Sstevel@tonic-gate 		ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
1340Sstevel@tonic-gate 		ctmpl_activate(tmplnode->ctfs_tmn_tmpl);
1350Sstevel@tonic-gate 		break;
1360Sstevel@tonic-gate 	case CT_TCLEAR:
1370Sstevel@tonic-gate 		ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
1380Sstevel@tonic-gate 		ctmpl_clear(tmplnode->ctfs_tmn_tmpl);
1390Sstevel@tonic-gate 		break;
1400Sstevel@tonic-gate 	case CT_TCREATE:
1410Sstevel@tonic-gate 		ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
1424845Svikram 		error = ctmpl_create(tmplnode->ctfs_tmn_tmpl, &ctid);
1434845Svikram 		if (error)
1444845Svikram 			return (error);
1454845Svikram 		*rvalp = ctid;
1464845Svikram 		break;
1470Sstevel@tonic-gate 	case CT_TSET:
1480Sstevel@tonic-gate 		if (copyin((void *)arg, &param, sizeof (ct_param_t)))
1490Sstevel@tonic-gate 			return (EFAULT);
1500Sstevel@tonic-gate 		return (ctmpl_set(tmplnode->ctfs_tmn_tmpl, &param, cr));
1510Sstevel@tonic-gate 	case CT_TGET:
1520Sstevel@tonic-gate 		if (copyin((void *)arg, &param, sizeof (ct_param_t)))
1530Sstevel@tonic-gate 			return (EFAULT);
1540Sstevel@tonic-gate 		error = ctmpl_get(tmplnode->ctfs_tmn_tmpl, &param);
1550Sstevel@tonic-gate 		if (error)
1560Sstevel@tonic-gate 			return (error);
1570Sstevel@tonic-gate 		if (copyout(&param, (void *)arg, sizeof (ct_param_t)))
1580Sstevel@tonic-gate 			return (EFAULT);
1590Sstevel@tonic-gate 		break;
1600Sstevel@tonic-gate 	default:
1610Sstevel@tonic-gate 		return (EINVAL);
1620Sstevel@tonic-gate 	}
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	return (0);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * ctfs_tmpl_inactive - VOP_INACTIVE entry point
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate /* ARGSUSED */
1710Sstevel@tonic-gate static void
172*5331Samw ctfs_tmpl_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	ctfs_tmplnode_t *tmplnode;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	if ((tmplnode = gfs_file_inactive(vp)) != NULL) {
1770Sstevel@tonic-gate 		ctmpl_free(tmplnode->ctfs_tmn_tmpl);
1780Sstevel@tonic-gate 		kmem_free(tmplnode, sizeof (ctfs_tmplnode_t));
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_tmpl[] = {
1833898Srsb 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_tmpl_open } },
1843898Srsb 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
1853898Srsb 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_tmpl_ioctl } },
1863898Srsb 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_tmpl_getattr } },
1873898Srsb 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_readwrite } },
1883898Srsb 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
1893898Srsb 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
1903898Srsb 	{ VOPNAME_INACTIVE,	{ .vop_inactive = ctfs_tmpl_inactive } },
1910Sstevel@tonic-gate 	{ NULL, NULL }
1920Sstevel@tonic-gate };
193