2 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/limits.h>
36 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/eventhandler.h>
43 #include <geom/geom.h>
45 #include <sys/kthread.h>
46 #include <sys/sched.h>
47 #include <geom/mirror/g_mirror.h>
49 FEATURE(geom_mirror, "GEOM mirroring support");
51 static MALLOC_DEFINE(M_MIRROR, "mirror_data", "GEOM_MIRROR Data");
53 SYSCTL_DECL(_kern_geom);
54 SYSCTL_NODE(_kern_geom, OID_AUTO, mirror, CTLFLAG_RW, 0, "GEOM_MIRROR stuff");
55 u_int g_mirror_debug = 0;
56 TUNABLE_INT("kern.geom.mirror.debug", &g_mirror_debug);
57 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, debug, CTLFLAG_RW, &g_mirror_debug, 0,
59 static u_int g_mirror_timeout = 4;
60 TUNABLE_INT("kern.geom.mirror.timeout", &g_mirror_timeout);
61 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, timeout, CTLFLAG_RW, &g_mirror_timeout,
62 0, "Time to wait on all mirror components");
63 static u_int g_mirror_idletime = 5;
64 TUNABLE_INT("kern.geom.mirror.idletime", &g_mirror_idletime);
65 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, idletime, CTLFLAG_RW,
66 &g_mirror_idletime, 0, "Mark components as clean when idling");
67 static u_int g_mirror_disconnect_on_failure = 1;
68 TUNABLE_INT("kern.geom.mirror.disconnect_on_failure",
69 &g_mirror_disconnect_on_failure);
70 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
71 &g_mirror_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
72 static u_int g_mirror_syncreqs = 2;
73 TUNABLE_INT("kern.geom.mirror.sync_requests", &g_mirror_syncreqs);
74 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
75 &g_mirror_syncreqs, 0, "Parallel synchronization I/O requests.");
77 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \
78 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
79 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
80 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
83 static eventhandler_tag g_mirror_pre_sync = NULL;
85 static int g_mirror_destroy_geom(struct gctl_req *req, struct g_class *mp,
87 static g_taste_t g_mirror_taste;
88 static void g_mirror_init(struct g_class *mp);
89 static void g_mirror_fini(struct g_class *mp);
91 struct g_class g_mirror_class = {
92 .name = G_MIRROR_CLASS_NAME,
94 .ctlreq = g_mirror_config,
95 .taste = g_mirror_taste,
96 .destroy_geom = g_mirror_destroy_geom,
97 .init = g_mirror_init,
102 static void g_mirror_destroy_provider(struct g_mirror_softc *sc);
103 static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
104 static void g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force);
105 static void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
106 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
107 static void g_mirror_sync_stop(struct g_mirror_disk *disk, int type);
108 static void g_mirror_register_request(struct bio *bp);
109 static void g_mirror_sync_release(struct g_mirror_softc *sc);
113 g_mirror_disk_state2str(int state)
117 case G_MIRROR_DISK_STATE_NONE:
119 case G_MIRROR_DISK_STATE_NEW:
121 case G_MIRROR_DISK_STATE_ACTIVE:
123 case G_MIRROR_DISK_STATE_STALE:
125 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
126 return ("SYNCHRONIZING");
127 case G_MIRROR_DISK_STATE_DISCONNECTED:
128 return ("DISCONNECTED");
129 case G_MIRROR_DISK_STATE_DESTROY:
137 g_mirror_device_state2str(int state)
141 case G_MIRROR_DEVICE_STATE_STARTING:
143 case G_MIRROR_DEVICE_STATE_RUNNING:
151 g_mirror_get_diskname(struct g_mirror_disk *disk)
154 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
155 return ("[unknown]");
156 return (disk->d_name);
160 * --- Events handling functions ---
161 * Events in geom_mirror are used to maintain disks and device status
162 * from one thread to simplify locking.
165 g_mirror_event_free(struct g_mirror_event *ep)
172 g_mirror_event_send(void *arg, int state, int flags)
174 struct g_mirror_softc *sc;
175 struct g_mirror_disk *disk;
176 struct g_mirror_event *ep;
179 ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
180 G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
181 if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
192 mtx_lock(&sc->sc_events_mtx);
193 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
194 mtx_unlock(&sc->sc_events_mtx);
195 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
196 mtx_lock(&sc->sc_queue_mtx);
198 mtx_unlock(&sc->sc_queue_mtx);
199 if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0)
201 sx_assert(&sc->sc_lock, SX_XLOCKED);
202 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
203 sx_xunlock(&sc->sc_lock);
204 while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) {
205 mtx_lock(&sc->sc_events_mtx);
206 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event",
210 g_mirror_event_free(ep);
211 sx_xlock(&sc->sc_lock);
215 static struct g_mirror_event *
216 g_mirror_event_get(struct g_mirror_softc *sc)
218 struct g_mirror_event *ep;
220 mtx_lock(&sc->sc_events_mtx);
221 ep = TAILQ_FIRST(&sc->sc_events);
222 mtx_unlock(&sc->sc_events_mtx);
227 g_mirror_event_remove(struct g_mirror_softc *sc, struct g_mirror_event *ep)
230 mtx_lock(&sc->sc_events_mtx);
231 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
232 mtx_unlock(&sc->sc_events_mtx);
236 g_mirror_event_cancel(struct g_mirror_disk *disk)
238 struct g_mirror_softc *sc;
239 struct g_mirror_event *ep, *tmpep;
242 sx_assert(&sc->sc_lock, SX_XLOCKED);
244 mtx_lock(&sc->sc_events_mtx);
245 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
246 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0)
248 if (ep->e_disk != disk)
250 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
251 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
252 g_mirror_event_free(ep);
254 ep->e_error = ECANCELED;
258 mtx_unlock(&sc->sc_events_mtx);
262 * Return the number of disks in given state.
263 * If state is equal to -1, count all connected disks.
266 g_mirror_ndisks(struct g_mirror_softc *sc, int state)
268 struct g_mirror_disk *disk;
271 sx_assert(&sc->sc_lock, SX_LOCKED);
273 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
274 if (state == -1 || disk->d_state == state)
281 * Find a disk in mirror by its disk ID.
283 static struct g_mirror_disk *
284 g_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id)
286 struct g_mirror_disk *disk;
288 sx_assert(&sc->sc_lock, SX_XLOCKED);
290 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
291 if (disk->d_id == id)
298 g_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp)
303 mtx_lock(&sc->sc_queue_mtx);
304 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
305 if (bp->bio_from == cp)
308 mtx_unlock(&sc->sc_queue_mtx);
313 g_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp)
318 "I/O requests for %s exist, can't destroy it now.",
322 if (g_mirror_nrequests(sc, cp) > 0) {
324 "I/O requests for %s in queue, can't destroy it now.",
332 g_mirror_destroy_consumer(void *arg, int flags __unused)
334 struct g_consumer *cp;
339 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
341 g_destroy_consumer(cp);
345 g_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
347 struct g_provider *pp;
353 if (g_mirror_is_busy(sc, cp))
358 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
361 G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
362 -cp->acw, -cp->ace, 0);
363 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
364 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
367 * After retaste event was send (inside g_access()), we can send
368 * event to detach and destroy consumer.
369 * A class, which has consumer to the given provider connected
370 * will not receive retaste event for the provider.
371 * This is the way how I ignore retaste events when I close
372 * consumers opened for write: I detach and destroy consumer
373 * after retaste event is sent.
375 g_post_event(g_mirror_destroy_consumer, cp, M_WAITOK, NULL);
378 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", pp->name);
380 g_destroy_consumer(cp);
384 g_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp)
386 struct g_consumer *cp;
389 g_topology_assert_not();
390 KASSERT(disk->d_consumer == NULL,
391 ("Disk already connected (device %s).", disk->d_softc->sc_name));
394 cp = g_new_consumer(disk->d_softc->sc_geom);
395 error = g_attach(cp, pp);
397 g_destroy_consumer(cp);
401 error = g_access(cp, 1, 1, 1);
404 g_destroy_consumer(cp);
406 G_MIRROR_DEBUG(0, "Cannot open consumer %s (error=%d).",
411 disk->d_consumer = cp;
412 disk->d_consumer->private = disk;
413 disk->d_consumer->index = 0;
415 G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk));
420 g_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
427 if (cp->provider != NULL)
428 g_mirror_kill_consumer(sc, cp);
430 g_destroy_consumer(cp);
434 * Initialize disk. This means allocate memory, create consumer, attach it
435 * to the provider and open access (r1w1e1) to it.
437 static struct g_mirror_disk *
438 g_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp,
439 struct g_mirror_metadata *md, int *errorp)
441 struct g_mirror_disk *disk;
444 disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO);
450 error = g_mirror_connect_disk(disk, pp);
453 disk->d_id = md->md_did;
454 disk->d_state = G_MIRROR_DISK_STATE_NONE;
455 disk->d_priority = md->md_priority;
456 disk->d_flags = md->md_dflags;
457 if (md->md_provider[0] != '\0')
458 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
459 disk->d_sync.ds_consumer = NULL;
460 disk->d_sync.ds_offset = md->md_sync_offset;
461 disk->d_sync.ds_offset_done = md->md_sync_offset;
462 disk->d_genid = md->md_genid;
463 disk->d_sync.ds_syncid = md->md_syncid;
471 free(disk, M_MIRROR);
476 g_mirror_destroy_disk(struct g_mirror_disk *disk)
478 struct g_mirror_softc *sc;
480 g_topology_assert_not();
482 sx_assert(&sc->sc_lock, SX_XLOCKED);
484 LIST_REMOVE(disk, d_next);
485 g_mirror_event_cancel(disk);
486 if (sc->sc_hint == disk)
488 switch (disk->d_state) {
489 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
490 g_mirror_sync_stop(disk, 1);
492 case G_MIRROR_DISK_STATE_NEW:
493 case G_MIRROR_DISK_STATE_STALE:
494 case G_MIRROR_DISK_STATE_ACTIVE:
496 g_mirror_disconnect_consumer(sc, disk->d_consumer);
498 free(disk, M_MIRROR);
501 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
502 g_mirror_get_diskname(disk),
503 g_mirror_disk_state2str(disk->d_state)));
508 g_mirror_destroy_device(struct g_mirror_softc *sc)
510 struct g_mirror_disk *disk;
511 struct g_mirror_event *ep;
513 struct g_consumer *cp, *tmpcp;
515 g_topology_assert_not();
516 sx_assert(&sc->sc_lock, SX_XLOCKED);
519 if (sc->sc_provider != NULL)
520 g_mirror_destroy_provider(sc);
521 for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL;
522 disk = LIST_FIRST(&sc->sc_disks)) {
523 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
524 g_mirror_update_metadata(disk);
525 g_mirror_destroy_disk(disk);
527 while ((ep = g_mirror_event_get(sc)) != NULL) {
528 g_mirror_event_remove(sc, ep);
529 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
530 g_mirror_event_free(ep);
532 ep->e_error = ECANCELED;
533 ep->e_flags |= G_MIRROR_EVENT_DONE;
534 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep);
535 mtx_lock(&sc->sc_events_mtx);
537 mtx_unlock(&sc->sc_events_mtx);
540 callout_drain(&sc->sc_callout);
543 LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
544 g_mirror_disconnect_consumer(sc, cp);
546 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
547 G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
548 g_wither_geom(gp, ENXIO);
550 mtx_destroy(&sc->sc_queue_mtx);
551 mtx_destroy(&sc->sc_events_mtx);
552 sx_xunlock(&sc->sc_lock);
553 sx_destroy(&sc->sc_lock);
557 g_mirror_orphan(struct g_consumer *cp)
559 struct g_mirror_disk *disk;
566 disk->d_softc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
567 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
568 G_MIRROR_EVENT_DONTWAIT);
572 * Function should return the next active disk on the list.
573 * It is possible that it will be the same disk as given.
574 * If there are no active disks on list, NULL is returned.
576 static __inline struct g_mirror_disk *
577 g_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
579 struct g_mirror_disk *dp;
581 for (dp = LIST_NEXT(disk, d_next); dp != disk;
582 dp = LIST_NEXT(dp, d_next)) {
584 dp = LIST_FIRST(&sc->sc_disks);
585 if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE)
588 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
593 static struct g_mirror_disk *
594 g_mirror_get_disk(struct g_mirror_softc *sc)
596 struct g_mirror_disk *disk;
598 if (sc->sc_hint == NULL) {
599 sc->sc_hint = LIST_FIRST(&sc->sc_disks);
600 if (sc->sc_hint == NULL)
604 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) {
605 disk = g_mirror_find_next(sc, disk);
609 sc->sc_hint = g_mirror_find_next(sc, disk);
614 g_mirror_write_metadata(struct g_mirror_disk *disk,
615 struct g_mirror_metadata *md)
617 struct g_mirror_softc *sc;
618 struct g_consumer *cp;
619 off_t offset, length;
623 g_topology_assert_not();
625 sx_assert(&sc->sc_lock, SX_LOCKED);
627 cp = disk->d_consumer;
628 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
629 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
630 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
631 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
633 length = cp->provider->sectorsize;
634 offset = cp->provider->mediasize - length;
635 sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO);
637 mirror_metadata_encode(md, sector);
638 error = g_write_data(cp, offset, sector, length);
639 free(sector, M_MIRROR);
641 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
642 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
643 G_MIRROR_DEBUG(0, "Cannot write metadata on %s "
644 "(device=%s, error=%d).",
645 g_mirror_get_diskname(disk), sc->sc_name, error);
647 G_MIRROR_DEBUG(1, "Cannot write metadata on %s "
648 "(device=%s, error=%d).",
649 g_mirror_get_diskname(disk), sc->sc_name, error);
651 if (g_mirror_disconnect_on_failure &&
652 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) {
653 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
654 g_mirror_event_send(disk,
655 G_MIRROR_DISK_STATE_DISCONNECTED,
656 G_MIRROR_EVENT_DONTWAIT);
663 g_mirror_clear_metadata(struct g_mirror_disk *disk)
667 g_topology_assert_not();
668 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
670 error = g_mirror_write_metadata(disk, NULL);
672 G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
673 g_mirror_get_diskname(disk));
676 "Cannot clear metadata on disk %s (error=%d).",
677 g_mirror_get_diskname(disk), error);
683 g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
684 struct g_mirror_metadata *md)
687 strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
688 md->md_version = G_MIRROR_VERSION;
689 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
690 md->md_mid = sc->sc_id;
691 md->md_all = sc->sc_ndisks;
692 md->md_slice = sc->sc_slice;
693 md->md_balance = sc->sc_balance;
694 md->md_genid = sc->sc_genid;
695 md->md_mediasize = sc->sc_mediasize;
696 md->md_sectorsize = sc->sc_sectorsize;
697 md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
698 bzero(md->md_provider, sizeof(md->md_provider));
700 md->md_did = arc4random();
704 md->md_sync_offset = 0;
707 md->md_did = disk->d_id;
708 md->md_priority = disk->d_priority;
709 md->md_syncid = disk->d_sync.ds_syncid;
710 md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
711 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
712 md->md_sync_offset = disk->d_sync.ds_offset_done;
714 md->md_sync_offset = 0;
715 if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
716 strlcpy(md->md_provider,
717 disk->d_consumer->provider->name,
718 sizeof(md->md_provider));
720 md->md_provsize = disk->d_consumer->provider->mediasize;
725 g_mirror_update_metadata(struct g_mirror_disk *disk)
727 struct g_mirror_softc *sc;
728 struct g_mirror_metadata md;
731 g_topology_assert_not();
733 sx_assert(&sc->sc_lock, SX_LOCKED);
735 g_mirror_fill_metadata(sc, disk, &md);
736 error = g_mirror_write_metadata(disk, &md);
738 G_MIRROR_DEBUG(2, "Metadata on %s updated.",
739 g_mirror_get_diskname(disk));
742 "Cannot update metadata on disk %s (error=%d).",
743 g_mirror_get_diskname(disk), error);
748 g_mirror_bump_syncid(struct g_mirror_softc *sc)
750 struct g_mirror_disk *disk;
752 g_topology_assert_not();
753 sx_assert(&sc->sc_lock, SX_XLOCKED);
754 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
755 ("%s called with no active disks (device=%s).", __func__,
759 G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
761 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
762 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
763 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
764 disk->d_sync.ds_syncid = sc->sc_syncid;
765 g_mirror_update_metadata(disk);
771 g_mirror_bump_genid(struct g_mirror_softc *sc)
773 struct g_mirror_disk *disk;
775 g_topology_assert_not();
776 sx_assert(&sc->sc_lock, SX_XLOCKED);
777 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
778 ("%s called with no active disks (device=%s).", __func__,
782 G_MIRROR_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
784 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
785 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
786 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
787 disk->d_genid = sc->sc_genid;
788 g_mirror_update_metadata(disk);
794 g_mirror_idle(struct g_mirror_softc *sc, int acw)
796 struct g_mirror_disk *disk;
799 g_topology_assert_not();
800 sx_assert(&sc->sc_lock, SX_XLOCKED);
802 if (sc->sc_provider == NULL)
804 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
808 if (sc->sc_writes > 0)
810 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
811 timeout = g_mirror_idletime - (time_uptime - sc->sc_last_write);
816 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
817 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
819 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
820 g_mirror_get_diskname(disk), sc->sc_name);
821 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
822 g_mirror_update_metadata(disk);
828 g_mirror_unidle(struct g_mirror_softc *sc)
830 struct g_mirror_disk *disk;
832 g_topology_assert_not();
833 sx_assert(&sc->sc_lock, SX_XLOCKED);
835 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
838 sc->sc_last_write = time_uptime;
839 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
840 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
842 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
843 g_mirror_get_diskname(disk), sc->sc_name);
844 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
845 g_mirror_update_metadata(disk);
850 g_mirror_done(struct bio *bp)
852 struct g_mirror_softc *sc;
854 sc = bp->bio_from->geom->softc;
855 bp->bio_cflags = G_MIRROR_BIO_FLAG_REGULAR;
856 mtx_lock(&sc->sc_queue_mtx);
857 bioq_disksort(&sc->sc_queue, bp);
858 mtx_unlock(&sc->sc_queue_mtx);
863 g_mirror_regular_request(struct bio *bp)
865 struct g_mirror_softc *sc;
866 struct g_mirror_disk *disk;
869 g_topology_assert_not();
871 pbp = bp->bio_parent;
872 sc = pbp->bio_to->geom->softc;
873 bp->bio_from->index--;
874 if (bp->bio_cmd == BIO_WRITE)
876 disk = bp->bio_from->private;
879 g_mirror_kill_consumer(sc, bp->bio_from);
884 KASSERT(pbp->bio_inbed <= pbp->bio_children,
885 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
887 if (bp->bio_error == 0 && pbp->bio_error == 0) {
888 G_MIRROR_LOGREQ(3, bp, "Request delivered.");
890 if (pbp->bio_children == pbp->bio_inbed) {
891 G_MIRROR_LOGREQ(3, pbp, "Request delivered.");
892 pbp->bio_completed = pbp->bio_length;
893 if (pbp->bio_cmd == BIO_WRITE) {
894 bioq_remove(&sc->sc_inflight, pbp);
895 /* Release delayed sync requests if possible. */
896 g_mirror_sync_release(sc);
898 g_io_deliver(pbp, pbp->bio_error);
901 } else if (bp->bio_error != 0) {
902 if (pbp->bio_error == 0)
903 pbp->bio_error = bp->bio_error;
905 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
906 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
907 G_MIRROR_LOGREQ(0, bp,
908 "Request failed (error=%d).",
911 G_MIRROR_LOGREQ(1, bp,
912 "Request failed (error=%d).",
915 if (g_mirror_disconnect_on_failure &&
916 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1)
918 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
919 g_mirror_event_send(disk,
920 G_MIRROR_DISK_STATE_DISCONNECTED,
921 G_MIRROR_EVENT_DONTWAIT);
924 switch (pbp->bio_cmd) {
934 switch (pbp->bio_cmd) {
936 if (pbp->bio_inbed < pbp->bio_children)
938 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1)
939 g_io_deliver(pbp, pbp->bio_error);
942 mtx_lock(&sc->sc_queue_mtx);
943 bioq_disksort(&sc->sc_queue, pbp);
944 mtx_unlock(&sc->sc_queue_mtx);
945 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
951 if (pbp->bio_children == 0) {
953 * All requests failed.
955 } else if (pbp->bio_inbed < pbp->bio_children) {
958 } else if (pbp->bio_children == pbp->bio_inbed) {
959 /* Some requests succeeded. */
961 pbp->bio_completed = pbp->bio_length;
963 bioq_remove(&sc->sc_inflight, pbp);
964 /* Release delayed sync requests if possible. */
965 g_mirror_sync_release(sc);
966 g_io_deliver(pbp, pbp->bio_error);
969 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
975 g_mirror_sync_done(struct bio *bp)
977 struct g_mirror_softc *sc;
979 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
980 sc = bp->bio_from->geom->softc;
981 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
982 mtx_lock(&sc->sc_queue_mtx);
983 bioq_disksort(&sc->sc_queue, bp);
984 mtx_unlock(&sc->sc_queue_mtx);
989 g_mirror_kernel_dump(struct bio *bp)
991 struct g_mirror_softc *sc;
992 struct g_mirror_disk *disk;
994 struct g_kerneldump *gkd;
997 * We configure dumping to the first component, because this component
998 * will be used for reading with 'prefer' balance algorithm.
999 * If the component with the higest priority is currently disconnected
1000 * we will not be able to read the dump after the reboot if it will be
1001 * connected and synchronized later. Can we do something better?
1003 sc = bp->bio_to->geom->softc;
1004 disk = LIST_FIRST(&sc->sc_disks);
1006 gkd = (struct g_kerneldump *)bp->bio_data;
1007 if (gkd->length > bp->bio_to->mediasize)
1008 gkd->length = bp->bio_to->mediasize;
1009 cbp = g_clone_bio(bp);
1011 g_io_deliver(bp, ENOMEM);
1014 cbp->bio_done = g_std_done;
1015 g_io_request(cbp, disk->d_consumer);
1016 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1017 g_mirror_get_diskname(disk));
1021 g_mirror_flush(struct g_mirror_softc *sc, struct bio *bp)
1023 struct bio_queue_head queue;
1024 struct g_mirror_disk *disk;
1025 struct g_consumer *cp;
1029 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1030 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1032 cbp = g_clone_bio(bp);
1034 for (cbp = bioq_first(&queue); cbp != NULL;
1035 cbp = bioq_first(&queue)) {
1036 bioq_remove(&queue, cbp);
1039 if (bp->bio_error == 0)
1040 bp->bio_error = ENOMEM;
1041 g_io_deliver(bp, bp->bio_error);
1044 bioq_insert_tail(&queue, cbp);
1045 cbp->bio_done = g_std_done;
1046 cbp->bio_caller1 = disk;
1047 cbp->bio_to = disk->d_consumer->provider;
1049 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1050 bioq_remove(&queue, cbp);
1051 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1052 disk = cbp->bio_caller1;
1053 cbp->bio_caller1 = NULL;
1054 cp = disk->d_consumer;
1055 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1056 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1057 cp->acr, cp->acw, cp->ace));
1058 g_io_request(cbp, disk->d_consumer);
1063 g_mirror_start(struct bio *bp)
1065 struct g_mirror_softc *sc;
1067 sc = bp->bio_to->geom->softc;
1069 * If sc == NULL or there are no valid disks, provider's error
1070 * should be set and g_mirror_start() should not be called at all.
1072 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1073 ("Provider's error should be set (error=%d)(mirror=%s).",
1074 bp->bio_to->error, bp->bio_to->name));
1075 G_MIRROR_LOGREQ(3, bp, "Request received.");
1077 switch (bp->bio_cmd) {
1083 g_mirror_flush(sc, bp);
1086 if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1087 g_mirror_kernel_dump(bp);
1092 g_io_deliver(bp, EOPNOTSUPP);
1095 mtx_lock(&sc->sc_queue_mtx);
1096 bioq_disksort(&sc->sc_queue, bp);
1097 mtx_unlock(&sc->sc_queue_mtx);
1098 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1103 * Return TRUE if the given request is colliding with a in-progress
1104 * synchronization request.
1107 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1109 struct g_mirror_disk *disk;
1111 off_t rstart, rend, sstart, send;
1114 if (sc->sc_sync.ds_ndisks == 0)
1116 rstart = bp->bio_offset;
1117 rend = bp->bio_offset + bp->bio_length;
1118 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1119 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1121 for (i = 0; i < g_mirror_syncreqs; i++) {
1122 sbp = disk->d_sync.ds_bios[i];
1125 sstart = sbp->bio_offset;
1126 send = sbp->bio_offset + sbp->bio_length;
1127 if (rend > sstart && rstart < send)
1135 * Return TRUE if the given sync request is colliding with a in-progress regular
1139 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1141 off_t rstart, rend, sstart, send;
1144 if (sc->sc_sync.ds_ndisks == 0)
1146 sstart = sbp->bio_offset;
1147 send = sbp->bio_offset + sbp->bio_length;
1148 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1149 rstart = bp->bio_offset;
1150 rend = bp->bio_offset + bp->bio_length;
1151 if (rend > sstart && rstart < send)
1158 * Puts request onto delayed queue.
1161 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1164 G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1165 bioq_insert_head(&sc->sc_regular_delayed, bp);
1169 * Puts synchronization request onto delayed queue.
1172 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1175 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1176 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1180 * Releases delayed regular requests which don't collide anymore with sync
1184 g_mirror_regular_release(struct g_mirror_softc *sc)
1186 struct bio *bp, *bp2;
1188 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1189 if (g_mirror_sync_collision(sc, bp))
1191 bioq_remove(&sc->sc_regular_delayed, bp);
1192 G_MIRROR_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1193 mtx_lock(&sc->sc_queue_mtx);
1194 bioq_insert_head(&sc->sc_queue, bp);
1197 * wakeup() is not needed, because this function is called from
1198 * the worker thread.
1200 wakeup(&sc->sc_queue);
1202 mtx_unlock(&sc->sc_queue_mtx);
1207 * Releases delayed sync requests which don't collide anymore with regular
1211 g_mirror_sync_release(struct g_mirror_softc *sc)
1213 struct bio *bp, *bp2;
1215 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1216 if (g_mirror_regular_collision(sc, bp))
1218 bioq_remove(&sc->sc_sync_delayed, bp);
1219 G_MIRROR_LOGREQ(2, bp,
1220 "Releasing delayed synchronization request.");
1221 g_io_request(bp, bp->bio_from);
1226 * Handle synchronization requests.
1227 * Every synchronization request is two-steps process: first, READ request is
1228 * send to active provider and then WRITE request (with read data) to the provider
1229 * beeing synchronized. When WRITE is finished, new synchronization request is
1233 g_mirror_sync_request(struct bio *bp)
1235 struct g_mirror_softc *sc;
1236 struct g_mirror_disk *disk;
1238 bp->bio_from->index--;
1239 sc = bp->bio_from->geom->softc;
1240 disk = bp->bio_from->private;
1242 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1244 g_mirror_kill_consumer(sc, bp->bio_from);
1245 g_topology_unlock();
1246 free(bp->bio_data, M_MIRROR);
1248 sx_xlock(&sc->sc_lock);
1253 * Synchronization request.
1255 switch (bp->bio_cmd) {
1258 struct g_consumer *cp;
1260 if (bp->bio_error != 0) {
1261 G_MIRROR_LOGREQ(0, bp,
1262 "Synchronization request failed (error=%d).",
1267 G_MIRROR_LOGREQ(3, bp,
1268 "Synchronization request half-finished.");
1269 bp->bio_cmd = BIO_WRITE;
1271 cp = disk->d_consumer;
1272 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1273 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1274 cp->acr, cp->acw, cp->ace));
1276 g_io_request(bp, cp);
1281 struct g_mirror_disk_sync *sync;
1286 if (bp->bio_error != 0) {
1287 G_MIRROR_LOGREQ(0, bp,
1288 "Synchronization request failed (error=%d).",
1291 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1292 g_mirror_event_send(disk,
1293 G_MIRROR_DISK_STATE_DISCONNECTED,
1294 G_MIRROR_EVENT_DONTWAIT);
1297 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1298 sync = &disk->d_sync;
1299 if (sync->ds_offset == sc->sc_mediasize ||
1300 sync->ds_consumer == NULL ||
1301 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1302 /* Don't send more synchronization requests. */
1303 sync->ds_inflight--;
1304 if (sync->ds_bios != NULL) {
1305 i = (int)(uintptr_t)bp->bio_caller1;
1306 sync->ds_bios[i] = NULL;
1308 free(bp->bio_data, M_MIRROR);
1310 if (sync->ds_inflight > 0)
1312 if (sync->ds_consumer == NULL ||
1313 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1316 /* Disk up-to-date, activate it. */
1317 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1318 G_MIRROR_EVENT_DONTWAIT);
1322 /* Send next synchronization request. */
1323 data = bp->bio_data;
1324 bzero(bp, sizeof(*bp));
1325 bp->bio_cmd = BIO_READ;
1326 bp->bio_offset = sync->ds_offset;
1327 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1328 sync->ds_offset += bp->bio_length;
1329 bp->bio_done = g_mirror_sync_done;
1330 bp->bio_data = data;
1331 bp->bio_from = sync->ds_consumer;
1332 bp->bio_to = sc->sc_provider;
1333 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1334 sync->ds_consumer->index++;
1336 * Delay the request if it is colliding with a regular request.
1338 if (g_mirror_regular_collision(sc, bp))
1339 g_mirror_sync_delay(sc, bp);
1341 g_io_request(bp, sync->ds_consumer);
1343 /* Release delayed requests if possible. */
1344 g_mirror_regular_release(sc);
1346 /* Find the smallest offset */
1347 offset = sc->sc_mediasize;
1348 for (i = 0; i < g_mirror_syncreqs; i++) {
1349 bp = sync->ds_bios[i];
1350 if (bp->bio_offset < offset)
1351 offset = bp->bio_offset;
1353 if (sync->ds_offset_done + (MAXPHYS * 100) < offset) {
1354 /* Update offset_done on every 100 blocks. */
1355 sync->ds_offset_done = offset;
1356 g_mirror_update_metadata(disk);
1361 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1362 bp->bio_cmd, sc->sc_name));
1368 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1370 struct g_mirror_disk *disk;
1371 struct g_consumer *cp;
1374 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1375 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1379 if (bp->bio_error == 0)
1380 bp->bio_error = ENXIO;
1381 g_io_deliver(bp, bp->bio_error);
1384 cbp = g_clone_bio(bp);
1386 if (bp->bio_error == 0)
1387 bp->bio_error = ENOMEM;
1388 g_io_deliver(bp, bp->bio_error);
1392 * Fill in the component buf structure.
1394 cp = disk->d_consumer;
1395 cbp->bio_done = g_mirror_done;
1396 cbp->bio_to = cp->provider;
1397 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1398 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1399 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1402 g_io_request(cbp, cp);
1406 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1408 struct g_mirror_disk *disk;
1409 struct g_consumer *cp;
1412 disk = g_mirror_get_disk(sc);
1414 if (bp->bio_error == 0)
1415 bp->bio_error = ENXIO;
1416 g_io_deliver(bp, bp->bio_error);
1419 cbp = g_clone_bio(bp);
1421 if (bp->bio_error == 0)
1422 bp->bio_error = ENOMEM;
1423 g_io_deliver(bp, bp->bio_error);
1427 * Fill in the component buf structure.
1429 cp = disk->d_consumer;
1430 cbp->bio_done = g_mirror_done;
1431 cbp->bio_to = cp->provider;
1432 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1433 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1434 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1437 g_io_request(cbp, cp);
1440 #define TRACK_SIZE (1 * 1024 * 1024)
1441 #define LOAD_SCALE 256
1442 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
1445 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1447 struct g_mirror_disk *disk, *dp;
1448 struct g_consumer *cp;
1452 /* Find a disk with the smallest load. */
1455 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1456 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1459 /* If disk head is precisely in position - highly prefer it. */
1460 if (dp->d_last_offset == bp->bio_offset)
1461 prio -= 2 * LOAD_SCALE;
1463 /* If disk head is close to position - prefer it. */
1464 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1465 prio -= 1 * LOAD_SCALE;
1471 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1472 cbp = g_clone_bio(bp);
1474 if (bp->bio_error == 0)
1475 bp->bio_error = ENOMEM;
1476 g_io_deliver(bp, bp->bio_error);
1480 * Fill in the component buf structure.
1482 cp = disk->d_consumer;
1483 cbp->bio_done = g_mirror_done;
1484 cbp->bio_to = cp->provider;
1485 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1486 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1487 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1490 /* Remember last head position */
1491 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1493 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1494 dp->load = (dp->d_consumer->index * LOAD_SCALE +
1497 g_io_request(cbp, cp);
1501 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1503 struct bio_queue_head queue;
1504 struct g_mirror_disk *disk;
1505 struct g_consumer *cp;
1507 off_t left, mod, offset, slice;
1511 if (bp->bio_length <= sc->sc_slice) {
1512 g_mirror_request_round_robin(sc, bp);
1515 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1516 slice = bp->bio_length / ndisks;
1517 mod = slice % sc->sc_provider->sectorsize;
1519 slice += sc->sc_provider->sectorsize - mod;
1521 * Allocate all bios before sending any request, so we can
1522 * return ENOMEM in nice and clean way.
1524 left = bp->bio_length;
1525 offset = bp->bio_offset;
1526 data = bp->bio_data;
1528 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1529 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1531 cbp = g_clone_bio(bp);
1533 for (cbp = bioq_first(&queue); cbp != NULL;
1534 cbp = bioq_first(&queue)) {
1535 bioq_remove(&queue, cbp);
1538 if (bp->bio_error == 0)
1539 bp->bio_error = ENOMEM;
1540 g_io_deliver(bp, bp->bio_error);
1543 bioq_insert_tail(&queue, cbp);
1544 cbp->bio_done = g_mirror_done;
1545 cbp->bio_caller1 = disk;
1546 cbp->bio_to = disk->d_consumer->provider;
1547 cbp->bio_offset = offset;
1548 cbp->bio_data = data;
1549 cbp->bio_length = MIN(left, slice);
1550 left -= cbp->bio_length;
1553 offset += cbp->bio_length;
1554 data += cbp->bio_length;
1556 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1557 bioq_remove(&queue, cbp);
1558 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1559 disk = cbp->bio_caller1;
1560 cbp->bio_caller1 = NULL;
1561 cp = disk->d_consumer;
1562 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1563 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1564 cp->acr, cp->acw, cp->ace));
1565 disk->d_consumer->index++;
1566 g_io_request(cbp, disk->d_consumer);
1571 g_mirror_register_request(struct bio *bp)
1573 struct g_mirror_softc *sc;
1575 sc = bp->bio_to->geom->softc;
1576 switch (bp->bio_cmd) {
1578 switch (sc->sc_balance) {
1579 case G_MIRROR_BALANCE_LOAD:
1580 g_mirror_request_load(sc, bp);
1582 case G_MIRROR_BALANCE_PREFER:
1583 g_mirror_request_prefer(sc, bp);
1585 case G_MIRROR_BALANCE_ROUND_ROBIN:
1586 g_mirror_request_round_robin(sc, bp);
1588 case G_MIRROR_BALANCE_SPLIT:
1589 g_mirror_request_split(sc, bp);
1596 struct g_mirror_disk *disk;
1597 struct g_mirror_disk_sync *sync;
1598 struct bio_queue_head queue;
1599 struct g_consumer *cp;
1603 * Delay the request if it is colliding with a synchronization
1606 if (g_mirror_sync_collision(sc, bp)) {
1607 g_mirror_regular_delay(sc, bp);
1612 g_mirror_unidle(sc);
1614 sc->sc_last_write = time_uptime;
1617 * Allocate all bios before sending any request, so we can
1618 * return ENOMEM in nice and clean way.
1621 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1622 sync = &disk->d_sync;
1623 switch (disk->d_state) {
1624 case G_MIRROR_DISK_STATE_ACTIVE:
1626 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1627 if (bp->bio_offset >= sync->ds_offset)
1633 cbp = g_clone_bio(bp);
1635 for (cbp = bioq_first(&queue); cbp != NULL;
1636 cbp = bioq_first(&queue)) {
1637 bioq_remove(&queue, cbp);
1640 if (bp->bio_error == 0)
1641 bp->bio_error = ENOMEM;
1642 g_io_deliver(bp, bp->bio_error);
1645 bioq_insert_tail(&queue, cbp);
1646 cbp->bio_done = g_mirror_done;
1647 cp = disk->d_consumer;
1648 cbp->bio_caller1 = cp;
1649 cbp->bio_to = cp->provider;
1650 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1651 ("Consumer %s not opened (r%dw%de%d).",
1652 cp->provider->name, cp->acr, cp->acw, cp->ace));
1654 for (cbp = bioq_first(&queue); cbp != NULL;
1655 cbp = bioq_first(&queue)) {
1656 bioq_remove(&queue, cbp);
1657 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1658 cp = cbp->bio_caller1;
1659 cbp->bio_caller1 = NULL;
1662 g_io_request(cbp, cp);
1665 * Put request onto inflight queue, so we can check if new
1666 * synchronization requests don't collide with it.
1668 bioq_insert_tail(&sc->sc_inflight, bp);
1670 * Bump syncid on first write.
1672 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1673 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1674 g_mirror_bump_syncid(sc);
1679 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1680 bp->bio_cmd, sc->sc_name));
1686 g_mirror_can_destroy(struct g_mirror_softc *sc)
1689 struct g_consumer *cp;
1691 g_topology_assert();
1693 if (gp->softc == NULL)
1695 LIST_FOREACH(cp, &gp->consumer, consumer) {
1696 if (g_mirror_is_busy(sc, cp))
1699 gp = sc->sc_sync.ds_geom;
1700 LIST_FOREACH(cp, &gp->consumer, consumer) {
1701 if (g_mirror_is_busy(sc, cp))
1704 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1710 g_mirror_try_destroy(struct g_mirror_softc *sc)
1713 if (sc->sc_rootmount != NULL) {
1714 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1716 root_mount_rel(sc->sc_rootmount);
1717 sc->sc_rootmount = NULL;
1720 if (!g_mirror_can_destroy(sc)) {
1721 g_topology_unlock();
1724 sc->sc_geom->softc = NULL;
1725 sc->sc_sync.ds_geom->softc = NULL;
1726 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WAIT) != 0) {
1727 g_topology_unlock();
1728 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1730 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
1731 sx_xunlock(&sc->sc_lock);
1732 wakeup(&sc->sc_worker);
1733 sc->sc_worker = NULL;
1735 g_topology_unlock();
1736 g_mirror_destroy_device(sc);
1746 g_mirror_worker(void *arg)
1748 struct g_mirror_softc *sc;
1749 struct g_mirror_event *ep;
1754 thread_lock(curthread);
1755 sched_prio(curthread, PRIBIO);
1756 thread_unlock(curthread);
1758 sx_xlock(&sc->sc_lock);
1760 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1762 * First take a look at events.
1763 * This is important to handle events before any I/O requests.
1765 ep = g_mirror_event_get(sc);
1767 g_mirror_event_remove(sc, ep);
1768 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1769 /* Update only device status. */
1771 "Running event for device %s.",
1774 g_mirror_update_device(sc, 1);
1776 /* Update disk status. */
1777 G_MIRROR_DEBUG(3, "Running event for disk %s.",
1778 g_mirror_get_diskname(ep->e_disk));
1779 ep->e_error = g_mirror_update_disk(ep->e_disk,
1781 if (ep->e_error == 0)
1782 g_mirror_update_device(sc, 0);
1784 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1785 KASSERT(ep->e_error == 0,
1786 ("Error cannot be handled."));
1787 g_mirror_event_free(ep);
1789 ep->e_flags |= G_MIRROR_EVENT_DONE;
1790 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1792 mtx_lock(&sc->sc_events_mtx);
1794 mtx_unlock(&sc->sc_events_mtx);
1797 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1798 if (g_mirror_try_destroy(sc)) {
1799 curthread->td_pflags &= ~TDP_GEOM;
1800 G_MIRROR_DEBUG(1, "Thread exiting.");
1804 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1808 * Check if we can mark array as CLEAN and if we can't take
1809 * how much seconds should we wait.
1811 timeout = g_mirror_idle(sc, -1);
1815 /* Get first request from the queue. */
1816 mtx_lock(&sc->sc_queue_mtx);
1817 bp = bioq_first(&sc->sc_queue);
1820 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1821 mtx_unlock(&sc->sc_queue_mtx);
1822 if (g_mirror_try_destroy(sc)) {
1823 curthread->td_pflags &= ~TDP_GEOM;
1824 G_MIRROR_DEBUG(1, "Thread exiting.");
1827 mtx_lock(&sc->sc_queue_mtx);
1829 sx_xunlock(&sc->sc_lock);
1831 * XXX: We can miss an event here, because an event
1832 * can be added without sx-device-lock and without
1833 * mtx-queue-lock. Maybe I should just stop using
1834 * dedicated mutex for events synchronization and
1835 * stick with the queue lock?
1836 * The event will hang here until next I/O request
1837 * or next event is received.
1839 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
1841 sx_xlock(&sc->sc_lock);
1842 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
1845 bioq_remove(&sc->sc_queue, bp);
1846 mtx_unlock(&sc->sc_queue_mtx);
1848 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
1849 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
1850 g_mirror_sync_request(bp); /* READ */
1851 } else if (bp->bio_to != sc->sc_provider) {
1852 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
1853 g_mirror_regular_request(bp);
1854 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
1855 g_mirror_sync_request(bp); /* WRITE */
1858 ("Invalid request cflags=0x%hhx to=%s.",
1859 bp->bio_cflags, bp->bio_to->name));
1862 g_mirror_register_request(bp);
1864 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
1869 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
1872 sx_assert(&sc->sc_lock, SX_LOCKED);
1874 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
1876 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
1877 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
1878 g_mirror_get_diskname(disk), sc->sc_name);
1879 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1880 } else if (sc->sc_idle &&
1881 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
1882 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
1883 g_mirror_get_diskname(disk), sc->sc_name);
1884 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
1889 g_mirror_sync_start(struct g_mirror_disk *disk)
1891 struct g_mirror_softc *sc;
1892 struct g_consumer *cp;
1896 g_topology_assert_not();
1898 sx_assert(&sc->sc_lock, SX_LOCKED);
1900 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1901 ("Disk %s is not marked for synchronization.",
1902 g_mirror_get_diskname(disk)));
1903 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1904 ("Device not in RUNNING state (%s, %u).", sc->sc_name,
1907 sx_xunlock(&sc->sc_lock);
1909 cp = g_new_consumer(sc->sc_sync.ds_geom);
1910 error = g_attach(cp, sc->sc_provider);
1912 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
1913 error = g_access(cp, 1, 0, 0);
1914 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
1915 g_topology_unlock();
1916 sx_xlock(&sc->sc_lock);
1918 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
1919 g_mirror_get_diskname(disk));
1920 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
1921 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1922 KASSERT(disk->d_sync.ds_consumer == NULL,
1923 ("Sync consumer already exists (device=%s, disk=%s).",
1924 sc->sc_name, g_mirror_get_diskname(disk)));
1926 disk->d_sync.ds_consumer = cp;
1927 disk->d_sync.ds_consumer->private = disk;
1928 disk->d_sync.ds_consumer->index = 0;
1931 * Allocate memory for synchronization bios and initialize them.
1933 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
1934 M_MIRROR, M_WAITOK);
1935 for (i = 0; i < g_mirror_syncreqs; i++) {
1937 disk->d_sync.ds_bios[i] = bp;
1938 bp->bio_parent = NULL;
1939 bp->bio_cmd = BIO_READ;
1940 bp->bio_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK);
1942 bp->bio_offset = disk->d_sync.ds_offset;
1943 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1944 disk->d_sync.ds_offset += bp->bio_length;
1945 bp->bio_done = g_mirror_sync_done;
1946 bp->bio_from = disk->d_sync.ds_consumer;
1947 bp->bio_to = sc->sc_provider;
1948 bp->bio_caller1 = (void *)(uintptr_t)i;
1951 /* Increase the number of disks in SYNCHRONIZING state. */
1952 sc->sc_sync.ds_ndisks++;
1953 /* Set the number of in-flight synchronization requests. */
1954 disk->d_sync.ds_inflight = g_mirror_syncreqs;
1957 * Fire off first synchronization requests.
1959 for (i = 0; i < g_mirror_syncreqs; i++) {
1960 bp = disk->d_sync.ds_bios[i];
1961 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1962 disk->d_sync.ds_consumer->index++;
1964 * Delay the request if it is colliding with a regular request.
1966 if (g_mirror_regular_collision(sc, bp))
1967 g_mirror_sync_delay(sc, bp);
1969 g_io_request(bp, disk->d_sync.ds_consumer);
1974 * Stop synchronization process.
1975 * type: 0 - synchronization finished
1976 * 1 - synchronization stopped
1979 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
1981 struct g_mirror_softc *sc;
1982 struct g_consumer *cp;
1984 g_topology_assert_not();
1986 sx_assert(&sc->sc_lock, SX_LOCKED);
1988 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1989 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
1990 g_mirror_disk_state2str(disk->d_state)));
1991 if (disk->d_sync.ds_consumer == NULL)
1995 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
1996 sc->sc_name, g_mirror_get_diskname(disk));
1997 } else /* if (type == 1) */ {
1998 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
1999 sc->sc_name, g_mirror_get_diskname(disk));
2001 free(disk->d_sync.ds_bios, M_MIRROR);
2002 disk->d_sync.ds_bios = NULL;
2003 cp = disk->d_sync.ds_consumer;
2004 disk->d_sync.ds_consumer = NULL;
2005 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2006 sc->sc_sync.ds_ndisks--;
2007 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2009 g_mirror_kill_consumer(sc, cp);
2010 g_topology_unlock();
2011 sx_xlock(&sc->sc_lock);
2015 g_mirror_launch_provider(struct g_mirror_softc *sc)
2017 struct g_mirror_disk *disk;
2018 struct g_provider *pp;
2020 sx_assert(&sc->sc_lock, SX_LOCKED);
2023 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2024 pp->mediasize = sc->sc_mediasize;
2025 pp->sectorsize = sc->sc_sectorsize;
2027 pp->stripeoffset = 0;
2028 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2029 if (disk->d_consumer && disk->d_consumer->provider &&
2030 disk->d_consumer->provider->stripesize > pp->stripesize) {
2031 pp->stripesize = disk->d_consumer->provider->stripesize;
2032 pp->stripeoffset = disk->d_consumer->provider->stripeoffset;
2035 sc->sc_provider = pp;
2036 g_error_provider(pp, 0);
2037 g_topology_unlock();
2038 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2039 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2040 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2041 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2042 g_mirror_sync_start(disk);
2047 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2049 struct g_mirror_disk *disk;
2052 g_topology_assert_not();
2053 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2057 g_error_provider(sc->sc_provider, ENXIO);
2058 mtx_lock(&sc->sc_queue_mtx);
2059 while ((bp = bioq_first(&sc->sc_queue)) != NULL) {
2060 bioq_remove(&sc->sc_queue, bp);
2061 g_io_deliver(bp, ENXIO);
2063 mtx_unlock(&sc->sc_queue_mtx);
2064 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2065 sc->sc_provider->name);
2066 sc->sc_provider->flags |= G_PF_WITHER;
2067 g_orphan_provider(sc->sc_provider, ENXIO);
2068 g_topology_unlock();
2069 sc->sc_provider = NULL;
2070 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2071 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2072 g_mirror_sync_stop(disk, 1);
2077 g_mirror_go(void *arg)
2079 struct g_mirror_softc *sc;
2082 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2083 g_mirror_event_send(sc, 0,
2084 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2088 g_mirror_determine_state(struct g_mirror_disk *disk)
2090 struct g_mirror_softc *sc;
2094 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2095 if ((disk->d_flags &
2096 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2097 /* Disk does not need synchronization. */
2098 state = G_MIRROR_DISK_STATE_ACTIVE;
2101 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2103 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2105 * We can start synchronization from
2106 * the stored offset.
2108 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2110 state = G_MIRROR_DISK_STATE_STALE;
2113 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2115 * Reset all synchronization data for this disk,
2116 * because if it even was synchronized, it was
2117 * synchronized to disks with different syncid.
2119 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2120 disk->d_sync.ds_offset = 0;
2121 disk->d_sync.ds_offset_done = 0;
2122 disk->d_sync.ds_syncid = sc->sc_syncid;
2123 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2124 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2125 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2127 state = G_MIRROR_DISK_STATE_STALE;
2129 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2131 * Not good, NOT GOOD!
2132 * It means that mirror was started on stale disks
2133 * and more fresh disk just arrive.
2134 * If there were writes, mirror is broken, sorry.
2135 * I think the best choice here is don't touch
2136 * this disk and inform the user loudly.
2138 G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2139 "disk (%s) arrives!! It will not be connected to the "
2140 "running device.", sc->sc_name,
2141 g_mirror_get_diskname(disk));
2142 g_mirror_destroy_disk(disk);
2143 state = G_MIRROR_DISK_STATE_NONE;
2144 /* Return immediately, because disk was destroyed. */
2147 G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2148 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2153 * Update device state.
2156 g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force)
2158 struct g_mirror_disk *disk;
2161 sx_assert(&sc->sc_lock, SX_XLOCKED);
2163 switch (sc->sc_state) {
2164 case G_MIRROR_DEVICE_STATE_STARTING:
2166 struct g_mirror_disk *pdisk, *tdisk;
2167 u_int dirty, ndisks, genid, syncid;
2169 KASSERT(sc->sc_provider == NULL,
2170 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2172 * Are we ready? We are, if all disks are connected or
2173 * if we have any disks and 'force' is true.
2175 ndisks = g_mirror_ndisks(sc, -1);
2176 if (sc->sc_ndisks == ndisks || (force && ndisks > 0)) {
2178 } else if (ndisks == 0) {
2180 * Disks went down in starting phase, so destroy
2183 callout_drain(&sc->sc_callout);
2184 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2185 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2187 root_mount_rel(sc->sc_rootmount);
2188 sc->sc_rootmount = NULL;
2195 * Activate all disks with the biggest syncid.
2199 * If 'force' is true, we have been called due to
2200 * timeout, so don't bother canceling timeout.
2203 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2204 if ((disk->d_flags &
2205 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2210 /* No valid disks found, destroy device. */
2211 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2212 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2213 __LINE__, sc->sc_rootmount);
2214 root_mount_rel(sc->sc_rootmount);
2215 sc->sc_rootmount = NULL;
2219 /* Cancel timeout. */
2220 callout_drain(&sc->sc_callout);
2224 * Find the biggest genid.
2227 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2228 if (disk->d_genid > genid)
2229 genid = disk->d_genid;
2231 sc->sc_genid = genid;
2233 * Remove all disks without the biggest genid.
2235 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2236 if (disk->d_genid < genid) {
2238 "Component %s (device %s) broken, skipping.",
2239 g_mirror_get_diskname(disk), sc->sc_name);
2240 g_mirror_destroy_disk(disk);
2245 * Find the biggest syncid.
2248 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2249 if (disk->d_sync.ds_syncid > syncid)
2250 syncid = disk->d_sync.ds_syncid;
2254 * Here we need to look for dirty disks and if all disks
2255 * with the biggest syncid are dirty, we have to choose
2256 * one with the biggest priority and rebuild the rest.
2259 * Find the number of dirty disks with the biggest syncid.
2260 * Find the number of disks with the biggest syncid.
2261 * While here, find a disk with the biggest priority.
2265 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2266 if (disk->d_sync.ds_syncid != syncid)
2268 if ((disk->d_flags &
2269 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2273 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2275 if (pdisk == NULL ||
2276 pdisk->d_priority < disk->d_priority) {
2282 /* No dirty disks at all, great. */
2283 } else if (dirty == ndisks) {
2285 * Force synchronization for all dirty disks except one
2286 * with the biggest priority.
2288 KASSERT(pdisk != NULL, ("pdisk == NULL"));
2289 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2290 "master disk for synchronization.",
2291 g_mirror_get_diskname(pdisk), sc->sc_name);
2292 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2293 if (disk->d_sync.ds_syncid != syncid)
2295 if ((disk->d_flags &
2296 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2299 KASSERT((disk->d_flags &
2300 G_MIRROR_DISK_FLAG_DIRTY) != 0,
2301 ("Disk %s isn't marked as dirty.",
2302 g_mirror_get_diskname(disk)));
2303 /* Skip the disk with the biggest priority. */
2306 disk->d_sync.ds_syncid = 0;
2308 } else if (dirty < ndisks) {
2310 * Force synchronization for all dirty disks.
2311 * We have some non-dirty disks.
2313 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2314 if (disk->d_sync.ds_syncid != syncid)
2316 if ((disk->d_flags &
2317 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2320 if ((disk->d_flags &
2321 G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2324 disk->d_sync.ds_syncid = 0;
2330 sc->sc_syncid = syncid;
2332 /* Remember to bump syncid on first write. */
2333 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2335 state = G_MIRROR_DEVICE_STATE_RUNNING;
2336 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2337 sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2338 g_mirror_device_state2str(state));
2339 sc->sc_state = state;
2340 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2341 state = g_mirror_determine_state(disk);
2342 g_mirror_event_send(disk, state,
2343 G_MIRROR_EVENT_DONTWAIT);
2344 if (state == G_MIRROR_DISK_STATE_STALE)
2345 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2349 case G_MIRROR_DEVICE_STATE_RUNNING:
2350 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2351 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2353 * No active disks or no disks at all,
2354 * so destroy device.
2356 if (sc->sc_provider != NULL)
2357 g_mirror_destroy_provider(sc);
2358 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2360 } else if (g_mirror_ndisks(sc,
2361 G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2362 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2364 * We have active disks, launch provider if it doesn't
2367 if (sc->sc_provider == NULL)
2368 g_mirror_launch_provider(sc);
2369 if (sc->sc_rootmount != NULL) {
2370 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2371 __LINE__, sc->sc_rootmount);
2372 root_mount_rel(sc->sc_rootmount);
2373 sc->sc_rootmount = NULL;
2377 * Genid should be bumped immediately, so do it here.
2379 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2380 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2381 g_mirror_bump_genid(sc);
2385 KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2386 sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2392 * Update disk state and device state if needed.
2394 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \
2395 "Disk %s state changed from %s to %s (device %s).", \
2396 g_mirror_get_diskname(disk), \
2397 g_mirror_disk_state2str(disk->d_state), \
2398 g_mirror_disk_state2str(state), sc->sc_name)
2400 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2402 struct g_mirror_softc *sc;
2405 sx_assert(&sc->sc_lock, SX_XLOCKED);
2408 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2409 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2410 g_mirror_disk_state2str(state));
2412 case G_MIRROR_DISK_STATE_NEW:
2414 * Possible scenarios:
2415 * 1. New disk arrive.
2417 /* Previous state should be NONE. */
2418 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2419 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2420 g_mirror_disk_state2str(disk->d_state)));
2421 DISK_STATE_CHANGED();
2423 disk->d_state = state;
2424 if (LIST_EMPTY(&sc->sc_disks))
2425 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2427 struct g_mirror_disk *dp;
2429 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2430 if (disk->d_priority >= dp->d_priority) {
2431 LIST_INSERT_BEFORE(dp, disk, d_next);
2435 if (LIST_NEXT(dp, d_next) == NULL)
2439 LIST_INSERT_AFTER(dp, disk, d_next);
2441 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2442 sc->sc_name, g_mirror_get_diskname(disk));
2443 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2445 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2446 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2447 g_mirror_device_state2str(sc->sc_state),
2448 g_mirror_get_diskname(disk),
2449 g_mirror_disk_state2str(disk->d_state)));
2450 state = g_mirror_determine_state(disk);
2451 if (state != G_MIRROR_DISK_STATE_NONE)
2454 case G_MIRROR_DISK_STATE_ACTIVE:
2456 * Possible scenarios:
2457 * 1. New disk does not need synchronization.
2458 * 2. Synchronization process finished successfully.
2460 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2461 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2462 g_mirror_device_state2str(sc->sc_state),
2463 g_mirror_get_diskname(disk),
2464 g_mirror_disk_state2str(disk->d_state)));
2465 /* Previous state should be NEW or SYNCHRONIZING. */
2466 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2467 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2468 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2469 g_mirror_disk_state2str(disk->d_state)));
2470 DISK_STATE_CHANGED();
2472 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2473 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2474 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2475 g_mirror_sync_stop(disk, 0);
2477 disk->d_state = state;
2478 disk->d_sync.ds_offset = 0;
2479 disk->d_sync.ds_offset_done = 0;
2480 g_mirror_update_idle(sc, disk);
2481 g_mirror_update_metadata(disk);
2482 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2483 sc->sc_name, g_mirror_get_diskname(disk));
2485 case G_MIRROR_DISK_STATE_STALE:
2487 * Possible scenarios:
2488 * 1. Stale disk was connected.
2490 /* Previous state should be NEW. */
2491 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2492 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2493 g_mirror_disk_state2str(disk->d_state)));
2494 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2495 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2496 g_mirror_device_state2str(sc->sc_state),
2497 g_mirror_get_diskname(disk),
2498 g_mirror_disk_state2str(disk->d_state)));
2500 * STALE state is only possible if device is marked
2503 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2504 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2505 g_mirror_device_state2str(sc->sc_state),
2506 g_mirror_get_diskname(disk),
2507 g_mirror_disk_state2str(disk->d_state)));
2508 DISK_STATE_CHANGED();
2510 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2511 disk->d_state = state;
2512 g_mirror_update_metadata(disk);
2513 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2514 sc->sc_name, g_mirror_get_diskname(disk));
2516 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2518 * Possible scenarios:
2519 * 1. Disk which needs synchronization was connected.
2521 /* Previous state should be NEW. */
2522 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2523 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2524 g_mirror_disk_state2str(disk->d_state)));
2525 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2526 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2527 g_mirror_device_state2str(sc->sc_state),
2528 g_mirror_get_diskname(disk),
2529 g_mirror_disk_state2str(disk->d_state)));
2530 DISK_STATE_CHANGED();
2532 if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2533 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2534 disk->d_state = state;
2535 if (sc->sc_provider != NULL) {
2536 g_mirror_sync_start(disk);
2537 g_mirror_update_metadata(disk);
2540 case G_MIRROR_DISK_STATE_DISCONNECTED:
2542 * Possible scenarios:
2543 * 1. Device wasn't running yet, but disk disappear.
2544 * 2. Disk was active and disapppear.
2545 * 3. Disk disappear during synchronization process.
2547 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2549 * Previous state should be ACTIVE, STALE or
2552 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2553 disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2554 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2555 ("Wrong disk state (%s, %s).",
2556 g_mirror_get_diskname(disk),
2557 g_mirror_disk_state2str(disk->d_state)));
2558 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2559 /* Previous state should be NEW. */
2560 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2561 ("Wrong disk state (%s, %s).",
2562 g_mirror_get_diskname(disk),
2563 g_mirror_disk_state2str(disk->d_state)));
2565 * Reset bumping syncid if disk disappeared in STARTING
2568 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2569 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2572 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2574 g_mirror_device_state2str(sc->sc_state),
2575 g_mirror_get_diskname(disk),
2576 g_mirror_disk_state2str(disk->d_state)));
2579 DISK_STATE_CHANGED();
2580 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2581 sc->sc_name, g_mirror_get_diskname(disk));
2583 g_mirror_destroy_disk(disk);
2585 case G_MIRROR_DISK_STATE_DESTROY:
2589 error = g_mirror_clear_metadata(disk);
2592 DISK_STATE_CHANGED();
2593 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2594 sc->sc_name, g_mirror_get_diskname(disk));
2596 g_mirror_destroy_disk(disk);
2598 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2599 g_mirror_update_metadata(disk);
2604 KASSERT(1 == 0, ("Unknown state (%u).", state));
2609 #undef DISK_STATE_CHANGED
2612 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2614 struct g_provider *pp;
2618 g_topology_assert();
2620 error = g_access(cp, 1, 0, 0);
2624 g_topology_unlock();
2625 /* Metadata are stored on last sector. */
2626 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2629 g_access(cp, -1, 0, 0);
2631 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2632 cp->provider->name, error);
2636 /* Decode metadata. */
2637 error = mirror_metadata_decode(buf, md);
2639 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2641 if (md->md_version > G_MIRROR_VERSION) {
2643 "Kernel module is too old to handle metadata from %s.",
2644 cp->provider->name);
2648 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2649 cp->provider->name);
2657 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2658 struct g_mirror_metadata *md)
2661 if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2662 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2663 pp->name, md->md_did);
2666 if (md->md_all != sc->sc_ndisks) {
2668 "Invalid '%s' field on disk %s (device %s), skipping.",
2669 "md_all", pp->name, sc->sc_name);
2672 if (md->md_slice != sc->sc_slice) {
2674 "Invalid '%s' field on disk %s (device %s), skipping.",
2675 "md_slice", pp->name, sc->sc_name);
2678 if (md->md_balance != sc->sc_balance) {
2680 "Invalid '%s' field on disk %s (device %s), skipping.",
2681 "md_balance", pp->name, sc->sc_name);
2684 if (md->md_mediasize != sc->sc_mediasize) {
2686 "Invalid '%s' field on disk %s (device %s), skipping.",
2687 "md_mediasize", pp->name, sc->sc_name);
2690 if (sc->sc_mediasize > pp->mediasize) {
2692 "Invalid size of disk %s (device %s), skipping.", pp->name,
2696 if (md->md_sectorsize != sc->sc_sectorsize) {
2698 "Invalid '%s' field on disk %s (device %s), skipping.",
2699 "md_sectorsize", pp->name, sc->sc_name);
2702 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2704 "Invalid sector size of disk %s (device %s), skipping.",
2705 pp->name, sc->sc_name);
2708 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2710 "Invalid device flags on disk %s (device %s), skipping.",
2711 pp->name, sc->sc_name);
2714 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2716 "Invalid disk flags on disk %s (device %s), skipping.",
2717 pp->name, sc->sc_name);
2724 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2725 struct g_mirror_metadata *md)
2727 struct g_mirror_disk *disk;
2730 g_topology_assert_not();
2731 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2733 error = g_mirror_check_metadata(sc, pp, md);
2736 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING &&
2737 md->md_genid < sc->sc_genid) {
2738 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
2739 pp->name, sc->sc_name);
2742 disk = g_mirror_init_disk(sc, pp, md, &error);
2745 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
2746 G_MIRROR_EVENT_WAIT);
2749 if (md->md_version < G_MIRROR_VERSION) {
2750 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
2751 pp->name, md->md_version, G_MIRROR_VERSION);
2752 g_mirror_update_metadata(disk);
2758 g_mirror_destroy_delayed(void *arg, int flag)
2760 struct g_mirror_softc *sc;
2763 if (flag == EV_CANCEL) {
2764 G_MIRROR_DEBUG(1, "Destroying canceled.");
2768 g_topology_unlock();
2769 sx_xlock(&sc->sc_lock);
2770 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
2771 ("DESTROY flag set on %s.", sc->sc_name));
2772 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0,
2773 ("DESTROYING flag not set on %s.", sc->sc_name));
2774 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
2775 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
2777 G_MIRROR_DEBUG(0, "Cannot destroy %s.", sc->sc_name);
2778 sx_xunlock(&sc->sc_lock);
2784 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
2786 struct g_mirror_softc *sc;
2787 int dcr, dcw, dce, error = 0;
2789 g_topology_assert();
2790 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2793 sc = pp->geom->softc;
2794 if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
2796 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
2798 dcr = pp->acr + acr;
2799 dcw = pp->acw + acw;
2800 dce = pp->ace + ace;
2802 g_topology_unlock();
2803 sx_xlock(&sc->sc_lock);
2804 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
2805 LIST_EMPTY(&sc->sc_disks)) {
2806 if (acr > 0 || acw > 0 || ace > 0)
2810 if (dcw == 0 && !sc->sc_idle)
2811 g_mirror_idle(sc, dcw);
2812 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0) {
2813 if (acr > 0 || acw > 0 || ace > 0) {
2817 if (dcr == 0 && dcw == 0 && dce == 0) {
2818 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK,
2823 sx_xunlock(&sc->sc_lock);
2828 static struct g_geom *
2829 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
2831 struct g_mirror_softc *sc;
2835 g_topology_assert();
2836 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
2839 /* One disk is minimum. */
2845 gp = g_new_geomf(mp, "%s", md->md_name);
2846 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
2847 gp->start = g_mirror_start;
2848 gp->orphan = g_mirror_orphan;
2849 gp->access = g_mirror_access;
2850 gp->dumpconf = g_mirror_dumpconf;
2852 sc->sc_id = md->md_mid;
2853 sc->sc_slice = md->md_slice;
2854 sc->sc_balance = md->md_balance;
2855 sc->sc_mediasize = md->md_mediasize;
2856 sc->sc_sectorsize = md->md_sectorsize;
2857 sc->sc_ndisks = md->md_all;
2858 sc->sc_flags = md->md_mflags;
2861 sc->sc_last_write = time_uptime;
2863 sx_init(&sc->sc_lock, "gmirror:lock");
2864 bioq_init(&sc->sc_queue);
2865 mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
2866 bioq_init(&sc->sc_regular_delayed);
2867 bioq_init(&sc->sc_inflight);
2868 bioq_init(&sc->sc_sync_delayed);
2869 LIST_INIT(&sc->sc_disks);
2870 TAILQ_INIT(&sc->sc_events);
2871 mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
2872 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
2873 sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
2876 sc->sc_provider = NULL;
2878 * Synchronization geom.
2880 gp = g_new_geomf(mp, "%s.sync", md->md_name);
2882 gp->orphan = g_mirror_orphan;
2883 sc->sc_sync.ds_geom = gp;
2884 sc->sc_sync.ds_ndisks = 0;
2885 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
2886 "g_mirror %s", md->md_name);
2888 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
2890 g_destroy_geom(sc->sc_sync.ds_geom);
2891 mtx_destroy(&sc->sc_events_mtx);
2892 mtx_destroy(&sc->sc_queue_mtx);
2893 sx_destroy(&sc->sc_lock);
2894 g_destroy_geom(sc->sc_geom);
2899 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
2900 sc->sc_name, sc->sc_ndisks, sc->sc_id);
2902 sc->sc_rootmount = root_mount_hold("GMIRROR");
2903 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
2907 timeout = g_mirror_timeout * hz;
2908 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
2909 return (sc->sc_geom);
2913 g_mirror_destroy(struct g_mirror_softc *sc, int how)
2915 struct g_mirror_disk *disk;
2916 struct g_provider *pp;
2918 g_topology_assert_not();
2921 sx_assert(&sc->sc_lock, SX_XLOCKED);
2923 pp = sc->sc_provider;
2924 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
2926 case G_MIRROR_DESTROY_SOFT:
2928 "Device %s is still open (r%dw%de%d).", pp->name,
2929 pp->acr, pp->acw, pp->ace);
2931 case G_MIRROR_DESTROY_DELAYED:
2933 "Device %s will be destroyed on last close.",
2935 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2936 if (disk->d_state ==
2937 G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2938 g_mirror_sync_stop(disk, 1);
2941 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROYING;
2943 case G_MIRROR_DESTROY_HARD:
2944 G_MIRROR_DEBUG(1, "Device %s is still open, so it "
2945 "can't be definitely removed.", pp->name);
2950 if (sc->sc_geom->softc == NULL) {
2951 g_topology_unlock();
2954 sc->sc_geom->softc = NULL;
2955 sc->sc_sync.ds_geom->softc = NULL;
2956 g_topology_unlock();
2958 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2959 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WAIT;
2960 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
2961 sx_xunlock(&sc->sc_lock);
2962 mtx_lock(&sc->sc_queue_mtx);
2964 mtx_unlock(&sc->sc_queue_mtx);
2965 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
2966 while (sc->sc_worker != NULL)
2967 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
2968 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
2969 sx_xlock(&sc->sc_lock);
2970 g_mirror_destroy_device(sc);
2976 g_mirror_taste_orphan(struct g_consumer *cp)
2979 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2980 cp->provider->name));
2983 static struct g_geom *
2984 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2986 struct g_mirror_metadata md;
2987 struct g_mirror_softc *sc;
2988 struct g_consumer *cp;
2992 g_topology_assert();
2993 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
2994 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
2996 gp = g_new_geomf(mp, "mirror:taste");
2998 * This orphan function should be never called.
3000 gp->orphan = g_mirror_taste_orphan;
3001 cp = g_new_consumer(gp);
3003 error = g_mirror_read_metadata(cp, &md);
3005 g_destroy_consumer(cp);
3011 if (md.md_provider[0] != '\0' &&
3012 !g_compare_names(md.md_provider, pp->name))
3014 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3016 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3018 "Device %s: provider %s marked as inactive, skipping.",
3019 md.md_name, pp->name);
3022 if (g_mirror_debug >= 2)
3023 mirror_metadata_dump(&md);
3026 * Let's check if device already exists.
3029 LIST_FOREACH(gp, &mp->geom, geom) {
3033 if (sc->sc_sync.ds_geom == gp)
3035 if (strcmp(md.md_name, sc->sc_name) != 0)
3037 if (md.md_mid != sc->sc_id) {
3038 G_MIRROR_DEBUG(0, "Device %s already configured.",
3045 gp = g_mirror_create(mp, &md);
3047 G_MIRROR_DEBUG(0, "Cannot create device %s.",
3053 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3054 g_topology_unlock();
3055 sx_xlock(&sc->sc_lock);
3056 error = g_mirror_add_disk(sc, pp, &md);
3058 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3059 pp->name, gp->name, error);
3060 if (LIST_EMPTY(&sc->sc_disks)) {
3062 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3068 sx_xunlock(&sc->sc_lock);
3074 g_mirror_destroy_geom(struct gctl_req *req __unused,
3075 struct g_class *mp __unused, struct g_geom *gp)
3077 struct g_mirror_softc *sc;
3080 g_topology_unlock();
3082 sx_xlock(&sc->sc_lock);
3084 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3086 sx_xunlock(&sc->sc_lock);
3092 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3093 struct g_consumer *cp, struct g_provider *pp)
3095 struct g_mirror_softc *sc;
3097 g_topology_assert();
3102 /* Skip synchronization geom. */
3103 if (gp == sc->sc_sync.ds_geom)
3107 } else if (cp != NULL) {
3108 struct g_mirror_disk *disk;
3113 g_topology_unlock();
3114 sx_xlock(&sc->sc_lock);
3115 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3116 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3117 sbuf_printf(sb, "%s<Synchronized>", indent);
3118 if (disk->d_sync.ds_offset == 0)
3119 sbuf_printf(sb, "0%%");
3121 sbuf_printf(sb, "%u%%",
3122 (u_int)((disk->d_sync.ds_offset * 100) /
3123 sc->sc_provider->mediasize));
3125 sbuf_printf(sb, "</Synchronized>\n");
3127 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3128 disk->d_sync.ds_syncid);
3129 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3131 sbuf_printf(sb, "%s<Flags>", indent);
3132 if (disk->d_flags == 0)
3133 sbuf_printf(sb, "NONE");
3137 #define ADD_FLAG(flag, name) do { \
3138 if ((disk->d_flags & (flag)) != 0) { \
3140 sbuf_printf(sb, ", "); \
3143 sbuf_printf(sb, name); \
3146 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3147 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3148 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3149 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3151 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3152 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3155 sbuf_printf(sb, "</Flags>\n");
3156 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3158 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3159 g_mirror_disk_state2str(disk->d_state));
3160 sx_xunlock(&sc->sc_lock);
3163 g_topology_unlock();
3164 sx_xlock(&sc->sc_lock);
3165 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3166 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3167 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3168 sbuf_printf(sb, "%s<Flags>", indent);
3169 if (sc->sc_flags == 0)
3170 sbuf_printf(sb, "NONE");
3174 #define ADD_FLAG(flag, name) do { \
3175 if ((sc->sc_flags & (flag)) != 0) { \
3177 sbuf_printf(sb, ", "); \
3180 sbuf_printf(sb, name); \
3183 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3184 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3187 sbuf_printf(sb, "</Flags>\n");
3188 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3189 (u_int)sc->sc_slice);
3190 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3191 balance_name(sc->sc_balance));
3192 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3194 sbuf_printf(sb, "%s<State>", indent);
3195 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3196 sbuf_printf(sb, "%s", "STARTING");
3197 else if (sc->sc_ndisks ==
3198 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3199 sbuf_printf(sb, "%s", "COMPLETE");
3201 sbuf_printf(sb, "%s", "DEGRADED");
3202 sbuf_printf(sb, "</State>\n");
3203 sx_xunlock(&sc->sc_lock);
3209 g_mirror_shutdown_pre_sync(void *arg, int howto)
3212 struct g_geom *gp, *gp2;
3213 struct g_mirror_softc *sc;
3219 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3220 if ((sc = gp->softc) == NULL)
3222 /* Skip synchronization geom. */
3223 if (gp == sc->sc_sync.ds_geom)
3225 g_topology_unlock();
3226 sx_xlock(&sc->sc_lock);
3228 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3230 sx_xunlock(&sc->sc_lock);
3233 g_topology_unlock();
3238 g_mirror_init(struct g_class *mp)
3241 g_mirror_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
3242 g_mirror_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
3243 if (g_mirror_pre_sync == NULL)
3244 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3248 g_mirror_fini(struct g_class *mp)
3251 if (g_mirror_pre_sync != NULL)
3252 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mirror_pre_sync);
3255 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);