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 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 
  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>/latest vnode.
  48  */
  49 
  50 /*
  51  * ctfs_create_latenode
  52  */
  53 vnode_t *
  54 ctfs_create_latenode(vnode_t *pvp)
  55 {
  56         return (gfs_file_create(sizeof (ctfs_latenode_t), pvp,
  57             ctfs_ops_latest));
  58 }
  59 
  60 /*
  61  * ctfs_latest_nested_open
  62  *
  63  * The latest node is just a doorway to the status file; this function
  64  * is used by ctfs_latest_access, ctfs_latest_open, and
  65  * ctfs_latest_getattr to obtain that file.
  66  */
  67 static vnode_t *
  68 ctfs_latest_nested_open(vnode_t *vp, cred_t *cr)
  69 {
  70         contract_t *ct = ttolwp(curthread)->lwp_ct_latest[
  71             gfs_file_index(gfs_file_parent(vp))];
  72 
  73         if (ct) {
  74                 vnode_t *cvp, *svp;
  75 
  76                 cvp = ctfs_create_cdirnode(gfs_file_parent(vp), ct);
  77 
  78                 gfs_file_set_index(cvp, -1);
  79 
  80                 VERIFY(gfs_dir_lookup(cvp, "status", &svp,
  81                     cr, 0, NULL, NULL) == 0);
  82 
  83                 VN_RELE(cvp);
  84 
  85                 return (svp);
  86         }
  87 
  88         return (NULL);
  89 }
  90 
  91 /*
  92  * ctfs_latest_access - VOP_ACCESS entry point
  93  *
  94  * Fails if there isn't a latest contract.
  95  */
  96 /* ARGSUSED */
  97 static int
  98 ctfs_latest_access(
  99         vnode_t *vp,
 100         int mode,
 101         int flags,
 102         cred_t *cr,
 103         caller_context_t *ct)
 104 {
 105         vnode_t *nvp;
 106 
 107         if (mode & (VEXEC | VWRITE))
 108                 return (EACCES);
 109 
 110         if (nvp = ctfs_latest_nested_open(vp, cr)) {
 111                 VN_RELE(nvp);
 112                 return (0);
 113         }
 114 
 115         return (ESRCH);
 116 }
 117 
 118 /*
 119  * ctfs_latest_open - VOP_OPEN entry point
 120  *
 121  * After checking the mode bits, opens and returns the status file for
 122  * the LWP's latest contract.
 123  */
 124 static int
 125 ctfs_latest_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
 126 {
 127         vnode_t *nvp;
 128 
 129         if (flag != (FREAD | FOFFMAX))
 130                 return (EINVAL);
 131 
 132         if (nvp = ctfs_latest_nested_open(*vpp, cr)) {
 133                 VN_RELE(*vpp);
 134                 *vpp = nvp;
 135                 return (VOP_OPEN(vpp, flag, cr, ct));
 136         }
 137 
 138         return (ESRCH);
 139 }
 140 
 141 /*
 142  * ctfs_latest_getattr - the VOP_GETATTR entry point
 143  *
 144  * Fetches and calls VOP_GETATTR on the status file for the LWP's
 145  * latest contract.  Otherwise it fakes up something bland.
 146  */
 147 static int
 148 ctfs_latest_getattr(
 149         vnode_t *vp,
 150         vattr_t *vap,
 151         int flags,
 152         cred_t *cr,
 153         caller_context_t *ct)
 154 {
 155         vnode_t *nvp;
 156 
 157         if (nvp = ctfs_latest_nested_open(vp, cr)) {
 158                 int res = VOP_GETATTR(nvp, vap, flags, cr, ct);
 159                 VN_RELE(nvp);
 160                 return (res);
 161         }
 162 
 163         vap->va_type = VREG;
 164         vap->va_mode = 0444;
 165         vap->va_nlink = 1;
 166         vap->va_size = 0;
 167         vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
 168         vap->va_ctime.tv_nsec = 0;
 169         vap->va_atime = vap->va_mtime = vap->va_ctime;
 170         ctfs_common_getattr(vp, vap);
 171 
 172         return (0);
 173 }
 174 
 175 const fs_operation_def_t ctfs_tops_latest[] = {
 176         { VOPNAME_OPEN,         { .vop_open = ctfs_latest_open } },
 177         { VOPNAME_CLOSE,        { .error = fs_inval } },
 178         { VOPNAME_IOCTL,        { .error = fs_inval } },
 179         { VOPNAME_GETATTR,      { .vop_getattr = ctfs_latest_getattr } },
 180         { VOPNAME_ACCESS,       { .vop_access = ctfs_latest_access } },
 181         { VOPNAME_READDIR,      { .error = fs_notdir } },
 182         { VOPNAME_LOOKUP,       { .error = fs_notdir } },
 183         { VOPNAME_INACTIVE,     { .vop_inactive = gfs_vop_inactive } },
 184         { NULL,                 { NULL } }
 185 };