17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5aa59c4cbSrsb * Common Development and Distribution License (the "License").
6aa59c4cbSrsb * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
227b209c2cSacruz * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/time.h>
297c478bd9Sstevel@tonic-gate #include <sys/cred.h>
307c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
31aa59c4cbSrsb #include <sys/vfs_opreg.h>
327c478bd9Sstevel@tonic-gate #include <sys/gfs.h>
337c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
387c478bd9Sstevel@tonic-gate #include <sys/contract.h>
397c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
407c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
417c478bd9Sstevel@tonic-gate #include <sys/ctfs_impl.h>
427c478bd9Sstevel@tonic-gate #include <sys/file.h>
437b209c2cSacruz #include <sys/model.h>
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate * CTFS routines for the /system/contract/<type>/template vnode.
477c478bd9Sstevel@tonic-gate */
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate * ctfs_create_tmplnode
517c478bd9Sstevel@tonic-gate *
527c478bd9Sstevel@tonic-gate * Creates a new template and tdirnode, and returns the tdirnode.
537c478bd9Sstevel@tonic-gate */
547c478bd9Sstevel@tonic-gate vnode_t *
ctfs_create_tmplnode(vnode_t * pvp)557c478bd9Sstevel@tonic-gate ctfs_create_tmplnode(vnode_t *pvp)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate vnode_t *vp;
587c478bd9Sstevel@tonic-gate ctfs_tmplnode_t *tmplnode;
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate ASSERT(gfs_file_index(pvp) < ct_ntypes);
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate vp = gfs_file_create(sizeof (ctfs_tmplnode_t), pvp, ctfs_ops_tmpl);
637c478bd9Sstevel@tonic-gate tmplnode = vp->v_data;
647c478bd9Sstevel@tonic-gate tmplnode->ctfs_tmn_tmpl =
657c478bd9Sstevel@tonic-gate ct_types[gfs_file_index(pvp)]->ct_type_default();
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate return (vp);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate * ctfs_tmpl_open - VOP_OPEN entry point
727c478bd9Sstevel@tonic-gate *
737c478bd9Sstevel@tonic-gate * Just ensures the right mode bits are set.
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate /* ARGSUSED */
767c478bd9Sstevel@tonic-gate static int
ctfs_tmpl_open(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)77da6c28aaSamw ctfs_tmpl_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate if (flag != (FREAD | FWRITE | FOFFMAX))
807c478bd9Sstevel@tonic-gate return (EINVAL);
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate return (0);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate * ctfs_tmpl_getattr - VOP_GETATTR entry point
877c478bd9Sstevel@tonic-gate */
887c478bd9Sstevel@tonic-gate /* ARGSUSED */
897c478bd9Sstevel@tonic-gate static int
ctfs_tmpl_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)90da6c28aaSamw ctfs_tmpl_getattr(
91da6c28aaSamw vnode_t *vp,
92da6c28aaSamw vattr_t *vap,
93da6c28aaSamw int flags,
94da6c28aaSamw cred_t *cr,
95da6c28aaSamw caller_context_t *ct)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate vap->va_type = VREG;
987c478bd9Sstevel@tonic-gate vap->va_mode = 0666;
997c478bd9Sstevel@tonic-gate vap->va_nlink = 1;
1007c478bd9Sstevel@tonic-gate vap->va_size = 0;
1017c478bd9Sstevel@tonic-gate vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
1027c478bd9Sstevel@tonic-gate vap->va_ctime.tv_nsec = 0;
1037c478bd9Sstevel@tonic-gate vap->va_atime = vap->va_mtime = vap->va_ctime;
1047c478bd9Sstevel@tonic-gate ctfs_common_getattr(vp, vap);
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate return (0);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * ctfs_tmpl_ioctl - VOP_IOCTL entry point
1117c478bd9Sstevel@tonic-gate *
1127c478bd9Sstevel@tonic-gate * All the ct_tmpl_*(3contract) interfaces point here.
1137c478bd9Sstevel@tonic-gate */
1147c478bd9Sstevel@tonic-gate /* ARGSUSED */
1157c478bd9Sstevel@tonic-gate static int
ctfs_tmpl_ioctl(vnode_t * vp,int cmd,intptr_t arg,int flag,cred_t * cr,int * rvalp,caller_context_t * ct)116da6c28aaSamw ctfs_tmpl_ioctl(
117da6c28aaSamw vnode_t *vp,
118da6c28aaSamw int cmd,
119da6c28aaSamw intptr_t arg,
120da6c28aaSamw int flag,
121da6c28aaSamw cred_t *cr,
122da6c28aaSamw int *rvalp,
123da6c28aaSamw caller_context_t *ct)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate ctfs_tmplnode_t *tmplnode = vp->v_data;
126*c5a9a4fcSAntonello Cruz ct_kparam_t kparam;
127*c5a9a4fcSAntonello Cruz ct_param_t *param = &kparam.param;
12825e8c5aaSvikram ctid_t ctid;
1297c478bd9Sstevel@tonic-gate int error;
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate switch (cmd) {
1327c478bd9Sstevel@tonic-gate case CT_TACTIVATE:
1337c478bd9Sstevel@tonic-gate ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
1347c478bd9Sstevel@tonic-gate ctmpl_activate(tmplnode->ctfs_tmn_tmpl);
1357c478bd9Sstevel@tonic-gate break;
1367c478bd9Sstevel@tonic-gate case CT_TCLEAR:
1377c478bd9Sstevel@tonic-gate ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
1387c478bd9Sstevel@tonic-gate ctmpl_clear(tmplnode->ctfs_tmn_tmpl);
1397c478bd9Sstevel@tonic-gate break;
1407c478bd9Sstevel@tonic-gate case CT_TCREATE:
1417c478bd9Sstevel@tonic-gate ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
14225e8c5aaSvikram error = ctmpl_create(tmplnode->ctfs_tmn_tmpl, &ctid);
14325e8c5aaSvikram if (error)
14425e8c5aaSvikram return (error);
14525e8c5aaSvikram *rvalp = ctid;
14625e8c5aaSvikram break;
1477c478bd9Sstevel@tonic-gate case CT_TSET:
148*c5a9a4fcSAntonello Cruz error = ctparam_copyin((void *)arg, &kparam, flag, cmd);
149*c5a9a4fcSAntonello Cruz if (error != 0)
150*c5a9a4fcSAntonello Cruz return (error);
151*c5a9a4fcSAntonello Cruz error = ctmpl_set(tmplnode->ctfs_tmn_tmpl, &kparam, cr);
152*c5a9a4fcSAntonello Cruz kmem_free(kparam.ctpm_kbuf, param->ctpm_size);
153*c5a9a4fcSAntonello Cruz
1547c478bd9Sstevel@tonic-gate return (error);
1557b209c2cSacruz case CT_TGET:
156*c5a9a4fcSAntonello Cruz error = ctparam_copyin((void *)arg, &kparam, flag, cmd);
157*c5a9a4fcSAntonello Cruz if (error != 0)
158*c5a9a4fcSAntonello Cruz return (error);
159*c5a9a4fcSAntonello Cruz error = ctmpl_get(tmplnode->ctfs_tmn_tmpl, &kparam);
160*c5a9a4fcSAntonello Cruz if (error != 0) {
161*c5a9a4fcSAntonello Cruz kmem_free(kparam.ctpm_kbuf, param->ctpm_size);
162*c5a9a4fcSAntonello Cruz } else {
163*c5a9a4fcSAntonello Cruz error = ctparam_copyout(&kparam, (void *)arg, flag);
164*c5a9a4fcSAntonello Cruz }
165*c5a9a4fcSAntonello Cruz
1667b209c2cSacruz return (error);
1677c478bd9Sstevel@tonic-gate default:
1687c478bd9Sstevel@tonic-gate return (EINVAL);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate
1717c478bd9Sstevel@tonic-gate return (0);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate * ctfs_tmpl_inactive - VOP_INACTIVE entry point
1767c478bd9Sstevel@tonic-gate */
1777c478bd9Sstevel@tonic-gate /* ARGSUSED */
1787c478bd9Sstevel@tonic-gate static void
ctfs_tmpl_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)179da6c28aaSamw ctfs_tmpl_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate ctfs_tmplnode_t *tmplnode;
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate if ((tmplnode = gfs_file_inactive(vp)) != NULL) {
1847c478bd9Sstevel@tonic-gate ctmpl_free(tmplnode->ctfs_tmn_tmpl);
1857c478bd9Sstevel@tonic-gate kmem_free(tmplnode, sizeof (ctfs_tmplnode_t));
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_tmpl[] = {
190aa59c4cbSrsb { VOPNAME_OPEN, { .vop_open = ctfs_tmpl_open } },
191aa59c4cbSrsb { VOPNAME_CLOSE, { .vop_close = ctfs_close } },
192aa59c4cbSrsb { VOPNAME_IOCTL, { .vop_ioctl = ctfs_tmpl_ioctl } },
193aa59c4cbSrsb { VOPNAME_GETATTR, { .vop_getattr = ctfs_tmpl_getattr } },
194aa59c4cbSrsb { VOPNAME_ACCESS, { .vop_access = ctfs_access_readwrite } },
195aa59c4cbSrsb { VOPNAME_READDIR, { .error = fs_notdir } },
196aa59c4cbSrsb { VOPNAME_LOOKUP, { .error = fs_notdir } },
197aa59c4cbSrsb { VOPNAME_INACTIVE, { .vop_inactive = ctfs_tmpl_inactive } },
1987c478bd9Sstevel@tonic-gate { NULL, NULL }
1997c478bd9Sstevel@tonic-gate };
200