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 static SYSCTL_NODE(_kern_geom, OID_AUTO, mirror, CTLFLAG_RW, 0,
56 u_int g_mirror_debug = 0;
57 TUNABLE_INT("kern.geom.mirror.debug", &g_mirror_debug);
58 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, debug, CTLFLAG_RW, &g_mirror_debug, 0,
60 static u_int g_mirror_timeout = 4;
61 TUNABLE_INT("kern.geom.mirror.timeout", &g_mirror_timeout);
62 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, timeout, CTLFLAG_RW, &g_mirror_timeout,
63 0, "Time to wait on all mirror components");
64 static u_int g_mirror_idletime = 5;
65 TUNABLE_INT("kern.geom.mirror.idletime", &g_mirror_idletime);
66 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, idletime, CTLFLAG_RW,
67 &g_mirror_idletime, 0, "Mark components as clean when idling");
68 static u_int g_mirror_disconnect_on_failure = 1;
69 TUNABLE_INT("kern.geom.mirror.disconnect_on_failure",
70 &g_mirror_disconnect_on_failure);
71 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
72 &g_mirror_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
73 static u_int g_mirror_syncreqs = 2;
74 TUNABLE_INT("kern.geom.mirror.sync_requests", &g_mirror_syncreqs);
75 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
76 &g_mirror_syncreqs, 0, "Parallel synchronization I/O requests.");
78 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \
79 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
80 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
81 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
84 static eventhandler_tag g_mirror_post_sync = NULL;
85 static int g_mirror_shutdown = 0;
87 static int g_mirror_destroy_geom(struct gctl_req *req, struct g_class *mp,
89 static g_taste_t g_mirror_taste;
90 static void g_mirror_init(struct g_class *mp);
91 static void g_mirror_fini(struct g_class *mp);
93 struct g_class g_mirror_class = {
94 .name = G_MIRROR_CLASS_NAME,
96 .ctlreq = g_mirror_config,
97 .taste = g_mirror_taste,
98 .destroy_geom = g_mirror_destroy_geom,
99 .init = g_mirror_init,
100 .fini = g_mirror_fini
104 static void g_mirror_destroy_provider(struct g_mirror_softc *sc);
105 static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
106 static void g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force);
107 static void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
108 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
109 static void g_mirror_sync_stop(struct g_mirror_disk *disk, int type);
110 static void g_mirror_register_request(struct bio *bp);
111 static void g_mirror_sync_release(struct g_mirror_softc *sc);
115 g_mirror_disk_state2str(int state)
119 case G_MIRROR_DISK_STATE_NONE:
121 case G_MIRROR_DISK_STATE_NEW:
123 case G_MIRROR_DISK_STATE_ACTIVE:
125 case G_MIRROR_DISK_STATE_STALE:
127 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
128 return ("SYNCHRONIZING");
129 case G_MIRROR_DISK_STATE_DISCONNECTED:
130 return ("DISCONNECTED");
131 case G_MIRROR_DISK_STATE_DESTROY:
139 g_mirror_device_state2str(int state)
143 case G_MIRROR_DEVICE_STATE_STARTING:
145 case G_MIRROR_DEVICE_STATE_RUNNING:
153 g_mirror_get_diskname(struct g_mirror_disk *disk)
156 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
157 return ("[unknown]");
158 return (disk->d_name);
162 * --- Events handling functions ---
163 * Events in geom_mirror are used to maintain disks and device status
164 * from one thread to simplify locking.
167 g_mirror_event_free(struct g_mirror_event *ep)
174 g_mirror_event_send(void *arg, int state, int flags)
176 struct g_mirror_softc *sc;
177 struct g_mirror_disk *disk;
178 struct g_mirror_event *ep;
181 ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
182 G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
183 if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
194 mtx_lock(&sc->sc_events_mtx);
195 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
196 mtx_unlock(&sc->sc_events_mtx);
197 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
198 mtx_lock(&sc->sc_queue_mtx);
200 mtx_unlock(&sc->sc_queue_mtx);
201 if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0)
203 sx_assert(&sc->sc_lock, SX_XLOCKED);
204 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
205 sx_xunlock(&sc->sc_lock);
206 while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) {
207 mtx_lock(&sc->sc_events_mtx);
208 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event",
212 g_mirror_event_free(ep);
213 sx_xlock(&sc->sc_lock);
217 static struct g_mirror_event *
218 g_mirror_event_get(struct g_mirror_softc *sc)
220 struct g_mirror_event *ep;
222 mtx_lock(&sc->sc_events_mtx);
223 ep = TAILQ_FIRST(&sc->sc_events);
224 mtx_unlock(&sc->sc_events_mtx);
229 g_mirror_event_remove(struct g_mirror_softc *sc, struct g_mirror_event *ep)
232 mtx_lock(&sc->sc_events_mtx);
233 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
234 mtx_unlock(&sc->sc_events_mtx);
238 g_mirror_event_cancel(struct g_mirror_disk *disk)
240 struct g_mirror_softc *sc;
241 struct g_mirror_event *ep, *tmpep;
244 sx_assert(&sc->sc_lock, SX_XLOCKED);
246 mtx_lock(&sc->sc_events_mtx);
247 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
248 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0)
250 if (ep->e_disk != disk)
252 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
253 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
254 g_mirror_event_free(ep);
256 ep->e_error = ECANCELED;
260 mtx_unlock(&sc->sc_events_mtx);
264 * Return the number of disks in given state.
265 * If state is equal to -1, count all connected disks.
268 g_mirror_ndisks(struct g_mirror_softc *sc, int state)
270 struct g_mirror_disk *disk;
273 sx_assert(&sc->sc_lock, SX_LOCKED);
275 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
276 if (state == -1 || disk->d_state == state)
283 * Find a disk in mirror by its disk ID.
285 static struct g_mirror_disk *
286 g_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id)
288 struct g_mirror_disk *disk;
290 sx_assert(&sc->sc_lock, SX_XLOCKED);
292 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
293 if (disk->d_id == id)
300 g_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp)
305 mtx_lock(&sc->sc_queue_mtx);
306 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
307 if (bp->bio_from == cp)
310 mtx_unlock(&sc->sc_queue_mtx);
315 g_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp)
320 "I/O requests for %s exist, can't destroy it now.",
324 if (g_mirror_nrequests(sc, cp) > 0) {
326 "I/O requests for %s in queue, can't destroy it now.",
334 g_mirror_destroy_consumer(void *arg, int flags __unused)
336 struct g_consumer *cp;
341 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
343 g_destroy_consumer(cp);
347 g_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
349 struct g_provider *pp;
355 if (g_mirror_is_busy(sc, cp))
360 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
363 G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
364 -cp->acw, -cp->ace, 0);
365 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
366 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
369 * After retaste event was send (inside g_access()), we can send
370 * event to detach and destroy consumer.
371 * A class, which has consumer to the given provider connected
372 * will not receive retaste event for the provider.
373 * This is the way how I ignore retaste events when I close
374 * consumers opened for write: I detach and destroy consumer
375 * after retaste event is sent.
377 g_post_event(g_mirror_destroy_consumer, cp, M_WAITOK, NULL);
380 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", pp->name);
382 g_destroy_consumer(cp);
386 g_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp)
388 struct g_consumer *cp;
391 g_topology_assert_not();
392 KASSERT(disk->d_consumer == NULL,
393 ("Disk already connected (device %s).", disk->d_softc->sc_name));
396 cp = g_new_consumer(disk->d_softc->sc_geom);
397 error = g_attach(cp, pp);
399 g_destroy_consumer(cp);
403 error = g_access(cp, 1, 1, 1);
406 g_destroy_consumer(cp);
408 G_MIRROR_DEBUG(0, "Cannot open consumer %s (error=%d).",
413 disk->d_consumer = cp;
414 disk->d_consumer->private = disk;
415 disk->d_consumer->index = 0;
417 G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk));
422 g_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
429 if (cp->provider != NULL)
430 g_mirror_kill_consumer(sc, cp);
432 g_destroy_consumer(cp);
436 * Initialize disk. This means allocate memory, create consumer, attach it
437 * to the provider and open access (r1w1e1) to it.
439 static struct g_mirror_disk *
440 g_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp,
441 struct g_mirror_metadata *md, int *errorp)
443 struct g_mirror_disk *disk;
446 disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO);
452 error = g_mirror_connect_disk(disk, pp);
455 disk->d_id = md->md_did;
456 disk->d_state = G_MIRROR_DISK_STATE_NONE;
457 disk->d_priority = md->md_priority;
458 disk->d_flags = md->md_dflags;
459 error = g_getattr("GEOM::candelete", disk->d_consumer, &i);
460 if (error == 0 && i != 0)
461 disk->d_flags |= G_MIRROR_DISK_FLAG_CANDELETE;
462 if (md->md_provider[0] != '\0')
463 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
464 disk->d_sync.ds_consumer = NULL;
465 disk->d_sync.ds_offset = md->md_sync_offset;
466 disk->d_sync.ds_offset_done = md->md_sync_offset;
467 disk->d_genid = md->md_genid;
468 disk->d_sync.ds_syncid = md->md_syncid;
476 free(disk, M_MIRROR);
481 g_mirror_destroy_disk(struct g_mirror_disk *disk)
483 struct g_mirror_softc *sc;
485 g_topology_assert_not();
487 sx_assert(&sc->sc_lock, SX_XLOCKED);
489 LIST_REMOVE(disk, d_next);
490 g_mirror_event_cancel(disk);
491 if (sc->sc_hint == disk)
493 switch (disk->d_state) {
494 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
495 g_mirror_sync_stop(disk, 1);
497 case G_MIRROR_DISK_STATE_NEW:
498 case G_MIRROR_DISK_STATE_STALE:
499 case G_MIRROR_DISK_STATE_ACTIVE:
501 g_mirror_disconnect_consumer(sc, disk->d_consumer);
503 free(disk, M_MIRROR);
506 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
507 g_mirror_get_diskname(disk),
508 g_mirror_disk_state2str(disk->d_state)));
513 g_mirror_destroy_device(struct g_mirror_softc *sc)
515 struct g_mirror_disk *disk;
516 struct g_mirror_event *ep;
518 struct g_consumer *cp, *tmpcp;
520 g_topology_assert_not();
521 sx_assert(&sc->sc_lock, SX_XLOCKED);
524 if (sc->sc_provider != NULL)
525 g_mirror_destroy_provider(sc);
526 for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL;
527 disk = LIST_FIRST(&sc->sc_disks)) {
528 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
529 g_mirror_update_metadata(disk);
530 g_mirror_destroy_disk(disk);
532 while ((ep = g_mirror_event_get(sc)) != NULL) {
533 g_mirror_event_remove(sc, ep);
534 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
535 g_mirror_event_free(ep);
537 ep->e_error = ECANCELED;
538 ep->e_flags |= G_MIRROR_EVENT_DONE;
539 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep);
540 mtx_lock(&sc->sc_events_mtx);
542 mtx_unlock(&sc->sc_events_mtx);
545 callout_drain(&sc->sc_callout);
548 LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
549 g_mirror_disconnect_consumer(sc, cp);
551 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
552 G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
553 g_wither_geom(gp, ENXIO);
555 mtx_destroy(&sc->sc_queue_mtx);
556 mtx_destroy(&sc->sc_events_mtx);
557 sx_xunlock(&sc->sc_lock);
558 sx_destroy(&sc->sc_lock);
562 g_mirror_orphan(struct g_consumer *cp)
564 struct g_mirror_disk *disk;
571 disk->d_softc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
572 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
573 G_MIRROR_EVENT_DONTWAIT);
577 * Function should return the next active disk on the list.
578 * It is possible that it will be the same disk as given.
579 * If there are no active disks on list, NULL is returned.
581 static __inline struct g_mirror_disk *
582 g_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
584 struct g_mirror_disk *dp;
586 for (dp = LIST_NEXT(disk, d_next); dp != disk;
587 dp = LIST_NEXT(dp, d_next)) {
589 dp = LIST_FIRST(&sc->sc_disks);
590 if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE)
593 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
598 static struct g_mirror_disk *
599 g_mirror_get_disk(struct g_mirror_softc *sc)
601 struct g_mirror_disk *disk;
603 if (sc->sc_hint == NULL) {
604 sc->sc_hint = LIST_FIRST(&sc->sc_disks);
605 if (sc->sc_hint == NULL)
609 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) {
610 disk = g_mirror_find_next(sc, disk);
614 sc->sc_hint = g_mirror_find_next(sc, disk);
619 g_mirror_write_metadata(struct g_mirror_disk *disk,
620 struct g_mirror_metadata *md)
622 struct g_mirror_softc *sc;
623 struct g_consumer *cp;
624 off_t offset, length;
628 g_topology_assert_not();
630 sx_assert(&sc->sc_lock, SX_LOCKED);
632 cp = disk->d_consumer;
633 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
634 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
635 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
636 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
638 length = cp->provider->sectorsize;
639 offset = cp->provider->mediasize - length;
640 sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO);
642 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0)
643 mirror_metadata_encode(md, sector);
644 error = g_write_data(cp, offset, sector, length);
645 free(sector, M_MIRROR);
647 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
648 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
649 G_MIRROR_DEBUG(0, "Cannot write metadata on %s "
650 "(device=%s, error=%d).",
651 g_mirror_get_diskname(disk), sc->sc_name, error);
653 G_MIRROR_DEBUG(1, "Cannot write metadata on %s "
654 "(device=%s, error=%d).",
655 g_mirror_get_diskname(disk), sc->sc_name, error);
657 if (g_mirror_disconnect_on_failure &&
658 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) {
659 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
660 g_mirror_event_send(disk,
661 G_MIRROR_DISK_STATE_DISCONNECTED,
662 G_MIRROR_EVENT_DONTWAIT);
669 g_mirror_clear_metadata(struct g_mirror_disk *disk)
673 g_topology_assert_not();
674 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
676 error = g_mirror_write_metadata(disk, NULL);
678 G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
679 g_mirror_get_diskname(disk));
682 "Cannot clear metadata on disk %s (error=%d).",
683 g_mirror_get_diskname(disk), error);
689 g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
690 struct g_mirror_metadata *md)
693 strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
694 md->md_version = G_MIRROR_VERSION;
695 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
696 md->md_mid = sc->sc_id;
697 md->md_all = sc->sc_ndisks;
698 md->md_slice = sc->sc_slice;
699 md->md_balance = sc->sc_balance;
700 md->md_genid = sc->sc_genid;
701 md->md_mediasize = sc->sc_mediasize;
702 md->md_sectorsize = sc->sc_sectorsize;
703 md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
704 bzero(md->md_provider, sizeof(md->md_provider));
706 md->md_did = arc4random();
710 md->md_sync_offset = 0;
713 md->md_did = disk->d_id;
714 md->md_priority = disk->d_priority;
715 md->md_syncid = disk->d_sync.ds_syncid;
716 md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
717 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
718 md->md_sync_offset = disk->d_sync.ds_offset_done;
720 md->md_sync_offset = 0;
721 if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
722 strlcpy(md->md_provider,
723 disk->d_consumer->provider->name,
724 sizeof(md->md_provider));
726 md->md_provsize = disk->d_consumer->provider->mediasize;
731 g_mirror_update_metadata(struct g_mirror_disk *disk)
733 struct g_mirror_softc *sc;
734 struct g_mirror_metadata md;
737 g_topology_assert_not();
739 sx_assert(&sc->sc_lock, SX_LOCKED);
741 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0)
742 g_mirror_fill_metadata(sc, disk, &md);
743 error = g_mirror_write_metadata(disk, &md);
745 G_MIRROR_DEBUG(2, "Metadata on %s updated.",
746 g_mirror_get_diskname(disk));
749 "Cannot update metadata on disk %s (error=%d).",
750 g_mirror_get_diskname(disk), error);
755 g_mirror_bump_syncid(struct g_mirror_softc *sc)
757 struct g_mirror_disk *disk;
759 g_topology_assert_not();
760 sx_assert(&sc->sc_lock, SX_XLOCKED);
761 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
762 ("%s called with no active disks (device=%s).", __func__,
766 G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
768 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
769 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
770 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
771 disk->d_sync.ds_syncid = sc->sc_syncid;
772 g_mirror_update_metadata(disk);
778 g_mirror_bump_genid(struct g_mirror_softc *sc)
780 struct g_mirror_disk *disk;
782 g_topology_assert_not();
783 sx_assert(&sc->sc_lock, SX_XLOCKED);
784 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
785 ("%s called with no active disks (device=%s).", __func__,
789 G_MIRROR_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
791 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
792 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
793 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
794 disk->d_genid = sc->sc_genid;
795 g_mirror_update_metadata(disk);
801 g_mirror_idle(struct g_mirror_softc *sc, int acw)
803 struct g_mirror_disk *disk;
806 g_topology_assert_not();
807 sx_assert(&sc->sc_lock, SX_XLOCKED);
809 if (sc->sc_provider == NULL)
811 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
815 if (sc->sc_writes > 0)
817 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
818 timeout = g_mirror_idletime - (time_uptime - sc->sc_last_write);
819 if (!g_mirror_shutdown && timeout > 0)
823 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
824 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
826 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
827 g_mirror_get_diskname(disk), sc->sc_name);
828 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
829 g_mirror_update_metadata(disk);
835 g_mirror_unidle(struct g_mirror_softc *sc)
837 struct g_mirror_disk *disk;
839 g_topology_assert_not();
840 sx_assert(&sc->sc_lock, SX_XLOCKED);
842 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
845 sc->sc_last_write = time_uptime;
846 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
847 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
849 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
850 g_mirror_get_diskname(disk), sc->sc_name);
851 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
852 g_mirror_update_metadata(disk);
857 g_mirror_done(struct bio *bp)
859 struct g_mirror_softc *sc;
861 sc = bp->bio_from->geom->softc;
862 bp->bio_cflags = G_MIRROR_BIO_FLAG_REGULAR;
863 mtx_lock(&sc->sc_queue_mtx);
864 bioq_disksort(&sc->sc_queue, bp);
865 mtx_unlock(&sc->sc_queue_mtx);
870 g_mirror_regular_request(struct bio *bp)
872 struct g_mirror_softc *sc;
873 struct g_mirror_disk *disk;
876 g_topology_assert_not();
878 pbp = bp->bio_parent;
879 sc = pbp->bio_to->geom->softc;
880 bp->bio_from->index--;
881 if (bp->bio_cmd == BIO_WRITE)
883 disk = bp->bio_from->private;
886 g_mirror_kill_consumer(sc, bp->bio_from);
891 KASSERT(pbp->bio_inbed <= pbp->bio_children,
892 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
894 if (bp->bio_error == 0 && pbp->bio_error == 0) {
895 G_MIRROR_LOGREQ(3, bp, "Request delivered.");
897 if (pbp->bio_children == pbp->bio_inbed) {
898 G_MIRROR_LOGREQ(3, pbp, "Request delivered.");
899 pbp->bio_completed = pbp->bio_length;
900 if (pbp->bio_cmd == BIO_WRITE ||
901 pbp->bio_cmd == BIO_DELETE) {
902 bioq_remove(&sc->sc_inflight, pbp);
903 /* Release delayed sync requests if possible. */
904 g_mirror_sync_release(sc);
906 g_io_deliver(pbp, pbp->bio_error);
909 } else if (bp->bio_error != 0) {
910 if (pbp->bio_error == 0)
911 pbp->bio_error = bp->bio_error;
913 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
914 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
915 G_MIRROR_LOGREQ(0, bp,
916 "Request failed (error=%d).",
919 G_MIRROR_LOGREQ(1, bp,
920 "Request failed (error=%d).",
923 if (g_mirror_disconnect_on_failure &&
924 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1)
926 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
927 g_mirror_event_send(disk,
928 G_MIRROR_DISK_STATE_DISCONNECTED,
929 G_MIRROR_EVENT_DONTWAIT);
932 switch (pbp->bio_cmd) {
942 switch (pbp->bio_cmd) {
944 if (pbp->bio_inbed < pbp->bio_children)
946 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1)
947 g_io_deliver(pbp, pbp->bio_error);
950 mtx_lock(&sc->sc_queue_mtx);
951 bioq_disksort(&sc->sc_queue, pbp);
952 mtx_unlock(&sc->sc_queue_mtx);
953 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
959 if (pbp->bio_children == 0) {
961 * All requests failed.
963 } else if (pbp->bio_inbed < pbp->bio_children) {
966 } else if (pbp->bio_children == pbp->bio_inbed) {
967 /* Some requests succeeded. */
969 pbp->bio_completed = pbp->bio_length;
971 bioq_remove(&sc->sc_inflight, pbp);
972 /* Release delayed sync requests if possible. */
973 g_mirror_sync_release(sc);
974 g_io_deliver(pbp, pbp->bio_error);
977 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
983 g_mirror_sync_done(struct bio *bp)
985 struct g_mirror_softc *sc;
987 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
988 sc = bp->bio_from->geom->softc;
989 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
990 mtx_lock(&sc->sc_queue_mtx);
991 bioq_disksort(&sc->sc_queue, bp);
992 mtx_unlock(&sc->sc_queue_mtx);
997 g_mirror_kernel_dump(struct bio *bp)
999 struct g_mirror_softc *sc;
1000 struct g_mirror_disk *disk;
1002 struct g_kerneldump *gkd;
1005 * We configure dumping to the first component, because this component
1006 * will be used for reading with 'prefer' balance algorithm.
1007 * If the component with the higest priority is currently disconnected
1008 * we will not be able to read the dump after the reboot if it will be
1009 * connected and synchronized later. Can we do something better?
1011 sc = bp->bio_to->geom->softc;
1012 disk = LIST_FIRST(&sc->sc_disks);
1014 gkd = (struct g_kerneldump *)bp->bio_data;
1015 if (gkd->length > bp->bio_to->mediasize)
1016 gkd->length = bp->bio_to->mediasize;
1017 cbp = g_clone_bio(bp);
1019 g_io_deliver(bp, ENOMEM);
1022 cbp->bio_done = g_std_done;
1023 g_io_request(cbp, disk->d_consumer);
1024 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1025 g_mirror_get_diskname(disk));
1029 g_mirror_flush(struct g_mirror_softc *sc, struct bio *bp)
1031 struct bio_queue_head queue;
1032 struct g_mirror_disk *disk;
1033 struct g_consumer *cp;
1037 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1038 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1040 cbp = g_clone_bio(bp);
1042 for (cbp = bioq_first(&queue); cbp != NULL;
1043 cbp = bioq_first(&queue)) {
1044 bioq_remove(&queue, cbp);
1047 if (bp->bio_error == 0)
1048 bp->bio_error = ENOMEM;
1049 g_io_deliver(bp, bp->bio_error);
1052 bioq_insert_tail(&queue, cbp);
1053 cbp->bio_done = g_std_done;
1054 cbp->bio_caller1 = disk;
1055 cbp->bio_to = disk->d_consumer->provider;
1057 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1058 bioq_remove(&queue, cbp);
1059 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1060 disk = cbp->bio_caller1;
1061 cbp->bio_caller1 = NULL;
1062 cp = disk->d_consumer;
1063 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1064 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1065 cp->acr, cp->acw, cp->ace));
1066 g_io_request(cbp, disk->d_consumer);
1071 g_mirror_start(struct bio *bp)
1073 struct g_mirror_softc *sc;
1075 sc = bp->bio_to->geom->softc;
1077 * If sc == NULL or there are no valid disks, provider's error
1078 * should be set and g_mirror_start() should not be called at all.
1080 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1081 ("Provider's error should be set (error=%d)(mirror=%s).",
1082 bp->bio_to->error, bp->bio_to->name));
1083 G_MIRROR_LOGREQ(3, bp, "Request received.");
1085 switch (bp->bio_cmd) {
1091 g_mirror_flush(sc, bp);
1094 if (g_handleattr_int(bp, "GEOM::candelete", 1))
1096 else if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1097 g_mirror_kernel_dump(bp);
1102 g_io_deliver(bp, EOPNOTSUPP);
1105 mtx_lock(&sc->sc_queue_mtx);
1106 bioq_disksort(&sc->sc_queue, bp);
1107 mtx_unlock(&sc->sc_queue_mtx);
1108 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1113 * Return TRUE if the given request is colliding with a in-progress
1114 * synchronization request.
1117 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1119 struct g_mirror_disk *disk;
1121 off_t rstart, rend, sstart, send;
1124 if (sc->sc_sync.ds_ndisks == 0)
1126 rstart = bp->bio_offset;
1127 rend = bp->bio_offset + bp->bio_length;
1128 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1129 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1131 for (i = 0; i < g_mirror_syncreqs; i++) {
1132 sbp = disk->d_sync.ds_bios[i];
1135 sstart = sbp->bio_offset;
1136 send = sbp->bio_offset + sbp->bio_length;
1137 if (rend > sstart && rstart < send)
1145 * Return TRUE if the given sync request is colliding with a in-progress regular
1149 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1151 off_t rstart, rend, sstart, send;
1154 if (sc->sc_sync.ds_ndisks == 0)
1156 sstart = sbp->bio_offset;
1157 send = sbp->bio_offset + sbp->bio_length;
1158 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1159 rstart = bp->bio_offset;
1160 rend = bp->bio_offset + bp->bio_length;
1161 if (rend > sstart && rstart < send)
1168 * Puts request onto delayed queue.
1171 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1174 G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1175 bioq_insert_head(&sc->sc_regular_delayed, bp);
1179 * Puts synchronization request onto delayed queue.
1182 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1185 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1186 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1190 * Releases delayed regular requests which don't collide anymore with sync
1194 g_mirror_regular_release(struct g_mirror_softc *sc)
1196 struct bio *bp, *bp2;
1198 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1199 if (g_mirror_sync_collision(sc, bp))
1201 bioq_remove(&sc->sc_regular_delayed, bp);
1202 G_MIRROR_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1203 mtx_lock(&sc->sc_queue_mtx);
1204 bioq_insert_head(&sc->sc_queue, bp);
1207 * wakeup() is not needed, because this function is called from
1208 * the worker thread.
1210 wakeup(&sc->sc_queue);
1212 mtx_unlock(&sc->sc_queue_mtx);
1217 * Releases delayed sync requests which don't collide anymore with regular
1221 g_mirror_sync_release(struct g_mirror_softc *sc)
1223 struct bio *bp, *bp2;
1225 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1226 if (g_mirror_regular_collision(sc, bp))
1228 bioq_remove(&sc->sc_sync_delayed, bp);
1229 G_MIRROR_LOGREQ(2, bp,
1230 "Releasing delayed synchronization request.");
1231 g_io_request(bp, bp->bio_from);
1236 * Handle synchronization requests.
1237 * Every synchronization request is two-steps process: first, READ request is
1238 * send to active provider and then WRITE request (with read data) to the provider
1239 * beeing synchronized. When WRITE is finished, new synchronization request is
1243 g_mirror_sync_request(struct bio *bp)
1245 struct g_mirror_softc *sc;
1246 struct g_mirror_disk *disk;
1248 bp->bio_from->index--;
1249 sc = bp->bio_from->geom->softc;
1250 disk = bp->bio_from->private;
1252 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1254 g_mirror_kill_consumer(sc, bp->bio_from);
1255 g_topology_unlock();
1256 free(bp->bio_data, M_MIRROR);
1258 sx_xlock(&sc->sc_lock);
1263 * Synchronization request.
1265 switch (bp->bio_cmd) {
1268 struct g_consumer *cp;
1270 if (bp->bio_error != 0) {
1271 G_MIRROR_LOGREQ(0, bp,
1272 "Synchronization request failed (error=%d).",
1277 G_MIRROR_LOGREQ(3, bp,
1278 "Synchronization request half-finished.");
1279 bp->bio_cmd = BIO_WRITE;
1281 cp = disk->d_consumer;
1282 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1283 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1284 cp->acr, cp->acw, cp->ace));
1286 g_io_request(bp, cp);
1291 struct g_mirror_disk_sync *sync;
1296 if (bp->bio_error != 0) {
1297 G_MIRROR_LOGREQ(0, bp,
1298 "Synchronization request failed (error=%d).",
1301 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1302 g_mirror_event_send(disk,
1303 G_MIRROR_DISK_STATE_DISCONNECTED,
1304 G_MIRROR_EVENT_DONTWAIT);
1307 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1308 sync = &disk->d_sync;
1309 if (sync->ds_offset == sc->sc_mediasize ||
1310 sync->ds_consumer == NULL ||
1311 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1312 /* Don't send more synchronization requests. */
1313 sync->ds_inflight--;
1314 if (sync->ds_bios != NULL) {
1315 i = (int)(uintptr_t)bp->bio_caller1;
1316 sync->ds_bios[i] = NULL;
1318 free(bp->bio_data, M_MIRROR);
1320 if (sync->ds_inflight > 0)
1322 if (sync->ds_consumer == NULL ||
1323 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1326 /* Disk up-to-date, activate it. */
1327 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1328 G_MIRROR_EVENT_DONTWAIT);
1332 /* Send next synchronization request. */
1333 data = bp->bio_data;
1334 bzero(bp, sizeof(*bp));
1335 bp->bio_cmd = BIO_READ;
1336 bp->bio_offset = sync->ds_offset;
1337 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1338 sync->ds_offset += bp->bio_length;
1339 bp->bio_done = g_mirror_sync_done;
1340 bp->bio_data = data;
1341 bp->bio_from = sync->ds_consumer;
1342 bp->bio_to = sc->sc_provider;
1343 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1344 sync->ds_consumer->index++;
1346 * Delay the request if it is colliding with a regular request.
1348 if (g_mirror_regular_collision(sc, bp))
1349 g_mirror_sync_delay(sc, bp);
1351 g_io_request(bp, sync->ds_consumer);
1353 /* Release delayed requests if possible. */
1354 g_mirror_regular_release(sc);
1356 /* Find the smallest offset */
1357 offset = sc->sc_mediasize;
1358 for (i = 0; i < g_mirror_syncreqs; i++) {
1359 bp = sync->ds_bios[i];
1360 if (bp->bio_offset < offset)
1361 offset = bp->bio_offset;
1363 if (sync->ds_offset_done + (MAXPHYS * 100) < offset) {
1364 /* Update offset_done on every 100 blocks. */
1365 sync->ds_offset_done = offset;
1366 g_mirror_update_metadata(disk);
1371 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1372 bp->bio_cmd, sc->sc_name));
1378 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1380 struct g_mirror_disk *disk;
1381 struct g_consumer *cp;
1384 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1385 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1389 if (bp->bio_error == 0)
1390 bp->bio_error = ENXIO;
1391 g_io_deliver(bp, bp->bio_error);
1394 cbp = g_clone_bio(bp);
1396 if (bp->bio_error == 0)
1397 bp->bio_error = ENOMEM;
1398 g_io_deliver(bp, bp->bio_error);
1402 * Fill in the component buf structure.
1404 cp = disk->d_consumer;
1405 cbp->bio_done = g_mirror_done;
1406 cbp->bio_to = cp->provider;
1407 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1408 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1409 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1412 g_io_request(cbp, cp);
1416 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1418 struct g_mirror_disk *disk;
1419 struct g_consumer *cp;
1422 disk = g_mirror_get_disk(sc);
1424 if (bp->bio_error == 0)
1425 bp->bio_error = ENXIO;
1426 g_io_deliver(bp, bp->bio_error);
1429 cbp = g_clone_bio(bp);
1431 if (bp->bio_error == 0)
1432 bp->bio_error = ENOMEM;
1433 g_io_deliver(bp, bp->bio_error);
1437 * Fill in the component buf structure.
1439 cp = disk->d_consumer;
1440 cbp->bio_done = g_mirror_done;
1441 cbp->bio_to = cp->provider;
1442 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1443 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1444 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1447 g_io_request(cbp, cp);
1450 #define TRACK_SIZE (1 * 1024 * 1024)
1451 #define LOAD_SCALE 256
1452 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
1455 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1457 struct g_mirror_disk *disk, *dp;
1458 struct g_consumer *cp;
1462 /* Find a disk with the smallest load. */
1465 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1466 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1469 /* If disk head is precisely in position - highly prefer it. */
1470 if (dp->d_last_offset == bp->bio_offset)
1471 prio -= 2 * LOAD_SCALE;
1473 /* If disk head is close to position - prefer it. */
1474 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1475 prio -= 1 * LOAD_SCALE;
1481 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1482 cbp = g_clone_bio(bp);
1484 if (bp->bio_error == 0)
1485 bp->bio_error = ENOMEM;
1486 g_io_deliver(bp, bp->bio_error);
1490 * Fill in the component buf structure.
1492 cp = disk->d_consumer;
1493 cbp->bio_done = g_mirror_done;
1494 cbp->bio_to = cp->provider;
1495 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1496 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1497 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1500 /* Remember last head position */
1501 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1503 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1504 dp->load = (dp->d_consumer->index * LOAD_SCALE +
1507 g_io_request(cbp, cp);
1511 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1513 struct bio_queue_head queue;
1514 struct g_mirror_disk *disk;
1515 struct g_consumer *cp;
1517 off_t left, mod, offset, slice;
1521 if (bp->bio_length <= sc->sc_slice) {
1522 g_mirror_request_round_robin(sc, bp);
1525 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1526 slice = bp->bio_length / ndisks;
1527 mod = slice % sc->sc_provider->sectorsize;
1529 slice += sc->sc_provider->sectorsize - mod;
1531 * Allocate all bios before sending any request, so we can
1532 * return ENOMEM in nice and clean way.
1534 left = bp->bio_length;
1535 offset = bp->bio_offset;
1536 data = bp->bio_data;
1538 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1539 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1541 cbp = g_clone_bio(bp);
1543 for (cbp = bioq_first(&queue); cbp != NULL;
1544 cbp = bioq_first(&queue)) {
1545 bioq_remove(&queue, cbp);
1548 if (bp->bio_error == 0)
1549 bp->bio_error = ENOMEM;
1550 g_io_deliver(bp, bp->bio_error);
1553 bioq_insert_tail(&queue, cbp);
1554 cbp->bio_done = g_mirror_done;
1555 cbp->bio_caller1 = disk;
1556 cbp->bio_to = disk->d_consumer->provider;
1557 cbp->bio_offset = offset;
1558 cbp->bio_data = data;
1559 cbp->bio_length = MIN(left, slice);
1560 left -= cbp->bio_length;
1563 offset += cbp->bio_length;
1564 data += cbp->bio_length;
1566 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1567 bioq_remove(&queue, cbp);
1568 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1569 disk = cbp->bio_caller1;
1570 cbp->bio_caller1 = NULL;
1571 cp = disk->d_consumer;
1572 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1573 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1574 cp->acr, cp->acw, cp->ace));
1575 disk->d_consumer->index++;
1576 g_io_request(cbp, disk->d_consumer);
1581 g_mirror_register_request(struct bio *bp)
1583 struct g_mirror_softc *sc;
1585 sc = bp->bio_to->geom->softc;
1586 switch (bp->bio_cmd) {
1588 switch (sc->sc_balance) {
1589 case G_MIRROR_BALANCE_LOAD:
1590 g_mirror_request_load(sc, bp);
1592 case G_MIRROR_BALANCE_PREFER:
1593 g_mirror_request_prefer(sc, bp);
1595 case G_MIRROR_BALANCE_ROUND_ROBIN:
1596 g_mirror_request_round_robin(sc, bp);
1598 case G_MIRROR_BALANCE_SPLIT:
1599 g_mirror_request_split(sc, bp);
1606 struct g_mirror_disk *disk;
1607 struct g_mirror_disk_sync *sync;
1608 struct bio_queue_head queue;
1609 struct g_consumer *cp;
1613 * Delay the request if it is colliding with a synchronization
1616 if (g_mirror_sync_collision(sc, bp)) {
1617 g_mirror_regular_delay(sc, bp);
1622 g_mirror_unidle(sc);
1624 sc->sc_last_write = time_uptime;
1627 * Allocate all bios before sending any request, so we can
1628 * return ENOMEM in nice and clean way.
1631 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1632 sync = &disk->d_sync;
1633 switch (disk->d_state) {
1634 case G_MIRROR_DISK_STATE_ACTIVE:
1636 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1637 if (bp->bio_offset >= sync->ds_offset)
1643 if (bp->bio_cmd == BIO_DELETE &&
1644 (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE) == 0)
1646 cbp = g_clone_bio(bp);
1648 for (cbp = bioq_first(&queue); cbp != NULL;
1649 cbp = bioq_first(&queue)) {
1650 bioq_remove(&queue, cbp);
1653 if (bp->bio_error == 0)
1654 bp->bio_error = ENOMEM;
1655 g_io_deliver(bp, bp->bio_error);
1658 bioq_insert_tail(&queue, cbp);
1659 cbp->bio_done = g_mirror_done;
1660 cp = disk->d_consumer;
1661 cbp->bio_caller1 = cp;
1662 cbp->bio_to = cp->provider;
1663 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1664 ("Consumer %s not opened (r%dw%de%d).",
1665 cp->provider->name, cp->acr, cp->acw, cp->ace));
1667 for (cbp = bioq_first(&queue); cbp != NULL;
1668 cbp = bioq_first(&queue)) {
1669 bioq_remove(&queue, cbp);
1670 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1671 cp = cbp->bio_caller1;
1672 cbp->bio_caller1 = NULL;
1675 g_io_request(cbp, cp);
1678 * Put request onto inflight queue, so we can check if new
1679 * synchronization requests don't collide with it.
1681 bioq_insert_tail(&sc->sc_inflight, bp);
1683 * Bump syncid on first write.
1685 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1686 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1687 g_mirror_bump_syncid(sc);
1692 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1693 bp->bio_cmd, sc->sc_name));
1699 g_mirror_can_destroy(struct g_mirror_softc *sc)
1702 struct g_consumer *cp;
1704 g_topology_assert();
1706 if (gp->softc == NULL)
1708 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_TASTING) != 0)
1710 LIST_FOREACH(cp, &gp->consumer, consumer) {
1711 if (g_mirror_is_busy(sc, cp))
1714 gp = sc->sc_sync.ds_geom;
1715 LIST_FOREACH(cp, &gp->consumer, consumer) {
1716 if (g_mirror_is_busy(sc, cp))
1719 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1725 g_mirror_try_destroy(struct g_mirror_softc *sc)
1728 if (sc->sc_rootmount != NULL) {
1729 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1731 root_mount_rel(sc->sc_rootmount);
1732 sc->sc_rootmount = NULL;
1735 if (!g_mirror_can_destroy(sc)) {
1736 g_topology_unlock();
1739 sc->sc_geom->softc = NULL;
1740 sc->sc_sync.ds_geom->softc = NULL;
1741 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WAIT) != 0) {
1742 g_topology_unlock();
1743 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1745 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
1746 sx_xunlock(&sc->sc_lock);
1747 wakeup(&sc->sc_worker);
1748 sc->sc_worker = NULL;
1750 g_topology_unlock();
1751 g_mirror_destroy_device(sc);
1761 g_mirror_worker(void *arg)
1763 struct g_mirror_softc *sc;
1764 struct g_mirror_event *ep;
1769 thread_lock(curthread);
1770 sched_prio(curthread, PRIBIO);
1771 thread_unlock(curthread);
1773 sx_xlock(&sc->sc_lock);
1775 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1777 * First take a look at events.
1778 * This is important to handle events before any I/O requests.
1780 ep = g_mirror_event_get(sc);
1782 g_mirror_event_remove(sc, ep);
1783 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1784 /* Update only device status. */
1786 "Running event for device %s.",
1789 g_mirror_update_device(sc, 1);
1791 /* Update disk status. */
1792 G_MIRROR_DEBUG(3, "Running event for disk %s.",
1793 g_mirror_get_diskname(ep->e_disk));
1794 ep->e_error = g_mirror_update_disk(ep->e_disk,
1796 if (ep->e_error == 0)
1797 g_mirror_update_device(sc, 0);
1799 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1800 KASSERT(ep->e_error == 0,
1801 ("Error cannot be handled."));
1802 g_mirror_event_free(ep);
1804 ep->e_flags |= G_MIRROR_EVENT_DONE;
1805 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1807 mtx_lock(&sc->sc_events_mtx);
1809 mtx_unlock(&sc->sc_events_mtx);
1812 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1813 if (g_mirror_try_destroy(sc)) {
1814 curthread->td_pflags &= ~TDP_GEOM;
1815 G_MIRROR_DEBUG(1, "Thread exiting.");
1819 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1823 * Check if we can mark array as CLEAN and if we can't take
1824 * how much seconds should we wait.
1826 timeout = g_mirror_idle(sc, -1);
1830 /* Get first request from the queue. */
1831 mtx_lock(&sc->sc_queue_mtx);
1832 bp = bioq_first(&sc->sc_queue);
1835 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1836 mtx_unlock(&sc->sc_queue_mtx);
1837 if (g_mirror_try_destroy(sc)) {
1838 curthread->td_pflags &= ~TDP_GEOM;
1839 G_MIRROR_DEBUG(1, "Thread exiting.");
1842 mtx_lock(&sc->sc_queue_mtx);
1844 sx_xunlock(&sc->sc_lock);
1846 * XXX: We can miss an event here, because an event
1847 * can be added without sx-device-lock and without
1848 * mtx-queue-lock. Maybe I should just stop using
1849 * dedicated mutex for events synchronization and
1850 * stick with the queue lock?
1851 * The event will hang here until next I/O request
1852 * or next event is received.
1854 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
1856 sx_xlock(&sc->sc_lock);
1857 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
1860 bioq_remove(&sc->sc_queue, bp);
1861 mtx_unlock(&sc->sc_queue_mtx);
1863 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
1864 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
1865 g_mirror_sync_request(bp); /* READ */
1866 } else if (bp->bio_to != sc->sc_provider) {
1867 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
1868 g_mirror_regular_request(bp);
1869 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
1870 g_mirror_sync_request(bp); /* WRITE */
1873 ("Invalid request cflags=0x%hhx to=%s.",
1874 bp->bio_cflags, bp->bio_to->name));
1877 g_mirror_register_request(bp);
1879 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
1884 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
1887 sx_assert(&sc->sc_lock, SX_LOCKED);
1889 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
1891 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
1892 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
1893 g_mirror_get_diskname(disk), sc->sc_name);
1894 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1895 } else if (sc->sc_idle &&
1896 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
1897 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
1898 g_mirror_get_diskname(disk), sc->sc_name);
1899 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
1904 g_mirror_sync_start(struct g_mirror_disk *disk)
1906 struct g_mirror_softc *sc;
1907 struct g_consumer *cp;
1911 g_topology_assert_not();
1913 sx_assert(&sc->sc_lock, SX_LOCKED);
1915 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1916 ("Disk %s is not marked for synchronization.",
1917 g_mirror_get_diskname(disk)));
1918 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1919 ("Device not in RUNNING state (%s, %u).", sc->sc_name,
1922 sx_xunlock(&sc->sc_lock);
1924 cp = g_new_consumer(sc->sc_sync.ds_geom);
1925 error = g_attach(cp, sc->sc_provider);
1927 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
1928 error = g_access(cp, 1, 0, 0);
1929 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
1930 g_topology_unlock();
1931 sx_xlock(&sc->sc_lock);
1933 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
1934 g_mirror_get_diskname(disk));
1935 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
1936 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1937 KASSERT(disk->d_sync.ds_consumer == NULL,
1938 ("Sync consumer already exists (device=%s, disk=%s).",
1939 sc->sc_name, g_mirror_get_diskname(disk)));
1941 disk->d_sync.ds_consumer = cp;
1942 disk->d_sync.ds_consumer->private = disk;
1943 disk->d_sync.ds_consumer->index = 0;
1946 * Allocate memory for synchronization bios and initialize them.
1948 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
1949 M_MIRROR, M_WAITOK);
1950 for (i = 0; i < g_mirror_syncreqs; i++) {
1952 disk->d_sync.ds_bios[i] = bp;
1953 bp->bio_parent = NULL;
1954 bp->bio_cmd = BIO_READ;
1955 bp->bio_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK);
1957 bp->bio_offset = disk->d_sync.ds_offset;
1958 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1959 disk->d_sync.ds_offset += bp->bio_length;
1960 bp->bio_done = g_mirror_sync_done;
1961 bp->bio_from = disk->d_sync.ds_consumer;
1962 bp->bio_to = sc->sc_provider;
1963 bp->bio_caller1 = (void *)(uintptr_t)i;
1966 /* Increase the number of disks in SYNCHRONIZING state. */
1967 sc->sc_sync.ds_ndisks++;
1968 /* Set the number of in-flight synchronization requests. */
1969 disk->d_sync.ds_inflight = g_mirror_syncreqs;
1972 * Fire off first synchronization requests.
1974 for (i = 0; i < g_mirror_syncreqs; i++) {
1975 bp = disk->d_sync.ds_bios[i];
1976 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1977 disk->d_sync.ds_consumer->index++;
1979 * Delay the request if it is colliding with a regular request.
1981 if (g_mirror_regular_collision(sc, bp))
1982 g_mirror_sync_delay(sc, bp);
1984 g_io_request(bp, disk->d_sync.ds_consumer);
1989 * Stop synchronization process.
1990 * type: 0 - synchronization finished
1991 * 1 - synchronization stopped
1994 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
1996 struct g_mirror_softc *sc;
1997 struct g_consumer *cp;
1999 g_topology_assert_not();
2001 sx_assert(&sc->sc_lock, SX_LOCKED);
2003 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2004 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2005 g_mirror_disk_state2str(disk->d_state)));
2006 if (disk->d_sync.ds_consumer == NULL)
2010 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2011 sc->sc_name, g_mirror_get_diskname(disk));
2012 } else /* if (type == 1) */ {
2013 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2014 sc->sc_name, g_mirror_get_diskname(disk));
2016 free(disk->d_sync.ds_bios, M_MIRROR);
2017 disk->d_sync.ds_bios = NULL;
2018 cp = disk->d_sync.ds_consumer;
2019 disk->d_sync.ds_consumer = NULL;
2020 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2021 sc->sc_sync.ds_ndisks--;
2022 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2024 g_mirror_kill_consumer(sc, cp);
2025 g_topology_unlock();
2026 sx_xlock(&sc->sc_lock);
2030 g_mirror_launch_provider(struct g_mirror_softc *sc)
2032 struct g_mirror_disk *disk;
2033 struct g_provider *pp, *dp;
2035 sx_assert(&sc->sc_lock, SX_LOCKED);
2038 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2039 pp->mediasize = sc->sc_mediasize;
2040 pp->sectorsize = sc->sc_sectorsize;
2042 pp->stripeoffset = 0;
2044 /* Splitting of unmapped BIO's could work but isn't implemented now */
2045 if (sc->sc_balance != G_MIRROR_BALANCE_SPLIT)
2046 pp->flags |= G_PF_ACCEPT_UNMAPPED;
2048 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2049 if (disk->d_consumer && disk->d_consumer->provider) {
2050 dp = disk->d_consumer->provider;
2051 if (dp->stripesize > pp->stripesize) {
2052 pp->stripesize = dp->stripesize;
2053 pp->stripeoffset = dp->stripeoffset;
2055 /* A provider underneath us doesn't support unmapped */
2056 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
2057 G_MIRROR_DEBUG(0, "cancelling unmapped "
2058 "because of %s\n", dp->name);
2059 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
2063 sc->sc_provider = pp;
2064 g_error_provider(pp, 0);
2065 g_topology_unlock();
2066 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2067 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2068 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2069 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2070 g_mirror_sync_start(disk);
2075 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2077 struct g_mirror_disk *disk;
2080 g_topology_assert_not();
2081 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2085 g_error_provider(sc->sc_provider, ENXIO);
2086 mtx_lock(&sc->sc_queue_mtx);
2087 while ((bp = bioq_first(&sc->sc_queue)) != NULL) {
2088 bioq_remove(&sc->sc_queue, bp);
2089 g_io_deliver(bp, ENXIO);
2091 mtx_unlock(&sc->sc_queue_mtx);
2092 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2093 sc->sc_provider->name);
2094 sc->sc_provider->flags |= G_PF_WITHER;
2095 g_orphan_provider(sc->sc_provider, ENXIO);
2096 g_topology_unlock();
2097 sc->sc_provider = NULL;
2098 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2099 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2100 g_mirror_sync_stop(disk, 1);
2105 g_mirror_go(void *arg)
2107 struct g_mirror_softc *sc;
2110 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2111 g_mirror_event_send(sc, 0,
2112 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2116 g_mirror_determine_state(struct g_mirror_disk *disk)
2118 struct g_mirror_softc *sc;
2122 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2123 if ((disk->d_flags &
2124 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2125 /* Disk does not need synchronization. */
2126 state = G_MIRROR_DISK_STATE_ACTIVE;
2129 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2131 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2133 * We can start synchronization from
2134 * the stored offset.
2136 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2138 state = G_MIRROR_DISK_STATE_STALE;
2141 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2143 * Reset all synchronization data for this disk,
2144 * because if it even was synchronized, it was
2145 * synchronized to disks with different syncid.
2147 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2148 disk->d_sync.ds_offset = 0;
2149 disk->d_sync.ds_offset_done = 0;
2150 disk->d_sync.ds_syncid = sc->sc_syncid;
2151 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2152 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2153 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2155 state = G_MIRROR_DISK_STATE_STALE;
2157 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2159 * Not good, NOT GOOD!
2160 * It means that mirror was started on stale disks
2161 * and more fresh disk just arrive.
2162 * If there were writes, mirror is broken, sorry.
2163 * I think the best choice here is don't touch
2164 * this disk and inform the user loudly.
2166 G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2167 "disk (%s) arrives!! It will not be connected to the "
2168 "running device.", sc->sc_name,
2169 g_mirror_get_diskname(disk));
2170 g_mirror_destroy_disk(disk);
2171 state = G_MIRROR_DISK_STATE_NONE;
2172 /* Return immediately, because disk was destroyed. */
2175 G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2176 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2181 * Update device state.
2184 g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force)
2186 struct g_mirror_disk *disk;
2189 sx_assert(&sc->sc_lock, SX_XLOCKED);
2191 switch (sc->sc_state) {
2192 case G_MIRROR_DEVICE_STATE_STARTING:
2194 struct g_mirror_disk *pdisk, *tdisk;
2195 u_int dirty, ndisks, genid, syncid;
2197 KASSERT(sc->sc_provider == NULL,
2198 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2200 * Are we ready? We are, if all disks are connected or
2201 * if we have any disks and 'force' is true.
2203 ndisks = g_mirror_ndisks(sc, -1);
2204 if (sc->sc_ndisks == ndisks || (force && ndisks > 0)) {
2206 } else if (ndisks == 0) {
2208 * Disks went down in starting phase, so destroy
2211 callout_drain(&sc->sc_callout);
2212 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2213 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2215 root_mount_rel(sc->sc_rootmount);
2216 sc->sc_rootmount = NULL;
2223 * Activate all disks with the biggest syncid.
2227 * If 'force' is true, we have been called due to
2228 * timeout, so don't bother canceling timeout.
2231 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2232 if ((disk->d_flags &
2233 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2238 /* No valid disks found, destroy device. */
2239 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2240 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2241 __LINE__, sc->sc_rootmount);
2242 root_mount_rel(sc->sc_rootmount);
2243 sc->sc_rootmount = NULL;
2247 /* Cancel timeout. */
2248 callout_drain(&sc->sc_callout);
2252 * Find the biggest genid.
2255 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2256 if (disk->d_genid > genid)
2257 genid = disk->d_genid;
2259 sc->sc_genid = genid;
2261 * Remove all disks without the biggest genid.
2263 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2264 if (disk->d_genid < genid) {
2266 "Component %s (device %s) broken, skipping.",
2267 g_mirror_get_diskname(disk), sc->sc_name);
2268 g_mirror_destroy_disk(disk);
2273 * Find the biggest syncid.
2276 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2277 if (disk->d_sync.ds_syncid > syncid)
2278 syncid = disk->d_sync.ds_syncid;
2282 * Here we need to look for dirty disks and if all disks
2283 * with the biggest syncid are dirty, we have to choose
2284 * one with the biggest priority and rebuild the rest.
2287 * Find the number of dirty disks with the biggest syncid.
2288 * Find the number of disks with the biggest syncid.
2289 * While here, find a disk with the biggest priority.
2293 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2294 if (disk->d_sync.ds_syncid != syncid)
2296 if ((disk->d_flags &
2297 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2301 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2303 if (pdisk == NULL ||
2304 pdisk->d_priority < disk->d_priority) {
2310 /* No dirty disks at all, great. */
2311 } else if (dirty == ndisks) {
2313 * Force synchronization for all dirty disks except one
2314 * with the biggest priority.
2316 KASSERT(pdisk != NULL, ("pdisk == NULL"));
2317 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2318 "master disk for synchronization.",
2319 g_mirror_get_diskname(pdisk), sc->sc_name);
2320 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2321 if (disk->d_sync.ds_syncid != syncid)
2323 if ((disk->d_flags &
2324 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2327 KASSERT((disk->d_flags &
2328 G_MIRROR_DISK_FLAG_DIRTY) != 0,
2329 ("Disk %s isn't marked as dirty.",
2330 g_mirror_get_diskname(disk)));
2331 /* Skip the disk with the biggest priority. */
2334 disk->d_sync.ds_syncid = 0;
2336 } else if (dirty < ndisks) {
2338 * Force synchronization for all dirty disks.
2339 * We have some non-dirty disks.
2341 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2342 if (disk->d_sync.ds_syncid != syncid)
2344 if ((disk->d_flags &
2345 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2348 if ((disk->d_flags &
2349 G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2352 disk->d_sync.ds_syncid = 0;
2358 sc->sc_syncid = syncid;
2360 /* Remember to bump syncid on first write. */
2361 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2363 state = G_MIRROR_DEVICE_STATE_RUNNING;
2364 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2365 sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2366 g_mirror_device_state2str(state));
2367 sc->sc_state = state;
2368 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2369 state = g_mirror_determine_state(disk);
2370 g_mirror_event_send(disk, state,
2371 G_MIRROR_EVENT_DONTWAIT);
2372 if (state == G_MIRROR_DISK_STATE_STALE)
2373 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2377 case G_MIRROR_DEVICE_STATE_RUNNING:
2378 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2379 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2381 * No active disks or no disks at all,
2382 * so destroy device.
2384 if (sc->sc_provider != NULL)
2385 g_mirror_destroy_provider(sc);
2386 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2388 } else if (g_mirror_ndisks(sc,
2389 G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2390 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2392 * We have active disks, launch provider if it doesn't
2395 if (sc->sc_provider == NULL)
2396 g_mirror_launch_provider(sc);
2397 if (sc->sc_rootmount != NULL) {
2398 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2399 __LINE__, sc->sc_rootmount);
2400 root_mount_rel(sc->sc_rootmount);
2401 sc->sc_rootmount = NULL;
2405 * Genid should be bumped immediately, so do it here.
2407 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2408 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2409 g_mirror_bump_genid(sc);
2413 KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2414 sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2420 * Update disk state and device state if needed.
2422 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \
2423 "Disk %s state changed from %s to %s (device %s).", \
2424 g_mirror_get_diskname(disk), \
2425 g_mirror_disk_state2str(disk->d_state), \
2426 g_mirror_disk_state2str(state), sc->sc_name)
2428 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2430 struct g_mirror_softc *sc;
2433 sx_assert(&sc->sc_lock, SX_XLOCKED);
2436 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2437 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2438 g_mirror_disk_state2str(state));
2440 case G_MIRROR_DISK_STATE_NEW:
2442 * Possible scenarios:
2443 * 1. New disk arrive.
2445 /* Previous state should be NONE. */
2446 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2447 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2448 g_mirror_disk_state2str(disk->d_state)));
2449 DISK_STATE_CHANGED();
2451 disk->d_state = state;
2452 if (LIST_EMPTY(&sc->sc_disks))
2453 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2455 struct g_mirror_disk *dp;
2457 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2458 if (disk->d_priority >= dp->d_priority) {
2459 LIST_INSERT_BEFORE(dp, disk, d_next);
2463 if (LIST_NEXT(dp, d_next) == NULL)
2467 LIST_INSERT_AFTER(dp, disk, d_next);
2469 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2470 sc->sc_name, g_mirror_get_diskname(disk));
2471 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2473 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2474 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2475 g_mirror_device_state2str(sc->sc_state),
2476 g_mirror_get_diskname(disk),
2477 g_mirror_disk_state2str(disk->d_state)));
2478 state = g_mirror_determine_state(disk);
2479 if (state != G_MIRROR_DISK_STATE_NONE)
2482 case G_MIRROR_DISK_STATE_ACTIVE:
2484 * Possible scenarios:
2485 * 1. New disk does not need synchronization.
2486 * 2. Synchronization process finished successfully.
2488 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2489 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2490 g_mirror_device_state2str(sc->sc_state),
2491 g_mirror_get_diskname(disk),
2492 g_mirror_disk_state2str(disk->d_state)));
2493 /* Previous state should be NEW or SYNCHRONIZING. */
2494 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2495 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2496 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2497 g_mirror_disk_state2str(disk->d_state)));
2498 DISK_STATE_CHANGED();
2500 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2501 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2502 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2503 g_mirror_sync_stop(disk, 0);
2505 disk->d_state = state;
2506 disk->d_sync.ds_offset = 0;
2507 disk->d_sync.ds_offset_done = 0;
2508 g_mirror_update_idle(sc, disk);
2509 g_mirror_update_metadata(disk);
2510 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2511 sc->sc_name, g_mirror_get_diskname(disk));
2513 case G_MIRROR_DISK_STATE_STALE:
2515 * Possible scenarios:
2516 * 1. Stale disk was connected.
2518 /* Previous state should be NEW. */
2519 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2520 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2521 g_mirror_disk_state2str(disk->d_state)));
2522 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2523 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2524 g_mirror_device_state2str(sc->sc_state),
2525 g_mirror_get_diskname(disk),
2526 g_mirror_disk_state2str(disk->d_state)));
2528 * STALE state is only possible if device is marked
2531 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2532 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2533 g_mirror_device_state2str(sc->sc_state),
2534 g_mirror_get_diskname(disk),
2535 g_mirror_disk_state2str(disk->d_state)));
2536 DISK_STATE_CHANGED();
2538 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2539 disk->d_state = state;
2540 g_mirror_update_metadata(disk);
2541 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2542 sc->sc_name, g_mirror_get_diskname(disk));
2544 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2546 * Possible scenarios:
2547 * 1. Disk which needs synchronization was connected.
2549 /* Previous state should be NEW. */
2550 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2551 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2552 g_mirror_disk_state2str(disk->d_state)));
2553 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2554 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2555 g_mirror_device_state2str(sc->sc_state),
2556 g_mirror_get_diskname(disk),
2557 g_mirror_disk_state2str(disk->d_state)));
2558 DISK_STATE_CHANGED();
2560 if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2561 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2562 disk->d_state = state;
2563 if (sc->sc_provider != NULL) {
2564 g_mirror_sync_start(disk);
2565 g_mirror_update_metadata(disk);
2568 case G_MIRROR_DISK_STATE_DISCONNECTED:
2570 * Possible scenarios:
2571 * 1. Device wasn't running yet, but disk disappear.
2572 * 2. Disk was active and disapppear.
2573 * 3. Disk disappear during synchronization process.
2575 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2577 * Previous state should be ACTIVE, STALE or
2580 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2581 disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2582 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2583 ("Wrong disk state (%s, %s).",
2584 g_mirror_get_diskname(disk),
2585 g_mirror_disk_state2str(disk->d_state)));
2586 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2587 /* Previous state should be NEW. */
2588 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2589 ("Wrong disk state (%s, %s).",
2590 g_mirror_get_diskname(disk),
2591 g_mirror_disk_state2str(disk->d_state)));
2593 * Reset bumping syncid if disk disappeared in STARTING
2596 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2597 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2600 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2602 g_mirror_device_state2str(sc->sc_state),
2603 g_mirror_get_diskname(disk),
2604 g_mirror_disk_state2str(disk->d_state)));
2607 DISK_STATE_CHANGED();
2608 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2609 sc->sc_name, g_mirror_get_diskname(disk));
2611 g_mirror_destroy_disk(disk);
2613 case G_MIRROR_DISK_STATE_DESTROY:
2617 error = g_mirror_clear_metadata(disk);
2620 DISK_STATE_CHANGED();
2621 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2622 sc->sc_name, g_mirror_get_diskname(disk));
2624 g_mirror_destroy_disk(disk);
2626 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2627 g_mirror_update_metadata(disk);
2632 KASSERT(1 == 0, ("Unknown state (%u).", state));
2637 #undef DISK_STATE_CHANGED
2640 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2642 struct g_provider *pp;
2646 g_topology_assert();
2648 error = g_access(cp, 1, 0, 0);
2652 g_topology_unlock();
2653 /* Metadata are stored on last sector. */
2654 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2657 g_access(cp, -1, 0, 0);
2659 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2660 cp->provider->name, error);
2664 /* Decode metadata. */
2665 error = mirror_metadata_decode(buf, md);
2667 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2669 if (md->md_version > G_MIRROR_VERSION) {
2671 "Kernel module is too old to handle metadata from %s.",
2672 cp->provider->name);
2676 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2677 cp->provider->name);
2685 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2686 struct g_mirror_metadata *md)
2689 if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2690 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2691 pp->name, md->md_did);
2694 if (md->md_all != sc->sc_ndisks) {
2696 "Invalid '%s' field on disk %s (device %s), skipping.",
2697 "md_all", pp->name, sc->sc_name);
2700 if (md->md_slice != sc->sc_slice) {
2702 "Invalid '%s' field on disk %s (device %s), skipping.",
2703 "md_slice", pp->name, sc->sc_name);
2706 if (md->md_balance != sc->sc_balance) {
2708 "Invalid '%s' field on disk %s (device %s), skipping.",
2709 "md_balance", pp->name, sc->sc_name);
2712 if (md->md_mediasize != sc->sc_mediasize) {
2714 "Invalid '%s' field on disk %s (device %s), skipping.",
2715 "md_mediasize", pp->name, sc->sc_name);
2718 if (sc->sc_mediasize > pp->mediasize) {
2720 "Invalid size of disk %s (device %s), skipping.", pp->name,
2724 if (md->md_sectorsize != sc->sc_sectorsize) {
2726 "Invalid '%s' field on disk %s (device %s), skipping.",
2727 "md_sectorsize", pp->name, sc->sc_name);
2730 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2732 "Invalid sector size of disk %s (device %s), skipping.",
2733 pp->name, sc->sc_name);
2736 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2738 "Invalid device flags on disk %s (device %s), skipping.",
2739 pp->name, sc->sc_name);
2742 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2744 "Invalid disk flags on disk %s (device %s), skipping.",
2745 pp->name, sc->sc_name);
2752 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2753 struct g_mirror_metadata *md)
2755 struct g_mirror_disk *disk;
2758 g_topology_assert_not();
2759 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2761 error = g_mirror_check_metadata(sc, pp, md);
2764 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING &&
2765 md->md_genid < sc->sc_genid) {
2766 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
2767 pp->name, sc->sc_name);
2770 disk = g_mirror_init_disk(sc, pp, md, &error);
2773 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
2774 G_MIRROR_EVENT_WAIT);
2777 if (md->md_version < G_MIRROR_VERSION) {
2778 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
2779 pp->name, md->md_version, G_MIRROR_VERSION);
2780 g_mirror_update_metadata(disk);
2786 g_mirror_destroy_delayed(void *arg, int flag)
2788 struct g_mirror_softc *sc;
2791 if (flag == EV_CANCEL) {
2792 G_MIRROR_DEBUG(1, "Destroying canceled.");
2796 g_topology_unlock();
2797 sx_xlock(&sc->sc_lock);
2798 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
2799 ("DESTROY flag set on %s.", sc->sc_name));
2800 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0,
2801 ("DESTROYING flag not set on %s.", sc->sc_name));
2802 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
2803 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
2805 G_MIRROR_DEBUG(0, "Cannot destroy %s.", sc->sc_name);
2806 sx_xunlock(&sc->sc_lock);
2812 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
2814 struct g_mirror_softc *sc;
2815 int dcr, dcw, dce, error = 0;
2817 g_topology_assert();
2818 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2821 sc = pp->geom->softc;
2822 if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
2824 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
2826 dcr = pp->acr + acr;
2827 dcw = pp->acw + acw;
2828 dce = pp->ace + ace;
2830 g_topology_unlock();
2831 sx_xlock(&sc->sc_lock);
2832 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
2833 LIST_EMPTY(&sc->sc_disks)) {
2834 if (acr > 0 || acw > 0 || ace > 0)
2839 g_mirror_idle(sc, dcw);
2840 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0) {
2841 if (acr > 0 || acw > 0 || ace > 0) {
2845 if (dcr == 0 && dcw == 0 && dce == 0) {
2846 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK,
2851 sx_xunlock(&sc->sc_lock);
2856 static struct g_geom *
2857 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
2859 struct g_mirror_softc *sc;
2863 g_topology_assert();
2864 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
2867 /* One disk is minimum. */
2873 gp = g_new_geomf(mp, "%s", md->md_name);
2874 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
2875 gp->start = g_mirror_start;
2876 gp->orphan = g_mirror_orphan;
2877 gp->access = g_mirror_access;
2878 gp->dumpconf = g_mirror_dumpconf;
2880 sc->sc_id = md->md_mid;
2881 sc->sc_slice = md->md_slice;
2882 sc->sc_balance = md->md_balance;
2883 sc->sc_mediasize = md->md_mediasize;
2884 sc->sc_sectorsize = md->md_sectorsize;
2885 sc->sc_ndisks = md->md_all;
2886 sc->sc_flags = md->md_mflags;
2889 sc->sc_last_write = time_uptime;
2891 sx_init(&sc->sc_lock, "gmirror:lock");
2892 bioq_init(&sc->sc_queue);
2893 mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
2894 bioq_init(&sc->sc_regular_delayed);
2895 bioq_init(&sc->sc_inflight);
2896 bioq_init(&sc->sc_sync_delayed);
2897 LIST_INIT(&sc->sc_disks);
2898 TAILQ_INIT(&sc->sc_events);
2899 mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
2900 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
2901 sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
2904 sc->sc_provider = NULL;
2906 * Synchronization geom.
2908 gp = g_new_geomf(mp, "%s.sync", md->md_name);
2910 gp->orphan = g_mirror_orphan;
2911 sc->sc_sync.ds_geom = gp;
2912 sc->sc_sync.ds_ndisks = 0;
2913 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
2914 "g_mirror %s", md->md_name);
2916 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
2918 g_destroy_geom(sc->sc_sync.ds_geom);
2919 mtx_destroy(&sc->sc_events_mtx);
2920 mtx_destroy(&sc->sc_queue_mtx);
2921 sx_destroy(&sc->sc_lock);
2922 g_destroy_geom(sc->sc_geom);
2927 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
2928 sc->sc_name, sc->sc_ndisks, sc->sc_id);
2930 sc->sc_rootmount = root_mount_hold("GMIRROR");
2931 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
2935 timeout = g_mirror_timeout * hz;
2936 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
2937 return (sc->sc_geom);
2941 g_mirror_destroy(struct g_mirror_softc *sc, int how)
2943 struct g_mirror_disk *disk;
2944 struct g_provider *pp;
2946 g_topology_assert_not();
2949 sx_assert(&sc->sc_lock, SX_XLOCKED);
2951 pp = sc->sc_provider;
2952 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
2954 case G_MIRROR_DESTROY_SOFT:
2956 "Device %s is still open (r%dw%de%d).", pp->name,
2957 pp->acr, pp->acw, pp->ace);
2959 case G_MIRROR_DESTROY_DELAYED:
2961 "Device %s will be destroyed on last close.",
2963 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2964 if (disk->d_state ==
2965 G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2966 g_mirror_sync_stop(disk, 1);
2969 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROYING;
2971 case G_MIRROR_DESTROY_HARD:
2972 G_MIRROR_DEBUG(1, "Device %s is still open, so it "
2973 "can't be definitely removed.", pp->name);
2978 if (sc->sc_geom->softc == NULL) {
2979 g_topology_unlock();
2982 sc->sc_geom->softc = NULL;
2983 sc->sc_sync.ds_geom->softc = NULL;
2984 g_topology_unlock();
2986 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2987 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WAIT;
2988 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
2989 sx_xunlock(&sc->sc_lock);
2990 mtx_lock(&sc->sc_queue_mtx);
2992 mtx_unlock(&sc->sc_queue_mtx);
2993 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
2994 while (sc->sc_worker != NULL)
2995 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
2996 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
2997 sx_xlock(&sc->sc_lock);
2998 g_mirror_destroy_device(sc);
3004 g_mirror_taste_orphan(struct g_consumer *cp)
3007 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3008 cp->provider->name));
3011 static struct g_geom *
3012 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3014 struct g_mirror_metadata md;
3015 struct g_mirror_softc *sc;
3016 struct g_consumer *cp;
3020 g_topology_assert();
3021 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3022 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
3024 gp = g_new_geomf(mp, "mirror:taste");
3026 * This orphan function should be never called.
3028 gp->orphan = g_mirror_taste_orphan;
3029 cp = g_new_consumer(gp);
3031 error = g_mirror_read_metadata(cp, &md);
3033 g_destroy_consumer(cp);
3039 if (md.md_provider[0] != '\0' &&
3040 !g_compare_names(md.md_provider, pp->name))
3042 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3044 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3046 "Device %s: provider %s marked as inactive, skipping.",
3047 md.md_name, pp->name);
3050 if (g_mirror_debug >= 2)
3051 mirror_metadata_dump(&md);
3054 * Let's check if device already exists.
3057 LIST_FOREACH(gp, &mp->geom, geom) {
3061 if (sc->sc_sync.ds_geom == gp)
3063 if (strcmp(md.md_name, sc->sc_name) != 0)
3065 if (md.md_mid != sc->sc_id) {
3066 G_MIRROR_DEBUG(0, "Device %s already configured.",
3073 gp = g_mirror_create(mp, &md);
3075 G_MIRROR_DEBUG(0, "Cannot create device %s.",
3081 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3082 g_topology_unlock();
3083 sx_xlock(&sc->sc_lock);
3084 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
3085 error = g_mirror_add_disk(sc, pp, &md);
3087 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3088 pp->name, gp->name, error);
3089 if (LIST_EMPTY(&sc->sc_disks)) {
3091 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3097 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
3098 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3099 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3103 sx_xunlock(&sc->sc_lock);
3109 g_mirror_destroy_geom(struct gctl_req *req __unused,
3110 struct g_class *mp __unused, struct g_geom *gp)
3112 struct g_mirror_softc *sc;
3115 g_topology_unlock();
3117 sx_xlock(&sc->sc_lock);
3119 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3121 sx_xunlock(&sc->sc_lock);
3127 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3128 struct g_consumer *cp, struct g_provider *pp)
3130 struct g_mirror_softc *sc;
3132 g_topology_assert();
3137 /* Skip synchronization geom. */
3138 if (gp == sc->sc_sync.ds_geom)
3142 } else if (cp != NULL) {
3143 struct g_mirror_disk *disk;
3148 g_topology_unlock();
3149 sx_xlock(&sc->sc_lock);
3150 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3151 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3152 sbuf_printf(sb, "%s<Synchronized>", indent);
3153 if (disk->d_sync.ds_offset == 0)
3154 sbuf_printf(sb, "0%%");
3156 sbuf_printf(sb, "%u%%",
3157 (u_int)((disk->d_sync.ds_offset * 100) /
3158 sc->sc_provider->mediasize));
3160 sbuf_printf(sb, "</Synchronized>\n");
3161 if (disk->d_sync.ds_offset > 0) {
3162 sbuf_printf(sb, "%s<BytesSynced>%jd"
3163 "</BytesSynced>\n", indent,
3164 (intmax_t)disk->d_sync.ds_offset);
3167 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3168 disk->d_sync.ds_syncid);
3169 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3171 sbuf_printf(sb, "%s<Flags>", indent);
3172 if (disk->d_flags == 0)
3173 sbuf_printf(sb, "NONE");
3177 #define ADD_FLAG(flag, name) do { \
3178 if ((disk->d_flags & (flag)) != 0) { \
3180 sbuf_printf(sb, ", "); \
3183 sbuf_printf(sb, name); \
3186 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3187 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3188 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3189 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3191 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3192 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3195 sbuf_printf(sb, "</Flags>\n");
3196 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3198 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3199 g_mirror_disk_state2str(disk->d_state));
3200 sx_xunlock(&sc->sc_lock);
3203 g_topology_unlock();
3204 sx_xlock(&sc->sc_lock);
3205 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3206 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3207 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3208 sbuf_printf(sb, "%s<Flags>", indent);
3209 if (sc->sc_flags == 0)
3210 sbuf_printf(sb, "NONE");
3214 #define ADD_FLAG(flag, name) do { \
3215 if ((sc->sc_flags & (flag)) != 0) { \
3217 sbuf_printf(sb, ", "); \
3220 sbuf_printf(sb, name); \
3223 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3224 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3227 sbuf_printf(sb, "</Flags>\n");
3228 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3229 (u_int)sc->sc_slice);
3230 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3231 balance_name(sc->sc_balance));
3232 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3234 sbuf_printf(sb, "%s<State>", indent);
3235 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3236 sbuf_printf(sb, "%s", "STARTING");
3237 else if (sc->sc_ndisks ==
3238 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3239 sbuf_printf(sb, "%s", "COMPLETE");
3241 sbuf_printf(sb, "%s", "DEGRADED");
3242 sbuf_printf(sb, "</State>\n");
3243 sx_xunlock(&sc->sc_lock);
3249 g_mirror_shutdown_post_sync(void *arg, int howto)
3252 struct g_geom *gp, *gp2;
3253 struct g_mirror_softc *sc;
3259 g_mirror_shutdown = 1;
3260 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3261 if ((sc = gp->softc) == NULL)
3263 /* Skip synchronization geom. */
3264 if (gp == sc->sc_sync.ds_geom)
3266 g_topology_unlock();
3267 sx_xlock(&sc->sc_lock);
3268 g_mirror_idle(sc, -1);
3270 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3272 sx_xunlock(&sc->sc_lock);
3275 g_topology_unlock();
3280 g_mirror_init(struct g_class *mp)
3283 g_mirror_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
3284 g_mirror_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
3285 if (g_mirror_post_sync == NULL)
3286 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3290 g_mirror_fini(struct g_class *mp)
3293 if (g_mirror_post_sync != NULL)
3294 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_mirror_post_sync);
3297 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);