1 /*- 2 * Copyright (c) 2004-2006 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/fail.h> 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 #include <sys/limits.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/bio.h> 39 #include <sys/sbuf.h> 40 #include <sys/sysctl.h> 41 #include <sys/malloc.h> 42 #include <sys/eventhandler.h> 43 #include <vm/uma.h> 44 #include <geom/geom.h> 45 #include <sys/proc.h> 46 #include <sys/kthread.h> 47 #include <sys/sched.h> 48 #include <geom/mirror/g_mirror.h> 49 50 FEATURE(geom_mirror, "GEOM mirroring support"); 51 52 static MALLOC_DEFINE(M_MIRROR, "mirror_data", "GEOM_MIRROR Data"); 53 54 SYSCTL_DECL(_kern_geom); 55 static SYSCTL_NODE(_kern_geom, OID_AUTO, mirror, CTLFLAG_RW, 0, 56 "GEOM_MIRROR stuff"); 57 u_int g_mirror_debug = 0; 58 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, debug, CTLFLAG_RWTUN, &g_mirror_debug, 0, 59 "Debug level"); 60 static u_int g_mirror_timeout = 4; 61 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, timeout, CTLFLAG_RWTUN, &g_mirror_timeout, 62 0, "Time to wait on all mirror components"); 63 static u_int g_mirror_idletime = 5; 64 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, idletime, CTLFLAG_RWTUN, 65 &g_mirror_idletime, 0, "Mark components as clean when idling"); 66 static u_int g_mirror_disconnect_on_failure = 1; 67 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, disconnect_on_failure, CTLFLAG_RWTUN, 68 &g_mirror_disconnect_on_failure, 0, "Disconnect component on I/O failure."); 69 static u_int g_mirror_syncreqs = 2; 70 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, sync_requests, CTLFLAG_RDTUN, 71 &g_mirror_syncreqs, 0, "Parallel synchronization I/O requests."); 72 73 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \ 74 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \ 75 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \ 76 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \ 77 } while (0) 78 79 static eventhandler_tag g_mirror_post_sync = NULL; 80 static int g_mirror_shutdown = 0; 81 82 static int g_mirror_destroy_geom(struct gctl_req *req, struct g_class *mp, 83 struct g_geom *gp); 84 static g_taste_t g_mirror_taste; 85 static g_resize_t g_mirror_resize; 86 static void g_mirror_init(struct g_class *mp); 87 static void g_mirror_fini(struct g_class *mp); 88 89 struct g_class g_mirror_class = { 90 .name = G_MIRROR_CLASS_NAME, 91 .version = G_VERSION, 92 .ctlreq = g_mirror_config, 93 .taste = g_mirror_taste, 94 .destroy_geom = g_mirror_destroy_geom, 95 .init = g_mirror_init, 96 .fini = g_mirror_fini, 97 .resize = g_mirror_resize 98 }; 99 100 101 static void g_mirror_destroy_provider(struct g_mirror_softc *sc); 102 static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state); 103 static void g_mirror_update_device(struct g_mirror_softc *sc, bool force); 104 static void g_mirror_dumpconf(struct sbuf *sb, const char *indent, 105 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp); 106 static void g_mirror_sync_stop(struct g_mirror_disk *disk, int type); 107 static void g_mirror_register_request(struct bio *bp); 108 static void g_mirror_sync_release(struct g_mirror_softc *sc); 109 110 111 static const char * 112 g_mirror_disk_state2str(int state) 113 { 114 115 switch (state) { 116 case G_MIRROR_DISK_STATE_NONE: 117 return ("NONE"); 118 case G_MIRROR_DISK_STATE_NEW: 119 return ("NEW"); 120 case G_MIRROR_DISK_STATE_ACTIVE: 121 return ("ACTIVE"); 122 case G_MIRROR_DISK_STATE_STALE: 123 return ("STALE"); 124 case G_MIRROR_DISK_STATE_SYNCHRONIZING: 125 return ("SYNCHRONIZING"); 126 case G_MIRROR_DISK_STATE_DISCONNECTED: 127 return ("DISCONNECTED"); 128 case G_MIRROR_DISK_STATE_DESTROY: 129 return ("DESTROY"); 130 default: 131 return ("INVALID"); 132 } 133 } 134 135 static const char * 136 g_mirror_device_state2str(int state) 137 { 138 139 switch (state) { 140 case G_MIRROR_DEVICE_STATE_STARTING: 141 return ("STARTING"); 142 case G_MIRROR_DEVICE_STATE_RUNNING: 143 return ("RUNNING"); 144 default: 145 return ("INVALID"); 146 } 147 } 148 149 static const char * 150 g_mirror_get_diskname(struct g_mirror_disk *disk) 151 { 152 153 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL) 154 return ("[unknown]"); 155 return (disk->d_name); 156 } 157 158 /* 159 * --- Events handling functions --- 160 * Events in geom_mirror are used to maintain disks and device status 161 * from one thread to simplify locking. 162 */ 163 static void 164 g_mirror_event_free(struct g_mirror_event *ep) 165 { 166 167 free(ep, M_MIRROR); 168 } 169 170 int 171 g_mirror_event_send(void *arg, int state, int flags) 172 { 173 struct g_mirror_softc *sc; 174 struct g_mirror_disk *disk; 175 struct g_mirror_event *ep; 176 int error; 177 178 ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK); 179 G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep); 180 if ((flags & G_MIRROR_EVENT_DEVICE) != 0) { 181 disk = NULL; 182 sc = arg; 183 } else { 184 disk = arg; 185 sc = disk->d_softc; 186 } 187 ep->e_disk = disk; 188 ep->e_state = state; 189 ep->e_flags = flags; 190 ep->e_error = 0; 191 mtx_lock(&sc->sc_events_mtx); 192 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next); 193 mtx_unlock(&sc->sc_events_mtx); 194 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc); 195 mtx_lock(&sc->sc_queue_mtx); 196 wakeup(sc); 197 mtx_unlock(&sc->sc_queue_mtx); 198 if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0) 199 return (0); 200 sx_assert(&sc->sc_lock, SX_XLOCKED); 201 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep); 202 sx_xunlock(&sc->sc_lock); 203 while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) { 204 mtx_lock(&sc->sc_events_mtx); 205 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event", 206 hz * 5); 207 } 208 error = ep->e_error; 209 g_mirror_event_free(ep); 210 sx_xlock(&sc->sc_lock); 211 return (error); 212 } 213 214 static struct g_mirror_event * 215 g_mirror_event_get(struct g_mirror_softc *sc) 216 { 217 struct g_mirror_event *ep; 218 219 mtx_lock(&sc->sc_events_mtx); 220 ep = TAILQ_FIRST(&sc->sc_events); 221 mtx_unlock(&sc->sc_events_mtx); 222 return (ep); 223 } 224 225 static void 226 g_mirror_event_remove(struct g_mirror_softc *sc, struct g_mirror_event *ep) 227 { 228 229 mtx_lock(&sc->sc_events_mtx); 230 TAILQ_REMOVE(&sc->sc_events, ep, e_next); 231 mtx_unlock(&sc->sc_events_mtx); 232 } 233 234 static void 235 g_mirror_event_cancel(struct g_mirror_disk *disk) 236 { 237 struct g_mirror_softc *sc; 238 struct g_mirror_event *ep, *tmpep; 239 240 sc = disk->d_softc; 241 sx_assert(&sc->sc_lock, SX_XLOCKED); 242 243 mtx_lock(&sc->sc_events_mtx); 244 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) { 245 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) 246 continue; 247 if (ep->e_disk != disk) 248 continue; 249 TAILQ_REMOVE(&sc->sc_events, ep, e_next); 250 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) 251 g_mirror_event_free(ep); 252 else { 253 ep->e_error = ECANCELED; 254 wakeup(ep); 255 } 256 } 257 mtx_unlock(&sc->sc_events_mtx); 258 } 259 260 /* 261 * Return the number of disks in given state. 262 * If state is equal to -1, count all connected disks. 263 */ 264 u_int 265 g_mirror_ndisks(struct g_mirror_softc *sc, int state) 266 { 267 struct g_mirror_disk *disk; 268 u_int n = 0; 269 270 sx_assert(&sc->sc_lock, SX_LOCKED); 271 272 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 273 if (state == -1 || disk->d_state == state) 274 n++; 275 } 276 return (n); 277 } 278 279 /* 280 * Find a disk in mirror by its disk ID. 281 */ 282 static struct g_mirror_disk * 283 g_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id) 284 { 285 struct g_mirror_disk *disk; 286 287 sx_assert(&sc->sc_lock, SX_XLOCKED); 288 289 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 290 if (disk->d_id == id) 291 return (disk); 292 } 293 return (NULL); 294 } 295 296 static u_int 297 g_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp) 298 { 299 struct bio *bp; 300 u_int nreqs = 0; 301 302 mtx_lock(&sc->sc_queue_mtx); 303 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) { 304 if (bp->bio_from == cp) 305 nreqs++; 306 } 307 mtx_unlock(&sc->sc_queue_mtx); 308 return (nreqs); 309 } 310 311 static int 312 g_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp) 313 { 314 315 if (cp->index > 0) { 316 G_MIRROR_DEBUG(2, 317 "I/O requests for %s exist, can't destroy it now.", 318 cp->provider->name); 319 return (1); 320 } 321 if (g_mirror_nrequests(sc, cp) > 0) { 322 G_MIRROR_DEBUG(2, 323 "I/O requests for %s in queue, can't destroy it now.", 324 cp->provider->name); 325 return (1); 326 } 327 return (0); 328 } 329 330 static void 331 g_mirror_destroy_consumer(void *arg, int flags __unused) 332 { 333 struct g_consumer *cp; 334 335 g_topology_assert(); 336 337 cp = arg; 338 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", cp->provider->name); 339 g_detach(cp); 340 g_destroy_consumer(cp); 341 } 342 343 static void 344 g_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp) 345 { 346 struct g_provider *pp; 347 int retaste_wait; 348 349 g_topology_assert(); 350 351 cp->private = NULL; 352 if (g_mirror_is_busy(sc, cp)) 353 return; 354 pp = cp->provider; 355 retaste_wait = 0; 356 if (cp->acw == 1) { 357 if ((pp->geom->flags & G_GEOM_WITHER) == 0) 358 retaste_wait = 1; 359 } 360 G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr, 361 -cp->acw, -cp->ace, 0); 362 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 363 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 364 if (retaste_wait) { 365 /* 366 * After retaste event was send (inside g_access()), we can send 367 * event to detach and destroy consumer. 368 * A class, which has consumer to the given provider connected 369 * will not receive retaste event for the provider. 370 * This is the way how I ignore retaste events when I close 371 * consumers opened for write: I detach and destroy consumer 372 * after retaste event is sent. 373 */ 374 g_post_event(g_mirror_destroy_consumer, cp, M_WAITOK, NULL); 375 return; 376 } 377 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", pp->name); 378 g_detach(cp); 379 g_destroy_consumer(cp); 380 } 381 382 static int 383 g_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp) 384 { 385 struct g_consumer *cp; 386 int error; 387 388 g_topology_assert_not(); 389 KASSERT(disk->d_consumer == NULL, 390 ("Disk already connected (device %s).", disk->d_softc->sc_name)); 391 392 g_topology_lock(); 393 cp = g_new_consumer(disk->d_softc->sc_geom); 394 cp->flags |= G_CF_DIRECT_RECEIVE; 395 error = g_attach(cp, pp); 396 if (error != 0) { 397 g_destroy_consumer(cp); 398 g_topology_unlock(); 399 return (error); 400 } 401 error = g_access(cp, 1, 1, 1); 402 if (error != 0) { 403 g_detach(cp); 404 g_destroy_consumer(cp); 405 g_topology_unlock(); 406 G_MIRROR_DEBUG(0, "Cannot open consumer %s (error=%d).", 407 pp->name, error); 408 return (error); 409 } 410 g_topology_unlock(); 411 disk->d_consumer = cp; 412 disk->d_consumer->private = disk; 413 disk->d_consumer->index = 0; 414 415 G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk)); 416 return (0); 417 } 418 419 static void 420 g_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp) 421 { 422 423 g_topology_assert(); 424 425 if (cp == NULL) 426 return; 427 if (cp->provider != NULL) 428 g_mirror_kill_consumer(sc, cp); 429 else 430 g_destroy_consumer(cp); 431 } 432 433 /* 434 * Initialize disk. This means allocate memory, create consumer, attach it 435 * to the provider and open access (r1w1e1) to it. 436 */ 437 static struct g_mirror_disk * 438 g_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp, 439 struct g_mirror_metadata *md, int *errorp) 440 { 441 struct g_mirror_disk *disk; 442 int i, error; 443 444 disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO); 445 if (disk == NULL) { 446 error = ENOMEM; 447 goto fail; 448 } 449 disk->d_softc = sc; 450 error = g_mirror_connect_disk(disk, pp); 451 if (error != 0) 452 goto fail; 453 disk->d_id = md->md_did; 454 disk->d_state = G_MIRROR_DISK_STATE_NONE; 455 disk->d_priority = md->md_priority; 456 disk->d_flags = md->md_dflags; 457 error = g_getattr("GEOM::candelete", disk->d_consumer, &i); 458 if (error == 0 && i != 0) 459 disk->d_flags |= G_MIRROR_DISK_FLAG_CANDELETE; 460 if (md->md_provider[0] != '\0') 461 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED; 462 disk->d_sync.ds_consumer = NULL; 463 disk->d_sync.ds_offset = md->md_sync_offset; 464 disk->d_sync.ds_offset_done = md->md_sync_offset; 465 disk->d_genid = md->md_genid; 466 disk->d_sync.ds_syncid = md->md_syncid; 467 if (errorp != NULL) 468 *errorp = 0; 469 return (disk); 470 fail: 471 if (errorp != NULL) 472 *errorp = error; 473 if (disk != NULL) 474 free(disk, M_MIRROR); 475 return (NULL); 476 } 477 478 static void 479 g_mirror_destroy_disk(struct g_mirror_disk *disk) 480 { 481 struct g_mirror_softc *sc; 482 483 g_topology_assert_not(); 484 sc = disk->d_softc; 485 sx_assert(&sc->sc_lock, SX_XLOCKED); 486 487 LIST_REMOVE(disk, d_next); 488 g_mirror_event_cancel(disk); 489 if (sc->sc_hint == disk) 490 sc->sc_hint = NULL; 491 switch (disk->d_state) { 492 case G_MIRROR_DISK_STATE_SYNCHRONIZING: 493 g_mirror_sync_stop(disk, 1); 494 /* FALLTHROUGH */ 495 case G_MIRROR_DISK_STATE_NEW: 496 case G_MIRROR_DISK_STATE_STALE: 497 case G_MIRROR_DISK_STATE_ACTIVE: 498 g_topology_lock(); 499 g_mirror_disconnect_consumer(sc, disk->d_consumer); 500 g_topology_unlock(); 501 free(disk, M_MIRROR); 502 break; 503 default: 504 KASSERT(0 == 1, ("Wrong disk state (%s, %s).", 505 g_mirror_get_diskname(disk), 506 g_mirror_disk_state2str(disk->d_state))); 507 } 508 } 509 510 static void 511 g_mirror_destroy_device(struct g_mirror_softc *sc) 512 { 513 struct g_mirror_disk *disk; 514 struct g_mirror_event *ep; 515 struct g_geom *gp; 516 struct g_consumer *cp, *tmpcp; 517 518 g_topology_assert_not(); 519 sx_assert(&sc->sc_lock, SX_XLOCKED); 520 521 gp = sc->sc_geom; 522 if (sc->sc_provider != NULL) 523 g_mirror_destroy_provider(sc); 524 for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL; 525 disk = LIST_FIRST(&sc->sc_disks)) { 526 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY; 527 g_mirror_update_metadata(disk); 528 g_mirror_destroy_disk(disk); 529 } 530 while ((ep = g_mirror_event_get(sc)) != NULL) { 531 g_mirror_event_remove(sc, ep); 532 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) 533 g_mirror_event_free(ep); 534 else { 535 ep->e_error = ECANCELED; 536 ep->e_flags |= G_MIRROR_EVENT_DONE; 537 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep); 538 mtx_lock(&sc->sc_events_mtx); 539 wakeup(ep); 540 mtx_unlock(&sc->sc_events_mtx); 541 } 542 } 543 callout_drain(&sc->sc_callout); 544 545 g_topology_lock(); 546 LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) { 547 g_mirror_disconnect_consumer(sc, cp); 548 } 549 g_wither_geom(sc->sc_sync.ds_geom, ENXIO); 550 G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name); 551 g_wither_geom(gp, ENXIO); 552 g_topology_unlock(); 553 mtx_destroy(&sc->sc_queue_mtx); 554 mtx_destroy(&sc->sc_events_mtx); 555 mtx_destroy(&sc->sc_done_mtx); 556 sx_xunlock(&sc->sc_lock); 557 sx_destroy(&sc->sc_lock); 558 } 559 560 static void 561 g_mirror_orphan(struct g_consumer *cp) 562 { 563 struct g_mirror_disk *disk; 564 565 g_topology_assert(); 566 567 disk = cp->private; 568 if (disk == NULL) 569 return; 570 disk->d_softc->sc_bump_id |= G_MIRROR_BUMP_SYNCID; 571 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED, 572 G_MIRROR_EVENT_DONTWAIT); 573 } 574 575 /* 576 * Function should return the next active disk on the list. 577 * It is possible that it will be the same disk as given. 578 * If there are no active disks on list, NULL is returned. 579 */ 580 static __inline struct g_mirror_disk * 581 g_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk) 582 { 583 struct g_mirror_disk *dp; 584 585 for (dp = LIST_NEXT(disk, d_next); dp != disk; 586 dp = LIST_NEXT(dp, d_next)) { 587 if (dp == NULL) 588 dp = LIST_FIRST(&sc->sc_disks); 589 if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE) 590 break; 591 } 592 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE) 593 return (NULL); 594 return (dp); 595 } 596 597 static struct g_mirror_disk * 598 g_mirror_get_disk(struct g_mirror_softc *sc) 599 { 600 struct g_mirror_disk *disk; 601 602 if (sc->sc_hint == NULL) { 603 sc->sc_hint = LIST_FIRST(&sc->sc_disks); 604 if (sc->sc_hint == NULL) 605 return (NULL); 606 } 607 disk = sc->sc_hint; 608 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) { 609 disk = g_mirror_find_next(sc, disk); 610 if (disk == NULL) 611 return (NULL); 612 } 613 sc->sc_hint = g_mirror_find_next(sc, disk); 614 return (disk); 615 } 616 617 static int 618 g_mirror_write_metadata(struct g_mirror_disk *disk, 619 struct g_mirror_metadata *md) 620 { 621 struct g_mirror_softc *sc; 622 struct g_consumer *cp; 623 off_t offset, length; 624 u_char *sector; 625 int error = 0; 626 627 g_topology_assert_not(); 628 sc = disk->d_softc; 629 sx_assert(&sc->sc_lock, SX_LOCKED); 630 631 cp = disk->d_consumer; 632 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name)); 633 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name)); 634 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1, 635 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr, 636 cp->acw, cp->ace)); 637 length = cp->provider->sectorsize; 638 offset = cp->provider->mediasize - length; 639 sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO); 640 if (md != NULL && 641 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0) { 642 /* 643 * Handle the case, when the size of parent provider reduced. 644 */ 645 if (offset < md->md_mediasize) 646 error = ENOSPC; 647 else 648 mirror_metadata_encode(md, sector); 649 } 650 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_metadata_write, error); 651 if (error == 0) 652 error = g_write_data(cp, offset, sector, length); 653 free(sector, M_MIRROR); 654 if (error != 0) { 655 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) { 656 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN; 657 G_MIRROR_DEBUG(0, "Cannot write metadata on %s " 658 "(device=%s, error=%d).", 659 g_mirror_get_diskname(disk), sc->sc_name, error); 660 } else { 661 G_MIRROR_DEBUG(1, "Cannot write metadata on %s " 662 "(device=%s, error=%d).", 663 g_mirror_get_diskname(disk), sc->sc_name, error); 664 } 665 if (g_mirror_disconnect_on_failure && 666 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) { 667 sc->sc_bump_id |= G_MIRROR_BUMP_GENID; 668 g_mirror_event_send(disk, 669 G_MIRROR_DISK_STATE_DISCONNECTED, 670 G_MIRROR_EVENT_DONTWAIT); 671 } 672 } 673 return (error); 674 } 675 676 static int 677 g_mirror_clear_metadata(struct g_mirror_disk *disk) 678 { 679 int error; 680 681 g_topology_assert_not(); 682 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED); 683 684 error = g_mirror_write_metadata(disk, NULL); 685 if (error == 0) { 686 G_MIRROR_DEBUG(2, "Metadata on %s cleared.", 687 g_mirror_get_diskname(disk)); 688 } else { 689 G_MIRROR_DEBUG(0, 690 "Cannot clear metadata on disk %s (error=%d).", 691 g_mirror_get_diskname(disk), error); 692 } 693 return (error); 694 } 695 696 void 697 g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk, 698 struct g_mirror_metadata *md) 699 { 700 701 strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic)); 702 md->md_version = G_MIRROR_VERSION; 703 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name)); 704 md->md_mid = sc->sc_id; 705 md->md_all = sc->sc_ndisks; 706 md->md_slice = sc->sc_slice; 707 md->md_balance = sc->sc_balance; 708 md->md_genid = sc->sc_genid; 709 md->md_mediasize = sc->sc_mediasize; 710 md->md_sectorsize = sc->sc_sectorsize; 711 md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK); 712 bzero(md->md_provider, sizeof(md->md_provider)); 713 if (disk == NULL) { 714 md->md_did = arc4random(); 715 md->md_priority = 0; 716 md->md_syncid = 0; 717 md->md_dflags = 0; 718 md->md_sync_offset = 0; 719 md->md_provsize = 0; 720 } else { 721 md->md_did = disk->d_id; 722 md->md_priority = disk->d_priority; 723 md->md_syncid = disk->d_sync.ds_syncid; 724 md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK); 725 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) 726 md->md_sync_offset = disk->d_sync.ds_offset_done; 727 else 728 md->md_sync_offset = 0; 729 if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) { 730 strlcpy(md->md_provider, 731 disk->d_consumer->provider->name, 732 sizeof(md->md_provider)); 733 } 734 md->md_provsize = disk->d_consumer->provider->mediasize; 735 } 736 } 737 738 void 739 g_mirror_update_metadata(struct g_mirror_disk *disk) 740 { 741 struct g_mirror_softc *sc; 742 struct g_mirror_metadata md; 743 int error; 744 745 g_topology_assert_not(); 746 sc = disk->d_softc; 747 sx_assert(&sc->sc_lock, SX_LOCKED); 748 749 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0) 750 g_mirror_fill_metadata(sc, disk, &md); 751 error = g_mirror_write_metadata(disk, &md); 752 if (error == 0) { 753 G_MIRROR_DEBUG(2, "Metadata on %s updated.", 754 g_mirror_get_diskname(disk)); 755 } else { 756 G_MIRROR_DEBUG(0, 757 "Cannot update metadata on disk %s (error=%d).", 758 g_mirror_get_diskname(disk), error); 759 } 760 } 761 762 static void 763 g_mirror_bump_syncid(struct g_mirror_softc *sc) 764 { 765 struct g_mirror_disk *disk; 766 767 g_topology_assert_not(); 768 sx_assert(&sc->sc_lock, SX_XLOCKED); 769 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0, 770 ("%s called with no active disks (device=%s).", __func__, 771 sc->sc_name)); 772 773 sc->sc_syncid++; 774 G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name, 775 sc->sc_syncid); 776 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 777 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE || 778 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) { 779 disk->d_sync.ds_syncid = sc->sc_syncid; 780 g_mirror_update_metadata(disk); 781 } 782 } 783 } 784 785 static void 786 g_mirror_bump_genid(struct g_mirror_softc *sc) 787 { 788 struct g_mirror_disk *disk; 789 790 g_topology_assert_not(); 791 sx_assert(&sc->sc_lock, SX_XLOCKED); 792 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0, 793 ("%s called with no active disks (device=%s).", __func__, 794 sc->sc_name)); 795 796 sc->sc_genid++; 797 G_MIRROR_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name, 798 sc->sc_genid); 799 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 800 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE || 801 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) { 802 disk->d_genid = sc->sc_genid; 803 g_mirror_update_metadata(disk); 804 } 805 } 806 } 807 808 static int 809 g_mirror_idle(struct g_mirror_softc *sc, int acw) 810 { 811 struct g_mirror_disk *disk; 812 int timeout; 813 814 g_topology_assert_not(); 815 sx_assert(&sc->sc_lock, SX_XLOCKED); 816 817 if (sc->sc_provider == NULL) 818 return (0); 819 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0) 820 return (0); 821 if (sc->sc_idle) 822 return (0); 823 if (sc->sc_writes > 0) 824 return (0); 825 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) { 826 timeout = g_mirror_idletime - (time_uptime - sc->sc_last_write); 827 if (!g_mirror_shutdown && timeout > 0) 828 return (timeout); 829 } 830 sc->sc_idle = 1; 831 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 832 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) 833 continue; 834 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as clean.", 835 g_mirror_get_diskname(disk), sc->sc_name); 836 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY; 837 g_mirror_update_metadata(disk); 838 } 839 return (0); 840 } 841 842 static void 843 g_mirror_unidle(struct g_mirror_softc *sc) 844 { 845 struct g_mirror_disk *disk; 846 847 g_topology_assert_not(); 848 sx_assert(&sc->sc_lock, SX_XLOCKED); 849 850 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0) 851 return; 852 sc->sc_idle = 0; 853 sc->sc_last_write = time_uptime; 854 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 855 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) 856 continue; 857 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as dirty.", 858 g_mirror_get_diskname(disk), sc->sc_name); 859 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY; 860 g_mirror_update_metadata(disk); 861 } 862 } 863 864 static void 865 g_mirror_flush_done(struct bio *bp) 866 { 867 struct g_mirror_softc *sc; 868 struct bio *pbp; 869 870 pbp = bp->bio_parent; 871 sc = pbp->bio_to->geom->softc; 872 mtx_lock(&sc->sc_done_mtx); 873 if (pbp->bio_error == 0) 874 pbp->bio_error = bp->bio_error; 875 pbp->bio_completed += bp->bio_completed; 876 pbp->bio_inbed++; 877 if (pbp->bio_children == pbp->bio_inbed) { 878 mtx_unlock(&sc->sc_done_mtx); 879 g_io_deliver(pbp, pbp->bio_error); 880 } else 881 mtx_unlock(&sc->sc_done_mtx); 882 g_destroy_bio(bp); 883 } 884 885 static void 886 g_mirror_done(struct bio *bp) 887 { 888 struct g_mirror_softc *sc; 889 890 sc = bp->bio_from->geom->softc; 891 bp->bio_cflags = G_MIRROR_BIO_FLAG_REGULAR; 892 mtx_lock(&sc->sc_queue_mtx); 893 bioq_insert_tail(&sc->sc_queue, bp); 894 mtx_unlock(&sc->sc_queue_mtx); 895 wakeup(sc); 896 } 897 898 static void 899 g_mirror_regular_request(struct bio *bp) 900 { 901 struct g_mirror_softc *sc; 902 struct g_mirror_disk *disk; 903 struct bio *pbp; 904 905 g_topology_assert_not(); 906 907 pbp = bp->bio_parent; 908 sc = pbp->bio_to->geom->softc; 909 bp->bio_from->index--; 910 if (bp->bio_cmd == BIO_WRITE) 911 sc->sc_writes--; 912 disk = bp->bio_from->private; 913 if (disk == NULL) { 914 g_topology_lock(); 915 g_mirror_kill_consumer(sc, bp->bio_from); 916 g_topology_unlock(); 917 } 918 919 if (bp->bio_cmd == BIO_READ) 920 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_regular_request_read, 921 bp->bio_error); 922 else if (bp->bio_cmd == BIO_WRITE) 923 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_regular_request_write, 924 bp->bio_error); 925 926 pbp->bio_inbed++; 927 KASSERT(pbp->bio_inbed <= pbp->bio_children, 928 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed, 929 pbp->bio_children)); 930 if (bp->bio_error == 0 && pbp->bio_error == 0) { 931 G_MIRROR_LOGREQ(3, bp, "Request delivered."); 932 g_destroy_bio(bp); 933 if (pbp->bio_children == pbp->bio_inbed) { 934 G_MIRROR_LOGREQ(3, pbp, "Request delivered."); 935 pbp->bio_completed = pbp->bio_length; 936 if (pbp->bio_cmd == BIO_WRITE || 937 pbp->bio_cmd == BIO_DELETE) { 938 bioq_remove(&sc->sc_inflight, pbp); 939 /* Release delayed sync requests if possible. */ 940 g_mirror_sync_release(sc); 941 } 942 g_io_deliver(pbp, pbp->bio_error); 943 } 944 return; 945 } else if (bp->bio_error != 0) { 946 if (pbp->bio_error == 0) 947 pbp->bio_error = bp->bio_error; 948 if (disk != NULL) { 949 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) { 950 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN; 951 G_MIRROR_LOGREQ(0, bp, 952 "Request failed (error=%d).", 953 bp->bio_error); 954 } else { 955 G_MIRROR_LOGREQ(1, bp, 956 "Request failed (error=%d).", 957 bp->bio_error); 958 } 959 if (g_mirror_disconnect_on_failure && 960 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) 961 { 962 sc->sc_bump_id |= G_MIRROR_BUMP_GENID; 963 g_mirror_event_send(disk, 964 G_MIRROR_DISK_STATE_DISCONNECTED, 965 G_MIRROR_EVENT_DONTWAIT); 966 } 967 } 968 switch (pbp->bio_cmd) { 969 case BIO_DELETE: 970 case BIO_WRITE: 971 pbp->bio_inbed--; 972 pbp->bio_children--; 973 break; 974 } 975 } 976 g_destroy_bio(bp); 977 978 switch (pbp->bio_cmd) { 979 case BIO_READ: 980 if (pbp->bio_inbed < pbp->bio_children) 981 break; 982 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1) 983 g_io_deliver(pbp, pbp->bio_error); 984 else { 985 pbp->bio_error = 0; 986 mtx_lock(&sc->sc_queue_mtx); 987 bioq_insert_tail(&sc->sc_queue, pbp); 988 mtx_unlock(&sc->sc_queue_mtx); 989 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc); 990 wakeup(sc); 991 } 992 break; 993 case BIO_DELETE: 994 case BIO_WRITE: 995 if (pbp->bio_children == 0) { 996 /* 997 * All requests failed. 998 */ 999 } else if (pbp->bio_inbed < pbp->bio_children) { 1000 /* Do nothing. */ 1001 break; 1002 } else if (pbp->bio_children == pbp->bio_inbed) { 1003 /* Some requests succeeded. */ 1004 pbp->bio_error = 0; 1005 pbp->bio_completed = pbp->bio_length; 1006 } 1007 bioq_remove(&sc->sc_inflight, pbp); 1008 /* Release delayed sync requests if possible. */ 1009 g_mirror_sync_release(sc); 1010 g_io_deliver(pbp, pbp->bio_error); 1011 break; 1012 default: 1013 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd)); 1014 break; 1015 } 1016 } 1017 1018 static void 1019 g_mirror_sync_done(struct bio *bp) 1020 { 1021 struct g_mirror_softc *sc; 1022 1023 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered."); 1024 sc = bp->bio_from->geom->softc; 1025 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC; 1026 mtx_lock(&sc->sc_queue_mtx); 1027 bioq_insert_tail(&sc->sc_queue, bp); 1028 mtx_unlock(&sc->sc_queue_mtx); 1029 wakeup(sc); 1030 } 1031 1032 static void 1033 g_mirror_candelete(struct bio *bp) 1034 { 1035 struct g_mirror_softc *sc; 1036 struct g_mirror_disk *disk; 1037 int *val; 1038 1039 sc = bp->bio_to->geom->softc; 1040 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 1041 if (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE) 1042 break; 1043 } 1044 val = (int *)bp->bio_data; 1045 *val = (disk != NULL); 1046 g_io_deliver(bp, 0); 1047 } 1048 1049 static void 1050 g_mirror_kernel_dump(struct bio *bp) 1051 { 1052 struct g_mirror_softc *sc; 1053 struct g_mirror_disk *disk; 1054 struct bio *cbp; 1055 struct g_kerneldump *gkd; 1056 1057 /* 1058 * We configure dumping to the first component, because this component 1059 * will be used for reading with 'prefer' balance algorithm. 1060 * If the component with the highest priority is currently disconnected 1061 * we will not be able to read the dump after the reboot if it will be 1062 * connected and synchronized later. Can we do something better? 1063 */ 1064 sc = bp->bio_to->geom->softc; 1065 disk = LIST_FIRST(&sc->sc_disks); 1066 1067 gkd = (struct g_kerneldump *)bp->bio_data; 1068 if (gkd->length > bp->bio_to->mediasize) 1069 gkd->length = bp->bio_to->mediasize; 1070 cbp = g_clone_bio(bp); 1071 if (cbp == NULL) { 1072 g_io_deliver(bp, ENOMEM); 1073 return; 1074 } 1075 cbp->bio_done = g_std_done; 1076 g_io_request(cbp, disk->d_consumer); 1077 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.", 1078 g_mirror_get_diskname(disk)); 1079 } 1080 1081 static void 1082 g_mirror_flush(struct g_mirror_softc *sc, struct bio *bp) 1083 { 1084 struct bio_queue_head queue; 1085 struct g_mirror_disk *disk; 1086 struct g_consumer *cp; 1087 struct bio *cbp; 1088 1089 bioq_init(&queue); 1090 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 1091 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) 1092 continue; 1093 cbp = g_clone_bio(bp); 1094 if (cbp == NULL) { 1095 while ((cbp = bioq_takefirst(&queue)) != NULL) 1096 g_destroy_bio(cbp); 1097 if (bp->bio_error == 0) 1098 bp->bio_error = ENOMEM; 1099 g_io_deliver(bp, bp->bio_error); 1100 return; 1101 } 1102 bioq_insert_tail(&queue, cbp); 1103 cbp->bio_done = g_mirror_flush_done; 1104 cbp->bio_caller1 = disk; 1105 cbp->bio_to = disk->d_consumer->provider; 1106 } 1107 while ((cbp = bioq_takefirst(&queue)) != NULL) { 1108 G_MIRROR_LOGREQ(3, cbp, "Sending request."); 1109 disk = cbp->bio_caller1; 1110 cbp->bio_caller1 = NULL; 1111 cp = disk->d_consumer; 1112 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1, 1113 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, 1114 cp->acr, cp->acw, cp->ace)); 1115 g_io_request(cbp, disk->d_consumer); 1116 } 1117 } 1118 1119 static void 1120 g_mirror_start(struct bio *bp) 1121 { 1122 struct g_mirror_softc *sc; 1123 1124 sc = bp->bio_to->geom->softc; 1125 /* 1126 * If sc == NULL or there are no valid disks, provider's error 1127 * should be set and g_mirror_start() should not be called at all. 1128 */ 1129 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING, 1130 ("Provider's error should be set (error=%d)(mirror=%s).", 1131 bp->bio_to->error, bp->bio_to->name)); 1132 G_MIRROR_LOGREQ(3, bp, "Request received."); 1133 1134 switch (bp->bio_cmd) { 1135 case BIO_READ: 1136 case BIO_WRITE: 1137 case BIO_DELETE: 1138 break; 1139 case BIO_FLUSH: 1140 g_mirror_flush(sc, bp); 1141 return; 1142 case BIO_GETATTR: 1143 if (!strcmp(bp->bio_attribute, "GEOM::candelete")) { 1144 g_mirror_candelete(bp); 1145 return; 1146 } else if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) { 1147 g_mirror_kernel_dump(bp); 1148 return; 1149 } 1150 /* FALLTHROUGH */ 1151 default: 1152 g_io_deliver(bp, EOPNOTSUPP); 1153 return; 1154 } 1155 mtx_lock(&sc->sc_queue_mtx); 1156 bioq_insert_tail(&sc->sc_queue, bp); 1157 mtx_unlock(&sc->sc_queue_mtx); 1158 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc); 1159 wakeup(sc); 1160 } 1161 1162 /* 1163 * Return TRUE if the given request is colliding with a in-progress 1164 * synchronization request. 1165 */ 1166 static int 1167 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp) 1168 { 1169 struct g_mirror_disk *disk; 1170 struct bio *sbp; 1171 off_t rstart, rend, sstart, send; 1172 u_int i; 1173 1174 if (sc->sc_sync.ds_ndisks == 0) 1175 return (0); 1176 rstart = bp->bio_offset; 1177 rend = bp->bio_offset + bp->bio_length; 1178 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 1179 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING) 1180 continue; 1181 for (i = 0; i < g_mirror_syncreqs; i++) { 1182 sbp = disk->d_sync.ds_bios[i]; 1183 if (sbp == NULL) 1184 continue; 1185 sstart = sbp->bio_offset; 1186 send = sbp->bio_offset + sbp->bio_length; 1187 if (rend > sstart && rstart < send) 1188 return (1); 1189 } 1190 } 1191 return (0); 1192 } 1193 1194 /* 1195 * Return TRUE if the given sync request is colliding with a in-progress regular 1196 * request. 1197 */ 1198 static int 1199 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp) 1200 { 1201 off_t rstart, rend, sstart, send; 1202 struct bio *bp; 1203 1204 if (sc->sc_sync.ds_ndisks == 0) 1205 return (0); 1206 sstart = sbp->bio_offset; 1207 send = sbp->bio_offset + sbp->bio_length; 1208 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) { 1209 rstart = bp->bio_offset; 1210 rend = bp->bio_offset + bp->bio_length; 1211 if (rend > sstart && rstart < send) 1212 return (1); 1213 } 1214 return (0); 1215 } 1216 1217 /* 1218 * Puts request onto delayed queue. 1219 */ 1220 static void 1221 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp) 1222 { 1223 1224 G_MIRROR_LOGREQ(2, bp, "Delaying request."); 1225 bioq_insert_head(&sc->sc_regular_delayed, bp); 1226 } 1227 1228 /* 1229 * Puts synchronization request onto delayed queue. 1230 */ 1231 static void 1232 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp) 1233 { 1234 1235 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request."); 1236 bioq_insert_tail(&sc->sc_sync_delayed, bp); 1237 } 1238 1239 /* 1240 * Releases delayed regular requests which don't collide anymore with sync 1241 * requests. 1242 */ 1243 static void 1244 g_mirror_regular_release(struct g_mirror_softc *sc) 1245 { 1246 struct bio *bp, *bp2; 1247 1248 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) { 1249 if (g_mirror_sync_collision(sc, bp)) 1250 continue; 1251 bioq_remove(&sc->sc_regular_delayed, bp); 1252 G_MIRROR_LOGREQ(2, bp, "Releasing delayed request (%p).", bp); 1253 mtx_lock(&sc->sc_queue_mtx); 1254 bioq_insert_head(&sc->sc_queue, bp); 1255 #if 0 1256 /* 1257 * wakeup() is not needed, because this function is called from 1258 * the worker thread. 1259 */ 1260 wakeup(&sc->sc_queue); 1261 #endif 1262 mtx_unlock(&sc->sc_queue_mtx); 1263 } 1264 } 1265 1266 /* 1267 * Releases delayed sync requests which don't collide anymore with regular 1268 * requests. 1269 */ 1270 static void 1271 g_mirror_sync_release(struct g_mirror_softc *sc) 1272 { 1273 struct bio *bp, *bp2; 1274 1275 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) { 1276 if (g_mirror_regular_collision(sc, bp)) 1277 continue; 1278 bioq_remove(&sc->sc_sync_delayed, bp); 1279 G_MIRROR_LOGREQ(2, bp, 1280 "Releasing delayed synchronization request."); 1281 g_io_request(bp, bp->bio_from); 1282 } 1283 } 1284 1285 /* 1286 * Handle synchronization requests. 1287 * Every synchronization request is two-steps process: first, READ request is 1288 * send to active provider and then WRITE request (with read data) to the provider 1289 * being synchronized. When WRITE is finished, new synchronization request is 1290 * send. 1291 */ 1292 static void 1293 g_mirror_sync_request(struct bio *bp) 1294 { 1295 struct g_mirror_softc *sc; 1296 struct g_mirror_disk *disk; 1297 1298 bp->bio_from->index--; 1299 sc = bp->bio_from->geom->softc; 1300 disk = bp->bio_from->private; 1301 if (disk == NULL) { 1302 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */ 1303 g_topology_lock(); 1304 g_mirror_kill_consumer(sc, bp->bio_from); 1305 g_topology_unlock(); 1306 free(bp->bio_data, M_MIRROR); 1307 g_destroy_bio(bp); 1308 sx_xlock(&sc->sc_lock); 1309 return; 1310 } 1311 1312 /* 1313 * Synchronization request. 1314 */ 1315 switch (bp->bio_cmd) { 1316 case BIO_READ: 1317 { 1318 struct g_consumer *cp; 1319 1320 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_sync_request_read, 1321 bp->bio_error); 1322 1323 if (bp->bio_error != 0) { 1324 G_MIRROR_LOGREQ(0, bp, 1325 "Synchronization request failed (error=%d).", 1326 bp->bio_error); 1327 g_destroy_bio(bp); 1328 return; 1329 } 1330 G_MIRROR_LOGREQ(3, bp, 1331 "Synchronization request half-finished."); 1332 bp->bio_cmd = BIO_WRITE; 1333 bp->bio_cflags = 0; 1334 cp = disk->d_consumer; 1335 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1, 1336 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, 1337 cp->acr, cp->acw, cp->ace)); 1338 cp->index++; 1339 g_io_request(bp, cp); 1340 return; 1341 } 1342 case BIO_WRITE: 1343 { 1344 struct g_mirror_disk_sync *sync; 1345 off_t offset; 1346 void *data; 1347 int i; 1348 1349 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_sync_request_write, 1350 bp->bio_error); 1351 1352 if (bp->bio_error != 0) { 1353 G_MIRROR_LOGREQ(0, bp, 1354 "Synchronization request failed (error=%d).", 1355 bp->bio_error); 1356 g_destroy_bio(bp); 1357 sc->sc_bump_id |= G_MIRROR_BUMP_GENID; 1358 g_mirror_event_send(disk, 1359 G_MIRROR_DISK_STATE_DISCONNECTED, 1360 G_MIRROR_EVENT_DONTWAIT); 1361 return; 1362 } 1363 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished."); 1364 sync = &disk->d_sync; 1365 if (sync->ds_offset >= sc->sc_mediasize || 1366 sync->ds_consumer == NULL || 1367 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) { 1368 /* Don't send more synchronization requests. */ 1369 sync->ds_inflight--; 1370 if (sync->ds_bios != NULL) { 1371 i = (int)(uintptr_t)bp->bio_caller1; 1372 sync->ds_bios[i] = NULL; 1373 } 1374 free(bp->bio_data, M_MIRROR); 1375 g_destroy_bio(bp); 1376 if (sync->ds_inflight > 0) 1377 return; 1378 if (sync->ds_consumer == NULL || 1379 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) { 1380 return; 1381 } 1382 /* Disk up-to-date, activate it. */ 1383 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE, 1384 G_MIRROR_EVENT_DONTWAIT); 1385 return; 1386 } 1387 1388 /* Send next synchronization request. */ 1389 data = bp->bio_data; 1390 g_reset_bio(bp); 1391 bp->bio_cmd = BIO_READ; 1392 bp->bio_offset = sync->ds_offset; 1393 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset); 1394 sync->ds_offset += bp->bio_length; 1395 bp->bio_done = g_mirror_sync_done; 1396 bp->bio_data = data; 1397 bp->bio_from = sync->ds_consumer; 1398 bp->bio_to = sc->sc_provider; 1399 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request."); 1400 sync->ds_consumer->index++; 1401 /* 1402 * Delay the request if it is colliding with a regular request. 1403 */ 1404 if (g_mirror_regular_collision(sc, bp)) 1405 g_mirror_sync_delay(sc, bp); 1406 else 1407 g_io_request(bp, sync->ds_consumer); 1408 1409 /* Release delayed requests if possible. */ 1410 g_mirror_regular_release(sc); 1411 1412 /* Find the smallest offset */ 1413 offset = sc->sc_mediasize; 1414 for (i = 0; i < g_mirror_syncreqs; i++) { 1415 bp = sync->ds_bios[i]; 1416 if (bp->bio_offset < offset) 1417 offset = bp->bio_offset; 1418 } 1419 if (sync->ds_offset_done + (MAXPHYS * 100) < offset) { 1420 /* Update offset_done on every 100 blocks. */ 1421 sync->ds_offset_done = offset; 1422 g_mirror_update_metadata(disk); 1423 } 1424 return; 1425 } 1426 default: 1427 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)", 1428 bp->bio_cmd, sc->sc_name)); 1429 break; 1430 } 1431 } 1432 1433 static void 1434 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp) 1435 { 1436 struct g_mirror_disk *disk; 1437 struct g_consumer *cp; 1438 struct bio *cbp; 1439 1440 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 1441 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) 1442 break; 1443 } 1444 if (disk == NULL) { 1445 if (bp->bio_error == 0) 1446 bp->bio_error = ENXIO; 1447 g_io_deliver(bp, bp->bio_error); 1448 return; 1449 } 1450 cbp = g_clone_bio(bp); 1451 if (cbp == NULL) { 1452 if (bp->bio_error == 0) 1453 bp->bio_error = ENOMEM; 1454 g_io_deliver(bp, bp->bio_error); 1455 return; 1456 } 1457 /* 1458 * Fill in the component buf structure. 1459 */ 1460 cp = disk->d_consumer; 1461 cbp->bio_done = g_mirror_done; 1462 cbp->bio_to = cp->provider; 1463 G_MIRROR_LOGREQ(3, cbp, "Sending request."); 1464 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1, 1465 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr, 1466 cp->acw, cp->ace)); 1467 cp->index++; 1468 g_io_request(cbp, cp); 1469 } 1470 1471 static void 1472 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp) 1473 { 1474 struct g_mirror_disk *disk; 1475 struct g_consumer *cp; 1476 struct bio *cbp; 1477 1478 disk = g_mirror_get_disk(sc); 1479 if (disk == NULL) { 1480 if (bp->bio_error == 0) 1481 bp->bio_error = ENXIO; 1482 g_io_deliver(bp, bp->bio_error); 1483 return; 1484 } 1485 cbp = g_clone_bio(bp); 1486 if (cbp == NULL) { 1487 if (bp->bio_error == 0) 1488 bp->bio_error = ENOMEM; 1489 g_io_deliver(bp, bp->bio_error); 1490 return; 1491 } 1492 /* 1493 * Fill in the component buf structure. 1494 */ 1495 cp = disk->d_consumer; 1496 cbp->bio_done = g_mirror_done; 1497 cbp->bio_to = cp->provider; 1498 G_MIRROR_LOGREQ(3, cbp, "Sending request."); 1499 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1, 1500 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr, 1501 cp->acw, cp->ace)); 1502 cp->index++; 1503 g_io_request(cbp, cp); 1504 } 1505 1506 #define TRACK_SIZE (1 * 1024 * 1024) 1507 #define LOAD_SCALE 256 1508 #define ABS(x) (((x) >= 0) ? (x) : (-(x))) 1509 1510 static void 1511 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp) 1512 { 1513 struct g_mirror_disk *disk, *dp; 1514 struct g_consumer *cp; 1515 struct bio *cbp; 1516 int prio, best; 1517 1518 /* Find a disk with the smallest load. */ 1519 disk = NULL; 1520 best = INT_MAX; 1521 LIST_FOREACH(dp, &sc->sc_disks, d_next) { 1522 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE) 1523 continue; 1524 prio = dp->load; 1525 /* If disk head is precisely in position - highly prefer it. */ 1526 if (dp->d_last_offset == bp->bio_offset) 1527 prio -= 2 * LOAD_SCALE; 1528 else 1529 /* If disk head is close to position - prefer it. */ 1530 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE) 1531 prio -= 1 * LOAD_SCALE; 1532 if (prio <= best) { 1533 disk = dp; 1534 best = prio; 1535 } 1536 } 1537 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name)); 1538 cbp = g_clone_bio(bp); 1539 if (cbp == NULL) { 1540 if (bp->bio_error == 0) 1541 bp->bio_error = ENOMEM; 1542 g_io_deliver(bp, bp->bio_error); 1543 return; 1544 } 1545 /* 1546 * Fill in the component buf structure. 1547 */ 1548 cp = disk->d_consumer; 1549 cbp->bio_done = g_mirror_done; 1550 cbp->bio_to = cp->provider; 1551 G_MIRROR_LOGREQ(3, cbp, "Sending request."); 1552 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1, 1553 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr, 1554 cp->acw, cp->ace)); 1555 cp->index++; 1556 /* Remember last head position */ 1557 disk->d_last_offset = bp->bio_offset + bp->bio_length; 1558 /* Update loads. */ 1559 LIST_FOREACH(dp, &sc->sc_disks, d_next) { 1560 dp->load = (dp->d_consumer->index * LOAD_SCALE + 1561 dp->load * 7) / 8; 1562 } 1563 g_io_request(cbp, cp); 1564 } 1565 1566 static void 1567 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp) 1568 { 1569 struct bio_queue_head queue; 1570 struct g_mirror_disk *disk; 1571 struct g_consumer *cp; 1572 struct bio *cbp; 1573 off_t left, mod, offset, slice; 1574 u_char *data; 1575 u_int ndisks; 1576 1577 if (bp->bio_length <= sc->sc_slice) { 1578 g_mirror_request_round_robin(sc, bp); 1579 return; 1580 } 1581 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE); 1582 slice = bp->bio_length / ndisks; 1583 mod = slice % sc->sc_provider->sectorsize; 1584 if (mod != 0) 1585 slice += sc->sc_provider->sectorsize - mod; 1586 /* 1587 * Allocate all bios before sending any request, so we can 1588 * return ENOMEM in nice and clean way. 1589 */ 1590 left = bp->bio_length; 1591 offset = bp->bio_offset; 1592 data = bp->bio_data; 1593 bioq_init(&queue); 1594 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 1595 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) 1596 continue; 1597 cbp = g_clone_bio(bp); 1598 if (cbp == NULL) { 1599 while ((cbp = bioq_takefirst(&queue)) != NULL) 1600 g_destroy_bio(cbp); 1601 if (bp->bio_error == 0) 1602 bp->bio_error = ENOMEM; 1603 g_io_deliver(bp, bp->bio_error); 1604 return; 1605 } 1606 bioq_insert_tail(&queue, cbp); 1607 cbp->bio_done = g_mirror_done; 1608 cbp->bio_caller1 = disk; 1609 cbp->bio_to = disk->d_consumer->provider; 1610 cbp->bio_offset = offset; 1611 cbp->bio_data = data; 1612 cbp->bio_length = MIN(left, slice); 1613 left -= cbp->bio_length; 1614 if (left == 0) 1615 break; 1616 offset += cbp->bio_length; 1617 data += cbp->bio_length; 1618 } 1619 while ((cbp = bioq_takefirst(&queue)) != NULL) { 1620 G_MIRROR_LOGREQ(3, cbp, "Sending request."); 1621 disk = cbp->bio_caller1; 1622 cbp->bio_caller1 = NULL; 1623 cp = disk->d_consumer; 1624 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1, 1625 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, 1626 cp->acr, cp->acw, cp->ace)); 1627 disk->d_consumer->index++; 1628 g_io_request(cbp, disk->d_consumer); 1629 } 1630 } 1631 1632 static void 1633 g_mirror_register_request(struct bio *bp) 1634 { 1635 struct g_mirror_softc *sc; 1636 1637 sc = bp->bio_to->geom->softc; 1638 switch (bp->bio_cmd) { 1639 case BIO_READ: 1640 switch (sc->sc_balance) { 1641 case G_MIRROR_BALANCE_LOAD: 1642 g_mirror_request_load(sc, bp); 1643 break; 1644 case G_MIRROR_BALANCE_PREFER: 1645 g_mirror_request_prefer(sc, bp); 1646 break; 1647 case G_MIRROR_BALANCE_ROUND_ROBIN: 1648 g_mirror_request_round_robin(sc, bp); 1649 break; 1650 case G_MIRROR_BALANCE_SPLIT: 1651 g_mirror_request_split(sc, bp); 1652 break; 1653 } 1654 return; 1655 case BIO_WRITE: 1656 case BIO_DELETE: 1657 { 1658 struct g_mirror_disk *disk; 1659 struct g_mirror_disk_sync *sync; 1660 struct bio_queue_head queue; 1661 struct g_consumer *cp; 1662 struct bio *cbp; 1663 1664 /* 1665 * Delay the request if it is colliding with a synchronization 1666 * request. 1667 */ 1668 if (g_mirror_sync_collision(sc, bp)) { 1669 g_mirror_regular_delay(sc, bp); 1670 return; 1671 } 1672 1673 if (sc->sc_idle) 1674 g_mirror_unidle(sc); 1675 else 1676 sc->sc_last_write = time_uptime; 1677 1678 /* 1679 * Bump syncid on first write. 1680 */ 1681 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) { 1682 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID; 1683 g_mirror_bump_syncid(sc); 1684 } 1685 1686 /* 1687 * Allocate all bios before sending any request, so we can 1688 * return ENOMEM in nice and clean way. 1689 */ 1690 bioq_init(&queue); 1691 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 1692 sync = &disk->d_sync; 1693 switch (disk->d_state) { 1694 case G_MIRROR_DISK_STATE_ACTIVE: 1695 break; 1696 case G_MIRROR_DISK_STATE_SYNCHRONIZING: 1697 if (bp->bio_offset >= sync->ds_offset) 1698 continue; 1699 break; 1700 default: 1701 continue; 1702 } 1703 if (bp->bio_cmd == BIO_DELETE && 1704 (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE) == 0) 1705 continue; 1706 cbp = g_clone_bio(bp); 1707 if (cbp == NULL) { 1708 while ((cbp = bioq_takefirst(&queue)) != NULL) 1709 g_destroy_bio(cbp); 1710 if (bp->bio_error == 0) 1711 bp->bio_error = ENOMEM; 1712 g_io_deliver(bp, bp->bio_error); 1713 return; 1714 } 1715 bioq_insert_tail(&queue, cbp); 1716 cbp->bio_done = g_mirror_done; 1717 cp = disk->d_consumer; 1718 cbp->bio_caller1 = cp; 1719 cbp->bio_to = cp->provider; 1720 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1, 1721 ("Consumer %s not opened (r%dw%de%d).", 1722 cp->provider->name, cp->acr, cp->acw, cp->ace)); 1723 } 1724 if (bioq_first(&queue) == NULL) { 1725 g_io_deliver(bp, EOPNOTSUPP); 1726 return; 1727 } 1728 while ((cbp = bioq_takefirst(&queue)) != NULL) { 1729 G_MIRROR_LOGREQ(3, cbp, "Sending request."); 1730 cp = cbp->bio_caller1; 1731 cbp->bio_caller1 = NULL; 1732 cp->index++; 1733 sc->sc_writes++; 1734 g_io_request(cbp, cp); 1735 } 1736 /* 1737 * Put request onto inflight queue, so we can check if new 1738 * synchronization requests don't collide with it. 1739 */ 1740 bioq_insert_tail(&sc->sc_inflight, bp); 1741 return; 1742 } 1743 default: 1744 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)", 1745 bp->bio_cmd, sc->sc_name)); 1746 break; 1747 } 1748 } 1749 1750 static int 1751 g_mirror_can_destroy(struct g_mirror_softc *sc) 1752 { 1753 struct g_geom *gp; 1754 struct g_consumer *cp; 1755 1756 g_topology_assert(); 1757 gp = sc->sc_geom; 1758 if (gp->softc == NULL) 1759 return (1); 1760 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_TASTING) != 0) 1761 return (0); 1762 LIST_FOREACH(cp, &gp->consumer, consumer) { 1763 if (g_mirror_is_busy(sc, cp)) 1764 return (0); 1765 } 1766 gp = sc->sc_sync.ds_geom; 1767 LIST_FOREACH(cp, &gp->consumer, consumer) { 1768 if (g_mirror_is_busy(sc, cp)) 1769 return (0); 1770 } 1771 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.", 1772 sc->sc_name); 1773 return (1); 1774 } 1775 1776 static int 1777 g_mirror_try_destroy(struct g_mirror_softc *sc) 1778 { 1779 1780 if (sc->sc_rootmount != NULL) { 1781 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__, 1782 sc->sc_rootmount); 1783 root_mount_rel(sc->sc_rootmount); 1784 sc->sc_rootmount = NULL; 1785 } 1786 g_topology_lock(); 1787 if (!g_mirror_can_destroy(sc)) { 1788 g_topology_unlock(); 1789 return (0); 1790 } 1791 sc->sc_geom->softc = NULL; 1792 sc->sc_sync.ds_geom->softc = NULL; 1793 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WAIT) != 0) { 1794 g_topology_unlock(); 1795 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, 1796 &sc->sc_worker); 1797 /* Unlock sc_lock here, as it can be destroyed after wakeup. */ 1798 sx_xunlock(&sc->sc_lock); 1799 wakeup(&sc->sc_worker); 1800 sc->sc_worker = NULL; 1801 } else { 1802 g_topology_unlock(); 1803 g_mirror_destroy_device(sc); 1804 free(sc, M_MIRROR); 1805 } 1806 return (1); 1807 } 1808 1809 /* 1810 * Worker thread. 1811 */ 1812 static void 1813 g_mirror_worker(void *arg) 1814 { 1815 struct g_mirror_softc *sc; 1816 struct g_mirror_event *ep; 1817 struct bio *bp; 1818 int timeout; 1819 1820 sc = arg; 1821 thread_lock(curthread); 1822 sched_prio(curthread, PRIBIO); 1823 thread_unlock(curthread); 1824 1825 sx_xlock(&sc->sc_lock); 1826 for (;;) { 1827 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__); 1828 /* 1829 * First take a look at events. 1830 * This is important to handle events before any I/O requests. 1831 */ 1832 ep = g_mirror_event_get(sc); 1833 if (ep != NULL) { 1834 g_mirror_event_remove(sc, ep); 1835 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) { 1836 /* Update only device status. */ 1837 G_MIRROR_DEBUG(3, 1838 "Running event for device %s.", 1839 sc->sc_name); 1840 ep->e_error = 0; 1841 g_mirror_update_device(sc, true); 1842 } else { 1843 /* Update disk status. */ 1844 G_MIRROR_DEBUG(3, "Running event for disk %s.", 1845 g_mirror_get_diskname(ep->e_disk)); 1846 ep->e_error = g_mirror_update_disk(ep->e_disk, 1847 ep->e_state); 1848 if (ep->e_error == 0) 1849 g_mirror_update_device(sc, false); 1850 } 1851 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) { 1852 KASSERT(ep->e_error == 0, 1853 ("Error cannot be handled.")); 1854 g_mirror_event_free(ep); 1855 } else { 1856 ep->e_flags |= G_MIRROR_EVENT_DONE; 1857 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, 1858 ep); 1859 mtx_lock(&sc->sc_events_mtx); 1860 wakeup(ep); 1861 mtx_unlock(&sc->sc_events_mtx); 1862 } 1863 if ((sc->sc_flags & 1864 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) { 1865 if (g_mirror_try_destroy(sc)) { 1866 curthread->td_pflags &= ~TDP_GEOM; 1867 G_MIRROR_DEBUG(1, "Thread exiting."); 1868 kproc_exit(0); 1869 } 1870 } 1871 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__); 1872 continue; 1873 } 1874 /* 1875 * Check if we can mark array as CLEAN and if we can't take 1876 * how much seconds should we wait. 1877 */ 1878 timeout = g_mirror_idle(sc, -1); 1879 /* 1880 * Now I/O requests. 1881 */ 1882 /* Get first request from the queue. */ 1883 mtx_lock(&sc->sc_queue_mtx); 1884 bp = bioq_takefirst(&sc->sc_queue); 1885 if (bp == NULL) { 1886 if ((sc->sc_flags & 1887 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) { 1888 mtx_unlock(&sc->sc_queue_mtx); 1889 if (g_mirror_try_destroy(sc)) { 1890 curthread->td_pflags &= ~TDP_GEOM; 1891 G_MIRROR_DEBUG(1, "Thread exiting."); 1892 kproc_exit(0); 1893 } 1894 mtx_lock(&sc->sc_queue_mtx); 1895 } 1896 sx_xunlock(&sc->sc_lock); 1897 /* 1898 * XXX: We can miss an event here, because an event 1899 * can be added without sx-device-lock and without 1900 * mtx-queue-lock. Maybe I should just stop using 1901 * dedicated mutex for events synchronization and 1902 * stick with the queue lock? 1903 * The event will hang here until next I/O request 1904 * or next event is received. 1905 */ 1906 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1", 1907 timeout * hz); 1908 sx_xlock(&sc->sc_lock); 1909 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__); 1910 continue; 1911 } 1912 mtx_unlock(&sc->sc_queue_mtx); 1913 1914 if (bp->bio_from->geom == sc->sc_sync.ds_geom && 1915 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) { 1916 g_mirror_sync_request(bp); /* READ */ 1917 } else if (bp->bio_to != sc->sc_provider) { 1918 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0) 1919 g_mirror_regular_request(bp); 1920 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) 1921 g_mirror_sync_request(bp); /* WRITE */ 1922 else { 1923 KASSERT(0, 1924 ("Invalid request cflags=0x%hx to=%s.", 1925 bp->bio_cflags, bp->bio_to->name)); 1926 } 1927 } else { 1928 g_mirror_register_request(bp); 1929 } 1930 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__); 1931 } 1932 } 1933 1934 static void 1935 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk) 1936 { 1937 1938 sx_assert(&sc->sc_lock, SX_LOCKED); 1939 1940 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0) 1941 return; 1942 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) { 1943 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as dirty.", 1944 g_mirror_get_diskname(disk), sc->sc_name); 1945 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY; 1946 } else if (sc->sc_idle && 1947 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) { 1948 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as clean.", 1949 g_mirror_get_diskname(disk), sc->sc_name); 1950 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY; 1951 } 1952 } 1953 1954 static void 1955 g_mirror_sync_start(struct g_mirror_disk *disk) 1956 { 1957 struct g_mirror_softc *sc; 1958 struct g_consumer *cp; 1959 struct bio *bp; 1960 int error, i; 1961 1962 g_topology_assert_not(); 1963 sc = disk->d_softc; 1964 sx_assert(&sc->sc_lock, SX_LOCKED); 1965 1966 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING, 1967 ("Disk %s is not marked for synchronization.", 1968 g_mirror_get_diskname(disk))); 1969 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING, 1970 ("Device not in RUNNING state (%s, %u).", sc->sc_name, 1971 sc->sc_state)); 1972 1973 sx_xunlock(&sc->sc_lock); 1974 g_topology_lock(); 1975 cp = g_new_consumer(sc->sc_sync.ds_geom); 1976 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 1977 error = g_attach(cp, sc->sc_provider); 1978 KASSERT(error == 0, 1979 ("Cannot attach to %s (error=%d).", sc->sc_name, error)); 1980 error = g_access(cp, 1, 0, 0); 1981 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error)); 1982 g_topology_unlock(); 1983 sx_xlock(&sc->sc_lock); 1984 1985 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name, 1986 g_mirror_get_diskname(disk)); 1987 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0) 1988 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY; 1989 KASSERT(disk->d_sync.ds_consumer == NULL, 1990 ("Sync consumer already exists (device=%s, disk=%s).", 1991 sc->sc_name, g_mirror_get_diskname(disk))); 1992 1993 disk->d_sync.ds_consumer = cp; 1994 disk->d_sync.ds_consumer->private = disk; 1995 disk->d_sync.ds_consumer->index = 0; 1996 1997 /* 1998 * Allocate memory for synchronization bios and initialize them. 1999 */ 2000 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs, 2001 M_MIRROR, M_WAITOK); 2002 for (i = 0; i < g_mirror_syncreqs; i++) { 2003 bp = g_alloc_bio(); 2004 disk->d_sync.ds_bios[i] = bp; 2005 bp->bio_parent = NULL; 2006 bp->bio_cmd = BIO_READ; 2007 bp->bio_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK); 2008 bp->bio_cflags = 0; 2009 bp->bio_offset = disk->d_sync.ds_offset; 2010 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset); 2011 disk->d_sync.ds_offset += bp->bio_length; 2012 bp->bio_done = g_mirror_sync_done; 2013 bp->bio_from = disk->d_sync.ds_consumer; 2014 bp->bio_to = sc->sc_provider; 2015 bp->bio_caller1 = (void *)(uintptr_t)i; 2016 } 2017 2018 /* Increase the number of disks in SYNCHRONIZING state. */ 2019 sc->sc_sync.ds_ndisks++; 2020 /* Set the number of in-flight synchronization requests. */ 2021 disk->d_sync.ds_inflight = g_mirror_syncreqs; 2022 2023 /* 2024 * Fire off first synchronization requests. 2025 */ 2026 for (i = 0; i < g_mirror_syncreqs; i++) { 2027 bp = disk->d_sync.ds_bios[i]; 2028 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request."); 2029 disk->d_sync.ds_consumer->index++; 2030 /* 2031 * Delay the request if it is colliding with a regular request. 2032 */ 2033 if (g_mirror_regular_collision(sc, bp)) 2034 g_mirror_sync_delay(sc, bp); 2035 else 2036 g_io_request(bp, disk->d_sync.ds_consumer); 2037 } 2038 } 2039 2040 /* 2041 * Stop synchronization process. 2042 * type: 0 - synchronization finished 2043 * 1 - synchronization stopped 2044 */ 2045 static void 2046 g_mirror_sync_stop(struct g_mirror_disk *disk, int type) 2047 { 2048 struct g_mirror_softc *sc; 2049 struct g_consumer *cp; 2050 2051 g_topology_assert_not(); 2052 sc = disk->d_softc; 2053 sx_assert(&sc->sc_lock, SX_LOCKED); 2054 2055 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING, 2056 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk), 2057 g_mirror_disk_state2str(disk->d_state))); 2058 if (disk->d_sync.ds_consumer == NULL) 2059 return; 2060 2061 if (type == 0) { 2062 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.", 2063 sc->sc_name, g_mirror_get_diskname(disk)); 2064 } else /* if (type == 1) */ { 2065 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.", 2066 sc->sc_name, g_mirror_get_diskname(disk)); 2067 } 2068 free(disk->d_sync.ds_bios, M_MIRROR); 2069 disk->d_sync.ds_bios = NULL; 2070 cp = disk->d_sync.ds_consumer; 2071 disk->d_sync.ds_consumer = NULL; 2072 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY; 2073 sc->sc_sync.ds_ndisks--; 2074 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */ 2075 g_topology_lock(); 2076 g_mirror_kill_consumer(sc, cp); 2077 g_topology_unlock(); 2078 sx_xlock(&sc->sc_lock); 2079 } 2080 2081 static void 2082 g_mirror_launch_provider(struct g_mirror_softc *sc) 2083 { 2084 struct g_mirror_disk *disk; 2085 struct g_provider *pp, *dp; 2086 2087 sx_assert(&sc->sc_lock, SX_LOCKED); 2088 2089 g_topology_lock(); 2090 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name); 2091 pp->flags |= G_PF_DIRECT_RECEIVE; 2092 pp->mediasize = sc->sc_mediasize; 2093 pp->sectorsize = sc->sc_sectorsize; 2094 pp->stripesize = 0; 2095 pp->stripeoffset = 0; 2096 2097 /* Splitting of unmapped BIO's could work but isn't implemented now */ 2098 if (sc->sc_balance != G_MIRROR_BALANCE_SPLIT) 2099 pp->flags |= G_PF_ACCEPT_UNMAPPED; 2100 2101 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 2102 if (disk->d_consumer && disk->d_consumer->provider) { 2103 dp = disk->d_consumer->provider; 2104 if (dp->stripesize > pp->stripesize) { 2105 pp->stripesize = dp->stripesize; 2106 pp->stripeoffset = dp->stripeoffset; 2107 } 2108 /* A provider underneath us doesn't support unmapped */ 2109 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) { 2110 G_MIRROR_DEBUG(0, "Cancelling unmapped " 2111 "because of %s.", dp->name); 2112 pp->flags &= ~G_PF_ACCEPT_UNMAPPED; 2113 } 2114 } 2115 } 2116 sc->sc_provider = pp; 2117 g_error_provider(pp, 0); 2118 g_topology_unlock(); 2119 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name, 2120 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks); 2121 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 2122 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) 2123 g_mirror_sync_start(disk); 2124 } 2125 } 2126 2127 static void 2128 g_mirror_destroy_provider(struct g_mirror_softc *sc) 2129 { 2130 struct g_mirror_disk *disk; 2131 struct bio *bp; 2132 2133 g_topology_assert_not(); 2134 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).", 2135 sc->sc_name)); 2136 2137 g_topology_lock(); 2138 g_error_provider(sc->sc_provider, ENXIO); 2139 mtx_lock(&sc->sc_queue_mtx); 2140 while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) { 2141 /* 2142 * Abort any pending I/O that wasn't generated by us. 2143 * Synchronization requests and requests destined for individual 2144 * mirror components can be destroyed immediately. 2145 */ 2146 if (bp->bio_to == sc->sc_provider && 2147 bp->bio_from->geom != sc->sc_sync.ds_geom) { 2148 g_io_deliver(bp, ENXIO); 2149 } else { 2150 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) 2151 free(bp->bio_data, M_MIRROR); 2152 g_destroy_bio(bp); 2153 } 2154 } 2155 mtx_unlock(&sc->sc_queue_mtx); 2156 g_wither_provider(sc->sc_provider, ENXIO); 2157 sc->sc_provider = NULL; 2158 G_MIRROR_DEBUG(0, "Device %s: provider destroyed.", sc->sc_name); 2159 g_topology_unlock(); 2160 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 2161 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) 2162 g_mirror_sync_stop(disk, 1); 2163 } 2164 } 2165 2166 static void 2167 g_mirror_go(void *arg) 2168 { 2169 struct g_mirror_softc *sc; 2170 2171 sc = arg; 2172 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name); 2173 g_mirror_event_send(sc, 0, 2174 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE); 2175 } 2176 2177 static u_int 2178 g_mirror_determine_state(struct g_mirror_disk *disk) 2179 { 2180 struct g_mirror_softc *sc; 2181 u_int state; 2182 2183 sc = disk->d_softc; 2184 if (sc->sc_syncid == disk->d_sync.ds_syncid) { 2185 if ((disk->d_flags & 2186 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) { 2187 /* Disk does not need synchronization. */ 2188 state = G_MIRROR_DISK_STATE_ACTIVE; 2189 } else { 2190 if ((sc->sc_flags & 2191 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 || 2192 (disk->d_flags & 2193 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) { 2194 /* 2195 * We can start synchronization from 2196 * the stored offset. 2197 */ 2198 state = G_MIRROR_DISK_STATE_SYNCHRONIZING; 2199 } else { 2200 state = G_MIRROR_DISK_STATE_STALE; 2201 } 2202 } 2203 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) { 2204 /* 2205 * Reset all synchronization data for this disk, 2206 * because if it even was synchronized, it was 2207 * synchronized to disks with different syncid. 2208 */ 2209 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING; 2210 disk->d_sync.ds_offset = 0; 2211 disk->d_sync.ds_offset_done = 0; 2212 disk->d_sync.ds_syncid = sc->sc_syncid; 2213 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 || 2214 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) { 2215 state = G_MIRROR_DISK_STATE_SYNCHRONIZING; 2216 } else { 2217 state = G_MIRROR_DISK_STATE_STALE; 2218 } 2219 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ { 2220 /* 2221 * Not good, NOT GOOD! 2222 * It means that mirror was started on stale disks 2223 * and more fresh disk just arrive. 2224 * If there were writes, mirror is broken, sorry. 2225 * I think the best choice here is don't touch 2226 * this disk and inform the user loudly. 2227 */ 2228 G_MIRROR_DEBUG(0, "Device %s was started before the freshest " 2229 "disk (%s) arrives!! It will not be connected to the " 2230 "running device.", sc->sc_name, 2231 g_mirror_get_diskname(disk)); 2232 g_mirror_destroy_disk(disk); 2233 state = G_MIRROR_DISK_STATE_NONE; 2234 /* Return immediately, because disk was destroyed. */ 2235 return (state); 2236 } 2237 G_MIRROR_DEBUG(3, "State for %s disk: %s.", 2238 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state)); 2239 return (state); 2240 } 2241 2242 /* 2243 * Update device state. 2244 */ 2245 static void 2246 g_mirror_update_device(struct g_mirror_softc *sc, bool force) 2247 { 2248 struct g_mirror_disk *disk; 2249 u_int state; 2250 2251 sx_assert(&sc->sc_lock, SX_XLOCKED); 2252 2253 switch (sc->sc_state) { 2254 case G_MIRROR_DEVICE_STATE_STARTING: 2255 { 2256 struct g_mirror_disk *pdisk, *tdisk; 2257 u_int dirty, ndisks, genid, syncid; 2258 bool broken; 2259 2260 KASSERT(sc->sc_provider == NULL, 2261 ("Non-NULL provider in STARTING state (%s).", sc->sc_name)); 2262 /* 2263 * Are we ready? We are, if all disks are connected or 2264 * if we have any disks and 'force' is true. 2265 */ 2266 ndisks = g_mirror_ndisks(sc, -1); 2267 if (sc->sc_ndisks == ndisks || (force && ndisks > 0)) { 2268 ; 2269 } else if (ndisks == 0) { 2270 /* 2271 * Disks went down in starting phase, so destroy 2272 * device. 2273 */ 2274 callout_drain(&sc->sc_callout); 2275 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY; 2276 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__, 2277 sc->sc_rootmount); 2278 root_mount_rel(sc->sc_rootmount); 2279 sc->sc_rootmount = NULL; 2280 return; 2281 } else { 2282 return; 2283 } 2284 2285 /* 2286 * Activate all disks with the biggest syncid. 2287 */ 2288 if (force) { 2289 /* 2290 * If 'force' is true, we have been called due to 2291 * timeout, so don't bother canceling timeout. 2292 */ 2293 ndisks = 0; 2294 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 2295 if ((disk->d_flags & 2296 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) { 2297 ndisks++; 2298 } 2299 } 2300 if (ndisks == 0) { 2301 /* No valid disks found, destroy device. */ 2302 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY; 2303 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", 2304 __LINE__, sc->sc_rootmount); 2305 root_mount_rel(sc->sc_rootmount); 2306 sc->sc_rootmount = NULL; 2307 return; 2308 } 2309 } else { 2310 /* Cancel timeout. */ 2311 callout_drain(&sc->sc_callout); 2312 } 2313 2314 /* 2315 * Find the biggest genid. 2316 */ 2317 genid = 0; 2318 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 2319 if (disk->d_genid > genid) 2320 genid = disk->d_genid; 2321 } 2322 sc->sc_genid = genid; 2323 /* 2324 * Remove all disks without the biggest genid. 2325 */ 2326 broken = false; 2327 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) { 2328 if (disk->d_genid < genid) { 2329 G_MIRROR_DEBUG(0, 2330 "Component %s (device %s) broken, skipping.", 2331 g_mirror_get_diskname(disk), sc->sc_name); 2332 g_mirror_destroy_disk(disk); 2333 /* 2334 * Bump the syncid in case we discover a healthy 2335 * replacement disk after starting the mirror. 2336 */ 2337 broken = true; 2338 } 2339 } 2340 2341 /* 2342 * Find the biggest syncid. 2343 */ 2344 syncid = 0; 2345 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 2346 if (disk->d_sync.ds_syncid > syncid) 2347 syncid = disk->d_sync.ds_syncid; 2348 } 2349 2350 /* 2351 * Here we need to look for dirty disks and if all disks 2352 * with the biggest syncid are dirty, we have to choose 2353 * one with the biggest priority and rebuild the rest. 2354 */ 2355 /* 2356 * Find the number of dirty disks with the biggest syncid. 2357 * Find the number of disks with the biggest syncid. 2358 * While here, find a disk with the biggest priority. 2359 */ 2360 dirty = ndisks = 0; 2361 pdisk = NULL; 2362 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 2363 if (disk->d_sync.ds_syncid != syncid) 2364 continue; 2365 if ((disk->d_flags & 2366 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) { 2367 continue; 2368 } 2369 ndisks++; 2370 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) { 2371 dirty++; 2372 if (pdisk == NULL || 2373 pdisk->d_priority < disk->d_priority) { 2374 pdisk = disk; 2375 } 2376 } 2377 } 2378 if (dirty == 0) { 2379 /* No dirty disks at all, great. */ 2380 } else if (dirty == ndisks) { 2381 /* 2382 * Force synchronization for all dirty disks except one 2383 * with the biggest priority. 2384 */ 2385 KASSERT(pdisk != NULL, ("pdisk == NULL")); 2386 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a " 2387 "master disk for synchronization.", 2388 g_mirror_get_diskname(pdisk), sc->sc_name); 2389 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 2390 if (disk->d_sync.ds_syncid != syncid) 2391 continue; 2392 if ((disk->d_flags & 2393 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) { 2394 continue; 2395 } 2396 KASSERT((disk->d_flags & 2397 G_MIRROR_DISK_FLAG_DIRTY) != 0, 2398 ("Disk %s isn't marked as dirty.", 2399 g_mirror_get_diskname(disk))); 2400 /* Skip the disk with the biggest priority. */ 2401 if (disk == pdisk) 2402 continue; 2403 disk->d_sync.ds_syncid = 0; 2404 } 2405 } else if (dirty < ndisks) { 2406 /* 2407 * Force synchronization for all dirty disks. 2408 * We have some non-dirty disks. 2409 */ 2410 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 2411 if (disk->d_sync.ds_syncid != syncid) 2412 continue; 2413 if ((disk->d_flags & 2414 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) { 2415 continue; 2416 } 2417 if ((disk->d_flags & 2418 G_MIRROR_DISK_FLAG_DIRTY) == 0) { 2419 continue; 2420 } 2421 disk->d_sync.ds_syncid = 0; 2422 } 2423 } 2424 2425 /* Reset hint. */ 2426 sc->sc_hint = NULL; 2427 sc->sc_syncid = syncid; 2428 if (force || broken) { 2429 /* Remember to bump syncid on first write. */ 2430 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID; 2431 } 2432 state = G_MIRROR_DEVICE_STATE_RUNNING; 2433 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.", 2434 sc->sc_name, g_mirror_device_state2str(sc->sc_state), 2435 g_mirror_device_state2str(state)); 2436 sc->sc_state = state; 2437 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 2438 state = g_mirror_determine_state(disk); 2439 g_mirror_event_send(disk, state, 2440 G_MIRROR_EVENT_DONTWAIT); 2441 if (state == G_MIRROR_DISK_STATE_STALE) 2442 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID; 2443 } 2444 break; 2445 } 2446 case G_MIRROR_DEVICE_STATE_RUNNING: 2447 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 && 2448 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) { 2449 /* 2450 * No active disks or no disks at all, 2451 * so destroy device. 2452 */ 2453 if (sc->sc_provider != NULL) 2454 g_mirror_destroy_provider(sc); 2455 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY; 2456 break; 2457 } else if (g_mirror_ndisks(sc, 2458 G_MIRROR_DISK_STATE_ACTIVE) > 0 && 2459 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) { 2460 /* 2461 * We have active disks, launch provider if it doesn't 2462 * exist. 2463 */ 2464 if (sc->sc_provider == NULL) 2465 g_mirror_launch_provider(sc); 2466 if (sc->sc_rootmount != NULL) { 2467 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", 2468 __LINE__, sc->sc_rootmount); 2469 root_mount_rel(sc->sc_rootmount); 2470 sc->sc_rootmount = NULL; 2471 } 2472 } 2473 /* 2474 * Genid should be bumped immediately, so do it here. 2475 */ 2476 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) { 2477 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID; 2478 g_mirror_bump_genid(sc); 2479 } 2480 break; 2481 default: 2482 KASSERT(1 == 0, ("Wrong device state (%s, %s).", 2483 sc->sc_name, g_mirror_device_state2str(sc->sc_state))); 2484 break; 2485 } 2486 } 2487 2488 /* 2489 * Update disk state and device state if needed. 2490 */ 2491 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \ 2492 "Disk %s state changed from %s to %s (device %s).", \ 2493 g_mirror_get_diskname(disk), \ 2494 g_mirror_disk_state2str(disk->d_state), \ 2495 g_mirror_disk_state2str(state), sc->sc_name) 2496 static int 2497 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state) 2498 { 2499 struct g_mirror_softc *sc; 2500 2501 sc = disk->d_softc; 2502 sx_assert(&sc->sc_lock, SX_XLOCKED); 2503 2504 again: 2505 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.", 2506 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state), 2507 g_mirror_disk_state2str(state)); 2508 switch (state) { 2509 case G_MIRROR_DISK_STATE_NEW: 2510 /* 2511 * Possible scenarios: 2512 * 1. New disk arrive. 2513 */ 2514 /* Previous state should be NONE. */ 2515 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE, 2516 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk), 2517 g_mirror_disk_state2str(disk->d_state))); 2518 DISK_STATE_CHANGED(); 2519 2520 disk->d_state = state; 2521 if (LIST_EMPTY(&sc->sc_disks)) 2522 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next); 2523 else { 2524 struct g_mirror_disk *dp; 2525 2526 LIST_FOREACH(dp, &sc->sc_disks, d_next) { 2527 if (disk->d_priority >= dp->d_priority) { 2528 LIST_INSERT_BEFORE(dp, disk, d_next); 2529 dp = NULL; 2530 break; 2531 } 2532 if (LIST_NEXT(dp, d_next) == NULL) 2533 break; 2534 } 2535 if (dp != NULL) 2536 LIST_INSERT_AFTER(dp, disk, d_next); 2537 } 2538 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.", 2539 sc->sc_name, g_mirror_get_diskname(disk)); 2540 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) 2541 break; 2542 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING, 2543 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name, 2544 g_mirror_device_state2str(sc->sc_state), 2545 g_mirror_get_diskname(disk), 2546 g_mirror_disk_state2str(disk->d_state))); 2547 state = g_mirror_determine_state(disk); 2548 if (state != G_MIRROR_DISK_STATE_NONE) 2549 goto again; 2550 break; 2551 case G_MIRROR_DISK_STATE_ACTIVE: 2552 /* 2553 * Possible scenarios: 2554 * 1. New disk does not need synchronization. 2555 * 2. Synchronization process finished successfully. 2556 */ 2557 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING, 2558 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name, 2559 g_mirror_device_state2str(sc->sc_state), 2560 g_mirror_get_diskname(disk), 2561 g_mirror_disk_state2str(disk->d_state))); 2562 /* Previous state should be NEW or SYNCHRONIZING. */ 2563 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW || 2564 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING, 2565 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk), 2566 g_mirror_disk_state2str(disk->d_state))); 2567 DISK_STATE_CHANGED(); 2568 2569 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) { 2570 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING; 2571 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC; 2572 g_mirror_sync_stop(disk, 0); 2573 } 2574 disk->d_state = state; 2575 disk->d_sync.ds_offset = 0; 2576 disk->d_sync.ds_offset_done = 0; 2577 g_mirror_update_idle(sc, disk); 2578 g_mirror_update_metadata(disk); 2579 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.", 2580 sc->sc_name, g_mirror_get_diskname(disk)); 2581 break; 2582 case G_MIRROR_DISK_STATE_STALE: 2583 /* 2584 * Possible scenarios: 2585 * 1. Stale disk was connected. 2586 */ 2587 /* Previous state should be NEW. */ 2588 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW, 2589 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk), 2590 g_mirror_disk_state2str(disk->d_state))); 2591 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING, 2592 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name, 2593 g_mirror_device_state2str(sc->sc_state), 2594 g_mirror_get_diskname(disk), 2595 g_mirror_disk_state2str(disk->d_state))); 2596 /* 2597 * STALE state is only possible if device is marked 2598 * NOAUTOSYNC. 2599 */ 2600 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0, 2601 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name, 2602 g_mirror_device_state2str(sc->sc_state), 2603 g_mirror_get_diskname(disk), 2604 g_mirror_disk_state2str(disk->d_state))); 2605 DISK_STATE_CHANGED(); 2606 2607 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY; 2608 disk->d_state = state; 2609 g_mirror_update_metadata(disk); 2610 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.", 2611 sc->sc_name, g_mirror_get_diskname(disk)); 2612 break; 2613 case G_MIRROR_DISK_STATE_SYNCHRONIZING: 2614 /* 2615 * Possible scenarios: 2616 * 1. Disk which needs synchronization was connected. 2617 */ 2618 /* Previous state should be NEW. */ 2619 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW, 2620 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk), 2621 g_mirror_disk_state2str(disk->d_state))); 2622 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING, 2623 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name, 2624 g_mirror_device_state2str(sc->sc_state), 2625 g_mirror_get_diskname(disk), 2626 g_mirror_disk_state2str(disk->d_state))); 2627 DISK_STATE_CHANGED(); 2628 2629 if (disk->d_state == G_MIRROR_DISK_STATE_NEW) 2630 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY; 2631 disk->d_state = state; 2632 if (sc->sc_provider != NULL) { 2633 g_mirror_sync_start(disk); 2634 g_mirror_update_metadata(disk); 2635 } 2636 break; 2637 case G_MIRROR_DISK_STATE_DISCONNECTED: 2638 /* 2639 * Possible scenarios: 2640 * 1. Device wasn't running yet, but disk disappear. 2641 * 2. Disk was active and disapppear. 2642 * 3. Disk disappear during synchronization process. 2643 */ 2644 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) { 2645 /* 2646 * Previous state should be ACTIVE, STALE or 2647 * SYNCHRONIZING. 2648 */ 2649 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE || 2650 disk->d_state == G_MIRROR_DISK_STATE_STALE || 2651 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING, 2652 ("Wrong disk state (%s, %s).", 2653 g_mirror_get_diskname(disk), 2654 g_mirror_disk_state2str(disk->d_state))); 2655 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) { 2656 /* Previous state should be NEW. */ 2657 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW, 2658 ("Wrong disk state (%s, %s).", 2659 g_mirror_get_diskname(disk), 2660 g_mirror_disk_state2str(disk->d_state))); 2661 /* 2662 * Reset bumping syncid if disk disappeared in STARTING 2663 * state. 2664 */ 2665 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) 2666 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID; 2667 #ifdef INVARIANTS 2668 } else { 2669 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).", 2670 sc->sc_name, 2671 g_mirror_device_state2str(sc->sc_state), 2672 g_mirror_get_diskname(disk), 2673 g_mirror_disk_state2str(disk->d_state))); 2674 #endif 2675 } 2676 DISK_STATE_CHANGED(); 2677 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.", 2678 sc->sc_name, g_mirror_get_diskname(disk)); 2679 2680 g_mirror_destroy_disk(disk); 2681 break; 2682 case G_MIRROR_DISK_STATE_DESTROY: 2683 { 2684 int error; 2685 2686 error = g_mirror_clear_metadata(disk); 2687 if (error != 0) { 2688 G_MIRROR_DEBUG(0, 2689 "Device %s: failed to clear metadata on %s: %d.", 2690 sc->sc_name, g_mirror_get_diskname(disk), error); 2691 break; 2692 } 2693 DISK_STATE_CHANGED(); 2694 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.", 2695 sc->sc_name, g_mirror_get_diskname(disk)); 2696 2697 g_mirror_destroy_disk(disk); 2698 sc->sc_ndisks--; 2699 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 2700 g_mirror_update_metadata(disk); 2701 } 2702 break; 2703 } 2704 default: 2705 KASSERT(1 == 0, ("Unknown state (%u).", state)); 2706 break; 2707 } 2708 return (0); 2709 } 2710 #undef DISK_STATE_CHANGED 2711 2712 int 2713 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md) 2714 { 2715 struct g_provider *pp; 2716 u_char *buf; 2717 int error; 2718 2719 g_topology_assert(); 2720 2721 error = g_access(cp, 1, 0, 0); 2722 if (error != 0) 2723 return (error); 2724 pp = cp->provider; 2725 g_topology_unlock(); 2726 /* Metadata are stored on last sector. */ 2727 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 2728 &error); 2729 g_topology_lock(); 2730 g_access(cp, -1, 0, 0); 2731 if (buf == NULL) { 2732 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).", 2733 cp->provider->name, error); 2734 return (error); 2735 } 2736 2737 /* Decode metadata. */ 2738 error = mirror_metadata_decode(buf, md); 2739 g_free(buf); 2740 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0) 2741 return (EINVAL); 2742 if (md->md_version > G_MIRROR_VERSION) { 2743 G_MIRROR_DEBUG(0, 2744 "Kernel module is too old to handle metadata from %s.", 2745 cp->provider->name); 2746 return (EINVAL); 2747 } 2748 if (error != 0) { 2749 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.", 2750 cp->provider->name); 2751 return (error); 2752 } 2753 2754 return (0); 2755 } 2756 2757 static int 2758 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp, 2759 struct g_mirror_metadata *md) 2760 { 2761 2762 if (g_mirror_id2disk(sc, md->md_did) != NULL) { 2763 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.", 2764 pp->name, md->md_did); 2765 return (EEXIST); 2766 } 2767 if (md->md_all != sc->sc_ndisks) { 2768 G_MIRROR_DEBUG(1, 2769 "Invalid '%s' field on disk %s (device %s), skipping.", 2770 "md_all", pp->name, sc->sc_name); 2771 return (EINVAL); 2772 } 2773 if (md->md_slice != sc->sc_slice) { 2774 G_MIRROR_DEBUG(1, 2775 "Invalid '%s' field on disk %s (device %s), skipping.", 2776 "md_slice", pp->name, sc->sc_name); 2777 return (EINVAL); 2778 } 2779 if (md->md_balance != sc->sc_balance) { 2780 G_MIRROR_DEBUG(1, 2781 "Invalid '%s' field on disk %s (device %s), skipping.", 2782 "md_balance", pp->name, sc->sc_name); 2783 return (EINVAL); 2784 } 2785 #if 0 2786 if (md->md_mediasize != sc->sc_mediasize) { 2787 G_MIRROR_DEBUG(1, 2788 "Invalid '%s' field on disk %s (device %s), skipping.", 2789 "md_mediasize", pp->name, sc->sc_name); 2790 return (EINVAL); 2791 } 2792 #endif 2793 if (sc->sc_mediasize > pp->mediasize) { 2794 G_MIRROR_DEBUG(1, 2795 "Invalid size of disk %s (device %s), skipping.", pp->name, 2796 sc->sc_name); 2797 return (EINVAL); 2798 } 2799 if (md->md_sectorsize != sc->sc_sectorsize) { 2800 G_MIRROR_DEBUG(1, 2801 "Invalid '%s' field on disk %s (device %s), skipping.", 2802 "md_sectorsize", pp->name, sc->sc_name); 2803 return (EINVAL); 2804 } 2805 if ((sc->sc_sectorsize % pp->sectorsize) != 0) { 2806 G_MIRROR_DEBUG(1, 2807 "Invalid sector size of disk %s (device %s), skipping.", 2808 pp->name, sc->sc_name); 2809 return (EINVAL); 2810 } 2811 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) { 2812 G_MIRROR_DEBUG(1, 2813 "Invalid device flags on disk %s (device %s), skipping.", 2814 pp->name, sc->sc_name); 2815 return (EINVAL); 2816 } 2817 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) { 2818 G_MIRROR_DEBUG(1, 2819 "Invalid disk flags on disk %s (device %s), skipping.", 2820 pp->name, sc->sc_name); 2821 return (EINVAL); 2822 } 2823 return (0); 2824 } 2825 2826 int 2827 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp, 2828 struct g_mirror_metadata *md) 2829 { 2830 struct g_mirror_disk *disk; 2831 int error; 2832 2833 g_topology_assert_not(); 2834 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name); 2835 2836 error = g_mirror_check_metadata(sc, pp, md); 2837 if (error != 0) 2838 return (error); 2839 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING && 2840 md->md_genid < sc->sc_genid) { 2841 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.", 2842 pp->name, sc->sc_name); 2843 return (EINVAL); 2844 } 2845 disk = g_mirror_init_disk(sc, pp, md, &error); 2846 if (disk == NULL) 2847 return (error); 2848 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW, 2849 G_MIRROR_EVENT_WAIT); 2850 if (error != 0) 2851 return (error); 2852 if (md->md_version < G_MIRROR_VERSION) { 2853 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).", 2854 pp->name, md->md_version, G_MIRROR_VERSION); 2855 g_mirror_update_metadata(disk); 2856 } 2857 return (0); 2858 } 2859 2860 static void 2861 g_mirror_destroy_delayed(void *arg, int flag) 2862 { 2863 struct g_mirror_softc *sc; 2864 int error; 2865 2866 if (flag == EV_CANCEL) { 2867 G_MIRROR_DEBUG(1, "Destroying canceled."); 2868 return; 2869 } 2870 sc = arg; 2871 g_topology_unlock(); 2872 sx_xlock(&sc->sc_lock); 2873 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0, 2874 ("DESTROY flag set on %s.", sc->sc_name)); 2875 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0, 2876 ("DESTROYING flag not set on %s.", sc->sc_name)); 2877 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name); 2878 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT); 2879 if (error != 0) { 2880 G_MIRROR_DEBUG(0, "Cannot destroy %s (error=%d).", 2881 sc->sc_name, error); 2882 sx_xunlock(&sc->sc_lock); 2883 } 2884 g_topology_lock(); 2885 } 2886 2887 static int 2888 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace) 2889 { 2890 struct g_mirror_softc *sc; 2891 int error = 0; 2892 2893 g_topology_assert(); 2894 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr, 2895 acw, ace); 2896 2897 sc = pp->geom->softc; 2898 if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0) 2899 return (0); 2900 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name)); 2901 2902 g_topology_unlock(); 2903 sx_xlock(&sc->sc_lock); 2904 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 || 2905 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0 || 2906 LIST_EMPTY(&sc->sc_disks)) { 2907 if (acr > 0 || acw > 0 || ace > 0) 2908 error = ENXIO; 2909 goto end; 2910 } 2911 sc->sc_provider_open += acr + acw + ace; 2912 if (pp->acw + acw == 0) 2913 g_mirror_idle(sc, 0); 2914 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0 && 2915 sc->sc_provider_open == 0) 2916 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK, sc, NULL); 2917 end: 2918 sx_xunlock(&sc->sc_lock); 2919 g_topology_lock(); 2920 return (error); 2921 } 2922 2923 static struct g_geom * 2924 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md) 2925 { 2926 struct g_mirror_softc *sc; 2927 struct g_geom *gp; 2928 int error, timeout; 2929 2930 g_topology_assert(); 2931 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name, 2932 md->md_mid); 2933 2934 /* One disk is minimum. */ 2935 if (md->md_all < 1) 2936 return (NULL); 2937 /* 2938 * Action geom. 2939 */ 2940 gp = g_new_geomf(mp, "%s", md->md_name); 2941 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO); 2942 gp->start = g_mirror_start; 2943 gp->orphan = g_mirror_orphan; 2944 gp->access = g_mirror_access; 2945 gp->dumpconf = g_mirror_dumpconf; 2946 2947 sc->sc_id = md->md_mid; 2948 sc->sc_slice = md->md_slice; 2949 sc->sc_balance = md->md_balance; 2950 sc->sc_mediasize = md->md_mediasize; 2951 sc->sc_sectorsize = md->md_sectorsize; 2952 sc->sc_ndisks = md->md_all; 2953 sc->sc_flags = md->md_mflags; 2954 sc->sc_bump_id = 0; 2955 sc->sc_idle = 1; 2956 sc->sc_last_write = time_uptime; 2957 sc->sc_writes = 0; 2958 sx_init(&sc->sc_lock, "gmirror:lock"); 2959 bioq_init(&sc->sc_queue); 2960 mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF); 2961 bioq_init(&sc->sc_regular_delayed); 2962 bioq_init(&sc->sc_inflight); 2963 bioq_init(&sc->sc_sync_delayed); 2964 LIST_INIT(&sc->sc_disks); 2965 TAILQ_INIT(&sc->sc_events); 2966 mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF); 2967 callout_init(&sc->sc_callout, 1); 2968 mtx_init(&sc->sc_done_mtx, "gmirror:done", NULL, MTX_DEF); 2969 sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING; 2970 gp->softc = sc; 2971 sc->sc_geom = gp; 2972 sc->sc_provider = NULL; 2973 sc->sc_provider_open = 0; 2974 /* 2975 * Synchronization geom. 2976 */ 2977 gp = g_new_geomf(mp, "%s.sync", md->md_name); 2978 gp->softc = sc; 2979 gp->orphan = g_mirror_orphan; 2980 sc->sc_sync.ds_geom = gp; 2981 sc->sc_sync.ds_ndisks = 0; 2982 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0, 2983 "g_mirror %s", md->md_name); 2984 if (error != 0) { 2985 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.", 2986 sc->sc_name); 2987 g_destroy_geom(sc->sc_sync.ds_geom); 2988 mtx_destroy(&sc->sc_done_mtx); 2989 mtx_destroy(&sc->sc_events_mtx); 2990 mtx_destroy(&sc->sc_queue_mtx); 2991 sx_destroy(&sc->sc_lock); 2992 g_destroy_geom(sc->sc_geom); 2993 free(sc, M_MIRROR); 2994 return (NULL); 2995 } 2996 2997 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).", 2998 sc->sc_name, sc->sc_ndisks, sc->sc_id); 2999 3000 sc->sc_rootmount = root_mount_hold("GMIRROR"); 3001 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount); 3002 /* 3003 * Run timeout. 3004 */ 3005 timeout = g_mirror_timeout * hz; 3006 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc); 3007 return (sc->sc_geom); 3008 } 3009 3010 int 3011 g_mirror_destroy(struct g_mirror_softc *sc, int how) 3012 { 3013 struct g_mirror_disk *disk; 3014 3015 g_topology_assert_not(); 3016 if (sc == NULL) 3017 return (ENXIO); 3018 sx_assert(&sc->sc_lock, SX_XLOCKED); 3019 3020 if (sc->sc_provider_open != 0 || SCHEDULER_STOPPED()) { 3021 switch (how) { 3022 case G_MIRROR_DESTROY_SOFT: 3023 G_MIRROR_DEBUG(1, 3024 "Device %s is still open (%d).", sc->sc_name, 3025 sc->sc_provider_open); 3026 return (EBUSY); 3027 case G_MIRROR_DESTROY_DELAYED: 3028 G_MIRROR_DEBUG(1, 3029 "Device %s will be destroyed on last close.", 3030 sc->sc_name); 3031 LIST_FOREACH(disk, &sc->sc_disks, d_next) { 3032 if (disk->d_state == 3033 G_MIRROR_DISK_STATE_SYNCHRONIZING) { 3034 g_mirror_sync_stop(disk, 1); 3035 } 3036 } 3037 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROYING; 3038 return (EBUSY); 3039 case G_MIRROR_DESTROY_HARD: 3040 G_MIRROR_DEBUG(1, "Device %s is still open, so it " 3041 "can't be definitely removed.", sc->sc_name); 3042 } 3043 } 3044 3045 g_topology_lock(); 3046 if (sc->sc_geom->softc == NULL) { 3047 g_topology_unlock(); 3048 return (0); 3049 } 3050 sc->sc_geom->softc = NULL; 3051 sc->sc_sync.ds_geom->softc = NULL; 3052 g_topology_unlock(); 3053 3054 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY; 3055 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WAIT; 3056 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc); 3057 sx_xunlock(&sc->sc_lock); 3058 mtx_lock(&sc->sc_queue_mtx); 3059 wakeup(sc); 3060 mtx_unlock(&sc->sc_queue_mtx); 3061 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker); 3062 while (sc->sc_worker != NULL) 3063 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5); 3064 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker); 3065 sx_xlock(&sc->sc_lock); 3066 g_mirror_destroy_device(sc); 3067 free(sc, M_MIRROR); 3068 return (0); 3069 } 3070 3071 static void 3072 g_mirror_taste_orphan(struct g_consumer *cp) 3073 { 3074 3075 KASSERT(1 == 0, ("%s called while tasting %s.", __func__, 3076 cp->provider->name)); 3077 } 3078 3079 static struct g_geom * 3080 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 3081 { 3082 struct g_mirror_metadata md; 3083 struct g_mirror_softc *sc; 3084 struct g_consumer *cp; 3085 struct g_geom *gp; 3086 int error; 3087 3088 g_topology_assert(); 3089 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 3090 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name); 3091 3092 gp = g_new_geomf(mp, "mirror:taste"); 3093 /* 3094 * This orphan function should be never called. 3095 */ 3096 gp->orphan = g_mirror_taste_orphan; 3097 cp = g_new_consumer(gp); 3098 g_attach(cp, pp); 3099 error = g_mirror_read_metadata(cp, &md); 3100 g_detach(cp); 3101 g_destroy_consumer(cp); 3102 g_destroy_geom(gp); 3103 if (error != 0) 3104 return (NULL); 3105 gp = NULL; 3106 3107 if (md.md_provider[0] != '\0' && 3108 !g_compare_names(md.md_provider, pp->name)) 3109 return (NULL); 3110 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize) 3111 return (NULL); 3112 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) { 3113 G_MIRROR_DEBUG(0, 3114 "Device %s: provider %s marked as inactive, skipping.", 3115 md.md_name, pp->name); 3116 return (NULL); 3117 } 3118 if (g_mirror_debug >= 2) 3119 mirror_metadata_dump(&md); 3120 3121 /* 3122 * Let's check if device already exists. 3123 */ 3124 sc = NULL; 3125 LIST_FOREACH(gp, &mp->geom, geom) { 3126 sc = gp->softc; 3127 if (sc == NULL) 3128 continue; 3129 if (sc->sc_sync.ds_geom == gp) 3130 continue; 3131 if (strcmp(md.md_name, sc->sc_name) != 0) 3132 continue; 3133 if (md.md_mid != sc->sc_id) { 3134 G_MIRROR_DEBUG(0, "Device %s already configured.", 3135 sc->sc_name); 3136 return (NULL); 3137 } 3138 break; 3139 } 3140 if (gp == NULL) { 3141 gp = g_mirror_create(mp, &md); 3142 if (gp == NULL) { 3143 G_MIRROR_DEBUG(0, "Cannot create device %s.", 3144 md.md_name); 3145 return (NULL); 3146 } 3147 sc = gp->softc; 3148 } 3149 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 3150 g_topology_unlock(); 3151 sx_xlock(&sc->sc_lock); 3152 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING; 3153 error = g_mirror_add_disk(sc, pp, &md); 3154 if (error != 0) { 3155 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).", 3156 pp->name, gp->name, error); 3157 if (LIST_EMPTY(&sc->sc_disks)) { 3158 g_cancel_event(sc); 3159 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD); 3160 g_topology_lock(); 3161 return (NULL); 3162 } 3163 gp = NULL; 3164 } 3165 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING; 3166 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) { 3167 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD); 3168 g_topology_lock(); 3169 return (NULL); 3170 } 3171 sx_xunlock(&sc->sc_lock); 3172 g_topology_lock(); 3173 return (gp); 3174 } 3175 3176 static void 3177 g_mirror_resize(struct g_consumer *cp) 3178 { 3179 struct g_mirror_disk *disk; 3180 3181 g_topology_assert(); 3182 g_trace(G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name); 3183 3184 disk = cp->private; 3185 if (disk == NULL) 3186 return; 3187 g_topology_unlock(); 3188 g_mirror_update_metadata(disk); 3189 g_topology_lock(); 3190 } 3191 3192 static int 3193 g_mirror_destroy_geom(struct gctl_req *req __unused, 3194 struct g_class *mp __unused, struct g_geom *gp) 3195 { 3196 struct g_mirror_softc *sc; 3197 int error; 3198 3199 g_topology_unlock(); 3200 sc = gp->softc; 3201 sx_xlock(&sc->sc_lock); 3202 g_cancel_event(sc); 3203 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT); 3204 if (error != 0) 3205 sx_xunlock(&sc->sc_lock); 3206 g_topology_lock(); 3207 return (error); 3208 } 3209 3210 static void 3211 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 3212 struct g_consumer *cp, struct g_provider *pp) 3213 { 3214 struct g_mirror_softc *sc; 3215 3216 g_topology_assert(); 3217 3218 sc = gp->softc; 3219 if (sc == NULL) 3220 return; 3221 /* Skip synchronization geom. */ 3222 if (gp == sc->sc_sync.ds_geom) 3223 return; 3224 if (pp != NULL) { 3225 /* Nothing here. */ 3226 } else if (cp != NULL) { 3227 struct g_mirror_disk *disk; 3228 3229 disk = cp->private; 3230 if (disk == NULL) 3231 return; 3232 g_topology_unlock(); 3233 sx_xlock(&sc->sc_lock); 3234 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id); 3235 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) { 3236 sbuf_printf(sb, "%s<Synchronized>", indent); 3237 if (disk->d_sync.ds_offset == 0) 3238 sbuf_printf(sb, "0%%"); 3239 else { 3240 sbuf_printf(sb, "%u%%", 3241 (u_int)((disk->d_sync.ds_offset * 100) / 3242 sc->sc_provider->mediasize)); 3243 } 3244 sbuf_printf(sb, "</Synchronized>\n"); 3245 if (disk->d_sync.ds_offset > 0) { 3246 sbuf_printf(sb, "%s<BytesSynced>%jd" 3247 "</BytesSynced>\n", indent, 3248 (intmax_t)disk->d_sync.ds_offset); 3249 } 3250 } 3251 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, 3252 disk->d_sync.ds_syncid); 3253 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, 3254 disk->d_genid); 3255 sbuf_printf(sb, "%s<Flags>", indent); 3256 if (disk->d_flags == 0) 3257 sbuf_printf(sb, "NONE"); 3258 else { 3259 int first = 1; 3260 3261 #define ADD_FLAG(flag, name) do { \ 3262 if ((disk->d_flags & (flag)) != 0) { \ 3263 if (!first) \ 3264 sbuf_printf(sb, ", "); \ 3265 else \ 3266 first = 0; \ 3267 sbuf_printf(sb, name); \ 3268 } \ 3269 } while (0) 3270 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY"); 3271 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED"); 3272 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE"); 3273 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING, 3274 "SYNCHRONIZING"); 3275 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC"); 3276 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN"); 3277 #undef ADD_FLAG 3278 } 3279 sbuf_printf(sb, "</Flags>\n"); 3280 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent, 3281 disk->d_priority); 3282 sbuf_printf(sb, "%s<State>%s</State>\n", indent, 3283 g_mirror_disk_state2str(disk->d_state)); 3284 sx_xunlock(&sc->sc_lock); 3285 g_topology_lock(); 3286 } else { 3287 g_topology_unlock(); 3288 sx_xlock(&sc->sc_lock); 3289 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id); 3290 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid); 3291 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid); 3292 sbuf_printf(sb, "%s<Flags>", indent); 3293 if (sc->sc_flags == 0) 3294 sbuf_printf(sb, "NONE"); 3295 else { 3296 int first = 1; 3297 3298 #define ADD_FLAG(flag, name) do { \ 3299 if ((sc->sc_flags & (flag)) != 0) { \ 3300 if (!first) \ 3301 sbuf_printf(sb, ", "); \ 3302 else \ 3303 first = 0; \ 3304 sbuf_printf(sb, name); \ 3305 } \ 3306 } while (0) 3307 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC"); 3308 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC"); 3309 #undef ADD_FLAG 3310 } 3311 sbuf_printf(sb, "</Flags>\n"); 3312 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent, 3313 (u_int)sc->sc_slice); 3314 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent, 3315 balance_name(sc->sc_balance)); 3316 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent, 3317 sc->sc_ndisks); 3318 sbuf_printf(sb, "%s<State>", indent); 3319 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) 3320 sbuf_printf(sb, "%s", "STARTING"); 3321 else if (sc->sc_ndisks == 3322 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE)) 3323 sbuf_printf(sb, "%s", "COMPLETE"); 3324 else 3325 sbuf_printf(sb, "%s", "DEGRADED"); 3326 sbuf_printf(sb, "</State>\n"); 3327 sx_xunlock(&sc->sc_lock); 3328 g_topology_lock(); 3329 } 3330 } 3331 3332 static void 3333 g_mirror_shutdown_post_sync(void *arg, int howto) 3334 { 3335 struct g_class *mp; 3336 struct g_geom *gp, *gp2; 3337 struct g_mirror_softc *sc; 3338 int error; 3339 3340 mp = arg; 3341 g_topology_lock(); 3342 g_mirror_shutdown = 1; 3343 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) { 3344 if ((sc = gp->softc) == NULL) 3345 continue; 3346 /* Skip synchronization geom. */ 3347 if (gp == sc->sc_sync.ds_geom) 3348 continue; 3349 g_topology_unlock(); 3350 sx_xlock(&sc->sc_lock); 3351 g_mirror_idle(sc, -1); 3352 g_cancel_event(sc); 3353 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED); 3354 if (error != 0) 3355 sx_xunlock(&sc->sc_lock); 3356 g_topology_lock(); 3357 } 3358 g_topology_unlock(); 3359 } 3360 3361 static void 3362 g_mirror_init(struct g_class *mp) 3363 { 3364 3365 g_mirror_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync, 3366 g_mirror_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST); 3367 if (g_mirror_post_sync == NULL) 3368 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event."); 3369 } 3370 3371 static void 3372 g_mirror_fini(struct g_class *mp) 3373 { 3374 3375 if (g_mirror_post_sync != NULL) 3376 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_mirror_post_sync); 3377 } 3378 3379 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror); 3380