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 <sys/bitstring.h> 40 #include <vm/uma.h> 41 #include <machine/atomic.h> 42 #include <geom/geom.h> 43 #include <sys/proc.h> 44 #include <sys/kthread.h> 45 #include <geom/mirror/g_mirror.h> 46 47 48 static struct g_mirror_softc * 49 g_mirror_find_device(struct g_class *mp, const char *name) 50 { 51 struct g_mirror_softc *sc; 52 struct g_geom *gp; 53 54 g_topology_assert(); 55 LIST_FOREACH(gp, &mp->geom, geom) { 56 sc = gp->softc; 57 if (sc == NULL) 58 continue; 59 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) 60 continue; 61 if (strcmp(gp->name, name) == 0 || 62 strcmp(sc->sc_name, name) == 0) { 63 return (sc); 64 } 65 } 66 return (NULL); 67 } 68 69 static struct g_mirror_disk * 70 g_mirror_find_disk(struct g_mirror_softc *sc, const char *name) 71 { 72 struct g_mirror_disk *disk; 73 74 g_topology_assert(); 75 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 76 if (disk->d_consumer == NULL) 77 continue; 78 if (disk->d_consumer->provider == NULL) 79 continue; 80 if (strcmp(disk->d_consumer->provider->name, name) == 0) 81 return (disk); 82 } 83 return (NULL); 84 } 85 86 static void 87 g_mirror_ctl_configure(struct gctl_req *req, struct g_class *mp) 88 { 89 struct g_mirror_softc *sc; 90 struct g_mirror_disk *disk; 91 const char *name, *balancep; 92 intmax_t *slicep; 93 uint32_t slice; 94 uint8_t balance; 95 int *nargs, *autosync, *noautosync, *hardcode, *dynamic, do_sync = 0; 96 97 g_topology_assert(); 98 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 99 if (*nargs != 1) { 100 gctl_error(req, "Invalid number of arguments."); 101 return; 102 } 103 name = gctl_get_asciiparam(req, "arg0"); 104 sc = g_mirror_find_device(mp, name); 105 if (sc == NULL) { 106 gctl_error(req, "No such device: %s.", name); 107 return; 108 } 109 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) { 110 gctl_error(req, "Not all disks connected."); 111 return; 112 } 113 balancep = gctl_get_asciiparam(req, "balance"); 114 if (strcmp(balancep, "none") == 0) 115 balance = sc->sc_balance; 116 else { 117 if (balance_id(balancep) == -1) { 118 gctl_error(req, "Invalid balance algorithm."); 119 return; 120 } 121 balance = balance_id(balancep); 122 } 123 slicep = gctl_get_paraml(req, "slice", sizeof(*slicep)); 124 if (slicep == NULL) { 125 gctl_error(req, "No '%s' argument.", "slice"); 126 return; 127 } 128 if (*slicep == -1) 129 slice = sc->sc_slice; 130 else 131 slice = *slicep; 132 autosync = gctl_get_paraml(req, "autosync", sizeof(*autosync)); 133 if (autosync == NULL) { 134 gctl_error(req, "No '%s' argument.", "autosync"); 135 return; 136 } 137 noautosync = gctl_get_paraml(req, "noautosync", sizeof(*noautosync)); 138 if (noautosync == NULL) { 139 gctl_error(req, "No '%s' argument.", "noautosync"); 140 return; 141 } 142 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode)); 143 if (hardcode == NULL) { 144 gctl_error(req, "No '%s' argument.", "hardcode"); 145 return; 146 } 147 dynamic = gctl_get_paraml(req, "dynamic", sizeof(*dynamic)); 148 if (dynamic == NULL) { 149 gctl_error(req, "No '%s' argument.", "dynamic"); 150 return; 151 } 152 if (sc->sc_balance == balance && sc->sc_slice == slice && !*autosync && 153 !*noautosync && !*hardcode && !*dynamic) { 154 gctl_error(req, "Nothing has changed."); 155 return; 156 } 157 if (*autosync && *noautosync) { 158 gctl_error(req, "'%s' and '%s' specified.", "autosync", 159 "noautosync"); 160 return; 161 } 162 if (*hardcode && *dynamic) { 163 gctl_error(req, "'%s' and '%s' specified.", "hardcode", 164 "dynamic"); 165 return; 166 } 167 sc->sc_balance = balance; 168 sc->sc_slice = slice; 169 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) { 170 if (*autosync) { 171 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_NOAUTOSYNC; 172 do_sync = 1; 173 } 174 } else { 175 if (*noautosync) 176 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_NOAUTOSYNC; 177 } 178 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 179 if (do_sync) { 180 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) 181 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC; 182 } 183 if (*hardcode) 184 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED; 185 else if (*dynamic) 186 disk->d_flags &= ~G_MIRROR_DISK_FLAG_HARDCODED; 187 g_mirror_update_metadata(disk); 188 if (do_sync) { 189 if (disk->d_state == G_MIRROR_DISK_STATE_STALE) { 190 g_mirror_event_send(disk, 191 G_MIRROR_DISK_STATE_DISCONNECTED, 192 G_MIRROR_EVENT_DONTWAIT); 193 } 194 } 195 } 196 } 197 198 static void 199 g_mirror_ctl_rebuild(struct gctl_req *req, struct g_class *mp) 200 { 201 struct g_mirror_softc *sc; 202 struct g_mirror_disk *disk; 203 const char *name; 204 char param[16]; 205 int *nargs; 206 u_int i; 207 208 g_topology_assert(); 209 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 210 if (nargs == NULL) { 211 gctl_error(req, "No '%s' argument.", "nargs"); 212 return; 213 } 214 if (*nargs < 2) { 215 gctl_error(req, "Too few arguments."); 216 return; 217 } 218 name = gctl_get_asciiparam(req, "arg0"); 219 if (name == NULL) { 220 gctl_error(req, "No 'arg%u' argument.", 0); 221 return; 222 } 223 sc = g_mirror_find_device(mp, name); 224 if (sc == NULL) { 225 gctl_error(req, "No such device: %s.", name); 226 return; 227 } 228 229 for (i = 1; i < (u_int)*nargs; i++) { 230 snprintf(param, sizeof(param), "arg%u", i); 231 name = gctl_get_asciiparam(req, param); 232 if (name == NULL) { 233 gctl_error(req, "No 'arg%u' argument.", i); 234 return; 235 } 236 disk = g_mirror_find_disk(sc, name); 237 if (disk == NULL) { 238 gctl_error(req, "No such provider: %s.", name); 239 return; 240 } 241 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1 && 242 disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) { 243 /* 244 * This is the last active disk. There will be nothing 245 * to rebuild it from, so deny this request. 246 */ 247 gctl_error(req, 248 "Provider %s is the last active provider in %s.", 249 name, sc->sc_geom->name); 250 return; 251 } 252 /* 253 * Do rebuild by resetting syncid and disconnecting disk. 254 * It'll be retasted, connected to the mirror and 255 * synchronized. 256 */ 257 disk->d_sync.ds_syncid = 0; 258 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0) 259 disk->d_flags |= G_MIRROR_DISK_FLAG_FORCE_SYNC; 260 g_mirror_update_metadata(disk); 261 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED, 262 G_MIRROR_EVENT_WAIT); 263 } 264 } 265 266 static void 267 g_mirror_ctl_insert(struct gctl_req *req, struct g_class *mp) 268 { 269 struct g_mirror_softc *sc; 270 struct g_mirror_disk *disk; 271 struct g_mirror_metadata md; 272 struct g_provider *pp; 273 struct g_consumer *cp; 274 intmax_t *priority; 275 const char *name; 276 char param[16]; 277 u_char *sector; 278 u_int i, n; 279 int error, *nargs, *hardcode, *inactive; 280 struct { 281 struct g_provider *provider; 282 struct g_consumer *consumer; 283 } *disks; 284 285 g_topology_assert(); 286 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 287 if (nargs == NULL) { 288 gctl_error(req, "No '%s' argument.", "nargs"); 289 return; 290 } 291 if (*nargs < 2) { 292 gctl_error(req, "Too few arguments."); 293 return; 294 } 295 priority = gctl_get_paraml(req, "priority", sizeof(*priority)); 296 if (priority == NULL) { 297 gctl_error(req, "No '%s' argument.", "priority"); 298 return; 299 } 300 inactive = gctl_get_paraml(req, "inactive", sizeof(*inactive)); 301 if (inactive == NULL) { 302 gctl_error(req, "No '%s' argument.", "inactive"); 303 return; 304 } 305 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode)); 306 if (hardcode == NULL) { 307 gctl_error(req, "No '%s' argument.", "hardcode"); 308 return; 309 } 310 name = gctl_get_asciiparam(req, "arg0"); 311 if (name == NULL) { 312 gctl_error(req, "No 'arg%u' argument.", 0); 313 return; 314 } 315 sc = g_mirror_find_device(mp, name); 316 if (sc == NULL) { 317 gctl_error(req, "No such device: %s.", name); 318 return; 319 } 320 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) { 321 gctl_error(req, "Not all disks connected."); 322 return; 323 } 324 325 disks = g_malloc(sizeof(*disks) * (*nargs), M_WAITOK | M_ZERO); 326 for (i = 1, n = 0; i < (u_int)*nargs; i++) { 327 snprintf(param, sizeof(param), "arg%u", i); 328 name = gctl_get_asciiparam(req, param); 329 if (name == NULL) { 330 gctl_error(req, "No 'arg%u' argument.", i); 331 continue; 332 } 333 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 334 name += strlen("/dev/"); 335 if (g_mirror_find_disk(sc, name) != NULL) { 336 gctl_error(req, "Provider %s already inserted.", name); 337 continue; 338 } 339 pp = g_provider_by_name(name); 340 if (pp == NULL) { 341 gctl_error(req, "Unknown provider %s.", name); 342 continue; 343 } 344 if (sc->sc_provider->mediasize > pp->mediasize) { 345 gctl_error(req, "Provider %s too small.", name); 346 continue; 347 } 348 if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) { 349 gctl_error(req, "Invalid sectorsize of provider %s.", 350 name); 351 continue; 352 } 353 cp = g_new_consumer(sc->sc_geom); 354 if (g_attach(cp, pp) != 0) { 355 g_destroy_consumer(cp); 356 gctl_error(req, "Cannot attach to provider %s.", name); 357 continue; 358 } 359 if (g_access(cp, 0, 1, 1) != 0) { 360 g_detach(cp); 361 g_destroy_consumer(cp); 362 gctl_error(req, "Cannot access provider %s.", name); 363 continue; 364 } 365 disks[n].provider = pp; 366 disks[n].consumer = cp; 367 n++; 368 } 369 if (n == 0) { 370 g_free(disks); 371 return; 372 } 373 sc->sc_ndisks += n; 374 again: 375 for (i = 0; i < n; i++) { 376 if (disks[i].consumer == NULL) 377 continue; 378 g_mirror_fill_metadata(sc, NULL, &md); 379 md.md_priority = *priority; 380 if (*inactive) 381 md.md_dflags |= G_MIRROR_DISK_FLAG_INACTIVE; 382 pp = disks[i].provider; 383 if (*hardcode) { 384 strlcpy(md.md_provider, pp->name, 385 sizeof(md.md_provider)); 386 } else { 387 bzero(md.md_provider, sizeof(md.md_provider)); 388 } 389 sector = g_malloc(pp->sectorsize, M_WAITOK); 390 mirror_metadata_encode(&md, sector); 391 error = g_write_data(disks[i].consumer, 392 pp->mediasize - pp->sectorsize, sector, pp->sectorsize); 393 g_free(sector); 394 if (error != 0) { 395 gctl_error(req, "Cannot store metadata on %s.", 396 pp->name); 397 g_access(disks[i].consumer, 0, -1, -1); 398 g_detach(disks[i].consumer); 399 g_destroy_consumer(disks[i].consumer); 400 disks[i].consumer = NULL; 401 disks[i].provider = NULL; 402 sc->sc_ndisks--; 403 goto again; 404 } 405 } 406 if (i == 0) { 407 /* All writes failed. */ 408 g_free(disks); 409 return; 410 } 411 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 412 g_mirror_update_metadata(disk); 413 } 414 /* 415 * Release provider and wait for retaste. 416 */ 417 for (i = 0; i < n; i++) { 418 if (disks[i].consumer == NULL) 419 continue; 420 g_access(disks[i].consumer, 0, -1, -1); 421 g_detach(disks[i].consumer); 422 g_destroy_consumer(disks[i].consumer); 423 } 424 g_free(disks); 425 } 426 427 static void 428 g_mirror_ctl_remove(struct gctl_req *req, struct g_class *mp) 429 { 430 struct g_mirror_softc *sc; 431 struct g_mirror_disk *disk; 432 const char *name; 433 char param[16]; 434 int *nargs; 435 u_int i; 436 437 g_topology_assert(); 438 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 439 if (nargs == NULL) { 440 gctl_error(req, "No '%s' argument.", "nargs"); 441 return; 442 } 443 if (*nargs < 2) { 444 gctl_error(req, "Too few arguments."); 445 return; 446 } 447 name = gctl_get_asciiparam(req, "arg0"); 448 if (name == NULL) { 449 gctl_error(req, "No 'arg%u' argument.", 0); 450 return; 451 } 452 sc = g_mirror_find_device(mp, name); 453 if (sc == NULL) { 454 gctl_error(req, "No such device: %s.", name); 455 return; 456 } 457 if (g_mirror_ndisks(sc, -1) < sc->sc_ndisks) { 458 gctl_error(req, "Not all disks connected."); 459 return; 460 } 461 462 for (i = 1; i < (u_int)*nargs; i++) { 463 snprintf(param, sizeof(param), "arg%u", i); 464 name = gctl_get_asciiparam(req, param); 465 if (name == NULL) { 466 gctl_error(req, "No 'arg%u' argument.", i); 467 return; 468 } 469 disk = g_mirror_find_disk(sc, name); 470 if (disk == NULL) { 471 gctl_error(req, "No such provider: %s.", name); 472 return; 473 } 474 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DESTROY, 475 G_MIRROR_EVENT_WAIT); 476 } 477 } 478 479 static void 480 g_mirror_ctl_deactivate(struct gctl_req *req, struct g_class *mp) 481 { 482 struct g_mirror_softc *sc; 483 struct g_mirror_disk *disk; 484 const char *name; 485 char param[16]; 486 int *nargs; 487 u_int i; 488 489 g_topology_assert(); 490 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 491 if (nargs == NULL) { 492 gctl_error(req, "No '%s' argument.", "nargs"); 493 return; 494 } 495 if (*nargs < 2) { 496 gctl_error(req, "Too few arguments."); 497 return; 498 } 499 name = gctl_get_asciiparam(req, "arg0"); 500 if (name == NULL) { 501 gctl_error(req, "No 'arg%u' argument.", 0); 502 return; 503 } 504 sc = g_mirror_find_device(mp, name); 505 if (sc == NULL) { 506 gctl_error(req, "No such device: %s.", name); 507 return; 508 } 509 510 for (i = 1; i < (u_int)*nargs; i++) { 511 snprintf(param, sizeof(param), "arg%u", i); 512 name = gctl_get_asciiparam(req, param); 513 if (name == NULL) { 514 gctl_error(req, "No 'arg%u' argument.", i); 515 return; 516 } 517 disk = g_mirror_find_disk(sc, name); 518 if (disk == NULL) { 519 gctl_error(req, "No such provider: %s.", name); 520 return; 521 } 522 /* 523 * Do rebuild by resetting syncid and disconnecting disk. 524 * It'll be retasted, connected to the mirror and 525 * synchronized. 526 */ 527 disk->d_flags |= G_MIRROR_DISK_FLAG_INACTIVE; 528 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC; 529 g_mirror_update_metadata(disk); 530 sc->sc_bump_syncid = G_MIRROR_BUMP_ON_FIRST_WRITE; 531 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED, 532 G_MIRROR_EVENT_WAIT); 533 } 534 } 535 536 static void 537 g_mirror_ctl_forget(struct gctl_req *req, struct g_class *mp) 538 { 539 struct g_mirror_softc *sc; 540 struct g_mirror_disk *disk; 541 const char *name; 542 char param[16]; 543 int *nargs; 544 u_int i; 545 546 g_topology_assert(); 547 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 548 if (nargs == NULL) { 549 gctl_error(req, "No '%s' argument.", "nargs"); 550 return; 551 } 552 if (*nargs < 1) { 553 gctl_error(req, "Missing device(s)."); 554 return; 555 } 556 557 for (i = 0; i < (u_int)*nargs; i++) { 558 snprintf(param, sizeof(param), "arg%u", i); 559 name = gctl_get_asciiparam(req, param); 560 if (name == NULL) { 561 gctl_error(req, "No 'arg%u' argument.", i); 562 return; 563 } 564 sc = g_mirror_find_device(mp, name); 565 if (sc == NULL) { 566 gctl_error(req, "No such device: %s.", name); 567 return; 568 } 569 if (g_mirror_ndisks(sc, -1) == sc->sc_ndisks) { 570 G_MIRROR_DEBUG(1, 571 "All disks connected in %s, skipping.", 572 sc->sc_name); 573 continue; 574 } 575 sc->sc_ndisks = g_mirror_ndisks(sc, -1); 576 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 577 g_mirror_update_metadata(disk); 578 } 579 } 580 } 581 582 static void 583 g_mirror_ctl_stop(struct gctl_req *req, struct g_class *mp) 584 { 585 struct g_mirror_softc *sc; 586 int *force, *nargs, error; 587 const char *name; 588 char param[16]; 589 u_int i; 590 591 g_topology_assert(); 592 593 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 594 if (nargs == NULL) { 595 gctl_error(req, "No '%s' argument.", "nargs"); 596 return; 597 } 598 if (*nargs < 1) { 599 gctl_error(req, "Missing device(s)."); 600 return; 601 } 602 force = gctl_get_paraml(req, "force", sizeof(*force)); 603 if (force == NULL) { 604 gctl_error(req, "No '%s' argument.", "force"); 605 return; 606 } 607 608 for (i = 0; i < (u_int)*nargs; i++) { 609 snprintf(param, sizeof(param), "arg%u", i); 610 name = gctl_get_asciiparam(req, param); 611 if (name == NULL) { 612 gctl_error(req, "No 'arg%u' argument.", i); 613 return; 614 } 615 sc = g_mirror_find_device(mp, name); 616 if (sc == NULL) { 617 gctl_error(req, "No such device: %s.", name); 618 return; 619 } 620 error = g_mirror_destroy(sc, *force); 621 if (error != 0) { 622 gctl_error(req, "Cannot destroy device %s (error=%d).", 623 sc->sc_geom->name, error); 624 return; 625 } 626 } 627 } 628 629 void 630 g_mirror_config(struct gctl_req *req, struct g_class *mp, const char *verb) 631 { 632 uint32_t *version; 633 634 g_topology_assert(); 635 636 version = gctl_get_paraml(req, "version", sizeof(*version)); 637 if (version == NULL) { 638 gctl_error(req, "No '%s' argument.", "version"); 639 return; 640 } 641 if (*version != G_MIRROR_VERSION) { 642 gctl_error(req, "Userland and kernel parts are out of sync."); 643 return; 644 } 645 646 if (strcmp(verb, "configure") == 0) 647 g_mirror_ctl_configure(req, mp); 648 else if (strcmp(verb, "rebuild") == 0) 649 g_mirror_ctl_rebuild(req, mp); 650 else if (strcmp(verb, "insert") == 0) 651 g_mirror_ctl_insert(req, mp); 652 else if (strcmp(verb, "remove") == 0) 653 g_mirror_ctl_remove(req, mp); 654 else if (strcmp(verb, "deactivate") == 0) 655 g_mirror_ctl_deactivate(req, mp); 656 else if (strcmp(verb, "forget") == 0) 657 g_mirror_ctl_forget(req, mp); 658 else if (strcmp(verb, "stop") == 0) 659 g_mirror_ctl_stop(req, mp); 660 else 661 gctl_error(req, "Unknown verb."); 662 } 663