geom_ctl.c revision 117150
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 36116196Sobrien#include <sys/cdefs.h> 37116196Sobrien__FBSDID("$FreeBSD: head/sys/geom/geom_ctl.c 117150 2003-07-02 08:07:07Z phk $"); 38116196Sobrien 39105068Sphk#include "opt_geom.h" 40105068Sphk 41105068Sphk#include <sys/param.h> 42105068Sphk#include <sys/systm.h> 43105068Sphk#include <sys/kernel.h> 44105068Sphk#include <sys/sysctl.h> 45105068Sphk#include <sys/bio.h> 46105068Sphk#include <sys/conf.h> 47105068Sphk#include <sys/disk.h> 48105068Sphk#include <sys/malloc.h> 49105068Sphk#include <sys/sysctl.h> 50113892Sphk#include <sys/sbuf.h> 51105068Sphk 52105068Sphk#include <sys/lock.h> 53105068Sphk#include <sys/mutex.h> 54112511Sphk 55112511Sphk#include <vm/vm.h> 56112511Sphk#include <vm/vm_extern.h> 57112511Sphk 58105068Sphk#include <geom/geom.h> 59105068Sphk#include <geom/geom_int.h> 60112709Sphk#define GCTL_TABLE 1 61112511Sphk#include <geom/geom_ctl.h> 62105068Sphk 63113892Sphk#include <machine/stdarg.h> 64113892Sphk 65105068Sphkstatic d_ioctl_t g_ctl_ioctl; 66105068Sphk 67112534Sphkstatic struct cdevsw g_ctl_cdevsw = { 68112534Sphk .d_open = nullopen, 69112534Sphk .d_close = nullclose, 70112534Sphk .d_ioctl = g_ctl_ioctl, 71112534Sphk .d_name = "g_ctl", 72105068Sphk}; 73105068Sphk 74112534Sphkvoid 75105068Sphkg_ctl_init(void) 76105068Sphk{ 77105068Sphk 78112534Sphk make_dev(&g_ctl_cdevsw, 0, 79112534Sphk UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL); 80112709Sphk KASSERT(GCTL_PARAM_RD == VM_PROT_READ, 81112709Sphk ("GCTL_PARAM_RD != VM_PROT_READ")); 82112709Sphk KASSERT(GCTL_PARAM_WR == VM_PROT_WRITE, 83112709Sphk ("GCTL_PARAM_WR != VM_PROT_WRITE")); 84105068Sphk} 85105068Sphk 86112511Sphk/* 87112511Sphk * Report an error back to the user in ascii format. Return whatever copyout 88112511Sphk * returned, or EINVAL if it succeeded. 89112511Sphk * XXX: should not be static. 90112511Sphk * XXX: should take printf like args. 91112511Sphk */ 92112709Sphkint 93113892Sphkgctl_error(struct gctl_req *req, const char *fmt, ...) 94112511Sphk{ 95113892Sphk va_list ap; 96112511Sphk 97115624Sphk if (req == NULL) 98115624Sphk return (EINVAL); 99115624Sphk 100115624Sphk /* We only record the first error */ 101115624Sphk if (req->nerror) 102115624Sphk return (req->nerror); 103115624Sphk 104115624Sphk va_start(ap, fmt); 105115624Sphk sbuf_vprintf(req->serror, fmt, ap); 106115949Sphk va_end(ap); 107115624Sphk sbuf_finish(req->serror); 108115624Sphk if (g_debugflags & G_F_CTLDUMP) 109115624Sphk printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror)); 110115624Sphk req->nerror = copyout(sbuf_data(req->serror), req->error, 111115624Sphk imin(req->lerror, sbuf_len(req->serror) + 1)); 112115624Sphk if (!req->nerror) 113115624Sphk req->nerror = EINVAL; 114115624Sphk return (req->nerror); 115112511Sphk} 116112511Sphk 117112511Sphk/* 118112511Sphk * Allocate space and copyin() something. 119112511Sphk * XXX: this should really be a standard function in the kernel. 120112511Sphk */ 121112511Sphkstatic void * 122115624Sphkgeom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len) 123112511Sphk{ 124112511Sphk void *ptr; 125112511Sphk 126112511Sphk ptr = g_malloc(len, M_WAITOK); 127112511Sphk if (ptr == NULL) 128115624Sphk req->nerror = ENOMEM; 129112511Sphk else 130115624Sphk req->nerror = copyin(uaddr, ptr, len); 131115624Sphk if (!req->nerror) 132112511Sphk return (ptr); 133112511Sphk if (ptr != NULL) 134112511Sphk g_free(ptr); 135112511Sphk return (NULL); 136112511Sphk} 137112511Sphk 138115624Sphkstatic void 139112709Sphkgctl_copyin(struct gctl_req *req) 140112511Sphk{ 141115624Sphk int error, i; 142112709Sphk struct gctl_req_arg *ap; 143112511Sphk char *p; 144112511Sphk 145115624Sphk ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap)); 146112709Sphk if (ap == NULL) { 147115624Sphk req->nerror = ENOMEM; 148115624Sphk req->arg = NULL; 149115624Sphk return; 150112709Sphk } 151112709Sphk 152115624Sphk /* Nothing have been copyin()'ed yet */ 153115624Sphk for (i = 0; i < req->narg; i++) { 154115624Sphk ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL); 155115624Sphk ap[i].flag &= ~GCTL_PARAM_CHANGED; 156115624Sphk ap[i].kvalue = NULL; 157115624Sphk } 158115624Sphk 159115624Sphk error = 0; 160115624Sphk for (i = 0; i < req->narg; i++) { 161112709Sphk if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) { 162115624Sphk error = gctl_error(req, 163115624Sphk "wrong param name length %d: %d", i, ap[i].nlen); 164112511Sphk break; 165112709Sphk } 166115624Sphk p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen); 167112709Sphk if (p == NULL) 168112511Sphk break; 169112709Sphk if (p[ap[i].nlen - 1] != '\0') { 170112709Sphk error = gctl_error(req, "unterminated param name"); 171112511Sphk g_free(p); 172112511Sphk break; 173112511Sphk } 174112709Sphk ap[i].name = p; 175115624Sphk ap[i].flag |= GCTL_PARAM_NAMEKERNEL; 176115624Sphk if (ap[i].len < 0) { 177115624Sphk error = gctl_error(req, "negative param length"); 178115624Sphk break; 179115624Sphk } 180115624Sphk if (ap[i].len == 0) { 181115624Sphk ap[i].kvalue = ap[i].value; 182115624Sphk ap[i].flag |= GCTL_PARAM_VALUEKERNEL; 183115624Sphk continue; 184115624Sphk } 185115624Sphk p = geom_alloc_copyin(req, ap[i].value, ap[i].len); 186115624Sphk if (p == NULL) 187115624Sphk break; 188115624Sphk if ((ap[i].flag & GCTL_PARAM_ASCII) && 189115624Sphk p[ap[i].len - 1] != '\0') { 190115624Sphk error = gctl_error(req, "unterminated param value"); 191115624Sphk g_free(p); 192115624Sphk break; 193115624Sphk } 194115624Sphk ap[i].kvalue = p; 195115624Sphk ap[i].flag |= GCTL_PARAM_VALUEKERNEL; 196112511Sphk } 197115624Sphk req->arg = ap; 198115624Sphk return; 199115624Sphk} 200115624Sphk 201115624Sphkstatic void 202115624Sphkgctl_copyout(struct gctl_req *req) 203115624Sphk{ 204115624Sphk int error, i; 205115624Sphk struct gctl_req_arg *ap; 206115624Sphk 207115624Sphk if (req->nerror) 208115624Sphk return; 209115624Sphk error = 0; 210115624Sphk ap = req->arg; 211115624Sphk for (i = 0; i < req->narg; i++, ap++) { 212115624Sphk if (!(ap->flag & GCTL_PARAM_CHANGED)) 213115624Sphk continue; 214115624Sphk error = copyout(ap->kvalue, ap->value, ap->len); 215115624Sphk if (!error) 216115624Sphk continue; 217115624Sphk req->nerror = error; 218115624Sphk return; 219112511Sphk } 220115624Sphk return; 221112511Sphk} 222112511Sphk 223112511Sphkstatic void 224114531Sphkgctl_free(struct gctl_req *req) 225114531Sphk{ 226114531Sphk int i; 227114531Sphk 228115624Sphk if (req->arg == NULL) 229115624Sphk return; 230114531Sphk for (i = 0; i < req->narg; i++) { 231115624Sphk if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL) 232114531Sphk g_free(req->arg[i].name); 233115624Sphk if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) && 234115624Sphk req->arg[i].len > 0) 235115624Sphk g_free(req->arg[i].kvalue); 236114531Sphk } 237114531Sphk g_free(req->arg); 238115624Sphk sbuf_delete(req->serror); 239114531Sphk} 240114531Sphk 241114531Sphkstatic void 242112709Sphkgctl_dump(struct gctl_req *req) 243112511Sphk{ 244112511Sphk u_int i; 245115624Sphk int j; 246112709Sphk struct gctl_req_arg *ap; 247112511Sphk 248115624Sphk printf("Dump of gctl request at %p:\n", req); 249115624Sphk if (req->nerror > 0) { 250115624Sphk printf(" nerror:\t%d\n", req->nerror); 251115624Sphk if (sbuf_len(req->serror) > 0) 252115624Sphk printf(" error:\t\"%s\"\n", sbuf_data(req->serror)); 253112511Sphk } 254112511Sphk for (i = 0; i < req->narg; i++) { 255112511Sphk ap = &req->arg[i]; 256115624Sphk if (!(ap->flag & GCTL_PARAM_NAMEKERNEL)) 257115624Sphk printf(" param:\t%d@%p", ap->nlen, ap->name); 258115624Sphk else 259115624Sphk printf(" param:\t\"%s\"", ap->name); 260112709Sphk printf(" [%s%s%d] = ", 261112709Sphk ap->flag & GCTL_PARAM_RD ? "R" : "", 262112709Sphk ap->flag & GCTL_PARAM_WR ? "W" : "", 263112709Sphk ap->len); 264115624Sphk if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) { 265115624Sphk printf(" =@ %p", ap->value); 266115624Sphk } else if (ap->flag & GCTL_PARAM_ASCII) { 267115624Sphk printf("\"%s\"", (char *)ap->kvalue); 268112511Sphk } else if (ap->len > 0) { 269117150Sphk for (j = 0; j < ap->len && j < 512; j++) 270115624Sphk printf(" %02x", ((u_char *)ap->kvalue)[j]); 271112511Sphk } else { 272115624Sphk printf(" = %p", ap->kvalue); 273112511Sphk } 274112511Sphk printf("\n"); 275112511Sphk } 276112511Sphk} 277112511Sphk 278115624Sphkvoid 279115624Sphkgctl_set_param(struct gctl_req *req, const char *param, void const *ptr, int len) 280114670Sphk{ 281115624Sphk int i; 282114670Sphk struct gctl_req_arg *ap; 283114670Sphk 284114670Sphk for (i = 0; i < req->narg; i++) { 285114670Sphk ap = &req->arg[i]; 286114670Sphk if (strcmp(param, ap->name)) 287114670Sphk continue; 288114670Sphk if (!(ap->flag & GCTL_PARAM_WR)) { 289114670Sphk gctl_error(req, "No write access %s argument", param); 290115624Sphk return; 291114670Sphk } 292115624Sphk if (ap->len < len) { 293114670Sphk gctl_error(req, "Wrong length %s argument", param); 294115624Sphk return; 295114670Sphk } 296115624Sphk bcopy(ptr, ap->kvalue, len); 297115624Sphk ap->flag |= GCTL_PARAM_CHANGED; 298115624Sphk return; 299114670Sphk } 300114670Sphk gctl_error(req, "Missing %s argument", param); 301115624Sphk return; 302114670Sphk} 303114670Sphk 304112709Sphkvoid * 305112709Sphkgctl_get_param(struct gctl_req *req, const char *param, int *len) 306112709Sphk{ 307115624Sphk int i; 308112709Sphk void *p; 309112709Sphk struct gctl_req_arg *ap; 310112709Sphk 311112709Sphk for (i = 0; i < req->narg; i++) { 312112709Sphk ap = &req->arg[i]; 313112709Sphk if (strcmp(param, ap->name)) 314112709Sphk continue; 315112709Sphk if (!(ap->flag & GCTL_PARAM_RD)) 316112709Sphk continue; 317115624Sphk p = ap->kvalue; 318112709Sphk if (len != NULL) 319115624Sphk *len = ap->len; 320112709Sphk return (p); 321112709Sphk } 322112709Sphk return (NULL); 323112709Sphk} 324112709Sphk 325115624Sphkchar const * 326115624Sphkgctl_get_asciiparam(struct gctl_req *req, const char *param) 327115624Sphk{ 328115624Sphk int i; 329115624Sphk char const *p; 330115624Sphk struct gctl_req_arg *ap; 331115624Sphk 332115624Sphk for (i = 0; i < req->narg; i++) { 333115624Sphk ap = &req->arg[i]; 334115624Sphk if (strcmp(param, ap->name)) 335115624Sphk continue; 336115624Sphk if (!(ap->flag & GCTL_PARAM_RD)) 337115624Sphk continue; 338115624Sphk p = ap->kvalue; 339115624Sphk if (ap->len < 1) { 340115624Sphk gctl_error(req, "No length argument (%s)", param); 341115624Sphk return (NULL); 342115624Sphk } 343115624Sphk if (p[ap->len - 1] != '\0') { 344115624Sphk gctl_error(req, "Unterminated argument (%s)", param); 345115624Sphk return (NULL); 346115624Sphk } 347115624Sphk return (p); 348115624Sphk } 349115624Sphk return (NULL); 350115624Sphk} 351115624Sphk 352113893Sphkvoid * 353113893Sphkgctl_get_paraml(struct gctl_req *req, const char *param, int len) 354113893Sphk{ 355113893Sphk int i; 356113893Sphk void *p; 357113893Sphk 358113893Sphk p = gctl_get_param(req, param, &i); 359113893Sphk if (p == NULL) 360113893Sphk gctl_error(req, "Missing %s argument", param); 361113893Sphk else if (i != len) { 362113893Sphk p = NULL; 363113893Sphk gctl_error(req, "Wrong length %s argument", param); 364113893Sphk } 365113893Sphk return (p); 366113893Sphk} 367113893Sphk 368115624Sphkstruct g_class * 369115624Sphkgctl_get_class(struct gctl_req *req, char const *arg) 370112709Sphk{ 371115624Sphk char const *p; 372112709Sphk struct g_class *cp; 373112709Sphk 374115624Sphk p = gctl_get_asciiparam(req, arg); 375112709Sphk if (p == NULL) 376112709Sphk return (NULL); 377112709Sphk LIST_FOREACH(cp, &g_classes, class) { 378115624Sphk if (!strcmp(p, cp->name)) 379112709Sphk return (cp); 380112709Sphk } 381112709Sphk gctl_error(req, "Class not found"); 382112709Sphk return (NULL); 383112709Sphk} 384112709Sphk 385115624Sphkstruct g_geom * 386115624Sphkgctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg) 387112709Sphk{ 388115624Sphk char const *p; 389112709Sphk struct g_class *mp; 390112709Sphk struct g_geom *gp; 391112709Sphk 392115624Sphk p = gctl_get_asciiparam(req, arg); 393115958Sphk if (p != NULL) { 394115958Sphk LIST_FOREACH(mp, &g_classes, class) { 395115958Sphk if (mpr != NULL && mpr != mp) 396115958Sphk continue; 397115958Sphk LIST_FOREACH(gp, &mp->geom, geom) { 398115958Sphk if (!strcmp(p, gp->name)) 399115958Sphk return (gp); 400115958Sphk } 401112709Sphk } 402112709Sphk } 403112709Sphk gctl_error(req, "Geom not found"); 404112709Sphk return (NULL); 405112709Sphk} 406112709Sphk 407115624Sphkstruct g_provider * 408115624Sphkgctl_get_provider(struct gctl_req *req, char const *arg) 409112709Sphk{ 410115624Sphk char const *p; 411112709Sphk struct g_provider *pp; 412112709Sphk 413115624Sphk p = gctl_get_asciiparam(req, arg); 414112709Sphk if (p == NULL) 415112709Sphk return (NULL); 416115850Sphk pp = g_provider_by_name(p); 417115850Sphk if (pp != NULL) 418115850Sphk return (pp); 419112709Sphk gctl_error(req, "Provider not found"); 420112709Sphk return (NULL); 421112709Sphk} 422112709Sphk 423115624Sphkstatic void 424115624Sphkg_ctl_req(void *arg, int flag __unused) 425112709Sphk{ 426112709Sphk struct g_class *mp; 427115624Sphk struct gctl_req *req; 428115624Sphk char const *verb; 429112709Sphk 430112709Sphk g_topology_assert(); 431115624Sphk req = arg; 432115624Sphk mp = gctl_get_class(req, "class"); 433115726Sphk if (mp == NULL) { 434115726Sphk gctl_error(req, "Class not found"); 435115624Sphk return; 436115726Sphk } 437115624Sphk verb = gctl_get_param(req, "verb", NULL); 438115624Sphk if (mp->ctlreq == NULL) 439115624Sphk gctl_error(req, "Class takes no requests"); 440115624Sphk else 441115624Sphk mp->ctlreq(req, mp, verb); 442112709Sphk g_topology_assert(); 443112709Sphk} 444112709Sphk 445112709Sphk 446113876Sphkstatic int 447112511Sphkg_ctl_ioctl_ctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 448112511Sphk{ 449112709Sphk struct gctl_req *req; 450112511Sphk 451112511Sphk req = (void *)data; 452115624Sphk req->nerror = 0; 453115624Sphk req->serror = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 454112709Sphk /* It is an error if we cannot return an error text */ 455115624Sphk if (req->lerror < 2) 456112511Sphk return (EINVAL); 457112709Sphk if (!useracc(req->error, req->lerror, VM_PROT_WRITE)) 458112709Sphk return (EINVAL); 459112709Sphk 460112709Sphk /* Check the version */ 461112709Sphk if (req->version != GCTL_VERSION) 462112709Sphk return (gctl_error(req, 463112709Sphk "kernel and libgeom version mismatch.")); 464112709Sphk 465112709Sphk /* Get things on board */ 466115624Sphk gctl_copyin(req); 467112709Sphk 468112876Sphk if (g_debugflags & G_F_CTLDUMP) 469112876Sphk gctl_dump(req); 470115624Sphk 471115624Sphk if (!req->nerror) { 472115624Sphk g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL); 473115624Sphk gctl_copyout(req); 474112709Sphk } 475115624Sphk 476117150Sphk g_waitidle(); 477114531Sphk gctl_free(req); 478115624Sphk return (req->nerror); 479112511Sphk} 480112511Sphk 481112511Sphkstatic int 482105068Sphkg_ctl_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 483105068Sphk{ 484105068Sphk int error; 485105068Sphk 486105068Sphk switch(cmd) { 487112511Sphk case GEOM_CTL: 488112511Sphk error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td); 489112511Sphk break; 490105068Sphk default: 491115624Sphk error = ENOIOCTL; 492105068Sphk break; 493105068Sphk } 494105068Sphk return (error); 495105068Sphk 496105068Sphk} 497