geom_ctl.c revision 115949
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 115949 2003-06-07 10:16:53Z 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); 105115949Sphk va_end(ap); 106115624Sphk sbuf_finish(req->serror); 107115624Sphk if (g_debugflags & G_F_CTLDUMP) 108115624Sphk printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror)); 109115624Sphk req->nerror = copyout(sbuf_data(req->serror), req->error, 110115624Sphk imin(req->lerror, sbuf_len(req->serror) + 1)); 111115624Sphk if (!req->nerror) 112115624Sphk req->nerror = EINVAL; 113115624Sphk return (req->nerror); 114112511Sphk} 115112511Sphk 116112511Sphk/* 117112511Sphk * Allocate space and copyin() something. 118112511Sphk * XXX: this should really be a standard function in the kernel. 119112511Sphk */ 120112511Sphkstatic void * 121115624Sphkgeom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len) 122112511Sphk{ 123112511Sphk void *ptr; 124112511Sphk 125112511Sphk ptr = g_malloc(len, M_WAITOK); 126112511Sphk if (ptr == NULL) 127115624Sphk req->nerror = ENOMEM; 128112511Sphk else 129115624Sphk req->nerror = copyin(uaddr, ptr, len); 130115624Sphk if (!req->nerror) 131112511Sphk return (ptr); 132112511Sphk if (ptr != NULL) 133112511Sphk g_free(ptr); 134112511Sphk return (NULL); 135112511Sphk} 136112511Sphk 137115624Sphkstatic void 138112709Sphkgctl_copyin(struct gctl_req *req) 139112511Sphk{ 140115624Sphk int error, i; 141112709Sphk struct gctl_req_arg *ap; 142112511Sphk char *p; 143112511Sphk 144115624Sphk ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap)); 145112709Sphk if (ap == NULL) { 146115624Sphk req->nerror = ENOMEM; 147115624Sphk req->arg = NULL; 148115624Sphk return; 149112709Sphk } 150112709Sphk 151115624Sphk /* Nothing have been copyin()'ed yet */ 152115624Sphk for (i = 0; i < req->narg; i++) { 153115624Sphk ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL); 154115624Sphk ap[i].flag &= ~GCTL_PARAM_CHANGED; 155115624Sphk ap[i].kvalue = NULL; 156115624Sphk } 157115624Sphk 158115624Sphk error = 0; 159115624Sphk for (i = 0; i < req->narg; i++) { 160112709Sphk if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) { 161115624Sphk error = gctl_error(req, 162115624Sphk "wrong param name length %d: %d", i, ap[i].nlen); 163112511Sphk break; 164112709Sphk } 165115624Sphk p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen); 166112709Sphk if (p == NULL) 167112511Sphk break; 168112709Sphk if (p[ap[i].nlen - 1] != '\0') { 169112709Sphk error = gctl_error(req, "unterminated param name"); 170112511Sphk g_free(p); 171112511Sphk break; 172112511Sphk } 173112709Sphk ap[i].name = p; 174115624Sphk ap[i].flag |= GCTL_PARAM_NAMEKERNEL; 175115624Sphk if (ap[i].len < 0) { 176115624Sphk error = gctl_error(req, "negative param length"); 177115624Sphk break; 178115624Sphk } 179115624Sphk if (ap[i].len == 0) { 180115624Sphk ap[i].kvalue = ap[i].value; 181115624Sphk ap[i].flag |= GCTL_PARAM_VALUEKERNEL; 182115624Sphk continue; 183115624Sphk } 184115624Sphk p = geom_alloc_copyin(req, ap[i].value, ap[i].len); 185115624Sphk if (p == NULL) 186115624Sphk break; 187115624Sphk if ((ap[i].flag & GCTL_PARAM_ASCII) && 188115624Sphk p[ap[i].len - 1] != '\0') { 189115624Sphk error = gctl_error(req, "unterminated param value"); 190115624Sphk g_free(p); 191115624Sphk break; 192115624Sphk } 193115624Sphk ap[i].kvalue = p; 194115624Sphk ap[i].flag |= GCTL_PARAM_VALUEKERNEL; 195112511Sphk } 196115624Sphk req->arg = ap; 197115624Sphk return; 198115624Sphk} 199115624Sphk 200115624Sphkstatic void 201115624Sphkgctl_copyout(struct gctl_req *req) 202115624Sphk{ 203115624Sphk int error, i; 204115624Sphk struct gctl_req_arg *ap; 205115624Sphk 206115624Sphk if (req->nerror) 207115624Sphk return; 208115624Sphk error = 0; 209115624Sphk ap = req->arg; 210115624Sphk for (i = 0; i < req->narg; i++, ap++) { 211115624Sphk if (!(ap->flag & GCTL_PARAM_CHANGED)) 212115624Sphk continue; 213115624Sphk error = copyout(ap->kvalue, ap->value, ap->len); 214115624Sphk if (!error) 215115624Sphk continue; 216115624Sphk req->nerror = error; 217115624Sphk return; 218112511Sphk } 219115624Sphk return; 220112511Sphk} 221112511Sphk 222112511Sphkstatic void 223114531Sphkgctl_free(struct gctl_req *req) 224114531Sphk{ 225114531Sphk int i; 226114531Sphk 227115624Sphk if (req->arg == NULL) 228115624Sphk return; 229114531Sphk for (i = 0; i < req->narg; i++) { 230115624Sphk if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL) 231114531Sphk g_free(req->arg[i].name); 232115624Sphk if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) && 233115624Sphk req->arg[i].len > 0) 234115624Sphk g_free(req->arg[i].kvalue); 235114531Sphk } 236114531Sphk g_free(req->arg); 237115624Sphk sbuf_delete(req->serror); 238114531Sphk} 239114531Sphk 240114531Sphkstatic void 241112709Sphkgctl_dump(struct gctl_req *req) 242112511Sphk{ 243112511Sphk u_int i; 244115624Sphk int j; 245112709Sphk struct gctl_req_arg *ap; 246112511Sphk 247115624Sphk printf("Dump of gctl request at %p:\n", req); 248115624Sphk if (req->nerror > 0) { 249115624Sphk printf(" nerror:\t%d\n", req->nerror); 250115624Sphk if (sbuf_len(req->serror) > 0) 251115624Sphk printf(" error:\t\"%s\"\n", sbuf_data(req->serror)); 252112511Sphk } 253112511Sphk for (i = 0; i < req->narg; i++) { 254112511Sphk ap = &req->arg[i]; 255115624Sphk if (!(ap->flag & GCTL_PARAM_NAMEKERNEL)) 256115624Sphk printf(" param:\t%d@%p", ap->nlen, ap->name); 257115624Sphk else 258115624Sphk printf(" param:\t\"%s\"", ap->name); 259112709Sphk printf(" [%s%s%d] = ", 260112709Sphk ap->flag & GCTL_PARAM_RD ? "R" : "", 261112709Sphk ap->flag & GCTL_PARAM_WR ? "W" : "", 262112709Sphk ap->len); 263115624Sphk if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) { 264115624Sphk printf(" =@ %p", ap->value); 265115624Sphk } else if (ap->flag & GCTL_PARAM_ASCII) { 266115624Sphk printf("\"%s\"", (char *)ap->kvalue); 267112511Sphk } else if (ap->len > 0) { 268112511Sphk for (j = 0; j < ap->len; j++) 269115624Sphk printf(" %02x", ((u_char *)ap->kvalue)[j]); 270112511Sphk } else { 271115624Sphk printf(" = %p", ap->kvalue); 272112511Sphk } 273112511Sphk printf("\n"); 274112511Sphk } 275112511Sphk} 276112511Sphk 277115624Sphkvoid 278115624Sphkgctl_set_param(struct gctl_req *req, const char *param, void const *ptr, int len) 279114670Sphk{ 280115624Sphk int i; 281114670Sphk struct gctl_req_arg *ap; 282114670Sphk 283114670Sphk for (i = 0; i < req->narg; i++) { 284114670Sphk ap = &req->arg[i]; 285114670Sphk if (strcmp(param, ap->name)) 286114670Sphk continue; 287114670Sphk if (!(ap->flag & GCTL_PARAM_WR)) { 288114670Sphk gctl_error(req, "No write access %s argument", param); 289115624Sphk return; 290114670Sphk } 291115624Sphk if (ap->len < len) { 292114670Sphk gctl_error(req, "Wrong length %s argument", param); 293115624Sphk return; 294114670Sphk } 295115624Sphk bcopy(ptr, ap->kvalue, len); 296115624Sphk ap->flag |= GCTL_PARAM_CHANGED; 297115624Sphk return; 298114670Sphk } 299114670Sphk gctl_error(req, "Missing %s argument", param); 300115624Sphk return; 301114670Sphk} 302114670Sphk 303112709Sphkvoid * 304112709Sphkgctl_get_param(struct gctl_req *req, const char *param, int *len) 305112709Sphk{ 306115624Sphk int i; 307112709Sphk void *p; 308112709Sphk struct gctl_req_arg *ap; 309112709Sphk 310112709Sphk for (i = 0; i < req->narg; i++) { 311112709Sphk ap = &req->arg[i]; 312112709Sphk if (strcmp(param, ap->name)) 313112709Sphk continue; 314112709Sphk if (!(ap->flag & GCTL_PARAM_RD)) 315112709Sphk continue; 316115624Sphk p = ap->kvalue; 317112709Sphk if (len != NULL) 318115624Sphk *len = ap->len; 319112709Sphk return (p); 320112709Sphk } 321112709Sphk return (NULL); 322112709Sphk} 323112709Sphk 324115624Sphkchar const * 325115624Sphkgctl_get_asciiparam(struct gctl_req *req, const char *param) 326115624Sphk{ 327115624Sphk int i; 328115624Sphk char const *p; 329115624Sphk struct gctl_req_arg *ap; 330115624Sphk 331115624Sphk for (i = 0; i < req->narg; i++) { 332115624Sphk ap = &req->arg[i]; 333115624Sphk if (strcmp(param, ap->name)) 334115624Sphk continue; 335115624Sphk if (!(ap->flag & GCTL_PARAM_RD)) 336115624Sphk continue; 337115624Sphk p = ap->kvalue; 338115624Sphk if (ap->len < 1) { 339115624Sphk gctl_error(req, "No length argument (%s)", param); 340115624Sphk return (NULL); 341115624Sphk } 342115624Sphk if (p[ap->len - 1] != '\0') { 343115624Sphk gctl_error(req, "Unterminated argument (%s)", param); 344115624Sphk return (NULL); 345115624Sphk } 346115624Sphk return (p); 347115624Sphk } 348115624Sphk return (NULL); 349115624Sphk} 350115624Sphk 351113893Sphkvoid * 352113893Sphkgctl_get_paraml(struct gctl_req *req, const char *param, int len) 353113893Sphk{ 354113893Sphk int i; 355113893Sphk void *p; 356113893Sphk 357113893Sphk p = gctl_get_param(req, param, &i); 358113893Sphk if (p == NULL) 359113893Sphk gctl_error(req, "Missing %s argument", param); 360113893Sphk else if (i != len) { 361113893Sphk p = NULL; 362113893Sphk gctl_error(req, "Wrong length %s argument", param); 363113893Sphk } 364113893Sphk return (p); 365113893Sphk} 366113893Sphk 367115624Sphkstruct g_class * 368115624Sphkgctl_get_class(struct gctl_req *req, char const *arg) 369112709Sphk{ 370115624Sphk char const *p; 371112709Sphk struct g_class *cp; 372112709Sphk 373115624Sphk p = gctl_get_asciiparam(req, arg); 374112709Sphk if (p == NULL) 375112709Sphk return (NULL); 376112709Sphk LIST_FOREACH(cp, &g_classes, class) { 377115624Sphk if (!strcmp(p, cp->name)) 378112709Sphk return (cp); 379112709Sphk } 380112709Sphk gctl_error(req, "Class not found"); 381112709Sphk return (NULL); 382112709Sphk} 383112709Sphk 384115624Sphkstruct g_geom * 385115624Sphkgctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg) 386112709Sphk{ 387115624Sphk char const *p; 388112709Sphk struct g_class *mp; 389112709Sphk struct g_geom *gp; 390112709Sphk 391115624Sphk p = gctl_get_asciiparam(req, arg); 392112709Sphk if (p == NULL) 393112709Sphk return (NULL); 394112709Sphk LIST_FOREACH(mp, &g_classes, class) { 395112709Sphk if (mpr != NULL && mpr != mp) 396112709Sphk continue; 397112709Sphk LIST_FOREACH(gp, &mp->geom, geom) { 398115624Sphk if (!strcmp(p, gp->name)) 399112709Sphk return (gp); 400112709Sphk } 401112709Sphk } 402112709Sphk gctl_error(req, "Geom not found"); 403112709Sphk return (NULL); 404112709Sphk} 405112709Sphk 406115624Sphkstruct g_provider * 407115624Sphkgctl_get_provider(struct gctl_req *req, char const *arg) 408112709Sphk{ 409115624Sphk char const *p; 410112709Sphk struct g_provider *pp; 411112709Sphk 412115624Sphk p = gctl_get_asciiparam(req, arg); 413112709Sphk if (p == NULL) 414112709Sphk return (NULL); 415115850Sphk pp = g_provider_by_name(p); 416115850Sphk if (pp != NULL) 417115850Sphk return (pp); 418112709Sphk gctl_error(req, "Provider not found"); 419112709Sphk return (NULL); 420112709Sphk} 421112709Sphk 422115624Sphkstatic void 423115624Sphkg_ctl_req(void *arg, int flag __unused) 424112709Sphk{ 425112709Sphk struct g_class *mp; 426115624Sphk struct gctl_req *req; 427115624Sphk char const *verb; 428112709Sphk 429112709Sphk g_topology_assert(); 430115624Sphk req = arg; 431115624Sphk mp = gctl_get_class(req, "class"); 432115726Sphk if (mp == NULL) { 433115726Sphk gctl_error(req, "Class not found"); 434115624Sphk return; 435115726Sphk } 436115624Sphk verb = gctl_get_param(req, "verb", NULL); 437115624Sphk if (mp->ctlreq == NULL) 438115624Sphk gctl_error(req, "Class takes no requests"); 439115624Sphk else 440115624Sphk mp->ctlreq(req, mp, verb); 441112709Sphk g_topology_assert(); 442112709Sphk} 443112709Sphk 444112709Sphk 445113876Sphkstatic int 446112511Sphkg_ctl_ioctl_ctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 447112511Sphk{ 448112709Sphk struct gctl_req *req; 449112511Sphk 450112511Sphk req = (void *)data; 451115624Sphk req->nerror = 0; 452115624Sphk req->serror = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 453112709Sphk /* It is an error if we cannot return an error text */ 454115624Sphk if (req->lerror < 2) 455112511Sphk return (EINVAL); 456112709Sphk if (!useracc(req->error, req->lerror, VM_PROT_WRITE)) 457112709Sphk return (EINVAL); 458112709Sphk 459112709Sphk /* Check the version */ 460112709Sphk if (req->version != GCTL_VERSION) 461112709Sphk return (gctl_error(req, 462112709Sphk "kernel and libgeom version mismatch.")); 463112709Sphk 464112709Sphk /* Get things on board */ 465115624Sphk gctl_copyin(req); 466112709Sphk 467112876Sphk if (g_debugflags & G_F_CTLDUMP) 468112876Sphk gctl_dump(req); 469115624Sphk 470115624Sphk if (!req->nerror) { 471115624Sphk g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL); 472115624Sphk gctl_copyout(req); 473112709Sphk } 474115624Sphk 475114531Sphk gctl_free(req); 476115624Sphk return (req->nerror); 477112511Sphk} 478112511Sphk 479112511Sphkstatic int 480105068Sphkg_ctl_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 481105068Sphk{ 482105068Sphk int error; 483105068Sphk 484105068Sphk switch(cmd) { 485112511Sphk case GEOM_CTL: 486112511Sphk error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td); 487112511Sphk break; 488105068Sphk default: 489115624Sphk error = ENOIOCTL; 490105068Sphk break; 491105068Sphk } 492105068Sphk return (error); 493105068Sphk 494105068Sphk} 495