1 /*- 2 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/lock.h> 35 #include <sys/mutex.h> 36 #include <sys/bio.h> 37 #include <sys/sysctl.h> 38 #include <sys/malloc.h> 39 #include <geom/geom.h> 40 #include <geom/nop/g_nop.h> 41 42 43 SYSCTL_DECL(_kern_geom); 44 SYSCTL_NODE(_kern_geom, OID_AUTO, nop, CTLFLAG_RW, 0, "GEOM_NOP stuff"); 45 static u_int g_nop_debug = 0; 46 SYSCTL_UINT(_kern_geom_nop, OID_AUTO, debug, CTLFLAG_RW, &g_nop_debug, 0, 47 "Debug level"); 48 49 static int g_nop_destroy(struct g_geom *gp, boolean_t force); 50 static int g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, 51 struct g_geom *gp); 52 static void g_nop_config(struct gctl_req *req, struct g_class *mp, 53 const char *verb); 54 static void g_nop_dumpconf(struct sbuf *sb, const char *indent, 55 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp); 56 57 struct g_class g_nop_class = { 58 .name = G_NOP_CLASS_NAME, 59 .version = G_VERSION, 60 .ctlreq = g_nop_config, 61 .destroy_geom = g_nop_destroy_geom 62 }; 63 64 65 static void 66 g_nop_orphan(struct g_consumer *cp) 67 { 68 69 g_topology_assert(); 70 g_nop_destroy(cp->geom, 1); 71 } 72 73 static void 74 g_nop_start(struct bio *bp) 75 { 76 struct g_nop_softc *sc; 77 struct g_geom *gp; 78 struct g_provider *pp; 79 struct bio *cbp; 80 81 gp = bp->bio_to->geom; 82 sc = gp->softc; 83 G_NOP_LOGREQ(bp, "Request received."); 84 cbp = g_clone_bio(bp); 85 if (cbp == NULL) { 86 g_io_deliver(bp, ENOMEM); 87 return; 88 } 89 if (sc->sc_failprob > 0) { 90 u_int rval; 91 92 rval = arc4random() % 100; 93 if (rval < sc->sc_failprob) { 94 g_io_deliver(bp, EIO); 95 return; 96 } 97 } 98 cbp->bio_done = g_std_done; 99 cbp->bio_offset = bp->bio_offset + sc->sc_offset; 100 cbp->bio_data = bp->bio_data; 101 cbp->bio_length = bp->bio_length; 102 pp = LIST_FIRST(&gp->provider); 103 KASSERT(pp != NULL, ("NULL pp")); 104 cbp->bio_to = pp; 105 G_NOP_LOGREQ(cbp, "Sending request."); 106 g_io_request(cbp, LIST_FIRST(&gp->consumer)); 107 } 108 109 static int 110 g_nop_access(struct g_provider *pp, int dr, int dw, int de) 111 { 112 struct g_geom *gp; 113 struct g_consumer *cp; 114 int error; 115 116 gp = pp->geom; 117 cp = LIST_FIRST(&gp->consumer); 118 error = g_access(cp, dr, dw, de); 119 120 return (error); 121 } 122 123 static int 124 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp, 125 u_int failprob, off_t offset, off_t size, u_int secsize) 126 { 127 struct g_nop_softc *sc; 128 struct g_geom *gp; 129 struct g_provider *newpp; 130 struct g_consumer *cp; 131 char name[64]; 132 int error; 133 134 g_topology_assert(); 135 136 gp = NULL; 137 newpp = NULL; 138 cp = NULL; 139 140 if ((offset % pp->sectorsize) != 0) { 141 gctl_error(req, "Invalid offset for provider %s.", pp->name); 142 return (EINVAL); 143 } 144 if ((size % pp->sectorsize) != 0) { 145 gctl_error(req, "Invalid size for provider %s.", pp->name); 146 return (EINVAL); 147 } 148 if (offset >= pp->mediasize) { 149 gctl_error(req, "Invalid offset for provider %s.", pp->name); 150 return (EINVAL); 151 } 152 if (size == 0) 153 size = pp->mediasize - offset; 154 if (offset + size > pp->mediasize) { 155 gctl_error(req, "Invalid size for provider %s.", pp->name); 156 return (EINVAL); 157 } 158 if (secsize == 0) 159 secsize = pp->sectorsize; 160 else if ((secsize % pp->sectorsize) != 0) { 161 gctl_error(req, "Invalid secsize for provider %s.", pp->name); 162 return (EINVAL); 163 } 164 snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX); 165 LIST_FOREACH(gp, &mp->geom, geom) { 166 if (strcmp(gp->name, name) == 0) { 167 gctl_error(req, "Provider %s already exists.", name); 168 return (EEXIST); 169 } 170 } 171 gp = g_new_geomf(mp, name); 172 if (gp == NULL) { 173 gctl_error(req, "Cannot create geom %s.", name); 174 return (ENOMEM); 175 } 176 sc = g_malloc(sizeof(*sc), M_WAITOK); 177 sc->sc_offset = offset; 178 sc->sc_failprob = failprob; 179 gp->softc = sc; 180 gp->start = g_nop_start; 181 gp->orphan = g_nop_orphan; 182 gp->access = g_nop_access; 183 gp->dumpconf = g_nop_dumpconf; 184 185 newpp = g_new_providerf(gp, gp->name); 186 if (newpp == NULL) { 187 gctl_error(req, "Cannot create provider %s.", name); 188 error = ENOMEM; 189 goto fail; 190 } 191 newpp->mediasize = size; 192 newpp->sectorsize = secsize; 193 194 cp = g_new_consumer(gp); 195 if (cp == NULL) { 196 gctl_error(req, "Cannot create consumer for %s.", gp->name); 197 error = ENOMEM; 198 goto fail; 199 } 200 error = g_attach(cp, pp); 201 if (error != 0) { 202 gctl_error(req, "Cannot attach to provider %s.", pp->name); 203 goto fail; 204 } 205 206 g_error_provider(newpp, 0); 207 G_NOP_DEBUG(0, "Device %s created.", gp->name); 208 return (0); 209 fail: 210 if (cp != NULL) { 211 if (cp->provider != NULL) 212 g_detach(cp); 213 g_destroy_consumer(cp); 214 } 215 if (newpp != NULL) 216 g_destroy_provider(pp); 217 if (gp != NULL) { 218 if (gp->softc != NULL) 219 g_free(gp->softc); 220 g_destroy_geom(gp); 221 } 222 return (error); 223 } 224 225 static int 226 g_nop_destroy(struct g_geom *gp, boolean_t force) 227 { 228 struct g_provider *pp; 229 230 g_topology_assert(); 231 if (gp->softc == NULL) 232 return (ENXIO); 233 pp = LIST_FIRST(&gp->provider); 234 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 235 if (force) { 236 G_NOP_DEBUG(0, "Device %s is still open, so it " 237 "can't be definitely removed.", pp->name); 238 } else { 239 G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).", 240 pp->name, pp->acr, pp->acw, pp->ace); 241 return (EBUSY); 242 } 243 } else { 244 G_NOP_DEBUG(0, "Device %s removed.", gp->name); 245 } 246 g_free(gp->softc); 247 gp->softc = NULL; 248 g_wither_geom(gp, ENXIO); 249 250 return (0); 251 } 252 253 static int 254 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp) 255 { 256 257 return (g_nop_destroy(gp, 0)); 258 } 259 260 static void 261 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp) 262 { 263 struct g_provider *pp; 264 intmax_t *failprob, *offset, *secsize, *size; 265 const char *name; 266 char param[16]; 267 int i, *nargs; 268 269 g_topology_assert(); 270 271 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 272 if (nargs == NULL) { 273 gctl_error(req, "No '%s' argument", "nargs"); 274 return; 275 } 276 if (*nargs <= 0) { 277 gctl_error(req, "Missing device(s)."); 278 return; 279 } 280 failprob = gctl_get_paraml(req, "failprob", sizeof(*failprob)); 281 if (failprob == NULL) { 282 gctl_error(req, "No '%s' argument", "failprob"); 283 return; 284 } 285 if (*failprob < 0 || *failprob > 100) { 286 gctl_error(req, "Invalid '%s' argument", "failprob"); 287 return; 288 } 289 offset = gctl_get_paraml(req, "offset", sizeof(*offset)); 290 if (offset == NULL) { 291 gctl_error(req, "No '%s' argument", "offset"); 292 return; 293 } 294 if (*offset < 0) { 295 gctl_error(req, "Invalid '%s' argument", "offset"); 296 return; 297 } 298 size = gctl_get_paraml(req, "size", sizeof(*size)); 299 if (size == NULL) { 300 gctl_error(req, "No '%s' argument", "size"); 301 return; 302 } 303 if (*size < 0) { 304 gctl_error(req, "Invalid '%s' argument", "size"); 305 return; 306 } 307 secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize)); 308 if (secsize == NULL) { 309 gctl_error(req, "No '%s' argument", "secsize"); 310 return; 311 } 312 if (*secsize < 0) { 313 gctl_error(req, "Invalid '%s' argument", "secsize"); 314 return; 315 } 316 317 for (i = 0; i < *nargs; i++) { 318 snprintf(param, sizeof(param), "arg%d", i); 319 name = gctl_get_asciiparam(req, param); 320 if (name == NULL) { 321 gctl_error(req, "No 'arg%d' argument", i); 322 return; 323 } 324 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 325 name += strlen("/dev/"); 326 pp = g_provider_by_name(name); 327 if (pp == NULL) { 328 G_NOP_DEBUG(1, "Provider %s is invalid.", name); 329 gctl_error(req, "Provider %s is invalid.", name); 330 return; 331 } 332 if (g_nop_create(req, mp, pp, (u_int)*failprob, (off_t)*offset, 333 (off_t)*size, (u_int)*secsize) != 0) { 334 return; 335 } 336 } 337 } 338 339 static void 340 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp) 341 { 342 struct g_nop_softc *sc; 343 struct g_provider *pp; 344 intmax_t *failprob; 345 const char *name; 346 char param[16]; 347 int i, *nargs; 348 349 g_topology_assert(); 350 351 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 352 if (nargs == NULL) { 353 gctl_error(req, "No '%s' argument", "nargs"); 354 return; 355 } 356 if (*nargs <= 0) { 357 gctl_error(req, "Missing device(s)."); 358 return; 359 } 360 failprob = gctl_get_paraml(req, "failprob", sizeof(*failprob)); 361 if (failprob == NULL) { 362 gctl_error(req, "No '%s' argument", "failprob"); 363 return; 364 } 365 if (*failprob < 0 || *failprob > 100) { 366 gctl_error(req, "Invalid '%s' argument", "failprob"); 367 return; 368 } 369 370 for (i = 0; i < *nargs; i++) { 371 snprintf(param, sizeof(param), "arg%d", i); 372 name = gctl_get_asciiparam(req, param); 373 if (name == NULL) { 374 gctl_error(req, "No 'arg%d' argument", i); 375 return; 376 } 377 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 378 name += strlen("/dev/"); 379 pp = g_provider_by_name(name); 380 if (pp == NULL || pp->geom->class != mp) { 381 G_NOP_DEBUG(1, "Provider %s is invalid.", name); 382 gctl_error(req, "Provider %s is invalid.", name); 383 return; 384 } 385 sc = pp->geom->softc; 386 sc->sc_failprob = (u_int)*failprob; 387 } 388 } 389 390 static struct g_geom * 391 g_nop_find_geom(struct g_class *mp, const char *name) 392 { 393 struct g_geom *gp; 394 395 LIST_FOREACH(gp, &mp->geom, geom) { 396 if (strcmp(gp->name, name) == 0) 397 return (gp); 398 } 399 return (NULL); 400 } 401 402 static void 403 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp) 404 { 405 int *nargs, *force, error, i; 406 struct g_geom *gp; 407 const char *name; 408 char param[16]; 409 410 g_topology_assert(); 411 412 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 413 if (nargs == NULL) { 414 gctl_error(req, "No '%s' argument", "nargs"); 415 return; 416 } 417 if (*nargs <= 0) { 418 gctl_error(req, "Missing device(s)."); 419 return; 420 } 421 force = gctl_get_paraml(req, "force", sizeof(*force)); 422 if (force == NULL) { 423 gctl_error(req, "No 'force' argument"); 424 return; 425 } 426 427 for (i = 0; i < *nargs; i++) { 428 snprintf(param, sizeof(param), "arg%d", i); 429 name = gctl_get_asciiparam(req, param); 430 if (name == NULL) { 431 gctl_error(req, "No 'arg%d' argument", i); 432 return; 433 } 434 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 435 name += strlen("/dev/"); 436 gp = g_nop_find_geom(mp, name); 437 if (gp == NULL) { 438 G_NOP_DEBUG(1, "Device %s is invalid.", name); 439 gctl_error(req, "Device %s is invalid.", name); 440 return; 441 } 442 error = g_nop_destroy(gp, *force); 443 if (error != 0) { 444 gctl_error(req, "Cannot destroy device %s (error=%d).", 445 gp->name, error); 446 return; 447 } 448 } 449 } 450 451 static void 452 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb) 453 { 454 uint32_t *version; 455 456 g_topology_assert(); 457 458 version = gctl_get_paraml(req, "version", sizeof(*version)); 459 if (version == NULL) { 460 gctl_error(req, "No '%s' argument.", "version"); 461 return; 462 } 463 if (*version != G_NOP_VERSION) { 464 gctl_error(req, "Userland and kernel parts are out of sync."); 465 return; 466 } 467 468 if (strcmp(verb, "create") == 0) { 469 g_nop_ctl_create(req, mp); 470 return; 471 } else if (strcmp(verb, "configure") == 0) { 472 g_nop_ctl_configure(req, mp); 473 return; 474 } else if (strcmp(verb, "destroy") == 0) { 475 g_nop_ctl_destroy(req, mp); 476 return; 477 } 478 479 gctl_error(req, "Unknown verb."); 480 } 481 482 static void 483 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 484 struct g_consumer *cp, struct g_provider *pp) 485 { 486 struct g_nop_softc *sc; 487 488 if (pp != NULL || cp != NULL) 489 return; 490 sc = gp->softc; 491 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent, 492 (intmax_t)sc->sc_offset); 493 sbuf_printf(sb, "%s<Failprob>%u</Failprob>\n", indent, sc->sc_failprob); 494 } 495 496 DECLARE_GEOM_CLASS(g_nop_class, g_nop); 497