17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5aa59c4crsb * Common Development and Distribution License (the "License").
6aa59c4crsb * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22ab04eb8timh * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate#include <sys/types.h>
297c478bdstevel@tonic-gate#include <sys/param.h>
307c478bdstevel@tonic-gate#include <sys/time.h>
317c478bdstevel@tonic-gate#include <sys/cred.h>
327c478bdstevel@tonic-gate#include <sys/vfs.h>
33aa59c4crsb#include <sys/vfs_opreg.h>
347c478bdstevel@tonic-gate#include <sys/gfs.h>
357c478bdstevel@tonic-gate#include <sys/vnode.h>
367c478bdstevel@tonic-gate#include <sys/systm.h>
377c478bdstevel@tonic-gate#include <sys/errno.h>
387c478bdstevel@tonic-gate#include <sys/sysmacros.h>
397c478bdstevel@tonic-gate#include <fs/fs_subr.h>
407c478bdstevel@tonic-gate#include <sys/contract.h>
417c478bdstevel@tonic-gate#include <sys/contract_impl.h>
427c478bdstevel@tonic-gate#include <sys/ctfs.h>
437c478bdstevel@tonic-gate#include <sys/ctfs_impl.h>
447c478bdstevel@tonic-gate#include <sys/file.h>
457c478bdstevel@tonic-gate
467c478bdstevel@tonic-gate/*
477c478bdstevel@tonic-gate * CTFS routines for the /system/contract/<type>/latest vnode.
487c478bdstevel@tonic-gate */
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gate/*
517c478bdstevel@tonic-gate * ctfs_create_latenode
527c478bdstevel@tonic-gate */
537c478bdstevel@tonic-gatevnode_t *
547c478bdstevel@tonic-gatectfs_create_latenode(vnode_t *pvp)
557c478bdstevel@tonic-gate{
567c478bdstevel@tonic-gate	return (gfs_file_create(sizeof (ctfs_latenode_t), pvp,
577c478bdstevel@tonic-gate	    ctfs_ops_latest));
587c478bdstevel@tonic-gate}
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate/*
617c478bdstevel@tonic-gate * ctfs_latest_nested_open
627c478bdstevel@tonic-gate *
637c478bdstevel@tonic-gate * The latest node is just a doorway to the status file; this function
647c478bdstevel@tonic-gate * is used by ctfs_latest_access, ctfs_latest_open, and
657c478bdstevel@tonic-gate * ctfs_latest_getattr to obtain that file.
667c478bdstevel@tonic-gate */
677c478bdstevel@tonic-gatestatic vnode_t *
68da6c28aamwctfs_latest_nested_open(vnode_t *vp, cred_t *cr)
697c478bdstevel@tonic-gate{
707c478bdstevel@tonic-gate	contract_t *ct = ttolwp(curthread)->lwp_ct_latest[
717c478bdstevel@tonic-gate	    gfs_file_index(gfs_file_parent(vp))];
727c478bdstevel@tonic-gate
737c478bdstevel@tonic-gate	if (ct) {
747c478bdstevel@tonic-gate		vnode_t *cvp, *svp;
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gate		cvp = ctfs_create_cdirnode(gfs_file_parent(vp), ct);
777c478bdstevel@tonic-gate
787c478bdstevel@tonic-gate		gfs_file_set_index(cvp, -1);
797c478bdstevel@tonic-gate
80ab04eb8timh		VERIFY(gfs_dir_lookup(cvp, "status", &svp,
81ab04eb8timh		    cr, 0, NULL, NULL) == 0);
827c478bdstevel@tonic-gate
837c478bdstevel@tonic-gate		VN_RELE(cvp);
847c478bdstevel@tonic-gate
857c478bdstevel@tonic-gate		return (svp);
867c478bdstevel@tonic-gate	}
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gate	return (NULL);
897c478bdstevel@tonic-gate}
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate/*
927c478bdstevel@tonic-gate * ctfs_latest_access - VOP_ACCESS entry point
937c478bdstevel@tonic-gate *
947c478bdstevel@tonic-gate * Fails if there isn't a latest contract.
957c478bdstevel@tonic-gate */
967c478bdstevel@tonic-gate/* ARGSUSED */
977c478bdstevel@tonic-gatestatic int
98da6c28aamwctfs_latest_access(
99da6c28aamw	vnode_t *vp,
100da6c28aamw	int mode,
101da6c28aamw	int flags,
102da6c28aamw	cred_t *cr,
103da6c28aamw	caller_context_t *ct)
1047c478bdstevel@tonic-gate{
1057c478bdstevel@tonic-gate	vnode_t *nvp;
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate	if (mode & (VEXEC | VWRITE))
1087c478bdstevel@tonic-gate		return (EACCES);
1097c478bdstevel@tonic-gate
110da6c28aamw	if (nvp = ctfs_latest_nested_open(vp, cr)) {
1117c478bdstevel@tonic-gate		VN_RELE(nvp);
1127c478bdstevel@tonic-gate		return (0);
1137c478bdstevel@tonic-gate	}
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate	return (ESRCH);
1167c478bdstevel@tonic-gate}
1177c478bdstevel@tonic-gate
1187c478bdstevel@tonic-gate/*
1197c478bdstevel@tonic-gate * ctfs_latest_open - VOP_OPEN entry point
1207c478bdstevel@tonic-gate *
1217c478bdstevel@tonic-gate * After checking the mode bits, opens and returns the status file for
1227c478bdstevel@tonic-gate * the LWP's latest contract.
1237c478bdstevel@tonic-gate */
1247c478bdstevel@tonic-gatestatic int
125da6c28aamwctfs_latest_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
1267c478bdstevel@tonic-gate{
1277c478bdstevel@tonic-gate	vnode_t *nvp;
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gate	if (flag != (FREAD | FOFFMAX))
1307c478bdstevel@tonic-gate		return (EINVAL);
1317c478bdstevel@tonic-gate
132da6c28aamw	if (nvp = ctfs_latest_nested_open(*vpp, cr)) {
1337c478bdstevel@tonic-gate		VN_RELE(*vpp);
1347c478bdstevel@tonic-gate		*vpp = nvp;
135da6c28aamw		return (VOP_OPEN(vpp, flag, cr, ct));
1367c478bdstevel@tonic-gate	}
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gate	return (ESRCH);
1397c478bdstevel@tonic-gate}
1407c478bdstevel@tonic-gate
1417c478bdstevel@tonic-gate/*
1427c478bdstevel@tonic-gate * ctfs_latest_getattr - the VOP_GETATTR entry point
1437c478bdstevel@tonic-gate *
1447c478bdstevel@tonic-gate * Fetches and calls VOP_GETATTR on the status file for the LWP's
1457c478bdstevel@tonic-gate * latest contract.  Otherwise it fakes up something bland.
1467c478bdstevel@tonic-gate */
1477c478bdstevel@tonic-gatestatic int
148da6c28aamwctfs_latest_getattr(
149da6c28aamw	vnode_t *vp,
150da6c28aamw	vattr_t *vap,
151da6c28aamw	int flags,
152da6c28aamw	cred_t *cr,
153da6c28aamw	caller_context_t *ct)
1547c478bdstevel@tonic-gate{
1557c478bdstevel@tonic-gate	vnode_t *nvp;
1567c478bdstevel@tonic-gate
157da6c28aamw	if (nvp = ctfs_latest_nested_open(vp, cr)) {
158da6c28aamw		int res = VOP_GETATTR(nvp, vap, flags, cr, ct);
1597c478bdstevel@tonic-gate		VN_RELE(nvp);
1607c478bdstevel@tonic-gate		return (res);
1617c478bdstevel@tonic-gate	}
1627c478bdstevel@tonic-gate
1637c478bdstevel@tonic-gate	vap->va_type = VREG;
1647c478bdstevel@tonic-gate	vap->va_mode = 0444;
1657c478bdstevel@tonic-gate	vap->va_nlink = 1;
1667c478bdstevel@tonic-gate	vap->va_size = 0;
1677c478bdstevel@tonic-gate	vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
1687c478bdstevel@tonic-gate	vap->va_ctime.tv_nsec = 0;
1697c478bdstevel@tonic-gate	vap->va_atime = vap->va_mtime = vap->va_ctime;
1707c478bdstevel@tonic-gate	ctfs_common_getattr(vp, vap);
1717c478bdstevel@tonic-gate
1727c478bdstevel@tonic-gate	return (0);
1737c478bdstevel@tonic-gate}
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gateconst fs_operation_def_t ctfs_tops_latest[] = {
176aa59c4crsb	{ VOPNAME_OPEN,		{ .vop_open = ctfs_latest_open } },
177aa59c4crsb	{ VOPNAME_CLOSE,	{ .error = fs_inval } },
178aa59c4crsb	{ VOPNAME_IOCTL,	{ .error = fs_inval } },
179aa59c4crsb	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_latest_getattr } },
180aa59c4crsb	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_latest_access } },
181aa59c4crsb	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
182aa59c4crsb	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
183aa59c4crsb	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
1847c478bdstevel@tonic-gate	{ NULL, NULL }
1857c478bdstevel@tonic-gate};
186