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 /* 22*6073Sacruz * 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 #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> 45*6073Sacruz #include <sys/model.h> 460Sstevel@tonic-gate 470Sstevel@tonic-gate /* 480Sstevel@tonic-gate * CTFS routines for the /system/contract/<type>/template vnode. 490Sstevel@tonic-gate */ 500Sstevel@tonic-gate 510Sstevel@tonic-gate /* 520Sstevel@tonic-gate * ctfs_create_tmplnode 530Sstevel@tonic-gate * 540Sstevel@tonic-gate * Creates a new template and tdirnode, and returns the tdirnode. 550Sstevel@tonic-gate */ 560Sstevel@tonic-gate vnode_t * 570Sstevel@tonic-gate ctfs_create_tmplnode(vnode_t *pvp) 580Sstevel@tonic-gate { 590Sstevel@tonic-gate vnode_t *vp; 600Sstevel@tonic-gate ctfs_tmplnode_t *tmplnode; 610Sstevel@tonic-gate 620Sstevel@tonic-gate ASSERT(gfs_file_index(pvp) < ct_ntypes); 630Sstevel@tonic-gate 640Sstevel@tonic-gate vp = gfs_file_create(sizeof (ctfs_tmplnode_t), pvp, ctfs_ops_tmpl); 650Sstevel@tonic-gate tmplnode = vp->v_data; 660Sstevel@tonic-gate tmplnode->ctfs_tmn_tmpl = 670Sstevel@tonic-gate ct_types[gfs_file_index(pvp)]->ct_type_default(); 680Sstevel@tonic-gate 690Sstevel@tonic-gate return (vp); 700Sstevel@tonic-gate } 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * ctfs_tmpl_open - VOP_OPEN entry point 740Sstevel@tonic-gate * 750Sstevel@tonic-gate * Just ensures the right mode bits are set. 760Sstevel@tonic-gate */ 770Sstevel@tonic-gate /* ARGSUSED */ 780Sstevel@tonic-gate static int 795331Samw ctfs_tmpl_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 800Sstevel@tonic-gate { 810Sstevel@tonic-gate if (flag != (FREAD | FWRITE | FOFFMAX)) 820Sstevel@tonic-gate return (EINVAL); 830Sstevel@tonic-gate 840Sstevel@tonic-gate return (0); 850Sstevel@tonic-gate } 860Sstevel@tonic-gate 870Sstevel@tonic-gate /* 880Sstevel@tonic-gate * ctfs_tmpl_getattr - VOP_GETATTR entry point 890Sstevel@tonic-gate */ 900Sstevel@tonic-gate /* ARGSUSED */ 910Sstevel@tonic-gate static int 925331Samw ctfs_tmpl_getattr( 935331Samw vnode_t *vp, 945331Samw vattr_t *vap, 955331Samw int flags, 965331Samw cred_t *cr, 975331Samw caller_context_t *ct) 980Sstevel@tonic-gate { 990Sstevel@tonic-gate vap->va_type = VREG; 1000Sstevel@tonic-gate vap->va_mode = 0666; 1010Sstevel@tonic-gate vap->va_nlink = 1; 1020Sstevel@tonic-gate vap->va_size = 0; 1030Sstevel@tonic-gate vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime; 1040Sstevel@tonic-gate vap->va_ctime.tv_nsec = 0; 1050Sstevel@tonic-gate vap->va_atime = vap->va_mtime = vap->va_ctime; 1060Sstevel@tonic-gate ctfs_common_getattr(vp, vap); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate return (0); 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* 1120Sstevel@tonic-gate * ctfs_tmpl_ioctl - VOP_IOCTL entry point 1130Sstevel@tonic-gate * 1140Sstevel@tonic-gate * All the ct_tmpl_*(3contract) interfaces point here. 1150Sstevel@tonic-gate */ 1160Sstevel@tonic-gate /* ARGSUSED */ 1170Sstevel@tonic-gate static int 1185331Samw ctfs_tmpl_ioctl( 1195331Samw vnode_t *vp, 1205331Samw int cmd, 1215331Samw intptr_t arg, 1225331Samw int flag, 1235331Samw cred_t *cr, 1245331Samw int *rvalp, 1255331Samw caller_context_t *ct) 1260Sstevel@tonic-gate { 1270Sstevel@tonic-gate ctfs_tmplnode_t *tmplnode = vp->v_data; 1280Sstevel@tonic-gate ct_param_t param; 129*6073Sacruz STRUCT_DECL(ct_param, uarg); 1304845Svikram ctid_t ctid; 131*6073Sacruz uint32_t local_ctpm_size; 1320Sstevel@tonic-gate int error; 1330Sstevel@tonic-gate 134*6073Sacruz STRUCT_INIT(uarg, flag); 135*6073Sacruz 1360Sstevel@tonic-gate switch (cmd) { 1370Sstevel@tonic-gate case CT_TACTIVATE: 1380Sstevel@tonic-gate ASSERT(tmplnode->ctfs_tmn_tmpl != NULL); 1390Sstevel@tonic-gate ctmpl_activate(tmplnode->ctfs_tmn_tmpl); 1400Sstevel@tonic-gate break; 1410Sstevel@tonic-gate case CT_TCLEAR: 1420Sstevel@tonic-gate ASSERT(tmplnode->ctfs_tmn_tmpl != NULL); 1430Sstevel@tonic-gate ctmpl_clear(tmplnode->ctfs_tmn_tmpl); 1440Sstevel@tonic-gate break; 1450Sstevel@tonic-gate case CT_TCREATE: 1460Sstevel@tonic-gate ASSERT(tmplnode->ctfs_tmn_tmpl != NULL); 1474845Svikram error = ctmpl_create(tmplnode->ctfs_tmn_tmpl, &ctid); 1484845Svikram if (error) 1494845Svikram return (error); 1504845Svikram *rvalp = ctid; 1514845Svikram break; 1520Sstevel@tonic-gate case CT_TSET: 153*6073Sacruz if (copyin((void *)arg, STRUCT_BUF(uarg), STRUCT_SIZE(uarg))) 1540Sstevel@tonic-gate return (EFAULT); 155*6073Sacruz param.ctpm_id = STRUCT_FGET(uarg, ctpm_id); 156*6073Sacruz param.ctpm_size = STRUCT_FGET(uarg, ctpm_size); 157*6073Sacruz if (param.ctpm_size > CT_PARAM_MAX_SIZE || 158*6073Sacruz param.ctpm_size == 0) 159*6073Sacruz return (EINVAL); 160*6073Sacruz param.ctpm_value = kmem_alloc(param.ctpm_size, KM_SLEEP); 161*6073Sacruz if (copyin(STRUCT_FGETP(uarg, ctpm_value), param.ctpm_value, 162*6073Sacruz param.ctpm_size)) 163*6073Sacruz return (EFAULT); 164*6073Sacruz error = ctmpl_set(tmplnode->ctfs_tmn_tmpl, ¶m, cr); 165*6073Sacruz kmem_free(param.ctpm_value, param.ctpm_size); 166*6073Sacruz return (error); 1670Sstevel@tonic-gate case CT_TGET: 168*6073Sacruz if (copyin((void *)arg, STRUCT_BUF(uarg), STRUCT_SIZE(uarg))) 1690Sstevel@tonic-gate return (EFAULT); 170*6073Sacruz param.ctpm_id = STRUCT_FGET(uarg, ctpm_id); 171*6073Sacruz param.ctpm_size = STRUCT_FGET(uarg, ctpm_size); 172*6073Sacruz if (param.ctpm_size > CT_PARAM_MAX_SIZE) 173*6073Sacruz param.ctpm_size = CT_PARAM_MAX_SIZE; 174*6073Sacruz if (param.ctpm_size == 0) 175*6073Sacruz return (EINVAL); 176*6073Sacruz local_ctpm_size = param.ctpm_size; 177*6073Sacruz param.ctpm_value = kmem_alloc(param.ctpm_size, KM_SLEEP); 1780Sstevel@tonic-gate error = ctmpl_get(tmplnode->ctfs_tmn_tmpl, ¶m); 179*6073Sacruz STRUCT_FSET(uarg, ctpm_size, param.ctpm_size); 180*6073Sacruz if (!error && 181*6073Sacruz (copyout(param.ctpm_value, STRUCT_FGETP(uarg, ctpm_value), 182*6073Sacruz MIN(local_ctpm_size, param.ctpm_size))) || 183*6073Sacruz copyout(STRUCT_BUF(uarg), (void *)arg, STRUCT_SIZE(uarg))) 184*6073Sacruz error = EFAULT; 185*6073Sacruz kmem_free(param.ctpm_value, local_ctpm_size); 186*6073Sacruz return (error); 1870Sstevel@tonic-gate default: 1880Sstevel@tonic-gate return (EINVAL); 1890Sstevel@tonic-gate } 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate return (0); 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate /* 1950Sstevel@tonic-gate * ctfs_tmpl_inactive - VOP_INACTIVE entry point 1960Sstevel@tonic-gate */ 1970Sstevel@tonic-gate /* ARGSUSED */ 1980Sstevel@tonic-gate static void 1995331Samw ctfs_tmpl_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 2000Sstevel@tonic-gate { 2010Sstevel@tonic-gate ctfs_tmplnode_t *tmplnode; 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate if ((tmplnode = gfs_file_inactive(vp)) != NULL) { 2040Sstevel@tonic-gate ctmpl_free(tmplnode->ctfs_tmn_tmpl); 2050Sstevel@tonic-gate kmem_free(tmplnode, sizeof (ctfs_tmplnode_t)); 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_tmpl[] = { 2103898Srsb { VOPNAME_OPEN, { .vop_open = ctfs_tmpl_open } }, 2113898Srsb { VOPNAME_CLOSE, { .vop_close = ctfs_close } }, 2123898Srsb { VOPNAME_IOCTL, { .vop_ioctl = ctfs_tmpl_ioctl } }, 2133898Srsb { VOPNAME_GETATTR, { .vop_getattr = ctfs_tmpl_getattr } }, 2143898Srsb { VOPNAME_ACCESS, { .vop_access = ctfs_access_readwrite } }, 2153898Srsb { VOPNAME_READDIR, { .error = fs_notdir } }, 2163898Srsb { VOPNAME_LOOKUP, { .error = fs_notdir } }, 2173898Srsb { VOPNAME_INACTIVE, { .vop_inactive = ctfs_tmpl_inactive } }, 2180Sstevel@tonic-gate { NULL, NULL } 2190Sstevel@tonic-gate }; 220