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: releng/10.3/sys/geom/geom_ctl.c 261285 2014-01-30 10:53:29Z ae $"); 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 78216952Skib make_dev_credf(MAKEDEV_ETERNAL, &g_ctl_cdevsw, 0, NULL, 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/* 87261285Sae * Report an error back to the user in ascii format. Return nerror 88261285Sae * or EINVAL if nerror isn't specified. 89112511Sphk */ 90112709Sphkint 91113892Sphkgctl_error(struct gctl_req *req, const char *fmt, ...) 92112511Sphk{ 93113892Sphk va_list ap; 94112511Sphk 95115624Sphk if (req == NULL) 96115624Sphk return (EINVAL); 97115624Sphk 98115624Sphk /* We only record the first error */ 99144789Spjd if (sbuf_done(req->serror)) { 100144789Spjd if (!req->nerror) 101144789Spjd req->nerror = EEXIST; 102261285Sae return (req->nerror); 103144789Spjd } 104261285Sae if (!req->nerror) 105261285Sae req->nerror = EINVAL; 106115624Sphk 107115624Sphk va_start(ap, fmt); 108115624Sphk sbuf_vprintf(req->serror, fmt, ap); 109115949Sphk va_end(ap); 110115624Sphk sbuf_finish(req->serror); 111115624Sphk if (g_debugflags & G_F_CTLDUMP) 112115624Sphk printf("gctl %p error \"%s\"\n", req, sbuf_data(req->serror)); 113261285Sae 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); 126261285Sae req->nerror = copyin(uaddr, ptr, len); 127115624Sphk if (!req->nerror) 128112511Sphk return (ptr); 129261285Sae g_free(ptr); 130112511Sphk return (NULL); 131112511Sphk} 132112511Sphk 133115624Sphkstatic void 134112709Sphkgctl_copyin(struct gctl_req *req) 135112511Sphk{ 136112709Sphk struct gctl_req_arg *ap; 137112511Sphk char *p; 138261285Sae int i; 139112511Sphk 140115624Sphk ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap)); 141112709Sphk if (ap == NULL) { 142261285Sae gctl_error(req, "bad control request"); 143115624Sphk req->arg = NULL; 144115624Sphk return; 145112709Sphk } 146112709Sphk 147115624Sphk /* Nothing have been copyin()'ed yet */ 148115624Sphk for (i = 0; i < req->narg; i++) { 149115624Sphk ap[i].flag &= ~(GCTL_PARAM_NAMEKERNEL|GCTL_PARAM_VALUEKERNEL); 150115624Sphk ap[i].flag &= ~GCTL_PARAM_CHANGED; 151115624Sphk ap[i].kvalue = NULL; 152115624Sphk } 153115624Sphk 154115624Sphk for (i = 0; i < req->narg; i++) { 155112709Sphk if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) { 156261285Sae gctl_error(req, 157115624Sphk "wrong param name length %d: %d", i, ap[i].nlen); 158112511Sphk break; 159112709Sphk } 160115624Sphk p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen); 161112709Sphk if (p == NULL) 162112511Sphk break; 163112709Sphk if (p[ap[i].nlen - 1] != '\0') { 164261285Sae gctl_error(req, "unterminated param name"); 165112511Sphk g_free(p); 166112511Sphk break; 167112511Sphk } 168112709Sphk ap[i].name = p; 169115624Sphk ap[i].flag |= GCTL_PARAM_NAMEKERNEL; 170140367Sphk if (ap[i].len <= 0) { 171261285Sae gctl_error(req, "negative param length"); 172115624Sphk break; 173115624Sphk } 174115624Sphk p = geom_alloc_copyin(req, ap[i].value, ap[i].len); 175115624Sphk if (p == NULL) 176115624Sphk break; 177115624Sphk if ((ap[i].flag & GCTL_PARAM_ASCII) && 178115624Sphk p[ap[i].len - 1] != '\0') { 179261285Sae gctl_error(req, "unterminated param value"); 180115624Sphk g_free(p); 181115624Sphk break; 182115624Sphk } 183115624Sphk ap[i].kvalue = p; 184115624Sphk ap[i].flag |= GCTL_PARAM_VALUEKERNEL; 185112511Sphk } 186115624Sphk req->arg = ap; 187115624Sphk return; 188115624Sphk} 189115624Sphk 190115624Sphkstatic void 191115624Sphkgctl_copyout(struct gctl_req *req) 192115624Sphk{ 193115624Sphk int error, i; 194115624Sphk struct gctl_req_arg *ap; 195115624Sphk 196115624Sphk if (req->nerror) 197115624Sphk return; 198115624Sphk error = 0; 199115624Sphk ap = req->arg; 200115624Sphk for (i = 0; i < req->narg; i++, ap++) { 201115624Sphk if (!(ap->flag & GCTL_PARAM_CHANGED)) 202115624Sphk continue; 203115624Sphk error = copyout(ap->kvalue, ap->value, ap->len); 204115624Sphk if (!error) 205115624Sphk continue; 206115624Sphk req->nerror = error; 207115624Sphk return; 208112511Sphk } 209115624Sphk return; 210112511Sphk} 211112511Sphk 212112511Sphkstatic void 213114531Sphkgctl_free(struct gctl_req *req) 214114531Sphk{ 215114531Sphk int i; 216114531Sphk 217261285Sae sbuf_delete(req->serror); 218115624Sphk if (req->arg == NULL) 219115624Sphk return; 220114531Sphk for (i = 0; i < req->narg; i++) { 221115624Sphk if (req->arg[i].flag & GCTL_PARAM_NAMEKERNEL) 222114531Sphk g_free(req->arg[i].name); 223115624Sphk if ((req->arg[i].flag & GCTL_PARAM_VALUEKERNEL) && 224115624Sphk req->arg[i].len > 0) 225115624Sphk g_free(req->arg[i].kvalue); 226114531Sphk } 227114531Sphk g_free(req->arg); 228114531Sphk} 229114531Sphk 230114531Sphkstatic void 231112709Sphkgctl_dump(struct gctl_req *req) 232112511Sphk{ 233261285Sae struct gctl_req_arg *ap; 234112511Sphk u_int i; 235115624Sphk int j; 236112511Sphk 237115624Sphk printf("Dump of gctl request at %p:\n", req); 238115624Sphk if (req->nerror > 0) { 239115624Sphk printf(" nerror:\t%d\n", req->nerror); 240115624Sphk if (sbuf_len(req->serror) > 0) 241115624Sphk printf(" error:\t\"%s\"\n", sbuf_data(req->serror)); 242112511Sphk } 243261285Sae if (req->arg == NULL) 244261285Sae return; 245112511Sphk for (i = 0; i < req->narg; i++) { 246112511Sphk ap = &req->arg[i]; 247115624Sphk if (!(ap->flag & GCTL_PARAM_NAMEKERNEL)) 248115624Sphk printf(" param:\t%d@%p", ap->nlen, ap->name); 249115624Sphk else 250115624Sphk printf(" param:\t\"%s\"", ap->name); 251112709Sphk printf(" [%s%s%d] = ", 252112709Sphk ap->flag & GCTL_PARAM_RD ? "R" : "", 253112709Sphk ap->flag & GCTL_PARAM_WR ? "W" : "", 254112709Sphk ap->len); 255115624Sphk if (!(ap->flag & GCTL_PARAM_VALUEKERNEL)) { 256115624Sphk printf(" =@ %p", ap->value); 257115624Sphk } else if (ap->flag & GCTL_PARAM_ASCII) { 258115624Sphk printf("\"%s\"", (char *)ap->kvalue); 259112511Sphk } else if (ap->len > 0) { 260117150Sphk for (j = 0; j < ap->len && j < 512; j++) 261115624Sphk printf(" %02x", ((u_char *)ap->kvalue)[j]); 262112511Sphk } else { 263115624Sphk printf(" = %p", ap->kvalue); 264112511Sphk } 265112511Sphk printf("\n"); 266112511Sphk } 267112511Sphk} 268112511Sphk 269157581Smarcelint 270157581Smarcelgctl_set_param(struct gctl_req *req, const char *param, void const *ptr, 271157581Smarcel int len) 272114670Sphk{ 273115624Sphk int i; 274114670Sphk struct gctl_req_arg *ap; 275114670Sphk 276114670Sphk for (i = 0; i < req->narg; i++) { 277114670Sphk ap = &req->arg[i]; 278114670Sphk if (strcmp(param, ap->name)) 279114670Sphk continue; 280157581Smarcel if (!(ap->flag & GCTL_PARAM_WR)) 281157581Smarcel return (EPERM); 282157581Smarcel ap->flag |= GCTL_PARAM_CHANGED; 283115624Sphk if (ap->len < len) { 284157581Smarcel bcopy(ptr, ap->kvalue, ap->len); 285157581Smarcel return (ENOSPC); 286114670Sphk } 287115624Sphk bcopy(ptr, ap->kvalue, len); 288157581Smarcel return (0); 289114670Sphk } 290157581Smarcel return (EINVAL); 291114670Sphk} 292114670Sphk 293157581Smarcelvoid 294157581Smarcelgctl_set_param_err(struct gctl_req *req, const char *param, void const *ptr, 295157581Smarcel int len) 296157581Smarcel{ 297157581Smarcel 298157581Smarcel switch (gctl_set_param(req, param, ptr, len)) { 299157581Smarcel case EPERM: 300157581Smarcel gctl_error(req, "No write access %s argument", param); 301157581Smarcel break; 302157581Smarcel case ENOSPC: 303157581Smarcel gctl_error(req, "Wrong length %s argument", param); 304157581Smarcel break; 305157581Smarcel case EINVAL: 306157581Smarcel gctl_error(req, "Missing %s argument", param); 307157581Smarcel break; 308157581Smarcel } 309157581Smarcel} 310157581Smarcel 311112709Sphkvoid * 312112709Sphkgctl_get_param(struct gctl_req *req, const char *param, int *len) 313112709Sphk{ 314115624Sphk int i; 315112709Sphk void *p; 316112709Sphk struct gctl_req_arg *ap; 317112709Sphk 318112709Sphk for (i = 0; i < req->narg; i++) { 319112709Sphk ap = &req->arg[i]; 320112709Sphk if (strcmp(param, ap->name)) 321112709Sphk continue; 322112709Sphk if (!(ap->flag & GCTL_PARAM_RD)) 323112709Sphk continue; 324115624Sphk p = ap->kvalue; 325112709Sphk if (len != NULL) 326115624Sphk *len = ap->len; 327112709Sphk return (p); 328112709Sphk } 329112709Sphk return (NULL); 330112709Sphk} 331112709Sphk 332115624Sphkchar const * 333115624Sphkgctl_get_asciiparam(struct gctl_req *req, const char *param) 334115624Sphk{ 335115624Sphk int i; 336115624Sphk char const *p; 337115624Sphk struct gctl_req_arg *ap; 338115624Sphk 339115624Sphk for (i = 0; i < req->narg; i++) { 340115624Sphk ap = &req->arg[i]; 341115624Sphk if (strcmp(param, ap->name)) 342115624Sphk continue; 343115624Sphk if (!(ap->flag & GCTL_PARAM_RD)) 344115624Sphk continue; 345115624Sphk p = ap->kvalue; 346115624Sphk if (ap->len < 1) { 347115624Sphk gctl_error(req, "No length argument (%s)", param); 348115624Sphk return (NULL); 349115624Sphk } 350115624Sphk if (p[ap->len - 1] != '\0') { 351115624Sphk gctl_error(req, "Unterminated argument (%s)", param); 352115624Sphk return (NULL); 353115624Sphk } 354115624Sphk return (p); 355115624Sphk } 356115624Sphk return (NULL); 357115624Sphk} 358115624Sphk 359113893Sphkvoid * 360113893Sphkgctl_get_paraml(struct gctl_req *req, const char *param, int len) 361113893Sphk{ 362113893Sphk int i; 363113893Sphk void *p; 364113893Sphk 365113893Sphk p = gctl_get_param(req, param, &i); 366113893Sphk if (p == NULL) 367113893Sphk gctl_error(req, "Missing %s argument", param); 368113893Sphk else if (i != len) { 369113893Sphk p = NULL; 370113893Sphk gctl_error(req, "Wrong length %s argument", param); 371113893Sphk } 372113893Sphk return (p); 373113893Sphk} 374113893Sphk 375115624Sphkstruct g_class * 376115624Sphkgctl_get_class(struct gctl_req *req, char const *arg) 377112709Sphk{ 378115624Sphk char const *p; 379112709Sphk struct g_class *cp; 380112709Sphk 381115624Sphk p = gctl_get_asciiparam(req, arg); 382112709Sphk if (p == NULL) 383112709Sphk return (NULL); 384112709Sphk LIST_FOREACH(cp, &g_classes, class) { 385115624Sphk if (!strcmp(p, cp->name)) 386112709Sphk return (cp); 387112709Sphk } 388112709Sphk return (NULL); 389112709Sphk} 390112709Sphk 391115624Sphkstruct g_geom * 392115624Sphkgctl_get_geom(struct gctl_req *req, struct g_class *mpr, char const *arg) 393112709Sphk{ 394115624Sphk char const *p; 395112709Sphk struct g_class *mp; 396112709Sphk struct g_geom *gp; 397112709Sphk 398115624Sphk p = gctl_get_asciiparam(req, arg); 399168052Sdelphij if (p == NULL) 400168052Sdelphij return (NULL); 401168052Sdelphij LIST_FOREACH(mp, &g_classes, class) { 402168052Sdelphij if (mpr != NULL && mpr != mp) 403168052Sdelphij continue; 404168052Sdelphij LIST_FOREACH(gp, &mp->geom, geom) { 405168052Sdelphij if (!strcmp(p, gp->name)) 406168052Sdelphij return (gp); 407112709Sphk } 408112709Sphk } 409168052Sdelphij gctl_error(req, "Geom not found: \"%s\"", p); 410112709Sphk return (NULL); 411112709Sphk} 412112709Sphk 413115624Sphkstruct g_provider * 414115624Sphkgctl_get_provider(struct gctl_req *req, char const *arg) 415112709Sphk{ 416115624Sphk char const *p; 417112709Sphk struct g_provider *pp; 418112709Sphk 419115624Sphk p = gctl_get_asciiparam(req, arg); 420112709Sphk if (p == NULL) 421112709Sphk return (NULL); 422115850Sphk pp = g_provider_by_name(p); 423115850Sphk if (pp != NULL) 424115850Sphk return (pp); 425168052Sdelphij gctl_error(req, "Provider not found: \"%s\"", p); 426112709Sphk return (NULL); 427112709Sphk} 428112709Sphk 429115624Sphkstatic void 430115624Sphkg_ctl_req(void *arg, int flag __unused) 431112709Sphk{ 432112709Sphk struct g_class *mp; 433115624Sphk struct gctl_req *req; 434115624Sphk char const *verb; 435112709Sphk 436112709Sphk g_topology_assert(); 437115624Sphk req = arg; 438115624Sphk mp = gctl_get_class(req, "class"); 439115726Sphk if (mp == NULL) { 440115726Sphk gctl_error(req, "Class not found"); 441115624Sphk return; 442115726Sphk } 443150304Smarcel if (mp->ctlreq == NULL) { 444150304Smarcel gctl_error(req, "Class takes no requests"); 445150304Smarcel return; 446150304Smarcel } 447115624Sphk verb = gctl_get_param(req, "verb", NULL); 448150304Smarcel if (verb == NULL) { 449150304Smarcel gctl_error(req, "Verb missing"); 450150304Smarcel return; 451150304Smarcel } 452150304Smarcel mp->ctlreq(req, mp, verb); 453112709Sphk g_topology_assert(); 454112709Sphk} 455112709Sphk 456112709Sphk 457113876Sphkstatic int 458130585Sphkg_ctl_ioctl_ctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 459112511Sphk{ 460112709Sphk struct gctl_req *req; 461136839Sphk int nerror; 462112511Sphk 463112511Sphk req = (void *)data; 464115624Sphk req->nerror = 0; 465112709Sphk /* It is an error if we cannot return an error text */ 466115624Sphk if (req->lerror < 2) 467112511Sphk return (EINVAL); 468112709Sphk if (!useracc(req->error, req->lerror, VM_PROT_WRITE)) 469112709Sphk return (EINVAL); 470112709Sphk 471261285Sae req->serror = sbuf_new_auto(); 472112709Sphk /* Check the version */ 473261285Sae if (req->version != GCTL_VERSION) { 474261285Sae gctl_error(req, "kernel and libgeom version mismatch."); 475261285Sae req->arg = NULL; 476261285Sae } else { 477261285Sae /* Get things on board */ 478261285Sae gctl_copyin(req); 479112709Sphk 480261285Sae if (g_debugflags & G_F_CTLDUMP) 481261285Sae gctl_dump(req); 482115624Sphk 483261285Sae if (!req->nerror) { 484261285Sae g_waitfor_event(g_ctl_req, req, M_WAITOK, NULL); 485261285Sae gctl_copyout(req); 486261285Sae } 487112709Sphk } 488144789Spjd if (sbuf_done(req->serror)) { 489261285Sae copyout(sbuf_data(req->serror), req->error, 490144789Spjd imin(req->lerror, sbuf_len(req->serror) + 1)); 491144789Spjd } 492115624Sphk 493136839Sphk nerror = req->nerror; 494114531Sphk gctl_free(req); 495136839Sphk return (nerror); 496112511Sphk} 497112511Sphk 498112511Sphkstatic int 499130585Sphkg_ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 500105068Sphk{ 501105068Sphk int error; 502105068Sphk 503105068Sphk switch(cmd) { 504112511Sphk case GEOM_CTL: 505112511Sphk error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td); 506112511Sphk break; 507105068Sphk default: 508115624Sphk error = ENOIOCTL; 509105068Sphk break; 510105068Sphk } 511105068Sphk return (error); 512105068Sphk 513105068Sphk} 514