2 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/limits.h>
36 #include <sys/mutex.h>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
40 #include <sys/eventhandler.h>
42 #include <geom/geom.h>
44 #include <sys/kthread.h>
45 #include <sys/sched.h>
46 #include <geom/mirror/g_mirror.h>
49 static MALLOC_DEFINE(M_MIRROR, "mirror_data", "GEOM_MIRROR Data");
51 SYSCTL_DECL(_kern_geom);
52 SYSCTL_NODE(_kern_geom, OID_AUTO, mirror, CTLFLAG_RW, 0, "GEOM_MIRROR stuff");
53 u_int g_mirror_debug = 0;
54 TUNABLE_INT("kern.geom.mirror.debug", &g_mirror_debug);
55 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, debug, CTLFLAG_RW, &g_mirror_debug, 0,
57 static u_int g_mirror_timeout = 4;
58 TUNABLE_INT("kern.geom.mirror.timeout", &g_mirror_timeout);
59 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, timeout, CTLFLAG_RW, &g_mirror_timeout,
60 0, "Time to wait on all mirror components");
61 static u_int g_mirror_idletime = 5;
62 TUNABLE_INT("kern.geom.mirror.idletime", &g_mirror_idletime);
63 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, idletime, CTLFLAG_RW,
64 &g_mirror_idletime, 0, "Mark components as clean when idling");
65 static u_int g_mirror_disconnect_on_failure = 1;
66 TUNABLE_INT("kern.geom.mirror.disconnect_on_failure",
67 &g_mirror_disconnect_on_failure);
68 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
69 &g_mirror_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
70 static u_int g_mirror_syncreqs = 2;
71 TUNABLE_INT("kern.geom.mirror.sync_requests", &g_mirror_syncreqs);
72 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
73 &g_mirror_syncreqs, 0, "Parallel synchronization I/O requests.");
75 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \
76 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
77 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
78 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
81 static eventhandler_tag g_mirror_pre_sync = NULL;
83 static int g_mirror_destroy_geom(struct gctl_req *req, struct g_class *mp,
85 static g_taste_t g_mirror_taste;
86 static void g_mirror_init(struct g_class *mp);
87 static void g_mirror_fini(struct g_class *mp);
89 struct g_class g_mirror_class = {
90 .name = G_MIRROR_CLASS_NAME,
92 .ctlreq = g_mirror_config,
93 .taste = g_mirror_taste,
94 .destroy_geom = g_mirror_destroy_geom,
95 .init = g_mirror_init,
100 static void g_mirror_destroy_provider(struct g_mirror_softc *sc);
101 static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
102 static void g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force);
103 static void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
104 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
105 static void g_mirror_sync_stop(struct g_mirror_disk *disk, int type);
106 static void g_mirror_register_request(struct bio *bp);
107 static void g_mirror_sync_release(struct g_mirror_softc *sc);
111 g_mirror_disk_state2str(int state)
115 case G_MIRROR_DISK_STATE_NONE:
117 case G_MIRROR_DISK_STATE_NEW:
119 case G_MIRROR_DISK_STATE_ACTIVE:
121 case G_MIRROR_DISK_STATE_STALE:
123 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
124 return ("SYNCHRONIZING");
125 case G_MIRROR_DISK_STATE_DISCONNECTED:
126 return ("DISCONNECTED");
127 case G_MIRROR_DISK_STATE_DESTROY:
135 g_mirror_device_state2str(int state)
139 case G_MIRROR_DEVICE_STATE_STARTING:
141 case G_MIRROR_DEVICE_STATE_RUNNING:
149 g_mirror_get_diskname(struct g_mirror_disk *disk)
152 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
153 return ("[unknown]");
154 return (disk->d_name);
158 * --- Events handling functions ---
159 * Events in geom_mirror are used to maintain disks and device status
160 * from one thread to simplify locking.
163 g_mirror_event_free(struct g_mirror_event *ep)
170 g_mirror_event_send(void *arg, int state, int flags)
172 struct g_mirror_softc *sc;
173 struct g_mirror_disk *disk;
174 struct g_mirror_event *ep;
177 ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
178 G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
179 if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
190 mtx_lock(&sc->sc_events_mtx);
191 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
192 mtx_unlock(&sc->sc_events_mtx);
193 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
194 mtx_lock(&sc->sc_queue_mtx);
196 mtx_unlock(&sc->sc_queue_mtx);
197 if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0)
199 sx_assert(&sc->sc_lock, SX_XLOCKED);
200 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
201 sx_xunlock(&sc->sc_lock);
202 while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) {
203 mtx_lock(&sc->sc_events_mtx);
204 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event",
208 g_mirror_event_free(ep);
209 sx_xlock(&sc->sc_lock);
213 static struct g_mirror_event *
214 g_mirror_event_get(struct g_mirror_softc *sc)
216 struct g_mirror_event *ep;
218 mtx_lock(&sc->sc_events_mtx);
219 ep = TAILQ_FIRST(&sc->sc_events);
220 mtx_unlock(&sc->sc_events_mtx);
225 g_mirror_event_remove(struct g_mirror_softc *sc, struct g_mirror_event *ep)
228 mtx_lock(&sc->sc_events_mtx);
229 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
230 mtx_unlock(&sc->sc_events_mtx);
234 g_mirror_event_cancel(struct g_mirror_disk *disk)
236 struct g_mirror_softc *sc;
237 struct g_mirror_event *ep, *tmpep;
240 sx_assert(&sc->sc_lock, SX_XLOCKED);
242 mtx_lock(&sc->sc_events_mtx);
243 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
244 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0)
246 if (ep->e_disk != disk)
248 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
249 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
250 g_mirror_event_free(ep);
252 ep->e_error = ECANCELED;
256 mtx_unlock(&sc->sc_events_mtx);
260 * Return the number of disks in given state.
261 * If state is equal to -1, count all connected disks.
264 g_mirror_ndisks(struct g_mirror_softc *sc, int state)
266 struct g_mirror_disk *disk;
269 sx_assert(&sc->sc_lock, SX_LOCKED);
271 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
272 if (state == -1 || disk->d_state == state)
279 * Find a disk in mirror by its disk ID.
281 static struct g_mirror_disk *
282 g_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id)
284 struct g_mirror_disk *disk;
286 sx_assert(&sc->sc_lock, SX_XLOCKED);
288 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
289 if (disk->d_id == id)
296 g_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp)
301 mtx_lock(&sc->sc_queue_mtx);
302 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
303 if (bp->bio_from == cp)
306 mtx_unlock(&sc->sc_queue_mtx);
311 g_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp)
316 "I/O requests for %s exist, can't destroy it now.",
320 if (g_mirror_nrequests(sc, cp) > 0) {
322 "I/O requests for %s in queue, can't destroy it now.",
330 g_mirror_destroy_consumer(void *arg, int flags __unused)
332 struct g_consumer *cp;
337 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
339 g_destroy_consumer(cp);
343 g_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
345 struct g_provider *pp;
351 if (g_mirror_is_busy(sc, cp))
356 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
359 G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
360 -cp->acw, -cp->ace, 0);
361 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
362 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
365 * After retaste event was send (inside g_access()), we can send
366 * event to detach and destroy consumer.
367 * A class, which has consumer to the given provider connected
368 * will not receive retaste event for the provider.
369 * This is the way how I ignore retaste events when I close
370 * consumers opened for write: I detach and destroy consumer
371 * after retaste event is sent.
373 g_post_event(g_mirror_destroy_consumer, cp, M_WAITOK, NULL);
376 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", pp->name);
378 g_destroy_consumer(cp);
382 g_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp)
384 struct g_consumer *cp;
387 g_topology_assert_not();
388 KASSERT(disk->d_consumer == NULL,
389 ("Disk already connected (device %s).", disk->d_softc->sc_name));
392 cp = g_new_consumer(disk->d_softc->sc_geom);
393 error = g_attach(cp, pp);
395 g_destroy_consumer(cp);
399 error = g_access(cp, 1, 1, 1);
402 g_destroy_consumer(cp);
404 G_MIRROR_DEBUG(0, "Cannot open consumer %s (error=%d).",
409 disk->d_consumer = cp;
410 disk->d_consumer->private = disk;
411 disk->d_consumer->index = 0;
413 G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk));
418 g_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
425 if (cp->provider != NULL)
426 g_mirror_kill_consumer(sc, cp);
428 g_destroy_consumer(cp);
432 * Initialize disk. This means allocate memory, create consumer, attach it
433 * to the provider and open access (r1w1e1) to it.
435 static struct g_mirror_disk *
436 g_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp,
437 struct g_mirror_metadata *md, int *errorp)
439 struct g_mirror_disk *disk;
442 disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO);
448 error = g_mirror_connect_disk(disk, pp);
451 disk->d_id = md->md_did;
452 disk->d_state = G_MIRROR_DISK_STATE_NONE;
453 disk->d_priority = md->md_priority;
454 disk->d_flags = md->md_dflags;
455 if (md->md_provider[0] != '\0')
456 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
457 disk->d_sync.ds_consumer = NULL;
458 disk->d_sync.ds_offset = md->md_sync_offset;
459 disk->d_sync.ds_offset_done = md->md_sync_offset;
460 disk->d_genid = md->md_genid;
461 disk->d_sync.ds_syncid = md->md_syncid;
469 free(disk, M_MIRROR);
474 g_mirror_destroy_disk(struct g_mirror_disk *disk)
476 struct g_mirror_softc *sc;
478 g_topology_assert_not();
480 sx_assert(&sc->sc_lock, SX_XLOCKED);
482 LIST_REMOVE(disk, d_next);
483 g_mirror_event_cancel(disk);
484 if (sc->sc_hint == disk)
486 switch (disk->d_state) {
487 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
488 g_mirror_sync_stop(disk, 1);
490 case G_MIRROR_DISK_STATE_NEW:
491 case G_MIRROR_DISK_STATE_STALE:
492 case G_MIRROR_DISK_STATE_ACTIVE:
494 g_mirror_disconnect_consumer(sc, disk->d_consumer);
496 free(disk, M_MIRROR);
499 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
500 g_mirror_get_diskname(disk),
501 g_mirror_disk_state2str(disk->d_state)));
506 g_mirror_destroy_device(struct g_mirror_softc *sc)
508 struct g_mirror_disk *disk;
509 struct g_mirror_event *ep;
511 struct g_consumer *cp, *tmpcp;
513 g_topology_assert_not();
514 sx_assert(&sc->sc_lock, SX_XLOCKED);
517 if (sc->sc_provider != NULL)
518 g_mirror_destroy_provider(sc);
519 for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL;
520 disk = LIST_FIRST(&sc->sc_disks)) {
521 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
522 g_mirror_update_metadata(disk);
523 g_mirror_destroy_disk(disk);
525 while ((ep = g_mirror_event_get(sc)) != NULL) {
526 g_mirror_event_remove(sc, ep);
527 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
528 g_mirror_event_free(ep);
530 ep->e_error = ECANCELED;
531 ep->e_flags |= G_MIRROR_EVENT_DONE;
532 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep);
533 mtx_lock(&sc->sc_events_mtx);
535 mtx_unlock(&sc->sc_events_mtx);
538 callout_drain(&sc->sc_callout);
541 LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
542 g_mirror_disconnect_consumer(sc, cp);
544 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
545 G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
546 g_wither_geom(gp, ENXIO);
548 mtx_destroy(&sc->sc_queue_mtx);
549 mtx_destroy(&sc->sc_events_mtx);
550 sx_xunlock(&sc->sc_lock);
551 sx_destroy(&sc->sc_lock);
555 g_mirror_orphan(struct g_consumer *cp)
557 struct g_mirror_disk *disk;
564 disk->d_softc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
565 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
566 G_MIRROR_EVENT_DONTWAIT);
570 * Function should return the next active disk on the list.
571 * It is possible that it will be the same disk as given.
572 * If there are no active disks on list, NULL is returned.
574 static __inline struct g_mirror_disk *
575 g_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
577 struct g_mirror_disk *dp;
579 for (dp = LIST_NEXT(disk, d_next); dp != disk;
580 dp = LIST_NEXT(dp, d_next)) {
582 dp = LIST_FIRST(&sc->sc_disks);
583 if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE)
586 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
591 static struct g_mirror_disk *
592 g_mirror_get_disk(struct g_mirror_softc *sc)
594 struct g_mirror_disk *disk;
596 if (sc->sc_hint == NULL) {
597 sc->sc_hint = LIST_FIRST(&sc->sc_disks);
598 if (sc->sc_hint == NULL)
602 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) {
603 disk = g_mirror_find_next(sc, disk);
607 sc->sc_hint = g_mirror_find_next(sc, disk);
612 g_mirror_write_metadata(struct g_mirror_disk *disk,
613 struct g_mirror_metadata *md)
615 struct g_mirror_softc *sc;
616 struct g_consumer *cp;
617 off_t offset, length;
621 g_topology_assert_not();
623 sx_assert(&sc->sc_lock, SX_LOCKED);
625 cp = disk->d_consumer;
626 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
627 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
628 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
629 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
631 length = cp->provider->sectorsize;
632 offset = cp->provider->mediasize - length;
633 sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO);
635 mirror_metadata_encode(md, sector);
636 error = g_write_data(cp, offset, sector, length);
637 free(sector, M_MIRROR);
639 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
640 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
641 G_MIRROR_DEBUG(0, "Cannot write metadata on %s "
642 "(device=%s, error=%d).",
643 g_mirror_get_diskname(disk), sc->sc_name, error);
645 G_MIRROR_DEBUG(1, "Cannot write metadata on %s "
646 "(device=%s, error=%d).",
647 g_mirror_get_diskname(disk), sc->sc_name, error);
649 if (g_mirror_disconnect_on_failure &&
650 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) {
651 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
652 g_mirror_event_send(disk,
653 G_MIRROR_DISK_STATE_DISCONNECTED,
654 G_MIRROR_EVENT_DONTWAIT);
661 g_mirror_clear_metadata(struct g_mirror_disk *disk)
665 g_topology_assert_not();
666 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
668 error = g_mirror_write_metadata(disk, NULL);
670 G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
671 g_mirror_get_diskname(disk));
674 "Cannot clear metadata on disk %s (error=%d).",
675 g_mirror_get_diskname(disk), error);
681 g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
682 struct g_mirror_metadata *md)
685 strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
686 md->md_version = G_MIRROR_VERSION;
687 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
688 md->md_mid = sc->sc_id;
689 md->md_all = sc->sc_ndisks;
690 md->md_slice = sc->sc_slice;
691 md->md_balance = sc->sc_balance;
692 md->md_genid = sc->sc_genid;
693 md->md_mediasize = sc->sc_mediasize;
694 md->md_sectorsize = sc->sc_sectorsize;
695 md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
696 bzero(md->md_provider, sizeof(md->md_provider));
698 md->md_did = arc4random();
702 md->md_sync_offset = 0;
705 md->md_did = disk->d_id;
706 md->md_priority = disk->d_priority;
707 md->md_syncid = disk->d_sync.ds_syncid;
708 md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
709 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
710 md->md_sync_offset = disk->d_sync.ds_offset_done;
712 md->md_sync_offset = 0;
713 if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
714 strlcpy(md->md_provider,
715 disk->d_consumer->provider->name,
716 sizeof(md->md_provider));
718 md->md_provsize = disk->d_consumer->provider->mediasize;
723 g_mirror_update_metadata(struct g_mirror_disk *disk)
725 struct g_mirror_softc *sc;
726 struct g_mirror_metadata md;
729 g_topology_assert_not();
731 sx_assert(&sc->sc_lock, SX_LOCKED);
733 g_mirror_fill_metadata(sc, disk, &md);
734 error = g_mirror_write_metadata(disk, &md);
736 G_MIRROR_DEBUG(2, "Metadata on %s updated.",
737 g_mirror_get_diskname(disk));
740 "Cannot update metadata on disk %s (error=%d).",
741 g_mirror_get_diskname(disk), error);
746 g_mirror_bump_syncid(struct g_mirror_softc *sc)
748 struct g_mirror_disk *disk;
750 g_topology_assert_not();
751 sx_assert(&sc->sc_lock, SX_XLOCKED);
752 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
753 ("%s called with no active disks (device=%s).", __func__,
757 G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
759 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
760 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
761 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
762 disk->d_sync.ds_syncid = sc->sc_syncid;
763 g_mirror_update_metadata(disk);
769 g_mirror_bump_genid(struct g_mirror_softc *sc)
771 struct g_mirror_disk *disk;
773 g_topology_assert_not();
774 sx_assert(&sc->sc_lock, SX_XLOCKED);
775 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
776 ("%s called with no active disks (device=%s).", __func__,
780 G_MIRROR_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
782 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
783 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
784 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
785 disk->d_genid = sc->sc_genid;
786 g_mirror_update_metadata(disk);
792 g_mirror_idle(struct g_mirror_softc *sc, int acw)
794 struct g_mirror_disk *disk;
797 g_topology_assert_not();
798 sx_assert(&sc->sc_lock, SX_XLOCKED);
800 if (sc->sc_provider == NULL)
802 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
806 if (sc->sc_writes > 0)
808 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
809 timeout = g_mirror_idletime - (time_uptime - sc->sc_last_write);
814 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
815 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
817 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
818 g_mirror_get_diskname(disk), sc->sc_name);
819 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
820 g_mirror_update_metadata(disk);
826 g_mirror_unidle(struct g_mirror_softc *sc)
828 struct g_mirror_disk *disk;
830 g_topology_assert_not();
831 sx_assert(&sc->sc_lock, SX_XLOCKED);
833 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
836 sc->sc_last_write = time_uptime;
837 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
838 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
840 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
841 g_mirror_get_diskname(disk), sc->sc_name);
842 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
843 g_mirror_update_metadata(disk);
848 g_mirror_done(struct bio *bp)
850 struct g_mirror_softc *sc;
852 sc = bp->bio_from->geom->softc;
853 bp->bio_cflags = G_MIRROR_BIO_FLAG_REGULAR;
854 mtx_lock(&sc->sc_queue_mtx);
855 bioq_disksort(&sc->sc_queue, bp);
856 mtx_unlock(&sc->sc_queue_mtx);
861 g_mirror_regular_request(struct bio *bp)
863 struct g_mirror_softc *sc;
864 struct g_mirror_disk *disk;
867 g_topology_assert_not();
869 pbp = bp->bio_parent;
870 sc = pbp->bio_to->geom->softc;
871 bp->bio_from->index--;
872 if (bp->bio_cmd == BIO_WRITE)
874 disk = bp->bio_from->private;
877 g_mirror_kill_consumer(sc, bp->bio_from);
882 KASSERT(pbp->bio_inbed <= pbp->bio_children,
883 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
885 if (bp->bio_error == 0 && pbp->bio_error == 0) {
886 G_MIRROR_LOGREQ(3, bp, "Request delivered.");
888 if (pbp->bio_children == pbp->bio_inbed) {
889 G_MIRROR_LOGREQ(3, pbp, "Request delivered.");
890 pbp->bio_completed = pbp->bio_length;
891 if (pbp->bio_cmd == BIO_WRITE) {
892 bioq_remove(&sc->sc_inflight, pbp);
893 /* Release delayed sync requests if possible. */
894 g_mirror_sync_release(sc);
896 g_io_deliver(pbp, pbp->bio_error);
899 } else if (bp->bio_error != 0) {
900 if (pbp->bio_error == 0)
901 pbp->bio_error = bp->bio_error;
903 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
904 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
905 G_MIRROR_LOGREQ(0, bp,
906 "Request failed (error=%d).",
909 G_MIRROR_LOGREQ(1, bp,
910 "Request failed (error=%d).",
913 if (g_mirror_disconnect_on_failure &&
914 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1)
916 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
917 g_mirror_event_send(disk,
918 G_MIRROR_DISK_STATE_DISCONNECTED,
919 G_MIRROR_EVENT_DONTWAIT);
922 switch (pbp->bio_cmd) {
932 switch (pbp->bio_cmd) {
934 if (pbp->bio_inbed < pbp->bio_children)
936 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1)
937 g_io_deliver(pbp, pbp->bio_error);
940 mtx_lock(&sc->sc_queue_mtx);
941 bioq_disksort(&sc->sc_queue, pbp);
942 mtx_unlock(&sc->sc_queue_mtx);
943 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
949 if (pbp->bio_children == 0) {
951 * All requests failed.
953 } else if (pbp->bio_inbed < pbp->bio_children) {
956 } else if (pbp->bio_children == pbp->bio_inbed) {
957 /* Some requests succeeded. */
959 pbp->bio_completed = pbp->bio_length;
961 bioq_remove(&sc->sc_inflight, pbp);
962 /* Release delayed sync requests if possible. */
963 g_mirror_sync_release(sc);
964 g_io_deliver(pbp, pbp->bio_error);
967 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
973 g_mirror_sync_done(struct bio *bp)
975 struct g_mirror_softc *sc;
977 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
978 sc = bp->bio_from->geom->softc;
979 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
980 mtx_lock(&sc->sc_queue_mtx);
981 bioq_disksort(&sc->sc_queue, bp);
982 mtx_unlock(&sc->sc_queue_mtx);
987 g_mirror_kernel_dump(struct bio *bp)
989 struct g_mirror_softc *sc;
990 struct g_mirror_disk *disk;
992 struct g_kerneldump *gkd;
995 * We configure dumping to the first component, because this component
996 * will be used for reading with 'prefer' balance algorithm.
997 * If the component with the higest priority is currently disconnected
998 * we will not be able to read the dump after the reboot if it will be
999 * connected and synchronized later. Can we do something better?
1001 sc = bp->bio_to->geom->softc;
1002 disk = LIST_FIRST(&sc->sc_disks);
1004 gkd = (struct g_kerneldump *)bp->bio_data;
1005 if (gkd->length > bp->bio_to->mediasize)
1006 gkd->length = bp->bio_to->mediasize;
1007 cbp = g_clone_bio(bp);
1009 g_io_deliver(bp, ENOMEM);
1012 cbp->bio_done = g_std_done;
1013 g_io_request(cbp, disk->d_consumer);
1014 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1015 g_mirror_get_diskname(disk));
1019 g_mirror_flush(struct g_mirror_softc *sc, struct bio *bp)
1021 struct bio_queue_head queue;
1022 struct g_mirror_disk *disk;
1023 struct g_consumer *cp;
1027 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1028 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1030 cbp = g_clone_bio(bp);
1032 for (cbp = bioq_first(&queue); cbp != NULL;
1033 cbp = bioq_first(&queue)) {
1034 bioq_remove(&queue, cbp);
1037 if (bp->bio_error == 0)
1038 bp->bio_error = ENOMEM;
1039 g_io_deliver(bp, bp->bio_error);
1042 bioq_insert_tail(&queue, cbp);
1043 cbp->bio_done = g_std_done;
1044 cbp->bio_caller1 = disk;
1045 cbp->bio_to = disk->d_consumer->provider;
1047 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1048 bioq_remove(&queue, cbp);
1049 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1050 disk = cbp->bio_caller1;
1051 cbp->bio_caller1 = NULL;
1052 cp = disk->d_consumer;
1053 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1054 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1055 cp->acr, cp->acw, cp->ace));
1056 g_io_request(cbp, disk->d_consumer);
1061 g_mirror_start(struct bio *bp)
1063 struct g_mirror_softc *sc;
1065 sc = bp->bio_to->geom->softc;
1067 * If sc == NULL or there are no valid disks, provider's error
1068 * should be set and g_mirror_start() should not be called at all.
1070 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1071 ("Provider's error should be set (error=%d)(mirror=%s).",
1072 bp->bio_to->error, bp->bio_to->name));
1073 G_MIRROR_LOGREQ(3, bp, "Request received.");
1075 switch (bp->bio_cmd) {
1081 g_mirror_flush(sc, bp);
1084 if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1085 g_mirror_kernel_dump(bp);
1090 g_io_deliver(bp, EOPNOTSUPP);
1093 mtx_lock(&sc->sc_queue_mtx);
1094 bioq_disksort(&sc->sc_queue, bp);
1095 mtx_unlock(&sc->sc_queue_mtx);
1096 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1101 * Return TRUE if the given request is colliding with a in-progress
1102 * synchronization request.
1105 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1107 struct g_mirror_disk *disk;
1109 off_t rstart, rend, sstart, send;
1112 if (sc->sc_sync.ds_ndisks == 0)
1114 rstart = bp->bio_offset;
1115 rend = bp->bio_offset + bp->bio_length;
1116 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1117 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1119 for (i = 0; i < g_mirror_syncreqs; i++) {
1120 sbp = disk->d_sync.ds_bios[i];
1123 sstart = sbp->bio_offset;
1124 send = sbp->bio_offset + sbp->bio_length;
1125 if (rend > sstart && rstart < send)
1133 * Return TRUE if the given sync request is colliding with a in-progress regular
1137 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1139 off_t rstart, rend, sstart, send;
1142 if (sc->sc_sync.ds_ndisks == 0)
1144 sstart = sbp->bio_offset;
1145 send = sbp->bio_offset + sbp->bio_length;
1146 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1147 rstart = bp->bio_offset;
1148 rend = bp->bio_offset + bp->bio_length;
1149 if (rend > sstart && rstart < send)
1156 * Puts request onto delayed queue.
1159 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1162 G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1163 bioq_insert_head(&sc->sc_regular_delayed, bp);
1167 * Puts synchronization request onto delayed queue.
1170 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1173 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1174 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1178 * Releases delayed regular requests which don't collide anymore with sync
1182 g_mirror_regular_release(struct g_mirror_softc *sc)
1184 struct bio *bp, *bp2;
1186 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1187 if (g_mirror_sync_collision(sc, bp))
1189 bioq_remove(&sc->sc_regular_delayed, bp);
1190 G_MIRROR_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1191 mtx_lock(&sc->sc_queue_mtx);
1192 bioq_insert_head(&sc->sc_queue, bp);
1195 * wakeup() is not needed, because this function is called from
1196 * the worker thread.
1198 wakeup(&sc->sc_queue);
1200 mtx_unlock(&sc->sc_queue_mtx);
1205 * Releases delayed sync requests which don't collide anymore with regular
1209 g_mirror_sync_release(struct g_mirror_softc *sc)
1211 struct bio *bp, *bp2;
1213 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1214 if (g_mirror_regular_collision(sc, bp))
1216 bioq_remove(&sc->sc_sync_delayed, bp);
1217 G_MIRROR_LOGREQ(2, bp,
1218 "Releasing delayed synchronization request.");
1219 g_io_request(bp, bp->bio_from);
1224 * Handle synchronization requests.
1225 * Every synchronization request is two-steps process: first, READ request is
1226 * send to active provider and then WRITE request (with read data) to the provider
1227 * beeing synchronized. When WRITE is finished, new synchronization request is
1231 g_mirror_sync_request(struct bio *bp)
1233 struct g_mirror_softc *sc;
1234 struct g_mirror_disk *disk;
1236 bp->bio_from->index--;
1237 sc = bp->bio_from->geom->softc;
1238 disk = bp->bio_from->private;
1240 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1242 g_mirror_kill_consumer(sc, bp->bio_from);
1243 g_topology_unlock();
1244 free(bp->bio_data, M_MIRROR);
1246 sx_xlock(&sc->sc_lock);
1251 * Synchronization request.
1253 switch (bp->bio_cmd) {
1256 struct g_consumer *cp;
1258 if (bp->bio_error != 0) {
1259 G_MIRROR_LOGREQ(0, bp,
1260 "Synchronization request failed (error=%d).",
1265 G_MIRROR_LOGREQ(3, bp,
1266 "Synchronization request half-finished.");
1267 bp->bio_cmd = BIO_WRITE;
1269 cp = disk->d_consumer;
1270 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1271 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1272 cp->acr, cp->acw, cp->ace));
1274 g_io_request(bp, cp);
1279 struct g_mirror_disk_sync *sync;
1284 if (bp->bio_error != 0) {
1285 G_MIRROR_LOGREQ(0, bp,
1286 "Synchronization request failed (error=%d).",
1289 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1290 g_mirror_event_send(disk,
1291 G_MIRROR_DISK_STATE_DISCONNECTED,
1292 G_MIRROR_EVENT_DONTWAIT);
1295 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1296 sync = &disk->d_sync;
1297 if (sync->ds_offset == sc->sc_mediasize ||
1298 sync->ds_consumer == NULL ||
1299 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1300 /* Don't send more synchronization requests. */
1301 sync->ds_inflight--;
1302 if (sync->ds_bios != NULL) {
1303 i = (int)(uintptr_t)bp->bio_caller1;
1304 sync->ds_bios[i] = NULL;
1306 free(bp->bio_data, M_MIRROR);
1308 if (sync->ds_inflight > 0)
1310 if (sync->ds_consumer == NULL ||
1311 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1314 /* Disk up-to-date, activate it. */
1315 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1316 G_MIRROR_EVENT_DONTWAIT);
1320 /* Send next synchronization request. */
1321 data = bp->bio_data;
1322 bzero(bp, sizeof(*bp));
1323 bp->bio_cmd = BIO_READ;
1324 bp->bio_offset = sync->ds_offset;
1325 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1326 sync->ds_offset += bp->bio_length;
1327 bp->bio_done = g_mirror_sync_done;
1328 bp->bio_data = data;
1329 bp->bio_from = sync->ds_consumer;
1330 bp->bio_to = sc->sc_provider;
1331 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1332 sync->ds_consumer->index++;
1334 * Delay the request if it is colliding with a regular request.
1336 if (g_mirror_regular_collision(sc, bp))
1337 g_mirror_sync_delay(sc, bp);
1339 g_io_request(bp, sync->ds_consumer);
1341 /* Release delayed requests if possible. */
1342 g_mirror_regular_release(sc);
1344 /* Find the smallest offset */
1345 offset = sc->sc_mediasize;
1346 for (i = 0; i < g_mirror_syncreqs; i++) {
1347 bp = sync->ds_bios[i];
1348 if (bp->bio_offset < offset)
1349 offset = bp->bio_offset;
1351 if (sync->ds_offset_done + (MAXPHYS * 100) < offset) {
1352 /* Update offset_done on every 100 blocks. */
1353 sync->ds_offset_done = offset;
1354 g_mirror_update_metadata(disk);
1359 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1360 bp->bio_cmd, sc->sc_name));
1366 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1368 struct g_mirror_disk *disk;
1369 struct g_consumer *cp;
1372 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1373 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1377 if (bp->bio_error == 0)
1378 bp->bio_error = ENXIO;
1379 g_io_deliver(bp, bp->bio_error);
1382 cbp = g_clone_bio(bp);
1384 if (bp->bio_error == 0)
1385 bp->bio_error = ENOMEM;
1386 g_io_deliver(bp, bp->bio_error);
1390 * Fill in the component buf structure.
1392 cp = disk->d_consumer;
1393 cbp->bio_done = g_mirror_done;
1394 cbp->bio_to = cp->provider;
1395 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1396 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1397 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1400 g_io_request(cbp, cp);
1404 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1406 struct g_mirror_disk *disk;
1407 struct g_consumer *cp;
1410 disk = g_mirror_get_disk(sc);
1412 if (bp->bio_error == 0)
1413 bp->bio_error = ENXIO;
1414 g_io_deliver(bp, bp->bio_error);
1417 cbp = g_clone_bio(bp);
1419 if (bp->bio_error == 0)
1420 bp->bio_error = ENOMEM;
1421 g_io_deliver(bp, bp->bio_error);
1425 * Fill in the component buf structure.
1427 cp = disk->d_consumer;
1428 cbp->bio_done = g_mirror_done;
1429 cbp->bio_to = cp->provider;
1430 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1431 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1432 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1435 g_io_request(cbp, cp);
1438 #define TRACK_SIZE (1 * 1024 * 1024)
1439 #define LOAD_SCALE 256
1440 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
1443 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1445 struct g_mirror_disk *disk, *dp;
1446 struct g_consumer *cp;
1450 /* Find a disk with the smallest load. */
1453 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1454 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1457 /* If disk head is precisely in position - highly prefer it. */
1458 if (dp->d_last_offset == bp->bio_offset)
1459 prio -= 2 * LOAD_SCALE;
1461 /* If disk head is close to position - prefer it. */
1462 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1463 prio -= 1 * LOAD_SCALE;
1469 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1470 cbp = g_clone_bio(bp);
1472 if (bp->bio_error == 0)
1473 bp->bio_error = ENOMEM;
1474 g_io_deliver(bp, bp->bio_error);
1478 * Fill in the component buf structure.
1480 cp = disk->d_consumer;
1481 cbp->bio_done = g_mirror_done;
1482 cbp->bio_to = cp->provider;
1483 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1484 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1485 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1488 /* Remember last head position */
1489 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1491 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1492 dp->load = (dp->d_consumer->index * LOAD_SCALE +
1495 g_io_request(cbp, cp);
1499 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1501 struct bio_queue_head queue;
1502 struct g_mirror_disk *disk;
1503 struct g_consumer *cp;
1505 off_t left, mod, offset, slice;
1509 if (bp->bio_length <= sc->sc_slice) {
1510 g_mirror_request_round_robin(sc, bp);
1513 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1514 slice = bp->bio_length / ndisks;
1515 mod = slice % sc->sc_provider->sectorsize;
1517 slice += sc->sc_provider->sectorsize - mod;
1519 * Allocate all bios before sending any request, so we can
1520 * return ENOMEM in nice and clean way.
1522 left = bp->bio_length;
1523 offset = bp->bio_offset;
1524 data = bp->bio_data;
1526 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1527 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1529 cbp = g_clone_bio(bp);
1531 for (cbp = bioq_first(&queue); cbp != NULL;
1532 cbp = bioq_first(&queue)) {
1533 bioq_remove(&queue, cbp);
1536 if (bp->bio_error == 0)
1537 bp->bio_error = ENOMEM;
1538 g_io_deliver(bp, bp->bio_error);
1541 bioq_insert_tail(&queue, cbp);
1542 cbp->bio_done = g_mirror_done;
1543 cbp->bio_caller1 = disk;
1544 cbp->bio_to = disk->d_consumer->provider;
1545 cbp->bio_offset = offset;
1546 cbp->bio_data = data;
1547 cbp->bio_length = MIN(left, slice);
1548 left -= cbp->bio_length;
1551 offset += cbp->bio_length;
1552 data += cbp->bio_length;
1554 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1555 bioq_remove(&queue, cbp);
1556 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1557 disk = cbp->bio_caller1;
1558 cbp->bio_caller1 = NULL;
1559 cp = disk->d_consumer;
1560 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1561 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1562 cp->acr, cp->acw, cp->ace));
1563 disk->d_consumer->index++;
1564 g_io_request(cbp, disk->d_consumer);
1569 g_mirror_register_request(struct bio *bp)
1571 struct g_mirror_softc *sc;
1573 sc = bp->bio_to->geom->softc;
1574 switch (bp->bio_cmd) {
1576 switch (sc->sc_balance) {
1577 case G_MIRROR_BALANCE_LOAD:
1578 g_mirror_request_load(sc, bp);
1580 case G_MIRROR_BALANCE_PREFER:
1581 g_mirror_request_prefer(sc, bp);
1583 case G_MIRROR_BALANCE_ROUND_ROBIN:
1584 g_mirror_request_round_robin(sc, bp);
1586 case G_MIRROR_BALANCE_SPLIT:
1587 g_mirror_request_split(sc, bp);
1594 struct g_mirror_disk *disk;
1595 struct g_mirror_disk_sync *sync;
1596 struct bio_queue_head queue;
1597 struct g_consumer *cp;
1601 * Delay the request if it is colliding with a synchronization
1604 if (g_mirror_sync_collision(sc, bp)) {
1605 g_mirror_regular_delay(sc, bp);
1610 g_mirror_unidle(sc);
1612 sc->sc_last_write = time_uptime;
1615 * Allocate all bios before sending any request, so we can
1616 * return ENOMEM in nice and clean way.
1619 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1620 sync = &disk->d_sync;
1621 switch (disk->d_state) {
1622 case G_MIRROR_DISK_STATE_ACTIVE:
1624 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1625 if (bp->bio_offset >= sync->ds_offset)
1631 cbp = g_clone_bio(bp);
1633 for (cbp = bioq_first(&queue); cbp != NULL;
1634 cbp = bioq_first(&queue)) {
1635 bioq_remove(&queue, cbp);
1638 if (bp->bio_error == 0)
1639 bp->bio_error = ENOMEM;
1640 g_io_deliver(bp, bp->bio_error);
1643 bioq_insert_tail(&queue, cbp);
1644 cbp->bio_done = g_mirror_done;
1645 cp = disk->d_consumer;
1646 cbp->bio_caller1 = cp;
1647 cbp->bio_to = cp->provider;
1648 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1649 ("Consumer %s not opened (r%dw%de%d).",
1650 cp->provider->name, cp->acr, cp->acw, cp->ace));
1652 for (cbp = bioq_first(&queue); cbp != NULL;
1653 cbp = bioq_first(&queue)) {
1654 bioq_remove(&queue, cbp);
1655 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1656 cp = cbp->bio_caller1;
1657 cbp->bio_caller1 = NULL;
1660 g_io_request(cbp, cp);
1663 * Put request onto inflight queue, so we can check if new
1664 * synchronization requests don't collide with it.
1666 bioq_insert_tail(&sc->sc_inflight, bp);
1668 * Bump syncid on first write.
1670 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1671 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1672 g_mirror_bump_syncid(sc);
1677 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1678 bp->bio_cmd, sc->sc_name));
1684 g_mirror_can_destroy(struct g_mirror_softc *sc)
1687 struct g_consumer *cp;
1689 g_topology_assert();
1691 if (gp->softc == NULL)
1693 LIST_FOREACH(cp, &gp->consumer, consumer) {
1694 if (g_mirror_is_busy(sc, cp))
1697 gp = sc->sc_sync.ds_geom;
1698 LIST_FOREACH(cp, &gp->consumer, consumer) {
1699 if (g_mirror_is_busy(sc, cp))
1702 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1708 g_mirror_try_destroy(struct g_mirror_softc *sc)
1711 if (sc->sc_rootmount != NULL) {
1712 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1714 root_mount_rel(sc->sc_rootmount);
1715 sc->sc_rootmount = NULL;
1718 if (!g_mirror_can_destroy(sc)) {
1719 g_topology_unlock();
1722 sc->sc_geom->softc = NULL;
1723 sc->sc_sync.ds_geom->softc = NULL;
1724 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WAIT) != 0) {
1725 g_topology_unlock();
1726 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1728 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
1729 sx_xunlock(&sc->sc_lock);
1730 wakeup(&sc->sc_worker);
1731 sc->sc_worker = NULL;
1733 g_topology_unlock();
1734 g_mirror_destroy_device(sc);
1744 g_mirror_worker(void *arg)
1746 struct g_mirror_softc *sc;
1747 struct g_mirror_event *ep;
1752 thread_lock(curthread);
1753 sched_prio(curthread, PRIBIO);
1754 thread_unlock(curthread);
1756 sx_xlock(&sc->sc_lock);
1758 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1760 * First take a look at events.
1761 * This is important to handle events before any I/O requests.
1763 ep = g_mirror_event_get(sc);
1765 g_mirror_event_remove(sc, ep);
1766 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1767 /* Update only device status. */
1769 "Running event for device %s.",
1772 g_mirror_update_device(sc, 1);
1774 /* Update disk status. */
1775 G_MIRROR_DEBUG(3, "Running event for disk %s.",
1776 g_mirror_get_diskname(ep->e_disk));
1777 ep->e_error = g_mirror_update_disk(ep->e_disk,
1779 if (ep->e_error == 0)
1780 g_mirror_update_device(sc, 0);
1782 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1783 KASSERT(ep->e_error == 0,
1784 ("Error cannot be handled."));
1785 g_mirror_event_free(ep);
1787 ep->e_flags |= G_MIRROR_EVENT_DONE;
1788 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1790 mtx_lock(&sc->sc_events_mtx);
1792 mtx_unlock(&sc->sc_events_mtx);
1795 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1796 if (g_mirror_try_destroy(sc)) {
1797 curthread->td_pflags &= ~TDP_GEOM;
1798 G_MIRROR_DEBUG(1, "Thread exiting.");
1802 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1806 * Check if we can mark array as CLEAN and if we can't take
1807 * how much seconds should we wait.
1809 timeout = g_mirror_idle(sc, -1);
1813 /* Get first request from the queue. */
1814 mtx_lock(&sc->sc_queue_mtx);
1815 bp = bioq_first(&sc->sc_queue);
1818 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1819 mtx_unlock(&sc->sc_queue_mtx);
1820 if (g_mirror_try_destroy(sc)) {
1821 curthread->td_pflags &= ~TDP_GEOM;
1822 G_MIRROR_DEBUG(1, "Thread exiting.");
1825 mtx_lock(&sc->sc_queue_mtx);
1827 sx_xunlock(&sc->sc_lock);
1829 * XXX: We can miss an event here, because an event
1830 * can be added without sx-device-lock and without
1831 * mtx-queue-lock. Maybe I should just stop using
1832 * dedicated mutex for events synchronization and
1833 * stick with the queue lock?
1834 * The event will hang here until next I/O request
1835 * or next event is received.
1837 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
1839 sx_xlock(&sc->sc_lock);
1840 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
1843 bioq_remove(&sc->sc_queue, bp);
1844 mtx_unlock(&sc->sc_queue_mtx);
1846 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
1847 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
1848 g_mirror_sync_request(bp); /* READ */
1849 } else if (bp->bio_to != sc->sc_provider) {
1850 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
1851 g_mirror_regular_request(bp);
1852 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
1853 g_mirror_sync_request(bp); /* WRITE */
1856 ("Invalid request cflags=0x%hhx to=%s.",
1857 bp->bio_cflags, bp->bio_to->name));
1860 g_mirror_register_request(bp);
1862 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
1867 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
1870 sx_assert(&sc->sc_lock, SX_LOCKED);
1872 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
1874 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
1875 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
1876 g_mirror_get_diskname(disk), sc->sc_name);
1877 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1878 } else if (sc->sc_idle &&
1879 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
1880 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
1881 g_mirror_get_diskname(disk), sc->sc_name);
1882 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
1887 g_mirror_sync_start(struct g_mirror_disk *disk)
1889 struct g_mirror_softc *sc;
1890 struct g_consumer *cp;
1894 g_topology_assert_not();
1896 sx_assert(&sc->sc_lock, SX_LOCKED);
1898 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1899 ("Disk %s is not marked for synchronization.",
1900 g_mirror_get_diskname(disk)));
1901 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1902 ("Device not in RUNNING state (%s, %u).", sc->sc_name,
1905 sx_xunlock(&sc->sc_lock);
1907 cp = g_new_consumer(sc->sc_sync.ds_geom);
1908 error = g_attach(cp, sc->sc_provider);
1910 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
1911 error = g_access(cp, 1, 0, 0);
1912 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
1913 g_topology_unlock();
1914 sx_xlock(&sc->sc_lock);
1916 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
1917 g_mirror_get_diskname(disk));
1918 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
1919 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1920 KASSERT(disk->d_sync.ds_consumer == NULL,
1921 ("Sync consumer already exists (device=%s, disk=%s).",
1922 sc->sc_name, g_mirror_get_diskname(disk)));
1924 disk->d_sync.ds_consumer = cp;
1925 disk->d_sync.ds_consumer->private = disk;
1926 disk->d_sync.ds_consumer->index = 0;
1929 * Allocate memory for synchronization bios and initialize them.
1931 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
1932 M_MIRROR, M_WAITOK);
1933 for (i = 0; i < g_mirror_syncreqs; i++) {
1935 disk->d_sync.ds_bios[i] = bp;
1936 bp->bio_parent = NULL;
1937 bp->bio_cmd = BIO_READ;
1938 bp->bio_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK);
1940 bp->bio_offset = disk->d_sync.ds_offset;
1941 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1942 disk->d_sync.ds_offset += bp->bio_length;
1943 bp->bio_done = g_mirror_sync_done;
1944 bp->bio_from = disk->d_sync.ds_consumer;
1945 bp->bio_to = sc->sc_provider;
1946 bp->bio_caller1 = (void *)(uintptr_t)i;
1949 /* Increase the number of disks in SYNCHRONIZING state. */
1950 sc->sc_sync.ds_ndisks++;
1951 /* Set the number of in-flight synchronization requests. */
1952 disk->d_sync.ds_inflight = g_mirror_syncreqs;
1955 * Fire off first synchronization requests.
1957 for (i = 0; i < g_mirror_syncreqs; i++) {
1958 bp = disk->d_sync.ds_bios[i];
1959 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1960 disk->d_sync.ds_consumer->index++;
1962 * Delay the request if it is colliding with a regular request.
1964 if (g_mirror_regular_collision(sc, bp))
1965 g_mirror_sync_delay(sc, bp);
1967 g_io_request(bp, disk->d_sync.ds_consumer);
1972 * Stop synchronization process.
1973 * type: 0 - synchronization finished
1974 * 1 - synchronization stopped
1977 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
1979 struct g_mirror_softc *sc;
1980 struct g_consumer *cp;
1982 g_topology_assert_not();
1984 sx_assert(&sc->sc_lock, SX_LOCKED);
1986 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1987 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
1988 g_mirror_disk_state2str(disk->d_state)));
1989 if (disk->d_sync.ds_consumer == NULL)
1993 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
1994 sc->sc_name, g_mirror_get_diskname(disk));
1995 } else /* if (type == 1) */ {
1996 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
1997 sc->sc_name, g_mirror_get_diskname(disk));
1999 free(disk->d_sync.ds_bios, M_MIRROR);
2000 disk->d_sync.ds_bios = NULL;
2001 cp = disk->d_sync.ds_consumer;
2002 disk->d_sync.ds_consumer = NULL;
2003 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2004 sc->sc_sync.ds_ndisks--;
2005 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2007 g_mirror_kill_consumer(sc, cp);
2008 g_topology_unlock();
2009 sx_xlock(&sc->sc_lock);
2013 g_mirror_launch_provider(struct g_mirror_softc *sc)
2015 struct g_mirror_disk *disk;
2016 struct g_provider *pp;
2018 sx_assert(&sc->sc_lock, SX_LOCKED);
2021 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2022 pp->mediasize = sc->sc_mediasize;
2023 pp->sectorsize = sc->sc_sectorsize;
2025 pp->stripeoffset = 0;
2026 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2027 if (disk->d_consumer && disk->d_consumer->provider &&
2028 disk->d_consumer->provider->stripesize > pp->stripesize) {
2029 pp->stripesize = disk->d_consumer->provider->stripesize;
2030 pp->stripeoffset = disk->d_consumer->provider->stripeoffset;
2033 sc->sc_provider = pp;
2034 g_error_provider(pp, 0);
2035 g_topology_unlock();
2036 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2037 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2038 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2039 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2040 g_mirror_sync_start(disk);
2045 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2047 struct g_mirror_disk *disk;
2050 g_topology_assert_not();
2051 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2055 g_error_provider(sc->sc_provider, ENXIO);
2056 mtx_lock(&sc->sc_queue_mtx);
2057 while ((bp = bioq_first(&sc->sc_queue)) != NULL) {
2058 bioq_remove(&sc->sc_queue, bp);
2059 g_io_deliver(bp, ENXIO);
2061 mtx_unlock(&sc->sc_queue_mtx);
2062 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2063 sc->sc_provider->name);
2064 sc->sc_provider->flags |= G_PF_WITHER;
2065 g_orphan_provider(sc->sc_provider, ENXIO);
2066 g_topology_unlock();
2067 sc->sc_provider = NULL;
2068 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2069 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2070 g_mirror_sync_stop(disk, 1);
2075 g_mirror_go(void *arg)
2077 struct g_mirror_softc *sc;
2080 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2081 g_mirror_event_send(sc, 0,
2082 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2086 g_mirror_determine_state(struct g_mirror_disk *disk)
2088 struct g_mirror_softc *sc;
2092 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2093 if ((disk->d_flags &
2094 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2095 /* Disk does not need synchronization. */
2096 state = G_MIRROR_DISK_STATE_ACTIVE;
2099 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2101 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2103 * We can start synchronization from
2104 * the stored offset.
2106 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2108 state = G_MIRROR_DISK_STATE_STALE;
2111 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2113 * Reset all synchronization data for this disk,
2114 * because if it even was synchronized, it was
2115 * synchronized to disks with different syncid.
2117 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2118 disk->d_sync.ds_offset = 0;
2119 disk->d_sync.ds_offset_done = 0;
2120 disk->d_sync.ds_syncid = sc->sc_syncid;
2121 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2122 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2123 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2125 state = G_MIRROR_DISK_STATE_STALE;
2127 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2129 * Not good, NOT GOOD!
2130 * It means that mirror was started on stale disks
2131 * and more fresh disk just arrive.
2132 * If there were writes, mirror is broken, sorry.
2133 * I think the best choice here is don't touch
2134 * this disk and inform the user loudly.
2136 G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2137 "disk (%s) arrives!! It will not be connected to the "
2138 "running device.", sc->sc_name,
2139 g_mirror_get_diskname(disk));
2140 g_mirror_destroy_disk(disk);
2141 state = G_MIRROR_DISK_STATE_NONE;
2142 /* Return immediately, because disk was destroyed. */
2145 G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2146 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2151 * Update device state.
2154 g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force)
2156 struct g_mirror_disk *disk;
2159 sx_assert(&sc->sc_lock, SX_XLOCKED);
2161 switch (sc->sc_state) {
2162 case G_MIRROR_DEVICE_STATE_STARTING:
2164 struct g_mirror_disk *pdisk, *tdisk;
2165 u_int dirty, ndisks, genid, syncid;
2167 KASSERT(sc->sc_provider == NULL,
2168 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2170 * Are we ready? We are, if all disks are connected or
2171 * if we have any disks and 'force' is true.
2173 ndisks = g_mirror_ndisks(sc, -1);
2174 if (sc->sc_ndisks == ndisks || (force && ndisks > 0)) {
2176 } else if (ndisks == 0) {
2178 * Disks went down in starting phase, so destroy
2181 callout_drain(&sc->sc_callout);
2182 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2183 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2185 root_mount_rel(sc->sc_rootmount);
2186 sc->sc_rootmount = NULL;
2193 * Activate all disks with the biggest syncid.
2197 * If 'force' is true, we have been called due to
2198 * timeout, so don't bother canceling timeout.
2201 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2202 if ((disk->d_flags &
2203 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2208 /* No valid disks found, destroy device. */
2209 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2210 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2211 __LINE__, sc->sc_rootmount);
2212 root_mount_rel(sc->sc_rootmount);
2213 sc->sc_rootmount = NULL;
2217 /* Cancel timeout. */
2218 callout_drain(&sc->sc_callout);
2222 * Find the biggest genid.
2225 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2226 if (disk->d_genid > genid)
2227 genid = disk->d_genid;
2229 sc->sc_genid = genid;
2231 * Remove all disks without the biggest genid.
2233 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2234 if (disk->d_genid < genid) {
2236 "Component %s (device %s) broken, skipping.",
2237 g_mirror_get_diskname(disk), sc->sc_name);
2238 g_mirror_destroy_disk(disk);
2243 * Find the biggest syncid.
2246 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2247 if (disk->d_sync.ds_syncid > syncid)
2248 syncid = disk->d_sync.ds_syncid;
2252 * Here we need to look for dirty disks and if all disks
2253 * with the biggest syncid are dirty, we have to choose
2254 * one with the biggest priority and rebuild the rest.
2257 * Find the number of dirty disks with the biggest syncid.
2258 * Find the number of disks with the biggest syncid.
2259 * While here, find a disk with the biggest priority.
2263 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2264 if (disk->d_sync.ds_syncid != syncid)
2266 if ((disk->d_flags &
2267 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2271 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2273 if (pdisk == NULL ||
2274 pdisk->d_priority < disk->d_priority) {
2280 /* No dirty disks at all, great. */
2281 } else if (dirty == ndisks) {
2283 * Force synchronization for all dirty disks except one
2284 * with the biggest priority.
2286 KASSERT(pdisk != NULL, ("pdisk == NULL"));
2287 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2288 "master disk for synchronization.",
2289 g_mirror_get_diskname(pdisk), sc->sc_name);
2290 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2291 if (disk->d_sync.ds_syncid != syncid)
2293 if ((disk->d_flags &
2294 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2297 KASSERT((disk->d_flags &
2298 G_MIRROR_DISK_FLAG_DIRTY) != 0,
2299 ("Disk %s isn't marked as dirty.",
2300 g_mirror_get_diskname(disk)));
2301 /* Skip the disk with the biggest priority. */
2304 disk->d_sync.ds_syncid = 0;
2306 } else if (dirty < ndisks) {
2308 * Force synchronization for all dirty disks.
2309 * We have some non-dirty disks.
2311 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2312 if (disk->d_sync.ds_syncid != syncid)
2314 if ((disk->d_flags &
2315 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2318 if ((disk->d_flags &
2319 G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2322 disk->d_sync.ds_syncid = 0;
2328 sc->sc_syncid = syncid;
2330 /* Remember to bump syncid on first write. */
2331 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2333 state = G_MIRROR_DEVICE_STATE_RUNNING;
2334 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2335 sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2336 g_mirror_device_state2str(state));
2337 sc->sc_state = state;
2338 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2339 state = g_mirror_determine_state(disk);
2340 g_mirror_event_send(disk, state,
2341 G_MIRROR_EVENT_DONTWAIT);
2342 if (state == G_MIRROR_DISK_STATE_STALE)
2343 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2347 case G_MIRROR_DEVICE_STATE_RUNNING:
2348 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2349 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2351 * No active disks or no disks at all,
2352 * so destroy device.
2354 if (sc->sc_provider != NULL)
2355 g_mirror_destroy_provider(sc);
2356 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2358 } else if (g_mirror_ndisks(sc,
2359 G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2360 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2362 * We have active disks, launch provider if it doesn't
2365 if (sc->sc_provider == NULL)
2366 g_mirror_launch_provider(sc);
2367 if (sc->sc_rootmount != NULL) {
2368 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2369 __LINE__, sc->sc_rootmount);
2370 root_mount_rel(sc->sc_rootmount);
2371 sc->sc_rootmount = NULL;
2375 * Genid should be bumped immediately, so do it here.
2377 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2378 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2379 g_mirror_bump_genid(sc);
2383 KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2384 sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2390 * Update disk state and device state if needed.
2392 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \
2393 "Disk %s state changed from %s to %s (device %s).", \
2394 g_mirror_get_diskname(disk), \
2395 g_mirror_disk_state2str(disk->d_state), \
2396 g_mirror_disk_state2str(state), sc->sc_name)
2398 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2400 struct g_mirror_softc *sc;
2403 sx_assert(&sc->sc_lock, SX_XLOCKED);
2406 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2407 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2408 g_mirror_disk_state2str(state));
2410 case G_MIRROR_DISK_STATE_NEW:
2412 * Possible scenarios:
2413 * 1. New disk arrive.
2415 /* Previous state should be NONE. */
2416 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2417 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2418 g_mirror_disk_state2str(disk->d_state)));
2419 DISK_STATE_CHANGED();
2421 disk->d_state = state;
2422 if (LIST_EMPTY(&sc->sc_disks))
2423 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2425 struct g_mirror_disk *dp;
2427 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2428 if (disk->d_priority >= dp->d_priority) {
2429 LIST_INSERT_BEFORE(dp, disk, d_next);
2433 if (LIST_NEXT(dp, d_next) == NULL)
2437 LIST_INSERT_AFTER(dp, disk, d_next);
2439 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2440 sc->sc_name, g_mirror_get_diskname(disk));
2441 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2443 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2444 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2445 g_mirror_device_state2str(sc->sc_state),
2446 g_mirror_get_diskname(disk),
2447 g_mirror_disk_state2str(disk->d_state)));
2448 state = g_mirror_determine_state(disk);
2449 if (state != G_MIRROR_DISK_STATE_NONE)
2452 case G_MIRROR_DISK_STATE_ACTIVE:
2454 * Possible scenarios:
2455 * 1. New disk does not need synchronization.
2456 * 2. Synchronization process finished successfully.
2458 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2459 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2460 g_mirror_device_state2str(sc->sc_state),
2461 g_mirror_get_diskname(disk),
2462 g_mirror_disk_state2str(disk->d_state)));
2463 /* Previous state should be NEW or SYNCHRONIZING. */
2464 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2465 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2466 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2467 g_mirror_disk_state2str(disk->d_state)));
2468 DISK_STATE_CHANGED();
2470 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2471 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2472 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2473 g_mirror_sync_stop(disk, 0);
2475 disk->d_state = state;
2476 disk->d_sync.ds_offset = 0;
2477 disk->d_sync.ds_offset_done = 0;
2478 g_mirror_update_idle(sc, disk);
2479 g_mirror_update_metadata(disk);
2480 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2481 sc->sc_name, g_mirror_get_diskname(disk));
2483 case G_MIRROR_DISK_STATE_STALE:
2485 * Possible scenarios:
2486 * 1. Stale disk was connected.
2488 /* Previous state should be NEW. */
2489 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2490 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2491 g_mirror_disk_state2str(disk->d_state)));
2492 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2493 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2494 g_mirror_device_state2str(sc->sc_state),
2495 g_mirror_get_diskname(disk),
2496 g_mirror_disk_state2str(disk->d_state)));
2498 * STALE state is only possible if device is marked
2501 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2502 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2503 g_mirror_device_state2str(sc->sc_state),
2504 g_mirror_get_diskname(disk),
2505 g_mirror_disk_state2str(disk->d_state)));
2506 DISK_STATE_CHANGED();
2508 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2509 disk->d_state = state;
2510 g_mirror_update_metadata(disk);
2511 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2512 sc->sc_name, g_mirror_get_diskname(disk));
2514 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2516 * Possible scenarios:
2517 * 1. Disk which needs synchronization was connected.
2519 /* Previous state should be NEW. */
2520 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2521 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2522 g_mirror_disk_state2str(disk->d_state)));
2523 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2524 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2525 g_mirror_device_state2str(sc->sc_state),
2526 g_mirror_get_diskname(disk),
2527 g_mirror_disk_state2str(disk->d_state)));
2528 DISK_STATE_CHANGED();
2530 if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2531 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2532 disk->d_state = state;
2533 if (sc->sc_provider != NULL) {
2534 g_mirror_sync_start(disk);
2535 g_mirror_update_metadata(disk);
2538 case G_MIRROR_DISK_STATE_DISCONNECTED:
2540 * Possible scenarios:
2541 * 1. Device wasn't running yet, but disk disappear.
2542 * 2. Disk was active and disapppear.
2543 * 3. Disk disappear during synchronization process.
2545 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2547 * Previous state should be ACTIVE, STALE or
2550 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2551 disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2552 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2553 ("Wrong disk state (%s, %s).",
2554 g_mirror_get_diskname(disk),
2555 g_mirror_disk_state2str(disk->d_state)));
2556 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2557 /* Previous state should be NEW. */
2558 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2559 ("Wrong disk state (%s, %s).",
2560 g_mirror_get_diskname(disk),
2561 g_mirror_disk_state2str(disk->d_state)));
2563 * Reset bumping syncid if disk disappeared in STARTING
2566 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2567 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2570 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2572 g_mirror_device_state2str(sc->sc_state),
2573 g_mirror_get_diskname(disk),
2574 g_mirror_disk_state2str(disk->d_state)));
2577 DISK_STATE_CHANGED();
2578 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2579 sc->sc_name, g_mirror_get_diskname(disk));
2581 g_mirror_destroy_disk(disk);
2583 case G_MIRROR_DISK_STATE_DESTROY:
2587 error = g_mirror_clear_metadata(disk);
2590 DISK_STATE_CHANGED();
2591 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2592 sc->sc_name, g_mirror_get_diskname(disk));
2594 g_mirror_destroy_disk(disk);
2596 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2597 g_mirror_update_metadata(disk);
2602 KASSERT(1 == 0, ("Unknown state (%u).", state));
2607 #undef DISK_STATE_CHANGED
2610 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2612 struct g_provider *pp;
2616 g_topology_assert();
2618 error = g_access(cp, 1, 0, 0);
2622 g_topology_unlock();
2623 /* Metadata are stored on last sector. */
2624 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2627 g_access(cp, -1, 0, 0);
2629 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2630 cp->provider->name, error);
2634 /* Decode metadata. */
2635 error = mirror_metadata_decode(buf, md);
2637 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2639 if (md->md_version > G_MIRROR_VERSION) {
2641 "Kernel module is too old to handle metadata from %s.",
2642 cp->provider->name);
2646 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2647 cp->provider->name);
2655 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2656 struct g_mirror_metadata *md)
2659 if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2660 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2661 pp->name, md->md_did);
2664 if (md->md_all != sc->sc_ndisks) {
2666 "Invalid '%s' field on disk %s (device %s), skipping.",
2667 "md_all", pp->name, sc->sc_name);
2670 if (md->md_slice != sc->sc_slice) {
2672 "Invalid '%s' field on disk %s (device %s), skipping.",
2673 "md_slice", pp->name, sc->sc_name);
2676 if (md->md_balance != sc->sc_balance) {
2678 "Invalid '%s' field on disk %s (device %s), skipping.",
2679 "md_balance", pp->name, sc->sc_name);
2682 if (md->md_mediasize != sc->sc_mediasize) {
2684 "Invalid '%s' field on disk %s (device %s), skipping.",
2685 "md_mediasize", pp->name, sc->sc_name);
2688 if (sc->sc_mediasize > pp->mediasize) {
2690 "Invalid size of disk %s (device %s), skipping.", pp->name,
2694 if (md->md_sectorsize != sc->sc_sectorsize) {
2696 "Invalid '%s' field on disk %s (device %s), skipping.",
2697 "md_sectorsize", pp->name, sc->sc_name);
2700 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2702 "Invalid sector size of disk %s (device %s), skipping.",
2703 pp->name, sc->sc_name);
2706 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2708 "Invalid device flags on disk %s (device %s), skipping.",
2709 pp->name, sc->sc_name);
2712 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2714 "Invalid disk flags on disk %s (device %s), skipping.",
2715 pp->name, sc->sc_name);
2722 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2723 struct g_mirror_metadata *md)
2725 struct g_mirror_disk *disk;
2728 g_topology_assert_not();
2729 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2731 error = g_mirror_check_metadata(sc, pp, md);
2734 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING &&
2735 md->md_genid < sc->sc_genid) {
2736 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
2737 pp->name, sc->sc_name);
2740 disk = g_mirror_init_disk(sc, pp, md, &error);
2743 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
2744 G_MIRROR_EVENT_WAIT);
2747 if (md->md_version < G_MIRROR_VERSION) {
2748 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
2749 pp->name, md->md_version, G_MIRROR_VERSION);
2750 g_mirror_update_metadata(disk);
2756 g_mirror_destroy_delayed(void *arg, int flag)
2758 struct g_mirror_softc *sc;
2761 if (flag == EV_CANCEL) {
2762 G_MIRROR_DEBUG(1, "Destroying canceled.");
2766 g_topology_unlock();
2767 sx_xlock(&sc->sc_lock);
2768 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
2769 ("DESTROY flag set on %s.", sc->sc_name));
2770 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0,
2771 ("DESTROYING flag not set on %s.", sc->sc_name));
2772 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
2773 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
2775 G_MIRROR_DEBUG(0, "Cannot destroy %s.", sc->sc_name);
2776 sx_xunlock(&sc->sc_lock);
2782 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
2784 struct g_mirror_softc *sc;
2785 int dcr, dcw, dce, error = 0;
2787 g_topology_assert();
2788 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2791 sc = pp->geom->softc;
2792 if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
2794 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
2796 dcr = pp->acr + acr;
2797 dcw = pp->acw + acw;
2798 dce = pp->ace + ace;
2800 g_topology_unlock();
2801 sx_xlock(&sc->sc_lock);
2802 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
2803 LIST_EMPTY(&sc->sc_disks)) {
2804 if (acr > 0 || acw > 0 || ace > 0)
2808 if (dcw == 0 && !sc->sc_idle)
2809 g_mirror_idle(sc, dcw);
2810 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0) {
2811 if (acr > 0 || acw > 0 || ace > 0) {
2815 if (dcr == 0 && dcw == 0 && dce == 0) {
2816 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK,
2821 sx_xunlock(&sc->sc_lock);
2826 static struct g_geom *
2827 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
2829 struct g_mirror_softc *sc;
2833 g_topology_assert();
2834 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
2837 /* One disk is minimum. */
2843 gp = g_new_geomf(mp, "%s", md->md_name);
2844 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
2845 gp->start = g_mirror_start;
2846 gp->orphan = g_mirror_orphan;
2847 gp->access = g_mirror_access;
2848 gp->dumpconf = g_mirror_dumpconf;
2850 sc->sc_id = md->md_mid;
2851 sc->sc_slice = md->md_slice;
2852 sc->sc_balance = md->md_balance;
2853 sc->sc_mediasize = md->md_mediasize;
2854 sc->sc_sectorsize = md->md_sectorsize;
2855 sc->sc_ndisks = md->md_all;
2856 sc->sc_flags = md->md_mflags;
2859 sc->sc_last_write = time_uptime;
2861 sx_init(&sc->sc_lock, "gmirror:lock");
2862 bioq_init(&sc->sc_queue);
2863 mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
2864 bioq_init(&sc->sc_regular_delayed);
2865 bioq_init(&sc->sc_inflight);
2866 bioq_init(&sc->sc_sync_delayed);
2867 LIST_INIT(&sc->sc_disks);
2868 TAILQ_INIT(&sc->sc_events);
2869 mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
2870 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
2871 sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
2874 sc->sc_provider = NULL;
2876 * Synchronization geom.
2878 gp = g_new_geomf(mp, "%s.sync", md->md_name);
2880 gp->orphan = g_mirror_orphan;
2881 sc->sc_sync.ds_geom = gp;
2882 sc->sc_sync.ds_ndisks = 0;
2883 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
2884 "g_mirror %s", md->md_name);
2886 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
2888 g_destroy_geom(sc->sc_sync.ds_geom);
2889 mtx_destroy(&sc->sc_events_mtx);
2890 mtx_destroy(&sc->sc_queue_mtx);
2891 sx_destroy(&sc->sc_lock);
2892 g_destroy_geom(sc->sc_geom);
2897 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
2898 sc->sc_name, sc->sc_ndisks, sc->sc_id);
2900 sc->sc_rootmount = root_mount_hold("GMIRROR");
2901 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
2905 timeout = g_mirror_timeout * hz;
2906 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
2907 return (sc->sc_geom);
2911 g_mirror_destroy(struct g_mirror_softc *sc, int how)
2913 struct g_mirror_disk *disk;
2914 struct g_provider *pp;
2916 g_topology_assert_not();
2919 sx_assert(&sc->sc_lock, SX_XLOCKED);
2921 pp = sc->sc_provider;
2922 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
2924 case G_MIRROR_DESTROY_SOFT:
2926 "Device %s is still open (r%dw%de%d).", pp->name,
2927 pp->acr, pp->acw, pp->ace);
2929 case G_MIRROR_DESTROY_DELAYED:
2931 "Device %s will be destroyed on last close.",
2933 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2934 if (disk->d_state ==
2935 G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2936 g_mirror_sync_stop(disk, 1);
2939 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROYING;
2941 case G_MIRROR_DESTROY_HARD:
2942 G_MIRROR_DEBUG(1, "Device %s is still open, so it "
2943 "can't be definitely removed.", pp->name);
2948 if (sc->sc_geom->softc == NULL) {
2949 g_topology_unlock();
2952 sc->sc_geom->softc = NULL;
2953 sc->sc_sync.ds_geom->softc = NULL;
2954 g_topology_unlock();
2956 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2957 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WAIT;
2958 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
2959 sx_xunlock(&sc->sc_lock);
2960 mtx_lock(&sc->sc_queue_mtx);
2962 mtx_unlock(&sc->sc_queue_mtx);
2963 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
2964 while (sc->sc_worker != NULL)
2965 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
2966 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
2967 sx_xlock(&sc->sc_lock);
2968 g_mirror_destroy_device(sc);
2974 g_mirror_taste_orphan(struct g_consumer *cp)
2977 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2978 cp->provider->name));
2981 static struct g_geom *
2982 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2984 struct g_mirror_metadata md;
2985 struct g_mirror_softc *sc;
2986 struct g_consumer *cp;
2990 g_topology_assert();
2991 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
2992 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
2994 gp = g_new_geomf(mp, "mirror:taste");
2996 * This orphan function should be never called.
2998 gp->orphan = g_mirror_taste_orphan;
2999 cp = g_new_consumer(gp);
3001 error = g_mirror_read_metadata(cp, &md);
3003 g_destroy_consumer(cp);
3009 if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0)
3011 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3013 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3015 "Device %s: provider %s marked as inactive, skipping.",
3016 md.md_name, pp->name);
3019 if (g_mirror_debug >= 2)
3020 mirror_metadata_dump(&md);
3023 * Let's check if device already exists.
3026 LIST_FOREACH(gp, &mp->geom, geom) {
3030 if (sc->sc_sync.ds_geom == gp)
3032 if (strcmp(md.md_name, sc->sc_name) != 0)
3034 if (md.md_mid != sc->sc_id) {
3035 G_MIRROR_DEBUG(0, "Device %s already configured.",
3042 gp = g_mirror_create(mp, &md);
3044 G_MIRROR_DEBUG(0, "Cannot create device %s.",
3050 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3051 g_topology_unlock();
3052 sx_xlock(&sc->sc_lock);
3053 error = g_mirror_add_disk(sc, pp, &md);
3055 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3056 pp->name, gp->name, error);
3057 if (LIST_EMPTY(&sc->sc_disks)) {
3059 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3065 sx_xunlock(&sc->sc_lock);
3071 g_mirror_destroy_geom(struct gctl_req *req __unused,
3072 struct g_class *mp __unused, struct g_geom *gp)
3074 struct g_mirror_softc *sc;
3077 g_topology_unlock();
3079 sx_xlock(&sc->sc_lock);
3081 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3083 sx_xunlock(&sc->sc_lock);
3089 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3090 struct g_consumer *cp, struct g_provider *pp)
3092 struct g_mirror_softc *sc;
3094 g_topology_assert();
3099 /* Skip synchronization geom. */
3100 if (gp == sc->sc_sync.ds_geom)
3104 } else if (cp != NULL) {
3105 struct g_mirror_disk *disk;
3110 g_topology_unlock();
3111 sx_xlock(&sc->sc_lock);
3112 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3113 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3114 sbuf_printf(sb, "%s<Synchronized>", indent);
3115 if (disk->d_sync.ds_offset == 0)
3116 sbuf_printf(sb, "0%%");
3118 sbuf_printf(sb, "%u%%",
3119 (u_int)((disk->d_sync.ds_offset * 100) /
3120 sc->sc_provider->mediasize));
3122 sbuf_printf(sb, "</Synchronized>\n");
3124 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3125 disk->d_sync.ds_syncid);
3126 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3128 sbuf_printf(sb, "%s<Flags>", indent);
3129 if (disk->d_flags == 0)
3130 sbuf_printf(sb, "NONE");
3134 #define ADD_FLAG(flag, name) do { \
3135 if ((disk->d_flags & (flag)) != 0) { \
3137 sbuf_printf(sb, ", "); \
3140 sbuf_printf(sb, name); \
3143 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3144 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3145 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3146 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3148 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3149 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3152 sbuf_printf(sb, "</Flags>\n");
3153 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3155 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3156 g_mirror_disk_state2str(disk->d_state));
3157 sx_xunlock(&sc->sc_lock);
3160 g_topology_unlock();
3161 sx_xlock(&sc->sc_lock);
3162 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3163 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3164 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3165 sbuf_printf(sb, "%s<Flags>", indent);
3166 if (sc->sc_flags == 0)
3167 sbuf_printf(sb, "NONE");
3171 #define ADD_FLAG(flag, name) do { \
3172 if ((sc->sc_flags & (flag)) != 0) { \
3174 sbuf_printf(sb, ", "); \
3177 sbuf_printf(sb, name); \
3180 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3181 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3184 sbuf_printf(sb, "</Flags>\n");
3185 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3186 (u_int)sc->sc_slice);
3187 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3188 balance_name(sc->sc_balance));
3189 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3191 sbuf_printf(sb, "%s<State>", indent);
3192 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3193 sbuf_printf(sb, "%s", "STARTING");
3194 else if (sc->sc_ndisks ==
3195 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3196 sbuf_printf(sb, "%s", "COMPLETE");
3198 sbuf_printf(sb, "%s", "DEGRADED");
3199 sbuf_printf(sb, "</State>\n");
3200 sx_xunlock(&sc->sc_lock);
3206 g_mirror_shutdown_pre_sync(void *arg, int howto)
3209 struct g_geom *gp, *gp2;
3210 struct g_mirror_softc *sc;
3216 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3217 if ((sc = gp->softc) == NULL)
3219 /* Skip synchronization geom. */
3220 if (gp == sc->sc_sync.ds_geom)
3222 g_topology_unlock();
3223 sx_xlock(&sc->sc_lock);
3225 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3227 sx_xunlock(&sc->sc_lock);
3230 g_topology_unlock();
3235 g_mirror_init(struct g_class *mp)
3238 g_mirror_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
3239 g_mirror_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
3240 if (g_mirror_pre_sync == NULL)
3241 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3245 g_mirror_fini(struct g_class *mp)
3248 if (g_mirror_pre_sync != NULL)
3249 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_mirror_pre_sync);
3252 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);