Deleted Added
full compact
geom_ctl.c (105504) geom_ctl.c (105551)
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
21 * permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
21 * permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: head/sys/geom/geom_ctl.c 105504 2002-10-20 08:42:18Z phk $
35 * $FreeBSD: head/sys/geom/geom_ctl.c 105551 2002-10-20 20:28:24Z phk $
36 */
37
38#include "opt_geom.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/sysctl.h>
44#include <sys/bio.h>
45#include <sys/conf.h>
46#include <sys/disk.h>
47#include <sys/malloc.h>
48#include <sys/sysctl.h>
49#include <sys/stdint.h>
50
51#include <sys/lock.h>
52#include <sys/mutex.h>
53#include <geom/geom.h>
54#include <geom/geom_int.h>
55
56static g_access_t g_ctl_access;
57static g_start_t g_ctl_start;
58static void g_ctl_init(void);
59static d_ioctl_t g_ctl_ioctl;
60
61struct g_class g_ctl_class = {
62 "GEOMCTL",
63 NULL,
64 NULL,
65 G_CLASS_INITIALIZER
66};
67
68DECLARE_GEOM_CLASS_INIT(g_ctl_class, g_ctl, g_ctl_init);
69
70/*
71 * We cannot do create our geom.ctl geom/provider in g_ctl_init() because
72 * the event thread has to finish adding our class first and that doesn't
73 * happen until later. We know however, that the events are processed in
74 * FIFO order, so scheduling g_ctl_init2() with g_call_me() is safe.
75 */
76
77static void
78g_ctl_init2(void *p __unused)
79{
80 struct g_geom *gp;
81 struct g_provider *pp;
82
83 g_topology_assert();
84 gp = g_new_geomf(&g_ctl_class, "geom.ctl");
85 gp->start = g_ctl_start;
86 gp->access = g_ctl_access;
87 pp = g_new_providerf(gp, "%s", gp->name);
36 */
37
38#include "opt_geom.h"
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/sysctl.h>
44#include <sys/bio.h>
45#include <sys/conf.h>
46#include <sys/disk.h>
47#include <sys/malloc.h>
48#include <sys/sysctl.h>
49#include <sys/stdint.h>
50
51#include <sys/lock.h>
52#include <sys/mutex.h>
53#include <geom/geom.h>
54#include <geom/geom_int.h>
55
56static g_access_t g_ctl_access;
57static g_start_t g_ctl_start;
58static void g_ctl_init(void);
59static d_ioctl_t g_ctl_ioctl;
60
61struct g_class g_ctl_class = {
62 "GEOMCTL",
63 NULL,
64 NULL,
65 G_CLASS_INITIALIZER
66};
67
68DECLARE_GEOM_CLASS_INIT(g_ctl_class, g_ctl, g_ctl_init);
69
70/*
71 * We cannot do create our geom.ctl geom/provider in g_ctl_init() because
72 * the event thread has to finish adding our class first and that doesn't
73 * happen until later. We know however, that the events are processed in
74 * FIFO order, so scheduling g_ctl_init2() with g_call_me() is safe.
75 */
76
77static void
78g_ctl_init2(void *p __unused)
79{
80 struct g_geom *gp;
81 struct g_provider *pp;
82
83 g_topology_assert();
84 gp = g_new_geomf(&g_ctl_class, "geom.ctl");
85 gp->start = g_ctl_start;
86 gp->access = g_ctl_access;
87 pp = g_new_providerf(gp, "%s", gp->name);
88 pp->sectorsize = 512;
88 g_error_provider(pp, 0);
89}
90
91static void
92g_ctl_init(void)
93{
94 mtx_unlock(&Giant);
95 g_add_class(&g_ctl_class);
96 g_call_me(g_ctl_init2, NULL);
97 mtx_lock(&Giant);
98}
99
100/*
101 * We allow any kind of access. Access control is handled at the devfs
102 * level.
103 */
104
105static int
106g_ctl_access(struct g_provider *pp, int r, int w, int e)
107{
108 int error;
109
110 g_trace(G_T_ACCESS, "g_ctl_access(%s, %d, %d, %d)",
111 pp->name, r, w, e);
112
113 g_topology_assert();
114 error = 0;
115 return (error);
116}
117
118static void
119g_ctl_start(struct bio *bp)
120{
121 struct g_ioctl *gio;
122 int error;
123
124 switch(bp->bio_cmd) {
125 case BIO_DELETE:
126 case BIO_READ:
127 case BIO_WRITE:
128 error = EOPNOTSUPP;
129 break;
130 case BIO_GETATTR:
131 case BIO_SETATTR:
132 if (strcmp(bp->bio_attribute, "GEOM::ioctl") ||
133 bp->bio_length != sizeof *gio) {
134 error = EOPNOTSUPP;
135 break;
136 }
137 gio = (struct g_ioctl *)bp->bio_data;
138 gio->func = g_ctl_ioctl;
139 error = EDIRIOCTL;
140 break;
141 default:
142 error = EOPNOTSUPP;
143 break;
144 }
145 g_io_deliver(bp, error);
146 return;
147}
148
149/*
150 * All the stuff above is really just needed to get to the stuff below
151 */
152
153static int
154g_ctl_ioctl_getconf(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
155{
156 struct geomgetconf *gcp;
157 struct sbuf *sb;
158 int error;
159 u_int l;
160
161 gcp = (struct geomgetconf *)data;
162 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
163 sbuf_clear(sb);
164 g_confxml(sb);
165 l = sbuf_len(sb) + 1;
166 if (l > gcp->len)
167 error = ENOMEM;
168 else
169 error = copyout(sbuf_data(sb), gcp->ptr, l);
170 sbuf_delete(sb);
171 return(error);
172}
173
174static int
175g_ctl_ioctl_configgeom(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
176{
177 struct geomconfiggeom *gcp;
178 struct g_createargs ga;
179 int error;
180
181 error = 0;
182 bzero(&ga, sizeof ga);
183 gcp = (struct geomconfiggeom *)data;
184 ga.class = g_idclass(&gcp->class);
185 if (ga.class == NULL)
186 return (EINVAL);
187 if (ga.class->create_geom == NULL)
188 return (EOPNOTSUPP);
189 ga.geom = g_idgeom(&gcp->geom);
190 ga.provider = g_idprovider(&gcp->provider);
191 ga.len = gcp->len;
192 if (gcp->len > 64 * 1024)
193 return (EINVAL);
194 else if (gcp->len == 0) {
195 ga.ptr = NULL;
196 } else {
197 ga.ptr = g_malloc(gcp->len, M_WAITOK);
198 copyin(gcp->ptr, ga.ptr, gcp->len);
199 }
200 ga.flag = gcp->flag;
201 error = ga.class->create_geom(&ga);
202 gcp->class.u.id = (uintptr_t)ga.class;
203 gcp->class.len = 0;
204 gcp->geom.u.id = (uintptr_t)ga.geom;
205 gcp->geom.len = 0;
206 gcp->provider.u.id = (uintptr_t)ga.provider;
207 gcp->provider.len = 0;
208 return(error);
209}
210
211static int
212g_ctl_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
213{
214 int error;
215
216 DROP_GIANT();
217 g_topology_lock();
218 switch(cmd) {
219 case GEOMGETCONF:
220 error = g_ctl_ioctl_getconf(dev, cmd, data, fflag, td);
221 break;
222 case GEOMCONFIGGEOM:
223 error = g_ctl_ioctl_configgeom(dev, cmd, data, fflag, td);
224 break;
225 default:
226 error = ENOTTY;
227 break;
228 }
229 g_topology_unlock();
230 PICKUP_GIANT();
231 return (error);
232
233}
89 g_error_provider(pp, 0);
90}
91
92static void
93g_ctl_init(void)
94{
95 mtx_unlock(&Giant);
96 g_add_class(&g_ctl_class);
97 g_call_me(g_ctl_init2, NULL);
98 mtx_lock(&Giant);
99}
100
101/*
102 * We allow any kind of access. Access control is handled at the devfs
103 * level.
104 */
105
106static int
107g_ctl_access(struct g_provider *pp, int r, int w, int e)
108{
109 int error;
110
111 g_trace(G_T_ACCESS, "g_ctl_access(%s, %d, %d, %d)",
112 pp->name, r, w, e);
113
114 g_topology_assert();
115 error = 0;
116 return (error);
117}
118
119static void
120g_ctl_start(struct bio *bp)
121{
122 struct g_ioctl *gio;
123 int error;
124
125 switch(bp->bio_cmd) {
126 case BIO_DELETE:
127 case BIO_READ:
128 case BIO_WRITE:
129 error = EOPNOTSUPP;
130 break;
131 case BIO_GETATTR:
132 case BIO_SETATTR:
133 if (strcmp(bp->bio_attribute, "GEOM::ioctl") ||
134 bp->bio_length != sizeof *gio) {
135 error = EOPNOTSUPP;
136 break;
137 }
138 gio = (struct g_ioctl *)bp->bio_data;
139 gio->func = g_ctl_ioctl;
140 error = EDIRIOCTL;
141 break;
142 default:
143 error = EOPNOTSUPP;
144 break;
145 }
146 g_io_deliver(bp, error);
147 return;
148}
149
150/*
151 * All the stuff above is really just needed to get to the stuff below
152 */
153
154static int
155g_ctl_ioctl_getconf(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
156{
157 struct geomgetconf *gcp;
158 struct sbuf *sb;
159 int error;
160 u_int l;
161
162 gcp = (struct geomgetconf *)data;
163 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
164 sbuf_clear(sb);
165 g_confxml(sb);
166 l = sbuf_len(sb) + 1;
167 if (l > gcp->len)
168 error = ENOMEM;
169 else
170 error = copyout(sbuf_data(sb), gcp->ptr, l);
171 sbuf_delete(sb);
172 return(error);
173}
174
175static int
176g_ctl_ioctl_configgeom(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
177{
178 struct geomconfiggeom *gcp;
179 struct g_createargs ga;
180 int error;
181
182 error = 0;
183 bzero(&ga, sizeof ga);
184 gcp = (struct geomconfiggeom *)data;
185 ga.class = g_idclass(&gcp->class);
186 if (ga.class == NULL)
187 return (EINVAL);
188 if (ga.class->create_geom == NULL)
189 return (EOPNOTSUPP);
190 ga.geom = g_idgeom(&gcp->geom);
191 ga.provider = g_idprovider(&gcp->provider);
192 ga.len = gcp->len;
193 if (gcp->len > 64 * 1024)
194 return (EINVAL);
195 else if (gcp->len == 0) {
196 ga.ptr = NULL;
197 } else {
198 ga.ptr = g_malloc(gcp->len, M_WAITOK);
199 copyin(gcp->ptr, ga.ptr, gcp->len);
200 }
201 ga.flag = gcp->flag;
202 error = ga.class->create_geom(&ga);
203 gcp->class.u.id = (uintptr_t)ga.class;
204 gcp->class.len = 0;
205 gcp->geom.u.id = (uintptr_t)ga.geom;
206 gcp->geom.len = 0;
207 gcp->provider.u.id = (uintptr_t)ga.provider;
208 gcp->provider.len = 0;
209 return(error);
210}
211
212static int
213g_ctl_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
214{
215 int error;
216
217 DROP_GIANT();
218 g_topology_lock();
219 switch(cmd) {
220 case GEOMGETCONF:
221 error = g_ctl_ioctl_getconf(dev, cmd, data, fflag, td);
222 break;
223 case GEOMCONFIGGEOM:
224 error = g_ctl_ioctl_configgeom(dev, cmd, data, fflag, td);
225 break;
226 default:
227 error = ENOTTY;
228 break;
229 }
230 g_topology_unlock();
231 PICKUP_GIANT();
232 return (error);
233
234}