geom_ctl.c revision 113862
1/*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $FreeBSD: head/sys/geom/geom_ctl.c 113862 2003-04-22 19:42:05Z phk $ 36 */ 37 38#include "opt_geom.h" 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43#include <sys/sysctl.h> 44#include <sys/bio.h> 45#include <sys/conf.h> 46#include <sys/disk.h> 47#include <sys/malloc.h> 48#include <sys/sysctl.h> 49 50#include <sys/lock.h> 51#include <sys/mutex.h> 52 53#include <vm/vm.h> 54#include <vm/vm_extern.h> 55 56#include <geom/geom.h> 57#include <geom/geom_int.h> 58#define GCTL_TABLE 1 59#include <geom/geom_ctl.h> 60#include <geom/geom_ext.h> 61 62static d_ioctl_t g_ctl_ioctl; 63 64static struct cdevsw g_ctl_cdevsw = { 65 .d_open = nullopen, 66 .d_close = nullclose, 67 .d_ioctl = g_ctl_ioctl, 68 .d_name = "g_ctl", 69}; 70 71void 72g_ctl_init(void) 73{ 74 75 make_dev(&g_ctl_cdevsw, 0, 76 UID_ROOT, GID_OPERATOR, 0640, PATH_GEOM_CTL); 77 KASSERT(GCTL_PARAM_RD == VM_PROT_READ, 78 ("GCTL_PARAM_RD != VM_PROT_READ")); 79 KASSERT(GCTL_PARAM_WR == VM_PROT_WRITE, 80 ("GCTL_PARAM_WR != VM_PROT_WRITE")); 81} 82 83/* 84 * Report an error back to the user in ascii format. Return whatever copyout 85 * returned, or EINVAL if it succeeded. 86 * XXX: should not be static. 87 * XXX: should take printf like args. 88 */ 89int 90gctl_error(struct gctl_req *req, const char *errtxt) 91{ 92 int error; 93 94 if (g_debugflags & G_F_CTLDUMP) 95 printf("gctl %p error \"%s\"\n", req, errtxt); 96 error = copyout(errtxt, req->error, 97 imin(req->lerror, strlen(errtxt) + 1)); 98 if (!error) 99 error = EINVAL; 100 return (error); 101} 102 103/* 104 * Allocate space and copyin() something. 105 * XXX: this should really be a standard function in the kernel. 106 */ 107static void * 108geom_alloc_copyin(struct gctl_req *req, void *uaddr, size_t len, int *errp) 109{ 110 int error; 111 void *ptr; 112 113 ptr = g_malloc(len, M_WAITOK); 114 if (ptr == NULL) 115 error = ENOMEM; 116 else 117 error = copyin(uaddr, ptr, len); 118 if (!error) 119 return (ptr); 120 gctl_error(req, "no access to argument"); 121 *errp = error; 122 if (ptr != NULL) 123 g_free(ptr); 124 return (NULL); 125} 126 127 128/* 129 * XXX: This function is a nightmare. It walks through the request and 130 * XXX: makes sure that the various bits and pieces are there and copies 131 * XXX: some of them into kernel memory to make things easier. 132 * XXX: I really wish we had a standard marshalling layer somewhere. 133 */ 134 135static int 136gctl_copyin(struct gctl_req *req) 137{ 138 int error, i, j; 139 struct gctl_req_arg *ap; 140 char *p; 141 142 error = 0; 143 ap = geom_alloc_copyin(req, req->arg, req->narg * sizeof(*ap), &error); 144 if (ap == NULL) { 145 gctl_error(req, "copyin() of arguments failed"); 146 return (error); 147 } 148 149 for (i = 0; !error && i < req->narg; i++) { 150 if (ap[i].len > 0 && 151 !useracc(ap[i].value, ap[i].len, 152 ap[i].flag & GCTL_PARAM_RW)) 153 error = gctl_error(req, "no access to param data"); 154 if (error) 155 break; 156 p = NULL; 157 if (ap[i].nlen < 1 || ap[i].nlen > SPECNAMELEN) { 158 error = gctl_error(req, "wrong param name length"); 159 break; 160 } 161 p = geom_alloc_copyin(req, ap[i].name, ap[i].nlen, &error); 162 if (p == NULL) 163 break; 164 if (p[ap[i].nlen - 1] != '\0') { 165 error = gctl_error(req, "unterminated param name"); 166 g_free(p); 167 break; 168 } 169 ap[i].name = p; 170 ap[i].nlen = 0; 171 } 172 if (!error) { 173 req->arg = ap; 174 return (0); 175 } 176 for (j = 0; j < i; j++) 177 if (ap[j].nlen == 0 && ap[j].name != NULL) 178 g_free(ap[j].name); 179 g_free(ap); 180 return (error); 181} 182 183static void 184gctl_dump(struct gctl_req *req) 185{ 186 u_int i; 187 int j, error; 188 struct gctl_req_arg *ap; 189 void *p; 190 191 192 printf("Dump of gctl %s request at %p:\n", req->reqt->name, req); 193 if (req->lerror > 0) { 194 p = geom_alloc_copyin(req, req->error, req->lerror, &error); 195 if (p != NULL) { 196 ((char *)p)[req->lerror - 1] = '\0'; 197 printf(" error:\t\"%s\"\n", (char *)p); 198 g_free(p); 199 } 200 } 201 for (i = 0; i < req->narg; i++) { 202 ap = &req->arg[i]; 203 printf(" param:\t\"%s\"", ap->name); 204 printf(" [%s%s%d] = ", 205 ap->flag & GCTL_PARAM_RD ? "R" : "", 206 ap->flag & GCTL_PARAM_WR ? "W" : "", 207 ap->len); 208 if (ap->flag & GCTL_PARAM_ASCII) { 209 p = geom_alloc_copyin(req, ap->value, ap->len, &error); 210 if (p != NULL) { 211 ((char *)p)[ap->len - 1] = '\0'; 212 printf("\"%s\"", (char *)p); 213 } 214 g_free(p); 215 } else if (ap->len > 0) { 216 p = geom_alloc_copyin(req, ap->value, ap->len, &error); 217 for (j = 0; j < ap->len; j++) 218 printf(" %02x", ((u_char *)p)[j]); 219 g_free(p); 220 } else { 221 printf(" = %p", ap->value); 222 } 223 printf("\n"); 224 } 225} 226 227void * 228gctl_get_param(struct gctl_req *req, const char *param, int *len) 229{ 230 int i, error, j; 231 void *p; 232 struct gctl_req_arg *ap; 233 234 for (i = 0; i < req->narg; i++) { 235 ap = &req->arg[i]; 236 if (strcmp(param, ap->name)) 237 continue; 238 if (!(ap->flag & GCTL_PARAM_RD)) 239 continue; 240 if (ap->len > 0) 241 j = ap->len; 242 else 243 j = 0; 244 if (j != 0) 245 p = geom_alloc_copyin(req, ap->value, j, &error); 246 /* XXX: should not fail, tested prviously */ 247 else 248 p = ap->value; 249 if (len != NULL) 250 *len = j; 251 return (p); 252 } 253 return (NULL); 254} 255 256static struct g_class* 257gctl_get_class(struct gctl_req *req) 258{ 259 char *p; 260 int len; 261 struct g_class *cp; 262 263 p = gctl_get_param(req, "class", &len); 264 if (p == NULL) 265 return (NULL); 266 if (p[len - 1] != '\0') { 267 gctl_error(req, "Unterminated class name"); 268 g_free(p); 269 return (NULL); 270 } 271 LIST_FOREACH(cp, &g_classes, class) { 272 if (!strcmp(p, cp->name)) { 273 g_free(p); 274 return (cp); 275 } 276 } 277 gctl_error(req, "Class not found"); 278 return (NULL); 279} 280 281static struct g_geom* 282gctl_get_geom(struct gctl_req *req, struct g_class *mpr) 283{ 284 char *p; 285 int len; 286 struct g_class *mp; 287 struct g_geom *gp; 288 289 p = gctl_get_param(req, "geom", &len); 290 if (p == NULL) 291 return (NULL); 292 if (p[len - 1] != '\0') { 293 gctl_error(req, "Unterminated provider name"); 294 g_free(p); 295 return (NULL); 296 } 297 LIST_FOREACH(mp, &g_classes, class) { 298 if (mpr != NULL && mpr != mp) 299 continue; 300 LIST_FOREACH(gp, &mp->geom, geom) { 301 if (!strcmp(p, gp->name)) { 302 g_free(p); 303 return (gp); 304 } 305 } 306 } 307 gctl_error(req, "Geom not found"); 308 return (NULL); 309} 310 311static struct g_provider* 312gctl_get_provider(struct gctl_req *req) 313{ 314 char *p; 315 int len; 316 struct g_class *cp; 317 struct g_geom *gp; 318 struct g_provider *pp; 319 320 p = gctl_get_param(req, "provider", &len); 321 if (p == NULL) 322 return (NULL); 323 if (p[len - 1] != '\0') { 324 gctl_error(req, "Unterminated provider name"); 325 g_free(p); 326 return (NULL); 327 } 328 LIST_FOREACH(cp, &g_classes, class) { 329 LIST_FOREACH(gp, &cp->geom, geom) { 330 LIST_FOREACH(pp, &gp->provider, provider) { 331 if (!strcmp(p, pp->name)) { 332 g_free(p); 333 return (pp); 334 } 335 } 336 } 337 } 338 gctl_error(req, "Provider not found"); 339 return (NULL); 340} 341 342static int 343gctl_create_geom(struct gctl_req *req) 344{ 345 struct g_class *mp; 346 struct g_provider *pp; 347 int error; 348 349 g_topology_assert(); 350 mp = gctl_get_class(req); 351 if (mp == NULL) 352 return (gctl_error(req, "Class not found")); 353 if (mp->create_geom == NULL) 354 return (gctl_error(req, "Class has no create_geom method")); 355 pp = gctl_get_provider(req); 356 error = mp->create_geom(req, mp, pp); 357 g_topology_assert(); 358 return (error); 359} 360 361static int 362gctl_destroy_geom(struct gctl_req *req) 363{ 364 struct g_class *mp; 365 struct g_geom *gp; 366 int error; 367 368 g_topology_assert(); 369 mp = gctl_get_class(req); 370 if (mp == NULL) 371 return (gctl_error(req, "Class not found")); 372 if (mp->destroy_geom == NULL) 373 return (gctl_error(req, "Class has no destroy_geom method")); 374 gp = gctl_get_geom(req, mp); 375 if (gp == NULL) 376 return (gctl_error(req, "Geom not specified")); 377 if (gp->class != mp) 378 return (gctl_error(req, "Geom not of specificed class")); 379 error = mp->destroy_geom(req, mp, gp); 380 g_topology_assert(); 381 return (error); 382} 383 384/* 385 * Handle ioctl from libgeom::geom_ctl.c 386 */ 387static int 388g_ctl_ioctl_ctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 389{ 390 int error; 391 int i; 392 struct gctl_req *req; 393 394 req = (void *)data; 395 /* It is an error if we cannot return an error text */ 396 if (req->lerror < 1) 397 return (EINVAL); 398 if (!useracc(req->error, req->lerror, VM_PROT_WRITE)) 399 return (EINVAL); 400 401 /* Check the version */ 402 if (req->version != GCTL_VERSION) 403 return (gctl_error(req, 404 "kernel and libgeom version mismatch.")); 405 406 /* Check the request type */ 407 for (i = 0; gcrt[i].request != GCTL_INVALID_REQUEST; i++) 408 if (gcrt[i].request == req->request) 409 break; 410 if (gcrt[i].request == GCTL_INVALID_REQUEST) 411 return (gctl_error(req, "invalid request")); 412 req->reqt = &gcrt[i]; 413 414 /* Get things on board */ 415 error = gctl_copyin(req); 416 if (error) 417 return (error); 418 419 if (g_debugflags & G_F_CTLDUMP) 420 gctl_dump(req); 421 g_topology_lock(); 422 switch (req->request) { 423 case GCTL_CREATE_GEOM: 424 error = gctl_create_geom(req); 425 break; 426 case GCTL_DESTROY_GEOM: 427 error = gctl_destroy_geom(req); 428 break; 429 default: 430 error = gctl_error(req, "XXX: TBD"); 431 break; 432 } 433 g_topology_unlock(); 434 return (error); 435} 436 437static int 438g_ctl_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 439{ 440 int error; 441 442 switch(cmd) { 443 case GEOM_CTL: 444 DROP_GIANT(); 445 error = g_ctl_ioctl_ctl(dev, cmd, data, fflag, td); 446 PICKUP_GIANT(); 447 break; 448 default: 449 error = ENOTTY; 450 break; 451 } 452 return (error); 453 454} 455