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 /*
226073Sacruz * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/time.h>
290Sstevel@tonic-gate #include <sys/cred.h>
300Sstevel@tonic-gate #include <sys/vfs.h>
313898Srsb #include <sys/vfs_opreg.h>
320Sstevel@tonic-gate #include <sys/gfs.h>
330Sstevel@tonic-gate #include <sys/vnode.h>
340Sstevel@tonic-gate #include <sys/systm.h>
350Sstevel@tonic-gate #include <sys/errno.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <fs/fs_subr.h>
380Sstevel@tonic-gate #include <sys/contract.h>
390Sstevel@tonic-gate #include <sys/contract_impl.h>
400Sstevel@tonic-gate #include <sys/ctfs.h>
410Sstevel@tonic-gate #include <sys/ctfs_impl.h>
420Sstevel@tonic-gate #include <sys/file.h>
436073Sacruz #include <sys/model.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * CTFS routines for the /system/contract/<type>/template vnode.
470Sstevel@tonic-gate */
480Sstevel@tonic-gate
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate * ctfs_create_tmplnode
510Sstevel@tonic-gate *
520Sstevel@tonic-gate * Creates a new template and tdirnode, and returns the tdirnode.
530Sstevel@tonic-gate */
540Sstevel@tonic-gate vnode_t *
ctfs_create_tmplnode(vnode_t * pvp)550Sstevel@tonic-gate ctfs_create_tmplnode(vnode_t *pvp)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate vnode_t *vp;
580Sstevel@tonic-gate ctfs_tmplnode_t *tmplnode;
590Sstevel@tonic-gate
600Sstevel@tonic-gate ASSERT(gfs_file_index(pvp) < ct_ntypes);
610Sstevel@tonic-gate
620Sstevel@tonic-gate vp = gfs_file_create(sizeof (ctfs_tmplnode_t), pvp, ctfs_ops_tmpl);
630Sstevel@tonic-gate tmplnode = vp->v_data;
640Sstevel@tonic-gate tmplnode->ctfs_tmn_tmpl =
650Sstevel@tonic-gate ct_types[gfs_file_index(pvp)]->ct_type_default();
660Sstevel@tonic-gate
670Sstevel@tonic-gate return (vp);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate * ctfs_tmpl_open - VOP_OPEN entry point
720Sstevel@tonic-gate *
730Sstevel@tonic-gate * Just ensures the right mode bits are set.
740Sstevel@tonic-gate */
750Sstevel@tonic-gate /* ARGSUSED */
760Sstevel@tonic-gate static int
ctfs_tmpl_open(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)775331Samw ctfs_tmpl_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate if (flag != (FREAD | FWRITE | FOFFMAX))
800Sstevel@tonic-gate return (EINVAL);
810Sstevel@tonic-gate
820Sstevel@tonic-gate return (0);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * ctfs_tmpl_getattr - VOP_GETATTR entry point
870Sstevel@tonic-gate */
880Sstevel@tonic-gate /* ARGSUSED */
890Sstevel@tonic-gate static int
ctfs_tmpl_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)905331Samw ctfs_tmpl_getattr(
915331Samw vnode_t *vp,
925331Samw vattr_t *vap,
935331Samw int flags,
945331Samw cred_t *cr,
955331Samw caller_context_t *ct)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate vap->va_type = VREG;
980Sstevel@tonic-gate vap->va_mode = 0666;
990Sstevel@tonic-gate vap->va_nlink = 1;
1000Sstevel@tonic-gate vap->va_size = 0;
1010Sstevel@tonic-gate vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
1020Sstevel@tonic-gate vap->va_ctime.tv_nsec = 0;
1030Sstevel@tonic-gate vap->va_atime = vap->va_mtime = vap->va_ctime;
1040Sstevel@tonic-gate ctfs_common_getattr(vp, vap);
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate return (0);
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate * ctfs_tmpl_ioctl - VOP_IOCTL entry point
1110Sstevel@tonic-gate *
1120Sstevel@tonic-gate * All the ct_tmpl_*(3contract) interfaces point here.
1130Sstevel@tonic-gate */
1140Sstevel@tonic-gate /* ARGSUSED */
1150Sstevel@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)1165331Samw ctfs_tmpl_ioctl(
1175331Samw vnode_t *vp,
1185331Samw int cmd,
1195331Samw intptr_t arg,
1205331Samw int flag,
1215331Samw cred_t *cr,
1225331Samw int *rvalp,
1235331Samw caller_context_t *ct)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate ctfs_tmplnode_t *tmplnode = vp->v_data;
126*7937SAntonello.Cruz@Sun.COM ct_kparam_t kparam;
127*7937SAntonello.Cruz@Sun.COM ct_param_t *param = &kparam.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:
148*7937SAntonello.Cruz@Sun.COM error = ctparam_copyin((void *)arg, &kparam, flag, cmd);
149*7937SAntonello.Cruz@Sun.COM if (error != 0)
150*7937SAntonello.Cruz@Sun.COM return (error);
151*7937SAntonello.Cruz@Sun.COM error = ctmpl_set(tmplnode->ctfs_tmn_tmpl, &kparam, cr);
152*7937SAntonello.Cruz@Sun.COM kmem_free(kparam.ctpm_kbuf, param->ctpm_size);
153*7937SAntonello.Cruz@Sun.COM
1546073Sacruz return (error);
1550Sstevel@tonic-gate case CT_TGET:
156*7937SAntonello.Cruz@Sun.COM error = ctparam_copyin((void *)arg, &kparam, flag, cmd);
157*7937SAntonello.Cruz@Sun.COM if (error != 0)
158*7937SAntonello.Cruz@Sun.COM return (error);
159*7937SAntonello.Cruz@Sun.COM error = ctmpl_get(tmplnode->ctfs_tmn_tmpl, &kparam);
160*7937SAntonello.Cruz@Sun.COM if (error != 0) {
161*7937SAntonello.Cruz@Sun.COM kmem_free(kparam.ctpm_kbuf, param->ctpm_size);
162*7937SAntonello.Cruz@Sun.COM } else {
163*7937SAntonello.Cruz@Sun.COM error = ctparam_copyout(&kparam, (void *)arg, flag);
164*7937SAntonello.Cruz@Sun.COM }
165*7937SAntonello.Cruz@Sun.COM
1666073Sacruz return (error);
1670Sstevel@tonic-gate default:
1680Sstevel@tonic-gate return (EINVAL);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate return (0);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate * ctfs_tmpl_inactive - VOP_INACTIVE entry point
1760Sstevel@tonic-gate */
1770Sstevel@tonic-gate /* ARGSUSED */
1780Sstevel@tonic-gate static void
ctfs_tmpl_inactive(vnode_t * vp,cred_t * cr,caller_context_t * ct)1795331Samw ctfs_tmpl_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate ctfs_tmplnode_t *tmplnode;
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate if ((tmplnode = gfs_file_inactive(vp)) != NULL) {
1840Sstevel@tonic-gate ctmpl_free(tmplnode->ctfs_tmn_tmpl);
1850Sstevel@tonic-gate kmem_free(tmplnode, sizeof (ctfs_tmplnode_t));
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_tmpl[] = {
1903898Srsb { VOPNAME_OPEN, { .vop_open = ctfs_tmpl_open } },
1913898Srsb { VOPNAME_CLOSE, { .vop_close = ctfs_close } },
1923898Srsb { VOPNAME_IOCTL, { .vop_ioctl = ctfs_tmpl_ioctl } },
1933898Srsb { VOPNAME_GETATTR, { .vop_getattr = ctfs_tmpl_getattr } },
1943898Srsb { VOPNAME_ACCESS, { .vop_access = ctfs_access_readwrite } },
1953898Srsb { VOPNAME_READDIR, { .error = fs_notdir } },
1963898Srsb { VOPNAME_LOOKUP, { .error = fs_notdir } },
1973898Srsb { VOPNAME_INACTIVE, { .vop_inactive = ctfs_tmpl_inactive } },
1980Sstevel@tonic-gate { NULL, NULL }
1990Sstevel@tonic-gate };
200