xref: /illumos-gate/usr/src/uts/common/fs/ctfs/ctfs_tmpl.c (revision 7c478bd9)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 #include <sys/time.h>
32 #include <sys/cred.h>
33 #include <sys/vfs.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 	int error;
118 
119 	switch (cmd) {
120 	case CT_TACTIVATE:
121 		ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
122 		ctmpl_activate(tmplnode->ctfs_tmn_tmpl);
123 		break;
124 	case CT_TCLEAR:
125 		ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
126 		ctmpl_clear(tmplnode->ctfs_tmn_tmpl);
127 		break;
128 	case CT_TCREATE:
129 		ASSERT(tmplnode->ctfs_tmn_tmpl != NULL);
130 		return (ctmpl_create(tmplnode->ctfs_tmn_tmpl));
131 	case CT_TSET:
132 		if (copyin((void *)arg, &param, sizeof (ct_param_t)))
133 			return (EFAULT);
134 		return (ctmpl_set(tmplnode->ctfs_tmn_tmpl, &param, cr));
135 	case CT_TGET:
136 		if (copyin((void *)arg, &param, sizeof (ct_param_t)))
137 			return (EFAULT);
138 		error = ctmpl_get(tmplnode->ctfs_tmn_tmpl, &param);
139 		if (error)
140 			return (error);
141 		if (copyout(&param, (void *)arg, sizeof (ct_param_t)))
142 			return (EFAULT);
143 		break;
144 	default:
145 		return (EINVAL);
146 	}
147 
148 	return (0);
149 }
150 
151 /*
152  * ctfs_tmpl_inactive - VOP_INACTIVE entry point
153  */
154 /* ARGSUSED */
155 static void
156 ctfs_tmpl_inactive(vnode_t *vp, cred_t *cr)
157 {
158 	ctfs_tmplnode_t *tmplnode;
159 
160 	if ((tmplnode = gfs_file_inactive(vp)) != NULL) {
161 		ctmpl_free(tmplnode->ctfs_tmn_tmpl);
162 		kmem_free(tmplnode, sizeof (ctfs_tmplnode_t));
163 	}
164 }
165 
166 const fs_operation_def_t ctfs_tops_tmpl[] = {
167 	{ VOPNAME_OPEN,		ctfs_tmpl_open },
168 	{ VOPNAME_CLOSE,	ctfs_close },
169 	{ VOPNAME_IOCTL,	ctfs_tmpl_ioctl },
170 	{ VOPNAME_GETATTR,	ctfs_tmpl_getattr },
171 	{ VOPNAME_ACCESS,	ctfs_access_readwrite },
172 	{ VOPNAME_READDIR,	fs_notdir },
173 	{ VOPNAME_LOOKUP,	fs_notdir },
174 	{ VOPNAME_INACTIVE,	(fs_generic_func_p) ctfs_tmpl_inactive },
175 	{ NULL, NULL }
176 };
177