geom_ctl.c revision 115726
1105068Sphk/*-
2105068Sphk * Copyright (c) 2002 Poul-Henning Kamp
3105068Sphk * Copyright (c) 2002 Networks Associates Technology, Inc.
4105068Sphk * All rights reserved.
5105068Sphk *
6105068Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7105068Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc.
8105068Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9105068Sphk * DARPA CHATS research program.
10105068Sphk *
11105068Sphk * Redistribution and use in source and binary forms, with or without
12105068Sphk * modification, are permitted provided that the following conditions
13105068Sphk * are met:
14105068Sphk * 1. Redistributions of source code must retain the above copyright
15105068Sphk *    notice, this list of conditions and the following disclaimer.
16105068Sphk * 2. Redistributions in binary form must reproduce the above copyright
17105068Sphk *    notice, this list of conditions and the following disclaimer in the
18105068Sphk *    documentation and/or other materials provided with the distribution.
19105068Sphk * 3. The names of the authors may not be used to endorse or promote
20105068Sphk *    products derived from this software without specific prior written
21105068Sphk *    permission.
22105068Sphk *
23105068Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24105068Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25105068Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26105068Sphk * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27105068Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28105068Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29105068Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30105068Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31105068Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32105068Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33105068Sphk * SUCH DAMAGE.
34105068Sphk *
35105068Sphk * $FreeBSD: head/sys/geom/geom_ctl.c 115726 2003-06-02 20:35:37Z phk $
36105068Sphk */
37105068Sphk
38105068Sphk#include "opt_geom.h"
39105068Sphk
40105068Sphk#include <sys/param.h>
41105068Sphk#include <sys/systm.h>
42105068Sphk#include <sys/kernel.h>
43105068Sphk#include <sys/sysctl.h>
44105068Sphk#include <sys/bio.h>
45105068Sphk#include <sys/conf.h>
46105068Sphk#include <sys/disk.h>
47105068Sphk#include <sys/malloc.h>
48105068Sphk#include <sys/sysctl.h>
49113892Sphk#include <sys/sbuf.h>
50105068Sphk
51105068Sphk#include <sys/lock.h>
52105068Sphk#include <sys/mutex.h>
53112511Sphk
54112511Sphk#include <vm/vm.h>
55112511Sphk#include <vm/vm_extern.h>
56112511Sphk
57105068Sphk#include <geom/geom.h>
58105068Sphk#include <geom/geom_int.h>
59112709Sphk#define GCTL_TABLE 1
60112511Sphk#include <geom/geom_ctl.h>
61105068Sphk
62113892Sphk#include <machine/stdarg.h>
63113892Sphk
64105068Sphkstatic d_ioctl_t g_ctl_ioctl;
65105068Sphk
66112534Sphkstatic struct cdevsw g_ctl_cdevsw = {
67112534Sphk	.d_open =	nullopen,
68112534Sphk	.d_close =	nullclose,
69112534Sphk	.d_ioctl =	g_ctl_ioctl,
70112534Sphk	.d_name =	"g_ctl",
71105068Sphk};
72105068Sphk
73112534Sphkvoid
74105068Sphkg_ctl_init(void)
75105068Sphk{
76105068Sphk
77112534Sphk	make_dev(&g_ctl_cdevsw, 0,
78112534Sphk	    UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL);
79112709Sphk	KASSERT(GCTL_PARAM_RD == VM_PROT_READ,
80112709Sphk		("GCTL_PARAM_RD != VM_PROT_READ"));
81112709Sphk	KASSERT(GCTL_PARAM_WR == VM_PROT_WRITE,
82112709Sphk		("GCTL_PARAM_WR != VM_PROT_WRITE"));
83105068Sphk}
84105068Sphk
85112511Sphk/*
86112511Sphk * Report an error back to the user in ascii format.  Return whatever copyout
87112511Sphk * returned, or EINVAL if it succeeded.
88112511Sphk * XXX: should not be static.
89112511Sphk * XXX: should take printf like args.
90112511Sphk */
91112709Sphkint
92113892Sphkgctl_error(struct gctl_req *req, const char *fmt, ...)
93112511Sphk{
94113892Sphk	va_list ap;
95112511Sphk
96115624Sphk	if (req == NULL)
97115624Sphk		return (EINVAL);
98115624Sphk
99115624Sphk	/* We only record the first error */
100115624Sphk	if (req->nerror)
101115624Sphk		return (req->nerror);
102115624Sphk
103115624Sphk	va_start(ap, fmt);
104115624Sphk	sbuf_vprintf(req->serror, fmt, ap);
105115624Sphk	sbuf_finish(req->serror);
106115624Sphk	if (g_debugflags & G_F_CTLDUMP)
107115624Sphk		printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror));
108115624Sphk	req->nerror = copyout(sbuf_data(req->serror), req->error,
109115624Sphk	    imin(req->lerror, sbuf_len(req->serror) + 1));
110115624Sphk	if (!req->nerror)
111115624Sphk		req->nerror = EINVAL;
112115624Sphk	return (req->nerror);
113112511Sphk}
114112511Sphk
115112511Sphk/*
116112511Sphk * Allocate space and copyin() something.
117112511Sphk * XXX: this should really be a standard function in the kernel.
118112511Sphk */
119112511Sphkstatic void *
120115624Sphkgeom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len)
121112511Sphk{
122112511Sphk	void *ptr;
123112511Sphk
124112511Sphk	ptr = g_malloc(len, M_WAITOK);
125112511Sphk	if (ptr == NULL)
126115624Sphk		req->nerror = ENOMEM;
127112511Sphk	else
128115624Sphk		req->nerror = copyin(uaddr, ptr, len);
129115624Sphk	if (!req->nerror)
130112511Sphk		return (ptr);
131112511Sphk	if (ptr != NULL)
132112511Sphk		g_free(ptr);
133112511Sphk	return (NULL);
134112511Sphk}
135112511Sphk
136115624Sphkstatic void
137112709Sphkgctl_copyin(struct gctl_req *req)
138112511Sphk{
139115624Sphk	int error, i;
140112709Sphk	struct gctl_req_arg *ap;
141112511Sphk	char *p;
142112511Sphk
143115624Sphk	ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap));
144112709Sphk	if (ap == NULL) {
145115624Sphk		req->nerror = ENOMEM;
146115624Sphk		req->arg = NULL;
147115624Sphk		return;
148112709Sphk	}
149112709Sphk
150115624Sphk	/* Nothing have been copyin()'ed yet */
151115624Sphk	for (i = 0; i < req->narg; i++) {
152115624Sphk		ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL);
153115624Sphk		ap[i].flag &= ~GCTL_PARAM_CHANGED;
154115624Sphk		ap[i].kvalue = NULL;
155115624Sphk	}
156115624Sphk
157115624Sphk	error = 0;
158115624Sphk	for (i = 0; i < req->narg; i++) {
159112709Sphk		if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) {
160115624Sphk			error = gctl_error(req,
161115624Sphk			    "wrong param name length %d: %d", i, ap[i].nlen);
162112511Sphk			break;
163112709Sphk		}
164115624Sphk		p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen);
165112709Sphk		if (p == NULL)
166112511Sphk			break;
167112709Sphk		if (p[ap[i].nlen - 1] != '\0') {
168112709Sphk			error = gctl_error(req, "unterminated param name");
169112511Sphk			g_free(p);
170112511Sphk			break;
171112511Sphk		}
172112709Sphk		ap[i].name = p;
173115624Sphk		ap[i].flag |= GCTL_PARAM_NAMEKERNEL;
174115624Sphk		if (ap[i].len < 0) {
175115624Sphk			error = gctl_error(req, "negative param length");
176115624Sphk			break;
177115624Sphk		}
178115624Sphk		if (ap[i].len == 0) {
179115624Sphk			ap[i].kvalue = ap[i].value;
180115624Sphk			ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
181115624Sphk			continue;
182115624Sphk		}
183115624Sphk		p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
184115624Sphk		if (p == NULL)
185115624Sphk			break;
186115624Sphk		if ((ap[i].flag & GCTL_PARAM_ASCII) &&
187115624Sphk		    p[ap[i].len - 1] != '\0') {
188115624Sphk			error = gctl_error(req, "unterminated param value");
189115624Sphk			g_free(p);
190115624Sphk			break;
191115624Sphk		}
192115624Sphk		ap[i].kvalue = p;
193115624Sphk		ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
194112511Sphk	}
195115624Sphk	req->arg = ap;
196115624Sphk	return;
197115624Sphk}
198115624Sphk
199115624Sphkstatic void
200115624Sphkgctl_copyout(struct gctl_req *req)
201115624Sphk{
202115624Sphk	int error, i;
203115624Sphk	struct gctl_req_arg *ap;
204115624Sphk
205115624Sphk	if (req->nerror)
206115624Sphk		return;
207115624Sphk	error = 0;
208115624Sphk	ap = req->arg;
209115624Sphk	for (i = 0; i < req->narg; i++, ap++) {
210115624Sphk		if (!(ap->flag & GCTL_PARAM_CHANGED))
211115624Sphk			continue;
212115624Sphk		error = copyout(ap->kvalue, ap->value, ap->len);
213115624Sphk		if (!error)
214115624Sphk			continue;
215115624Sphk		req->nerror = error;
216115624Sphk		return;
217112511Sphk	}
218115624Sphk	return;
219112511Sphk}
220112511Sphk
221112511Sphkstatic void
222114531Sphkgctl_free(struct gctl_req *req)
223114531Sphk{
224114531Sphk	int i;
225114531Sphk
226115624Sphk	if (req->arg == NULL)
227115624Sphk		return;
228114531Sphk	for (i = 0; i < req->narg; i++) {
229115624Sphk		if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
230114531Sphk			g_free(req->arg[i].name);
231115624Sphk		if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
232115624Sphk		    req->arg[i].len > 0)
233115624Sphk			g_free(req->arg[i].kvalue);
234114531Sphk	}
235114531Sphk	g_free(req->arg);
236115624Sphk	sbuf_delete(req->serror);
237114531Sphk}
238114531Sphk
239114531Sphkstatic void
240112709Sphkgctl_dump(struct gctl_req *req)
241112511Sphk{
242112511Sphk	u_int i;
243115624Sphk	int j;
244112709Sphk	struct gctl_req_arg *ap;
245112511Sphk
246115624Sphk	printf("Dump of gctl request at %p:\n", req);
247115624Sphk	if (req->nerror > 0) {
248115624Sphk		printf("  nerror:\t%d\n", req->nerror);
249115624Sphk		if (sbuf_len(req->serror) > 0)
250115624Sphk			printf("  error:\t\"%s\"\n", sbuf_data(req->serror));
251112511Sphk	}
252112511Sphk	for (i = 0; i < req->narg; i++) {
253112511Sphk		ap = &req->arg[i];
254115624Sphk		if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
255115624Sphk			printf("  param:\t%d@%p", ap->nlen, ap->name);
256115624Sphk		else
257115624Sphk			printf("  param:\t\"%s\"", ap->name);
258112709Sphk		printf(" [%s%s%d] = ",
259112709Sphk		    ap->flag & GCTL_PARAM_RD ? "R" : "",
260112709Sphk		    ap->flag & GCTL_PARAM_WR ? "W" : "",
261112709Sphk		    ap->len);
262115624Sphk		if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
263115624Sphk			printf(" =@ %p", ap->value);
264115624Sphk		} else if (ap->flag & GCTL_PARAM_ASCII) {
265115624Sphk			printf("\"%s\"", (char *)ap->kvalue);
266112511Sphk		} else if (ap->len > 0) {
267112511Sphk			for (j = 0; j < ap->len; j++)
268115624Sphk				printf(" %02x", ((u_char *)ap->kvalue)[j]);
269112511Sphk		} else {
270115624Sphk			printf(" = %p", ap->kvalue);
271112511Sphk		}
272112511Sphk		printf("\n");
273112511Sphk	}
274112511Sphk}
275112511Sphk
276115624Sphkvoid
277115624Sphkgctl_set_param(struct gctl_req *req, const char *param, void const *ptr, int len)
278114670Sphk{
279115624Sphk	int i;
280114670Sphk	struct gctl_req_arg *ap;
281114670Sphk
282114670Sphk	for (i = 0; i < req->narg; i++) {
283114670Sphk		ap = &req->arg[i];
284114670Sphk		if (strcmp(param, ap->name))
285114670Sphk			continue;
286114670Sphk		if (!(ap->flag & GCTL_PARAM_WR)) {
287114670Sphk			gctl_error(req, "No write access %s argument", param);
288115624Sphk			return;
289114670Sphk		}
290115624Sphk		if (ap->len < len) {
291114670Sphk			gctl_error(req, "Wrong length %s argument", param);
292115624Sphk			return;
293114670Sphk		}
294115624Sphk		bcopy(ptr, ap->kvalue, len);
295115624Sphk		ap->flag |= GCTL_PARAM_CHANGED;
296115624Sphk		return;
297114670Sphk	}
298114670Sphk	gctl_error(req, "Missing %s argument", param);
299115624Sphk	return;
300114670Sphk}
301114670Sphk
302112709Sphkvoid *
303112709Sphkgctl_get_param(struct gctl_req *req, const char *param, int *len)
304112709Sphk{
305115624Sphk	int i;
306112709Sphk	void *p;
307112709Sphk	struct gctl_req_arg *ap;
308112709Sphk
309112709Sphk	for (i = 0; i < req->narg; i++) {
310112709Sphk		ap = &req->arg[i];
311112709Sphk		if (strcmp(param, ap->name))
312112709Sphk			continue;
313112709Sphk		if (!(ap->flag & GCTL_PARAM_RD))
314112709Sphk			continue;
315115624Sphk		p = ap->kvalue;
316112709Sphk		if (len != NULL)
317115624Sphk			*len = ap->len;
318112709Sphk		return (p);
319112709Sphk	}
320112709Sphk	return (NULL);
321112709Sphk}
322112709Sphk
323115624Sphkchar const *
324115624Sphkgctl_get_asciiparam(struct gctl_req *req, const char *param)
325115624Sphk{
326115624Sphk	int i;
327115624Sphk	char const *p;
328115624Sphk	struct gctl_req_arg *ap;
329115624Sphk
330115624Sphk	for (i = 0; i < req->narg; i++) {
331115624Sphk		ap = &req->arg[i];
332115624Sphk		if (strcmp(param, ap->name))
333115624Sphk			continue;
334115624Sphk		if (!(ap->flag & GCTL_PARAM_RD))
335115624Sphk			continue;
336115624Sphk		p = ap->kvalue;
337115624Sphk		if (ap->len < 1) {
338115624Sphk			gctl_error(req, "No length argument (%s)", param);
339115624Sphk			return (NULL);
340115624Sphk		}
341115624Sphk		if (p[ap->len - 1] != '\0') {
342115624Sphk			gctl_error(req, "Unterminated argument (%s)", param);
343115624Sphk			return (NULL);
344115624Sphk		}
345115624Sphk		return (p);
346115624Sphk	}
347115624Sphk	return (NULL);
348115624Sphk}
349115624Sphk
350113893Sphkvoid *
351113893Sphkgctl_get_paraml(struct gctl_req *req, const char *param, int len)
352113893Sphk{
353113893Sphk	int i;
354113893Sphk	void *p;
355113893Sphk
356113893Sphk	p = gctl_get_param(req, param, &i);
357113893Sphk	if (p == NULL)
358113893Sphk		gctl_error(req, "Missing %s argument", param);
359113893Sphk	else if (i != len) {
360113893Sphk		p = NULL;
361113893Sphk		gctl_error(req, "Wrong length %s argument", param);
362113893Sphk	}
363113893Sphk	return (p);
364113893Sphk}
365113893Sphk
366115624Sphkstruct g_class *
367115624Sphkgctl_get_class(struct gctl_req *req, char const *arg)
368112709Sphk{
369115624Sphk	char const *p;
370112709Sphk	struct g_class *cp;
371112709Sphk
372115624Sphk	p = gctl_get_asciiparam(req, arg);
373112709Sphk	if (p == NULL)
374112709Sphk		return (NULL);
375112709Sphk	LIST_FOREACH(cp, &g_classes, class) {
376115624Sphk		if (!strcmp(p, cp->name))
377112709Sphk			return (cp);
378112709Sphk	}
379112709Sphk	gctl_error(req, "Class not found");
380112709Sphk	return (NULL);
381112709Sphk}
382112709Sphk
383115624Sphkstruct g_geom *
384115624Sphkgctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg)
385112709Sphk{
386115624Sphk	char const *p;
387112709Sphk	struct g_class *mp;
388112709Sphk	struct g_geom *gp;
389112709Sphk
390115624Sphk	p = gctl_get_asciiparam(req, arg);
391112709Sphk	if (p == NULL)
392112709Sphk		return (NULL);
393112709Sphk	LIST_FOREACH(mp, &g_classes, class) {
394112709Sphk		if (mpr != NULL && mpr != mp)
395112709Sphk			continue;
396112709Sphk		LIST_FOREACH(gp, &mp->geom, geom) {
397115624Sphk			if (!strcmp(p, gp->name))
398112709Sphk				return (gp);
399112709Sphk		}
400112709Sphk	}
401112709Sphk	gctl_error(req, "Geom not found");
402112709Sphk	return (NULL);
403112709Sphk}
404112709Sphk
405115624Sphkstruct g_provider *
406115624Sphkgctl_get_provider(struct gctl_req *req, char const *arg)
407112709Sphk{
408115624Sphk	char const *p;
409112709Sphk	struct g_class *cp;
410112709Sphk	struct g_geom *gp;
411112709Sphk	struct g_provider *pp;
412112709Sphk
413115624Sphk	p = gctl_get_asciiparam(req, arg);
414112709Sphk	if (p == NULL)
415112709Sphk		return (NULL);
416112709Sphk	LIST_FOREACH(cp, &g_classes, class) {
417112709Sphk		LIST_FOREACH(gp, &cp->geom, geom) {
418112709Sphk			LIST_FOREACH(pp, &gp->provider, provider) {
419115624Sphk				if (!strcmp(p, pp->name))
420112709Sphk					return (pp);
421112709Sphk			}
422112709Sphk		}
423112709Sphk	}
424112709Sphk	gctl_error(req, "Provider not found");
425112709Sphk	return (NULL);
426112709Sphk}
427112709Sphk
428115624Sphkstatic void
429115624Sphkg_ctl_req(void *arg, int flag __unused)
430112709Sphk{
431112709Sphk	struct g_class *mp;
432115624Sphk	struct gctl_req *req;
433115624Sphk	char const *verb;
434112709Sphk
435112709Sphk	g_topology_assert();
436115624Sphk	req = arg;
437115624Sphk	mp = gctl_get_class(req, "class");
438115726Sphk	if (mp == NULL) {
439115726Sphk		gctl_error(req, "Class not found");
440115624Sphk		return;
441115726Sphk	}
442115624Sphk	verb = gctl_get_param(req, "verb", NULL);
443115624Sphk	if (mp->ctlreq == NULL)
444115624Sphk		gctl_error(req, "Class takes no requests");
445115624Sphk	else
446115624Sphk		mp->ctlreq(req, mp, verb);
447112709Sphk	g_topology_assert();
448112709Sphk}
449112709Sphk
450112709Sphk
451113876Sphkstatic int
452112511Sphkg_ctl_ioctl_ctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
453112511Sphk{
454112709Sphk	struct gctl_req *req;
455112511Sphk
456112511Sphk	req = (void *)data;
457115624Sphk	req->nerror = 0;
458115624Sphk	req->serror = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
459112709Sphk	/* It is an error if we cannot return an error text */
460115624Sphk	if (req->lerror < 2)
461112511Sphk		return (EINVAL);
462112709Sphk	if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
463112709Sphk		return (EINVAL);
464112709Sphk
465112709Sphk	/* Check the version */
466112709Sphk	if (req->version != GCTL_VERSION)
467112709Sphk		return (gctl_error(req,
468112709Sphk		    "kernel and libgeom version mismatch."));
469112709Sphk
470112709Sphk	/* Get things on board */
471115624Sphk	gctl_copyin(req);
472112709Sphk
473112876Sphk	if (g_debugflags & G_F_CTLDUMP)
474112876Sphk		gctl_dump(req);
475115624Sphk
476115624Sphk	if (!req->nerror) {
477115624Sphk		g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
478115624Sphk		gctl_copyout(req);
479112709Sphk	}
480115624Sphk
481114531Sphk	gctl_free(req);
482115624Sphk	return (req->nerror);
483112511Sphk}
484112511Sphk
485112511Sphkstatic int
486105068Sphkg_ctl_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
487105068Sphk{
488105068Sphk	int error;
489105068Sphk
490105068Sphk	switch(cmd) {
491112511Sphk	case GEOM_CTL:
492112511Sphk		error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);
493112511Sphk		break;
494105068Sphk	default:
495115624Sphk		error = ENOIOCTL;
496105068Sphk		break;
497105068Sphk	}
498105068Sphk	return (error);
499105068Sphk
500105068Sphk}
501