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 .ctlreq = g_nop_config, 60 .destroy_geom = g_nop_destroy_geom 61 }; 62 63 64 static void 65 g_nop_orphan(struct g_consumer *cp) 66 { 67 68 g_topology_assert(); 69 g_nop_destroy(cp->geom, 1); 70 } 71 72 static void 73 g_nop_start(struct bio *bp) 74 { 75 struct g_nop_softc *sc; 76 struct g_geom *gp; 77 struct g_provider *pp; 78 struct bio *cbp; 79 80 gp = bp->bio_to->geom; 81 sc = gp->softc; 82 G_NOP_LOGREQ(bp, "Request received."); 83 cbp = g_clone_bio(bp); 84 if (cbp == NULL) { 85 g_io_deliver(bp, ENOMEM); 86 return; 87 } 88 pp = LIST_FIRST(&gp->provider); 89 KASSERT(pp != NULL, ("NULL pp")); 90 if (sc->sc_failprob > 0) { 91 u_int rval; 92 93 rval = arc4random() % 100; 94 if (rval < sc->sc_failprob) { 95 g_io_deliver(bp, EIO); 96 return; 97 } 98 } 99 cbp->bio_done = g_std_done; 100 cbp->bio_offset = bp->bio_offset + sc->sc_offset; 101 cbp->bio_data = bp->bio_data; 102 cbp->bio_length = bp->bio_length; 103 cbp->bio_to = LIST_FIRST(&gp->provider); 104 G_NOP_LOGREQ(cbp, "Sending request."); 105 g_io_request(cbp, LIST_FIRST(&gp->consumer)); 106 } 107 108 static int 109 g_nop_access(struct g_provider *pp, int dr, int dw, int de) 110 { 111 struct g_geom *gp; 112 struct g_consumer *cp; 113 int error; 114 115 gp = pp->geom; 116 cp = LIST_FIRST(&gp->consumer); 117 error = g_access(cp, dr, dw, de); 118 119 return (error); 120 } 121 122 static int 123 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp, 124 u_int failprob, off_t offset, off_t size) 125 { 126 struct g_nop_softc *sc; 127 struct g_geom *gp; 128 struct g_provider *newpp; 129 struct g_consumer *cp; 130 char name[64]; 131 int error; 132 133 g_topology_assert(); 134 135 gp = NULL; 136 newpp = NULL; 137 cp = NULL; 138 139 if ((offset % pp->sectorsize) != 0) { 140 gctl_error(req, "Invalid offset for provider %s.", pp->name); 141 return (EINVAL); 142 } 143 if ((size % pp->sectorsize) != 0) { 144 gctl_error(req, "Invalid size for provider %s.", pp->name); 145 return (EINVAL); 146 } 147 if (offset >= pp->mediasize) { 148 gctl_error(req, "Invalid offset for provider %s.", pp->name); 149 return (EINVAL); 150 } 151 if (size == 0) 152 size = pp->mediasize - offset; 153 if (offset + size > pp->mediasize) { 154 gctl_error(req, "Invalid size for provider %s.", pp->name); 155 return (EINVAL); 156 } 157 snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX); 158 LIST_FOREACH(gp, &mp->geom, geom) { 159 if (strcmp(gp->name, name) == 0) { 160 gctl_error(req, "Provider %s already exists.", name); 161 return (EEXIST); 162 } 163 } 164 gp = g_new_geomf(mp, name); 165 if (gp == NULL) { 166 gctl_error(req, "Cannot create geom %s.", name); 167 return (ENOMEM); 168 } 169 sc = g_malloc(sizeof(*sc), M_WAITOK); 170 sc->sc_offset = offset; 171 sc->sc_failprob = failprob; 172 gp->softc = sc; 173 gp->start = g_nop_start; 174 gp->spoiled = g_nop_orphan; 175 gp->orphan = g_nop_orphan; 176 gp->access = g_nop_access; 177 gp->dumpconf = g_nop_dumpconf; 178 179 newpp = g_new_providerf(gp, gp->name); 180 if (newpp == NULL) { 181 gctl_error(req, "Cannot create provider %s.", name); 182 error = ENOMEM; 183 goto fail; 184 } 185 newpp->mediasize = size; 186 newpp->sectorsize = pp->sectorsize; 187 188 cp = g_new_consumer(gp); 189 if (cp == NULL) { 190 gctl_error(req, "Cannot create consumer for %s.", gp->name); 191 error = ENOMEM; 192 goto fail; 193 } 194 error = g_attach(cp, pp); 195 if (error != 0) { 196 gctl_error(req, "Cannot attach to provider %s.", pp->name); 197 goto fail; 198 } 199 200 g_error_provider(newpp, 0); 201 G_NOP_DEBUG(0, "Device %s created.", gp->name); 202 return (0); 203 fail: 204 if (cp != NULL) { 205 if (cp->provider != NULL) 206 g_detach(cp); 207 g_destroy_consumer(cp); 208 } 209 if (newpp != NULL) 210 g_destroy_provider(pp); 211 if (gp != NULL) { 212 if (gp->softc != NULL) 213 g_free(gp->softc); 214 g_destroy_geom(gp); 215 } 216 return (error); 217 } 218 219 static int 220 g_nop_destroy(struct g_geom *gp, boolean_t force) 221 { 222 struct g_provider *pp; 223 224 g_topology_assert(); 225 pp = LIST_FIRST(&gp->provider); 226 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 227 if (force) { 228 G_NOP_DEBUG(0, "Device %s is still open, so it " 229 "can't be definitely removed.", pp->name); 230 } else { 231 G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).", 232 pp->name, pp->acr, pp->acw, pp->ace); 233 return (EBUSY); 234 } 235 } else { 236 G_NOP_DEBUG(0, "Device %s removed.", gp->name); 237 } 238 g_free(gp->softc); 239 gp->softc = NULL; 240 g_wither_geom(gp, ENXIO); 241 242 return (0); 243 } 244 245 static int 246 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp) 247 { 248 249 return (g_nop_destroy(gp, 0)); 250 } 251 252 static void 253 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp) 254 { 255 struct g_provider *pp; 256 intmax_t *failprob, *offset, *size; 257 const char *name; 258 char param[16]; 259 int i, *nargs; 260 261 g_topology_assert(); 262 263 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 264 if (nargs == NULL) { 265 gctl_error(req, "No '%s' argument", "nargs"); 266 return; 267 } 268 if (*nargs <= 0) { 269 gctl_error(req, "Missing device(s)."); 270 return; 271 } 272 failprob = gctl_get_paraml(req, "failprob", sizeof(*failprob)); 273 if (failprob == NULL) { 274 gctl_error(req, "No '%s' argument", "failprob"); 275 return; 276 } 277 if (*failprob < 0 || *failprob > 100) { 278 gctl_error(req, "Invalid '%s' argument", "failprob"); 279 return; 280 } 281 offset = gctl_get_paraml(req, "offset", sizeof(*offset)); 282 if (offset == NULL) { 283 gctl_error(req, "No '%s' argument", "offset"); 284 return; 285 } 286 if (*offset < 0) { 287 gctl_error(req, "Invalid '%s' argument", "offset"); 288 return; 289 } 290 size = gctl_get_paraml(req, "size", sizeof(*size)); 291 if (size == NULL) { 292 gctl_error(req, "No '%s' argument", "size"); 293 return; 294 } 295 if (*size < 0) { 296 gctl_error(req, "Invalid '%s' argument", "size"); 297 return; 298 } 299 300 for (i = 0; i < *nargs; i++) { 301 snprintf(param, sizeof(param), "arg%d", i); 302 name = gctl_get_asciiparam(req, param); 303 if (name == NULL) { 304 gctl_error(req, "No 'arg%d' argument", i); 305 return; 306 } 307 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 308 name += strlen("/dev/"); 309 pp = g_provider_by_name(name); 310 if (pp == NULL) { 311 G_NOP_DEBUG(1, "Provider %s is invalid.", name); 312 gctl_error(req, "Provider %s is invalid.", name); 313 return; 314 } 315 if (g_nop_create(req, mp, pp, (u_int)*failprob, (off_t)*offset, 316 (off_t)*size) != 0) { 317 return; 318 } 319 } 320 } 321 322 static void 323 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp) 324 { 325 struct g_nop_softc *sc; 326 struct g_provider *pp; 327 intmax_t *failprob; 328 const char *name; 329 char param[16]; 330 int i, *nargs; 331 332 g_topology_assert(); 333 334 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 335 if (nargs == NULL) { 336 gctl_error(req, "No '%s' argument", "nargs"); 337 return; 338 } 339 if (*nargs <= 0) { 340 gctl_error(req, "Missing device(s)."); 341 return; 342 } 343 failprob = gctl_get_paraml(req, "failprob", sizeof(*failprob)); 344 if (failprob == NULL) { 345 gctl_error(req, "No '%s' argument", "failprob"); 346 return; 347 } 348 if (*failprob < 0 || *failprob > 100) { 349 gctl_error(req, "Invalid '%s' argument", "failprob"); 350 return; 351 } 352 353 for (i = 0; i < *nargs; i++) { 354 snprintf(param, sizeof(param), "arg%d", i); 355 name = gctl_get_asciiparam(req, param); 356 if (name == NULL) { 357 gctl_error(req, "No 'arg%d' argument", i); 358 return; 359 } 360 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 361 name += strlen("/dev/"); 362 pp = g_provider_by_name(name); 363 if (pp == NULL || pp->geom->class != mp) { 364 G_NOP_DEBUG(1, "Provider %s is invalid.", name); 365 gctl_error(req, "Provider %s is invalid.", name); 366 return; 367 } 368 sc = pp->geom->softc; 369 sc->sc_failprob = (u_int)*failprob; 370 } 371 } 372 373 static struct g_geom * 374 g_nop_find_geom(struct g_class *mp, const char *name) 375 { 376 struct g_geom *gp; 377 378 LIST_FOREACH(gp, &mp->geom, geom) { 379 if (strcmp(gp->name, name) == 0) 380 return (gp); 381 } 382 return (NULL); 383 } 384 385 static void 386 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp) 387 { 388 int *nargs, *force, error, i; 389 struct g_geom *gp; 390 const char *name; 391 char param[16]; 392 393 g_topology_assert(); 394 395 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 396 if (nargs == NULL) { 397 gctl_error(req, "No '%s' argument", "nargs"); 398 return; 399 } 400 if (*nargs <= 0) { 401 gctl_error(req, "Missing device(s)."); 402 return; 403 } 404 force = gctl_get_paraml(req, "force", sizeof(*force)); 405 if (force == NULL) { 406 gctl_error(req, "No 'force' argument"); 407 return; 408 } 409 410 for (i = 0; i < *nargs; i++) { 411 snprintf(param, sizeof(param), "arg%d", i); 412 name = gctl_get_asciiparam(req, param); 413 if (name == NULL) { 414 gctl_error(req, "No 'arg%d' argument", i); 415 return; 416 } 417 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 418 name += strlen("/dev/"); 419 gp = g_nop_find_geom(mp, name); 420 if (gp == NULL) { 421 G_NOP_DEBUG(1, "Device %s is invalid.", name); 422 gctl_error(req, "Device %s is invalid.", name); 423 return; 424 } 425 error = g_nop_destroy(gp, *force); 426 if (error != 0) { 427 gctl_error(req, "Cannot destroy device %s (error=%d).", 428 gp->name, error); 429 return; 430 } 431 } 432 } 433 434 static void 435 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb) 436 { 437 uint32_t *version; 438 439 g_topology_assert(); 440 441 version = gctl_get_paraml(req, "version", sizeof(*version)); 442 if (version == NULL) { 443 gctl_error(req, "No '%s' argument.", "version"); 444 return; 445 } 446 if (*version != G_NOP_VERSION) { 447 gctl_error(req, "Userland and kernel parts are out of sync."); 448 return; 449 } 450 451 if (strcmp(verb, "create") == 0) { 452 g_nop_ctl_create(req, mp); 453 return; 454 } else if (strcmp(verb, "configure") == 0) { 455 g_nop_ctl_configure(req, mp); 456 return; 457 } else if (strcmp(verb, "destroy") == 0) { 458 g_nop_ctl_destroy(req, mp); 459 return; 460 } 461 462 gctl_error(req, "Unknown verb."); 463 } 464 465 static void 466 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 467 struct g_consumer *cp, struct g_provider *pp) 468 { 469 struct g_nop_softc *sc; 470 471 if (pp != NULL || cp != NULL) 472 return; 473 sc = gp->softc; 474 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent, 475 (intmax_t)sc->sc_offset); 476 sbuf_printf(sb, "%s<Failprob>%u</Failprob>\n", indent, sc->sc_failprob); 477 } 478 479 DECLARE_GEOM_CLASS(g_nop_class, g_nop); 480