xref: /titanic_44/usr/src/uts/common/fs/ctfs/ctfs_tmpl.c (revision 843e19887f64dde75055cf8842fc4db2171eff45)
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>/template vnode.
48  */
49 
50 /*
51  * ctfs_create_tmplnode
52  *
53  * Creates a new template and tdirnode, and returns the tdirnode.
54  */
55 vnode_t *
56 ctfs_create_tmplnode(vnode_t *pvp)
57 {
58 	vnode_t *vp;
59 	ctfs_tmplnode_t *tmplnode;
60 
61 	ASSERT(gfs_file_index(pvp) < ct_ntypes);
62 
63 	vp = gfs_file_create(sizeof (ctfs_tmplnode_t), pvp, ctfs_ops_tmpl);
64 	tmplnode = vp->v_data;
65 	tmplnode->ctfs_tmn_tmpl =
66 	    ct_types[gfs_file_index(pvp)]->ct_type_default();
67 
68 	return (vp);
69 }
70 
71 /*
72  * ctfs_tmpl_open - VOP_OPEN entry point
73  *
74  * Just ensures the right mode bits are set.
75  */
76 /* ARGSUSED */
77 static int
78 ctfs_tmpl_open(vnode_t **vpp, int flag, cred_t *cr)
79 {
80 	if (flag != (FREAD | FWRITE | FOFFMAX))
81 		return (EINVAL);
82 
83 	return (0);
84 }
85 
86 /*
87  * ctfs_tmpl_getattr - VOP_GETATTR entry point
88  */
89 /* ARGSUSED */
90 static int
91 ctfs_tmpl_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
92 {
93 	vap->va_type = VREG;
94 	vap->va_mode = 0666;
95 	vap->va_nlink = 1;
96 	vap->va_size = 0;
97 	vap->va_ctime.tv_sec = vp->v_vfsp->vfs_mtime;
98 	vap->va_ctime.tv_nsec = 0;
99 	vap->va_atime = vap->va_mtime = vap->va_ctime;
100 	ctfs_common_getattr(vp, vap);
101 
102 	return (0);
103 }
104 
105 /*
106  * ctfs_tmpl_ioctl - VOP_IOCTL entry point
107  *
108  * All the ct_tmpl_*(3contract) interfaces point here.
109  */
110 /* ARGSUSED */
111 static int
112 ctfs_tmpl_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr,
113     int *rvalp)
114 {
115 	ctfs_tmplnode_t	*tmplnode = vp->v_data;
116 	ct_param_t param;
117 	ctid_t ctid;
118 	int error;
119 
120 	switch (cmd) {
121 	case CT_TACTIVATE:
122 		ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
123 		ctmpl_activate(tmplnode->ctfs_tmn_tmpl);
124 		break;
125 	case CT_TCLEAR:
126 		ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
127 		ctmpl_clear(tmplnode->ctfs_tmn_tmpl);
128 		break;
129 	case CT_TCREATE:
130 		ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
131 		error = ctmpl_create(tmplnode->ctfs_tmn_tmpl, &ctid);
132 		if (error)
133 			return (error);
134 		*rvalp = ctid;
135 		break;
136 	case CT_TSET:
137 		if (copyin((void *)arg, &param, sizeof (ct_param_t)))
138 			return (EFAULT);
139 		return (ctmpl_set(tmplnode->ctfs_tmn_tmpl, &param, cr));
140 	case CT_TGET:
141 		if (copyin((void *)arg, &param, sizeof (ct_param_t)))
142 			return (EFAULT);
143 		error = ctmpl_get(tmplnode->ctfs_tmn_tmpl, &param);
144 		if (error)
145 			return (error);
146 		if (copyout(&param, (void *)arg, sizeof (ct_param_t)))
147 			return (EFAULT);
148 		break;
149 	default:
150 		return (EINVAL);
151 	}
152 
153 	return (0);
154 }
155 
156 /*
157  * ctfs_tmpl_inactive - VOP_INACTIVE entry point
158  */
159 /* ARGSUSED */
160 static void
161 ctfs_tmpl_inactive(vnode_t *vp, cred_t *cr)
162 {
163 	ctfs_tmplnode_t *tmplnode;
164 
165 	if ((tmplnode = gfs_file_inactive(vp)) != NULL) {
166 		ctmpl_free(tmplnode->ctfs_tmn_tmpl);
167 		kmem_free(tmplnode, sizeof (ctfs_tmplnode_t));
168 	}
169 }
170 
171 const fs_operation_def_t ctfs_tops_tmpl[] = {
172 	{ VOPNAME_OPEN,		{ .vop_open = ctfs_tmpl_open } },
173 	{ VOPNAME_CLOSE,	{ .vop_close = ctfs_close } },
174 	{ VOPNAME_IOCTL,	{ .vop_ioctl = ctfs_tmpl_ioctl } },
175 	{ VOPNAME_GETATTR,	{ .vop_getattr = ctfs_tmpl_getattr } },
176 	{ VOPNAME_ACCESS,	{ .vop_access = ctfs_access_readwrite } },
177 	{ VOPNAME_READDIR,	{ .error = fs_notdir } },
178 	{ VOPNAME_LOOKUP,	{ .error = fs_notdir } },
179 	{ VOPNAME_INACTIVE,	{ .vop_inactive = ctfs_tmpl_inactive } },
180 	{ NULL, NULL }
181 };
182