geom_ctl.c revision 105092
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 105092 2002-10-14 10:05:23Z 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);
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	gcp = (struct geomconfiggeom *)data;
183	ga.class = g_idclass(&gcp->class);
184	if (ga.class == NULL)
185		return (EINVAL);
186	if (ga.class->create_geom == NULL)
187		return (EOPNOTSUPP);
188	ga.provider = g_idprovider(&gcp->provider);
189	if (ga.provider == NULL)
190		return (EINVAL);
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	error = ga.class->create_geom(&ga);
201	gcp->geom = (uintptr_t)ga.geom;
202	return(error);
203}
204
205static int
206g_ctl_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
207{
208	int error;
209
210	DROP_GIANT();
211	g_topology_lock();
212	switch(cmd) {
213	case GEOMGETCONF:
214		error = g_ctl_ioctl_getconf(dev, cmd, data, fflag, td);
215		break;
216	case GEOMCONFIGGEOM:
217		error = g_ctl_ioctl_configgeom(dev, cmd, data, fflag, td);
218		break;
219	default:
220		error = ENOTTY;
221		break;
222	}
223	g_topology_unlock();
224	PICKUP_GIANT();
225	return (error);
226
227}
228