xref: /titanic_44/usr/src/uts/common/fs/ctfs/ctfs_latest.c (revision 2a9459bdd821c1cf59590a7a9069ac9c591e8a6b)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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, cr) == 0);
81 
82 		VN_RELE(cvp);
83 
84 		return (svp);
85 	}
86 
87 	return (NULL);
88 }
89 
90 /*
91  * ctfs_latest_access - VOP_ACCESS entry point
92  *
93  * Fails if there isn't a latest contract.
94  */
95 /* ARGSUSED */
96 static int
97 ctfs_latest_access(
98 	vnode_t *vp,
99 	int mode,
100 	int flags,
101 	cred_t *cr,
102 	caller_context_t *ct)
103 {
104 	vnode_t *nvp;
105 
106 	if (mode & (VEXEC | VWRITE))
107 		return (EACCES);
108 
109 	if (nvp = ctfs_latest_nested_open(vp, cr)) {
110 		VN_RELE(nvp);
111 		return (0);
112 	}
113 
114 	return (ESRCH);
115 }
116 
117 /*
118  * ctfs_latest_open - VOP_OPEN entry point
119  *
120  * After checking the mode bits, opens and returns the status file for
121  * the LWP's latest contract.
122  */
123 static int
124 ctfs_latest_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
125 {
126 	vnode_t *nvp;
127 
128 	if (flag != (FREAD | FOFFMAX))
129 		return (EINVAL);
130 
131 	if (nvp = ctfs_latest_nested_open(*vpp, cr)) {
132 		VN_RELE(*vpp);
133 		*vpp = nvp;
134 		return (VOP_OPEN(vpp, flag, cr, ct));
135 	}
136 
137 	return (ESRCH);
138 }
139 
140 /*
141  * ctfs_latest_getattr - the VOP_GETATTR entry point
142  *
143  * Fetches and calls VOP_GETATTR on the status file for the LWP's
144  * latest contract.  Otherwise it fakes up something bland.
145  */
146 static int
147 ctfs_latest_getattr(
148 	vnode_t *vp,
149 	vattr_t *vap,
150 	int flags,
151 	cred_t *cr,
152 	caller_context_t *ct)
153 {
154 	vnode_t *nvp;
155 
156 	if (nvp = ctfs_latest_nested_open(vp, cr)) {
157 		int res = VOP_GETATTR(nvp, vap, flags, cr, ct);
158 		VN_RELE(nvp);
159 		return (res);
160 	}
161 
162 	vap->va_type = VREG;
163 	vap->va_mode = 0444;
164 	vap->va_nlink = 1;
165 	vap->va_size = 0;
166 	vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
167 	vap->va_ctime.tv_nsec = 0;
168 	vap->va_atime = vap->va_mtime = vap->va_ctime;
169 	ctfs_common_getattr(vp, vap);
170 
171 	return (0);
172 }
173 
174 const fs_operation_def_t ctfs_tops_latest[] = {
175 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_latest_open } },
176 	{ VOPNAME_CLOSE,	{ .error = fs_inval } },
177 	{ VOPNAME_IOCTL,	{ .error = fs_inval } },
178 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_latest_getattr } },
179 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_latest_access } },
180 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
181 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
182 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
183 	{ NULL, NULL }
184 };
185