geom_ctl.c revision 144789
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 144789 2005-04-08 09:28:08Z pjd $"); 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 = { 68126080Sphk .d_version = D_VERSION, 69126080Sphk .d_flags = D_NEEDGIANT, 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 */ 101144789Spjd if (sbuf_done(req->serror)) { 102144789Spjd if (!req->nerror) 103144789Spjd req->nerror = EEXIST; 104144789Spjd } 105115624Sphk if (req->nerror) 106115624Sphk return (req->nerror); 107115624Sphk 108115624Sphk va_start(ap, fmt); 109115624Sphk sbuf_vprintf(req->serror, fmt, ap); 110115949Sphk va_end(ap); 111115624Sphk sbuf_finish(req->serror); 112115624Sphk if (g_debugflags & G_F_CTLDUMP) 113115624Sphk printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror)); 114144789Spjd return (0); 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; 176140367Sphk if (ap[i].len <= 0) { 177115624Sphk error = gctl_error(req, "negative param length"); 178115624Sphk break; 179115624Sphk } 180115624Sphk p = geom_alloc_copyin(req, ap[i].value, ap[i].len); 181115624Sphk if (p == NULL) 182115624Sphk break; 183115624Sphk if ((ap[i].flag & GCTL_PARAM_ASCII) && 184115624Sphk p[ap[i].len - 1] != '\0') { 185115624Sphk error = gctl_error(req, "unterminated param value"); 186115624Sphk g_free(p); 187115624Sphk break; 188115624Sphk } 189115624Sphk ap[i].kvalue = p; 190115624Sphk ap[i].flag |= GCTL_PARAM_VALUEKERNEL; 191112511Sphk } 192115624Sphk req->arg = ap; 193115624Sphk return; 194115624Sphk} 195115624Sphk 196115624Sphkstatic void 197115624Sphkgctl_copyout(struct gctl_req *req) 198115624Sphk{ 199115624Sphk int error, i; 200115624Sphk struct gctl_req_arg *ap; 201115624Sphk 202115624Sphk if (req->nerror) 203115624Sphk return; 204115624Sphk error = 0; 205115624Sphk ap = req->arg; 206115624Sphk for (i = 0; i < req->narg; i++, ap++) { 207115624Sphk if (!(ap->flag & GCTL_PARAM_CHANGED)) 208115624Sphk continue; 209115624Sphk error = copyout(ap->kvalue, ap->value, ap->len); 210115624Sphk if (!error) 211115624Sphk continue; 212115624Sphk req->nerror = error; 213115624Sphk return; 214112511Sphk } 215115624Sphk return; 216112511Sphk} 217112511Sphk 218112511Sphkstatic void 219114531Sphkgctl_free(struct gctl_req *req) 220114531Sphk{ 221114531Sphk int i; 222114531Sphk 223115624Sphk if (req->arg == NULL) 224115624Sphk return; 225114531Sphk for (i = 0; i < req->narg; i++) { 226115624Sphk if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL) 227114531Sphk g_free(req->arg[i].name); 228115624Sphk if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) && 229115624Sphk req->arg[i].len > 0) 230115624Sphk g_free(req->arg[i].kvalue); 231114531Sphk } 232114531Sphk g_free(req->arg); 233115624Sphk sbuf_delete(req->serror); 234114531Sphk} 235114531Sphk 236114531Sphkstatic void 237112709Sphkgctl_dump(struct gctl_req *req) 238112511Sphk{ 239112511Sphk u_int i; 240115624Sphk int j; 241112709Sphk struct gctl_req_arg *ap; 242112511Sphk 243115624Sphk printf("Dump of gctl request at %p:\n", req); 244115624Sphk if (req->nerror > 0) { 245115624Sphk printf(" nerror:\t%d\n", req->nerror); 246115624Sphk if (sbuf_len(req->serror) > 0) 247115624Sphk printf(" error:\t\"%s\"\n", sbuf_data(req->serror)); 248112511Sphk } 249112511Sphk for (i = 0; i < req->narg; i++) { 250112511Sphk ap = &req->arg[i]; 251115624Sphk if (!(ap->flag & GCTL_PARAM_NAMEKERNEL)) 252115624Sphk printf(" param:\t%d@%p", ap->nlen, ap->name); 253115624Sphk else 254115624Sphk printf(" param:\t\"%s\"", ap->name); 255112709Sphk printf(" [%s%s%d] = ", 256112709Sphk ap->flag & GCTL_PARAM_RD ? "R" : "", 257112709Sphk ap->flag & GCTL_PARAM_WR ? "W" : "", 258112709Sphk ap->len); 259115624Sphk if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) { 260115624Sphk printf(" =@ %p", ap->value); 261115624Sphk } else if (ap->flag & GCTL_PARAM_ASCII) { 262115624Sphk printf("\"%s\"", (char *)ap->kvalue); 263112511Sphk } else if (ap->len > 0) { 264117150Sphk for (j = 0; j < ap->len && j < 512; j++) 265115624Sphk printf(" %02x", ((u_char *)ap->kvalue)[j]); 266112511Sphk } else { 267115624Sphk printf(" = %p", ap->kvalue); 268112511Sphk } 269112511Sphk printf("\n"); 270112511Sphk } 271112511Sphk} 272112511Sphk 273115624Sphkvoid 274115624Sphkgctl_set_param(struct gctl_req *req, const char *param, void const *ptr, int len) 275114670Sphk{ 276115624Sphk int i; 277114670Sphk struct gctl_req_arg *ap; 278114670Sphk 279114670Sphk for (i = 0; i < req->narg; i++) { 280114670Sphk ap = &req->arg[i]; 281114670Sphk if (strcmp(param, ap->name)) 282114670Sphk continue; 283114670Sphk if (!(ap->flag & GCTL_PARAM_WR)) { 284114670Sphk gctl_error(req, "No write access %s argument", param); 285115624Sphk return; 286114670Sphk } 287115624Sphk if (ap->len < len) { 288114670Sphk gctl_error(req, "Wrong length %s argument", param); 289115624Sphk return; 290114670Sphk } 291115624Sphk bcopy(ptr, ap->kvalue, len); 292115624Sphk ap->flag |= GCTL_PARAM_CHANGED; 293115624Sphk return; 294114670Sphk } 295114670Sphk gctl_error(req, "Missing %s argument", param); 296115624Sphk return; 297114670Sphk} 298114670Sphk 299112709Sphkvoid * 300112709Sphkgctl_get_param(struct gctl_req *req, const char *param, int *len) 301112709Sphk{ 302115624Sphk int i; 303112709Sphk void *p; 304112709Sphk struct gctl_req_arg *ap; 305112709Sphk 306112709Sphk for (i = 0; i < req->narg; i++) { 307112709Sphk ap = &req->arg[i]; 308112709Sphk if (strcmp(param, ap->name)) 309112709Sphk continue; 310112709Sphk if (!(ap->flag & GCTL_PARAM_RD)) 311112709Sphk continue; 312115624Sphk p = ap->kvalue; 313112709Sphk if (len != NULL) 314115624Sphk *len = ap->len; 315112709Sphk return (p); 316112709Sphk } 317112709Sphk return (NULL); 318112709Sphk} 319112709Sphk 320115624Sphkchar const * 321115624Sphkgctl_get_asciiparam(struct gctl_req *req, const char *param) 322115624Sphk{ 323115624Sphk int i; 324115624Sphk char const *p; 325115624Sphk struct gctl_req_arg *ap; 326115624Sphk 327115624Sphk for (i = 0; i < req->narg; i++) { 328115624Sphk ap = &req->arg[i]; 329115624Sphk if (strcmp(param, ap->name)) 330115624Sphk continue; 331115624Sphk if (!(ap->flag & GCTL_PARAM_RD)) 332115624Sphk continue; 333115624Sphk p = ap->kvalue; 334115624Sphk if (ap->len < 1) { 335115624Sphk gctl_error(req, "No length argument (%s)", param); 336115624Sphk return (NULL); 337115624Sphk } 338115624Sphk if (p[ap->len - 1] != '\0') { 339115624Sphk gctl_error(req, "Unterminated argument (%s)", param); 340115624Sphk return (NULL); 341115624Sphk } 342115624Sphk return (p); 343115624Sphk } 344115624Sphk return (NULL); 345115624Sphk} 346115624Sphk 347113893Sphkvoid * 348113893Sphkgctl_get_paraml(struct gctl_req *req, const char *param, int len) 349113893Sphk{ 350113893Sphk int i; 351113893Sphk void *p; 352113893Sphk 353113893Sphk p = gctl_get_param(req, param, &i); 354113893Sphk if (p == NULL) 355113893Sphk gctl_error(req, "Missing %s argument", param); 356113893Sphk else if (i != len) { 357113893Sphk p = NULL; 358113893Sphk gctl_error(req, "Wrong length %s argument", param); 359113893Sphk } 360113893Sphk return (p); 361113893Sphk} 362113893Sphk 363115624Sphkstruct g_class * 364115624Sphkgctl_get_class(struct gctl_req *req, char const *arg) 365112709Sphk{ 366115624Sphk char const *p; 367112709Sphk struct g_class *cp; 368112709Sphk 369115624Sphk p = gctl_get_asciiparam(req, arg); 370112709Sphk if (p == NULL) 371112709Sphk return (NULL); 372112709Sphk LIST_FOREACH(cp, &g_classes, class) { 373115624Sphk if (!strcmp(p, cp->name)) 374112709Sphk return (cp); 375112709Sphk } 376112709Sphk gctl_error(req, "Class not found"); 377112709Sphk return (NULL); 378112709Sphk} 379112709Sphk 380115624Sphkstruct g_geom * 381115624Sphkgctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg) 382112709Sphk{ 383115624Sphk char const *p; 384112709Sphk struct g_class *mp; 385112709Sphk struct g_geom *gp; 386112709Sphk 387115624Sphk p = gctl_get_asciiparam(req, arg); 388115958Sphk if (p != NULL) { 389115958Sphk LIST_FOREACH(mp, &g_classes, class) { 390115958Sphk if (mpr != NULL && mpr != mp) 391115958Sphk continue; 392115958Sphk LIST_FOREACH(gp, &mp->geom, geom) { 393115958Sphk if (!strcmp(p, gp->name)) 394115958Sphk return (gp); 395115958Sphk } 396112709Sphk } 397112709Sphk } 398112709Sphk gctl_error(req, "Geom not found"); 399112709Sphk return (NULL); 400112709Sphk} 401112709Sphk 402115624Sphkstruct g_provider * 403115624Sphkgctl_get_provider(struct gctl_req *req, char const *arg) 404112709Sphk{ 405115624Sphk char const *p; 406112709Sphk struct g_provider *pp; 407112709Sphk 408115624Sphk p = gctl_get_asciiparam(req, arg); 409112709Sphk if (p == NULL) 410112709Sphk return (NULL); 411115850Sphk pp = g_provider_by_name(p); 412115850Sphk if (pp != NULL) 413115850Sphk return (pp); 414112709Sphk gctl_error(req, "Provider not found"); 415112709Sphk return (NULL); 416112709Sphk} 417112709Sphk 418115624Sphkstatic void 419115624Sphkg_ctl_req(void *arg, int flag __unused) 420112709Sphk{ 421112709Sphk struct g_class *mp; 422115624Sphk struct gctl_req *req; 423115624Sphk char const *verb; 424112709Sphk 425112709Sphk g_topology_assert(); 426115624Sphk req = arg; 427115624Sphk mp = gctl_get_class(req, "class"); 428115726Sphk if (mp == NULL) { 429115726Sphk gctl_error(req, "Class not found"); 430115624Sphk return; 431115726Sphk } 432115624Sphk verb = gctl_get_param(req, "verb", NULL); 433115624Sphk if (mp->ctlreq == NULL) 434115624Sphk gctl_error(req, "Class takes no requests"); 435115624Sphk else 436115624Sphk mp->ctlreq(req, mp, verb); 437112709Sphk g_topology_assert(); 438112709Sphk} 439112709Sphk 440112709Sphk 441113876Sphkstatic int 442130585Sphkg_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 443112511Sphk{ 444112709Sphk struct gctl_req *req; 445136839Sphk int nerror; 446112511Sphk 447112511Sphk req = (void *)data; 448115624Sphk req->nerror = 0; 449115624Sphk req->serror = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 450112709Sphk /* It is an error if we cannot return an error text */ 451115624Sphk if (req->lerror < 2) 452112511Sphk return (EINVAL); 453112709Sphk if (!useracc(req->error, req->lerror, VM_PROT_WRITE)) 454112709Sphk return (EINVAL); 455112709Sphk 456112709Sphk /* Check the version */ 457112709Sphk if (req->version != GCTL_VERSION) 458112709Sphk return (gctl_error(req, 459112709Sphk "kernel and libgeom version mismatch.")); 460112709Sphk 461112709Sphk /* Get things on board */ 462115624Sphk gctl_copyin(req); 463112709Sphk 464112876Sphk if (g_debugflags & G_F_CTLDUMP) 465112876Sphk gctl_dump(req); 466115624Sphk 467115624Sphk if (!req->nerror) { 468115624Sphk g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL); 469115624Sphk gctl_copyout(req); 470112709Sphk } 471144789Spjd if (sbuf_done(req->serror)) { 472144789Spjd req->nerror = copyout(sbuf_data(req->serror), req->error, 473144789Spjd imin(req->lerror, sbuf_len(req->serror) + 1)); 474144789Spjd } 475115624Sphk 476136839Sphk nerror = req->nerror; 477114531Sphk gctl_free(req); 478136839Sphk return (nerror); 479112511Sphk} 480112511Sphk 481112511Sphkstatic int 482130585Sphkg_ctl_ioctl(struct cdev *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