1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/types.h> 29 #include <sys/param.h> 30 #include <sys/time.h> 31 #include <sys/cred.h> 32 #include <sys/vfs.h> 33 #include <sys/vfs_opreg.h> 34 #include <sys/gfs.h> 35 #include <sys/vnode.h> 36 #include <sys/systm.h> 37 #include <sys/errno.h> 38 #include <sys/sysmacros.h> 39 #include <fs/fs_subr.h> 40 #include <sys/contract.h> 41 #include <sys/contract_impl.h> 42 #include <sys/ctfs.h> 43 #include <sys/ctfs_impl.h> 44 #include <sys/file.h> 45 46 /* 47 * CTFS routines for the /system/contract/<type>/<ctid>/ctl vnode. 48 * CTFS routines for the /system/contract/<type>/<ctid>/status vnode. 49 */ 50 51 /* 52 * ctfs_create_ctlnode 53 * 54 * If necessary, creates a ctlnode for a ctl file and inserts it into 55 * the specified cdirnode's gfs_dir_t. Returns either the existing 56 * vnode or the new one. 57 */ 58 vnode_t * 59 ctfs_create_ctlnode(vnode_t *pvp) 60 { 61 ctfs_ctlnode_t *ctlnode; 62 ctfs_cdirnode_t *cdirnode = pvp->v_data; 63 vnode_t *vp; 64 65 vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_ctl); 66 ctlnode = vp->v_data; 67 /* 68 * We transitively have a hold on the contract through our 69 * parent directory. 70 */ 71 ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract; 72 73 return (vp); 74 } 75 76 /* 77 * ctfs_ctl_access - VOP_ACCESS entry point 78 * 79 * You only get to access ctl files for contracts you own or were 80 * abandoned and inherited by your containing process contract. 81 */ 82 /* ARGSUSED */ 83 static int 84 ctfs_ctl_access(vnode_t *vp, int mode, int flags, cred_t *cr) 85 { 86 ctfs_ctlnode_t *ctlnode = vp->v_data; 87 contract_t *ct = ctlnode->ctfs_ctl_contract; 88 89 if (mode & (VEXEC | VREAD)) 90 return (EACCES); 91 92 mutex_enter(&ct->ct_lock); 93 if ((curproc == ct->ct_owner) || 94 (ct->ct_owner == NULL && ct->ct_regent != NULL && 95 ct->ct_regent->ct_data == curproc->p_ct_process)) { 96 mutex_exit(&ct->ct_lock); 97 return (0); 98 } 99 100 mutex_exit(&ct->ct_lock); 101 return (EACCES); 102 } 103 104 /* 105 * ctfs_ctl_open - VOP_OPEN entry point 106 * 107 * Just checks to make sure the mode bits are set, and that the 108 * constraints imposed by ctfs_ctl_access are met. 109 */ 110 static int 111 ctfs_ctl_open(vnode_t **vpp, int flag, cred_t *cr) 112 { 113 if (flag != (FWRITE | FOFFMAX)) 114 return (EINVAL); 115 116 return (ctfs_ctl_access(*vpp, VWRITE, 0, cr)); 117 } 118 119 /* 120 * ctfs_ctl_getattr - VOP_GETATTR entry point 121 */ 122 /* ARGSUSED */ 123 static int 124 ctfs_ctl_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr) 125 { 126 ctfs_ctlnode_t *ctlnode = vp->v_data; 127 128 vap->va_type = VREG; 129 vap->va_mode = 0222; 130 vap->va_nlink = 1; 131 vap->va_size = 0; 132 vap->va_ctime = ctlnode->ctfs_ctl_contract->ct_ctime; 133 mutex_enter(&ctlnode->ctfs_ctl_contract->ct_events.ctq_lock); 134 vap->va_atime = vap->va_mtime = 135 ctlnode->ctfs_ctl_contract->ct_events.ctq_atime; 136 mutex_exit(&ctlnode->ctfs_ctl_contract->ct_events.ctq_lock); 137 ctfs_common_getattr(vp, vap); 138 139 return (0); 140 } 141 142 /* 143 * ctfs_ctl_ioctl - VOP_IOCTL entry point 144 * 145 * All the ct_ctl_*(3contract) interfaces point here. 146 */ 147 /* ARGSUSED */ 148 static int 149 ctfs_ctl_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, 150 int *rvalp) 151 { 152 ctfs_ctlnode_t *ctlnode = vp->v_data; 153 contract_t *ct = ctlnode->ctfs_ctl_contract; 154 int error = 0; 155 uint64_t event; 156 157 switch (cmd) { 158 case CT_CABANDON: 159 error = contract_abandon(ct, curproc, 1); 160 break; 161 162 case CT_CACK: 163 if (copyin((void *)arg, &event, sizeof (uint64_t))) 164 return (EFAULT); 165 error = contract_ack(ct, event); 166 break; 167 168 case CT_CNEWCT: 169 break; 170 171 case CT_CQREQ: 172 break; 173 174 case CT_CADOPT: 175 error = contract_adopt(ct, curproc); 176 break; 177 178 default: 179 return (EINVAL); 180 } 181 182 return (error); 183 } 184 185 const fs_operation_def_t ctfs_tops_ctl[] = { 186 { VOPNAME_OPEN, { .vop_open = ctfs_ctl_open } }, 187 { VOPNAME_CLOSE, { .vop_close = ctfs_close } }, 188 { VOPNAME_IOCTL, { .vop_ioctl = ctfs_ctl_ioctl } }, 189 { VOPNAME_GETATTR, { .vop_getattr = ctfs_ctl_getattr } }, 190 { VOPNAME_ACCESS, { .vop_access = ctfs_ctl_access } }, 191 { VOPNAME_READDIR, { .error = fs_notdir } }, 192 { VOPNAME_LOOKUP, { .error = fs_notdir } }, 193 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 194 { NULL, NULL } 195 }; 196 197 /* 198 * ctfs_create_statnode 199 * 200 * If necessary, creates a ctlnode for a status file and inserts it 201 * into the specified cdirnode's gfs_dir_t. Returns either the 202 * existing vnode or the new one. 203 */ 204 vnode_t * 205 ctfs_create_statnode(vnode_t *pvp) 206 { 207 vnode_t *vp; 208 ctfs_cdirnode_t *cdirnode = pvp->v_data; 209 ctfs_ctlnode_t *ctlnode; 210 211 vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_stat); 212 ctlnode = vp->v_data; 213 /* 214 * We transitively have a hold on the contract through our 215 * parent directory. 216 */ 217 ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract; 218 219 return (vp); 220 } 221 222 /* 223 * ctfs_stat_ioctl - VOP_IOCTL entry point 224 * 225 * The kernel half of ct_status_read(3contract). 226 */ 227 /* ARGSUSED */ 228 static int 229 ctfs_stat_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, 230 int *rvalp) 231 { 232 ctfs_ctlnode_t *statnode = vp->v_data; 233 contract_t *ct = statnode->ctfs_ctl_contract; 234 ct_type_t *type = ct->ct_type; 235 STRUCT_DECL(ct_status, st); 236 nvlist_t *foo; 237 char *bufp = NULL; 238 size_t len; 239 model_t mdl = get_udatamodel(); 240 uint_t detail; 241 242 STRUCT_INIT(st, mdl); 243 244 if (cmd != CT_SSTATUS) 245 return (EINVAL); 246 247 if (copyin((void *)arg, STRUCT_BUF(st), STRUCT_SIZE(st))) 248 return (EFAULT); 249 detail = STRUCT_FGET(st, ctst_detail); 250 if (detail == CTD_COMMON) { 251 mutex_enter(&ct->ct_lock); 252 contract_status_common(ct, VTOZONE(vp), STRUCT_BUF(st), mdl); 253 mutex_exit(&ct->ct_lock); 254 } else if (detail <= CTD_ALL) { 255 VERIFY(nvlist_alloc(&foo, NV_UNIQUE_NAME, KM_SLEEP) == 0); 256 type->ct_type_ops->contop_status(ct, VTOZONE(vp), detail, foo, 257 STRUCT_BUF(st), mdl); 258 VERIFY(nvlist_pack(foo, &bufp, &len, NV_ENCODE_NATIVE, 259 KM_SLEEP) == 0); 260 nvlist_free(foo); 261 262 if ((len <= STRUCT_FGET(st, ctst_nbytes)) && 263 (copyout(bufp, STRUCT_FGETP(st, ctst_buffer), len) == -1)) { 264 kmem_free(bufp, len); 265 return (EFAULT); 266 } 267 kmem_free(bufp, len); 268 STRUCT_FSET(st, ctst_nbytes, len); 269 } else { 270 return (EINVAL); 271 } 272 if (copyout(STRUCT_BUF(st), (void *)arg, STRUCT_SIZE(st))) 273 return (EFAULT); 274 275 return (0); 276 } 277 278 const fs_operation_def_t ctfs_tops_stat[] = { 279 { VOPNAME_OPEN, { .vop_open = ctfs_open } }, 280 { VOPNAME_CLOSE, { .vop_close = ctfs_close } }, 281 { VOPNAME_IOCTL, { .vop_ioctl = ctfs_stat_ioctl } }, 282 { VOPNAME_GETATTR, { .vop_getattr = ctfs_ctl_getattr } }, 283 { VOPNAME_ACCESS, { .vop_access = ctfs_access_readonly } }, 284 { VOPNAME_READDIR, { .error = fs_notdir } }, 285 { VOPNAME_LOOKUP, { .error = fs_notdir } }, 286 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 287 { NULL, NULL } 288 }; 289