2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/eventhandler.h>
37 #include <sys/kernel.h>
38 #include <sys/kthread.h>
39 #include <sys/limits.h>
41 #include <sys/malloc.h>
42 #include <sys/mutex.h>
45 #include <sys/sched.h>
47 #include <sys/sysctl.h>
49 #include <geom/geom.h>
50 #include <geom/geom_dbg.h>
51 #include <geom/mirror/g_mirror.h>
53 FEATURE(geom_mirror, "GEOM mirroring support");
55 static MALLOC_DEFINE(M_MIRROR, "mirror_data", "GEOM_MIRROR Data");
57 SYSCTL_DECL(_kern_geom);
58 static SYSCTL_NODE(_kern_geom, OID_AUTO, mirror, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
60 int g_mirror_debug = 0;
61 SYSCTL_INT(_kern_geom_mirror, OID_AUTO, debug, CTLFLAG_RWTUN, &g_mirror_debug, 0,
63 bool g_launch_mirror_before_timeout = true;
64 SYSCTL_BOOL(_kern_geom_mirror, OID_AUTO, launch_mirror_before_timeout,
65 CTLFLAG_RWTUN, &g_launch_mirror_before_timeout, 0,
66 "If false, force gmirror to wait out the full kern.geom.mirror.timeout "
67 "before launching mirrors");
68 static u_int g_mirror_timeout = 4;
69 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, timeout, CTLFLAG_RWTUN, &g_mirror_timeout,
70 0, "Time to wait on all mirror components");
71 static u_int g_mirror_idletime = 5;
72 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, idletime, CTLFLAG_RWTUN,
73 &g_mirror_idletime, 0, "Mark components as clean when idling");
74 static u_int g_mirror_disconnect_on_failure = 1;
75 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, disconnect_on_failure, CTLFLAG_RWTUN,
76 &g_mirror_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
77 static u_int g_mirror_syncreqs = 2;
78 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
79 &g_mirror_syncreqs, 0, "Parallel synchronization I/O requests.");
80 static u_int g_mirror_sync_period = 5;
81 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, sync_update_period, CTLFLAG_RWTUN,
82 &g_mirror_sync_period, 0,
83 "Metadata update period during synchronization, in seconds");
85 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \
86 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
87 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
88 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
91 static eventhandler_tag g_mirror_post_sync = NULL;
92 static int g_mirror_shutdown = 0;
94 static g_ctl_destroy_geom_t g_mirror_destroy_geom;
95 static g_taste_t g_mirror_taste;
96 static g_init_t g_mirror_init;
97 static g_fini_t g_mirror_fini;
98 static g_provgone_t g_mirror_providergone;
99 static g_resize_t g_mirror_resize;
101 struct g_class g_mirror_class = {
102 .name = G_MIRROR_CLASS_NAME,
103 .version = G_VERSION,
104 .ctlreq = g_mirror_config,
105 .taste = g_mirror_taste,
106 .destroy_geom = g_mirror_destroy_geom,
107 .init = g_mirror_init,
108 .fini = g_mirror_fini,
109 .providergone = g_mirror_providergone,
110 .resize = g_mirror_resize
113 static void g_mirror_destroy_provider(struct g_mirror_softc *sc);
114 static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
115 static void g_mirror_update_device(struct g_mirror_softc *sc, bool force);
116 static void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
117 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
118 static void g_mirror_timeout_drain(struct g_mirror_softc *sc);
119 static int g_mirror_refresh_device(struct g_mirror_softc *sc,
120 const struct g_provider *pp, const struct g_mirror_metadata *md);
121 static void g_mirror_sync_reinit(const struct g_mirror_disk *disk,
122 struct bio *bp, off_t offset);
123 static void g_mirror_sync_stop(struct g_mirror_disk *disk, int type);
124 static void g_mirror_register_request(struct g_mirror_softc *sc,
126 static void g_mirror_sync_release(struct g_mirror_softc *sc);
129 g_mirror_disk_state2str(int state)
133 case G_MIRROR_DISK_STATE_NONE:
135 case G_MIRROR_DISK_STATE_NEW:
137 case G_MIRROR_DISK_STATE_ACTIVE:
139 case G_MIRROR_DISK_STATE_STALE:
141 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
142 return ("SYNCHRONIZING");
143 case G_MIRROR_DISK_STATE_DISCONNECTED:
144 return ("DISCONNECTED");
145 case G_MIRROR_DISK_STATE_DESTROY:
153 g_mirror_device_state2str(int state)
157 case G_MIRROR_DEVICE_STATE_STARTING:
159 case G_MIRROR_DEVICE_STATE_RUNNING:
167 g_mirror_get_diskname(struct g_mirror_disk *disk)
170 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
171 return ("[unknown]");
172 return (disk->d_name);
176 * --- Events handling functions ---
177 * Events in geom_mirror are used to maintain disks and device status
178 * from one thread to simplify locking.
181 g_mirror_event_free(struct g_mirror_event *ep)
188 g_mirror_event_dispatch(struct g_mirror_event *ep, void *arg, int state,
191 struct g_mirror_softc *sc;
192 struct g_mirror_disk *disk;
195 G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
196 if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
207 mtx_lock(&sc->sc_events_mtx);
208 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
209 mtx_unlock(&sc->sc_events_mtx);
210 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
211 mtx_lock(&sc->sc_queue_mtx);
213 mtx_unlock(&sc->sc_queue_mtx);
214 if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0)
216 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
217 sx_xunlock(&sc->sc_lock);
218 while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) {
219 mtx_lock(&sc->sc_events_mtx);
220 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event",
224 g_mirror_event_free(ep);
225 sx_xlock(&sc->sc_lock);
230 g_mirror_event_send(void *arg, int state, int flags)
232 struct g_mirror_event *ep;
234 ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
235 return (g_mirror_event_dispatch(ep, arg, state, flags));
238 static struct g_mirror_event *
239 g_mirror_event_first(struct g_mirror_softc *sc)
241 struct g_mirror_event *ep;
243 mtx_lock(&sc->sc_events_mtx);
244 ep = TAILQ_FIRST(&sc->sc_events);
245 mtx_unlock(&sc->sc_events_mtx);
250 g_mirror_event_remove(struct g_mirror_softc *sc, struct g_mirror_event *ep)
253 mtx_lock(&sc->sc_events_mtx);
254 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
255 mtx_unlock(&sc->sc_events_mtx);
259 g_mirror_event_cancel(struct g_mirror_disk *disk)
261 struct g_mirror_softc *sc;
262 struct g_mirror_event *ep, *tmpep;
265 sx_assert(&sc->sc_lock, SX_XLOCKED);
267 mtx_lock(&sc->sc_events_mtx);
268 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
269 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0)
271 if (ep->e_disk != disk)
273 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
274 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
275 g_mirror_event_free(ep);
277 ep->e_error = ECANCELED;
281 mtx_unlock(&sc->sc_events_mtx);
285 * Return the number of disks in given state.
286 * If state is equal to -1, count all connected disks.
289 g_mirror_ndisks(struct g_mirror_softc *sc, int state)
291 struct g_mirror_disk *disk;
294 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
295 if (state == -1 || disk->d_state == state)
302 * Find a disk in mirror by its disk ID.
304 static struct g_mirror_disk *
305 g_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id)
307 struct g_mirror_disk *disk;
309 sx_assert(&sc->sc_lock, SX_XLOCKED);
311 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
312 if (disk->d_id == id)
319 g_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp)
324 mtx_lock(&sc->sc_queue_mtx);
325 TAILQ_FOREACH(bp, &sc->sc_queue, bio_queue) {
326 if (bp->bio_from == cp)
329 mtx_unlock(&sc->sc_queue_mtx);
334 g_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp)
339 "I/O requests for %s exist, can't destroy it now.",
343 if (g_mirror_nrequests(sc, cp) > 0) {
345 "I/O requests for %s in queue, can't destroy it now.",
353 g_mirror_destroy_consumer(void *arg, int flags __unused)
355 struct g_consumer *cp;
360 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
362 g_destroy_consumer(cp);
366 g_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
368 struct g_provider *pp;
374 if (g_mirror_is_busy(sc, cp))
379 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
382 G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
383 -cp->acw, -cp->ace, 0);
384 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
385 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
388 * After retaste event was send (inside g_access()), we can send
389 * event to detach and destroy consumer.
390 * A class, which has consumer to the given provider connected
391 * will not receive retaste event for the provider.
392 * This is the way how I ignore retaste events when I close
393 * consumers opened for write: I detach and destroy consumer
394 * after retaste event is sent.
396 g_post_event(g_mirror_destroy_consumer, cp, M_WAITOK, NULL);
399 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", pp->name);
401 g_destroy_consumer(cp);
405 g_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp)
407 struct g_consumer *cp;
410 g_topology_assert_not();
411 KASSERT(disk->d_consumer == NULL,
412 ("Disk already connected (device %s).", disk->d_softc->sc_name));
415 cp = g_new_consumer(disk->d_softc->sc_geom);
416 cp->flags |= G_CF_DIRECT_RECEIVE;
417 error = g_attach(cp, pp);
419 g_destroy_consumer(cp);
423 error = g_access(cp, 1, 1, 1);
426 g_destroy_consumer(cp);
428 G_MIRROR_DEBUG(0, "Cannot open consumer %s (error=%d).",
433 disk->d_consumer = cp;
434 disk->d_consumer->private = disk;
435 disk->d_consumer->index = 0;
437 G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk));
442 g_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
449 if (cp->provider != NULL)
450 g_mirror_kill_consumer(sc, cp);
452 g_destroy_consumer(cp);
456 * Initialize disk. This means allocate memory, create consumer, attach it
457 * to the provider and open access (r1w1e1) to it.
459 static struct g_mirror_disk *
460 g_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp,
461 struct g_mirror_metadata *md, int *errorp)
463 struct g_mirror_disk *disk;
466 disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO);
472 error = g_mirror_connect_disk(disk, pp);
475 disk->d_id = md->md_did;
476 disk->d_state = G_MIRROR_DISK_STATE_NONE;
477 disk->d_priority = md->md_priority;
478 disk->d_flags = md->md_dflags;
479 error = g_getattr("GEOM::candelete", disk->d_consumer, &i);
480 if (error == 0 && i != 0)
481 disk->d_flags |= G_MIRROR_DISK_FLAG_CANDELETE;
482 if (md->md_provider[0] != '\0')
483 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
484 disk->d_sync.ds_consumer = NULL;
485 disk->d_sync.ds_offset = md->md_sync_offset;
486 disk->d_sync.ds_offset_done = md->md_sync_offset;
487 disk->d_sync.ds_update_ts = time_uptime;
488 disk->d_genid = md->md_genid;
489 disk->d_sync.ds_syncid = md->md_syncid;
490 disk->d_init_ndisks = md->md_all;
491 disk->d_init_slice = md->md_slice;
492 disk->d_init_balance = md->md_balance;
493 disk->d_init_mediasize = md->md_mediasize;
501 free(disk, M_MIRROR);
506 g_mirror_destroy_disk(struct g_mirror_disk *disk)
508 struct g_mirror_softc *sc;
510 g_topology_assert_not();
512 sx_assert(&sc->sc_lock, SX_XLOCKED);
515 LIST_REMOVE(disk, d_next);
517 g_mirror_event_cancel(disk);
518 if (sc->sc_hint == disk)
520 switch (disk->d_state) {
521 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
522 g_mirror_sync_stop(disk, 1);
524 case G_MIRROR_DISK_STATE_NEW:
525 case G_MIRROR_DISK_STATE_STALE:
526 case G_MIRROR_DISK_STATE_ACTIVE:
528 g_mirror_disconnect_consumer(sc, disk->d_consumer);
530 free(disk, M_MIRROR);
533 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
534 g_mirror_get_diskname(disk),
535 g_mirror_disk_state2str(disk->d_state)));
540 g_mirror_free_device(struct g_mirror_softc *sc)
545 mtx_destroy(&sc->sc_queue_mtx);
546 mtx_destroy(&sc->sc_events_mtx);
547 mtx_destroy(&sc->sc_done_mtx);
548 sx_destroy(&sc->sc_lock);
553 g_mirror_providergone(struct g_provider *pp)
555 struct g_mirror_softc *sc = pp->private;
557 if ((--sc->sc_refcnt) == 0)
558 g_mirror_free_device(sc);
562 g_mirror_destroy_device(struct g_mirror_softc *sc)
564 struct g_mirror_disk *disk;
565 struct g_mirror_event *ep;
567 struct g_consumer *cp, *tmpcp;
569 g_topology_assert_not();
570 sx_assert(&sc->sc_lock, SX_XLOCKED);
573 if (sc->sc_provider != NULL)
574 g_mirror_destroy_provider(sc);
575 for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL;
576 disk = LIST_FIRST(&sc->sc_disks)) {
577 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
578 g_mirror_update_metadata(disk);
579 g_mirror_destroy_disk(disk);
581 while ((ep = g_mirror_event_first(sc)) != NULL) {
582 g_mirror_event_remove(sc, ep);
583 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
584 g_mirror_event_free(ep);
586 ep->e_error = ECANCELED;
587 ep->e_flags |= G_MIRROR_EVENT_DONE;
588 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep);
589 mtx_lock(&sc->sc_events_mtx);
591 mtx_unlock(&sc->sc_events_mtx);
594 g_mirror_timeout_drain(sc);
597 LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
598 g_mirror_disconnect_consumer(sc, cp);
600 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
601 G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
602 g_wither_geom(gp, ENXIO);
603 sx_xunlock(&sc->sc_lock);
604 if ((--sc->sc_refcnt) == 0)
605 g_mirror_free_device(sc);
610 g_mirror_orphan(struct g_consumer *cp)
612 struct g_mirror_disk *disk;
619 disk->d_softc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
620 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
621 G_MIRROR_EVENT_DONTWAIT);
625 * Function should return the next active disk on the list.
626 * It is possible that it will be the same disk as given.
627 * If there are no active disks on list, NULL is returned.
629 static __inline struct g_mirror_disk *
630 g_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
632 struct g_mirror_disk *dp;
634 for (dp = LIST_NEXT(disk, d_next); dp != disk;
635 dp = LIST_NEXT(dp, d_next)) {
637 dp = LIST_FIRST(&sc->sc_disks);
638 if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE)
641 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
646 static struct g_mirror_disk *
647 g_mirror_get_disk(struct g_mirror_softc *sc)
649 struct g_mirror_disk *disk;
651 if (sc->sc_hint == NULL) {
652 sc->sc_hint = LIST_FIRST(&sc->sc_disks);
653 if (sc->sc_hint == NULL)
657 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) {
658 disk = g_mirror_find_next(sc, disk);
662 sc->sc_hint = g_mirror_find_next(sc, disk);
667 g_mirror_write_metadata(struct g_mirror_disk *disk,
668 struct g_mirror_metadata *md)
670 struct g_mirror_softc *sc;
671 struct g_consumer *cp;
672 off_t offset, length;
676 g_topology_assert_not();
678 sx_assert(&sc->sc_lock, SX_LOCKED);
680 cp = disk->d_consumer;
681 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
682 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
683 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
684 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
686 length = cp->provider->sectorsize;
687 offset = cp->provider->mediasize - length;
688 sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO);
690 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0) {
692 * Handle the case, when the size of parent provider reduced.
694 if (offset < md->md_mediasize)
697 mirror_metadata_encode(md, sector);
699 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_metadata_write, error);
701 error = g_write_data(cp, offset, sector, length);
702 free(sector, M_MIRROR);
704 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
705 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
706 G_MIRROR_DEBUG(0, "Cannot write metadata on %s "
707 "(device=%s, error=%d).",
708 g_mirror_get_diskname(disk), sc->sc_name, error);
710 G_MIRROR_DEBUG(1, "Cannot write metadata on %s "
711 "(device=%s, error=%d).",
712 g_mirror_get_diskname(disk), sc->sc_name, error);
714 if (g_mirror_disconnect_on_failure &&
715 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) {
716 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
717 g_mirror_event_send(disk,
718 G_MIRROR_DISK_STATE_DISCONNECTED,
719 G_MIRROR_EVENT_DONTWAIT);
726 g_mirror_clear_metadata(struct g_mirror_disk *disk)
730 g_topology_assert_not();
731 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
733 if (disk->d_softc->sc_type != G_MIRROR_TYPE_AUTOMATIC)
735 error = g_mirror_write_metadata(disk, NULL);
737 G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
738 g_mirror_get_diskname(disk));
741 "Cannot clear metadata on disk %s (error=%d).",
742 g_mirror_get_diskname(disk), error);
748 g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
749 struct g_mirror_metadata *md)
752 bzero(md, sizeof(*md));
753 strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
754 md->md_version = G_MIRROR_VERSION;
755 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
756 md->md_mid = sc->sc_id;
757 md->md_all = sc->sc_ndisks;
758 md->md_slice = sc->sc_slice;
759 md->md_balance = sc->sc_balance;
760 md->md_genid = sc->sc_genid;
761 md->md_mediasize = sc->sc_mediasize;
762 md->md_sectorsize = sc->sc_sectorsize;
763 md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
765 md->md_did = arc4random();
767 md->md_did = disk->d_id;
768 md->md_priority = disk->d_priority;
769 md->md_syncid = disk->d_sync.ds_syncid;
770 md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
771 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
772 md->md_sync_offset = disk->d_sync.ds_offset_done;
773 if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
774 strlcpy(md->md_provider,
775 disk->d_consumer->provider->name,
776 sizeof(md->md_provider));
778 md->md_provsize = disk->d_consumer->provider->mediasize;
783 g_mirror_update_metadata(struct g_mirror_disk *disk)
785 struct g_mirror_softc *sc;
786 struct g_mirror_metadata md;
789 g_topology_assert_not();
791 sx_assert(&sc->sc_lock, SX_LOCKED);
793 if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC)
795 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0)
796 g_mirror_fill_metadata(sc, disk, &md);
797 error = g_mirror_write_metadata(disk, &md);
799 G_MIRROR_DEBUG(2, "Metadata on %s updated.",
800 g_mirror_get_diskname(disk));
803 "Cannot update metadata on disk %s (error=%d).",
804 g_mirror_get_diskname(disk), error);
809 g_mirror_bump_syncid(struct g_mirror_softc *sc)
811 struct g_mirror_disk *disk;
813 g_topology_assert_not();
814 sx_assert(&sc->sc_lock, SX_XLOCKED);
815 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
816 ("%s called with no active disks (device=%s).", __func__,
820 G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
822 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
823 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
824 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
825 disk->d_sync.ds_syncid = sc->sc_syncid;
826 g_mirror_update_metadata(disk);
832 g_mirror_bump_genid(struct g_mirror_softc *sc)
834 struct g_mirror_disk *disk;
836 g_topology_assert_not();
837 sx_assert(&sc->sc_lock, SX_XLOCKED);
838 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
839 ("%s called with no active disks (device=%s).", __func__,
843 G_MIRROR_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
845 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
846 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
847 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
848 disk->d_genid = sc->sc_genid;
849 g_mirror_update_metadata(disk);
855 g_mirror_idle(struct g_mirror_softc *sc, int acw)
857 struct g_mirror_disk *disk;
860 g_topology_assert_not();
861 sx_assert(&sc->sc_lock, SX_XLOCKED);
863 if (sc->sc_provider == NULL)
865 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
869 if (sc->sc_writes > 0)
871 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
872 timeout = g_mirror_idletime - (time_uptime - sc->sc_last_write);
873 if (!g_mirror_shutdown && timeout > 0)
877 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
878 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
880 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as clean.",
881 g_mirror_get_diskname(disk), sc->sc_name);
882 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
883 g_mirror_update_metadata(disk);
889 g_mirror_unidle(struct g_mirror_softc *sc)
891 struct g_mirror_disk *disk;
893 g_topology_assert_not();
894 sx_assert(&sc->sc_lock, SX_XLOCKED);
896 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
899 sc->sc_last_write = time_uptime;
900 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
901 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
903 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as dirty.",
904 g_mirror_get_diskname(disk), sc->sc_name);
905 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
906 g_mirror_update_metadata(disk);
911 g_mirror_done(struct bio *bp)
913 struct g_mirror_softc *sc;
915 sc = bp->bio_from->geom->softc;
916 bp->bio_cflags = G_MIRROR_BIO_FLAG_REGULAR;
917 mtx_lock(&sc->sc_queue_mtx);
918 TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
919 mtx_unlock(&sc->sc_queue_mtx);
924 g_mirror_regular_request_error(struct g_mirror_softc *sc,
925 struct g_mirror_disk *disk, struct bio *bp)
928 if ((bp->bio_cmd == BIO_FLUSH || bp->bio_cmd == BIO_SPEEDUP) &&
929 bp->bio_error == EOPNOTSUPP)
932 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
933 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
934 G_MIRROR_LOGREQ(0, bp, "Request failed (error=%d).",
937 G_MIRROR_LOGREQ(1, bp, "Request failed (error=%d).",
940 if (g_mirror_disconnect_on_failure &&
941 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) {
942 if (bp->bio_error == ENXIO &&
943 bp->bio_cmd == BIO_READ)
944 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
945 else if (bp->bio_error == ENXIO)
946 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID_NOW;
948 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
949 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
950 G_MIRROR_EVENT_DONTWAIT);
955 g_mirror_regular_request(struct g_mirror_softc *sc, struct bio *bp)
957 struct g_mirror_disk *disk;
960 g_topology_assert_not();
961 KASSERT(sc->sc_provider == bp->bio_parent->bio_to,
962 ("regular request %p with unexpected origin", bp));
964 pbp = bp->bio_parent;
965 bp->bio_from->index--;
966 if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE)
968 disk = bp->bio_from->private;
971 g_mirror_kill_consumer(sc, bp->bio_from);
975 switch (bp->bio_cmd) {
977 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_regular_request_read,
981 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_regular_request_write,
985 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_regular_request_delete,
989 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_regular_request_flush,
993 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_regular_request_speedup,
999 KASSERT(pbp->bio_inbed <= pbp->bio_children,
1000 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
1001 pbp->bio_children));
1002 if (bp->bio_error == 0 && pbp->bio_error == 0) {
1003 G_MIRROR_LOGREQ(3, bp, "Request delivered.");
1005 if (pbp->bio_children == pbp->bio_inbed) {
1006 G_MIRROR_LOGREQ(3, pbp, "Request delivered.");
1007 pbp->bio_completed = pbp->bio_length;
1008 if (pbp->bio_cmd == BIO_WRITE ||
1009 pbp->bio_cmd == BIO_DELETE) {
1010 TAILQ_REMOVE(&sc->sc_inflight, pbp, bio_queue);
1011 /* Release delayed sync requests if possible. */
1012 g_mirror_sync_release(sc);
1014 g_io_deliver(pbp, pbp->bio_error);
1017 } else if (bp->bio_error != 0) {
1018 if (pbp->bio_error == 0)
1019 pbp->bio_error = bp->bio_error;
1021 g_mirror_regular_request_error(sc, disk, bp);
1022 switch (pbp->bio_cmd) {
1028 pbp->bio_children--;
1034 switch (pbp->bio_cmd) {
1036 if (pbp->bio_inbed < pbp->bio_children)
1038 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1)
1039 g_io_deliver(pbp, pbp->bio_error);
1042 mtx_lock(&sc->sc_queue_mtx);
1043 TAILQ_INSERT_TAIL(&sc->sc_queue, pbp, bio_queue);
1044 mtx_unlock(&sc->sc_queue_mtx);
1045 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1053 if (pbp->bio_children == 0) {
1055 * All requests failed.
1057 } else if (pbp->bio_inbed < pbp->bio_children) {
1060 } else if (pbp->bio_children == pbp->bio_inbed) {
1061 /* Some requests succeeded. */
1063 pbp->bio_completed = pbp->bio_length;
1065 if (pbp->bio_cmd == BIO_WRITE || pbp->bio_cmd == BIO_DELETE) {
1066 TAILQ_REMOVE(&sc->sc_inflight, pbp, bio_queue);
1067 /* Release delayed sync requests if possible. */
1068 g_mirror_sync_release(sc);
1070 g_io_deliver(pbp, pbp->bio_error);
1073 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
1079 g_mirror_sync_done(struct bio *bp)
1081 struct g_mirror_softc *sc;
1083 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
1084 sc = bp->bio_from->geom->softc;
1085 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
1086 mtx_lock(&sc->sc_queue_mtx);
1087 TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
1088 mtx_unlock(&sc->sc_queue_mtx);
1093 g_mirror_candelete(struct bio *bp)
1095 struct g_mirror_softc *sc;
1096 struct g_mirror_disk *disk;
1099 sc = bp->bio_to->private;
1100 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1101 if (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE)
1105 g_handleattr(bp, "GEOM::candelete", &val, sizeof(val));
1109 g_mirror_kernel_dump(struct bio *bp)
1111 struct g_mirror_softc *sc;
1112 struct g_mirror_disk *disk;
1114 struct g_kerneldump *gkd;
1117 * We configure dumping to the first component, because this component
1118 * will be used for reading with 'prefer' balance algorithm.
1119 * If the component with the highest priority is currently disconnected
1120 * we will not be able to read the dump after the reboot if it will be
1121 * connected and synchronized later. Can we do something better?
1123 sc = bp->bio_to->private;
1124 disk = LIST_FIRST(&sc->sc_disks);
1126 gkd = (struct g_kerneldump *)bp->bio_data;
1127 if (gkd->length > bp->bio_to->mediasize)
1128 gkd->length = bp->bio_to->mediasize;
1129 cbp = g_clone_bio(bp);
1131 g_io_deliver(bp, ENOMEM);
1134 cbp->bio_done = g_std_done;
1135 g_io_request(cbp, disk->d_consumer);
1136 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1137 g_mirror_get_diskname(disk));
1141 g_mirror_start(struct bio *bp)
1143 struct g_mirror_softc *sc;
1145 sc = bp->bio_to->private;
1147 * If sc == NULL or there are no valid disks, provider's error
1148 * should be set and g_mirror_start() should not be called at all.
1150 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1151 ("Provider's error should be set (error=%d)(mirror=%s).",
1152 bp->bio_to->error, bp->bio_to->name));
1153 G_MIRROR_LOGREQ(3, bp, "Request received.");
1155 switch (bp->bio_cmd) {
1163 if (!strcmp(bp->bio_attribute, "GEOM::candelete")) {
1164 g_mirror_candelete(bp);
1166 } else if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1167 g_mirror_kernel_dump(bp);
1172 g_io_deliver(bp, EOPNOTSUPP);
1175 mtx_lock(&sc->sc_queue_mtx);
1176 if (bp->bio_to->error != 0) {
1177 mtx_unlock(&sc->sc_queue_mtx);
1178 g_io_deliver(bp, bp->bio_to->error);
1181 TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
1182 mtx_unlock(&sc->sc_queue_mtx);
1183 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1188 * Return TRUE if the given request is colliding with a in-progress
1189 * synchronization request.
1192 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1194 struct g_mirror_disk *disk;
1196 off_t rstart, rend, sstart, send;
1199 if (sc->sc_sync.ds_ndisks == 0)
1201 rstart = bp->bio_offset;
1202 rend = bp->bio_offset + bp->bio_length;
1203 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1204 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1206 for (i = 0; i < g_mirror_syncreqs; i++) {
1207 sbp = disk->d_sync.ds_bios[i];
1210 sstart = sbp->bio_offset;
1211 send = sbp->bio_offset + sbp->bio_length;
1212 if (rend > sstart && rstart < send)
1220 * Return TRUE if the given sync request is colliding with a in-progress regular
1224 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1226 off_t rstart, rend, sstart, send;
1229 if (sc->sc_sync.ds_ndisks == 0)
1231 sstart = sbp->bio_offset;
1232 send = sbp->bio_offset + sbp->bio_length;
1233 TAILQ_FOREACH(bp, &sc->sc_inflight, bio_queue) {
1234 rstart = bp->bio_offset;
1235 rend = bp->bio_offset + bp->bio_length;
1236 if (rend > sstart && rstart < send)
1243 * Puts regular request onto delayed queue.
1246 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1249 G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1250 TAILQ_INSERT_TAIL(&sc->sc_regular_delayed, bp, bio_queue);
1254 * Puts synchronization request onto delayed queue.
1257 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1260 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1261 TAILQ_INSERT_TAIL(&sc->sc_sync_delayed, bp, bio_queue);
1265 * Requeue delayed regular requests.
1268 g_mirror_regular_release(struct g_mirror_softc *sc)
1272 if ((bp = TAILQ_FIRST(&sc->sc_regular_delayed)) == NULL)
1274 if (g_mirror_sync_collision(sc, bp))
1277 G_MIRROR_DEBUG(2, "Requeuing regular requests after collision.");
1278 mtx_lock(&sc->sc_queue_mtx);
1279 TAILQ_CONCAT(&sc->sc_regular_delayed, &sc->sc_queue, bio_queue);
1280 TAILQ_SWAP(&sc->sc_regular_delayed, &sc->sc_queue, bio, bio_queue);
1281 mtx_unlock(&sc->sc_queue_mtx);
1285 * Releases delayed sync requests which don't collide anymore with regular
1289 g_mirror_sync_release(struct g_mirror_softc *sc)
1291 struct bio *bp, *bp2;
1293 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed, bio_queue, bp2) {
1294 if (g_mirror_regular_collision(sc, bp))
1296 TAILQ_REMOVE(&sc->sc_sync_delayed, bp, bio_queue);
1297 G_MIRROR_LOGREQ(2, bp,
1298 "Releasing delayed synchronization request.");
1299 g_io_request(bp, bp->bio_from);
1304 * Free a synchronization request and clear its slot in the array.
1307 g_mirror_sync_request_free(struct g_mirror_disk *disk, struct bio *bp)
1311 if (disk != NULL && disk->d_sync.ds_bios != NULL) {
1312 idx = (int)(uintptr_t)bp->bio_caller1;
1313 KASSERT(disk->d_sync.ds_bios[idx] == bp,
1314 ("unexpected sync BIO at %p:%d", disk, idx));
1315 disk->d_sync.ds_bios[idx] = NULL;
1317 free(bp->bio_data, M_MIRROR);
1322 * Handle synchronization requests.
1323 * Every synchronization request is a two-step process: first, a read request is
1324 * sent to the mirror provider via the sync consumer. If that request completes
1325 * successfully, it is converted to a write and sent to the disk being
1326 * synchronized. If the write also completes successfully, the synchronization
1327 * offset is advanced and a new read request is submitted.
1330 g_mirror_sync_request(struct g_mirror_softc *sc, struct bio *bp)
1332 struct g_mirror_disk *disk;
1333 struct g_mirror_disk_sync *sync;
1335 KASSERT((bp->bio_cmd == BIO_READ &&
1336 bp->bio_from->geom == sc->sc_sync.ds_geom) ||
1337 (bp->bio_cmd == BIO_WRITE && bp->bio_from->geom == sc->sc_geom),
1338 ("Sync BIO %p with unexpected origin", bp));
1340 bp->bio_from->index--;
1341 disk = bp->bio_from->private;
1343 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1345 g_mirror_kill_consumer(sc, bp->bio_from);
1346 g_topology_unlock();
1347 g_mirror_sync_request_free(NULL, bp);
1348 sx_xlock(&sc->sc_lock);
1352 sync = &disk->d_sync;
1355 * Synchronization request.
1357 switch (bp->bio_cmd) {
1359 struct g_consumer *cp;
1361 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_sync_request_read,
1364 if (bp->bio_error != 0) {
1365 G_MIRROR_LOGREQ(0, bp,
1366 "Synchronization request failed (error=%d).",
1370 * The read error will trigger a syncid bump, so there's
1371 * no need to do that here.
1373 * The read error handling for regular requests will
1374 * retry the read from all active mirrors before passing
1375 * the error back up, so there's no need to retry here.
1377 g_mirror_sync_request_free(disk, bp);
1378 g_mirror_event_send(disk,
1379 G_MIRROR_DISK_STATE_DISCONNECTED,
1380 G_MIRROR_EVENT_DONTWAIT);
1383 G_MIRROR_LOGREQ(3, bp,
1384 "Synchronization request half-finished.");
1385 bp->bio_cmd = BIO_WRITE;
1387 cp = disk->d_consumer;
1388 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1389 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1390 cp->acr, cp->acw, cp->ace));
1392 g_io_request(bp, cp);
1399 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_sync_request_write,
1402 if (bp->bio_error != 0) {
1403 G_MIRROR_LOGREQ(0, bp,
1404 "Synchronization request failed (error=%d).",
1406 g_mirror_sync_request_free(disk, bp);
1407 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1408 g_mirror_event_send(disk,
1409 G_MIRROR_DISK_STATE_DISCONNECTED,
1410 G_MIRROR_EVENT_DONTWAIT);
1413 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1414 if (sync->ds_offset >= sc->sc_mediasize ||
1415 sync->ds_consumer == NULL ||
1416 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1417 /* Don't send more synchronization requests. */
1418 sync->ds_inflight--;
1419 g_mirror_sync_request_free(disk, bp);
1420 if (sync->ds_inflight > 0)
1422 if (sync->ds_consumer == NULL ||
1423 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1426 /* Disk up-to-date, activate it. */
1427 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1428 G_MIRROR_EVENT_DONTWAIT);
1432 /* Send next synchronization request. */
1433 g_mirror_sync_reinit(disk, bp, sync->ds_offset);
1434 sync->ds_offset += bp->bio_length;
1436 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1437 sync->ds_consumer->index++;
1440 * Delay the request if it is colliding with a regular request.
1442 if (g_mirror_regular_collision(sc, bp))
1443 g_mirror_sync_delay(sc, bp);
1445 g_io_request(bp, sync->ds_consumer);
1447 /* Requeue delayed requests if possible. */
1448 g_mirror_regular_release(sc);
1450 /* Find the smallest offset */
1451 offset = sc->sc_mediasize;
1452 for (i = 0; i < g_mirror_syncreqs; i++) {
1453 bp = sync->ds_bios[i];
1454 if (bp != NULL && bp->bio_offset < offset)
1455 offset = bp->bio_offset;
1457 if (g_mirror_sync_period > 0 &&
1458 time_uptime - sync->ds_update_ts > g_mirror_sync_period) {
1459 sync->ds_offset_done = offset;
1460 g_mirror_update_metadata(disk);
1461 sync->ds_update_ts = time_uptime;
1466 panic("Invalid I/O request %p", bp);
1471 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1473 struct g_mirror_disk *disk;
1474 struct g_consumer *cp;
1477 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1478 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1482 if (bp->bio_error == 0)
1483 bp->bio_error = ENXIO;
1484 g_io_deliver(bp, bp->bio_error);
1487 cbp = g_clone_bio(bp);
1489 if (bp->bio_error == 0)
1490 bp->bio_error = ENOMEM;
1491 g_io_deliver(bp, bp->bio_error);
1495 * Fill in the component buf structure.
1497 cp = disk->d_consumer;
1498 cbp->bio_done = g_mirror_done;
1499 cbp->bio_to = cp->provider;
1500 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1501 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1502 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1505 g_io_request(cbp, cp);
1509 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1511 struct g_mirror_disk *disk;
1512 struct g_consumer *cp;
1515 disk = g_mirror_get_disk(sc);
1517 if (bp->bio_error == 0)
1518 bp->bio_error = ENXIO;
1519 g_io_deliver(bp, bp->bio_error);
1522 cbp = g_clone_bio(bp);
1524 if (bp->bio_error == 0)
1525 bp->bio_error = ENOMEM;
1526 g_io_deliver(bp, bp->bio_error);
1530 * Fill in the component buf structure.
1532 cp = disk->d_consumer;
1533 cbp->bio_done = g_mirror_done;
1534 cbp->bio_to = cp->provider;
1535 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1536 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1537 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1540 g_io_request(cbp, cp);
1543 #define TRACK_SIZE (1 * 1024 * 1024)
1544 #define LOAD_SCALE 256
1545 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
1548 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1550 struct g_mirror_disk *disk, *dp;
1551 struct g_consumer *cp;
1555 /* Find a disk with the smallest load. */
1558 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1559 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1562 /* If disk head is precisely in position - highly prefer it. */
1563 if (dp->d_last_offset == bp->bio_offset)
1564 prio -= 2 * LOAD_SCALE;
1566 /* If disk head is close to position - prefer it. */
1567 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1568 prio -= 1 * LOAD_SCALE;
1574 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1575 cbp = g_clone_bio(bp);
1577 if (bp->bio_error == 0)
1578 bp->bio_error = ENOMEM;
1579 g_io_deliver(bp, bp->bio_error);
1583 * Fill in the component buf structure.
1585 cp = disk->d_consumer;
1586 cbp->bio_done = g_mirror_done;
1587 cbp->bio_to = cp->provider;
1588 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1589 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1590 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1593 /* Remember last head position */
1594 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1596 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1597 dp->load = (dp->d_consumer->index * LOAD_SCALE +
1600 g_io_request(cbp, cp);
1604 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1606 struct bio_queue queue;
1607 struct g_mirror_disk *disk;
1608 struct g_consumer *cp;
1610 off_t left, mod, offset, slice;
1614 if (bp->bio_length <= sc->sc_slice) {
1615 g_mirror_request_round_robin(sc, bp);
1618 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1619 slice = bp->bio_length / ndisks;
1620 mod = slice % sc->sc_provider->sectorsize;
1622 slice += sc->sc_provider->sectorsize - mod;
1624 * Allocate all bios before sending any request, so we can
1625 * return ENOMEM in nice and clean way.
1627 left = bp->bio_length;
1628 offset = bp->bio_offset;
1629 data = bp->bio_data;
1631 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1632 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1634 cbp = g_clone_bio(bp);
1636 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1637 TAILQ_REMOVE(&queue, cbp, bio_queue);
1640 if (bp->bio_error == 0)
1641 bp->bio_error = ENOMEM;
1642 g_io_deliver(bp, bp->bio_error);
1645 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
1646 cbp->bio_done = g_mirror_done;
1647 cbp->bio_caller1 = disk;
1648 cbp->bio_to = disk->d_consumer->provider;
1649 cbp->bio_offset = offset;
1650 cbp->bio_data = data;
1651 cbp->bio_length = MIN(left, slice);
1652 left -= cbp->bio_length;
1655 offset += cbp->bio_length;
1656 data += cbp->bio_length;
1658 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1659 TAILQ_REMOVE(&queue, cbp, bio_queue);
1660 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1661 disk = cbp->bio_caller1;
1662 cbp->bio_caller1 = NULL;
1663 cp = disk->d_consumer;
1664 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1665 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1666 cp->acr, cp->acw, cp->ace));
1667 disk->d_consumer->index++;
1668 g_io_request(cbp, disk->d_consumer);
1673 g_mirror_register_request(struct g_mirror_softc *sc, struct bio *bp)
1675 struct bio_queue queue;
1677 struct g_consumer *cp;
1678 struct g_mirror_disk *disk;
1680 sx_assert(&sc->sc_lock, SA_XLOCKED);
1683 * To avoid ordering issues, if a write is deferred because of a
1684 * collision with a sync request, all I/O is deferred until that
1685 * write is initiated.
1687 if (bp->bio_from->geom != sc->sc_sync.ds_geom &&
1688 !TAILQ_EMPTY(&sc->sc_regular_delayed)) {
1689 g_mirror_regular_delay(sc, bp);
1693 switch (bp->bio_cmd) {
1695 switch (sc->sc_balance) {
1696 case G_MIRROR_BALANCE_LOAD:
1697 g_mirror_request_load(sc, bp);
1699 case G_MIRROR_BALANCE_PREFER:
1700 g_mirror_request_prefer(sc, bp);
1702 case G_MIRROR_BALANCE_ROUND_ROBIN:
1703 g_mirror_request_round_robin(sc, bp);
1705 case G_MIRROR_BALANCE_SPLIT:
1706 g_mirror_request_split(sc, bp);
1713 * Delay the request if it is colliding with a synchronization
1716 if (g_mirror_sync_collision(sc, bp)) {
1717 g_mirror_regular_delay(sc, bp);
1722 g_mirror_unidle(sc);
1724 sc->sc_last_write = time_uptime;
1727 * Bump syncid on first write.
1729 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1730 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1731 g_mirror_bump_syncid(sc);
1735 * Allocate all bios before sending any request, so we can
1736 * return ENOMEM in nice and clean way.
1739 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1740 switch (disk->d_state) {
1741 case G_MIRROR_DISK_STATE_ACTIVE:
1743 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1744 if (bp->bio_offset >= disk->d_sync.ds_offset)
1750 if (bp->bio_cmd == BIO_DELETE &&
1751 (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE) == 0)
1753 cbp = g_clone_bio(bp);
1755 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1756 TAILQ_REMOVE(&queue, cbp, bio_queue);
1759 if (bp->bio_error == 0)
1760 bp->bio_error = ENOMEM;
1761 g_io_deliver(bp, bp->bio_error);
1764 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
1765 cbp->bio_done = g_mirror_done;
1766 cp = disk->d_consumer;
1767 cbp->bio_caller1 = cp;
1768 cbp->bio_to = cp->provider;
1769 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1770 ("Consumer %s not opened (r%dw%de%d).",
1771 cp->provider->name, cp->acr, cp->acw, cp->ace));
1773 if (TAILQ_EMPTY(&queue)) {
1774 KASSERT(bp->bio_cmd == BIO_DELETE,
1775 ("No consumers for regular request %p", bp));
1776 g_io_deliver(bp, EOPNOTSUPP);
1779 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1780 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1781 TAILQ_REMOVE(&queue, cbp, bio_queue);
1782 cp = cbp->bio_caller1;
1783 cbp->bio_caller1 = NULL;
1786 g_io_request(cbp, cp);
1789 * Put request onto inflight queue, so we can check if new
1790 * synchronization requests don't collide with it.
1792 TAILQ_INSERT_TAIL(&sc->sc_inflight, bp, bio_queue);
1797 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1798 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1800 cbp = g_clone_bio(bp);
1802 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1803 TAILQ_REMOVE(&queue, cbp, bio_queue);
1806 if (bp->bio_error == 0)
1807 bp->bio_error = ENOMEM;
1808 g_io_deliver(bp, bp->bio_error);
1811 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
1812 cbp->bio_done = g_mirror_done;
1813 cbp->bio_caller1 = disk;
1814 cbp->bio_to = disk->d_consumer->provider;
1816 KASSERT(!TAILQ_EMPTY(&queue),
1817 ("No consumers for regular request %p", bp));
1818 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1819 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1820 TAILQ_REMOVE(&queue, cbp, bio_queue);
1821 disk = cbp->bio_caller1;
1822 cbp->bio_caller1 = NULL;
1823 cp = disk->d_consumer;
1824 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1825 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1826 cp->acr, cp->acw, cp->ace));
1828 g_io_request(cbp, cp);
1832 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1833 bp->bio_cmd, sc->sc_name));
1839 g_mirror_can_destroy(struct g_mirror_softc *sc)
1842 struct g_consumer *cp;
1844 g_topology_assert();
1846 if (gp->softc == NULL)
1848 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_TASTING) != 0)
1850 LIST_FOREACH(cp, &gp->consumer, consumer) {
1851 if (g_mirror_is_busy(sc, cp))
1854 gp = sc->sc_sync.ds_geom;
1855 LIST_FOREACH(cp, &gp->consumer, consumer) {
1856 if (g_mirror_is_busy(sc, cp))
1859 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1865 g_mirror_try_destroy(struct g_mirror_softc *sc)
1868 if (sc->sc_rootmount != NULL) {
1869 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1871 root_mount_rel(sc->sc_rootmount);
1872 sc->sc_rootmount = NULL;
1875 if (!g_mirror_can_destroy(sc)) {
1876 g_topology_unlock();
1879 sc->sc_geom->softc = NULL;
1880 sc->sc_sync.ds_geom->softc = NULL;
1881 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DRAIN) != 0) {
1882 g_topology_unlock();
1883 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1885 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
1886 sx_xunlock(&sc->sc_lock);
1887 wakeup(&sc->sc_worker);
1888 sc->sc_worker = NULL;
1890 g_topology_unlock();
1891 g_mirror_destroy_device(sc);
1900 g_mirror_worker(void *arg)
1902 struct g_mirror_softc *sc;
1903 struct g_mirror_event *ep;
1908 thread_lock(curthread);
1909 sched_prio(curthread, PRIBIO);
1910 thread_unlock(curthread);
1912 sx_xlock(&sc->sc_lock);
1914 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1916 * First take a look at events.
1917 * This is important to handle events before any I/O requests.
1919 ep = g_mirror_event_first(sc);
1921 g_mirror_event_remove(sc, ep);
1922 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1923 /* Update only device status. */
1925 "Running event for device %s.",
1928 g_mirror_update_device(sc, true);
1930 /* Update disk status. */
1931 G_MIRROR_DEBUG(3, "Running event for disk %s.",
1932 g_mirror_get_diskname(ep->e_disk));
1933 ep->e_error = g_mirror_update_disk(ep->e_disk,
1935 if (ep->e_error == 0)
1936 g_mirror_update_device(sc, false);
1938 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1939 KASSERT(ep->e_error == 0,
1940 ("Error cannot be handled."));
1941 g_mirror_event_free(ep);
1943 ep->e_flags |= G_MIRROR_EVENT_DONE;
1944 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1946 mtx_lock(&sc->sc_events_mtx);
1948 mtx_unlock(&sc->sc_events_mtx);
1951 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1952 if (g_mirror_try_destroy(sc)) {
1953 curthread->td_pflags &= ~TDP_GEOM;
1954 G_MIRROR_DEBUG(1, "Thread exiting.");
1958 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1963 * Check if we can mark array as CLEAN and if we can't take
1964 * how much seconds should we wait.
1966 timeout = g_mirror_idle(sc, -1);
1969 * Handle I/O requests.
1971 mtx_lock(&sc->sc_queue_mtx);
1972 bp = TAILQ_FIRST(&sc->sc_queue);
1974 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
1977 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1978 mtx_unlock(&sc->sc_queue_mtx);
1979 if (g_mirror_try_destroy(sc)) {
1980 curthread->td_pflags &= ~TDP_GEOM;
1981 G_MIRROR_DEBUG(1, "Thread exiting.");
1984 mtx_lock(&sc->sc_queue_mtx);
1985 if (!TAILQ_EMPTY(&sc->sc_queue)) {
1986 mtx_unlock(&sc->sc_queue_mtx);
1990 if (g_mirror_event_first(sc) != NULL) {
1991 mtx_unlock(&sc->sc_queue_mtx);
1994 sx_xunlock(&sc->sc_lock);
1995 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
1997 sx_xlock(&sc->sc_lock);
1998 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
2001 mtx_unlock(&sc->sc_queue_mtx);
2003 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
2004 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
2006 * Handle completion of the first half (the read) of a
2007 * block synchronization operation.
2009 g_mirror_sync_request(sc, bp);
2010 } else if (bp->bio_to != sc->sc_provider) {
2011 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
2013 * Handle completion of a regular I/O request.
2015 g_mirror_regular_request(sc, bp);
2016 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
2018 * Handle completion of the second half (the
2019 * write) of a block synchronization operation.
2021 g_mirror_sync_request(sc, bp);
2024 ("Invalid request cflags=0x%hx to=%s.",
2025 bp->bio_cflags, bp->bio_to->name));
2029 * Initiate an I/O request.
2031 g_mirror_register_request(sc, bp);
2033 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
2038 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
2041 sx_assert(&sc->sc_lock, SX_LOCKED);
2043 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
2045 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2046 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as dirty.",
2047 g_mirror_get_diskname(disk), sc->sc_name);
2048 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
2049 } else if (sc->sc_idle &&
2050 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2051 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as clean.",
2052 g_mirror_get_diskname(disk), sc->sc_name);
2053 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2058 g_mirror_sync_reinit(const struct g_mirror_disk *disk, struct bio *bp,
2064 data = bp->bio_data;
2065 idx = (int)(uintptr_t)bp->bio_caller1;
2068 bp->bio_cmd = BIO_READ;
2069 bp->bio_data = data;
2070 bp->bio_done = g_mirror_sync_done;
2071 bp->bio_from = disk->d_sync.ds_consumer;
2072 bp->bio_to = disk->d_softc->sc_provider;
2073 bp->bio_caller1 = (void *)(uintptr_t)idx;
2074 bp->bio_offset = offset;
2075 bp->bio_length = MIN(maxphys,
2076 disk->d_softc->sc_mediasize - bp->bio_offset);
2080 g_mirror_sync_start(struct g_mirror_disk *disk)
2082 struct g_mirror_softc *sc;
2083 struct g_mirror_disk_sync *sync;
2084 struct g_consumer *cp;
2088 g_topology_assert_not();
2090 sync = &disk->d_sync;
2091 sx_assert(&sc->sc_lock, SX_LOCKED);
2093 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2094 ("Disk %s is not marked for synchronization.",
2095 g_mirror_get_diskname(disk)));
2096 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2097 ("Device not in RUNNING state (%s, %u).", sc->sc_name,
2100 sx_xunlock(&sc->sc_lock);
2102 cp = g_new_consumer(sc->sc_sync.ds_geom);
2103 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2104 error = g_attach(cp, sc->sc_provider);
2106 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
2107 error = g_access(cp, 1, 0, 0);
2108 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
2109 g_topology_unlock();
2110 sx_xlock(&sc->sc_lock);
2112 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
2113 g_mirror_get_diskname(disk));
2114 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
2115 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
2116 KASSERT(sync->ds_consumer == NULL,
2117 ("Sync consumer already exists (device=%s, disk=%s).",
2118 sc->sc_name, g_mirror_get_diskname(disk)));
2120 sync->ds_consumer = cp;
2121 sync->ds_consumer->private = disk;
2122 sync->ds_consumer->index = 0;
2125 * Allocate memory for synchronization bios and initialize them.
2127 sync->ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
2128 M_MIRROR, M_WAITOK);
2129 for (i = 0; i < g_mirror_syncreqs; i++) {
2131 sync->ds_bios[i] = bp;
2133 bp->bio_data = malloc(maxphys, M_MIRROR, M_WAITOK);
2134 bp->bio_caller1 = (void *)(uintptr_t)i;
2135 g_mirror_sync_reinit(disk, bp, sync->ds_offset);
2136 sync->ds_offset += bp->bio_length;
2139 /* Increase the number of disks in SYNCHRONIZING state. */
2140 sc->sc_sync.ds_ndisks++;
2141 /* Set the number of in-flight synchronization requests. */
2142 sync->ds_inflight = g_mirror_syncreqs;
2145 * Fire off first synchronization requests.
2147 for (i = 0; i < g_mirror_syncreqs; i++) {
2148 bp = sync->ds_bios[i];
2149 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
2150 sync->ds_consumer->index++;
2152 * Delay the request if it is colliding with a regular request.
2154 if (g_mirror_regular_collision(sc, bp))
2155 g_mirror_sync_delay(sc, bp);
2157 g_io_request(bp, sync->ds_consumer);
2162 * Stop synchronization process.
2163 * type: 0 - synchronization finished
2164 * 1 - synchronization stopped
2167 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
2169 struct g_mirror_softc *sc;
2170 struct g_consumer *cp;
2172 g_topology_assert_not();
2174 sx_assert(&sc->sc_lock, SX_LOCKED);
2176 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2177 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2178 g_mirror_disk_state2str(disk->d_state)));
2179 if (disk->d_sync.ds_consumer == NULL)
2183 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2184 sc->sc_name, g_mirror_get_diskname(disk));
2185 } else /* if (type == 1) */ {
2186 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2187 sc->sc_name, g_mirror_get_diskname(disk));
2189 g_mirror_regular_release(sc);
2190 free(disk->d_sync.ds_bios, M_MIRROR);
2191 disk->d_sync.ds_bios = NULL;
2192 cp = disk->d_sync.ds_consumer;
2193 disk->d_sync.ds_consumer = NULL;
2194 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2195 sc->sc_sync.ds_ndisks--;
2196 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2198 g_mirror_kill_consumer(sc, cp);
2199 g_topology_unlock();
2200 sx_xlock(&sc->sc_lock);
2204 g_mirror_launch_provider(struct g_mirror_softc *sc)
2206 struct g_mirror_disk *disk;
2207 struct g_provider *pp, *dp;
2209 sx_assert(&sc->sc_lock, SX_LOCKED);
2212 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2213 pp->flags |= G_PF_DIRECT_RECEIVE;
2214 pp->mediasize = sc->sc_mediasize;
2215 pp->sectorsize = sc->sc_sectorsize;
2217 pp->stripeoffset = 0;
2219 /* Splitting of unmapped BIO's could work but isn't implemented now */
2220 if (sc->sc_balance != G_MIRROR_BALANCE_SPLIT)
2221 pp->flags |= G_PF_ACCEPT_UNMAPPED;
2223 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2224 if (disk->d_consumer && disk->d_consumer->provider) {
2225 dp = disk->d_consumer->provider;
2226 if (dp->stripesize > pp->stripesize) {
2227 pp->stripesize = dp->stripesize;
2228 pp->stripeoffset = dp->stripeoffset;
2230 /* A provider underneath us doesn't support unmapped */
2231 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
2232 G_MIRROR_DEBUG(0, "Cancelling unmapped "
2233 "because of %s.", dp->name);
2234 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
2240 sc->sc_provider = pp;
2241 g_error_provider(pp, 0);
2242 g_topology_unlock();
2243 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2244 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2245 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2246 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2247 g_mirror_sync_start(disk);
2252 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2254 struct g_mirror_disk *disk;
2257 g_topology_assert_not();
2258 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2261 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2262 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2263 g_mirror_sync_stop(disk, 1);
2267 g_error_provider(sc->sc_provider, ENXIO);
2268 mtx_lock(&sc->sc_queue_mtx);
2269 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
2270 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
2272 * Abort any pending I/O that wasn't generated by us.
2273 * Synchronization requests and requests destined for individual
2274 * mirror components can be destroyed immediately.
2276 if (bp->bio_to == sc->sc_provider &&
2277 bp->bio_from->geom != sc->sc_sync.ds_geom) {
2278 g_io_deliver(bp, ENXIO);
2280 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
2281 free(bp->bio_data, M_MIRROR);
2285 mtx_unlock(&sc->sc_queue_mtx);
2286 g_wither_provider(sc->sc_provider, ENXIO);
2287 sc->sc_provider = NULL;
2288 G_MIRROR_DEBUG(0, "Device %s: provider destroyed.", sc->sc_name);
2289 g_topology_unlock();
2293 g_mirror_go(void *arg)
2295 struct g_mirror_softc *sc;
2296 struct g_mirror_event *ep;
2299 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2300 ep = sc->sc_timeout_event;
2301 sc->sc_timeout_event = NULL;
2302 g_mirror_event_dispatch(ep, sc, 0,
2303 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2307 g_mirror_timeout_drain(struct g_mirror_softc *sc)
2309 sx_assert(&sc->sc_lock, SX_XLOCKED);
2311 callout_drain(&sc->sc_callout);
2312 g_mirror_event_free(sc->sc_timeout_event);
2313 sc->sc_timeout_event = NULL;
2317 g_mirror_determine_state(struct g_mirror_disk *disk)
2319 struct g_mirror_softc *sc;
2323 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2324 if ((disk->d_flags &
2325 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0 &&
2326 (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 ||
2327 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0)) {
2328 /* Disk does not need synchronization. */
2329 state = G_MIRROR_DISK_STATE_ACTIVE;
2332 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2334 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2336 * We can start synchronization from
2337 * the stored offset.
2339 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2341 state = G_MIRROR_DISK_STATE_STALE;
2344 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2346 * Reset all synchronization data for this disk,
2347 * because if it even was synchronized, it was
2348 * synchronized to disks with different syncid.
2350 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2351 disk->d_sync.ds_offset = 0;
2352 disk->d_sync.ds_offset_done = 0;
2353 disk->d_sync.ds_syncid = sc->sc_syncid;
2354 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2355 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2356 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2358 state = G_MIRROR_DISK_STATE_STALE;
2360 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2362 * Not good, NOT GOOD!
2363 * It means that mirror was started on stale disks
2364 * and more fresh disk just arrive.
2365 * If there were writes, mirror is broken, sorry.
2366 * I think the best choice here is don't touch
2367 * this disk and inform the user loudly.
2369 G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2370 "disk (%s) arrives!! It will not be connected to the "
2371 "running device.", sc->sc_name,
2372 g_mirror_get_diskname(disk));
2373 g_mirror_destroy_disk(disk);
2374 state = G_MIRROR_DISK_STATE_NONE;
2375 /* Return immediately, because disk was destroyed. */
2378 G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2379 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2384 * Update device state.
2387 g_mirror_update_device(struct g_mirror_softc *sc, bool force)
2389 struct g_mirror_disk *disk;
2392 sx_assert(&sc->sc_lock, SX_XLOCKED);
2394 switch (sc->sc_state) {
2395 case G_MIRROR_DEVICE_STATE_STARTING:
2397 struct g_mirror_disk *pdisk, *tdisk;
2398 const char *mismatch;
2399 uintmax_t found, newest;
2400 u_int dirty, ndisks;
2402 /* Pre-flight checks */
2403 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2405 * Confirm we already detected the newest genid.
2407 KASSERT(sc->sc_genid >= disk->d_genid,
2408 ("%s: found newer genid %u (sc:%p had %u).", __func__,
2409 disk->d_genid, sc, sc->sc_genid));
2411 /* Kick out any previously tasted stale components. */
2412 if (disk->d_genid < sc->sc_genid) {
2413 G_MIRROR_DEBUG(0, "Stale 'genid' field on %s "
2414 "(device %s) (component=%u latest=%u), skipping.",
2415 g_mirror_get_diskname(disk), sc->sc_name,
2416 disk->d_genid, sc->sc_genid);
2417 g_mirror_destroy_disk(disk);
2418 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2423 * Confirm we already detected the newest syncid.
2425 KASSERT(sc->sc_syncid >= disk->d_sync.ds_syncid,
2426 ("%s: found newer syncid %u (sc:%p had %u).",
2427 __func__, disk->d_sync.ds_syncid, sc,
2430 #define DETECT_MISMATCH(field, name) \
2431 if (mismatch == NULL && \
2432 disk->d_init_ ## field != sc->sc_ ## field) { \
2434 found = (intmax_t)disk->d_init_ ## field; \
2435 newest = (intmax_t)sc->sc_ ## field; \
2438 DETECT_MISMATCH(ndisks, "md_all");
2439 DETECT_MISMATCH(balance, "md_balance");
2440 DETECT_MISMATCH(slice, "md_slice");
2441 DETECT_MISMATCH(mediasize, "md_mediasize");
2442 #undef DETECT_MISMATCH
2443 if (mismatch != NULL) {
2444 G_MIRROR_DEBUG(0, "Found a mismatching '%s' "
2445 "field on %s (device %s) (found=%ju "
2446 "newest=%ju).", mismatch,
2447 g_mirror_get_diskname(disk), sc->sc_name,
2449 g_mirror_destroy_disk(disk);
2450 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2455 KASSERT(sc->sc_provider == NULL,
2456 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2458 * Are we ready? If the timeout (force is true) has expired, and
2459 * any disks are present, then yes. If we're permitted to launch
2460 * before the timeout has expired and the expected number of
2461 * current-generation mirror disks have been tasted, then yes.
2463 ndisks = g_mirror_ndisks(sc, -1);
2464 if ((force && ndisks > 0) ||
2465 (g_launch_mirror_before_timeout && ndisks == sc->sc_ndisks)) {
2467 } else if (ndisks == 0) {
2469 * Disks went down in starting phase, so destroy
2472 g_mirror_timeout_drain(sc);
2473 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2474 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2476 root_mount_rel(sc->sc_rootmount);
2477 sc->sc_rootmount = NULL;
2484 * Activate all disks with the biggest syncid.
2488 * If 'force' is true, we have been called due to
2489 * timeout, so don't bother canceling timeout.
2492 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2493 if ((disk->d_flags &
2494 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2499 /* No valid disks found, destroy device. */
2500 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2501 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2502 __LINE__, sc->sc_rootmount);
2503 root_mount_rel(sc->sc_rootmount);
2504 sc->sc_rootmount = NULL;
2508 /* Cancel timeout. */
2509 g_mirror_timeout_drain(sc);
2513 * Here we need to look for dirty disks and if all disks
2514 * with the biggest syncid are dirty, we have to choose
2515 * one with the biggest priority and rebuild the rest.
2518 * Find the number of dirty disks with the biggest syncid.
2519 * Find the number of disks with the biggest syncid.
2520 * While here, find a disk with the biggest priority.
2524 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2525 if (disk->d_sync.ds_syncid != sc->sc_syncid)
2527 if ((disk->d_flags &
2528 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2532 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2534 if (pdisk == NULL ||
2535 pdisk->d_priority < disk->d_priority) {
2541 /* No dirty disks at all, great. */
2542 } else if (dirty == ndisks) {
2544 * Force synchronization for all dirty disks except one
2545 * with the biggest priority.
2547 KASSERT(pdisk != NULL, ("pdisk == NULL"));
2548 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2549 "master disk for synchronization.",
2550 g_mirror_get_diskname(pdisk), sc->sc_name);
2551 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2552 if (disk->d_sync.ds_syncid != sc->sc_syncid)
2554 if ((disk->d_flags &
2555 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2558 KASSERT((disk->d_flags &
2559 G_MIRROR_DISK_FLAG_DIRTY) != 0,
2560 ("Disk %s isn't marked as dirty.",
2561 g_mirror_get_diskname(disk)));
2562 /* Skip the disk with the biggest priority. */
2565 disk->d_sync.ds_syncid = 0;
2567 } else if (dirty < ndisks) {
2569 * Force synchronization for all dirty disks.
2570 * We have some non-dirty disks.
2572 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2573 if (disk->d_sync.ds_syncid != sc->sc_syncid)
2575 if ((disk->d_flags &
2576 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2579 if ((disk->d_flags &
2580 G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2583 disk->d_sync.ds_syncid = 0;
2590 /* Remember to bump syncid on first write. */
2591 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2593 state = G_MIRROR_DEVICE_STATE_RUNNING;
2594 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2595 sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2596 g_mirror_device_state2str(state));
2597 sc->sc_state = state;
2598 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2599 state = g_mirror_determine_state(disk);
2600 g_mirror_event_send(disk, state,
2601 G_MIRROR_EVENT_DONTWAIT);
2602 if (state == G_MIRROR_DISK_STATE_STALE)
2603 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2607 case G_MIRROR_DEVICE_STATE_RUNNING:
2608 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2609 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2611 * No usable disks, so destroy the device.
2613 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2615 } else if (g_mirror_ndisks(sc,
2616 G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2617 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2619 * We have active disks, launch provider if it doesn't
2622 if (sc->sc_provider == NULL)
2623 g_mirror_launch_provider(sc);
2624 if (sc->sc_rootmount != NULL) {
2625 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2626 __LINE__, sc->sc_rootmount);
2627 root_mount_rel(sc->sc_rootmount);
2628 sc->sc_rootmount = NULL;
2632 * Genid should be bumped immediately, so do it here.
2634 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2635 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2636 g_mirror_bump_genid(sc);
2638 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID_NOW) != 0) {
2639 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID_NOW;
2640 g_mirror_bump_syncid(sc);
2644 KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2645 sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2651 * Update disk state and device state if needed.
2653 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \
2654 "Disk %s state changed from %s to %s (device %s).", \
2655 g_mirror_get_diskname(disk), \
2656 g_mirror_disk_state2str(disk->d_state), \
2657 g_mirror_disk_state2str(state), sc->sc_name)
2659 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2661 struct g_mirror_softc *sc;
2664 sx_assert(&sc->sc_lock, SX_XLOCKED);
2667 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2668 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2669 g_mirror_disk_state2str(state));
2671 case G_MIRROR_DISK_STATE_NEW:
2673 * Possible scenarios:
2674 * 1. New disk arrive.
2676 /* Previous state should be NONE. */
2677 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2678 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2679 g_mirror_disk_state2str(disk->d_state)));
2680 DISK_STATE_CHANGED();
2682 disk->d_state = state;
2684 if (LIST_EMPTY(&sc->sc_disks))
2685 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2687 struct g_mirror_disk *dp;
2689 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2690 if (disk->d_priority >= dp->d_priority) {
2691 LIST_INSERT_BEFORE(dp, disk, d_next);
2695 if (LIST_NEXT(dp, d_next) == NULL)
2699 LIST_INSERT_AFTER(dp, disk, d_next);
2701 g_topology_unlock();
2702 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2703 sc->sc_name, g_mirror_get_diskname(disk));
2704 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2706 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2707 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2708 g_mirror_device_state2str(sc->sc_state),
2709 g_mirror_get_diskname(disk),
2710 g_mirror_disk_state2str(disk->d_state)));
2711 state = g_mirror_determine_state(disk);
2712 if (state != G_MIRROR_DISK_STATE_NONE)
2715 case G_MIRROR_DISK_STATE_ACTIVE:
2717 * Possible scenarios:
2718 * 1. New disk does not need synchronization.
2719 * 2. Synchronization process finished successfully.
2721 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2722 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2723 g_mirror_device_state2str(sc->sc_state),
2724 g_mirror_get_diskname(disk),
2725 g_mirror_disk_state2str(disk->d_state)));
2726 /* Previous state should be NEW or SYNCHRONIZING. */
2727 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2728 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2729 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2730 g_mirror_disk_state2str(disk->d_state)));
2731 DISK_STATE_CHANGED();
2733 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2734 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2735 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2736 g_mirror_sync_stop(disk, 0);
2738 disk->d_state = state;
2739 disk->d_sync.ds_offset = 0;
2740 disk->d_sync.ds_offset_done = 0;
2741 g_mirror_update_idle(sc, disk);
2742 g_mirror_update_metadata(disk);
2743 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2744 sc->sc_name, g_mirror_get_diskname(disk));
2746 case G_MIRROR_DISK_STATE_STALE:
2748 * Possible scenarios:
2749 * 1. Stale disk was connected.
2751 /* Previous state should be NEW. */
2752 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2753 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2754 g_mirror_disk_state2str(disk->d_state)));
2755 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2756 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2757 g_mirror_device_state2str(sc->sc_state),
2758 g_mirror_get_diskname(disk),
2759 g_mirror_disk_state2str(disk->d_state)));
2761 * STALE state is only possible if device is marked
2764 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2765 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2766 g_mirror_device_state2str(sc->sc_state),
2767 g_mirror_get_diskname(disk),
2768 g_mirror_disk_state2str(disk->d_state)));
2769 DISK_STATE_CHANGED();
2771 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2772 disk->d_state = state;
2773 g_mirror_update_metadata(disk);
2774 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2775 sc->sc_name, g_mirror_get_diskname(disk));
2777 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2779 * Possible scenarios:
2780 * 1. Disk which needs synchronization was connected.
2782 /* Previous state should be NEW. */
2783 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2784 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2785 g_mirror_disk_state2str(disk->d_state)));
2786 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2787 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2788 g_mirror_device_state2str(sc->sc_state),
2789 g_mirror_get_diskname(disk),
2790 g_mirror_disk_state2str(disk->d_state)));
2791 DISK_STATE_CHANGED();
2793 if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2794 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2795 disk->d_state = state;
2796 if (sc->sc_provider != NULL) {
2797 g_mirror_sync_start(disk);
2798 g_mirror_update_metadata(disk);
2801 case G_MIRROR_DISK_STATE_DISCONNECTED:
2803 * Possible scenarios:
2804 * 1. Device wasn't running yet, but disk disappear.
2805 * 2. Disk was active and disapppear.
2806 * 3. Disk disappear during synchronization process.
2808 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2810 * Previous state should be ACTIVE, STALE or
2813 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2814 disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2815 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2816 ("Wrong disk state (%s, %s).",
2817 g_mirror_get_diskname(disk),
2818 g_mirror_disk_state2str(disk->d_state)));
2819 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2820 /* Previous state should be NEW. */
2821 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2822 ("Wrong disk state (%s, %s).",
2823 g_mirror_get_diskname(disk),
2824 g_mirror_disk_state2str(disk->d_state)));
2826 * Reset bumping syncid if disk disappeared in STARTING
2829 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2830 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2833 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2835 g_mirror_device_state2str(sc->sc_state),
2836 g_mirror_get_diskname(disk),
2837 g_mirror_disk_state2str(disk->d_state)));
2840 DISK_STATE_CHANGED();
2841 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2842 sc->sc_name, g_mirror_get_diskname(disk));
2844 g_mirror_destroy_disk(disk);
2846 case G_MIRROR_DISK_STATE_DESTROY:
2850 error = g_mirror_clear_metadata(disk);
2853 "Device %s: failed to clear metadata on %s: %d.",
2854 sc->sc_name, g_mirror_get_diskname(disk), error);
2857 DISK_STATE_CHANGED();
2858 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2859 sc->sc_name, g_mirror_get_diskname(disk));
2861 g_mirror_destroy_disk(disk);
2863 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2864 g_mirror_update_metadata(disk);
2869 KASSERT(1 == 0, ("Unknown state (%u).", state));
2874 #undef DISK_STATE_CHANGED
2877 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2879 struct g_provider *pp;
2883 g_topology_assert();
2885 error = g_access(cp, 1, 0, 0);
2889 g_topology_unlock();
2890 /* Metadata are stored on last sector. */
2891 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2894 g_access(cp, -1, 0, 0);
2896 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2897 cp->provider->name, error);
2901 /* Decode metadata. */
2902 error = mirror_metadata_decode(buf, md);
2904 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2906 if (md->md_version > G_MIRROR_VERSION) {
2908 "Kernel module is too old to handle metadata from %s.",
2909 cp->provider->name);
2913 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2914 cp->provider->name);
2922 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2923 struct g_mirror_metadata *md)
2926 G_MIRROR_DEBUG(2, "%s: md_did 0x%u disk %s device %s md_all 0x%x "
2927 "sc_ndisks 0x%x md_slice 0x%x sc_slice 0x%x md_balance 0x%x "
2928 "sc_balance 0x%x sc_mediasize 0x%jx pp_mediasize 0x%jx "
2929 "md_sectorsize 0x%x sc_sectorsize 0x%x md_mflags 0x%jx "
2930 "md_dflags 0x%jx md_syncid 0x%x md_genid 0x%x md_priority 0x%x "
2932 __func__, md->md_did, pp->name, sc->sc_name, md->md_all,
2933 sc->sc_ndisks, md->md_slice, sc->sc_slice, md->md_balance,
2934 sc->sc_balance, (uintmax_t)sc->sc_mediasize,
2935 (uintmax_t)pp->mediasize, md->md_sectorsize, sc->sc_sectorsize,
2936 (uintmax_t)md->md_mflags, (uintmax_t)md->md_dflags, md->md_syncid,
2937 md->md_genid, md->md_priority, sc->sc_state);
2939 if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2940 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2941 pp->name, md->md_did);
2944 if (sc->sc_mediasize > pp->mediasize) {
2946 "Invalid size of disk %s (device %s), skipping.", pp->name,
2950 if (md->md_sectorsize != sc->sc_sectorsize) {
2952 "Invalid '%s' field on disk %s (device %s), skipping.",
2953 "md_sectorsize", pp->name, sc->sc_name);
2956 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2958 "Invalid sector size of disk %s (device %s), skipping.",
2959 pp->name, sc->sc_name);
2962 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2964 "Invalid device flags on disk %s (device %s), skipping.",
2965 pp->name, sc->sc_name);
2968 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2970 "Invalid disk flags on disk %s (device %s), skipping.",
2971 pp->name, sc->sc_name);
2978 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2979 struct g_mirror_metadata *md)
2981 struct g_mirror_disk *disk;
2984 g_topology_assert_not();
2985 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2987 error = g_mirror_check_metadata(sc, pp, md);
2991 if (md->md_genid < sc->sc_genid) {
2992 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
2993 pp->name, sc->sc_name);
2998 * If the component disk we're tasting has newer metadata than the
2999 * STARTING gmirror device, refresh the device from the component.
3001 error = g_mirror_refresh_device(sc, pp, md);
3005 disk = g_mirror_init_disk(sc, pp, md, &error);
3008 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
3009 G_MIRROR_EVENT_WAIT);
3012 if (md->md_version < G_MIRROR_VERSION) {
3013 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
3014 pp->name, md->md_version, G_MIRROR_VERSION);
3015 g_mirror_update_metadata(disk);
3021 g_mirror_destroy_delayed(void *arg, int flag)
3023 struct g_mirror_softc *sc;
3026 if (flag == EV_CANCEL) {
3027 G_MIRROR_DEBUG(1, "Destroying canceled.");
3031 g_topology_unlock();
3032 sx_xlock(&sc->sc_lock);
3033 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
3034 ("DESTROY flag set on %s.", sc->sc_name));
3035 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_CLOSEWAIT) != 0,
3036 ("CLOSEWAIT flag not set on %s.", sc->sc_name));
3037 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
3038 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
3040 G_MIRROR_DEBUG(0, "Cannot destroy %s (error=%d).",
3041 sc->sc_name, error);
3042 sx_xunlock(&sc->sc_lock);
3048 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
3050 struct g_mirror_softc *sc;
3053 g_topology_assert();
3054 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
3058 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
3060 g_topology_unlock();
3061 sx_xlock(&sc->sc_lock);
3062 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
3063 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_CLOSEWAIT) != 0 ||
3064 LIST_EMPTY(&sc->sc_disks)) {
3065 if (acr > 0 || acw > 0 || ace > 0)
3069 sc->sc_provider_open += acr + acw + ace;
3070 if (pp->acw + acw == 0)
3071 g_mirror_idle(sc, 0);
3072 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_CLOSEWAIT) != 0 &&
3073 sc->sc_provider_open == 0)
3074 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK, sc, NULL);
3076 sx_xunlock(&sc->sc_lock);
3082 g_mirror_reinit_from_metadata(struct g_mirror_softc *sc,
3083 const struct g_mirror_metadata *md)
3086 sc->sc_genid = md->md_genid;
3087 sc->sc_syncid = md->md_syncid;
3089 sc->sc_slice = md->md_slice;
3090 sc->sc_balance = md->md_balance;
3091 sc->sc_mediasize = md->md_mediasize;
3092 sc->sc_ndisks = md->md_all;
3093 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_MASK;
3094 sc->sc_flags |= (md->md_mflags & G_MIRROR_DEVICE_FLAG_MASK);
3098 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md,
3101 struct g_mirror_softc *sc;
3105 g_topology_assert();
3106 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
3109 /* One disk is minimum. */
3115 gp = g_new_geomf(mp, "%s", md->md_name);
3116 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
3117 gp->start = g_mirror_start;
3118 gp->orphan = g_mirror_orphan;
3119 gp->access = g_mirror_access;
3120 gp->dumpconf = g_mirror_dumpconf;
3123 sc->sc_id = md->md_mid;
3124 g_mirror_reinit_from_metadata(sc, md);
3125 sc->sc_sectorsize = md->md_sectorsize;
3128 sc->sc_last_write = time_uptime;
3131 sx_init(&sc->sc_lock, "gmirror:lock");
3132 TAILQ_INIT(&sc->sc_queue);
3133 mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
3134 TAILQ_INIT(&sc->sc_regular_delayed);
3135 TAILQ_INIT(&sc->sc_inflight);
3136 TAILQ_INIT(&sc->sc_sync_delayed);
3137 LIST_INIT(&sc->sc_disks);
3138 TAILQ_INIT(&sc->sc_events);
3139 mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
3140 callout_init(&sc->sc_callout, 1);
3141 mtx_init(&sc->sc_done_mtx, "gmirror:done", NULL, MTX_DEF);
3142 sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
3145 sc->sc_provider = NULL;
3146 sc->sc_provider_open = 0;
3148 * Synchronization geom.
3150 gp = g_new_geomf(mp, "%s.sync", md->md_name);
3152 gp->orphan = g_mirror_orphan;
3153 sc->sc_sync.ds_geom = gp;
3154 sc->sc_sync.ds_ndisks = 0;
3155 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
3156 "g_mirror %s", md->md_name);
3158 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
3160 g_destroy_geom(sc->sc_sync.ds_geom);
3161 g_destroy_geom(sc->sc_geom);
3162 g_mirror_free_device(sc);
3166 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
3167 sc->sc_name, sc->sc_ndisks, sc->sc_id);
3169 sc->sc_rootmount = root_mount_hold("GMIRROR");
3170 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
3173 * Schedule startup timeout.
3175 timeout = g_mirror_timeout * hz;
3176 sc->sc_timeout_event = malloc(sizeof(struct g_mirror_event), M_MIRROR,
3178 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
3179 return (sc->sc_geom);
3183 g_mirror_destroy(struct g_mirror_softc *sc, int how)
3185 struct g_mirror_disk *disk;
3187 g_topology_assert_not();
3188 sx_assert(&sc->sc_lock, SX_XLOCKED);
3190 if (sc->sc_provider_open != 0) {
3192 case G_MIRROR_DESTROY_SOFT:
3194 "Device %s is still open (%d).", sc->sc_name,
3195 sc->sc_provider_open);
3197 case G_MIRROR_DESTROY_DELAYED:
3199 "Device %s will be destroyed on last close.",
3201 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
3202 if (disk->d_state ==
3203 G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3204 g_mirror_sync_stop(disk, 1);
3207 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_CLOSEWAIT;
3209 case G_MIRROR_DESTROY_HARD:
3210 G_MIRROR_DEBUG(1, "Device %s is still open, so it "
3211 "can't be definitely removed.", sc->sc_name);
3215 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3216 sx_xunlock(&sc->sc_lock);
3219 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
3220 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DRAIN;
3221 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3222 sx_xunlock(&sc->sc_lock);
3223 mtx_lock(&sc->sc_queue_mtx);
3225 mtx_unlock(&sc->sc_queue_mtx);
3226 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3227 while (sc->sc_worker != NULL)
3228 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
3229 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3230 sx_xlock(&sc->sc_lock);
3231 g_mirror_destroy_device(sc);
3236 g_mirror_taste_orphan(struct g_consumer *cp)
3239 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3240 cp->provider->name));
3243 static struct g_geom *
3244 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3246 struct g_mirror_metadata md;
3247 struct g_mirror_softc *sc;
3248 struct g_consumer *cp;
3252 g_topology_assert();
3253 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3254 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
3256 gp = g_new_geomf(mp, "mirror:taste");
3258 * This orphan function should be never called.
3260 gp->orphan = g_mirror_taste_orphan;
3261 cp = g_new_consumer(gp);
3262 error = g_attach(cp, pp);
3264 error = g_mirror_read_metadata(cp, &md);
3267 g_destroy_consumer(cp);
3273 if (md.md_provider[0] != '\0' &&
3274 !g_compare_names(md.md_provider, pp->name))
3276 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3278 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3280 "Device %s: provider %s marked as inactive, skipping.",
3281 md.md_name, pp->name);
3284 if (g_mirror_debug >= 2)
3285 mirror_metadata_dump(&md);
3288 * Let's check if device already exists.
3291 LIST_FOREACH(gp, &mp->geom, geom) {
3295 if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC)
3297 if (sc->sc_sync.ds_geom == gp)
3299 if (strcmp(md.md_name, sc->sc_name) != 0)
3301 if (md.md_mid != sc->sc_id) {
3302 G_MIRROR_DEBUG(0, "Device %s already configured.",
3309 gp = g_mirror_create(mp, &md, G_MIRROR_TYPE_AUTOMATIC);
3311 G_MIRROR_DEBUG(0, "Cannot create device %s.",
3317 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3318 g_topology_unlock();
3319 sx_xlock(&sc->sc_lock);
3320 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
3321 error = g_mirror_add_disk(sc, pp, &md);
3322 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
3324 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3325 pp->name, gp->name, error);
3326 if (LIST_EMPTY(&sc->sc_disks)) {
3328 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3334 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3335 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3339 sx_xunlock(&sc->sc_lock);
3345 g_mirror_resize(struct g_consumer *cp)
3347 struct g_mirror_disk *disk;
3349 g_topology_assert();
3350 g_trace(G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name);
3355 g_topology_unlock();
3356 g_mirror_update_metadata(disk);
3361 g_mirror_destroy_geom(struct gctl_req *req __unused,
3362 struct g_class *mp __unused, struct g_geom *gp)
3364 struct g_mirror_softc *sc;
3367 g_topology_unlock();
3369 sx_xlock(&sc->sc_lock);
3371 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3373 sx_xunlock(&sc->sc_lock);
3379 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3380 struct g_consumer *cp, struct g_provider *pp)
3382 struct g_mirror_softc *sc;
3384 g_topology_assert();
3389 /* Skip synchronization geom. */
3390 if (gp == sc->sc_sync.ds_geom)
3394 } else if (cp != NULL) {
3395 struct g_mirror_disk *disk;
3400 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3401 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3402 sbuf_printf(sb, "%s<Synchronized>", indent);
3403 if (disk->d_sync.ds_offset == 0)
3406 sbuf_printf(sb, "%u%%",
3407 (u_int)((disk->d_sync.ds_offset * 100) /
3409 sbuf_cat(sb, "</Synchronized>\n");
3410 if (disk->d_sync.ds_offset > 0)
3411 sbuf_printf(sb, "%s<BytesSynced>%jd"
3412 "</BytesSynced>\n", indent,
3413 (intmax_t)disk->d_sync.ds_offset);
3415 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3416 disk->d_sync.ds_syncid);
3417 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3419 sbuf_printf(sb, "%s<Flags>", indent);
3420 if (disk->d_flags == 0)
3421 sbuf_cat(sb, "NONE");
3425 #define ADD_FLAG(flag, name) do { \
3426 if ((disk->d_flags & (flag)) != 0) { \
3428 sbuf_cat(sb, ", "); \
3431 sbuf_cat(sb, name); \
3434 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3435 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3436 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3437 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3439 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3440 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3443 sbuf_cat(sb, "</Flags>\n");
3444 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3446 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3447 g_mirror_disk_state2str(disk->d_state));
3449 sbuf_printf(sb, "%s<Type>", indent);
3450 switch (sc->sc_type) {
3451 case G_MIRROR_TYPE_AUTOMATIC:
3452 sbuf_cat(sb, "AUTOMATIC");
3454 case G_MIRROR_TYPE_MANUAL:
3455 sbuf_cat(sb, "MANUAL");
3458 sbuf_cat(sb, "UNKNOWN");
3461 sbuf_cat(sb, "</Type>\n");
3462 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3463 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3464 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3465 sbuf_printf(sb, "%s<Flags>", indent);
3466 if (sc->sc_flags == 0)
3467 sbuf_cat(sb, "NONE");
3471 #define ADD_FLAG(flag, name) do { \
3472 if ((sc->sc_flags & (flag)) != 0) { \
3474 sbuf_cat(sb, ", "); \
3477 sbuf_cat(sb, name); \
3480 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3481 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3484 sbuf_cat(sb, "</Flags>\n");
3485 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3486 (u_int)sc->sc_slice);
3487 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3488 balance_name(sc->sc_balance));
3489 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3491 sbuf_printf(sb, "%s<State>", indent);
3492 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3493 sbuf_printf(sb, "%s", "STARTING");
3494 else if (sc->sc_ndisks ==
3495 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3496 sbuf_printf(sb, "%s", "COMPLETE");
3498 sbuf_printf(sb, "%s", "DEGRADED");
3499 sbuf_cat(sb, "</State>\n");
3504 g_mirror_shutdown_post_sync(void *arg, int howto)
3507 struct g_geom *gp, *gp2;
3508 struct g_mirror_softc *sc;
3511 if (KERNEL_PANICKED())
3516 g_mirror_shutdown = 1;
3517 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3518 if ((sc = gp->softc) == NULL)
3520 /* Skip synchronization geom. */
3521 if (gp == sc->sc_sync.ds_geom)
3523 g_topology_unlock();
3524 sx_xlock(&sc->sc_lock);
3525 g_mirror_idle(sc, -1);
3527 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3529 sx_xunlock(&sc->sc_lock);
3532 g_topology_unlock();
3536 g_mirror_init(struct g_class *mp)
3539 g_mirror_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
3540 g_mirror_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
3541 if (g_mirror_post_sync == NULL)
3542 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3546 g_mirror_fini(struct g_class *mp)
3549 if (g_mirror_post_sync != NULL)
3550 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_mirror_post_sync);
3554 * Refresh the mirror device's metadata when gmirror encounters a newer
3555 * generation as the individual components are being added to the mirror set.
3558 g_mirror_refresh_device(struct g_mirror_softc *sc, const struct g_provider *pp,
3559 const struct g_mirror_metadata *md)
3562 g_topology_assert_not();
3563 sx_assert(&sc->sc_lock, SX_XLOCKED);
3565 KASSERT(sc->sc_genid <= md->md_genid,
3566 ("%s: attempted to refresh from stale component %s (device %s) "
3567 "(%u < %u).", __func__, pp->name, sc->sc_name, md->md_genid,
3570 if (sc->sc_genid > md->md_genid || (sc->sc_genid == md->md_genid &&
3571 sc->sc_syncid >= md->md_syncid))
3574 G_MIRROR_DEBUG(0, "Found newer version for device %s (genid: curr=%u "
3575 "new=%u; syncid: curr=%u new=%u; ndisks: curr=%u new=%u; "
3576 "provider=%s).", sc->sc_name, sc->sc_genid, md->md_genid,
3577 sc->sc_syncid, md->md_syncid, sc->sc_ndisks, md->md_all, pp->name);
3579 if (sc->sc_state != G_MIRROR_DEVICE_STATE_STARTING) {
3580 /* Probable data corruption detected */
3581 G_MIRROR_DEBUG(0, "Cannot refresh metadata in %s state "
3582 "(device=%s genid=%u). A stale mirror device was launched.",
3583 g_mirror_device_state2str(sc->sc_state), sc->sc_name,
3589 g_mirror_reinit_from_metadata(sc, md);
3591 G_MIRROR_DEBUG(1, "Refresh device %s (id=%u, state=%s) from disk %s "
3592 "(genid=%u syncid=%u md_all=%u).", sc->sc_name, md->md_mid,
3593 g_mirror_device_state2str(sc->sc_state), pp->name, md->md_genid,
3594 md->md_syncid, (unsigned)md->md_all);
3599 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);
3600 MODULE_VERSION(geom_mirror, 0);