xref: /titanic_41/usr/src/uts/common/fs/ctfs/ctfs_ctl.c (revision 8d483882aa3390058094b043f3d62187b5d1de03)
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 	int		ack;
181 
182 	switch (cmd) {
183 	case CT_CABANDON:
184 		error = contract_abandon(ct, curproc, 1);
185 		break;
186 
187 	case CT_CACK:
188 	case CT_CNACK:
189 		if (copyin((void *)arg, &event, sizeof (uint64_t)))
190 			return (EFAULT);
191 		ack = (cmd == CT_CACK) ? CT_ACK : CT_NACK;
192 		error = contract_ack(ct, event, ack);
193 		break;
194 
195 	case CT_CNEWCT:
196 		error = contract_newct(ct);
197 		break;
198 
199 	case CT_CQREQ:
200 		if (copyin((void *)arg, &event, sizeof (uint64_t)))
201 			return (EFAULT);
202 		error = contract_qack(ct, event);
203 		break;
204 
205 	case CT_CADOPT:
206 		error = contract_adopt(ct, curproc);
207 		break;
208 
209 	default:
210 		return (EINVAL);
211 	}
212 
213 	return (error);
214 }
215 
216 const fs_operation_def_t ctfs_tops_ctl[] = {
217 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_ctl_open } },
218 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
219 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_ctl_ioctl } },
220 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_ctl_getattr } },
221 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_ctl_access } },
222 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
223 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
224 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
225 	{ NULL, NULL }
226 };
227 
228 /*
229  * ctfs_create_statnode
230  *
231  * If necessary, creates a ctlnode for a status file and inserts it
232  * into the specified cdirnode's gfs_dir_t.  Returns either the
233  * existing vnode or the new one.
234  */
235 vnode_t *
236 ctfs_create_statnode(vnode_t *pvp)
237 {
238 	vnode_t *vp;
239 	ctfs_cdirnode_t *cdirnode = pvp->v_data;
240 	ctfs_ctlnode_t *ctlnode;
241 
242 	vp = gfs_file_create(sizeof (ctfs_ctlnode_t), pvp, ctfs_ops_stat);
243 	ctlnode = vp->v_data;
244 	/*
245 	 * We transitively have a hold on the contract through our
246 	 * parent directory.
247 	 */
248 	ctlnode->ctfs_ctl_contract = cdirnode->ctfs_cn_contract;
249 
250 	return (vp);
251 }
252 
253 /*
254  * ctfs_stat_ioctl - VOP_IOCTL entry point
255  *
256  * The kernel half of ct_status_read(3contract).
257  */
258 /* ARGSUSED */
259 static int
260 ctfs_stat_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr,
261     int *rvalp)
262 {
263 	ctfs_ctlnode_t	*statnode = vp->v_data;
264 	contract_t	*ct = statnode->ctfs_ctl_contract;
265 	ct_type_t	*type = ct->ct_type;
266 	STRUCT_DECL(ct_status, st);
267 	nvlist_t	*foo;
268 	char		*bufp = NULL;
269 	size_t		len;
270 	model_t		mdl = get_udatamodel();
271 	uint_t		detail;
272 
273 	STRUCT_INIT(st, mdl);
274 
275 	if (cmd != CT_SSTATUS)
276 		return (EINVAL);
277 
278 	if (copyin((void *)arg, STRUCT_BUF(st), STRUCT_SIZE(st)))
279 		return (EFAULT);
280 	detail = STRUCT_FGET(st, ctst_detail);
281 	if (detail == CTD_COMMON) {
282 		mutex_enter(&ct->ct_lock);
283 		contract_status_common(ct, VTOZONE(vp), STRUCT_BUF(st), mdl);
284 		mutex_exit(&ct->ct_lock);
285 	} else if (detail <= CTD_ALL) {
286 		VERIFY(nvlist_alloc(&foo, NV_UNIQUE_NAME, KM_SLEEP) == 0);
287 		type->ct_type_ops->contop_status(ct, VTOZONE(vp), detail, foo,
288 		    STRUCT_BUF(st), mdl);
289 		VERIFY(nvlist_pack(foo, &bufp, &len, NV_ENCODE_NATIVE,
290 		    KM_SLEEP) == 0);
291 		nvlist_free(foo);
292 
293 		if ((len <= STRUCT_FGET(st, ctst_nbytes)) &&
294 		    (copyout(bufp, STRUCT_FGETP(st, ctst_buffer), len) == -1)) {
295 			kmem_free(bufp, len);
296 			return (EFAULT);
297 		}
298 		kmem_free(bufp, len);
299 		STRUCT_FSET(st, ctst_nbytes, len);
300 	} else {
301 		return (EINVAL);
302 	}
303 	if (copyout(STRUCT_BUF(st), (void *)arg, STRUCT_SIZE(st)))
304 		return (EFAULT);
305 
306 	return (0);
307 }
308 
309 const fs_operation_def_t ctfs_tops_stat[] = {
310 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_open } },
311 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
312 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_stat_ioctl } },
313 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_stat_getattr } },
314 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_readonly } },
315 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
316 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
317 	{ VOPNAME_INACTIVE,	{ .vop_inactive = gfs_vop_inactive } },
318 	{ NULL, NULL }
319 };
320