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)
1040 * If there is only one active disk we want to double-check that
1041 * it is, in fact, the disk that we already tried. This is
1042 * necessary because we might have just lost a race with a
1043 * removal of the tried disk (likely because of the same error)
1044 * and the only remaining disk is still viable for a retry.
1046 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1 &&
1048 disk->d_state == G_MIRROR_DISK_STATE_ACTIVE) {
1049 g_io_deliver(pbp, pbp->bio_error);
1052 mtx_lock(&sc->sc_queue_mtx);
1053 TAILQ_INSERT_TAIL(&sc->sc_queue, pbp, bio_queue);
1054 mtx_unlock(&sc->sc_queue_mtx);
1055 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1063 if (pbp->bio_children == 0) {
1065 * All requests failed.
1067 } else if (pbp->bio_inbed < pbp->bio_children) {
1070 } else if (pbp->bio_children == pbp->bio_inbed) {
1071 /* Some requests succeeded. */
1073 pbp->bio_completed = pbp->bio_length;
1075 if (pbp->bio_cmd == BIO_WRITE || pbp->bio_cmd == BIO_DELETE) {
1076 TAILQ_REMOVE(&sc->sc_inflight, pbp, bio_queue);
1077 /* Release delayed sync requests if possible. */
1078 g_mirror_sync_release(sc);
1080 g_io_deliver(pbp, pbp->bio_error);
1083 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
1089 g_mirror_sync_done(struct bio *bp)
1091 struct g_mirror_softc *sc;
1093 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
1094 sc = bp->bio_from->geom->softc;
1095 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
1096 mtx_lock(&sc->sc_queue_mtx);
1097 TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
1098 mtx_unlock(&sc->sc_queue_mtx);
1103 g_mirror_candelete(struct bio *bp)
1105 struct g_mirror_softc *sc;
1106 struct g_mirror_disk *disk;
1109 sc = bp->bio_to->private;
1110 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1111 if (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE)
1115 g_handleattr(bp, "GEOM::candelete", &val, sizeof(val));
1119 g_mirror_kernel_dump(struct bio *bp)
1121 struct g_mirror_softc *sc;
1122 struct g_mirror_disk *disk;
1124 struct g_kerneldump *gkd;
1127 * We configure dumping to the first component, because this component
1128 * will be used for reading with 'prefer' balance algorithm.
1129 * If the component with the highest priority is currently disconnected
1130 * we will not be able to read the dump after the reboot if it will be
1131 * connected and synchronized later. Can we do something better?
1133 sc = bp->bio_to->private;
1134 disk = LIST_FIRST(&sc->sc_disks);
1136 gkd = (struct g_kerneldump *)bp->bio_data;
1137 if (gkd->length > bp->bio_to->mediasize)
1138 gkd->length = bp->bio_to->mediasize;
1139 cbp = g_clone_bio(bp);
1141 g_io_deliver(bp, ENOMEM);
1144 cbp->bio_done = g_std_done;
1145 g_io_request(cbp, disk->d_consumer);
1146 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1147 g_mirror_get_diskname(disk));
1151 g_mirror_start(struct bio *bp)
1153 struct g_mirror_softc *sc;
1155 sc = bp->bio_to->private;
1157 * If sc == NULL or there are no valid disks, provider's error
1158 * should be set and g_mirror_start() should not be called at all.
1160 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1161 ("Provider's error should be set (error=%d)(mirror=%s).",
1162 bp->bio_to->error, bp->bio_to->name));
1163 G_MIRROR_LOGREQ(3, bp, "Request received.");
1165 switch (bp->bio_cmd) {
1173 if (!strcmp(bp->bio_attribute, "GEOM::candelete")) {
1174 g_mirror_candelete(bp);
1176 } else if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1177 g_mirror_kernel_dump(bp);
1182 g_io_deliver(bp, EOPNOTSUPP);
1185 mtx_lock(&sc->sc_queue_mtx);
1186 if (bp->bio_to->error != 0) {
1187 mtx_unlock(&sc->sc_queue_mtx);
1188 g_io_deliver(bp, bp->bio_to->error);
1191 TAILQ_INSERT_TAIL(&sc->sc_queue, bp, bio_queue);
1192 mtx_unlock(&sc->sc_queue_mtx);
1193 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1198 * Return TRUE if the given request is colliding with a in-progress
1199 * synchronization request.
1202 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1204 struct g_mirror_disk *disk;
1206 off_t rstart, rend, sstart, send;
1209 if (sc->sc_sync.ds_ndisks == 0)
1211 rstart = bp->bio_offset;
1212 rend = bp->bio_offset + bp->bio_length;
1213 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1214 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1216 for (i = 0; i < g_mirror_syncreqs; i++) {
1217 sbp = disk->d_sync.ds_bios[i];
1220 sstart = sbp->bio_offset;
1221 send = sbp->bio_offset + sbp->bio_length;
1222 if (rend > sstart && rstart < send)
1230 * Return TRUE if the given sync request is colliding with a in-progress regular
1234 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1236 off_t rstart, rend, sstart, send;
1239 if (sc->sc_sync.ds_ndisks == 0)
1241 sstart = sbp->bio_offset;
1242 send = sbp->bio_offset + sbp->bio_length;
1243 TAILQ_FOREACH(bp, &sc->sc_inflight, bio_queue) {
1244 rstart = bp->bio_offset;
1245 rend = bp->bio_offset + bp->bio_length;
1246 if (rend > sstart && rstart < send)
1253 * Puts regular request onto delayed queue.
1256 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1259 G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1260 TAILQ_INSERT_TAIL(&sc->sc_regular_delayed, bp, bio_queue);
1264 * Puts synchronization request onto delayed queue.
1267 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1270 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1271 TAILQ_INSERT_TAIL(&sc->sc_sync_delayed, bp, bio_queue);
1275 * Requeue delayed regular requests.
1278 g_mirror_regular_release(struct g_mirror_softc *sc)
1282 if ((bp = TAILQ_FIRST(&sc->sc_regular_delayed)) == NULL)
1284 if (g_mirror_sync_collision(sc, bp))
1287 G_MIRROR_DEBUG(2, "Requeuing regular requests after collision.");
1288 mtx_lock(&sc->sc_queue_mtx);
1289 TAILQ_CONCAT(&sc->sc_regular_delayed, &sc->sc_queue, bio_queue);
1290 TAILQ_SWAP(&sc->sc_regular_delayed, &sc->sc_queue, bio, bio_queue);
1291 mtx_unlock(&sc->sc_queue_mtx);
1295 * Releases delayed sync requests which don't collide anymore with regular
1299 g_mirror_sync_release(struct g_mirror_softc *sc)
1301 struct bio *bp, *bp2;
1303 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed, bio_queue, bp2) {
1304 if (g_mirror_regular_collision(sc, bp))
1306 TAILQ_REMOVE(&sc->sc_sync_delayed, bp, bio_queue);
1307 G_MIRROR_LOGREQ(2, bp,
1308 "Releasing delayed synchronization request.");
1309 g_io_request(bp, bp->bio_from);
1314 * Free a synchronization request and clear its slot in the array.
1317 g_mirror_sync_request_free(struct g_mirror_disk *disk, struct bio *bp)
1321 if (disk != NULL && disk->d_sync.ds_bios != NULL) {
1322 idx = (int)(uintptr_t)bp->bio_caller1;
1323 KASSERT(disk->d_sync.ds_bios[idx] == bp,
1324 ("unexpected sync BIO at %p:%d", disk, idx));
1325 disk->d_sync.ds_bios[idx] = NULL;
1327 free(bp->bio_data, M_MIRROR);
1332 * Handle synchronization requests.
1333 * Every synchronization request is a two-step process: first, a read request is
1334 * sent to the mirror provider via the sync consumer. If that request completes
1335 * successfully, it is converted to a write and sent to the disk being
1336 * synchronized. If the write also completes successfully, the synchronization
1337 * offset is advanced and a new read request is submitted.
1340 g_mirror_sync_request(struct g_mirror_softc *sc, struct bio *bp)
1342 struct g_mirror_disk *disk;
1343 struct g_mirror_disk_sync *sync;
1345 KASSERT((bp->bio_cmd == BIO_READ &&
1346 bp->bio_from->geom == sc->sc_sync.ds_geom) ||
1347 (bp->bio_cmd == BIO_WRITE && bp->bio_from->geom == sc->sc_geom),
1348 ("Sync BIO %p with unexpected origin", bp));
1350 bp->bio_from->index--;
1351 disk = bp->bio_from->private;
1353 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1355 g_mirror_kill_consumer(sc, bp->bio_from);
1356 g_topology_unlock();
1357 g_mirror_sync_request_free(NULL, bp);
1358 sx_xlock(&sc->sc_lock);
1362 sync = &disk->d_sync;
1365 * Synchronization request.
1367 switch (bp->bio_cmd) {
1369 struct g_consumer *cp;
1371 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_sync_request_read,
1374 if (bp->bio_error != 0) {
1375 G_MIRROR_LOGREQ(0, bp,
1376 "Synchronization request failed (error=%d).",
1380 * The read error will trigger a syncid bump, so there's
1381 * no need to do that here.
1383 * The read error handling for regular requests will
1384 * retry the read from all active mirrors before passing
1385 * the error back up, so there's no need to retry here.
1387 g_mirror_sync_request_free(disk, bp);
1388 g_mirror_event_send(disk,
1389 G_MIRROR_DISK_STATE_DISCONNECTED,
1390 G_MIRROR_EVENT_DONTWAIT);
1393 G_MIRROR_LOGREQ(3, bp,
1394 "Synchronization request half-finished.");
1395 bp->bio_cmd = BIO_WRITE;
1397 cp = disk->d_consumer;
1398 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1399 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1400 cp->acr, cp->acw, cp->ace));
1402 g_io_request(bp, cp);
1409 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_sync_request_write,
1412 if (bp->bio_error != 0) {
1413 G_MIRROR_LOGREQ(0, bp,
1414 "Synchronization request failed (error=%d).",
1416 g_mirror_sync_request_free(disk, bp);
1417 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1418 g_mirror_event_send(disk,
1419 G_MIRROR_DISK_STATE_DISCONNECTED,
1420 G_MIRROR_EVENT_DONTWAIT);
1423 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1424 if (sync->ds_offset >= sc->sc_mediasize ||
1425 sync->ds_consumer == NULL ||
1426 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1427 /* Don't send more synchronization requests. */
1428 sync->ds_inflight--;
1429 g_mirror_sync_request_free(disk, bp);
1430 if (sync->ds_inflight > 0)
1432 if (sync->ds_consumer == NULL ||
1433 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1436 /* Disk up-to-date, activate it. */
1437 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1438 G_MIRROR_EVENT_DONTWAIT);
1442 /* Send next synchronization request. */
1443 g_mirror_sync_reinit(disk, bp, sync->ds_offset);
1444 sync->ds_offset += bp->bio_length;
1446 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1447 sync->ds_consumer->index++;
1450 * Delay the request if it is colliding with a regular request.
1452 if (g_mirror_regular_collision(sc, bp))
1453 g_mirror_sync_delay(sc, bp);
1455 g_io_request(bp, sync->ds_consumer);
1457 /* Requeue delayed requests if possible. */
1458 g_mirror_regular_release(sc);
1460 /* Find the smallest offset */
1461 offset = sc->sc_mediasize;
1462 for (i = 0; i < g_mirror_syncreqs; i++) {
1463 bp = sync->ds_bios[i];
1464 if (bp != NULL && bp->bio_offset < offset)
1465 offset = bp->bio_offset;
1467 if (g_mirror_sync_period > 0 &&
1468 time_uptime - sync->ds_update_ts > g_mirror_sync_period) {
1469 sync->ds_offset_done = offset;
1470 g_mirror_update_metadata(disk);
1471 sync->ds_update_ts = time_uptime;
1476 panic("Invalid I/O request %p", bp);
1481 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1483 struct g_mirror_disk *disk;
1484 struct g_consumer *cp;
1487 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1488 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1492 if (bp->bio_error == 0)
1493 bp->bio_error = ENXIO;
1494 g_io_deliver(bp, bp->bio_error);
1497 cbp = g_clone_bio(bp);
1499 if (bp->bio_error == 0)
1500 bp->bio_error = ENOMEM;
1501 g_io_deliver(bp, bp->bio_error);
1505 * Fill in the component buf structure.
1507 cp = disk->d_consumer;
1508 cbp->bio_done = g_mirror_done;
1509 cbp->bio_to = cp->provider;
1510 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1511 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1512 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1515 g_io_request(cbp, cp);
1519 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1521 struct g_mirror_disk *disk;
1522 struct g_consumer *cp;
1525 disk = g_mirror_get_disk(sc);
1527 if (bp->bio_error == 0)
1528 bp->bio_error = ENXIO;
1529 g_io_deliver(bp, bp->bio_error);
1532 cbp = g_clone_bio(bp);
1534 if (bp->bio_error == 0)
1535 bp->bio_error = ENOMEM;
1536 g_io_deliver(bp, bp->bio_error);
1540 * Fill in the component buf structure.
1542 cp = disk->d_consumer;
1543 cbp->bio_done = g_mirror_done;
1544 cbp->bio_to = cp->provider;
1545 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1546 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1547 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1550 g_io_request(cbp, cp);
1553 #define TRACK_SIZE (1 * 1024 * 1024)
1554 #define LOAD_SCALE 256
1555 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
1558 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1560 struct g_mirror_disk *disk, *dp;
1561 struct g_consumer *cp;
1565 /* Find a disk with the smallest load. */
1568 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1569 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1572 /* If disk head is precisely in position - highly prefer it. */
1573 if (dp->d_last_offset == bp->bio_offset)
1574 prio -= 2 * LOAD_SCALE;
1576 /* If disk head is close to position - prefer it. */
1577 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1578 prio -= 1 * LOAD_SCALE;
1584 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1585 cbp = g_clone_bio(bp);
1587 if (bp->bio_error == 0)
1588 bp->bio_error = ENOMEM;
1589 g_io_deliver(bp, bp->bio_error);
1593 * Fill in the component buf structure.
1595 cp = disk->d_consumer;
1596 cbp->bio_done = g_mirror_done;
1597 cbp->bio_to = cp->provider;
1598 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1599 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1600 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1603 /* Remember last head position */
1604 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1606 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1607 dp->load = (dp->d_consumer->index * LOAD_SCALE +
1610 g_io_request(cbp, cp);
1614 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1616 struct bio_queue queue;
1617 struct g_mirror_disk *disk;
1618 struct g_consumer *cp __diagused;
1620 off_t left, mod, offset, slice;
1624 if (bp->bio_length <= sc->sc_slice) {
1625 g_mirror_request_round_robin(sc, bp);
1628 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1629 slice = bp->bio_length / ndisks;
1630 mod = slice % sc->sc_provider->sectorsize;
1632 slice += sc->sc_provider->sectorsize - mod;
1634 * Allocate all bios before sending any request, so we can
1635 * return ENOMEM in nice and clean way.
1637 left = bp->bio_length;
1638 offset = bp->bio_offset;
1639 data = bp->bio_data;
1641 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1642 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1644 cbp = g_clone_bio(bp);
1646 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1647 TAILQ_REMOVE(&queue, cbp, bio_queue);
1650 if (bp->bio_error == 0)
1651 bp->bio_error = ENOMEM;
1652 g_io_deliver(bp, bp->bio_error);
1655 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
1656 cbp->bio_done = g_mirror_done;
1657 cbp->bio_caller1 = disk;
1658 cbp->bio_to = disk->d_consumer->provider;
1659 cbp->bio_offset = offset;
1660 cbp->bio_data = data;
1661 cbp->bio_length = MIN(left, slice);
1662 left -= cbp->bio_length;
1665 offset += cbp->bio_length;
1666 data += cbp->bio_length;
1668 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1669 TAILQ_REMOVE(&queue, cbp, bio_queue);
1670 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1671 disk = cbp->bio_caller1;
1672 cbp->bio_caller1 = NULL;
1673 cp = disk->d_consumer;
1674 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1675 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1676 cp->acr, cp->acw, cp->ace));
1677 disk->d_consumer->index++;
1678 g_io_request(cbp, disk->d_consumer);
1683 g_mirror_register_request(struct g_mirror_softc *sc, struct bio *bp)
1685 struct bio_queue queue;
1687 struct g_consumer *cp;
1688 struct g_mirror_disk *disk;
1690 sx_assert(&sc->sc_lock, SA_XLOCKED);
1693 * To avoid ordering issues, if a write is deferred because of a
1694 * collision with a sync request, all I/O is deferred until that
1695 * write is initiated.
1697 if (bp->bio_from->geom != sc->sc_sync.ds_geom &&
1698 !TAILQ_EMPTY(&sc->sc_regular_delayed)) {
1699 g_mirror_regular_delay(sc, bp);
1703 switch (bp->bio_cmd) {
1705 switch (sc->sc_balance) {
1706 case G_MIRROR_BALANCE_LOAD:
1707 g_mirror_request_load(sc, bp);
1709 case G_MIRROR_BALANCE_PREFER:
1710 g_mirror_request_prefer(sc, bp);
1712 case G_MIRROR_BALANCE_ROUND_ROBIN:
1713 g_mirror_request_round_robin(sc, bp);
1715 case G_MIRROR_BALANCE_SPLIT:
1716 g_mirror_request_split(sc, bp);
1723 * Delay the request if it is colliding with a synchronization
1726 if (g_mirror_sync_collision(sc, bp)) {
1727 g_mirror_regular_delay(sc, bp);
1732 g_mirror_unidle(sc);
1734 sc->sc_last_write = time_uptime;
1737 * Bump syncid on first write.
1739 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1740 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1741 g_mirror_bump_syncid(sc);
1745 * Allocate all bios before sending any request, so we can
1746 * return ENOMEM in nice and clean way.
1749 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1750 switch (disk->d_state) {
1751 case G_MIRROR_DISK_STATE_ACTIVE:
1753 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1754 if (bp->bio_offset >= disk->d_sync.ds_offset)
1760 if (bp->bio_cmd == BIO_DELETE &&
1761 (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE) == 0)
1763 cbp = g_clone_bio(bp);
1765 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1766 TAILQ_REMOVE(&queue, cbp, bio_queue);
1769 if (bp->bio_error == 0)
1770 bp->bio_error = ENOMEM;
1771 g_io_deliver(bp, bp->bio_error);
1774 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
1775 cbp->bio_done = g_mirror_done;
1776 cp = disk->d_consumer;
1777 cbp->bio_caller1 = cp;
1778 cbp->bio_to = cp->provider;
1779 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1780 ("Consumer %s not opened (r%dw%de%d).",
1781 cp->provider->name, cp->acr, cp->acw, cp->ace));
1783 if (TAILQ_EMPTY(&queue)) {
1784 KASSERT(bp->bio_cmd == BIO_DELETE,
1785 ("No consumers for regular request %p", bp));
1786 g_io_deliver(bp, EOPNOTSUPP);
1789 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1790 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1791 TAILQ_REMOVE(&queue, cbp, bio_queue);
1792 cp = cbp->bio_caller1;
1793 cbp->bio_caller1 = NULL;
1796 g_io_request(cbp, cp);
1799 * Put request onto inflight queue, so we can check if new
1800 * synchronization requests don't collide with it.
1802 TAILQ_INSERT_TAIL(&sc->sc_inflight, bp, bio_queue);
1807 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1808 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1810 cbp = g_clone_bio(bp);
1812 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1813 TAILQ_REMOVE(&queue, cbp, bio_queue);
1816 if (bp->bio_error == 0)
1817 bp->bio_error = ENOMEM;
1818 g_io_deliver(bp, bp->bio_error);
1821 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
1822 cbp->bio_done = g_mirror_done;
1823 cbp->bio_caller1 = disk;
1824 cbp->bio_to = disk->d_consumer->provider;
1826 KASSERT(!TAILQ_EMPTY(&queue),
1827 ("No consumers for regular request %p", bp));
1828 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
1829 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1830 TAILQ_REMOVE(&queue, cbp, bio_queue);
1831 disk = cbp->bio_caller1;
1832 cbp->bio_caller1 = NULL;
1833 cp = disk->d_consumer;
1834 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1835 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1836 cp->acr, cp->acw, cp->ace));
1838 g_io_request(cbp, cp);
1842 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1843 bp->bio_cmd, sc->sc_name));
1849 g_mirror_can_destroy(struct g_mirror_softc *sc)
1852 struct g_consumer *cp;
1854 g_topology_assert();
1856 if (gp->softc == NULL)
1858 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_TASTING) != 0)
1860 LIST_FOREACH(cp, &gp->consumer, consumer) {
1861 if (g_mirror_is_busy(sc, cp))
1864 gp = sc->sc_sync.ds_geom;
1865 LIST_FOREACH(cp, &gp->consumer, consumer) {
1866 if (g_mirror_is_busy(sc, cp))
1869 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1875 g_mirror_try_destroy(struct g_mirror_softc *sc)
1878 if (sc->sc_rootmount != NULL) {
1879 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1881 root_mount_rel(sc->sc_rootmount);
1882 sc->sc_rootmount = NULL;
1885 if (!g_mirror_can_destroy(sc)) {
1886 g_topology_unlock();
1889 sc->sc_geom->softc = NULL;
1890 sc->sc_sync.ds_geom->softc = NULL;
1891 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DRAIN) != 0) {
1892 g_topology_unlock();
1893 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1895 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
1896 sx_xunlock(&sc->sc_lock);
1897 wakeup(&sc->sc_worker);
1898 sc->sc_worker = NULL;
1900 g_topology_unlock();
1901 g_mirror_destroy_device(sc);
1910 g_mirror_worker(void *arg)
1912 struct g_mirror_softc *sc;
1913 struct g_mirror_event *ep;
1918 thread_lock(curthread);
1919 sched_prio(curthread, PRIBIO);
1920 thread_unlock(curthread);
1922 sx_xlock(&sc->sc_lock);
1924 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1926 * First take a look at events.
1927 * This is important to handle events before any I/O requests.
1929 ep = g_mirror_event_first(sc);
1931 g_mirror_event_remove(sc, ep);
1932 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1933 /* Update only device status. */
1935 "Running event for device %s.",
1938 g_mirror_update_device(sc, true);
1940 /* Update disk status. */
1941 G_MIRROR_DEBUG(3, "Running event for disk %s.",
1942 g_mirror_get_diskname(ep->e_disk));
1943 ep->e_error = g_mirror_update_disk(ep->e_disk,
1945 if (ep->e_error == 0)
1946 g_mirror_update_device(sc, false);
1948 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1949 KASSERT(ep->e_error == 0,
1950 ("Error cannot be handled."));
1951 g_mirror_event_free(ep);
1953 ep->e_flags |= G_MIRROR_EVENT_DONE;
1954 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1956 mtx_lock(&sc->sc_events_mtx);
1958 mtx_unlock(&sc->sc_events_mtx);
1961 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1962 if (g_mirror_try_destroy(sc)) {
1963 curthread->td_pflags &= ~TDP_GEOM;
1964 G_MIRROR_DEBUG(1, "Thread exiting.");
1968 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1973 * Check if we can mark array as CLEAN and if we can't take
1974 * how much seconds should we wait.
1976 timeout = g_mirror_idle(sc, -1);
1979 * Handle I/O requests.
1981 mtx_lock(&sc->sc_queue_mtx);
1982 bp = TAILQ_FIRST(&sc->sc_queue);
1984 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
1987 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1988 mtx_unlock(&sc->sc_queue_mtx);
1989 if (g_mirror_try_destroy(sc)) {
1990 curthread->td_pflags &= ~TDP_GEOM;
1991 G_MIRROR_DEBUG(1, "Thread exiting.");
1994 mtx_lock(&sc->sc_queue_mtx);
1995 if (!TAILQ_EMPTY(&sc->sc_queue)) {
1996 mtx_unlock(&sc->sc_queue_mtx);
2000 if (g_mirror_event_first(sc) != NULL) {
2001 mtx_unlock(&sc->sc_queue_mtx);
2004 sx_xunlock(&sc->sc_lock);
2005 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
2007 sx_xlock(&sc->sc_lock);
2008 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
2011 mtx_unlock(&sc->sc_queue_mtx);
2013 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
2014 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
2016 * Handle completion of the first half (the read) of a
2017 * block synchronization operation.
2019 g_mirror_sync_request(sc, bp);
2020 } else if (bp->bio_to != sc->sc_provider) {
2021 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
2023 * Handle completion of a regular I/O request.
2025 g_mirror_regular_request(sc, bp);
2026 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
2028 * Handle completion of the second half (the
2029 * write) of a block synchronization operation.
2031 g_mirror_sync_request(sc, bp);
2034 ("Invalid request cflags=0x%hx to=%s.",
2035 bp->bio_cflags, bp->bio_to->name));
2039 * Initiate an I/O request.
2041 g_mirror_register_request(sc, bp);
2043 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
2048 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
2051 sx_assert(&sc->sc_lock, SX_LOCKED);
2053 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
2055 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2056 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as dirty.",
2057 g_mirror_get_diskname(disk), sc->sc_name);
2058 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
2059 } else if (sc->sc_idle &&
2060 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2061 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as clean.",
2062 g_mirror_get_diskname(disk), sc->sc_name);
2063 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2068 g_mirror_sync_reinit(const struct g_mirror_disk *disk, struct bio *bp,
2074 data = bp->bio_data;
2075 idx = (int)(uintptr_t)bp->bio_caller1;
2078 bp->bio_cmd = BIO_READ;
2079 bp->bio_data = data;
2080 bp->bio_done = g_mirror_sync_done;
2081 bp->bio_from = disk->d_sync.ds_consumer;
2082 bp->bio_to = disk->d_softc->sc_provider;
2083 bp->bio_caller1 = (void *)(uintptr_t)idx;
2084 bp->bio_offset = offset;
2085 bp->bio_length = MIN(maxphys,
2086 disk->d_softc->sc_mediasize - bp->bio_offset);
2090 g_mirror_sync_start(struct g_mirror_disk *disk)
2092 struct g_mirror_softc *sc;
2093 struct g_mirror_disk_sync *sync;
2094 struct g_consumer *cp;
2096 int error __diagused, i;
2098 g_topology_assert_not();
2100 sync = &disk->d_sync;
2101 sx_assert(&sc->sc_lock, SX_LOCKED);
2103 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2104 ("Disk %s is not marked for synchronization.",
2105 g_mirror_get_diskname(disk)));
2106 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2107 ("Device not in RUNNING state (%s, %u).", sc->sc_name,
2110 sx_xunlock(&sc->sc_lock);
2112 cp = g_new_consumer(sc->sc_sync.ds_geom);
2113 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2114 error = g_attach(cp, sc->sc_provider);
2116 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
2117 error = g_access(cp, 1, 0, 0);
2118 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
2119 g_topology_unlock();
2120 sx_xlock(&sc->sc_lock);
2122 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
2123 g_mirror_get_diskname(disk));
2124 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
2125 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
2126 KASSERT(sync->ds_consumer == NULL,
2127 ("Sync consumer already exists (device=%s, disk=%s).",
2128 sc->sc_name, g_mirror_get_diskname(disk)));
2130 sync->ds_consumer = cp;
2131 sync->ds_consumer->private = disk;
2132 sync->ds_consumer->index = 0;
2135 * Allocate memory for synchronization bios and initialize them.
2137 sync->ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
2138 M_MIRROR, M_WAITOK);
2139 for (i = 0; i < g_mirror_syncreqs; i++) {
2141 sync->ds_bios[i] = bp;
2143 bp->bio_data = malloc(maxphys, M_MIRROR, M_WAITOK);
2144 bp->bio_caller1 = (void *)(uintptr_t)i;
2145 g_mirror_sync_reinit(disk, bp, sync->ds_offset);
2146 sync->ds_offset += bp->bio_length;
2149 /* Increase the number of disks in SYNCHRONIZING state. */
2150 sc->sc_sync.ds_ndisks++;
2151 /* Set the number of in-flight synchronization requests. */
2152 sync->ds_inflight = g_mirror_syncreqs;
2155 * Fire off first synchronization requests.
2157 for (i = 0; i < g_mirror_syncreqs; i++) {
2158 bp = sync->ds_bios[i];
2159 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
2160 sync->ds_consumer->index++;
2162 * Delay the request if it is colliding with a regular request.
2164 if (g_mirror_regular_collision(sc, bp))
2165 g_mirror_sync_delay(sc, bp);
2167 g_io_request(bp, sync->ds_consumer);
2172 * Stop synchronization process.
2173 * type: 0 - synchronization finished
2174 * 1 - synchronization stopped
2177 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
2179 struct g_mirror_softc *sc;
2180 struct g_consumer *cp;
2182 g_topology_assert_not();
2184 sx_assert(&sc->sc_lock, SX_LOCKED);
2186 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2187 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2188 g_mirror_disk_state2str(disk->d_state)));
2189 if (disk->d_sync.ds_consumer == NULL)
2193 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2194 sc->sc_name, g_mirror_get_diskname(disk));
2195 } else /* if (type == 1) */ {
2196 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2197 sc->sc_name, g_mirror_get_diskname(disk));
2199 g_mirror_regular_release(sc);
2200 free(disk->d_sync.ds_bios, M_MIRROR);
2201 disk->d_sync.ds_bios = NULL;
2202 cp = disk->d_sync.ds_consumer;
2203 disk->d_sync.ds_consumer = NULL;
2204 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2205 sc->sc_sync.ds_ndisks--;
2206 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2208 g_mirror_kill_consumer(sc, cp);
2209 g_topology_unlock();
2210 sx_xlock(&sc->sc_lock);
2214 g_mirror_launch_provider(struct g_mirror_softc *sc)
2216 struct g_mirror_disk *disk;
2217 struct g_provider *pp, *dp;
2219 sx_assert(&sc->sc_lock, SX_LOCKED);
2222 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2223 pp->flags |= G_PF_DIRECT_RECEIVE;
2224 pp->mediasize = sc->sc_mediasize;
2225 pp->sectorsize = sc->sc_sectorsize;
2227 pp->stripeoffset = 0;
2229 /* Splitting of unmapped BIO's could work but isn't implemented now */
2230 if (sc->sc_balance != G_MIRROR_BALANCE_SPLIT)
2231 pp->flags |= G_PF_ACCEPT_UNMAPPED;
2233 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2234 if (disk->d_consumer && disk->d_consumer->provider) {
2235 dp = disk->d_consumer->provider;
2236 if (dp->stripesize > pp->stripesize) {
2237 pp->stripesize = dp->stripesize;
2238 pp->stripeoffset = dp->stripeoffset;
2240 /* A provider underneath us doesn't support unmapped */
2241 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
2242 G_MIRROR_DEBUG(0, "Cancelling unmapped "
2243 "because of %s.", dp->name);
2244 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
2250 sc->sc_provider = pp;
2251 g_error_provider(pp, 0);
2252 g_topology_unlock();
2253 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2254 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2255 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2256 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2257 g_mirror_sync_start(disk);
2262 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2264 struct g_mirror_disk *disk;
2267 g_topology_assert_not();
2268 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2271 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2272 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2273 g_mirror_sync_stop(disk, 1);
2277 g_error_provider(sc->sc_provider, ENXIO);
2278 mtx_lock(&sc->sc_queue_mtx);
2279 while ((bp = TAILQ_FIRST(&sc->sc_queue)) != NULL) {
2280 TAILQ_REMOVE(&sc->sc_queue, bp, bio_queue);
2282 * Abort any pending I/O that wasn't generated by us.
2283 * Synchronization requests and requests destined for individual
2284 * mirror components can be destroyed immediately.
2286 if (bp->bio_to == sc->sc_provider &&
2287 bp->bio_from->geom != sc->sc_sync.ds_geom) {
2288 g_io_deliver(bp, ENXIO);
2290 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
2291 free(bp->bio_data, M_MIRROR);
2295 mtx_unlock(&sc->sc_queue_mtx);
2296 g_wither_provider(sc->sc_provider, ENXIO);
2297 sc->sc_provider = NULL;
2298 G_MIRROR_DEBUG(0, "Device %s: provider destroyed.", sc->sc_name);
2299 g_topology_unlock();
2303 g_mirror_go(void *arg)
2305 struct g_mirror_softc *sc;
2306 struct g_mirror_event *ep;
2309 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2310 ep = sc->sc_timeout_event;
2311 sc->sc_timeout_event = NULL;
2312 g_mirror_event_dispatch(ep, sc, 0,
2313 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2317 g_mirror_timeout_drain(struct g_mirror_softc *sc)
2319 sx_assert(&sc->sc_lock, SX_XLOCKED);
2321 callout_drain(&sc->sc_callout);
2322 g_mirror_event_free(sc->sc_timeout_event);
2323 sc->sc_timeout_event = NULL;
2327 g_mirror_determine_state(struct g_mirror_disk *disk)
2329 struct g_mirror_softc *sc;
2333 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2334 if ((disk->d_flags &
2335 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0 &&
2336 (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 ||
2337 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0)) {
2338 /* Disk does not need synchronization. */
2339 state = G_MIRROR_DISK_STATE_ACTIVE;
2342 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2344 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2346 * We can start synchronization from
2347 * the stored offset.
2349 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2351 state = G_MIRROR_DISK_STATE_STALE;
2354 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2356 * Reset all synchronization data for this disk,
2357 * because if it even was synchronized, it was
2358 * synchronized to disks with different syncid.
2360 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2361 disk->d_sync.ds_offset = 0;
2362 disk->d_sync.ds_offset_done = 0;
2363 disk->d_sync.ds_syncid = sc->sc_syncid;
2364 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2365 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2366 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2368 state = G_MIRROR_DISK_STATE_STALE;
2370 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2372 * Not good, NOT GOOD!
2373 * It means that mirror was started on stale disks
2374 * and more fresh disk just arrive.
2375 * If there were writes, mirror is broken, sorry.
2376 * I think the best choice here is don't touch
2377 * this disk and inform the user loudly.
2379 G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2380 "disk (%s) arrives!! It will not be connected to the "
2381 "running device.", sc->sc_name,
2382 g_mirror_get_diskname(disk));
2383 g_mirror_destroy_disk(disk);
2384 state = G_MIRROR_DISK_STATE_NONE;
2385 /* Return immediately, because disk was destroyed. */
2388 G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2389 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2394 * Update device state.
2397 g_mirror_update_device(struct g_mirror_softc *sc, bool force)
2399 struct g_mirror_disk *disk;
2402 sx_assert(&sc->sc_lock, SX_XLOCKED);
2404 switch (sc->sc_state) {
2405 case G_MIRROR_DEVICE_STATE_STARTING:
2407 struct g_mirror_disk *pdisk, *tdisk;
2408 const char *mismatch;
2409 uintmax_t found, newest;
2410 u_int dirty, ndisks;
2412 /* Pre-flight checks */
2413 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2415 * Confirm we already detected the newest genid.
2417 KASSERT(sc->sc_genid >= disk->d_genid,
2418 ("%s: found newer genid %u (sc:%p had %u).", __func__,
2419 disk->d_genid, sc, sc->sc_genid));
2421 /* Kick out any previously tasted stale components. */
2422 if (disk->d_genid < sc->sc_genid) {
2423 G_MIRROR_DEBUG(0, "Stale 'genid' field on %s "
2424 "(device %s) (component=%u latest=%u), skipping.",
2425 g_mirror_get_diskname(disk), sc->sc_name,
2426 disk->d_genid, sc->sc_genid);
2427 g_mirror_destroy_disk(disk);
2428 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2433 * Confirm we already detected the newest syncid.
2435 KASSERT(sc->sc_syncid >= disk->d_sync.ds_syncid,
2436 ("%s: found newer syncid %u (sc:%p had %u).",
2437 __func__, disk->d_sync.ds_syncid, sc,
2440 #define DETECT_MISMATCH(field, name) \
2441 if (mismatch == NULL && \
2442 disk->d_init_ ## field != sc->sc_ ## field) { \
2444 found = (intmax_t)disk->d_init_ ## field; \
2445 newest = (intmax_t)sc->sc_ ## field; \
2448 DETECT_MISMATCH(ndisks, "md_all");
2449 DETECT_MISMATCH(balance, "md_balance");
2450 DETECT_MISMATCH(slice, "md_slice");
2451 DETECT_MISMATCH(mediasize, "md_mediasize");
2452 #undef DETECT_MISMATCH
2453 if (mismatch != NULL) {
2454 G_MIRROR_DEBUG(0, "Found a mismatching '%s' "
2455 "field on %s (device %s) (found=%ju "
2456 "newest=%ju).", mismatch,
2457 g_mirror_get_diskname(disk), sc->sc_name,
2459 g_mirror_destroy_disk(disk);
2460 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2465 KASSERT(sc->sc_provider == NULL,
2466 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2468 * Are we ready? If the timeout (force is true) has expired, and
2469 * any disks are present, then yes. If we're permitted to launch
2470 * before the timeout has expired and the expected number of
2471 * current-generation mirror disks have been tasted, then yes.
2473 ndisks = g_mirror_ndisks(sc, -1);
2474 if ((force && ndisks > 0) ||
2475 (g_launch_mirror_before_timeout && ndisks == sc->sc_ndisks)) {
2477 } else if (ndisks == 0) {
2479 * Disks went down in starting phase, so destroy
2482 g_mirror_timeout_drain(sc);
2483 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2484 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2486 root_mount_rel(sc->sc_rootmount);
2487 sc->sc_rootmount = NULL;
2494 * Activate all disks with the biggest syncid.
2498 * If 'force' is true, we have been called due to
2499 * timeout, so don't bother canceling timeout.
2502 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2503 if ((disk->d_flags &
2504 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2509 /* No valid disks found, destroy device. */
2510 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2511 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2512 __LINE__, sc->sc_rootmount);
2513 root_mount_rel(sc->sc_rootmount);
2514 sc->sc_rootmount = NULL;
2518 /* Cancel timeout. */
2519 g_mirror_timeout_drain(sc);
2523 * Here we need to look for dirty disks and if all disks
2524 * with the biggest syncid are dirty, we have to choose
2525 * one with the biggest priority and rebuild the rest.
2528 * Find the number of dirty disks with the biggest syncid.
2529 * Find the number of disks with the biggest syncid.
2530 * While here, find a disk with the biggest priority.
2534 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2535 if (disk->d_sync.ds_syncid != sc->sc_syncid)
2537 if ((disk->d_flags &
2538 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2542 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2544 if (pdisk == NULL ||
2545 pdisk->d_priority < disk->d_priority) {
2551 /* No dirty disks at all, great. */
2552 } else if (dirty == ndisks) {
2554 * Force synchronization for all dirty disks except one
2555 * with the biggest priority.
2557 KASSERT(pdisk != NULL, ("pdisk == NULL"));
2558 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2559 "master disk for synchronization.",
2560 g_mirror_get_diskname(pdisk), sc->sc_name);
2561 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2562 if (disk->d_sync.ds_syncid != sc->sc_syncid)
2564 if ((disk->d_flags &
2565 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2568 KASSERT((disk->d_flags &
2569 G_MIRROR_DISK_FLAG_DIRTY) != 0,
2570 ("Disk %s isn't marked as dirty.",
2571 g_mirror_get_diskname(disk)));
2572 /* Skip the disk with the biggest priority. */
2575 disk->d_sync.ds_syncid = 0;
2577 } else if (dirty < ndisks) {
2579 * Force synchronization for all dirty disks.
2580 * We have some non-dirty disks.
2582 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2583 if (disk->d_sync.ds_syncid != sc->sc_syncid)
2585 if ((disk->d_flags &
2586 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2589 if ((disk->d_flags &
2590 G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2593 disk->d_sync.ds_syncid = 0;
2600 /* Remember to bump syncid on first write. */
2601 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2603 state = G_MIRROR_DEVICE_STATE_RUNNING;
2604 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2605 sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2606 g_mirror_device_state2str(state));
2607 sc->sc_state = state;
2608 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2609 state = g_mirror_determine_state(disk);
2610 g_mirror_event_send(disk, state,
2611 G_MIRROR_EVENT_DONTWAIT);
2612 if (state == G_MIRROR_DISK_STATE_STALE)
2613 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2617 case G_MIRROR_DEVICE_STATE_RUNNING:
2618 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2619 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2621 * No usable disks, so destroy the device.
2623 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2625 } else if (g_mirror_ndisks(sc,
2626 G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2627 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2629 * We have active disks, launch provider if it doesn't
2632 if (sc->sc_provider == NULL)
2633 g_mirror_launch_provider(sc);
2634 if (sc->sc_rootmount != NULL) {
2635 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2636 __LINE__, sc->sc_rootmount);
2637 root_mount_rel(sc->sc_rootmount);
2638 sc->sc_rootmount = NULL;
2642 * Genid should be bumped immediately, so do it here.
2644 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2645 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2646 g_mirror_bump_genid(sc);
2648 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID_NOW) != 0) {
2649 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID_NOW;
2650 g_mirror_bump_syncid(sc);
2654 KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2655 sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2661 * Update disk state and device state if needed.
2663 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \
2664 "Disk %s state changed from %s to %s (device %s).", \
2665 g_mirror_get_diskname(disk), \
2666 g_mirror_disk_state2str(disk->d_state), \
2667 g_mirror_disk_state2str(state), sc->sc_name)
2669 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2671 struct g_mirror_softc *sc;
2674 sx_assert(&sc->sc_lock, SX_XLOCKED);
2677 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2678 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2679 g_mirror_disk_state2str(state));
2681 case G_MIRROR_DISK_STATE_NEW:
2683 * Possible scenarios:
2684 * 1. New disk arrive.
2686 /* Previous state should be NONE. */
2687 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2688 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2689 g_mirror_disk_state2str(disk->d_state)));
2690 DISK_STATE_CHANGED();
2692 disk->d_state = state;
2694 if (LIST_EMPTY(&sc->sc_disks))
2695 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2697 struct g_mirror_disk *dp;
2699 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2700 if (disk->d_priority >= dp->d_priority) {
2701 LIST_INSERT_BEFORE(dp, disk, d_next);
2705 if (LIST_NEXT(dp, d_next) == NULL)
2709 LIST_INSERT_AFTER(dp, disk, d_next);
2711 g_topology_unlock();
2712 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2713 sc->sc_name, g_mirror_get_diskname(disk));
2714 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2716 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2717 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2718 g_mirror_device_state2str(sc->sc_state),
2719 g_mirror_get_diskname(disk),
2720 g_mirror_disk_state2str(disk->d_state)));
2721 state = g_mirror_determine_state(disk);
2722 if (state != G_MIRROR_DISK_STATE_NONE)
2725 case G_MIRROR_DISK_STATE_ACTIVE:
2727 * Possible scenarios:
2728 * 1. New disk does not need synchronization.
2729 * 2. Synchronization process finished successfully.
2731 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2732 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2733 g_mirror_device_state2str(sc->sc_state),
2734 g_mirror_get_diskname(disk),
2735 g_mirror_disk_state2str(disk->d_state)));
2736 /* Previous state should be NEW or SYNCHRONIZING. */
2737 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2738 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2739 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2740 g_mirror_disk_state2str(disk->d_state)));
2741 DISK_STATE_CHANGED();
2743 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2744 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2745 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2746 g_mirror_sync_stop(disk, 0);
2748 disk->d_state = state;
2749 disk->d_sync.ds_offset = 0;
2750 disk->d_sync.ds_offset_done = 0;
2751 g_mirror_update_idle(sc, disk);
2752 g_mirror_update_metadata(disk);
2753 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2754 sc->sc_name, g_mirror_get_diskname(disk));
2756 case G_MIRROR_DISK_STATE_STALE:
2758 * Possible scenarios:
2759 * 1. Stale disk was connected.
2761 /* Previous state should be NEW. */
2762 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2763 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2764 g_mirror_disk_state2str(disk->d_state)));
2765 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2766 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2767 g_mirror_device_state2str(sc->sc_state),
2768 g_mirror_get_diskname(disk),
2769 g_mirror_disk_state2str(disk->d_state)));
2771 * STALE state is only possible if device is marked
2774 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2775 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2776 g_mirror_device_state2str(sc->sc_state),
2777 g_mirror_get_diskname(disk),
2778 g_mirror_disk_state2str(disk->d_state)));
2779 DISK_STATE_CHANGED();
2781 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2782 disk->d_state = state;
2783 g_mirror_update_metadata(disk);
2784 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2785 sc->sc_name, g_mirror_get_diskname(disk));
2787 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2789 * Possible scenarios:
2790 * 1. Disk which needs synchronization was connected.
2792 /* Previous state should be NEW. */
2793 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2794 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2795 g_mirror_disk_state2str(disk->d_state)));
2796 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2797 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2798 g_mirror_device_state2str(sc->sc_state),
2799 g_mirror_get_diskname(disk),
2800 g_mirror_disk_state2str(disk->d_state)));
2801 DISK_STATE_CHANGED();
2803 if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2804 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2805 disk->d_state = state;
2806 if (sc->sc_provider != NULL) {
2807 g_mirror_sync_start(disk);
2808 g_mirror_update_metadata(disk);
2811 case G_MIRROR_DISK_STATE_DISCONNECTED:
2813 * Possible scenarios:
2814 * 1. Device wasn't running yet, but disk disappear.
2815 * 2. Disk was active and disapppear.
2816 * 3. Disk disappear during synchronization process.
2818 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2820 * Previous state should be ACTIVE, STALE or
2823 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2824 disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2825 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2826 ("Wrong disk state (%s, %s).",
2827 g_mirror_get_diskname(disk),
2828 g_mirror_disk_state2str(disk->d_state)));
2829 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2830 /* Previous state should be NEW. */
2831 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2832 ("Wrong disk state (%s, %s).",
2833 g_mirror_get_diskname(disk),
2834 g_mirror_disk_state2str(disk->d_state)));
2836 * Reset bumping syncid if disk disappeared in STARTING
2839 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2840 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2843 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2845 g_mirror_device_state2str(sc->sc_state),
2846 g_mirror_get_diskname(disk),
2847 g_mirror_disk_state2str(disk->d_state)));
2850 DISK_STATE_CHANGED();
2851 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2852 sc->sc_name, g_mirror_get_diskname(disk));
2854 g_mirror_destroy_disk(disk);
2856 case G_MIRROR_DISK_STATE_DESTROY:
2860 error = g_mirror_clear_metadata(disk);
2863 "Device %s: failed to clear metadata on %s: %d.",
2864 sc->sc_name, g_mirror_get_diskname(disk), error);
2867 DISK_STATE_CHANGED();
2868 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2869 sc->sc_name, g_mirror_get_diskname(disk));
2871 g_mirror_destroy_disk(disk);
2873 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2874 g_mirror_update_metadata(disk);
2879 KASSERT(1 == 0, ("Unknown state (%u).", state));
2884 #undef DISK_STATE_CHANGED
2887 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2889 struct g_provider *pp;
2893 g_topology_assert();
2895 error = g_access(cp, 1, 0, 0);
2899 g_topology_unlock();
2900 /* Metadata are stored on last sector. */
2901 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2904 g_access(cp, -1, 0, 0);
2906 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2907 cp->provider->name, error);
2911 /* Decode metadata. */
2912 error = mirror_metadata_decode(buf, md);
2914 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2916 if (md->md_version > G_MIRROR_VERSION) {
2918 "Kernel module is too old to handle metadata from %s.",
2919 cp->provider->name);
2923 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2924 cp->provider->name);
2932 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2933 struct g_mirror_metadata *md)
2936 G_MIRROR_DEBUG(2, "%s: md_did 0x%u disk %s device %s md_all 0x%x "
2937 "sc_ndisks 0x%x md_slice 0x%x sc_slice 0x%x md_balance 0x%x "
2938 "sc_balance 0x%x sc_mediasize 0x%jx pp_mediasize 0x%jx "
2939 "md_sectorsize 0x%x sc_sectorsize 0x%x md_mflags 0x%jx "
2940 "md_dflags 0x%jx md_syncid 0x%x md_genid 0x%x md_priority 0x%x "
2942 __func__, md->md_did, pp->name, sc->sc_name, md->md_all,
2943 sc->sc_ndisks, md->md_slice, sc->sc_slice, md->md_balance,
2944 sc->sc_balance, (uintmax_t)sc->sc_mediasize,
2945 (uintmax_t)pp->mediasize, md->md_sectorsize, sc->sc_sectorsize,
2946 (uintmax_t)md->md_mflags, (uintmax_t)md->md_dflags, md->md_syncid,
2947 md->md_genid, md->md_priority, sc->sc_state);
2949 if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2950 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2951 pp->name, md->md_did);
2954 if (sc->sc_mediasize > pp->mediasize) {
2956 "Invalid size of disk %s (device %s), skipping.", pp->name,
2960 if (md->md_sectorsize != sc->sc_sectorsize) {
2962 "Invalid '%s' field on disk %s (device %s), skipping.",
2963 "md_sectorsize", pp->name, sc->sc_name);
2966 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2968 "Invalid sector size of disk %s (device %s), skipping.",
2969 pp->name, sc->sc_name);
2972 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2974 "Invalid device flags on disk %s (device %s), skipping.",
2975 pp->name, sc->sc_name);
2978 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2980 "Invalid disk flags on disk %s (device %s), skipping.",
2981 pp->name, sc->sc_name);
2988 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2989 struct g_mirror_metadata *md)
2991 struct g_mirror_disk *disk;
2994 g_topology_assert_not();
2995 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2997 error = g_mirror_check_metadata(sc, pp, md);
3001 if (md->md_genid < sc->sc_genid) {
3002 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
3003 pp->name, sc->sc_name);
3008 * If the component disk we're tasting has newer metadata than the
3009 * STARTING gmirror device, refresh the device from the component.
3011 error = g_mirror_refresh_device(sc, pp, md);
3015 disk = g_mirror_init_disk(sc, pp, md, &error);
3018 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
3019 G_MIRROR_EVENT_WAIT);
3022 if (md->md_version < G_MIRROR_VERSION) {
3023 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
3024 pp->name, md->md_version, G_MIRROR_VERSION);
3025 g_mirror_update_metadata(disk);
3031 g_mirror_destroy_delayed(void *arg, int flag)
3033 struct g_mirror_softc *sc;
3036 if (flag == EV_CANCEL) {
3037 G_MIRROR_DEBUG(1, "Destroying canceled.");
3041 g_topology_unlock();
3042 sx_xlock(&sc->sc_lock);
3043 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
3044 ("DESTROY flag set on %s.", sc->sc_name));
3045 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_CLOSEWAIT) != 0,
3046 ("CLOSEWAIT flag not set on %s.", sc->sc_name));
3047 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
3048 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
3050 G_MIRROR_DEBUG(0, "Cannot destroy %s (error=%d).",
3051 sc->sc_name, error);
3052 sx_xunlock(&sc->sc_lock);
3058 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
3060 struct g_mirror_softc *sc;
3063 g_topology_assert();
3064 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
3068 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
3070 g_topology_unlock();
3071 sx_xlock(&sc->sc_lock);
3072 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
3073 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_CLOSEWAIT) != 0 ||
3074 LIST_EMPTY(&sc->sc_disks)) {
3075 if (acr > 0 || acw > 0 || ace > 0)
3079 sc->sc_provider_open += acr + acw + ace;
3080 if (pp->acw + acw == 0)
3081 g_mirror_idle(sc, 0);
3082 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_CLOSEWAIT) != 0 &&
3083 sc->sc_provider_open == 0)
3084 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK, sc, NULL);
3086 sx_xunlock(&sc->sc_lock);
3092 g_mirror_reinit_from_metadata(struct g_mirror_softc *sc,
3093 const struct g_mirror_metadata *md)
3096 sc->sc_genid = md->md_genid;
3097 sc->sc_syncid = md->md_syncid;
3099 sc->sc_slice = md->md_slice;
3100 sc->sc_balance = md->md_balance;
3101 sc->sc_mediasize = md->md_mediasize;
3102 sc->sc_ndisks = md->md_all;
3103 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_MASK;
3104 sc->sc_flags |= (md->md_mflags & G_MIRROR_DEVICE_FLAG_MASK);
3108 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md,
3111 struct g_mirror_softc *sc;
3115 g_topology_assert();
3116 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
3119 /* One disk is minimum. */
3125 gp = g_new_geomf(mp, "%s", md->md_name);
3126 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
3127 gp->start = g_mirror_start;
3128 gp->orphan = g_mirror_orphan;
3129 gp->access = g_mirror_access;
3130 gp->dumpconf = g_mirror_dumpconf;
3133 sc->sc_id = md->md_mid;
3134 g_mirror_reinit_from_metadata(sc, md);
3135 sc->sc_sectorsize = md->md_sectorsize;
3138 sc->sc_last_write = time_uptime;
3141 sx_init(&sc->sc_lock, "gmirror:lock");
3142 TAILQ_INIT(&sc->sc_queue);
3143 mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
3144 TAILQ_INIT(&sc->sc_regular_delayed);
3145 TAILQ_INIT(&sc->sc_inflight);
3146 TAILQ_INIT(&sc->sc_sync_delayed);
3147 LIST_INIT(&sc->sc_disks);
3148 TAILQ_INIT(&sc->sc_events);
3149 mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
3150 callout_init(&sc->sc_callout, 1);
3151 mtx_init(&sc->sc_done_mtx, "gmirror:done", NULL, MTX_DEF);
3152 sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
3155 sc->sc_provider = NULL;
3156 sc->sc_provider_open = 0;
3158 * Synchronization geom.
3160 gp = g_new_geomf(mp, "%s.sync", md->md_name);
3162 gp->orphan = g_mirror_orphan;
3163 sc->sc_sync.ds_geom = gp;
3164 sc->sc_sync.ds_ndisks = 0;
3165 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
3166 "g_mirror %s", md->md_name);
3168 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
3170 g_destroy_geom(sc->sc_sync.ds_geom);
3171 g_destroy_geom(sc->sc_geom);
3172 g_mirror_free_device(sc);
3176 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
3177 sc->sc_name, sc->sc_ndisks, sc->sc_id);
3179 sc->sc_rootmount = root_mount_hold("GMIRROR");
3180 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
3183 * Schedule startup timeout.
3185 timeout = g_mirror_timeout * hz;
3186 sc->sc_timeout_event = malloc(sizeof(struct g_mirror_event), M_MIRROR,
3188 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
3189 return (sc->sc_geom);
3193 g_mirror_destroy(struct g_mirror_softc *sc, int how)
3195 struct g_mirror_disk *disk;
3197 g_topology_assert_not();
3198 sx_assert(&sc->sc_lock, SX_XLOCKED);
3200 if (sc->sc_provider_open != 0) {
3202 case G_MIRROR_DESTROY_SOFT:
3204 "Device %s is still open (%d).", sc->sc_name,
3205 sc->sc_provider_open);
3207 case G_MIRROR_DESTROY_DELAYED:
3209 "Device %s will be destroyed on last close.",
3211 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
3212 if (disk->d_state ==
3213 G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3214 g_mirror_sync_stop(disk, 1);
3217 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_CLOSEWAIT;
3219 case G_MIRROR_DESTROY_HARD:
3220 G_MIRROR_DEBUG(1, "Device %s is still open, so it "
3221 "can't be definitely removed.", sc->sc_name);
3225 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3226 sx_xunlock(&sc->sc_lock);
3229 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
3230 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DRAIN;
3231 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3232 sx_xunlock(&sc->sc_lock);
3233 mtx_lock(&sc->sc_queue_mtx);
3235 mtx_unlock(&sc->sc_queue_mtx);
3236 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3237 while (sc->sc_worker != NULL)
3238 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
3239 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3240 sx_xlock(&sc->sc_lock);
3241 g_mirror_destroy_device(sc);
3246 g_mirror_taste_orphan(struct g_consumer *cp)
3249 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3250 cp->provider->name));
3253 static struct g_geom *
3254 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3256 struct g_mirror_metadata md;
3257 struct g_mirror_softc *sc;
3258 struct g_consumer *cp;
3262 g_topology_assert();
3263 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3264 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
3266 gp = g_new_geomf(mp, "mirror:taste");
3268 * This orphan function should be never called.
3270 gp->orphan = g_mirror_taste_orphan;
3271 cp = g_new_consumer(gp);
3272 error = g_attach(cp, pp);
3274 error = g_mirror_read_metadata(cp, &md);
3277 g_destroy_consumer(cp);
3283 if (md.md_provider[0] != '\0' &&
3284 !g_compare_names(md.md_provider, pp->name))
3286 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3288 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3290 "Device %s: provider %s marked as inactive, skipping.",
3291 md.md_name, pp->name);
3294 if (g_mirror_debug >= 2)
3295 mirror_metadata_dump(&md);
3298 * Let's check if device already exists.
3301 LIST_FOREACH(gp, &mp->geom, geom) {
3305 if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC)
3307 if (sc->sc_sync.ds_geom == gp)
3309 if (strcmp(md.md_name, sc->sc_name) != 0)
3311 if (md.md_mid != sc->sc_id) {
3312 G_MIRROR_DEBUG(0, "Device %s already configured.",
3319 gp = g_mirror_create(mp, &md, G_MIRROR_TYPE_AUTOMATIC);
3321 G_MIRROR_DEBUG(0, "Cannot create device %s.",
3327 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3328 g_topology_unlock();
3329 sx_xlock(&sc->sc_lock);
3330 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
3331 error = g_mirror_add_disk(sc, pp, &md);
3332 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
3334 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3335 pp->name, gp->name, error);
3336 if (LIST_EMPTY(&sc->sc_disks)) {
3338 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3344 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3345 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3349 sx_xunlock(&sc->sc_lock);
3355 g_mirror_resize(struct g_consumer *cp)
3357 struct g_mirror_disk *disk;
3359 g_topology_assert();
3360 g_trace(G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name);
3365 g_topology_unlock();
3366 g_mirror_update_metadata(disk);
3371 g_mirror_destroy_geom(struct gctl_req *req __unused,
3372 struct g_class *mp __unused, struct g_geom *gp)
3374 struct g_mirror_softc *sc;
3377 g_topology_unlock();
3379 sx_xlock(&sc->sc_lock);
3381 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3383 sx_xunlock(&sc->sc_lock);
3389 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3390 struct g_consumer *cp, struct g_provider *pp)
3392 struct g_mirror_softc *sc;
3394 g_topology_assert();
3399 /* Skip synchronization geom. */
3400 if (gp == sc->sc_sync.ds_geom)
3404 } else if (cp != NULL) {
3405 struct g_mirror_disk *disk;
3410 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3411 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3412 sbuf_printf(sb, "%s<Synchronized>", indent);
3413 if (disk->d_sync.ds_offset == 0)
3416 sbuf_printf(sb, "%u%%",
3417 (u_int)((disk->d_sync.ds_offset * 100) /
3419 sbuf_cat(sb, "</Synchronized>\n");
3420 if (disk->d_sync.ds_offset > 0)
3421 sbuf_printf(sb, "%s<BytesSynced>%jd"
3422 "</BytesSynced>\n", indent,
3423 (intmax_t)disk->d_sync.ds_offset);
3425 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3426 disk->d_sync.ds_syncid);
3427 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3429 sbuf_printf(sb, "%s<Flags>", indent);
3430 if (disk->d_flags == 0)
3431 sbuf_cat(sb, "NONE");
3435 #define ADD_FLAG(flag, name) do { \
3436 if ((disk->d_flags & (flag)) != 0) { \
3438 sbuf_cat(sb, ", "); \
3441 sbuf_cat(sb, name); \
3444 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3445 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3446 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3447 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3449 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3450 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3453 sbuf_cat(sb, "</Flags>\n");
3454 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3456 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3457 g_mirror_disk_state2str(disk->d_state));
3459 sbuf_printf(sb, "%s<Type>", indent);
3460 switch (sc->sc_type) {
3461 case G_MIRROR_TYPE_AUTOMATIC:
3462 sbuf_cat(sb, "AUTOMATIC");
3464 case G_MIRROR_TYPE_MANUAL:
3465 sbuf_cat(sb, "MANUAL");
3468 sbuf_cat(sb, "UNKNOWN");
3471 sbuf_cat(sb, "</Type>\n");
3472 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3473 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3474 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3475 sbuf_printf(sb, "%s<Flags>", indent);
3476 if (sc->sc_flags == 0)
3477 sbuf_cat(sb, "NONE");
3481 #define ADD_FLAG(flag, name) do { \
3482 if ((sc->sc_flags & (flag)) != 0) { \
3484 sbuf_cat(sb, ", "); \
3487 sbuf_cat(sb, name); \
3490 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3491 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3494 sbuf_cat(sb, "</Flags>\n");
3495 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3496 (u_int)sc->sc_slice);
3497 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3498 balance_name(sc->sc_balance));
3499 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3501 sbuf_printf(sb, "%s<State>", indent);
3502 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3503 sbuf_printf(sb, "%s", "STARTING");
3504 else if (sc->sc_ndisks ==
3505 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3506 sbuf_printf(sb, "%s", "COMPLETE");
3508 sbuf_printf(sb, "%s", "DEGRADED");
3509 sbuf_cat(sb, "</State>\n");
3514 g_mirror_shutdown_post_sync(void *arg, int howto)
3517 struct g_geom *gp, *gp2;
3518 struct g_mirror_softc *sc;
3521 if (KERNEL_PANICKED())
3526 g_mirror_shutdown = 1;
3527 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3528 if ((sc = gp->softc) == NULL)
3530 /* Skip synchronization geom. */
3531 if (gp == sc->sc_sync.ds_geom)
3533 g_topology_unlock();
3534 sx_xlock(&sc->sc_lock);
3535 g_mirror_idle(sc, -1);
3537 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3539 sx_xunlock(&sc->sc_lock);
3542 g_topology_unlock();
3546 g_mirror_init(struct g_class *mp)
3549 g_mirror_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
3550 g_mirror_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
3551 if (g_mirror_post_sync == NULL)
3552 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3556 g_mirror_fini(struct g_class *mp)
3559 if (g_mirror_post_sync != NULL)
3560 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_mirror_post_sync);
3564 * Refresh the mirror device's metadata when gmirror encounters a newer
3565 * generation as the individual components are being added to the mirror set.
3568 g_mirror_refresh_device(struct g_mirror_softc *sc, const struct g_provider *pp,
3569 const struct g_mirror_metadata *md)
3572 g_topology_assert_not();
3573 sx_assert(&sc->sc_lock, SX_XLOCKED);
3575 KASSERT(sc->sc_genid <= md->md_genid,
3576 ("%s: attempted to refresh from stale component %s (device %s) "
3577 "(%u < %u).", __func__, pp->name, sc->sc_name, md->md_genid,
3580 if (sc->sc_genid > md->md_genid || (sc->sc_genid == md->md_genid &&
3581 sc->sc_syncid >= md->md_syncid))
3584 G_MIRROR_DEBUG(0, "Found newer version for device %s (genid: curr=%u "
3585 "new=%u; syncid: curr=%u new=%u; ndisks: curr=%u new=%u; "
3586 "provider=%s).", sc->sc_name, sc->sc_genid, md->md_genid,
3587 sc->sc_syncid, md->md_syncid, sc->sc_ndisks, md->md_all, pp->name);
3589 if (sc->sc_state != G_MIRROR_DEVICE_STATE_STARTING) {
3590 /* Probable data corruption detected */
3591 G_MIRROR_DEBUG(0, "Cannot refresh metadata in %s state "
3592 "(device=%s genid=%u). A stale mirror device was launched.",
3593 g_mirror_device_state2str(sc->sc_state), sc->sc_name,
3599 g_mirror_reinit_from_metadata(sc, md);
3601 G_MIRROR_DEBUG(1, "Refresh device %s (id=%u, state=%s) from disk %s "
3602 "(genid=%u syncid=%u md_all=%u).", sc->sc_name, md->md_mid,
3603 g_mirror_device_state2str(sc->sc_state), pp->name, md->md_genid,
3604 md->md_syncid, (unsigned)md->md_all);
3609 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);
3610 MODULE_VERSION(geom_mirror, 0);