geom_ctl.c revision 113893
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 113893 2003-04-23 08:03:47Z 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> 61112511Sphk#include <geom/geom_ext.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{ 95112511Sphk int error; 96113892Sphk va_list ap; 97113892Sphk struct sbuf *sb; 98112511Sphk 99113892Sphk sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 100113892Sphk if (sb == NULL) { 101113892Sphk error = copyout(fmt, req->error, 102113892Sphk imin(req->lerror, strlen(fmt) + 1)); 103113892Sphk } else { 104113892Sphk va_start(ap, fmt); 105113892Sphk sbuf_vprintf(sb, fmt, ap); 106113892Sphk sbuf_finish(sb); 107113892Sphk if (g_debugflags & G_F_CTLDUMP) 108113892Sphk printf("gctl %p error \"%s\"\n", req, sbuf_data(sb)); 109113892Sphk error = copyout(sbuf_data(sb), req->error, 110113892Sphk imin(req->lerror, sbuf_len(sb) + 1)); 111113892Sphk } 112112511Sphk if (!error) 113112511Sphk error = EINVAL; 114113892Sphk sbuf_delete(sb); 115112511Sphk return (error); 116112511Sphk} 117112511Sphk 118112511Sphk/* 119112511Sphk * Allocate space and copyin() something. 120112511Sphk * XXX: this should really be a standard function in the kernel. 121112511Sphk */ 122112511Sphkstatic void * 123112709Sphkgeom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len, int *errp) 124112511Sphk{ 125112511Sphk int error; 126112511Sphk void *ptr; 127112511Sphk 128112511Sphk ptr = g_malloc(len, M_WAITOK); 129112511Sphk if (ptr == NULL) 130112511Sphk error = ENOMEM; 131112511Sphk else 132112511Sphk error = copyin(uaddr, ptr, len); 133112511Sphk if (!error) 134112511Sphk return (ptr); 135112709Sphk gctl_error(req, "no access to argument"); 136112511Sphk *errp = error; 137112511Sphk if (ptr != NULL) 138112511Sphk g_free(ptr); 139112511Sphk return (NULL); 140112511Sphk} 141112511Sphk 142112511Sphk 143112511Sphk/* 144112511Sphk * XXX: This function is a nightmare. It walks through the request and 145112511Sphk * XXX: makes sure that the various bits and pieces are there and copies 146112511Sphk * XXX: some of them into kernel memory to make things easier. 147112511Sphk * XXX: I really wish we had a standard marshalling layer somewhere. 148112511Sphk */ 149112511Sphk 150112511Sphkstatic int 151112709Sphkgctl_copyin(struct gctl_req *req) 152112511Sphk{ 153112511Sphk int error, i, j; 154112709Sphk struct gctl_req_arg *ap; 155112511Sphk char *p; 156112511Sphk 157112511Sphk error = 0; 158112709Sphk ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap), &error); 159112709Sphk if (ap == NULL) { 160112709Sphk gctl_error(req, "copyin() of arguments failed"); 161112511Sphk return (error); 162112709Sphk } 163112709Sphk 164112511Sphk for (i = 0; !error && i < req->narg; i++) { 165112709Sphk if (ap[i].len > 0 && 166112709Sphk !useracc(ap[i].value, ap[i].len, 167112709Sphk ap[i].flag & GCTL_PARAM_RW)) 168112709Sphk error = gctl_error(req, "no access to param data"); 169113862Sphk if (error) 170112709Sphk break; 171112511Sphk p = NULL; 172112709Sphk if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) { 173112709Sphk error = gctl_error(req, "wrong param name length"); 174112511Sphk break; 175112709Sphk } 176112709Sphk p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen, &error); 177112709Sphk if (p == NULL) 178112511Sphk break; 179112709Sphk if (p[ap[i].nlen - 1] != '\0') { 180112709Sphk error = gctl_error(req, "unterminated param name"); 181112511Sphk g_free(p); 182112511Sphk break; 183112511Sphk } 184112709Sphk ap[i].name = p; 185112709Sphk ap[i].nlen = 0; 186112511Sphk } 187112511Sphk if (!error) { 188112511Sphk req->arg = ap; 189112511Sphk return (0); 190112511Sphk } 191112511Sphk for (j = 0; j < i; j++) 192112511Sphk if (ap[j].nlen == 0 && ap[j].name != NULL) 193112511Sphk g_free(ap[j].name); 194112511Sphk g_free(ap); 195112511Sphk return (error); 196112511Sphk} 197112511Sphk 198112511Sphkstatic void 199112709Sphkgctl_dump(struct gctl_req *req) 200112511Sphk{ 201112511Sphk u_int i; 202112511Sphk int j, error; 203112709Sphk struct gctl_req_arg *ap; 204112511Sphk void *p; 205112511Sphk 206112511Sphk 207112709Sphk printf("Dump of gctl %s request at %p:\n", req->reqt->name, req); 208112511Sphk if (req->lerror > 0) { 209112709Sphk p = geom_alloc_copyin(req, req->error, req->lerror, &error); 210112511Sphk if (p != NULL) { 211112511Sphk ((char *)p)[req->lerror - 1] = '\0'; 212112511Sphk printf(" error:\t\"%s\"\n", (char *)p); 213112511Sphk g_free(p); 214112511Sphk } 215112511Sphk } 216112511Sphk for (i = 0; i < req->narg; i++) { 217112511Sphk ap = &req->arg[i]; 218113862Sphk printf(" param:\t\"%s\"", ap->name); 219112709Sphk printf(" [%s%s%d] = ", 220112709Sphk ap->flag & GCTL_PARAM_RD ? "R" : "", 221112709Sphk ap->flag & GCTL_PARAM_WR ? "W" : "", 222112709Sphk ap->len); 223112709Sphk if (ap->flag & GCTL_PARAM_ASCII) { 224112709Sphk p = geom_alloc_copyin(req, ap->value, ap->len, &error); 225112709Sphk if (p != NULL) { 226112709Sphk ((char *)p)[ap->len - 1] = '\0'; 227112511Sphk printf("\"%s\"", (char *)p); 228112709Sphk } 229112511Sphk g_free(p); 230112511Sphk } else if (ap->len > 0) { 231112709Sphk p = geom_alloc_copyin(req, ap->value, ap->len, &error); 232112511Sphk for (j = 0; j < ap->len; j++) 233112511Sphk printf(" %02x", ((u_char *)p)[j]); 234112511Sphk g_free(p); 235112511Sphk } else { 236112511Sphk printf(" = %p", ap->value); 237112511Sphk } 238112511Sphk printf("\n"); 239112511Sphk } 240112511Sphk} 241112511Sphk 242112709Sphkvoid * 243112709Sphkgctl_get_param(struct gctl_req *req, const char *param, int *len) 244112709Sphk{ 245112709Sphk int i, error, j; 246112709Sphk void *p; 247112709Sphk struct gctl_req_arg *ap; 248112709Sphk 249112709Sphk for (i = 0; i < req->narg; i++) { 250112709Sphk ap = &req->arg[i]; 251112709Sphk if (strcmp(param, ap->name)) 252112709Sphk continue; 253112709Sphk if (!(ap->flag & GCTL_PARAM_RD)) 254112709Sphk continue; 255112709Sphk if (ap->len > 0) 256112709Sphk j = ap->len; 257112709Sphk else 258112709Sphk j = 0; 259112709Sphk if (j != 0) 260112709Sphk p = geom_alloc_copyin(req, ap->value, j, &error); 261112709Sphk /* XXX: should not fail, tested prviously */ 262112709Sphk else 263112709Sphk p = ap->value; 264112709Sphk if (len != NULL) 265112709Sphk *len = j; 266112709Sphk return (p); 267112709Sphk } 268112709Sphk return (NULL); 269112709Sphk} 270112709Sphk 271113893Sphkvoid * 272113893Sphkgctl_get_paraml(struct gctl_req *req, const char *param, int len) 273113893Sphk{ 274113893Sphk int i; 275113893Sphk void *p; 276113893Sphk 277113893Sphk p = gctl_get_param(req, param, &i); 278113893Sphk if (p == NULL) 279113893Sphk gctl_error(req, "Missing %s argument", param); 280113893Sphk else if (i != len) { 281113893Sphk g_free(p); 282113893Sphk p = NULL; 283113893Sphk gctl_error(req, "Wrong length %s argument", param); 284113893Sphk } 285113893Sphk return (p); 286113893Sphk} 287113893Sphk 288112709Sphkstatic struct g_class* 289112709Sphkgctl_get_class(struct gctl_req *req) 290112709Sphk{ 291112709Sphk char *p; 292112709Sphk int len; 293112709Sphk struct g_class *cp; 294112709Sphk 295112709Sphk p = gctl_get_param(req, "class", &len); 296112709Sphk if (p == NULL) 297112709Sphk return (NULL); 298112709Sphk if (p[len - 1] != '\0') { 299112709Sphk gctl_error(req, "Unterminated class name"); 300112709Sphk g_free(p); 301112709Sphk return (NULL); 302112709Sphk } 303112709Sphk LIST_FOREACH(cp, &g_classes, class) { 304112709Sphk if (!strcmp(p, cp->name)) { 305112709Sphk g_free(p); 306112709Sphk return (cp); 307112709Sphk } 308112709Sphk } 309112709Sphk gctl_error(req, "Class not found"); 310112709Sphk return (NULL); 311112709Sphk} 312112709Sphk 313112709Sphkstatic struct g_geom* 314112709Sphkgctl_get_geom(struct gctl_req *req, struct g_class *mpr) 315112709Sphk{ 316112709Sphk char *p; 317112709Sphk int len; 318112709Sphk struct g_class *mp; 319112709Sphk struct g_geom *gp; 320112709Sphk 321112709Sphk p = gctl_get_param(req, "geom", &len); 322112709Sphk if (p == NULL) 323112709Sphk return (NULL); 324112709Sphk if (p[len - 1] != '\0') { 325112709Sphk gctl_error(req, "Unterminated provider name"); 326112709Sphk g_free(p); 327112709Sphk return (NULL); 328112709Sphk } 329112709Sphk LIST_FOREACH(mp, &g_classes, class) { 330112709Sphk if (mpr != NULL && mpr != mp) 331112709Sphk continue; 332112709Sphk LIST_FOREACH(gp, &mp->geom, geom) { 333112709Sphk if (!strcmp(p, gp->name)) { 334112709Sphk g_free(p); 335112709Sphk return (gp); 336112709Sphk } 337112709Sphk } 338112709Sphk } 339112709Sphk gctl_error(req, "Geom not found"); 340112709Sphk return (NULL); 341112709Sphk} 342112709Sphk 343112709Sphkstatic struct g_provider* 344112709Sphkgctl_get_provider(struct gctl_req *req) 345112709Sphk{ 346112709Sphk char *p; 347112709Sphk int len; 348112709Sphk struct g_class *cp; 349112709Sphk struct g_geom *gp; 350112709Sphk struct g_provider *pp; 351112709Sphk 352112709Sphk p = gctl_get_param(req, "provider", &len); 353112709Sphk if (p == NULL) 354112709Sphk return (NULL); 355112709Sphk if (p[len - 1] != '\0') { 356112709Sphk gctl_error(req, "Unterminated provider name"); 357112709Sphk g_free(p); 358112709Sphk return (NULL); 359112709Sphk } 360112709Sphk LIST_FOREACH(cp, &g_classes, class) { 361112709Sphk LIST_FOREACH(gp, &cp->geom, geom) { 362112709Sphk LIST_FOREACH(pp, &gp->provider, provider) { 363112709Sphk if (!strcmp(p, pp->name)) { 364112709Sphk g_free(p); 365112709Sphk return (pp); 366112709Sphk } 367112709Sphk } 368112709Sphk } 369112709Sphk } 370112709Sphk gctl_error(req, "Provider not found"); 371112709Sphk return (NULL); 372112709Sphk} 373112709Sphk 374113008Sphkstatic int 375112709Sphkgctl_create_geom(struct gctl_req *req) 376112709Sphk{ 377112709Sphk struct g_class *mp; 378112709Sphk struct g_provider *pp; 379113008Sphk int error; 380112709Sphk 381112709Sphk g_topology_assert(); 382112709Sphk mp = gctl_get_class(req); 383112709Sphk if (mp == NULL) 384113008Sphk return (gctl_error(req, "Class not found")); 385113008Sphk if (mp->create_geom == NULL) 386113008Sphk return (gctl_error(req, "Class has no create_geom method")); 387112709Sphk pp = gctl_get_provider(req); 388113008Sphk error = mp->create_geom(req, mp, pp); 389112709Sphk g_topology_assert(); 390113008Sphk return (error); 391112709Sphk} 392112709Sphk 393113008Sphkstatic int 394112709Sphkgctl_destroy_geom(struct gctl_req *req) 395112709Sphk{ 396112709Sphk struct g_class *mp; 397112709Sphk struct g_geom *gp; 398113008Sphk int error; 399112709Sphk 400112709Sphk g_topology_assert(); 401112709Sphk mp = gctl_get_class(req); 402112709Sphk if (mp == NULL) 403113008Sphk return (gctl_error(req, "Class not found")); 404113008Sphk if (mp->destroy_geom == NULL) 405113008Sphk return (gctl_error(req, "Class has no destroy_geom method")); 406112709Sphk gp = gctl_get_geom(req, mp); 407113008Sphk if (gp == NULL) 408113008Sphk return (gctl_error(req, "Geom not specified")); 409113008Sphk if (gp->class != mp) 410113008Sphk return (gctl_error(req, "Geom not of specificed class")); 411113008Sphk error = mp->destroy_geom(req, mp, gp); 412112709Sphk g_topology_assert(); 413113008Sphk return (error); 414112709Sphk} 415112709Sphk 416113876Sphkstatic int 417113876Sphkgctl_config_geom(struct gctl_req *req) 418113876Sphk{ 419113876Sphk struct g_class *mp; 420113876Sphk struct g_geom *gp; 421113876Sphk char *verb; 422113876Sphk int error, vlen; 423113876Sphk 424113876Sphk g_topology_assert(); 425113876Sphk mp = gctl_get_class(req); 426113876Sphk if (mp == NULL) 427113876Sphk return (gctl_error(req, "Class not found")); 428113876Sphk if (mp->config_geom == NULL) 429113876Sphk return (gctl_error(req, "Class has no config_geom method")); 430113876Sphk gp = gctl_get_geom(req, mp); 431113876Sphk if (gp == NULL) 432113876Sphk return (gctl_error(req, "Geom not specified")); 433113876Sphk if (gp->class != mp) 434113876Sphk return (gctl_error(req, "Geom not of specificed class")); 435113876Sphk verb = gctl_get_param(req, "verb", &vlen); 436113876Sphk if (verb == NULL) 437113876Sphk return (gctl_error(req, "Missing verb parameter")); 438113876Sphk if (vlen < 2) { 439113876Sphk g_free(verb); 440113876Sphk return (gctl_error(req, "Too short verb parameter")); 441113876Sphk } 442113876Sphk if (verb[vlen - 1] != '\0') { 443113876Sphk g_free(verb); 444113876Sphk return (gctl_error(req, "Unterminated verb parameter")); 445113876Sphk } 446113876Sphk error = mp->config_geom(req, gp, verb); 447113876Sphk g_free(verb); 448113876Sphk g_topology_assert(); 449113876Sphk return (error); 450113876Sphk} 451113876Sphk 452112511Sphk/* 453112511Sphk * Handle ioctl from libgeom::geom_ctl.c 454112511Sphk */ 455112511Sphkstatic int 456112511Sphkg_ctl_ioctl_ctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 457112511Sphk{ 458112511Sphk int error; 459112511Sphk int i; 460112709Sphk struct gctl_req *req; 461112511Sphk 462112511Sphk req = (void *)data; 463112709Sphk /* It is an error if we cannot return an error text */ 464112511Sphk if (req->lerror < 1) 465112511Sphk return (EINVAL); 466112709Sphk if (!useracc(req->error, req->lerror, VM_PROT_WRITE)) 467112709Sphk return (EINVAL); 468112709Sphk 469112709Sphk /* Check the version */ 470112709Sphk if (req->version != GCTL_VERSION) 471112709Sphk return (gctl_error(req, 472112709Sphk "kernel and libgeom version mismatch.")); 473112709Sphk 474112709Sphk /* Check the request type */ 475112709Sphk for (i = 0; gcrt[i].request != GCTL_INVALID_REQUEST; i++) 476112709Sphk if (gcrt[i].request == req->request) 477112511Sphk break; 478112709Sphk if (gcrt[i].request == GCTL_INVALID_REQUEST) 479112709Sphk return (gctl_error(req, "invalid request")); 480112709Sphk req->reqt = &gcrt[i]; 481112709Sphk 482112709Sphk /* Get things on board */ 483112709Sphk error = gctl_copyin(req); 484112511Sphk if (error) 485112511Sphk return (error); 486112709Sphk 487112876Sphk if (g_debugflags & G_F_CTLDUMP) 488112876Sphk gctl_dump(req); 489112709Sphk g_topology_lock(); 490112709Sphk switch (req->request) { 491112709Sphk case GCTL_CREATE_GEOM: 492113008Sphk error = gctl_create_geom(req); 493112709Sphk break; 494112709Sphk case GCTL_DESTROY_GEOM: 495113008Sphk error = gctl_destroy_geom(req); 496112709Sphk break; 497113876Sphk case GCTL_CONFIG_GEOM: 498113876Sphk error = gctl_config_geom(req); 499113876Sphk break; 500112709Sphk default: 501113008Sphk error = gctl_error(req, "XXX: TBD"); 502112709Sphk break; 503112709Sphk } 504112709Sphk g_topology_unlock(); 505113008Sphk return (error); 506112511Sphk} 507112511Sphk 508112511Sphkstatic int 509105068Sphkg_ctl_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 510105068Sphk{ 511105068Sphk int error; 512105068Sphk 513105068Sphk switch(cmd) { 514112511Sphk case GEOM_CTL: 515112709Sphk DROP_GIANT(); 516112511Sphk error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td); 517112709Sphk PICKUP_GIANT(); 518112511Sphk break; 519105068Sphk default: 520105068Sphk error = ENOTTY; 521105068Sphk break; 522105068Sphk } 523105068Sphk return (error); 524105068Sphk 525105068Sphk} 526