2 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/limits.h>
36 #include <sys/mutex.h>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
40 #include <sys/eventhandler.h>
42 #include <geom/geom.h>
44 #include <sys/kthread.h>
45 #include <sys/sched.h>
46 #include <geom/mirror/g_mirror.h>
49 static MALLOC_DEFINE(M_MIRROR, "mirror_data", "GEOM_MIRROR Data");
51 SYSCTL_DECL(_kern_geom);
52 SYSCTL_NODE(_kern_geom, OID_AUTO, mirror, CTLFLAG_RW, 0, "GEOM_MIRROR stuff");
53 u_int g_mirror_debug = 0;
54 TUNABLE_INT("kern.geom.mirror.debug", &g_mirror_debug);
55 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, debug, CTLFLAG_RW, &g_mirror_debug, 0,
57 static u_int g_mirror_timeout = 4;
58 TUNABLE_INT("kern.geom.mirror.timeout", &g_mirror_timeout);
59 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, timeout, CTLFLAG_RW, &g_mirror_timeout,
60 0, "Time to wait on all mirror components");
61 static u_int g_mirror_idletime = 5;
62 TUNABLE_INT("kern.geom.mirror.idletime", &g_mirror_idletime);
63 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, idletime, CTLFLAG_RW,
64 &g_mirror_idletime, 0, "Mark components as clean when idling");
65 static u_int g_mirror_disconnect_on_failure = 1;
66 TUNABLE_INT("kern.geom.mirror.disconnect_on_failure",
67 &g_mirror_disconnect_on_failure);
68 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
69 &g_mirror_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
70 static u_int g_mirror_syncreqs = 2;
71 TUNABLE_INT("kern.geom.mirror.sync_requests", &g_mirror_syncreqs);
72 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
73 &g_mirror_syncreqs, 0, "Parallel synchronization I/O requests.");
75 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \
76 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
77 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
78 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
81 static eventhandler_tag g_mirror_pre_sync = NULL;
83 static int g_mirror_destroy_geom(struct gctl_req *req, struct g_class *mp,
85 static g_taste_t g_mirror_taste;
86 static void g_mirror_init(struct g_class *mp);
87 static void g_mirror_fini(struct g_class *mp);
89 struct g_class g_mirror_class = {
90 .name = G_MIRROR_CLASS_NAME,
92 .ctlreq = g_mirror_config,
93 .taste = g_mirror_taste,
94 .destroy_geom = g_mirror_destroy_geom,
95 .init = g_mirror_init,
100 static void g_mirror_destroy_provider(struct g_mirror_softc *sc);
101 static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
102 static void g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force);
103 static void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
104 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
105 static void g_mirror_sync_stop(struct g_mirror_disk *disk, int type);
106 static void g_mirror_register_request(struct bio *bp);
107 static void g_mirror_sync_release(struct g_mirror_softc *sc);
111 g_mirror_disk_state2str(int state)
115 case G_MIRROR_DISK_STATE_NONE:
117 case G_MIRROR_DISK_STATE_NEW:
119 case G_MIRROR_DISK_STATE_ACTIVE:
121 case G_MIRROR_DISK_STATE_STALE:
123 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
124 return ("SYNCHRONIZING");
125 case G_MIRROR_DISK_STATE_DISCONNECTED:
126 return ("DISCONNECTED");
127 case G_MIRROR_DISK_STATE_DESTROY:
135 g_mirror_device_state2str(int state)
139 case G_MIRROR_DEVICE_STATE_STARTING:
141 case G_MIRROR_DEVICE_STATE_RUNNING:
149 g_mirror_get_diskname(struct g_mirror_disk *disk)
152 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
153 return ("[unknown]");
154 return (disk->d_name);
158 * --- Events handling functions ---
159 * Events in geom_mirror are used to maintain disks and device status
160 * from one thread to simplify locking.
163 g_mirror_event_free(struct g_mirror_event *ep)
170 g_mirror_event_send(void *arg, int state, int flags)
172 struct g_mirror_softc *sc;
173 struct g_mirror_disk *disk;
174 struct g_mirror_event *ep;
177 ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
178 G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
179 if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
190 mtx_lock(&sc->sc_events_mtx);
191 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
192 mtx_unlock(&sc->sc_events_mtx);
193 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
194 mtx_lock(&sc->sc_queue_mtx);
196 mtx_unlock(&sc->sc_queue_mtx);
197 if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0)
199 sx_assert(&sc->sc_lock, SX_XLOCKED);
200 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
201 sx_xunlock(&sc->sc_lock);
202 while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) {
203 mtx_lock(&sc->sc_events_mtx);
204 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event",
208 g_mirror_event_free(ep);
209 sx_xlock(&sc->sc_lock);
213 static struct g_mirror_event *
214 g_mirror_event_get(struct g_mirror_softc *sc)
216 struct g_mirror_event *ep;
218 mtx_lock(&sc->sc_events_mtx);
219 ep = TAILQ_FIRST(&sc->sc_events);
220 mtx_unlock(&sc->sc_events_mtx);
225 g_mirror_event_remove(struct g_mirror_softc *sc, struct g_mirror_event *ep)
228 mtx_lock(&sc->sc_events_mtx);
229 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
230 mtx_unlock(&sc->sc_events_mtx);
234 g_mirror_event_cancel(struct g_mirror_disk *disk)
236 struct g_mirror_softc *sc;
237 struct g_mirror_event *ep, *tmpep;
240 sx_assert(&sc->sc_lock, SX_XLOCKED);
242 mtx_lock(&sc->sc_events_mtx);
243 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
244 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0)
246 if (ep->e_disk != disk)
248 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
249 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
250 g_mirror_event_free(ep);
252 ep->e_error = ECANCELED;
256 mtx_unlock(&sc->sc_events_mtx);
260 * Return the number of disks in given state.
261 * If state is equal to -1, count all connected disks.
264 g_mirror_ndisks(struct g_mirror_softc *sc, int state)
266 struct g_mirror_disk *disk;
269 sx_assert(&sc->sc_lock, SX_LOCKED);
271 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
272 if (state == -1 || disk->d_state == state)
279 * Find a disk in mirror by its disk ID.
281 static struct g_mirror_disk *
282 g_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id)
284 struct g_mirror_disk *disk;
286 sx_assert(&sc->sc_lock, SX_XLOCKED);
288 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
289 if (disk->d_id == id)
296 g_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp)
301 mtx_lock(&sc->sc_queue_mtx);
302 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
303 if (bp->bio_from == cp)
306 mtx_unlock(&sc->sc_queue_mtx);
311 g_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp)
316 "I/O requests for %s exist, can't destroy it now.",
320 if (g_mirror_nrequests(sc, cp) > 0) {
322 "I/O requests for %s in queue, can't destroy it now.",
330 g_mirror_destroy_consumer(void *arg, int flags __unused)
332 struct g_consumer *cp;
337 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
339 g_destroy_consumer(cp);
343 g_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
345 struct g_provider *pp;
351 if (g_mirror_is_busy(sc, cp))
356 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
359 G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
360 -cp->acw, -cp->ace, 0);
361 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
362 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
365 * After retaste event was send (inside g_access()), we can send
366 * event to detach and destroy consumer.
367 * A class, which has consumer to the given provider connected
368 * will not receive retaste event for the provider.
369 * This is the way how I ignore retaste events when I close
370 * consumers opened for write: I detach and destroy consumer
371 * after retaste event is sent.
373 g_post_event(g_mirror_destroy_consumer, cp, M_WAITOK, NULL);
376 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", pp->name);
378 g_destroy_consumer(cp);
382 g_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp)
384 struct g_consumer *cp;
387 g_topology_assert_not();
388 KASSERT(disk->d_consumer == NULL,
389 ("Disk already connected (device %s).", disk->d_softc->sc_name));
392 cp = g_new_consumer(disk->d_softc->sc_geom);
393 error = g_attach(cp, pp);
395 g_destroy_consumer(cp);
399 error = g_access(cp, 1, 1, 1);
402 g_destroy_consumer(cp);
404 G_MIRROR_DEBUG(0, "Cannot open consumer %s (error=%d).",
409 disk->d_consumer = cp;
410 disk->d_consumer->private = disk;
411 disk->d_consumer->index = 0;
413 G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk));
418 g_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
425 if (cp->provider != NULL)
426 g_mirror_kill_consumer(sc, cp);
428 g_destroy_consumer(cp);
432 * Initialize disk. This means allocate memory, create consumer, attach it
433 * to the provider and open access (r1w1e1) to it.
435 static struct g_mirror_disk *
436 g_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp,
437 struct g_mirror_metadata *md, int *errorp)
439 struct g_mirror_disk *disk;
442 disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO);
448 error = g_mirror_connect_disk(disk, pp);
451 disk->d_id = md->md_did;
452 disk->d_state = G_MIRROR_DISK_STATE_NONE;
453 disk->d_priority = md->md_priority;
454 disk->d_flags = md->md_dflags;
455 if (md->md_provider[0] != '\0')
456 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
457 disk->d_sync.ds_consumer = NULL;
458 disk->d_sync.ds_offset = md->md_sync_offset;
459 disk->d_sync.ds_offset_done = md->md_sync_offset;
460 disk->d_genid = md->md_genid;
461 disk->d_sync.ds_syncid = md->md_syncid;
469 free(disk, M_MIRROR);
474 g_mirror_destroy_disk(struct g_mirror_disk *disk)
476 struct g_mirror_softc *sc;
478 g_topology_assert_not();
480 sx_assert(&sc->sc_lock, SX_XLOCKED);
482 LIST_REMOVE(disk, d_next);
483 g_mirror_event_cancel(disk);
484 if (sc->sc_hint == disk)
486 switch (disk->d_state) {
487 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
488 g_mirror_sync_stop(disk, 1);
490 case G_MIRROR_DISK_STATE_NEW:
491 case G_MIRROR_DISK_STATE_STALE:
492 case G_MIRROR_DISK_STATE_ACTIVE:
494 g_mirror_disconnect_consumer(sc, disk->d_consumer);
496 free(disk, M_MIRROR);
499 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
500 g_mirror_get_diskname(disk),
501 g_mirror_disk_state2str(disk->d_state)));
506 g_mirror_destroy_device(struct g_mirror_softc *sc)
508 struct g_mirror_disk *disk;
509 struct g_mirror_event *ep;
511 struct g_consumer *cp, *tmpcp;
513 g_topology_assert_not();
514 sx_assert(&sc->sc_lock, SX_XLOCKED);
517 if (sc->sc_provider != NULL)
518 g_mirror_destroy_provider(sc);
519 for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL;
520 disk = LIST_FIRST(&sc->sc_disks)) {
521 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
522 g_mirror_update_metadata(disk);
523 g_mirror_destroy_disk(disk);
525 while ((ep = g_mirror_event_get(sc)) != NULL) {
526 g_mirror_event_remove(sc, ep);
527 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
528 g_mirror_event_free(ep);
530 ep->e_error = ECANCELED;
531 ep->e_flags |= G_MIRROR_EVENT_DONE;
532 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep);
533 mtx_lock(&sc->sc_events_mtx);
535 mtx_unlock(&sc->sc_events_mtx);
538 callout_drain(&sc->sc_callout);
541 LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
542 g_mirror_disconnect_consumer(sc, cp);
544 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
545 G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
546 g_wither_geom(gp, ENXIO);
548 mtx_destroy(&sc->sc_queue_mtx);
549 mtx_destroy(&sc->sc_events_mtx);
550 sx_xunlock(&sc->sc_lock);
551 sx_destroy(&sc->sc_lock);
555 g_mirror_orphan(struct g_consumer *cp)
557 struct g_mirror_disk *disk;
564 disk->d_softc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
565 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
566 G_MIRROR_EVENT_DONTWAIT);
570 * Function should return the next active disk on the list.
571 * It is possible that it will be the same disk as given.
572 * If there are no active disks on list, NULL is returned.
574 static __inline struct g_mirror_disk *
575 g_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
577 struct g_mirror_disk *dp;
579 for (dp = LIST_NEXT(disk, d_next); dp != disk;
580 dp = LIST_NEXT(dp, d_next)) {
582 dp = LIST_FIRST(&sc->sc_disks);
583 if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE)
586 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
591 static struct g_mirror_disk *
592 g_mirror_get_disk(struct g_mirror_softc *sc)
594 struct g_mirror_disk *disk;
596 if (sc->sc_hint == NULL) {
597 sc->sc_hint = LIST_FIRST(&sc->sc_disks);
598 if (sc->sc_hint == NULL)
602 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) {
603 disk = g_mirror_find_next(sc, disk);
607 sc->sc_hint = g_mirror_find_next(sc, disk);
612 g_mirror_write_metadata(struct g_mirror_disk *disk,
613 struct g_mirror_metadata *md)
615 struct g_mirror_softc *sc;
616 struct g_consumer *cp;
617 off_t offset, length;
621 g_topology_assert_not();
623 sx_assert(&sc->sc_lock, SX_LOCKED);
625 cp = disk->d_consumer;
626 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
627 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
628 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
629 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
631 length = cp->provider->sectorsize;
632 offset = cp->provider->mediasize - length;
633 sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO);
635 mirror_metadata_encode(md, sector);
636 error = g_write_data(cp, offset, sector, length);
637 free(sector, M_MIRROR);
639 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
640 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
641 G_MIRROR_DEBUG(0, "Cannot write metadata on %s "
642 "(device=%s, error=%d).",
643 g_mirror_get_diskname(disk), sc->sc_name, error);
645 G_MIRROR_DEBUG(1, "Cannot write metadata on %s "
646 "(device=%s, error=%d).",
647 g_mirror_get_diskname(disk), sc->sc_name, error);
649 if (g_mirror_disconnect_on_failure &&
650 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) {
651 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
652 g_mirror_event_send(disk,
653 G_MIRROR_DISK_STATE_DISCONNECTED,
654 G_MIRROR_EVENT_DONTWAIT);
661 g_mirror_clear_metadata(struct g_mirror_disk *disk)
665 g_topology_assert_not();
666 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
668 error = g_mirror_write_metadata(disk, NULL);
670 G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
671 g_mirror_get_diskname(disk));
674 "Cannot clear metadata on disk %s (error=%d).",
675 g_mirror_get_diskname(disk), error);
681 g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
682 struct g_mirror_metadata *md)
685 strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
686 md->md_version = G_MIRROR_VERSION;
687 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
688 md->md_mid = sc->sc_id;
689 md->md_all = sc->sc_ndisks;
690 md->md_slice = sc->sc_slice;
691 md->md_balance = sc->sc_balance;
692 md->md_genid = sc->sc_genid;
693 md->md_mediasize = sc->sc_mediasize;
694 md->md_sectorsize = sc->sc_sectorsize;
695 md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
696 bzero(md->md_provider, sizeof(md->md_provider));
698 md->md_did = arc4random();
702 md->md_sync_offset = 0;
705 md->md_did = disk->d_id;
706 md->md_priority = disk->d_priority;
707 md->md_syncid = disk->d_sync.ds_syncid;
708 md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
709 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
710 md->md_sync_offset = disk->d_sync.ds_offset_done;
712 md->md_sync_offset = 0;
713 if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
714 strlcpy(md->md_provider,
715 disk->d_consumer->provider->name,
716 sizeof(md->md_provider));
718 md->md_provsize = disk->d_consumer->provider->mediasize;
723 g_mirror_update_metadata(struct g_mirror_disk *disk)
725 struct g_mirror_softc *sc;
726 struct g_mirror_metadata md;
729 g_topology_assert_not();
731 sx_assert(&sc->sc_lock, SX_LOCKED);
733 g_mirror_fill_metadata(sc, disk, &md);
734 error = g_mirror_write_metadata(disk, &md);
736 G_MIRROR_DEBUG(2, "Metadata on %s updated.",
737 g_mirror_get_diskname(disk));
740 "Cannot update metadata on disk %s (error=%d).",
741 g_mirror_get_diskname(disk), error);
746 g_mirror_bump_syncid(struct g_mirror_softc *sc)
748 struct g_mirror_disk *disk;
750 g_topology_assert_not();
751 sx_assert(&sc->sc_lock, SX_XLOCKED);
752 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
753 ("%s called with no active disks (device=%s).", __func__,
757 G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
759 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
760 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
761 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
762 disk->d_sync.ds_syncid = sc->sc_syncid;
763 g_mirror_update_metadata(disk);
769 g_mirror_bump_genid(struct g_mirror_softc *sc)
771 struct g_mirror_disk *disk;
773 g_topology_assert_not();
774 sx_assert(&sc->sc_lock, SX_XLOCKED);
775 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
776 ("%s called with no active disks (device=%s).", __func__,
780 G_MIRROR_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
782 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
783 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
784 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
785 disk->d_genid = sc->sc_genid;
786 g_mirror_update_metadata(disk);
792 g_mirror_idle(struct g_mirror_softc *sc, int acw)
794 struct g_mirror_disk *disk;
797 g_topology_assert_not();
798 sx_assert(&sc->sc_lock, SX_XLOCKED);
800 if (sc->sc_provider == NULL)
802 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
806 if (sc->sc_writes > 0)
808 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
809 timeout = g_mirror_idletime - (time_uptime - sc->sc_last_write);
814 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
815 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
817 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
818 g_mirror_get_diskname(disk), sc->sc_name);
819 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
820 g_mirror_update_metadata(disk);
826 g_mirror_unidle(struct g_mirror_softc *sc)
828 struct g_mirror_disk *disk;
830 g_topology_assert_not();
831 sx_assert(&sc->sc_lock, SX_XLOCKED);
833 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
836 sc->sc_last_write = time_uptime;
837 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
838 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
840 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
841 g_mirror_get_diskname(disk), sc->sc_name);
842 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
843 g_mirror_update_metadata(disk);
848 bintime_cmp(struct bintime *bt1, struct bintime *bt2)
851 if (bt1->sec < bt2->sec)
853 else if (bt1->sec > bt2->sec)
855 if (bt1->frac < bt2->frac)
857 else if (bt1->frac > bt2->frac)
863 g_mirror_done(struct bio *bp)
865 struct g_mirror_softc *sc;
867 sc = bp->bio_from->geom->softc;
868 bp->bio_cflags = G_MIRROR_BIO_FLAG_REGULAR;
869 mtx_lock(&sc->sc_queue_mtx);
870 bioq_disksort(&sc->sc_queue, bp);
871 mtx_unlock(&sc->sc_queue_mtx);
876 g_mirror_regular_request(struct bio *bp)
878 struct g_mirror_softc *sc;
879 struct g_mirror_disk *disk;
882 g_topology_assert_not();
884 pbp = bp->bio_parent;
885 sc = pbp->bio_to->geom->softc;
886 bp->bio_from->index--;
887 if (bp->bio_cmd == BIO_WRITE)
889 disk = bp->bio_from->private;
892 g_mirror_kill_consumer(sc, bp->bio_from);
897 KASSERT(pbp->bio_inbed <= pbp->bio_children,
898 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
900 if (bp->bio_error == 0 && pbp->bio_error == 0) {
901 G_MIRROR_LOGREQ(3, bp, "Request delivered.");
903 if (pbp->bio_children == pbp->bio_inbed) {
904 G_MIRROR_LOGREQ(3, pbp, "Request delivered.");
905 pbp->bio_completed = pbp->bio_length;
906 if (pbp->bio_cmd == BIO_WRITE) {
907 bioq_remove(&sc->sc_inflight, pbp);
908 /* Release delayed sync requests if possible. */
909 g_mirror_sync_release(sc);
911 g_io_deliver(pbp, pbp->bio_error);
914 } else if (bp->bio_error != 0) {
915 if (pbp->bio_error == 0)
916 pbp->bio_error = bp->bio_error;
918 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
919 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
920 G_MIRROR_LOGREQ(0, bp,
921 "Request failed (error=%d).",
924 G_MIRROR_LOGREQ(1, bp,
925 "Request failed (error=%d).",
928 if (g_mirror_disconnect_on_failure &&
929 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1)
931 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
932 g_mirror_event_send(disk,
933 G_MIRROR_DISK_STATE_DISCONNECTED,
934 G_MIRROR_EVENT_DONTWAIT);
937 switch (pbp->bio_cmd) {
947 switch (pbp->bio_cmd) {
949 if (pbp->bio_inbed < pbp->bio_children)
951 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1)
952 g_io_deliver(pbp, pbp->bio_error);
955 mtx_lock(&sc->sc_queue_mtx);
956 bioq_disksort(&sc->sc_queue, pbp);
957 mtx_unlock(&sc->sc_queue_mtx);
958 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
964 if (pbp->bio_children == 0) {
966 * All requests failed.
968 } else if (pbp->bio_inbed < pbp->bio_children) {
971 } else if (pbp->bio_children == pbp->bio_inbed) {
972 /* Some requests succeeded. */
974 pbp->bio_completed = pbp->bio_length;
976 bioq_remove(&sc->sc_inflight, pbp);
977 /* Release delayed sync requests if possible. */
978 g_mirror_sync_release(sc);
979 g_io_deliver(pbp, pbp->bio_error);
982 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
988 g_mirror_sync_done(struct bio *bp)
990 struct g_mirror_softc *sc;
992 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
993 sc = bp->bio_from->geom->softc;
994 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
995 mtx_lock(&sc->sc_queue_mtx);
996 bioq_disksort(&sc->sc_queue, bp);
997 mtx_unlock(&sc->sc_queue_mtx);
1002 g_mirror_kernel_dump(struct bio *bp)
1004 struct g_mirror_softc *sc;
1005 struct g_mirror_disk *disk;
1007 struct g_kerneldump *gkd;
1010 * We configure dumping to the first component, because this component
1011 * will be used for reading with 'prefer' balance algorithm.
1012 * If the component with the higest priority is currently disconnected
1013 * we will not be able to read the dump after the reboot if it will be
1014 * connected and synchronized later. Can we do something better?
1016 sc = bp->bio_to->geom->softc;
1017 disk = LIST_FIRST(&sc->sc_disks);
1019 gkd = (struct g_kerneldump *)bp->bio_data;
1020 if (gkd->length > bp->bio_to->mediasize)
1021 gkd->length = bp->bio_to->mediasize;
1022 cbp = g_clone_bio(bp);
1024 g_io_deliver(bp, ENOMEM);
1027 cbp->bio_done = g_std_done;
1028 g_io_request(cbp, disk->d_consumer);
1029 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1030 g_mirror_get_diskname(disk));
1034 g_mirror_flush(struct g_mirror_softc *sc, struct bio *bp)
1036 struct bio_queue_head queue;
1037 struct g_mirror_disk *disk;
1038 struct g_consumer *cp;
1042 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1043 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1045 cbp = g_clone_bio(bp);
1047 for (cbp = bioq_first(&queue); cbp != NULL;
1048 cbp = bioq_first(&queue)) {
1049 bioq_remove(&queue, cbp);
1052 if (bp->bio_error == 0)
1053 bp->bio_error = ENOMEM;
1054 g_io_deliver(bp, bp->bio_error);
1057 bioq_insert_tail(&queue, cbp);
1058 cbp->bio_done = g_std_done;
1059 cbp->bio_caller1 = disk;
1060 cbp->bio_to = disk->d_consumer->provider;
1062 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1063 bioq_remove(&queue, cbp);
1064 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1065 disk = cbp->bio_caller1;
1066 cbp->bio_caller1 = NULL;
1067 cp = disk->d_consumer;
1068 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1069 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1070 cp->acr, cp->acw, cp->ace));
1071 g_io_request(cbp, disk->d_consumer);
1076 g_mirror_start(struct bio *bp)
1078 struct g_mirror_softc *sc;
1080 sc = bp->bio_to->geom->softc;
1082 * If sc == NULL or there are no valid disks, provider's error
1083 * should be set and g_mirror_start() should not be called at all.
1085 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1086 ("Provider's error should be set (error=%d)(mirror=%s).",
1087 bp->bio_to->error, bp->bio_to->name));
1088 G_MIRROR_LOGREQ(3, bp, "Request received.");
1090 switch (bp->bio_cmd) {
1096 g_mirror_flush(sc, bp);
1099 if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1100 g_mirror_kernel_dump(bp);
1105 g_io_deliver(bp, EOPNOTSUPP);
1108 mtx_lock(&sc->sc_queue_mtx);
1109 bioq_disksort(&sc->sc_queue, bp);
1110 mtx_unlock(&sc->sc_queue_mtx);
1111 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1116 * Return TRUE if the given request is colliding with a in-progress
1117 * synchronization request.
1120 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1122 struct g_mirror_disk *disk;
1124 off_t rstart, rend, sstart, send;
1127 if (sc->sc_sync.ds_ndisks == 0)
1129 rstart = bp->bio_offset;
1130 rend = bp->bio_offset + bp->bio_length;
1131 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1132 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1134 for (i = 0; i < g_mirror_syncreqs; i++) {
1135 sbp = disk->d_sync.ds_bios[i];
1138 sstart = sbp->bio_offset;
1139 send = sbp->bio_offset + sbp->bio_length;
1140 if (rend > sstart && rstart < send)
1148 * Return TRUE if the given sync request is colliding with a in-progress regular
1152 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1154 off_t rstart, rend, sstart, send;
1157 if (sc->sc_sync.ds_ndisks == 0)
1159 sstart = sbp->bio_offset;
1160 send = sbp->bio_offset + sbp->bio_length;
1161 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1162 rstart = bp->bio_offset;
1163 rend = bp->bio_offset + bp->bio_length;
1164 if (rend > sstart && rstart < send)
1171 * Puts request onto delayed queue.
1174 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1177 G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1178 bioq_insert_head(&sc->sc_regular_delayed, bp);
1182 * Puts synchronization request onto delayed queue.
1185 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1188 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1189 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1193 * Releases delayed regular requests which don't collide anymore with sync
1197 g_mirror_regular_release(struct g_mirror_softc *sc)
1199 struct bio *bp, *bp2;
1201 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1202 if (g_mirror_sync_collision(sc, bp))
1204 bioq_remove(&sc->sc_regular_delayed, bp);
1205 G_MIRROR_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1206 mtx_lock(&sc->sc_queue_mtx);
1207 bioq_insert_head(&sc->sc_queue, bp);
1210 * wakeup() is not needed, because this function is called from
1211 * the worker thread.
1213 wakeup(&sc->sc_queue);
1215 mtx_unlock(&sc->sc_queue_mtx);
1220 * Releases delayed sync requests which don't collide anymore with regular
1224 g_mirror_sync_release(struct g_mirror_softc *sc)
1226 struct bio *bp, *bp2;
1228 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1229 if (g_mirror_regular_collision(sc, bp))
1231 bioq_remove(&sc->sc_sync_delayed, bp);
1232 G_MIRROR_LOGREQ(2, bp,
1233 "Releasing delayed synchronization request.");
1234 g_io_request(bp, bp->bio_from);
1239 * Handle synchronization requests.
1240 * Every synchronization request is two-steps process: first, READ request is
1241 * send to active provider and then WRITE request (with read data) to the provider
1242 * beeing synchronized. When WRITE is finished, new synchronization request is
1246 g_mirror_sync_request(struct bio *bp)
1248 struct g_mirror_softc *sc;
1249 struct g_mirror_disk *disk;
1251 bp->bio_from->index--;
1252 sc = bp->bio_from->geom->softc;
1253 disk = bp->bio_from->private;
1255 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1257 g_mirror_kill_consumer(sc, bp->bio_from);
1258 g_topology_unlock();
1259 free(bp->bio_data, M_MIRROR);
1261 sx_xlock(&sc->sc_lock);
1266 * Synchronization request.
1268 switch (bp->bio_cmd) {
1271 struct g_consumer *cp;
1273 if (bp->bio_error != 0) {
1274 G_MIRROR_LOGREQ(0, bp,
1275 "Synchronization request failed (error=%d).",
1280 G_MIRROR_LOGREQ(3, bp,
1281 "Synchronization request half-finished.");
1282 bp->bio_cmd = BIO_WRITE;
1284 cp = disk->d_consumer;
1285 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1286 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1287 cp->acr, cp->acw, cp->ace));
1289 g_io_request(bp, cp);
1294 struct g_mirror_disk_sync *sync;
1299 if (bp->bio_error != 0) {
1300 G_MIRROR_LOGREQ(0, bp,
1301 "Synchronization request failed (error=%d).",
1304 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1305 g_mirror_event_send(disk,
1306 G_MIRROR_DISK_STATE_DISCONNECTED,
1307 G_MIRROR_EVENT_DONTWAIT);
1310 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1311 sync = &disk->d_sync;
1312 if (sync->ds_offset == sc->sc_mediasize ||
1313 sync->ds_consumer == NULL ||
1314 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1315 /* Don't send more synchronization requests. */
1316 sync->ds_inflight--;
1317 if (sync->ds_bios != NULL) {
1318 i = (int)(uintptr_t)bp->bio_caller1;
1319 sync->ds_bios[i] = NULL;
1321 free(bp->bio_data, M_MIRROR);
1323 if (sync->ds_inflight > 0)
1325 if (sync->ds_consumer == NULL ||
1326 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1329 /* Disk up-to-date, activate it. */
1330 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1331 G_MIRROR_EVENT_DONTWAIT);
1335 /* Send next synchronization request. */
1336 data = bp->bio_data;
1337 bzero(bp, sizeof(*bp));
1338 bp->bio_cmd = BIO_READ;
1339 bp->bio_offset = sync->ds_offset;
1340 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1341 sync->ds_offset += bp->bio_length;
1342 bp->bio_done = g_mirror_sync_done;
1343 bp->bio_data = data;
1344 bp->bio_from = sync->ds_consumer;
1345 bp->bio_to = sc->sc_provider;
1346 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1347 sync->ds_consumer->index++;
1349 * Delay the request if it is colliding with a regular request.
1351 if (g_mirror_regular_collision(sc, bp))
1352 g_mirror_sync_delay(sc, bp);
1354 g_io_request(bp, sync->ds_consumer);
1356 /* Release delayed requests if possible. */
1357 g_mirror_regular_release(sc);
1359 /* Find the smallest offset */
1360 offset = sc->sc_mediasize;
1361 for (i = 0; i < g_mirror_syncreqs; i++) {
1362 bp = sync->ds_bios[i];
1363 if (bp->bio_offset < offset)
1364 offset = bp->bio_offset;
1366 if (sync->ds_offset_done + (MAXPHYS * 100) < offset) {
1367 /* Update offset_done on every 100 blocks. */
1368 sync->ds_offset_done = offset;
1369 g_mirror_update_metadata(disk);
1374 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1375 bp->bio_cmd, sc->sc_name));
1381 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1383 struct g_mirror_disk *disk;
1384 struct g_consumer *cp;
1387 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1388 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1392 if (bp->bio_error == 0)
1393 bp->bio_error = ENXIO;
1394 g_io_deliver(bp, bp->bio_error);
1397 cbp = g_clone_bio(bp);
1399 if (bp->bio_error == 0)
1400 bp->bio_error = ENOMEM;
1401 g_io_deliver(bp, bp->bio_error);
1405 * Fill in the component buf structure.
1407 cp = disk->d_consumer;
1408 cbp->bio_done = g_mirror_done;
1409 cbp->bio_to = cp->provider;
1410 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1411 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1412 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1415 g_io_request(cbp, cp);
1419 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1421 struct g_mirror_disk *disk;
1422 struct g_consumer *cp;
1425 disk = g_mirror_get_disk(sc);
1427 if (bp->bio_error == 0)
1428 bp->bio_error = ENXIO;
1429 g_io_deliver(bp, bp->bio_error);
1432 cbp = g_clone_bio(bp);
1434 if (bp->bio_error == 0)
1435 bp->bio_error = ENOMEM;
1436 g_io_deliver(bp, bp->bio_error);
1440 * Fill in the component buf structure.
1442 cp = disk->d_consumer;
1443 cbp->bio_done = g_mirror_done;
1444 cbp->bio_to = cp->provider;
1445 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1446 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1447 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1450 g_io_request(cbp, cp);
1453 #define TRACK_SIZE (1 * 1024 * 1024)
1454 #define LOAD_SCALE 256
1455 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
1458 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1460 struct g_mirror_disk *disk, *dp;
1461 struct g_consumer *cp;
1465 /* Find a disk with the smallest load. */
1468 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1469 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1472 /* If disk head is precisely in position - highly prefer it. */
1473 if (dp->d_last_offset == bp->bio_offset)
1474 prio -= 2 * LOAD_SCALE;
1476 /* If disk head is close to position - prefer it. */
1477 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1478 prio -= 1 * LOAD_SCALE;
1484 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1485 cbp = g_clone_bio(bp);
1487 if (bp->bio_error == 0)
1488 bp->bio_error = ENOMEM;
1489 g_io_deliver(bp, bp->bio_error);
1493 * Fill in the component buf structure.
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,
1503 /* Remember last head position */
1504 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1506 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1507 dp->load = (dp->d_consumer->index * LOAD_SCALE +
1510 g_io_request(cbp, cp);
1514 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1516 struct bio_queue_head queue;
1517 struct g_mirror_disk *disk;
1518 struct g_consumer *cp;
1520 off_t left, mod, offset, slice;
1524 if (bp->bio_length <= sc->sc_slice) {
1525 g_mirror_request_round_robin(sc, bp);
1528 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1529 slice = bp->bio_length / ndisks;
1530 mod = slice % sc->sc_provider->sectorsize;
1532 slice += sc->sc_provider->sectorsize - mod;
1534 * Allocate all bios before sending any request, so we can
1535 * return ENOMEM in nice and clean way.
1537 left = bp->bio_length;
1538 offset = bp->bio_offset;
1539 data = bp->bio_data;
1541 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1542 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1544 cbp = g_clone_bio(bp);
1546 for (cbp = bioq_first(&queue); cbp != NULL;
1547 cbp = bioq_first(&queue)) {
1548 bioq_remove(&queue, cbp);
1551 if (bp->bio_error == 0)
1552 bp->bio_error = ENOMEM;
1553 g_io_deliver(bp, bp->bio_error);
1556 bioq_insert_tail(&queue, cbp);
1557 cbp->bio_done = g_mirror_done;
1558 cbp->bio_caller1 = disk;
1559 cbp->bio_to = disk->d_consumer->provider;
1560 cbp->bio_offset = offset;
1561 cbp->bio_data = data;
1562 cbp->bio_length = MIN(left, slice);
1563 left -= cbp->bio_length;
1566 offset += cbp->bio_length;
1567 data += cbp->bio_length;
1569 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1570 bioq_remove(&queue, cbp);
1571 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1572 disk = cbp->bio_caller1;
1573 cbp->bio_caller1 = NULL;
1574 cp = disk->d_consumer;
1575 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1576 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1577 cp->acr, cp->acw, cp->ace));
1578 disk->d_consumer->index++;
1579 g_io_request(cbp, disk->d_consumer);
1584 g_mirror_register_request(struct bio *bp)
1586 struct g_mirror_softc *sc;
1588 sc = bp->bio_to->geom->softc;
1589 switch (bp->bio_cmd) {
1591 switch (sc->sc_balance) {
1592 case G_MIRROR_BALANCE_LOAD:
1593 g_mirror_request_load(sc, bp);
1595 case G_MIRROR_BALANCE_PREFER:
1596 g_mirror_request_prefer(sc, bp);
1598 case G_MIRROR_BALANCE_ROUND_ROBIN:
1599 g_mirror_request_round_robin(sc, bp);
1601 case G_MIRROR_BALANCE_SPLIT:
1602 g_mirror_request_split(sc, bp);
1609 struct g_mirror_disk *disk;
1610 struct g_mirror_disk_sync *sync;
1611 struct bio_queue_head queue;
1612 struct g_consumer *cp;
1616 * Delay the request if it is colliding with a synchronization
1619 if (g_mirror_sync_collision(sc, bp)) {
1620 g_mirror_regular_delay(sc, bp);
1625 g_mirror_unidle(sc);
1627 sc->sc_last_write = time_uptime;
1630 * Allocate all bios before sending any request, so we can
1631 * return ENOMEM in nice and clean way.
1634 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1635 sync = &disk->d_sync;
1636 switch (disk->d_state) {
1637 case G_MIRROR_DISK_STATE_ACTIVE:
1639 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1640 if (bp->bio_offset >= sync->ds_offset)
1646 cbp = g_clone_bio(bp);
1648 for (cbp = bioq_first(&queue); cbp != NULL;
1649 cbp = bioq_first(&queue)) {
1650 bioq_remove(&queue, cbp);
1653 if (bp->bio_error == 0)
1654 bp->bio_error = ENOMEM;
1655 g_io_deliver(bp, bp->bio_error);
1658 bioq_insert_tail(&queue, cbp);
1659 cbp->bio_done = g_mirror_done;
1660 cp = disk->d_consumer;
1661 cbp->bio_caller1 = cp;
1662 cbp->bio_to = cp->provider;
1663 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1664 ("Consumer %s not opened (r%dw%de%d).",
1665 cp->provider->name, cp->acr, cp->acw, cp->ace));
1667 for (cbp = bioq_first(&queue); cbp != NULL;
1668 cbp = bioq_first(&queue)) {
1669 bioq_remove(&queue, cbp);
1670 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1671 cp = cbp->bio_caller1;
1672 cbp->bio_caller1 = NULL;
1675 g_io_request(cbp, cp);
1678 * Put request onto inflight queue, so we can check if new
1679 * synchronization requests don't collide with it.
1681 bioq_insert_tail(&sc->sc_inflight, bp);
1683 * Bump syncid on first write.
1685 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1686 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1687 g_mirror_bump_syncid(sc);
1692 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1693 bp->bio_cmd, sc->sc_name));
1699 g_mirror_can_destroy(struct g_mirror_softc *sc)
1702 struct g_consumer *cp;
1704 g_topology_assert();
1706 if (gp->softc == NULL)
1708 LIST_FOREACH(cp, &gp->consumer, consumer) {
1709 if (g_mirror_is_busy(sc, cp))
1712 gp = sc->sc_sync.ds_geom;
1713 LIST_FOREACH(cp, &gp->consumer, consumer) {
1714 if (g_mirror_is_busy(sc, cp))
1717 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1723 g_mirror_try_destroy(struct g_mirror_softc *sc)
1726 if (sc->sc_rootmount != NULL) {
1727 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1729 root_mount_rel(sc->sc_rootmount);
1730 sc->sc_rootmount = NULL;
1733 if (!g_mirror_can_destroy(sc)) {
1734 g_topology_unlock();
1737 sc->sc_geom->softc = NULL;
1738 sc->sc_sync.ds_geom->softc = NULL;
1739 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WAIT) != 0) {
1740 g_topology_unlock();
1741 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1743 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
1744 sx_xunlock(&sc->sc_lock);
1745 wakeup(&sc->sc_worker);
1746 sc->sc_worker = NULL;
1748 g_topology_unlock();
1749 g_mirror_destroy_device(sc);
1759 g_mirror_worker(void *arg)
1761 struct g_mirror_softc *sc;
1762 struct g_mirror_event *ep;
1767 thread_lock(curthread);
1768 sched_prio(curthread, PRIBIO);
1769 thread_unlock(curthread);
1771 sx_xlock(&sc->sc_lock);
1773 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1775 * First take a look at events.
1776 * This is important to handle events before any I/O requests.
1778 ep = g_mirror_event_get(sc);
1780 g_mirror_event_remove(sc, ep);
1781 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1782 /* Update only device status. */
1784 "Running event for device %s.",
1787 g_mirror_update_device(sc, 1);
1789 /* Update disk status. */
1790 G_MIRROR_DEBUG(3, "Running event for disk %s.",
1791 g_mirror_get_diskname(ep->e_disk));
1792 ep->e_error = g_mirror_update_disk(ep->e_disk,
1794 if (ep->e_error == 0)
1795 g_mirror_update_device(sc, 0);
1797 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1798 KASSERT(ep->e_error == 0,
1799 ("Error cannot be handled."));
1800 g_mirror_event_free(ep);
1802 ep->e_flags |= G_MIRROR_EVENT_DONE;
1803 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1805 mtx_lock(&sc->sc_events_mtx);
1807 mtx_unlock(&sc->sc_events_mtx);
1810 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1811 if (g_mirror_try_destroy(sc)) {
1812 curthread->td_pflags &= ~TDP_GEOM;
1813 G_MIRROR_DEBUG(1, "Thread exiting.");
1817 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1821 * Check if we can mark array as CLEAN and if we can't take
1822 * how much seconds should we wait.
1824 timeout = g_mirror_idle(sc, -1);
1828 /* Get first request from the queue. */
1829 mtx_lock(&sc->sc_queue_mtx);
1830 bp = bioq_first(&sc->sc_queue);
1833 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1834 mtx_unlock(&sc->sc_queue_mtx);
1835 if (g_mirror_try_destroy(sc)) {
1836 curthread->td_pflags &= ~TDP_GEOM;
1837 G_MIRROR_DEBUG(1, "Thread exiting.");
1840 mtx_lock(&sc->sc_queue_mtx);
1842 sx_xunlock(&sc->sc_lock);
1844 * XXX: We can miss an event here, because an event
1845 * can be added without sx-device-lock and without
1846 * mtx-queue-lock. Maybe I should just stop using
1847 * dedicated mutex for events synchronization and
1848 * stick with the queue lock?
1849 * The event will hang here until next I/O request
1850 * or next event is received.
1852 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
1854 sx_xlock(&sc->sc_lock);
1855 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
1858 bioq_remove(&sc->sc_queue, bp);
1859 mtx_unlock(&sc->sc_queue_mtx);
1861 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
1862 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
1863 g_mirror_sync_request(bp); /* READ */
1864 } else if (bp->bio_to != sc->sc_provider) {
1865 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
1866 g_mirror_regular_request(bp);
1867 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
1868 g_mirror_sync_request(bp); /* WRITE */
1871 ("Invalid request cflags=0x%hhx to=%s.",
1872 bp->bio_cflags, bp->bio_to->name));
1875 g_mirror_register_request(bp);
1877 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
1882 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
1885 sx_assert(&sc->sc_lock, SX_LOCKED);
1887 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
1889 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
1890 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
1891 g_mirror_get_diskname(disk), sc->sc_name);
1892 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1893 } else if (sc->sc_idle &&
1894 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
1895 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
1896 g_mirror_get_diskname(disk), sc->sc_name);
1897 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
1902 g_mirror_sync_start(struct g_mirror_disk *disk)
1904 struct g_mirror_softc *sc;
1905 struct g_consumer *cp;
1909 g_topology_assert_not();
1911 sx_assert(&sc->sc_lock, SX_LOCKED);
1913 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1914 ("Disk %s is not marked for synchronization.",
1915 g_mirror_get_diskname(disk)));
1916 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1917 ("Device not in RUNNING state (%s, %u).", sc->sc_name,
1920 sx_xunlock(&sc->sc_lock);
1922 cp = g_new_consumer(sc->sc_sync.ds_geom);
1923 error = g_attach(cp, sc->sc_provider);
1925 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
1926 error = g_access(cp, 1, 0, 0);
1927 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
1928 g_topology_unlock();
1929 sx_xlock(&sc->sc_lock);
1931 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
1932 g_mirror_get_diskname(disk));
1933 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
1934 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1935 KASSERT(disk->d_sync.ds_consumer == NULL,
1936 ("Sync consumer already exists (device=%s, disk=%s).",
1937 sc->sc_name, g_mirror_get_diskname(disk)));
1939 disk->d_sync.ds_consumer = cp;
1940 disk->d_sync.ds_consumer->private = disk;
1941 disk->d_sync.ds_consumer->index = 0;
1944 * Allocate memory for synchronization bios and initialize them.
1946 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
1947 M_MIRROR, M_WAITOK);
1948 for (i = 0; i < g_mirror_syncreqs; i++) {
1950 disk->d_sync.ds_bios[i] = bp;
1951 bp->bio_parent = NULL;
1952 bp->bio_cmd = BIO_READ;
1953 bp->bio_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK);
1955 bp->bio_offset = disk->d_sync.ds_offset;
1956 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1957 disk->d_sync.ds_offset += bp->bio_length;
1958 bp->bio_done = g_mirror_sync_done;
1959 bp->bio_from = disk->d_sync.ds_consumer;
1960 bp->bio_to = sc->sc_provider;
1961 bp->bio_caller1 = (void *)(uintptr_t)i;
1964 /* Increase the number of disks in SYNCHRONIZING state. */
1965 sc->sc_sync.ds_ndisks++;
1966 /* Set the number of in-flight synchronization requests. */
1967 disk->d_sync.ds_inflight = g_mirror_syncreqs;
1970 * Fire off first synchronization requests.
1972 for (i = 0; i < g_mirror_syncreqs; i++) {
1973 bp = disk->d_sync.ds_bios[i];
1974 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1975 disk->d_sync.ds_consumer->index++;
1977 * Delay the request if it is colliding with a regular request.
1979 if (g_mirror_regular_collision(sc, bp))
1980 g_mirror_sync_delay(sc, bp);
1982 g_io_request(bp, disk->d_sync.ds_consumer);
1987 * Stop synchronization process.
1988 * type: 0 - synchronization finished
1989 * 1 - synchronization stopped
1992 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
1994 struct g_mirror_softc *sc;
1995 struct g_consumer *cp;
1997 g_topology_assert_not();
1999 sx_assert(&sc->sc_lock, SX_LOCKED);
2001 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2002 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2003 g_mirror_disk_state2str(disk->d_state)));
2004 if (disk->d_sync.ds_consumer == NULL)
2008 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2009 sc->sc_name, g_mirror_get_diskname(disk));
2010 } else /* if (type == 1) */ {
2011 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2012 sc->sc_name, g_mirror_get_diskname(disk));
2014 free(disk->d_sync.ds_bios, M_MIRROR);
2015 disk->d_sync.ds_bios = NULL;
2016 cp = disk->d_sync.ds_consumer;
2017 disk->d_sync.ds_consumer = NULL;
2018 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2019 sc->sc_sync.ds_ndisks--;
2020 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2022 g_mirror_kill_consumer(sc, cp);
2023 g_topology_unlock();
2024 sx_xlock(&sc->sc_lock);
2028 g_mirror_launch_provider(struct g_mirror_softc *sc)
2030 struct g_mirror_disk *disk;
2031 struct g_provider *pp;
2033 sx_assert(&sc->sc_lock, SX_LOCKED);
2036 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2037 pp->mediasize = sc->sc_mediasize;
2038 pp->sectorsize = sc->sc_sectorsize;
2040 pp->stripeoffset = 0;
2041 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2042 if (disk->d_consumer && disk->d_consumer->provider &&
2043 disk->d_consumer->provider->stripesize > pp->stripesize) {
2044 pp->stripesize = disk->d_consumer->provider->stripesize;
2045 pp->stripeoffset = disk->d_consumer->provider->stripeoffset;
2048 sc->sc_provider = pp;
2049 g_error_provider(pp, 0);
2050 g_topology_unlock();
2051 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2052 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2053 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2054 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2055 g_mirror_sync_start(disk);
2060 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2062 struct g_mirror_disk *disk;
2065 g_topology_assert_not();
2066 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2070 g_error_provider(sc->sc_provider, ENXIO);
2071 mtx_lock(&sc->sc_queue_mtx);
2072 while ((bp = bioq_first(&sc->sc_queue)) != NULL) {
2073 bioq_remove(&sc->sc_queue, bp);
2074 g_io_deliver(bp, ENXIO);
2076 mtx_unlock(&sc->sc_queue_mtx);
2077 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2078 sc->sc_provider->name);
2079 sc->sc_provider->flags |= G_PF_WITHER;
2080 g_orphan_provider(sc->sc_provider, ENXIO);
2081 g_topology_unlock();
2082 sc->sc_provider = NULL;
2083 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2084 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2085 g_mirror_sync_stop(disk, 1);
2090 g_mirror_go(void *arg)
2092 struct g_mirror_softc *sc;
2095 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2096 g_mirror_event_send(sc, 0,
2097 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2101 g_mirror_determine_state(struct g_mirror_disk *disk)
2103 struct g_mirror_softc *sc;
2107 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2108 if ((disk->d_flags &
2109 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2110 /* Disk does not need synchronization. */
2111 state = G_MIRROR_DISK_STATE_ACTIVE;
2114 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2116 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2118 * We can start synchronization from
2119 * the stored offset.
2121 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2123 state = G_MIRROR_DISK_STATE_STALE;
2126 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2128 * Reset all synchronization data for this disk,
2129 * because if it even was synchronized, it was
2130 * synchronized to disks with different syncid.
2132 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2133 disk->d_sync.ds_offset = 0;
2134 disk->d_sync.ds_offset_done = 0;
2135 disk->d_sync.ds_syncid = sc->sc_syncid;
2136 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2137 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2138 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2140 state = G_MIRROR_DISK_STATE_STALE;
2142 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2144 * Not good, NOT GOOD!
2145 * It means that mirror was started on stale disks
2146 * and more fresh disk just arrive.
2147 * If there were writes, mirror is broken, sorry.
2148 * I think the best choice here is don't touch
2149 * this disk and inform the user loudly.
2151 G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2152 "disk (%s) arrives!! It will not be connected to the "
2153 "running device.", sc->sc_name,
2154 g_mirror_get_diskname(disk));
2155 g_mirror_destroy_disk(disk);
2156 state = G_MIRROR_DISK_STATE_NONE;
2157 /* Return immediately, because disk was destroyed. */
2160 G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2161 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2166 * Update device state.
2169 g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force)
2171 struct g_mirror_disk *disk;
2174 sx_assert(&sc->sc_lock, SX_XLOCKED);
2176 switch (sc->sc_state) {
2177 case G_MIRROR_DEVICE_STATE_STARTING:
2179 struct g_mirror_disk *pdisk, *tdisk;
2180 u_int dirty, ndisks, genid, syncid;
2182 KASSERT(sc->sc_provider == NULL,
2183 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2185 * Are we ready? We are, if all disks are connected or
2186 * if we have any disks and 'force' is true.
2188 ndisks = g_mirror_ndisks(sc, -1);
2189 if (sc->sc_ndisks == ndisks || (force && ndisks > 0)) {
2191 } else if (ndisks == 0) {
2193 * Disks went down in starting phase, so destroy
2196 callout_drain(&sc->sc_callout);
2197 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2198 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2200 root_mount_rel(sc->sc_rootmount);
2201 sc->sc_rootmount = NULL;
2208 * Activate all disks with the biggest syncid.
2212 * If 'force' is true, we have been called due to
2213 * timeout, so don't bother canceling timeout.
2216 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2217 if ((disk->d_flags &
2218 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2223 /* No valid disks found, destroy device. */
2224 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2225 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2226 __LINE__, sc->sc_rootmount);
2227 root_mount_rel(sc->sc_rootmount);
2228 sc->sc_rootmount = NULL;
2232 /* Cancel timeout. */
2233 callout_drain(&sc->sc_callout);
2237 * Find the biggest genid.
2240 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2241 if (disk->d_genid > genid)
2242 genid = disk->d_genid;
2244 sc->sc_genid = genid;
2246 * Remove all disks without the biggest genid.
2248 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2249 if (disk->d_genid < genid) {
2251 "Component %s (device %s) broken, skipping.",
2252 g_mirror_get_diskname(disk), sc->sc_name);
2253 g_mirror_destroy_disk(disk);
2258 * Find the biggest syncid.
2261 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2262 if (disk->d_sync.ds_syncid > syncid)
2263 syncid = disk->d_sync.ds_syncid;
2267 * Here we need to look for dirty disks and if all disks
2268 * with the biggest syncid are dirty, we have to choose
2269 * one with the biggest priority and rebuild the rest.
2272 * Find the number of dirty disks with the biggest syncid.
2273 * Find the number of disks with the biggest syncid.
2274 * While here, find a disk with the biggest priority.
2278 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2279 if (disk->d_sync.ds_syncid != syncid)
2281 if ((disk->d_flags &
2282 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2286 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2288 if (pdisk == NULL ||
2289 pdisk->d_priority < disk->d_priority) {
2295 /* No dirty disks at all, great. */
2296 } else if (dirty == ndisks) {
2298 * Force synchronization for all dirty disks except one
2299 * with the biggest priority.
2301 KASSERT(pdisk != NULL, ("pdisk == NULL"));
2302 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2303 "master disk for synchronization.",
2304 g_mirror_get_diskname(pdisk), sc->sc_name);
2305 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2306 if (disk->d_sync.ds_syncid != syncid)
2308 if ((disk->d_flags &
2309 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2312 KASSERT((disk->d_flags &
2313 G_MIRROR_DISK_FLAG_DIRTY) != 0,
2314 ("Disk %s isn't marked as dirty.",
2315 g_mirror_get_diskname(disk)));
2316 /* Skip the disk with the biggest priority. */
2319 disk->d_sync.ds_syncid = 0;
2321 } else if (dirty < ndisks) {
2323 * Force synchronization for all dirty disks.
2324 * We have some non-dirty disks.
2326 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2327 if (disk->d_sync.ds_syncid != syncid)
2329 if ((disk->d_flags &
2330 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2333 if ((disk->d_flags &
2334 G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2337 disk->d_sync.ds_syncid = 0;
2343 sc->sc_syncid = syncid;
2345 /* Remember to bump syncid on first write. */
2346 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2348 state = G_MIRROR_DEVICE_STATE_RUNNING;
2349 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2350 sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2351 g_mirror_device_state2str(state));
2352 sc->sc_state = state;
2353 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2354 state = g_mirror_determine_state(disk);
2355 g_mirror_event_send(disk, state,
2356 G_MIRROR_EVENT_DONTWAIT);
2357 if (state == G_MIRROR_DISK_STATE_STALE)
2358 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2362 case G_MIRROR_DEVICE_STATE_RUNNING:
2363 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2364 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2366 * No active disks or no disks at all,
2367 * so destroy device.
2369 if (sc->sc_provider != NULL)
2370 g_mirror_destroy_provider(sc);
2371 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2373 } else if (g_mirror_ndisks(sc,
2374 G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2375 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2377 * We have active disks, launch provider if it doesn't
2380 if (sc->sc_provider == NULL)
2381 g_mirror_launch_provider(sc);
2382 if (sc->sc_rootmount != NULL) {
2383 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2384 __LINE__, sc->sc_rootmount);
2385 root_mount_rel(sc->sc_rootmount);
2386 sc->sc_rootmount = NULL;
2390 * Genid should be bumped immediately, so do it here.
2392 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2393 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2394 g_mirror_bump_genid(sc);
2398 KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2399 sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2405 * Update disk state and device state if needed.
2407 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \
2408 "Disk %s state changed from %s to %s (device %s).", \
2409 g_mirror_get_diskname(disk), \
2410 g_mirror_disk_state2str(disk->d_state), \
2411 g_mirror_disk_state2str(state), sc->sc_name)
2413 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2415 struct g_mirror_softc *sc;
2418 sx_assert(&sc->sc_lock, SX_XLOCKED);
2421 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2422 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2423 g_mirror_disk_state2str(state));
2425 case G_MIRROR_DISK_STATE_NEW:
2427 * Possible scenarios:
2428 * 1. New disk arrive.
2430 /* Previous state should be NONE. */
2431 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2432 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2433 g_mirror_disk_state2str(disk->d_state)));
2434 DISK_STATE_CHANGED();
2436 disk->d_state = state;
2437 if (LIST_EMPTY(&sc->sc_disks))
2438 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2440 struct g_mirror_disk *dp;
2442 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2443 if (disk->d_priority >= dp->d_priority) {
2444 LIST_INSERT_BEFORE(dp, disk, d_next);
2448 if (LIST_NEXT(dp, d_next) == NULL)
2452 LIST_INSERT_AFTER(dp, disk, d_next);
2454 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2455 sc->sc_name, g_mirror_get_diskname(disk));
2456 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2458 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2459 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2460 g_mirror_device_state2str(sc->sc_state),
2461 g_mirror_get_diskname(disk),
2462 g_mirror_disk_state2str(disk->d_state)));
2463 state = g_mirror_determine_state(disk);
2464 if (state != G_MIRROR_DISK_STATE_NONE)
2467 case G_MIRROR_DISK_STATE_ACTIVE:
2469 * Possible scenarios:
2470 * 1. New disk does not need synchronization.
2471 * 2. Synchronization process finished successfully.
2473 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2474 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2475 g_mirror_device_state2str(sc->sc_state),
2476 g_mirror_get_diskname(disk),
2477 g_mirror_disk_state2str(disk->d_state)));
2478 /* Previous state should be NEW or SYNCHRONIZING. */
2479 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2480 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2481 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2482 g_mirror_disk_state2str(disk->d_state)));
2483 DISK_STATE_CHANGED();
2485 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2486 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2487 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2488 g_mirror_sync_stop(disk, 0);
2490 disk->d_state = state;
2491 disk->d_sync.ds_offset = 0;
2492 disk->d_sync.ds_offset_done = 0;
2493 g_mirror_update_idle(sc, disk);
2494 g_mirror_update_metadata(disk);
2495 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2496 sc->sc_name, g_mirror_get_diskname(disk));
2498 case G_MIRROR_DISK_STATE_STALE:
2500 * Possible scenarios:
2501 * 1. Stale disk was connected.
2503 /* Previous state should be NEW. */
2504 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2505 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2506 g_mirror_disk_state2str(disk->d_state)));
2507 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2508 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2509 g_mirror_device_state2str(sc->sc_state),
2510 g_mirror_get_diskname(disk),
2511 g_mirror_disk_state2str(disk->d_state)));
2513 * STALE state is only possible if device is marked
2516 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2517 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2518 g_mirror_device_state2str(sc->sc_state),
2519 g_mirror_get_diskname(disk),
2520 g_mirror_disk_state2str(disk->d_state)));
2521 DISK_STATE_CHANGED();
2523 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2524 disk->d_state = state;
2525 g_mirror_update_metadata(disk);
2526 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2527 sc->sc_name, g_mirror_get_diskname(disk));
2529 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2531 * Possible scenarios:
2532 * 1. Disk which needs synchronization was connected.
2534 /* Previous state should be NEW. */
2535 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2536 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2537 g_mirror_disk_state2str(disk->d_state)));
2538 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2539 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2540 g_mirror_device_state2str(sc->sc_state),
2541 g_mirror_get_diskname(disk),
2542 g_mirror_disk_state2str(disk->d_state)));
2543 DISK_STATE_CHANGED();
2545 if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2546 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2547 disk->d_state = state;
2548 if (sc->sc_provider != NULL) {
2549 g_mirror_sync_start(disk);
2550 g_mirror_update_metadata(disk);
2553 case G_MIRROR_DISK_STATE_DISCONNECTED:
2555 * Possible scenarios:
2556 * 1. Device wasn't running yet, but disk disappear.
2557 * 2. Disk was active and disapppear.
2558 * 3. Disk disappear during synchronization process.
2560 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2562 * Previous state should be ACTIVE, STALE or
2565 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2566 disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2567 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2568 ("Wrong disk state (%s, %s).",
2569 g_mirror_get_diskname(disk),
2570 g_mirror_disk_state2str(disk->d_state)));
2571 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2572 /* Previous state should be NEW. */
2573 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2574 ("Wrong disk state (%s, %s).",
2575 g_mirror_get_diskname(disk),
2576 g_mirror_disk_state2str(disk->d_state)));
2578 * Reset bumping syncid if disk disappeared in STARTING
2581 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2582 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2585 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2587 g_mirror_device_state2str(sc->sc_state),
2588 g_mirror_get_diskname(disk),
2589 g_mirror_disk_state2str(disk->d_state)));
2592 DISK_STATE_CHANGED();
2593 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2594 sc->sc_name, g_mirror_get_diskname(disk));
2596 g_mirror_destroy_disk(disk);
2598 case G_MIRROR_DISK_STATE_DESTROY:
2602 error = g_mirror_clear_metadata(disk);
2605 DISK_STATE_CHANGED();
2606 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2607 sc->sc_name, g_mirror_get_diskname(disk));
2609 g_mirror_destroy_disk(disk);
2611 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2612 g_mirror_update_metadata(disk);
2617 KASSERT(1 == 0, ("Unknown state (%u).", state));
2622 #undef DISK_STATE_CHANGED
2625 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2627 struct g_provider *pp;
2631 g_topology_assert();
2633 error = g_access(cp, 1, 0, 0);
2637 g_topology_unlock();
2638 /* Metadata are stored on last sector. */
2639 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2642 g_access(cp, -1, 0, 0);
2644 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2645 cp->provider->name, error);
2649 /* Decode metadata. */
2650 error = mirror_metadata_decode(buf, md);
2652 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2654 if (md->md_version > G_MIRROR_VERSION) {
2656 "Kernel module is too old to handle metadata from %s.",
2657 cp->provider->name);
2661 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2662 cp->provider->name);
2670 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2671 struct g_mirror_metadata *md)
2674 if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2675 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2676 pp->name, md->md_did);
2679 if (md->md_all != sc->sc_ndisks) {
2681 "Invalid '%s' field on disk %s (device %s), skipping.",
2682 "md_all", pp->name, sc->sc_name);
2685 if (md->md_slice != sc->sc_slice) {
2687 "Invalid '%s' field on disk %s (device %s), skipping.",
2688 "md_slice", pp->name, sc->sc_name);
2691 if (md->md_balance != sc->sc_balance) {
2693 "Invalid '%s' field on disk %s (device %s), skipping.",
2694 "md_balance", pp->name, sc->sc_name);
2697 if (md->md_mediasize != sc->sc_mediasize) {
2699 "Invalid '%s' field on disk %s (device %s), skipping.",
2700 "md_mediasize", pp->name, sc->sc_name);
2703 if (sc->sc_mediasize > pp->mediasize) {
2705 "Invalid size of disk %s (device %s), skipping.", pp->name,
2709 if (md->md_sectorsize != sc->sc_sectorsize) {
2711 "Invalid '%s' field on disk %s (device %s), skipping.",
2712 "md_sectorsize", pp->name, sc->sc_name);
2715 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2717 "Invalid sector size of disk %s (device %s), skipping.",
2718 pp->name, sc->sc_name);
2721 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2723 "Invalid device flags on disk %s (device %s), skipping.",
2724 pp->name, sc->sc_name);
2727 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2729 "Invalid disk flags on disk %s (device %s), skipping.",
2730 pp->name, sc->sc_name);
2737 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2738 struct g_mirror_metadata *md)
2740 struct g_mirror_disk *disk;
2743 g_topology_assert_not();
2744 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2746 error = g_mirror_check_metadata(sc, pp, md);
2749 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING &&
2750 md->md_genid < sc->sc_genid) {
2751 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
2752 pp->name, sc->sc_name);
2755 disk = g_mirror_init_disk(sc, pp, md, &error);
2758 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
2759 G_MIRROR_EVENT_WAIT);
2762 if (md->md_version < G_MIRROR_VERSION) {
2763 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
2764 pp->name, md->md_version, G_MIRROR_VERSION);
2765 g_mirror_update_metadata(disk);
2771 g_mirror_destroy_delayed(void *arg, int flag)
2773 struct g_mirror_softc *sc;
2776 if (flag == EV_CANCEL) {
2777 G_MIRROR_DEBUG(1, "Destroying canceled.");
2781 g_topology_unlock();
2782 sx_xlock(&sc->sc_lock);
2783 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
2784 ("DESTROY flag set on %s.", sc->sc_name));
2785 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0,
2786 ("DESTROYING flag not set on %s.", sc->sc_name));
2787 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
2788 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
2790 G_MIRROR_DEBUG(0, "Cannot destroy %s.", sc->sc_name);
2791 sx_xunlock(&sc->sc_lock);
2797 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
2799 struct g_mirror_softc *sc;
2800 int dcr, dcw, dce, error = 0;
2802 g_topology_assert();
2803 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2806 sc = pp->geom->softc;
2807 if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
2809 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
2811 dcr = pp->acr + acr;
2812 dcw = pp->acw + acw;
2813 dce = pp->ace + ace;
2815 g_topology_unlock();
2816 sx_xlock(&sc->sc_lock);
2817 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
2818 LIST_EMPTY(&sc->sc_disks)) {
2819 if (acr > 0 || acw > 0 || ace > 0)
2823 if (dcw == 0 && !sc->sc_idle)
2824 g_mirror_idle(sc, dcw);
2825 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0) {
2826 if (acr > 0 || acw > 0 || ace > 0) {
2830 if (dcr == 0 && dcw == 0 && dce == 0) {
2831 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK,
2836 sx_xunlock(&sc->sc_lock);
2841 static struct g_geom *
2842 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
2844 struct g_mirror_softc *sc;
2848 g_topology_assert();
2849 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
2852 /* One disk is minimum. */
2858 gp = g_new_geomf(mp, "%s", md->md_name);
2859 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
2860 gp->start = g_mirror_start;
2861 gp->orphan = g_mirror_orphan;
2862 gp->access = g_mirror_access;
2863 gp->dumpconf = g_mirror_dumpconf;
2865 sc->sc_id = md->md_mid;
2866 sc->sc_slice = md->md_slice;
2867 sc->sc_balance = md->md_balance;
2868 sc->sc_mediasize = md->md_mediasize;
2869 sc->sc_sectorsize = md->md_sectorsize;
2870 sc->sc_ndisks = md->md_all;
2871 sc->sc_flags = md->md_mflags;
2874 sc->sc_last_write = time_uptime;
2876 sx_init(&sc->sc_lock, "gmirror:lock");
2877 bioq_init(&sc->sc_queue);
2878 mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
2879 bioq_init(&sc->sc_regular_delayed);
2880 bioq_init(&sc->sc_inflight);
2881 bioq_init(&sc->sc_sync_delayed);
2882 LIST_INIT(&sc->sc_disks);
2883 TAILQ_INIT(&sc->sc_events);
2884 mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
2885 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
2886 sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
2889 sc->sc_provider = NULL;
2891 * Synchronization geom.
2893 gp = g_new_geomf(mp, "%s.sync", md->md_name);
2895 gp->orphan = g_mirror_orphan;
2896 sc->sc_sync.ds_geom = gp;
2897 sc->sc_sync.ds_ndisks = 0;
2898 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
2899 "g_mirror %s", md->md_name);
2901 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
2903 g_destroy_geom(sc->sc_sync.ds_geom);
2904 mtx_destroy(&sc->sc_events_mtx);
2905 mtx_destroy(&sc->sc_queue_mtx);
2906 sx_destroy(&sc->sc_lock);
2907 g_destroy_geom(sc->sc_geom);
2912 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
2913 sc->sc_name, sc->sc_ndisks, sc->sc_id);
2915 sc->sc_rootmount = root_mount_hold("GMIRROR");
2916 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
2920 timeout = g_mirror_timeout * hz;
2921 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
2922 return (sc->sc_geom);
2926 g_mirror_destroy(struct g_mirror_softc *sc, int how)
2928 struct g_mirror_disk *disk;
2929 struct g_provider *pp;
2931 g_topology_assert_not();
2934 sx_assert(&sc->sc_lock, SX_XLOCKED);
2936 pp = sc->sc_provider;
2937 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
2939 case G_MIRROR_DESTROY_SOFT:
2941 "Device %s is still open (r%dw%de%d).", pp->name,
2942 pp->acr, pp->acw, pp->ace);
2944 case G_MIRROR_DESTROY_DELAYED:
2946 "Device %s will be destroyed on last close.",
2948 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2949 if (disk->d_state ==
2950 G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2951 g_mirror_sync_stop(disk, 1);
2954 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROYING;
2956 case G_MIRROR_DESTROY_HARD:
2957 G_MIRROR_DEBUG(1, "Device %s is still open, so it "
2958 "can't be definitely removed.", pp->name);
2963 if (sc->sc_geom->softc == NULL) {
2964 g_topology_unlock();
2967 sc->sc_geom->softc = NULL;
2968 sc->sc_sync.ds_geom->softc = NULL;
2969 g_topology_unlock();
2971 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2972 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WAIT;
2973 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
2974 sx_xunlock(&sc->sc_lock);
2975 mtx_lock(&sc->sc_queue_mtx);
2977 mtx_unlock(&sc->sc_queue_mtx);
2978 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
2979 while (sc->sc_worker != NULL)
2980 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
2981 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
2982 sx_xlock(&sc->sc_lock);
2983 g_mirror_destroy_device(sc);
2989 g_mirror_taste_orphan(struct g_consumer *cp)
2992 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2993 cp->provider->name));
2996 static struct g_geom *
2997 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2999 struct g_mirror_metadata md;
3000 struct g_mirror_softc *sc;
3001 struct g_consumer *cp;
3005 g_topology_assert();
3006 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3007 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
3009 gp = g_new_geomf(mp, "mirror:taste");
3011 * This orphan function should be never called.
3013 gp->orphan = g_mirror_taste_orphan;
3014 cp = g_new_consumer(gp);
3016 error = g_mirror_read_metadata(cp, &md);
3018 g_destroy_consumer(cp);
3024 if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0)
3026 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3028 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3030 "Device %s: provider %s marked as inactive, skipping.",
3031 md.md_name, pp->name);
3034 if (g_mirror_debug >= 2)
3035 mirror_metadata_dump(&md);
3038 * Let's check if device already exists.
3041 LIST_FOREACH(gp, &mp->geom, geom) {
3045 if (sc->sc_sync.ds_geom == gp)
3047 if (strcmp(md.md_name, sc->sc_name) != 0)
3049 if (md.md_mid != sc->sc_id) {
3050 G_MIRROR_DEBUG(0, "Device %s already configured.",
3057 gp = g_mirror_create(mp, &md);
3059 G_MIRROR_DEBUG(0, "Cannot create device %s.",
3065 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3066 g_topology_unlock();
3067 sx_xlock(&sc->sc_lock);
3068 error = g_mirror_add_disk(sc, pp, &md);
3070 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3071 pp->name, gp->name, error);
3072 if (LIST_EMPTY(&sc->sc_disks)) {
3074 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3080 sx_xunlock(&sc->sc_lock);
3086 g_mirror_destroy_geom(struct gctl_req *req __unused,
3087 struct g_class *mp __unused, struct g_geom *gp)
3089 struct g_mirror_softc *sc;
3092 g_topology_unlock();
3094 sx_xlock(&sc->sc_lock);
3096 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3098 sx_xunlock(&sc->sc_lock);
3104 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3105 struct g_consumer *cp, struct g_provider *pp)
3107 struct g_mirror_softc *sc;
3109 g_topology_assert();
3114 /* Skip synchronization geom. */
3115 if (gp == sc->sc_sync.ds_geom)
3119 } else if (cp != NULL) {
3120 struct g_mirror_disk *disk;
3125 g_topology_unlock();
3126 sx_xlock(&sc->sc_lock);
3127 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3128 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3129 sbuf_printf(sb, "%s<Synchronized>", indent);
3130 if (disk->d_sync.ds_offset == 0)
3131 sbuf_printf(sb, "0%%");
3133 sbuf_printf(sb, "%u%%",
3134 (u_int)((disk->d_sync.ds_offset * 100) /
3135 sc->sc_provider->mediasize));
3137 sbuf_printf(sb, "</Synchronized>\n");
3139 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3140 disk->d_sync.ds_syncid);
3141 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3143 sbuf_printf(sb, "%s<Flags>", indent);
3144 if (disk->d_flags == 0)
3145 sbuf_printf(sb, "NONE");
3149 #define ADD_FLAG(flag, name) do { \
3150 if ((disk->d_flags & (flag)) != 0) { \
3152 sbuf_printf(sb, ", "); \
3155 sbuf_printf(sb, name); \
3158 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3159 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3160 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3161 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3163 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3164 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3167 sbuf_printf(sb, "</Flags>\n");
3168 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3170 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3171 g_mirror_disk_state2str(disk->d_state));
3172 sx_xunlock(&sc->sc_lock);
3175 g_topology_unlock();
3176 sx_xlock(&sc->sc_lock);
3177 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3178 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3179 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3180 sbuf_printf(sb, "%s<Flags>", indent);
3181 if (sc->sc_flags == 0)
3182 sbuf_printf(sb, "NONE");
3186 #define ADD_FLAG(flag, name) do { \
3187 if ((sc->sc_flags & (flag)) != 0) { \
3189 sbuf_printf(sb, ", "); \
3192 sbuf_printf(sb, name); \
3195 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3196 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3199 sbuf_printf(sb, "</Flags>\n");
3200 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3201 (u_int)sc->sc_slice);
3202 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3203 balance_name(sc->sc_balance));
3204 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3206 sbuf_printf(sb, "%s<State>", indent);
3207 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3208 sbuf_printf(sb, "%s", "STARTING");
3209 else if (sc->sc_ndisks ==
3210 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3211 sbuf_printf(sb, "%s", "COMPLETE");
3213 sbuf_printf(sb, "%s", "DEGRADED");
3214 sbuf_printf(sb, "</State>\n");
3215 sx_xunlock(&sc->sc_lock);
3221 g_mirror_shutdown_pre_sync(void *arg, int howto)
3224 struct g_geom *gp, *gp2;
3225 struct g_mirror_softc *sc;
3231 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3232 if ((sc = gp->softc) == NULL)
3234 /* Skip synchronization geom. */
3235 if (gp == sc->sc_sync.ds_geom)
3237 g_topology_unlock();
3238 sx_xlock(&sc->sc_lock);
3240 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3242 sx_xunlock(&sc->sc_lock);
3245 g_topology_unlock();
3250 g_mirror_init(struct g_class *mp)
3253 g_mirror_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
3254 g_mirror_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
3255 if (g_mirror_pre_sync == NULL)
3256 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3260 g_mirror_fini(struct g_class *mp)
3263 if (g_mirror_pre_sync != NULL)
3264 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mirror_pre_sync);
3267 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);