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