xref: /illumos-gate/usr/src/uts/common/fs/ctfs/ctfs_latest.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/gfs.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
39*7c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/contract.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/ctfs_impl.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/file.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * CTFS routines for the /system/contract/<type>/latest vnode.
48*7c478bd9Sstevel@tonic-gate  */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate  * ctfs_create_latenode
52*7c478bd9Sstevel@tonic-gate  */
53*7c478bd9Sstevel@tonic-gate vnode_t *
54*7c478bd9Sstevel@tonic-gate ctfs_create_latenode(vnode_t *pvp)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	return (gfs_file_create(sizeof (ctfs_latenode_t), pvp,
57*7c478bd9Sstevel@tonic-gate 	    ctfs_ops_latest));
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate /*
61*7c478bd9Sstevel@tonic-gate  * ctfs_latest_nested_open
62*7c478bd9Sstevel@tonic-gate  *
63*7c478bd9Sstevel@tonic-gate  * The latest node is just a doorway to the status file; this function
64*7c478bd9Sstevel@tonic-gate  * is used by ctfs_latest_access, ctfs_latest_open, and
65*7c478bd9Sstevel@tonic-gate  * ctfs_latest_getattr to obtain that file.
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate static vnode_t *
68*7c478bd9Sstevel@tonic-gate ctfs_latest_nested_open(vnode_t *vp)
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate 	contract_t *ct = ttolwp(curthread)->lwp_ct_latest[
71*7c478bd9Sstevel@tonic-gate 	    gfs_file_index(gfs_file_parent(vp))];
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	if (ct) {
74*7c478bd9Sstevel@tonic-gate 		vnode_t *cvp, *svp;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 		cvp = ctfs_create_cdirnode(gfs_file_parent(vp), ct);
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 		gfs_file_set_index(cvp, -1);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 		VERIFY(gfs_dir_lookup(cvp, "status", &svp) == 0);
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 		return (svp);
85*7c478bd9Sstevel@tonic-gate 	}
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	return (NULL);
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate /*
91*7c478bd9Sstevel@tonic-gate  * ctfs_latest_access - VOP_ACCESS entry point
92*7c478bd9Sstevel@tonic-gate  *
93*7c478bd9Sstevel@tonic-gate  * Fails if there isn't a latest contract.
94*7c478bd9Sstevel@tonic-gate  */
95*7c478bd9Sstevel@tonic-gate /* ARGSUSED */
96*7c478bd9Sstevel@tonic-gate static int
97*7c478bd9Sstevel@tonic-gate ctfs_latest_access(vnode_t *vp, int mode, int flags, cred_t *cr)
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate 	vnode_t *nvp;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	if (mode & (VEXEC | VWRITE))
102*7c478bd9Sstevel@tonic-gate 		return (EACCES);
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (nvp = ctfs_latest_nested_open(vp)) {
105*7c478bd9Sstevel@tonic-gate 		VN_RELE(nvp);
106*7c478bd9Sstevel@tonic-gate 		return (0);
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	return (ESRCH);
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate /*
113*7c478bd9Sstevel@tonic-gate  * ctfs_latest_open - VOP_OPEN entry point
114*7c478bd9Sstevel@tonic-gate  *
115*7c478bd9Sstevel@tonic-gate  * After checking the mode bits, opens and returns the status file for
116*7c478bd9Sstevel@tonic-gate  * the LWP's latest contract.
117*7c478bd9Sstevel@tonic-gate  */
118*7c478bd9Sstevel@tonic-gate static int
119*7c478bd9Sstevel@tonic-gate ctfs_latest_open(vnode_t **vpp, int flag, cred_t *cr)
120*7c478bd9Sstevel@tonic-gate {
121*7c478bd9Sstevel@tonic-gate 	vnode_t *nvp;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	if (flag != (FREAD | FOFFMAX))
124*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	if (nvp = ctfs_latest_nested_open(*vpp)) {
127*7c478bd9Sstevel@tonic-gate 		VN_RELE(*vpp);
128*7c478bd9Sstevel@tonic-gate 		*vpp = nvp;
129*7c478bd9Sstevel@tonic-gate 		return (VOP_OPEN(vpp, flag, cr));
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	return (ESRCH);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate /*
136*7c478bd9Sstevel@tonic-gate  * ctfs_latest_getattr - the VOP_GETATTR entry point
137*7c478bd9Sstevel@tonic-gate  *
138*7c478bd9Sstevel@tonic-gate  * Fetches and calls VOP_GETATTR on the status file for the LWP's
139*7c478bd9Sstevel@tonic-gate  * latest contract.  Otherwise it fakes up something bland.
140*7c478bd9Sstevel@tonic-gate  */
141*7c478bd9Sstevel@tonic-gate static int
142*7c478bd9Sstevel@tonic-gate ctfs_latest_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
143*7c478bd9Sstevel@tonic-gate {
144*7c478bd9Sstevel@tonic-gate 	vnode_t *nvp;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	if (nvp = ctfs_latest_nested_open(vp)) {
147*7c478bd9Sstevel@tonic-gate 		int res = VOP_GETATTR(nvp, vap, flags, cr);
148*7c478bd9Sstevel@tonic-gate 		VN_RELE(nvp);
149*7c478bd9Sstevel@tonic-gate 		return (res);
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	vap->va_type = VREG;
153*7c478bd9Sstevel@tonic-gate 	vap->va_mode = 0444;
154*7c478bd9Sstevel@tonic-gate 	vap->va_nlink = 1;
155*7c478bd9Sstevel@tonic-gate 	vap->va_size = 0;
156*7c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
157*7c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = 0;
158*7c478bd9Sstevel@tonic-gate 	vap->va_atime = vap->va_mtime = vap->va_ctime;
159*7c478bd9Sstevel@tonic-gate 	ctfs_common_getattr(vp, vap);
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	return (0);
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate const fs_operation_def_t ctfs_tops_latest[] = {
165*7c478bd9Sstevel@tonic-gate 	{ VOPNAME_OPEN,		ctfs_latest_open },
166*7c478bd9Sstevel@tonic-gate 	{ VOPNAME_CLOSE,	fs_inval },
167*7c478bd9Sstevel@tonic-gate 	{ VOPNAME_IOCTL,	fs_inval },
168*7c478bd9Sstevel@tonic-gate 	{ VOPNAME_GETATTR,	ctfs_latest_getattr },
169*7c478bd9Sstevel@tonic-gate 	{ VOPNAME_ACCESS,	ctfs_latest_access },
170*7c478bd9Sstevel@tonic-gate 	{ VOPNAME_READDIR,	fs_notdir },
171*7c478bd9Sstevel@tonic-gate 	{ VOPNAME_LOOKUP,	fs_notdir },
172*7c478bd9Sstevel@tonic-gate 	{ VOPNAME_INACTIVE,	(fs_generic_func_p) gfs_vop_inactive },
173*7c478bd9Sstevel@tonic-gate 	{ NULL, NULL }
174*7c478bd9Sstevel@tonic-gate };
175