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 /*
22*ab04eb8eStimh  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/time.h>
297c478bd9Sstevel@tonic-gate #include <sys/cred.h>
307c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
31aa59c4cbSrsb #include <sys/vfs_opreg.h>
327c478bd9Sstevel@tonic-gate #include <sys/gfs.h>
337c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
387c478bd9Sstevel@tonic-gate #include <sys/contract.h>
397c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
407c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
417c478bd9Sstevel@tonic-gate #include <sys/ctfs_impl.h>
427c478bd9Sstevel@tonic-gate #include <sys/file.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * CTFS routines for the /system/contract/<type>/latest vnode.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * ctfs_create_latenode
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate vnode_t *
ctfs_create_latenode(vnode_t * pvp)527c478bd9Sstevel@tonic-gate ctfs_create_latenode(vnode_t *pvp)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	return (gfs_file_create(sizeof (ctfs_latenode_t), pvp,
557c478bd9Sstevel@tonic-gate 	    ctfs_ops_latest));
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * ctfs_latest_nested_open
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * The latest node is just a doorway to the status file; this function
627c478bd9Sstevel@tonic-gate  * is used by ctfs_latest_access, ctfs_latest_open, and
637c478bd9Sstevel@tonic-gate  * ctfs_latest_getattr to obtain that file.
647c478bd9Sstevel@tonic-gate  */
657c478bd9Sstevel@tonic-gate static vnode_t *
ctfs_latest_nested_open(vnode_t * vp,cred_t * cr)66da6c28aaSamw ctfs_latest_nested_open(vnode_t *vp, cred_t *cr)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	contract_t *ct = ttolwp(curthread)->lwp_ct_latest[
697c478bd9Sstevel@tonic-gate 	    gfs_file_index(gfs_file_parent(vp))];
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	if (ct) {
727c478bd9Sstevel@tonic-gate 		vnode_t *cvp, *svp;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 		cvp = ctfs_create_cdirnode(gfs_file_parent(vp), ct);
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 		gfs_file_set_index(cvp, -1);
777c478bd9Sstevel@tonic-gate 
78*ab04eb8eStimh 		VERIFY(gfs_dir_lookup(cvp, "status", &svp,
79*ab04eb8eStimh 		    cr, 0, NULL, NULL) == 0);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 		return (svp);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	return (NULL);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * ctfs_latest_access - VOP_ACCESS entry point
917c478bd9Sstevel@tonic-gate  *
927c478bd9Sstevel@tonic-gate  * Fails if there isn't a latest contract.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate /* ARGSUSED */
957c478bd9Sstevel@tonic-gate static int
ctfs_latest_access(vnode_t * vp,int mode,int flags,cred_t * cr,caller_context_t * ct)96da6c28aaSamw ctfs_latest_access(
97da6c28aaSamw 	vnode_t *vp,
98da6c28aaSamw 	int mode,
99da6c28aaSamw 	int flags,
100da6c28aaSamw 	cred_t *cr,
101da6c28aaSamw 	caller_context_t *ct)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	vnode_t *nvp;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if (mode & (VEXEC | VWRITE))
1067c478bd9Sstevel@tonic-gate 		return (EACCES);
1077c478bd9Sstevel@tonic-gate 
108da6c28aaSamw 	if (nvp = ctfs_latest_nested_open(vp, cr)) {
1097c478bd9Sstevel@tonic-gate 		VN_RELE(nvp);
1107c478bd9Sstevel@tonic-gate 		return (0);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	return (ESRCH);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * ctfs_latest_open - VOP_OPEN entry point
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * After checking the mode bits, opens and returns the status file for
1207c478bd9Sstevel@tonic-gate  * the LWP's latest contract.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static int
ctfs_latest_open(vnode_t ** vpp,int flag,cred_t * cr,caller_context_t * ct)123da6c28aaSamw ctfs_latest_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	vnode_t *nvp;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (flag != (FREAD | FOFFMAX))
1287c478bd9Sstevel@tonic-gate 		return (EINVAL);
1297c478bd9Sstevel@tonic-gate 
130da6c28aaSamw 	if (nvp = ctfs_latest_nested_open(*vpp, cr)) {
1317c478bd9Sstevel@tonic-gate 		VN_RELE(*vpp);
1327c478bd9Sstevel@tonic-gate 		*vpp = nvp;
133da6c28aaSamw 		return (VOP_OPEN(vpp, flag, cr, ct));
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	return (ESRCH);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * ctfs_latest_getattr - the VOP_GETATTR entry point
1417c478bd9Sstevel@tonic-gate  *
1427c478bd9Sstevel@tonic-gate  * Fetches and calls VOP_GETATTR on the status file for the LWP's
1437c478bd9Sstevel@tonic-gate  * latest contract.  Otherwise it fakes up something bland.
1447c478bd9Sstevel@tonic-gate  */
1457c478bd9Sstevel@tonic-gate static int
ctfs_latest_getattr(vnode_t * vp,vattr_t * vap,int flags,cred_t * cr,caller_context_t * ct)146da6c28aaSamw ctfs_latest_getattr(
147da6c28aaSamw 	vnode_t *vp,
148da6c28aaSamw 	vattr_t *vap,
149da6c28aaSamw 	int flags,
150da6c28aaSamw 	cred_t *cr,
151da6c28aaSamw 	caller_context_t *ct)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	vnode_t *nvp;
1547c478bd9Sstevel@tonic-gate 
155da6c28aaSamw 	if (nvp = ctfs_latest_nested_open(vp, cr)) {
156da6c28aaSamw 		int res = VOP_GETATTR(nvp, vap, flags, cr, ct);
1577c478bd9Sstevel@tonic-gate 		VN_RELE(nvp);
1587c478bd9Sstevel@tonic-gate 		return (res);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	vap->va_type = VREG;
1627c478bd9Sstevel@tonic-gate 	vap->va_mode = 0444;
1637c478bd9Sstevel@tonic-gate 	vap->va_nlink = 1;
1647c478bd9Sstevel@tonic-gate 	vap->va_size = 0;
1657c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
1667c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = 0;
1677c478bd9Sstevel@tonic-gate 	vap->va_atime = vap->va_mtime = vap->va_ctime;
1687c478bd9Sstevel@tonic-gate 	ctfs_common_getattr(vp, vap);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	return (0);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_latest[] = {
174aa59c4cbSrsb 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_latest_open } },
175aa59c4cbSrsb 	{ VOPNAME_CLOSE,	{ .error = fs_inval } },
176aa59c4cbSrsb 	{ VOPNAME_IOCTL,	{ .error = fs_inval } },
177aa59c4cbSrsb 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_latest_getattr } },
178aa59c4cbSrsb 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_latest_access } },
179aa59c4cbSrsb 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
180aa59c4cbSrsb 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
181aa59c4cbSrsb 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
1827c478bd9Sstevel@tonic-gate 	{ NULL, NULL }
1837c478bd9Sstevel@tonic-gate };
184