geom_ctl.c revision 115624
1143880Spjd/*-
2143880Spjd * Copyright (c) 2002 Poul-Henning Kamp
3143880Spjd * Copyright (c) 2002 Networks Associates Technology, Inc.
4143880Spjd * All rights reserved.
5143880Spjd *
6143880Spjd * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7143880Spjd * and NAI Labs, the Security Research Division of Network Associates, Inc.
8143880Spjd * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9263351Sjmmv * DARPA CHATS research program.
10143880Spjd *
11143880Spjd * Redistribution and use in source and binary forms, with or without
12143880Spjd * modification, are permitted provided that the following conditions
13143880Spjd * are met:
14143880Spjd * 1. Redistributions of source code must retain the above copyright
15143880Spjd *    notice, this list of conditions and the following disclaimer.
16143880Spjd * 2. Redistributions in binary form must reproduce the above copyright
17143880Spjd *    notice, this list of conditions and the following disclaimer in the
18143880Spjd *    documentation and/or other materials provided with the distribution.
19143880Spjd * 3. The names of the authors may not be used to endorse or promote
20143880Spjd *    products derived from this software without specific prior written
21143880Spjd *    permission.
22143880Spjd *
23143880Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24143880Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25143880Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26143880Spjd * 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 115624 2003-06-01 13:47:51Z 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/sbuf.h>
50
51#include <sys/lock.h>
52#include <sys/mutex.h>
53
54#include <vm/vm.h>
55#include <vm/vm_extern.h>
56
57#include <geom/geom.h>
58#include <geom/geom_int.h>
59#define GCTL_TABLE 1
60#include <geom/geom_ctl.h>
61
62#include <machine/stdarg.h>
63
64static d_ioctl_t g_ctl_ioctl;
65
66static struct cdevsw g_ctl_cdevsw = {
67	.d_open =	nullopen,
68	.d_close =	nullclose,
69	.d_ioctl =	g_ctl_ioctl,
70	.d_name =	"g_ctl",
71};
72
73void
74g_ctl_init(void)
75{
76
77	make_dev(&g_ctl_cdevsw, 0,
78	    UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL);
79	KASSERT(GCTL_PARAM_RD == VM_PROT_READ,
80		("GCTL_PARAM_RD != VM_PROT_READ"));
81	KASSERT(GCTL_PARAM_WR == VM_PROT_WRITE,
82		("GCTL_PARAM_WR != VM_PROT_WRITE"));
83}
84
85/*
86 * Report an error back to the user in ascii format.  Return whatever copyout
87 * returned, or EINVAL if it succeeded.
88 * XXX: should not be static.
89 * XXX: should take printf like args.
90 */
91int
92gctl_error(struct gctl_req *req, const char *fmt, ...)
93{
94	va_list ap;
95
96	if (req == NULL)
97		return (EINVAL);
98
99	/* We only record the first error */
100	if (req->nerror)
101		return (req->nerror);
102
103	va_start(ap, fmt);
104	sbuf_vprintf(req->serror, fmt, ap);
105	sbuf_finish(req->serror);
106	if (g_debugflags & G_F_CTLDUMP)
107		printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror));
108	req->nerror = copyout(sbuf_data(req->serror), req->error,
109	    imin(req->lerror, sbuf_len(req->serror) + 1));
110	if (!req->nerror)
111		req->nerror = EINVAL;
112	return (req->nerror);
113}
114
115/*
116 * Allocate space and copyin() something.
117 * XXX: this should really be a standard function in the kernel.
118 */
119static void *
120geom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len)
121{
122	void *ptr;
123
124	ptr = g_malloc(len, M_WAITOK);
125	if (ptr == NULL)
126		req->nerror = ENOMEM;
127	else
128		req->nerror = copyin(uaddr, ptr, len);
129	if (!req->nerror)
130		return (ptr);
131	if (ptr != NULL)
132		g_free(ptr);
133	return (NULL);
134}
135
136static void
137gctl_copyin(struct gctl_req *req)
138{
139	int error, i;
140	struct gctl_req_arg *ap;
141	char *p;
142
143	ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap));
144	if (ap == NULL) {
145		req->nerror = ENOMEM;
146		req->arg = NULL;
147		return;
148	}
149
150	/* Nothing have been copyin()'ed yet */
151	for (i = 0; i < req->narg; i++) {
152		ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL);
153		ap[i].flag &= ~GCTL_PARAM_CHANGED;
154		ap[i].kvalue = NULL;
155	}
156
157	error = 0;
158	for (i = 0; i < req->narg; i++) {
159		if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) {
160			error = gctl_error(req,
161			    "wrong param name length %d: %d", i, ap[i].nlen);
162			break;
163		}
164		p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen);
165		if (p == NULL)
166			break;
167		if (p[ap[i].nlen - 1] != '\0') {
168			error = gctl_error(req, "unterminated param name");
169			g_free(p);
170			break;
171		}
172		ap[i].name = p;
173		ap[i].flag |= GCTL_PARAM_NAMEKERNEL;
174		if (ap[i].len < 0) {
175			error = gctl_error(req, "negative param length");
176			break;
177		}
178		if (ap[i].len == 0) {
179			ap[i].kvalue = ap[i].value;
180			ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
181			continue;
182		}
183		p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
184		if (p == NULL)
185			break;
186		if ((ap[i].flag & GCTL_PARAM_ASCII) &&
187		    p[ap[i].len - 1] != '\0') {
188			error = gctl_error(req, "unterminated param value");
189			g_free(p);
190			break;
191		}
192		ap[i].kvalue = p;
193		ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
194	}
195	req->arg = ap;
196	return;
197}
198
199static void
200gctl_copyout(struct gctl_req *req)
201{
202	int error, i;
203	struct gctl_req_arg *ap;
204
205	if (req->nerror)
206		return;
207	error = 0;
208	ap = req->arg;
209	for (i = 0; i < req->narg; i++, ap++) {
210		if (!(ap->flag & GCTL_PARAM_CHANGED))
211			continue;
212		error = copyout(ap->kvalue, ap->value, ap->len);
213		if (!error)
214			continue;
215		req->nerror = error;
216		return;
217	}
218	return;
219}
220
221static void
222gctl_free(struct gctl_req *req)
223{
224	int i;
225
226	if (req->arg == NULL)
227		return;
228	for (i = 0; i < req->narg; i++) {
229		if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
230			g_free(req->arg[i].name);
231		if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
232		    req->arg[i].len > 0)
233			g_free(req->arg[i].kvalue);
234	}
235	g_free(req->arg);
236	sbuf_delete(req->serror);
237}
238
239static void
240gctl_dump(struct gctl_req *req)
241{
242	u_int i;
243	int j;
244	struct gctl_req_arg *ap;
245
246	printf("Dump of gctl request at %p:\n", req);
247	if (req->nerror > 0) {
248		printf("  nerror:\t%d\n", req->nerror);
249		if (sbuf_len(req->serror) > 0)
250			printf("  error:\t\"%s\"\n", sbuf_data(req->serror));
251	}
252	for (i = 0; i < req->narg; i++) {
253		ap = &req->arg[i];
254		if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
255			printf("  param:\t%d@%p", ap->nlen, ap->name);
256		else
257			printf("  param:\t\"%s\"", ap->name);
258		printf(" [%s%s%d] = ",
259		    ap->flag & GCTL_PARAM_RD ? "R" : "",
260		    ap->flag & GCTL_PARAM_WR ? "W" : "",
261		    ap->len);
262		if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
263			printf(" =@ %p", ap->value);
264		} else if (ap->flag & GCTL_PARAM_ASCII) {
265			printf("\"%s\"", (char *)ap->kvalue);
266		} else if (ap->len > 0) {
267			for (j = 0; j < ap->len; j++)
268				printf(" %02x", ((u_char *)ap->kvalue)[j]);
269		} else {
270			printf(" = %p", ap->kvalue);
271		}
272		printf("\n");
273	}
274}
275
276void
277gctl_set_param(struct gctl_req *req, const char *param, void const *ptr, int len)
278{
279	int i;
280	struct gctl_req_arg *ap;
281
282	for (i = 0; i < req->narg; i++) {
283		ap = &req->arg[i];
284		if (strcmp(param, ap->name))
285			continue;
286		if (!(ap->flag & GCTL_PARAM_WR)) {
287			gctl_error(req, "No write access %s argument", param);
288			return;
289		}
290		if (ap->len < len) {
291			gctl_error(req, "Wrong length %s argument", param);
292			return;
293		}
294		bcopy(ptr, ap->kvalue, len);
295		ap->flag |= GCTL_PARAM_CHANGED;
296		return;
297	}
298	gctl_error(req, "Missing %s argument", param);
299	return;
300}
301
302void *
303gctl_get_param(struct gctl_req *req, const char *param, int *len)
304{
305	int i;
306	void *p;
307	struct gctl_req_arg *ap;
308
309	for (i = 0; i < req->narg; i++) {
310		ap = &req->arg[i];
311		if (strcmp(param, ap->name))
312			continue;
313		if (!(ap->flag & GCTL_PARAM_RD))
314			continue;
315		p = ap->kvalue;
316		if (len != NULL)
317			*len = ap->len;
318		return (p);
319	}
320	return (NULL);
321}
322
323char const *
324gctl_get_asciiparam(struct gctl_req *req, const char *param)
325{
326	int i;
327	char const *p;
328	struct gctl_req_arg *ap;
329
330	for (i = 0; i < req->narg; i++) {
331		ap = &req->arg[i];
332		if (strcmp(param, ap->name))
333			continue;
334		if (!(ap->flag & GCTL_PARAM_RD))
335			continue;
336		p = ap->kvalue;
337		if (ap->len < 1) {
338			gctl_error(req, "No length argument (%s)", param);
339			return (NULL);
340		}
341		if (p[ap->len - 1] != '\0') {
342			gctl_error(req, "Unterminated argument (%s)", param);
343			return (NULL);
344		}
345		return (p);
346	}
347	return (NULL);
348}
349
350void *
351gctl_get_paraml(struct gctl_req *req, const char *param, int len)
352{
353	int i;
354	void *p;
355
356	p = gctl_get_param(req, param, &i);
357	if (p == NULL)
358		gctl_error(req, "Missing %s argument", param);
359	else if (i != len) {
360		p = NULL;
361		gctl_error(req, "Wrong length %s argument", param);
362	}
363	return (p);
364}
365
366struct g_class *
367gctl_get_class(struct gctl_req *req, char const *arg)
368{
369	char const *p;
370	struct g_class *cp;
371
372	p = gctl_get_asciiparam(req, arg);
373	if (p == NULL)
374		return (NULL);
375	LIST_FOREACH(cp, &g_classes, class) {
376		if (!strcmp(p, cp->name))
377			return (cp);
378	}
379	gctl_error(req, "Class not found");
380	return (NULL);
381}
382
383struct g_geom *
384gctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg)
385{
386	char const *p;
387	struct g_class *mp;
388	struct g_geom *gp;
389
390	p = gctl_get_asciiparam(req, arg);
391	if (p == NULL)
392		return (NULL);
393	LIST_FOREACH(mp, &g_classes, class) {
394		if (mpr != NULL && mpr != mp)
395			continue;
396		LIST_FOREACH(gp, &mp->geom, geom) {
397			if (!strcmp(p, gp->name))
398				return (gp);
399		}
400	}
401	gctl_error(req, "Geom not found");
402	return (NULL);
403}
404
405struct g_provider *
406gctl_get_provider(struct gctl_req *req, char const *arg)
407{
408	char const *p;
409	struct g_class *cp;
410	struct g_geom *gp;
411	struct g_provider *pp;
412
413	p = gctl_get_asciiparam(req, arg);
414	if (p == NULL)
415		return (NULL);
416	LIST_FOREACH(cp, &g_classes, class) {
417		LIST_FOREACH(gp, &cp->geom, geom) {
418			LIST_FOREACH(pp, &gp->provider, provider) {
419				if (!strcmp(p, pp->name))
420					return (pp);
421			}
422		}
423	}
424	gctl_error(req, "Provider not found");
425	return (NULL);
426}
427
428static void
429g_ctl_req(void *arg, int flag __unused)
430{
431	struct g_class *mp;
432	struct gctl_req *req;
433	char const *verb;
434
435	g_topology_assert();
436	req = arg;
437	mp = gctl_get_class(req, "class");
438	if (mp == NULL)
439		return;
440	verb = gctl_get_param(req, "verb", NULL);
441	if (mp->ctlreq == NULL)
442		gctl_error(req, "Class takes no requests");
443	else
444		mp->ctlreq(req, mp, verb);
445	g_topology_assert();
446}
447
448
449static int
450g_ctl_ioctl_ctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
451{
452	struct gctl_req *req;
453
454	req = (void *)data;
455	req->nerror = 0;
456	req->serror = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
457	/* It is an error if we cannot return an error text */
458	if (req->lerror < 2)
459		return (EINVAL);
460	if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
461		return (EINVAL);
462
463	/* Check the version */
464	if (req->version != GCTL_VERSION)
465		return (gctl_error(req,
466		    "kernel and libgeom version mismatch."));
467
468	/* Get things on board */
469	gctl_copyin(req);
470
471	if (g_debugflags & G_F_CTLDUMP)
472		gctl_dump(req);
473
474	if (!req->nerror) {
475		g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
476		gctl_copyout(req);
477	}
478
479	gctl_free(req);
480	return (req->nerror);
481}
482
483static int
484g_ctl_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
485{
486	int error;
487
488	switch(cmd) {
489	case GEOM_CTL:
490		error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);
491		break;
492	default:
493		error = ENOIOCTL;
494		break;
495	}
496	return (error);
497
498}
499