2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2002 Poul-Henning Kamp
5 * Copyright (c) 2002 Networks Associates Technology, Inc.
8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9 * and NAI Labs, the Security Research Division of Network Associates, Inc.
10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11 * DARPA CHATS research program.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. The names of the authors may not be used to endorse or promote
22 * products derived from this software without specific prior written
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/malloc.h>
48 #include <vm/vm_extern.h>
50 #include <geom/geom.h>
51 #include <geom/geom_int.h>
53 #include <geom/geom_ctl.h>
55 #include <machine/stdarg.h>
57 static d_ioctl_t g_ctl_ioctl;
59 static struct cdevsw g_ctl_cdevsw = {
60 .d_version = D_VERSION,
62 .d_ioctl = g_ctl_ioctl,
66 CTASSERT(GCTL_PARAM_RD == VM_PROT_READ);
67 CTASSERT(GCTL_PARAM_WR == VM_PROT_WRITE);
73 make_dev_credf(MAKEDEV_ETERNAL, &g_ctl_cdevsw, 0, NULL,
74 UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL);
78 * Report an error back to the user in ascii format. Return nerror
79 * or EINVAL if nerror isn't specified.
82 gctl_error(struct gctl_req *req, const char *fmt, ...)
89 /* We only record the first error */
90 if (sbuf_done(req->serror)) {
94 printf("gctl_error: buffer closed, message discarded.\n");
101 /* If this is the last of several messages, indent it on a new line */
102 if (sbuf_len(req->serror) > 0)
103 sbuf_cat(req->serror, "\n\t");
105 sbuf_vprintf(req->serror, fmt, ap);
107 gctl_post_messages(req);
108 return (req->nerror);
112 * The gctl_error() function will only report a single message.
113 * Commands that handle multiple devices may want to report a
114 * message for each of the devices. The gctl_msg() function
115 * can be called multiple times to post messages. When done
116 * the application must either call gctl_post_messages() or
117 * call gctl_error() to cause the messages to be reported to
118 * the calling process.
121 gctl_msg(struct gctl_req *req, const char *fmt, ...)
127 if (sbuf_done(req->serror)) {
129 printf("gctl_msg: buffer closed, message discarded.\n");
133 /* Put second and later messages indented on a new line */
134 if (sbuf_len(req->serror) > 0)
135 sbuf_cat(req->serror, "\n\t");
137 sbuf_vprintf(req->serror, fmt, ap);
142 * Post the messages to the user.
145 gctl_post_messages(struct gctl_req *req)
148 if (sbuf_done(req->serror)) {
150 printf("gctl_post_messages: message buffer already closed.\n");
154 sbuf_finish(req->serror);
155 if (g_debugflags & G_F_CTLDUMP)
156 printf("gctl %p message(s) \"%s\"\n", req,
157 sbuf_data(req->serror));
161 * Allocate space and copyin() something.
162 * XXX: this should really be a standard function in the kernel.
165 geom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len)
169 ptr = g_malloc(len, M_WAITOK);
170 req->nerror = copyin(uaddr, ptr, len);
178 gctl_copyin(struct gctl_req *req)
180 struct gctl_req_arg *ap;
184 if (req->narg > GEOM_CTL_ARG_MAX) {
185 gctl_error(req, "too many arguments");
190 ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap));
192 gctl_error(req, "bad control request");
197 /* Nothing have been copyin()'ed yet */
198 for (i = 0; i < req->narg; i++) {
199 ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL);
200 ap[i].flag &= ~GCTL_PARAM_CHANGED;
204 for (i = 0; i < req->narg; i++) {
205 if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) {
207 "wrong param name length %d: %d", i, ap[i].nlen);
210 p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen);
213 if (p[ap[i].nlen - 1] != '\0') {
214 gctl_error(req, "unterminated param name");
219 ap[i].flag |= GCTL_PARAM_NAMEKERNEL;
220 if (ap[i].len <= 0) {
221 gctl_error(req, "negative param length");
224 if (ap[i].flag & GCTL_PARAM_RD) {
225 p = geom_alloc_copyin(req, ap[i].value, ap[i].len);
228 if ((ap[i].flag & GCTL_PARAM_ASCII) &&
229 p[ap[i].len - 1] != '\0') {
230 gctl_error(req, "unterminated param value");
235 p = g_malloc(ap[i].len, M_WAITOK | M_ZERO);
238 ap[i].flag |= GCTL_PARAM_VALUEKERNEL;
245 gctl_copyout(struct gctl_req *req)
248 struct gctl_req_arg *ap;
254 for (i = 0; i < req->narg; i++, ap++) {
255 if (!(ap->flag & GCTL_PARAM_CHANGED))
257 error = copyout(ap->kvalue, ap->value, ap->len);
267 gctl_free(struct gctl_req *req)
271 sbuf_delete(req->serror);
272 if (req->arg == NULL)
274 for (i = 0; i < req->narg; i++) {
275 if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL)
276 g_free(req->arg[i].name);
277 if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) &&
279 g_free(req->arg[i].kvalue);
285 gctl_dump(struct gctl_req *req)
287 struct gctl_req_arg *ap;
291 printf("Dump of gctl request at %p:\n", req);
292 if (req->nerror > 0) {
293 printf(" nerror:\t%d\n", req->nerror);
294 if (sbuf_len(req->serror) > 0)
295 printf(" error:\t\"%s\"\n", sbuf_data(req->serror));
297 if (req->arg == NULL)
299 for (i = 0; i < req->narg; i++) {
301 if (!(ap->flag & GCTL_PARAM_NAMEKERNEL))
302 printf(" param:\t%d@%p", ap->nlen, ap->name);
304 printf(" param:\t\"%s\"", ap->name);
305 printf(" [%s%s%d] = ",
306 ap->flag & GCTL_PARAM_RD ? "R" : "",
307 ap->flag & GCTL_PARAM_WR ? "W" : "",
309 if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) {
310 printf(" =@ %p", ap->value);
311 } else if (ap->flag & GCTL_PARAM_ASCII) {
312 printf("\"%s\"", (char *)ap->kvalue);
313 } else if (ap->len > 0) {
314 for (j = 0; j < ap->len && j < 512; j++)
315 printf(" %02x", ((u_char *)ap->kvalue)[j]);
317 printf(" = %p", ap->kvalue);
324 gctl_set_param(struct gctl_req *req, const char *param, void const *ptr,
328 struct gctl_req_arg *ap;
330 for (i = 0; i < req->narg; i++) {
332 if (strcmp(param, ap->name))
334 if (!(ap->flag & GCTL_PARAM_WR))
336 ap->flag |= GCTL_PARAM_CHANGED;
338 bcopy(ptr, ap->kvalue, ap->len);
341 bcopy(ptr, ap->kvalue, len);
348 gctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr,
352 switch (gctl_set_param(req, param, ptr, len)) {
354 gctl_error(req, "No write access %s argument", param);
357 gctl_error(req, "Wrong length %s argument", param);
360 gctl_error(req, "Missing %s argument", param);
366 gctl_get_param(struct gctl_req *req, const char *param, int *len)
370 struct gctl_req_arg *ap;
372 for (i = 0; i < req->narg; i++) {
374 if (strcmp(param, ap->name))
376 if (!(ap->flag & GCTL_PARAM_RD))
387 gctl_get_asciiparam(struct gctl_req *req, const char *param)
391 struct gctl_req_arg *ap;
393 for (i = 0; i < req->narg; i++) {
395 if (strcmp(param, ap->name))
397 if (!(ap->flag & GCTL_PARAM_RD))
401 gctl_error(req, "No length argument (%s)", param);
404 if (p[ap->len - 1] != '\0') {
405 gctl_error(req, "Unterminated argument (%s)", param);
414 gctl_get_paraml_opt(struct gctl_req *req, const char *param, int len)
419 p = gctl_get_param(req, param, &i);
422 gctl_error(req, "Wrong length %s argument", param);
428 gctl_get_paraml(struct gctl_req *req, const char *param, int len)
432 p = gctl_get_paraml_opt(req, param, len);
434 gctl_error(req, "Missing %s argument", param);
439 gctl_get_class(struct gctl_req *req, char const *arg)
444 p = gctl_get_asciiparam(req, arg);
446 gctl_error(req, "Missing %s argument", arg);
449 LIST_FOREACH(cp, &g_classes, class) {
450 if (!strcmp(p, cp->name))
453 gctl_error(req, "Class not found: \"%s\"", p);
458 gctl_get_geom(struct gctl_req *req, struct g_class *mp, char const *arg)
464 p = gctl_get_asciiparam(req, arg);
466 gctl_error(req, "Missing %s argument", arg);
469 LIST_FOREACH(gp, &mp->geom, geom)
470 if (!strcmp(p, gp->name))
472 gctl_error(req, "Geom not found: \"%s\"", p);
477 gctl_get_provider(struct gctl_req *req, char const *arg)
480 struct g_provider *pp;
482 p = gctl_get_asciiparam(req, arg);
484 gctl_error(req, "Missing '%s' argument", arg);
487 pp = g_provider_by_name(p);
490 gctl_error(req, "Provider not found: \"%s\"", p);
495 g_ctl_req(void *arg, int flag __unused)
498 struct gctl_req *req;
503 mp = gctl_get_class(req, "class");
506 if (mp->ctlreq == NULL) {
507 gctl_error(req, "Class takes no requests");
510 verb = gctl_get_param(req, "verb", NULL);
512 gctl_error(req, "Verb missing");
515 mp->ctlreq(req, mp, verb);
520 g_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
522 struct gctl_req *req;
527 /* It is an error if we cannot return an error text */
530 if (!useracc(req->error, req->lerror, VM_PROT_WRITE))
533 req->serror = sbuf_new_auto();
534 /* Check the version */
535 if (req->version != GCTL_VERSION) {
536 gctl_error(req, "kernel and libgeom version mismatch.");
539 /* Get things on board */
542 if (g_debugflags & G_F_CTLDUMP)
546 g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL);
550 if (sbuf_done(req->serror)) {
551 copyout(sbuf_data(req->serror), req->error,
552 imin(req->lerror, sbuf_len(req->serror) + 1));
555 nerror = req->nerror;
561 g_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
567 error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td);