2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
48 #include <sys/malloc.h>
49 #include <sys/sysctl.h>
53 #include <sys/mutex.h>
56 #include <vm/vm_extern.h>
58 #include <geom/geom.h>
59 #include <geom/geom_int.h>
61 #include <geom/geom_ctl.h>
63 #include <machine/stdarg.h>
65 static d_ioctl_t g_ctl_ioctl;
67 static struct cdevsw g_ctl_cdevsw = {
68 .d_version = D_VERSION,
69 .d_flags = D_NEEDGIANT,
70 .d_ioctl = g_ctl_ioctl,
78 make_dev(&g_ctl_cdevsw, 0,
79 UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL);
80 KASSERT(GCTL_PARAM_RD == VM_PROT_READ,
81 ("GCTL_PARAM_RD != VM_PROT_READ"));
82 KASSERT(GCTL_PARAM_WR == VM_PROT_WRITE,
83 ("GCTL_PARAM_WR != VM_PROT_WRITE"));
87 * Report an error back to the user in ascii format. Return whatever copyout
88 * returned, or EINVAL if it succeeded.
91 gctl_error(struct gctl_req *req, const char *fmt, ...)
98 /* We only record the first error */
99 if (sbuf_done(req->serror)) {
101 req->nerror = EEXIST;
104 return (req->nerror);
107 sbuf_vprintf(req->serror, fmt, ap);
109 sbuf_finish(req->serror);
110 if (g_debugflags & G_F_CTLDUMP)
111 printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror));
116 * Allocate space and copyin() something.
117 * XXX: this should really be a standard function in the kernel.
120 geom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len)
124 ptr = g_malloc(len, M_WAITOK);
126 req->nerror = ENOMEM;
128 req->nerror = copyin(uaddr, ptr, len);
137 gctl_copyin(struct gctl_req *req)
140 struct gctl_req_arg *ap;
143 ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap));
145 req->nerror = ENOMEM;
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;
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);
164 p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen);
167 if (p[ap[i].nlen - 1] != '\0') {
168 error = gctl_error(req, "unterminated param name");
173 ap[i].flag |= GCTL_PARAM_NAMEKERNEL;
174 if (ap[i].len <= 0) {
175 error = gctl_error(req, "negative param length");
178 p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
181 if ((ap[i].flag & GCTL_PARAM_ASCII) &&
182 p[ap[i].len - 1] != '\0') {
183 error = gctl_error(req, "unterminated param value");
188 ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
195 gctl_copyout(struct gctl_req *req)
198 struct gctl_req_arg *ap;
204 for (i = 0; i < req->narg; i++, ap++) {
205 if (!(ap->flag & GCTL_PARAM_CHANGED))
207 error = copyout(ap->kvalue, ap->value, ap->len);
217 gctl_free(struct gctl_req *req)
221 if (req->arg == NULL)
223 for (i = 0; i < req->narg; i++) {
224 if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
225 g_free(req->arg[i].name);
226 if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
228 g_free(req->arg[i].kvalue);
231 sbuf_delete(req->serror);
235 gctl_dump(struct gctl_req *req)
239 struct gctl_req_arg *ap;
241 printf("Dump of gctl request at %p:\n", req);
242 if (req->nerror > 0) {
243 printf(" nerror:\t%d\n", req->nerror);
244 if (sbuf_len(req->serror) > 0)
245 printf(" error:\t\"%s\"\n", sbuf_data(req->serror));
247 for (i = 0; i < req->narg; i++) {
249 if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
250 printf(" param:\t%d@%p", ap->nlen, ap->name);
252 printf(" param:\t\"%s\"", ap->name);
253 printf(" [%s%s%d] = ",
254 ap->flag & GCTL_PARAM_RD ? "R" : "",
255 ap->flag & GCTL_PARAM_WR ? "W" : "",
257 if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
258 printf(" =@ %p", ap->value);
259 } else if (ap->flag & GCTL_PARAM_ASCII) {
260 printf("\"%s\"", (char *)ap->kvalue);
261 } else if (ap->len > 0) {
262 for (j = 0; j < ap->len && j < 512; j++)
263 printf(" %02x", ((u_char *)ap->kvalue)[j]);
265 printf(" = %p", ap->kvalue);
272 gctl_set_param(struct gctl_req *req, const char *param, void const *ptr,
276 struct gctl_req_arg *ap;
278 for (i = 0; i < req->narg; i++) {
280 if (strcmp(param, ap->name))
282 if (!(ap->flag & GCTL_PARAM_WR))
284 ap->flag |= GCTL_PARAM_CHANGED;
286 bcopy(ptr, ap->kvalue, ap->len);
289 bcopy(ptr, ap->kvalue, len);
296 gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr,
300 switch (gctl_set_param(req, param, ptr, len)) {
302 gctl_error(req, "No write access %s argument", param);
305 gctl_error(req, "Wrong length %s argument", param);
308 gctl_error(req, "Missing %s argument", param);
314 gctl_get_param(struct gctl_req *req, const char *param, int *len)
318 struct gctl_req_arg *ap;
320 for (i = 0; i < req->narg; i++) {
322 if (strcmp(param, ap->name))
324 if (!(ap->flag & GCTL_PARAM_RD))
335 gctl_get_asciiparam(struct gctl_req *req, const char *param)
339 struct gctl_req_arg *ap;
341 for (i = 0; i < req->narg; i++) {
343 if (strcmp(param, ap->name))
345 if (!(ap->flag & GCTL_PARAM_RD))
349 gctl_error(req, "No length argument (%s)", param);
352 if (p[ap->len - 1] != '\0') {
353 gctl_error(req, "Unterminated argument (%s)", param);
362 gctl_get_paraml(struct gctl_req *req, const char *param, int len)
367 p = gctl_get_param(req, param, &i);
369 gctl_error(req, "Missing %s argument", param);
372 gctl_error(req, "Wrong length %s argument", param);
378 gctl_get_class(struct gctl_req *req, char const *arg)
383 p = gctl_get_asciiparam(req, arg);
386 LIST_FOREACH(cp, &g_classes, class) {
387 if (!strcmp(p, cp->name))
394 gctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg)
400 p = gctl_get_asciiparam(req, arg);
403 LIST_FOREACH(mp, &g_classes, class) {
404 if (mpr != NULL && mpr != mp)
406 LIST_FOREACH(gp, &mp->geom, geom) {
407 if (!strcmp(p, gp->name))
411 gctl_error(req, "Geom not found: \"%s\"", p);
416 gctl_get_provider(struct gctl_req *req, char const *arg)
419 struct g_provider *pp;
421 p = gctl_get_asciiparam(req, arg);
424 pp = g_provider_by_name(p);
427 gctl_error(req, "Provider not found: \"%s\"", p);
432 g_ctl_req(void *arg, int flag __unused)
435 struct gctl_req *req;
440 mp = gctl_get_class(req, "class");
442 gctl_error(req, "Class not found");
445 if (mp->ctlreq == NULL) {
446 gctl_error(req, "Class takes no requests");
449 verb = gctl_get_param(req, "verb", NULL);
451 gctl_error(req, "Verb missing");
454 mp->ctlreq(req, mp, verb);
460 g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
462 struct gctl_req *req;
467 req->serror = sbuf_new_auto();
468 /* It is an error if we cannot return an error text */
471 if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
474 /* Check the version */
475 if (req->version != GCTL_VERSION)
476 return (gctl_error(req,
477 "kernel and libgeom version mismatch."));
479 /* Get things on board */
482 if (g_debugflags & G_F_CTLDUMP)
486 g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
489 if (sbuf_done(req->serror)) {
490 req->nerror = copyout(sbuf_data(req->serror), req->error,
491 imin(req->lerror, sbuf_len(req->serror) + 1));
494 nerror = req->nerror;
500 g_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
506 error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);