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 /* 22aa59c4cbSrsb * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <sys/param.h> 307c478bd9Sstevel@tonic-gate #include <sys/time.h> 317c478bd9Sstevel@tonic-gate #include <sys/cred.h> 327c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 33aa59c4cbSrsb #include <sys/vfs_opreg.h> 347c478bd9Sstevel@tonic-gate #include <sys/gfs.h> 357c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 367c478bd9Sstevel@tonic-gate #include <sys/systm.h> 377c478bd9Sstevel@tonic-gate #include <sys/errno.h> 387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 397c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h> 407c478bd9Sstevel@tonic-gate #include <sys/contract.h> 417c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h> 427c478bd9Sstevel@tonic-gate #include <sys/ctfs.h> 437c478bd9Sstevel@tonic-gate #include <sys/ctfs_impl.h> 447c478bd9Sstevel@tonic-gate #include <sys/file.h> 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* 477c478bd9Sstevel@tonic-gate * CTFS routines for the /system/contract/<type>/<ctid>/ctl vnode. 487c478bd9Sstevel@tonic-gate * CTFS routines for the /system/contract/<type>/<ctid>/status vnode. 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate /* 527c478bd9Sstevel@tonic-gate * ctfs_create_ctlnode 537c478bd9Sstevel@tonic-gate * 547c478bd9Sstevel@tonic-gate * If necessary, creates a ctlnode for a ctl file and inserts it into 557c478bd9Sstevel@tonic-gate * the specified cdirnode's gfs_dir_t. Returns either the existing 567c478bd9Sstevel@tonic-gate * vnode or the new one. 577c478bd9Sstevel@tonic-gate */ 587c478bd9Sstevel@tonic-gate vnode_t * 597c478bd9Sstevel@tonic-gate ctfs_create_ctlnode(vnode_t *pvp) 607c478bd9Sstevel@tonic-gate { 617c478bd9Sstevel@tonic-gate ctfs_ctlnode_t *ctlnode; 627c478bd9Sstevel@tonic-gate ctfs_cdirnode_t *cdirnode = pvp->v_data; 637c478bd9Sstevel@tonic-gate vnode_t *vp; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_ctl); 667c478bd9Sstevel@tonic-gate ctlnode = vp->v_data; 677c478bd9Sstevel@tonic-gate /* 687c478bd9Sstevel@tonic-gate * We transitively have a hold on the contract through our 697c478bd9Sstevel@tonic-gate * parent directory. 707c478bd9Sstevel@tonic-gate */ 717c478bd9Sstevel@tonic-gate ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate return (vp); 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * ctfs_ctl_access - VOP_ACCESS entry point 787c478bd9Sstevel@tonic-gate * 797c478bd9Sstevel@tonic-gate * You only get to access ctl files for contracts you own or were 807c478bd9Sstevel@tonic-gate * abandoned and inherited by your containing process contract. 817c478bd9Sstevel@tonic-gate */ 827c478bd9Sstevel@tonic-gate /* ARGSUSED */ 837c478bd9Sstevel@tonic-gate static int 84*da6c28aaSamw ctfs_ctl_access( 85*da6c28aaSamw vnode_t *vp, 86*da6c28aaSamw int mode, 87*da6c28aaSamw int flags, 88*da6c28aaSamw cred_t *cr, 89*da6c28aaSamw caller_context_t *cct) 907c478bd9Sstevel@tonic-gate { 917c478bd9Sstevel@tonic-gate ctfs_ctlnode_t *ctlnode = vp->v_data; 927c478bd9Sstevel@tonic-gate contract_t *ct = ctlnode->ctfs_ctl_contract; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate if (mode & (VEXEC | VREAD)) 957c478bd9Sstevel@tonic-gate return (EACCES); 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 987c478bd9Sstevel@tonic-gate if ((curproc == ct->ct_owner) || 997c478bd9Sstevel@tonic-gate (ct->ct_owner == NULL && ct->ct_regent != NULL && 1007c478bd9Sstevel@tonic-gate ct->ct_regent->ct_data == curproc->p_ct_process)) { 1017c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 1027c478bd9Sstevel@tonic-gate return (0); 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 1067c478bd9Sstevel@tonic-gate return (EACCES); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate /* 1107c478bd9Sstevel@tonic-gate * ctfs_ctl_open - VOP_OPEN entry point 1117c478bd9Sstevel@tonic-gate * 1127c478bd9Sstevel@tonic-gate * Just checks to make sure the mode bits are set, and that the 1137c478bd9Sstevel@tonic-gate * constraints imposed by ctfs_ctl_access are met. 1147c478bd9Sstevel@tonic-gate */ 1157c478bd9Sstevel@tonic-gate static int 116*da6c28aaSamw ctfs_ctl_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 1177c478bd9Sstevel@tonic-gate { 1187c478bd9Sstevel@tonic-gate if (flag != (FWRITE | FOFFMAX)) 1197c478bd9Sstevel@tonic-gate return (EINVAL); 1207c478bd9Sstevel@tonic-gate 121*da6c28aaSamw return (ctfs_ctl_access(*vpp, VWRITE, 0, cr, ct)); 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* 125567faccaSacruz * ctfs_ctl_common_getattr 126*da6c28aaSamw * Implements functionality common to ctl and status ctfs VOP_GETATTR 127567faccaSacruz * entry points. It assumes vp->v_data is set 1287c478bd9Sstevel@tonic-gate */ 1297c478bd9Sstevel@tonic-gate static int 130964da2a9Sacruz ctfs_ctl_common_getattr(vnode_t *vp, vattr_t *vap) 1317c478bd9Sstevel@tonic-gate { 1327c478bd9Sstevel@tonic-gate ctfs_ctlnode_t *ctlnode = vp->v_data; 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate vap->va_type = VREG; 1357c478bd9Sstevel@tonic-gate vap->va_nlink = 1; 1367c478bd9Sstevel@tonic-gate vap->va_size = 0; 1377c478bd9Sstevel@tonic-gate vap->va_ctime = ctlnode->ctfs_ctl_contract->ct_ctime; 1387c478bd9Sstevel@tonic-gate mutex_enter(&ctlnode->ctfs_ctl_contract->ct_events.ctq_lock); 1397c478bd9Sstevel@tonic-gate vap->va_atime = vap->va_mtime = 1407c478bd9Sstevel@tonic-gate ctlnode->ctfs_ctl_contract->ct_events.ctq_atime; 1417c478bd9Sstevel@tonic-gate mutex_exit(&ctlnode->ctfs_ctl_contract->ct_events.ctq_lock); 1427c478bd9Sstevel@tonic-gate ctfs_common_getattr(vp, vap); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate return (0); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate /* 148567faccaSacruz * ctfs_ctl_getattr - VOP_GETATTR entry point 149567faccaSacruz */ 150567faccaSacruz /* ARGSUSED */ 151567faccaSacruz static int 152*da6c28aaSamw ctfs_ctl_getattr(vnode_t *vp, vattr_t *vap, int flags, 153*da6c28aaSamw cred_t *cr, caller_context_t *ct) 154567faccaSacruz { 155567faccaSacruz vap->va_mode = 0222; 156567faccaSacruz 157964da2a9Sacruz return (ctfs_ctl_common_getattr(vp, vap)); 158567faccaSacruz } 159567faccaSacruz 160567faccaSacruz /* 161567faccaSacruz * ctfs_stat_getattr - VOP_GETATTR entry point 162567faccaSacruz */ 163567faccaSacruz /* ARGSUSED */ 164567faccaSacruz static int 165*da6c28aaSamw ctfs_stat_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 166*da6c28aaSamw caller_context_t *ct) 167567faccaSacruz { 168567faccaSacruz vap->va_mode = 0444; 169567faccaSacruz 170964da2a9Sacruz return (ctfs_ctl_common_getattr(vp, vap)); 171567faccaSacruz } 172567faccaSacruz 173567faccaSacruz /* 1747c478bd9Sstevel@tonic-gate * ctfs_ctl_ioctl - VOP_IOCTL entry point 1757c478bd9Sstevel@tonic-gate * 1767c478bd9Sstevel@tonic-gate * All the ct_ctl_*(3contract) interfaces point here. 1777c478bd9Sstevel@tonic-gate */ 1787c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1797c478bd9Sstevel@tonic-gate static int 180*da6c28aaSamw ctfs_ctl_ioctl( 181*da6c28aaSamw vnode_t *vp, 182*da6c28aaSamw int cmd, 183*da6c28aaSamw intptr_t arg, 184*da6c28aaSamw int flag, 185*da6c28aaSamw cred_t *cr, 186*da6c28aaSamw int *rvalp, 187*da6c28aaSamw caller_context_t *cct) 1887c478bd9Sstevel@tonic-gate { 1897c478bd9Sstevel@tonic-gate ctfs_ctlnode_t *ctlnode = vp->v_data; 1907c478bd9Sstevel@tonic-gate contract_t *ct = ctlnode->ctfs_ctl_contract; 1917c478bd9Sstevel@tonic-gate int error = 0; 1927c478bd9Sstevel@tonic-gate uint64_t event; 19325e8c5aaSvikram int ack; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate switch (cmd) { 1967c478bd9Sstevel@tonic-gate case CT_CABANDON: 1977c478bd9Sstevel@tonic-gate error = contract_abandon(ct, curproc, 1); 1987c478bd9Sstevel@tonic-gate break; 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate case CT_CACK: 20125e8c5aaSvikram case CT_CNACK: 2027c478bd9Sstevel@tonic-gate if (copyin((void *)arg, &event, sizeof (uint64_t))) 2037c478bd9Sstevel@tonic-gate return (EFAULT); 20425e8c5aaSvikram ack = (cmd == CT_CACK) ? CT_ACK : CT_NACK; 20525e8c5aaSvikram error = contract_ack(ct, event, ack); 2067c478bd9Sstevel@tonic-gate break; 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate case CT_CNEWCT: 20925e8c5aaSvikram error = contract_newct(ct); 2107c478bd9Sstevel@tonic-gate break; 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate case CT_CQREQ: 21325e8c5aaSvikram if (copyin((void *)arg, &event, sizeof (uint64_t))) 21425e8c5aaSvikram return (EFAULT); 21525e8c5aaSvikram error = contract_qack(ct, event); 2167c478bd9Sstevel@tonic-gate break; 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate case CT_CADOPT: 2197c478bd9Sstevel@tonic-gate error = contract_adopt(ct, curproc); 2207c478bd9Sstevel@tonic-gate break; 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate default: 2237c478bd9Sstevel@tonic-gate return (EINVAL); 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate return (error); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_ctl[] = { 230aa59c4cbSrsb { VOPNAME_OPEN, { .vop_open = ctfs_ctl_open } }, 231aa59c4cbSrsb { VOPNAME_CLOSE, { .vop_close = ctfs_close } }, 232aa59c4cbSrsb { VOPNAME_IOCTL, { .vop_ioctl = ctfs_ctl_ioctl } }, 233aa59c4cbSrsb { VOPNAME_GETATTR, { .vop_getattr = ctfs_ctl_getattr } }, 234aa59c4cbSrsb { VOPNAME_ACCESS, { .vop_access = ctfs_ctl_access } }, 235aa59c4cbSrsb { VOPNAME_READDIR, { .error = fs_notdir } }, 236aa59c4cbSrsb { VOPNAME_LOOKUP, { .error = fs_notdir } }, 237aa59c4cbSrsb { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 2387c478bd9Sstevel@tonic-gate { NULL, NULL } 2397c478bd9Sstevel@tonic-gate }; 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* 2427c478bd9Sstevel@tonic-gate * ctfs_create_statnode 2437c478bd9Sstevel@tonic-gate * 2447c478bd9Sstevel@tonic-gate * If necessary, creates a ctlnode for a status file and inserts it 2457c478bd9Sstevel@tonic-gate * into the specified cdirnode's gfs_dir_t. Returns either the 2467c478bd9Sstevel@tonic-gate * existing vnode or the new one. 2477c478bd9Sstevel@tonic-gate */ 2487c478bd9Sstevel@tonic-gate vnode_t * 2497c478bd9Sstevel@tonic-gate ctfs_create_statnode(vnode_t *pvp) 2507c478bd9Sstevel@tonic-gate { 2517c478bd9Sstevel@tonic-gate vnode_t *vp; 2527c478bd9Sstevel@tonic-gate ctfs_cdirnode_t *cdirnode = pvp->v_data; 2537c478bd9Sstevel@tonic-gate ctfs_ctlnode_t *ctlnode; 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_stat); 2567c478bd9Sstevel@tonic-gate ctlnode = vp->v_data; 2577c478bd9Sstevel@tonic-gate /* 2587c478bd9Sstevel@tonic-gate * We transitively have a hold on the contract through our 2597c478bd9Sstevel@tonic-gate * parent directory. 2607c478bd9Sstevel@tonic-gate */ 2617c478bd9Sstevel@tonic-gate ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract; 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate return (vp); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate /* 2677c478bd9Sstevel@tonic-gate * ctfs_stat_ioctl - VOP_IOCTL entry point 2687c478bd9Sstevel@tonic-gate * 2697c478bd9Sstevel@tonic-gate * The kernel half of ct_status_read(3contract). 2707c478bd9Sstevel@tonic-gate */ 2717c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2727c478bd9Sstevel@tonic-gate static int 273*da6c28aaSamw ctfs_stat_ioctl( 274*da6c28aaSamw vnode_t *vp, 275*da6c28aaSamw int cmd, 276*da6c28aaSamw intptr_t arg, 277*da6c28aaSamw int flag, 278*da6c28aaSamw cred_t *cr, 279*da6c28aaSamw int *rvalp, 280*da6c28aaSamw caller_context_t *cct) 2817c478bd9Sstevel@tonic-gate { 2827c478bd9Sstevel@tonic-gate ctfs_ctlnode_t *statnode = vp->v_data; 2837c478bd9Sstevel@tonic-gate contract_t *ct = statnode->ctfs_ctl_contract; 2847c478bd9Sstevel@tonic-gate ct_type_t *type = ct->ct_type; 2857c478bd9Sstevel@tonic-gate STRUCT_DECL(ct_status, st); 2867c478bd9Sstevel@tonic-gate nvlist_t *foo; 2877c478bd9Sstevel@tonic-gate char *bufp = NULL; 2887c478bd9Sstevel@tonic-gate size_t len; 2897c478bd9Sstevel@tonic-gate model_t mdl = get_udatamodel(); 2907c478bd9Sstevel@tonic-gate uint_t detail; 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate STRUCT_INIT(st, mdl); 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate if (cmd != CT_SSTATUS) 2957c478bd9Sstevel@tonic-gate return (EINVAL); 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate if (copyin((void *)arg, STRUCT_BUF(st), STRUCT_SIZE(st))) 2987c478bd9Sstevel@tonic-gate return (EFAULT); 2997c478bd9Sstevel@tonic-gate detail = STRUCT_FGET(st, ctst_detail); 3007c478bd9Sstevel@tonic-gate if (detail == CTD_COMMON) { 3017c478bd9Sstevel@tonic-gate mutex_enter(&ct->ct_lock); 302fa9e4066Sahrens contract_status_common(ct, VTOZONE(vp), STRUCT_BUF(st), mdl); 3037c478bd9Sstevel@tonic-gate mutex_exit(&ct->ct_lock); 3047c478bd9Sstevel@tonic-gate } else if (detail <= CTD_ALL) { 3057c478bd9Sstevel@tonic-gate VERIFY(nvlist_alloc(&foo, NV_UNIQUE_NAME, KM_SLEEP) == 0); 306fa9e4066Sahrens type->ct_type_ops->contop_status(ct, VTOZONE(vp), detail, foo, 3077c478bd9Sstevel@tonic-gate STRUCT_BUF(st), mdl); 3087c478bd9Sstevel@tonic-gate VERIFY(nvlist_pack(foo, &bufp, &len, NV_ENCODE_NATIVE, 3097c478bd9Sstevel@tonic-gate KM_SLEEP) == 0); 3107c478bd9Sstevel@tonic-gate nvlist_free(foo); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate if ((len <= STRUCT_FGET(st, ctst_nbytes)) && 3137c478bd9Sstevel@tonic-gate (copyout(bufp, STRUCT_FGETP(st, ctst_buffer), len) == -1)) { 3147c478bd9Sstevel@tonic-gate kmem_free(bufp, len); 3157c478bd9Sstevel@tonic-gate return (EFAULT); 3167c478bd9Sstevel@tonic-gate } 3177c478bd9Sstevel@tonic-gate kmem_free(bufp, len); 3187c478bd9Sstevel@tonic-gate STRUCT_FSET(st, ctst_nbytes, len); 3197c478bd9Sstevel@tonic-gate } else { 3207c478bd9Sstevel@tonic-gate return (EINVAL); 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate if (copyout(STRUCT_BUF(st), (void *)arg, STRUCT_SIZE(st))) 3237c478bd9Sstevel@tonic-gate return (EFAULT); 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate return (0); 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_stat[] = { 329aa59c4cbSrsb { VOPNAME_OPEN, { .vop_open = ctfs_open } }, 330aa59c4cbSrsb { VOPNAME_CLOSE, { .vop_close = ctfs_close } }, 331aa59c4cbSrsb { VOPNAME_IOCTL, { .vop_ioctl = ctfs_stat_ioctl } }, 332567faccaSacruz { VOPNAME_GETATTR, { .vop_getattr = ctfs_stat_getattr } }, 333aa59c4cbSrsb { VOPNAME_ACCESS, { .vop_access = ctfs_access_readonly } }, 334aa59c4cbSrsb { VOPNAME_READDIR, { .error = fs_notdir } }, 335aa59c4cbSrsb { VOPNAME_LOOKUP, { .error = fs_notdir } }, 336aa59c4cbSrsb { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } }, 3377c478bd9Sstevel@tonic-gate { NULL, NULL } 3387c478bd9Sstevel@tonic-gate }; 339