xref: /titanic_44/usr/src/uts/common/fs/ctfs/ctfs_ctl.c (revision fdd1ecae0dfe07e6aa8ee90687e2e91c876dc189)
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>/<ctid>/ctl vnode.
48  * CTFS routines for the /system/contract/<type>/<ctid>/status vnode.
49  */
50 
51 /*
52  * ctfs_create_ctlnode
53  *
54  * If necessary, creates a ctlnode for a ctl file and inserts it into
55  * the specified cdirnode's gfs_dir_t.  Returns either the existing
56  * vnode or the new one.
57  */
58 vnode_t *
59 ctfs_create_ctlnode(vnode_t *pvp)
60 {
61 	ctfs_ctlnode_t *ctlnode;
62 	ctfs_cdirnode_t *cdirnode = pvp->v_data;
63 	vnode_t *vp;
64 
65 	vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_ctl);
66 	ctlnode = vp->v_data;
67 	/*
68 	 * We transitively have a hold on the contract through our
69 	 * parent directory.
70 	 */
71 	ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract;
72 
73 	return (vp);
74 }
75 
76 /*
77  * ctfs_ctl_access - VOP_ACCESS entry point
78  *
79  * You only get to access ctl files for contracts you own or were
80  * abandoned and inherited by your containing process contract.
81  */
82 /* ARGSUSED */
83 static int
84 ctfs_ctl_access(vnode_t *vp, int mode, int flags, cred_t *cr)
85 {
86 	ctfs_ctlnode_t *ctlnode = vp->v_data;
87 	contract_t *ct = ctlnode->ctfs_ctl_contract;
88 
89 	if (mode & (VEXEC | VREAD))
90 		return (EACCES);
91 
92 	mutex_enter(&ct->ct_lock);
93 	if ((curproc == ct->ct_owner) ||
94 	    (ct->ct_owner == NULL && ct->ct_regent != NULL &&
95 	    ct->ct_regent->ct_data == curproc->p_ct_process)) {
96 		mutex_exit(&ct->ct_lock);
97 		return (0);
98 	}
99 
100 	mutex_exit(&ct->ct_lock);
101 	return (EACCES);
102 }
103 
104 /*
105  * ctfs_ctl_open - VOP_OPEN entry point
106  *
107  * Just checks to make sure the mode bits are set, and that the
108  * constraints imposed by ctfs_ctl_access are met.
109  */
110 static int
111 ctfs_ctl_open(vnode_t **vpp, int flag, cred_t *cr)
112 {
113 	if (flag != (FWRITE | FOFFMAX))
114 		return (EINVAL);
115 
116 	return (ctfs_ctl_access(*vpp, VWRITE, 0, cr));
117 }
118 
119 /*
120  * ctfs_ctl_common_getattr
121  * Implements fucntionality common to ctl and status ctfs VOP_GETATTR
122  * entry points. It assumes vp->v_data is set
123  */
124 static int
125 ctfs_ctl_common_getattr(vnode_t *vp, vattr_t *vap)
126 {
127 	ctfs_ctlnode_t *ctlnode = vp->v_data;
128 
129 	vap->va_type = VREG;
130 	vap->va_nlink = 1;
131 	vap->va_size = 0;
132 	vap->va_ctime = ctlnode->ctfs_ctl_contract->ct_ctime;
133 	mutex_enter(&ctlnode->ctfs_ctl_contract->ct_events.ctq_lock);
134 	vap->va_atime = vap->va_mtime =
135 	    ctlnode->ctfs_ctl_contract->ct_events.ctq_atime;
136 	mutex_exit(&ctlnode->ctfs_ctl_contract->ct_events.ctq_lock);
137 	ctfs_common_getattr(vp, vap);
138 
139 	return (0);
140 }
141 
142 /*
143  * ctfs_ctl_getattr - VOP_GETATTR entry point
144  */
145 /* ARGSUSED */
146 static int
147 ctfs_ctl_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
148 {
149 	vap->va_mode = 0222;
150 
151 	return (ctfs_ctl_common_getattr(vp, vap));
152 }
153 
154 /*
155  * ctfs_stat_getattr - VOP_GETATTR entry point
156  */
157 /* ARGSUSED */
158 static int
159 ctfs_stat_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
160 {
161 	vap->va_mode = 0444;
162 
163 	return (ctfs_ctl_common_getattr(vp, vap));
164 }
165 
166 /*
167  * ctfs_ctl_ioctl - VOP_IOCTL entry point
168  *
169  * All the ct_ctl_*(3contract) interfaces point here.
170  */
171 /* ARGSUSED */
172 static int
173 ctfs_ctl_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr,
174     int *rvalp)
175 {
176 	ctfs_ctlnode_t	*ctlnode = vp->v_data;
177 	contract_t	*ct = ctlnode->ctfs_ctl_contract;
178 	int		error = 0;
179 	uint64_t	event;
180 
181 	switch (cmd) {
182 	case CT_CABANDON:
183 		error = contract_abandon(ct, curproc, 1);
184 		break;
185 
186 	case CT_CACK:
187 		if (copyin((void *)arg, &event, sizeof (uint64_t)))
188 			return (EFAULT);
189 		error = contract_ack(ct, event);
190 		break;
191 
192 	case CT_CNEWCT:
193 		break;
194 
195 	case CT_CQREQ:
196 		break;
197 
198 	case CT_CADOPT:
199 		error = contract_adopt(ct, curproc);
200 		break;
201 
202 	default:
203 		return (EINVAL);
204 	}
205 
206 	return (error);
207 }
208 
209 const fs_operation_def_t ctfs_tops_ctl[] = {
210 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_ctl_open } },
211 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
212 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_ctl_ioctl } },
213 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_ctl_getattr } },
214 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_ctl_access } },
215 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
216 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
217 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
218 	{ NULL, NULL }
219 };
220 
221 /*
222  * ctfs_create_statnode
223  *
224  * If necessary, creates a ctlnode for a status file and inserts it
225  * into the specified cdirnode's gfs_dir_t.  Returns either the
226  * existing vnode or the new one.
227  */
228 vnode_t *
229 ctfs_create_statnode(vnode_t *pvp)
230 {
231 	vnode_t *vp;
232 	ctfs_cdirnode_t *cdirnode = pvp->v_data;
233 	ctfs_ctlnode_t *ctlnode;
234 
235 	vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_stat);
236 	ctlnode = vp->v_data;
237 	/*
238 	 * We transitively have a hold on the contract through our
239 	 * parent directory.
240 	 */
241 	ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract;
242 
243 	return (vp);
244 }
245 
246 /*
247  * ctfs_stat_ioctl - VOP_IOCTL entry point
248  *
249  * The kernel half of ct_status_read(3contract).
250  */
251 /* ARGSUSED */
252 static int
253 ctfs_stat_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr,
254     int *rvalp)
255 {
256 	ctfs_ctlnode_t	*statnode = vp->v_data;
257 	contract_t	*ct = statnode->ctfs_ctl_contract;
258 	ct_type_t	*type = ct->ct_type;
259 	STRUCT_DECL(ct_status, st);
260 	nvlist_t	*foo;
261 	char		*bufp = NULL;
262 	size_t		len;
263 	model_t		mdl = get_udatamodel();
264 	uint_t		detail;
265 
266 	STRUCT_INIT(st, mdl);
267 
268 	if (cmd != CT_SSTATUS)
269 		return (EINVAL);
270 
271 	if (copyin((void *)arg, STRUCT_BUF(st), STRUCT_SIZE(st)))
272 		return (EFAULT);
273 	detail = STRUCT_FGET(st, ctst_detail);
274 	if (detail == CTD_COMMON) {
275 		mutex_enter(&ct->ct_lock);
276 		contract_status_common(ct, VTOZONE(vp), STRUCT_BUF(st), mdl);
277 		mutex_exit(&ct->ct_lock);
278 	} else if (detail <= CTD_ALL) {
279 		VERIFY(nvlist_alloc(&foo, NV_UNIQUE_NAME, KM_SLEEP) == 0);
280 		type->ct_type_ops->contop_status(ct, VTOZONE(vp), detail, foo,
281 		    STRUCT_BUF(st), mdl);
282 		VERIFY(nvlist_pack(foo, &bufp, &len, NV_ENCODE_NATIVE,
283 		    KM_SLEEP) == 0);
284 		nvlist_free(foo);
285 
286 		if ((len <= STRUCT_FGET(st, ctst_nbytes)) &&
287 		    (copyout(bufp, STRUCT_FGETP(st, ctst_buffer), len) == -1)) {
288 			kmem_free(bufp, len);
289 			return (EFAULT);
290 		}
291 		kmem_free(bufp, len);
292 		STRUCT_FSET(st, ctst_nbytes, len);
293 	} else {
294 		return (EINVAL);
295 	}
296 	if (copyout(STRUCT_BUF(st), (void *)arg, STRUCT_SIZE(st)))
297 		return (EFAULT);
298 
299 	return (0);
300 }
301 
302 const fs_operation_def_t ctfs_tops_stat[] = {
303 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_open } },
304 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
305 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_stat_ioctl } },
306 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_stat_getattr } },
307 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_readonly } },
308 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
309 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
310 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
311 	{ NULL, NULL }
312 };
313