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 g_resize_t g_mirror_resize;
91 static void g_mirror_init(struct g_class *mp);
92 static void g_mirror_fini(struct g_class *mp);
94 struct g_class g_mirror_class = {
95 .name = G_MIRROR_CLASS_NAME,
97 .ctlreq = g_mirror_config,
98 .taste = g_mirror_taste,
99 .destroy_geom = g_mirror_destroy_geom,
100 .init = g_mirror_init,
101 .fini = g_mirror_fini,
102 .resize = g_mirror_resize
106 static void g_mirror_destroy_provider(struct g_mirror_softc *sc);
107 static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
108 static void g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force);
109 static void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
110 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
111 static void g_mirror_sync_stop(struct g_mirror_disk *disk, int type);
112 static void g_mirror_register_request(struct bio *bp);
113 static void g_mirror_sync_release(struct g_mirror_softc *sc);
117 g_mirror_disk_state2str(int state)
121 case G_MIRROR_DISK_STATE_NONE:
123 case G_MIRROR_DISK_STATE_NEW:
125 case G_MIRROR_DISK_STATE_ACTIVE:
127 case G_MIRROR_DISK_STATE_STALE:
129 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
130 return ("SYNCHRONIZING");
131 case G_MIRROR_DISK_STATE_DISCONNECTED:
132 return ("DISCONNECTED");
133 case G_MIRROR_DISK_STATE_DESTROY:
141 g_mirror_device_state2str(int state)
145 case G_MIRROR_DEVICE_STATE_STARTING:
147 case G_MIRROR_DEVICE_STATE_RUNNING:
155 g_mirror_get_diskname(struct g_mirror_disk *disk)
158 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
159 return ("[unknown]");
160 return (disk->d_name);
164 * --- Events handling functions ---
165 * Events in geom_mirror are used to maintain disks and device status
166 * from one thread to simplify locking.
169 g_mirror_event_free(struct g_mirror_event *ep)
176 g_mirror_event_send(void *arg, int state, int flags)
178 struct g_mirror_softc *sc;
179 struct g_mirror_disk *disk;
180 struct g_mirror_event *ep;
183 ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
184 G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
185 if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
196 mtx_lock(&sc->sc_events_mtx);
197 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
198 mtx_unlock(&sc->sc_events_mtx);
199 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
200 mtx_lock(&sc->sc_queue_mtx);
202 mtx_unlock(&sc->sc_queue_mtx);
203 if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0)
205 sx_assert(&sc->sc_lock, SX_XLOCKED);
206 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
207 sx_xunlock(&sc->sc_lock);
208 while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) {
209 mtx_lock(&sc->sc_events_mtx);
210 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event",
214 g_mirror_event_free(ep);
215 sx_xlock(&sc->sc_lock);
219 static struct g_mirror_event *
220 g_mirror_event_get(struct g_mirror_softc *sc)
222 struct g_mirror_event *ep;
224 mtx_lock(&sc->sc_events_mtx);
225 ep = TAILQ_FIRST(&sc->sc_events);
226 mtx_unlock(&sc->sc_events_mtx);
231 g_mirror_event_remove(struct g_mirror_softc *sc, struct g_mirror_event *ep)
234 mtx_lock(&sc->sc_events_mtx);
235 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
236 mtx_unlock(&sc->sc_events_mtx);
240 g_mirror_event_cancel(struct g_mirror_disk *disk)
242 struct g_mirror_softc *sc;
243 struct g_mirror_event *ep, *tmpep;
246 sx_assert(&sc->sc_lock, SX_XLOCKED);
248 mtx_lock(&sc->sc_events_mtx);
249 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
250 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0)
252 if (ep->e_disk != disk)
254 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
255 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
256 g_mirror_event_free(ep);
258 ep->e_error = ECANCELED;
262 mtx_unlock(&sc->sc_events_mtx);
266 * Return the number of disks in given state.
267 * If state is equal to -1, count all connected disks.
270 g_mirror_ndisks(struct g_mirror_softc *sc, int state)
272 struct g_mirror_disk *disk;
275 sx_assert(&sc->sc_lock, SX_LOCKED);
277 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
278 if (state == -1 || disk->d_state == state)
285 * Find a disk in mirror by its disk ID.
287 static struct g_mirror_disk *
288 g_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id)
290 struct g_mirror_disk *disk;
292 sx_assert(&sc->sc_lock, SX_XLOCKED);
294 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
295 if (disk->d_id == id)
302 g_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp)
307 mtx_lock(&sc->sc_queue_mtx);
308 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
309 if (bp->bio_from == cp)
312 mtx_unlock(&sc->sc_queue_mtx);
317 g_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp)
322 "I/O requests for %s exist, can't destroy it now.",
326 if (g_mirror_nrequests(sc, cp) > 0) {
328 "I/O requests for %s in queue, can't destroy it now.",
336 g_mirror_destroy_consumer(void *arg, int flags __unused)
338 struct g_consumer *cp;
343 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
345 g_destroy_consumer(cp);
349 g_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
351 struct g_provider *pp;
357 if (g_mirror_is_busy(sc, cp))
362 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
365 G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
366 -cp->acw, -cp->ace, 0);
367 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
368 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
371 * After retaste event was send (inside g_access()), we can send
372 * event to detach and destroy consumer.
373 * A class, which has consumer to the given provider connected
374 * will not receive retaste event for the provider.
375 * This is the way how I ignore retaste events when I close
376 * consumers opened for write: I detach and destroy consumer
377 * after retaste event is sent.
379 g_post_event(g_mirror_destroy_consumer, cp, M_WAITOK, NULL);
382 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", pp->name);
384 g_destroy_consumer(cp);
388 g_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp)
390 struct g_consumer *cp;
393 g_topology_assert_not();
394 KASSERT(disk->d_consumer == NULL,
395 ("Disk already connected (device %s).", disk->d_softc->sc_name));
398 cp = g_new_consumer(disk->d_softc->sc_geom);
399 cp->flags |= G_CF_DIRECT_RECEIVE;
400 error = g_attach(cp, pp);
402 g_destroy_consumer(cp);
406 error = g_access(cp, 1, 1, 1);
409 g_destroy_consumer(cp);
411 G_MIRROR_DEBUG(0, "Cannot open consumer %s (error=%d).",
416 disk->d_consumer = cp;
417 disk->d_consumer->private = disk;
418 disk->d_consumer->index = 0;
420 G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk));
425 g_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
432 if (cp->provider != NULL)
433 g_mirror_kill_consumer(sc, cp);
435 g_destroy_consumer(cp);
439 * Initialize disk. This means allocate memory, create consumer, attach it
440 * to the provider and open access (r1w1e1) to it.
442 static struct g_mirror_disk *
443 g_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp,
444 struct g_mirror_metadata *md, int *errorp)
446 struct g_mirror_disk *disk;
449 disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO);
455 error = g_mirror_connect_disk(disk, pp);
458 disk->d_id = md->md_did;
459 disk->d_state = G_MIRROR_DISK_STATE_NONE;
460 disk->d_priority = md->md_priority;
461 disk->d_flags = md->md_dflags;
462 error = g_getattr("GEOM::candelete", disk->d_consumer, &i);
463 if (error == 0 && i != 0)
464 disk->d_flags |= G_MIRROR_DISK_FLAG_CANDELETE;
465 if (md->md_provider[0] != '\0')
466 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
467 disk->d_sync.ds_consumer = NULL;
468 disk->d_sync.ds_offset = md->md_sync_offset;
469 disk->d_sync.ds_offset_done = md->md_sync_offset;
470 disk->d_genid = md->md_genid;
471 disk->d_sync.ds_syncid = md->md_syncid;
479 free(disk, M_MIRROR);
484 g_mirror_destroy_disk(struct g_mirror_disk *disk)
486 struct g_mirror_softc *sc;
488 g_topology_assert_not();
490 sx_assert(&sc->sc_lock, SX_XLOCKED);
492 LIST_REMOVE(disk, d_next);
493 g_mirror_event_cancel(disk);
494 if (sc->sc_hint == disk)
496 switch (disk->d_state) {
497 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
498 g_mirror_sync_stop(disk, 1);
500 case G_MIRROR_DISK_STATE_NEW:
501 case G_MIRROR_DISK_STATE_STALE:
502 case G_MIRROR_DISK_STATE_ACTIVE:
504 g_mirror_disconnect_consumer(sc, disk->d_consumer);
506 free(disk, M_MIRROR);
509 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
510 g_mirror_get_diskname(disk),
511 g_mirror_disk_state2str(disk->d_state)));
516 g_mirror_destroy_device(struct g_mirror_softc *sc)
518 struct g_mirror_disk *disk;
519 struct g_mirror_event *ep;
521 struct g_consumer *cp, *tmpcp;
523 g_topology_assert_not();
524 sx_assert(&sc->sc_lock, SX_XLOCKED);
527 if (sc->sc_provider != NULL)
528 g_mirror_destroy_provider(sc);
529 for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL;
530 disk = LIST_FIRST(&sc->sc_disks)) {
531 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
532 g_mirror_update_metadata(disk);
533 g_mirror_destroy_disk(disk);
535 while ((ep = g_mirror_event_get(sc)) != NULL) {
536 g_mirror_event_remove(sc, ep);
537 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
538 g_mirror_event_free(ep);
540 ep->e_error = ECANCELED;
541 ep->e_flags |= G_MIRROR_EVENT_DONE;
542 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep);
543 mtx_lock(&sc->sc_events_mtx);
545 mtx_unlock(&sc->sc_events_mtx);
548 callout_drain(&sc->sc_callout);
551 LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
552 g_mirror_disconnect_consumer(sc, cp);
554 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
555 G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
556 g_wither_geom(gp, ENXIO);
558 mtx_destroy(&sc->sc_queue_mtx);
559 mtx_destroy(&sc->sc_events_mtx);
560 mtx_destroy(&sc->sc_done_mtx);
561 sx_xunlock(&sc->sc_lock);
562 sx_destroy(&sc->sc_lock);
566 g_mirror_orphan(struct g_consumer *cp)
568 struct g_mirror_disk *disk;
575 disk->d_softc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
576 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
577 G_MIRROR_EVENT_DONTWAIT);
581 * Function should return the next active disk on the list.
582 * It is possible that it will be the same disk as given.
583 * If there are no active disks on list, NULL is returned.
585 static __inline struct g_mirror_disk *
586 g_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
588 struct g_mirror_disk *dp;
590 for (dp = LIST_NEXT(disk, d_next); dp != disk;
591 dp = LIST_NEXT(dp, d_next)) {
593 dp = LIST_FIRST(&sc->sc_disks);
594 if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE)
597 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
602 static struct g_mirror_disk *
603 g_mirror_get_disk(struct g_mirror_softc *sc)
605 struct g_mirror_disk *disk;
607 if (sc->sc_hint == NULL) {
608 sc->sc_hint = LIST_FIRST(&sc->sc_disks);
609 if (sc->sc_hint == NULL)
613 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) {
614 disk = g_mirror_find_next(sc, disk);
618 sc->sc_hint = g_mirror_find_next(sc, disk);
623 g_mirror_write_metadata(struct g_mirror_disk *disk,
624 struct g_mirror_metadata *md)
626 struct g_mirror_softc *sc;
627 struct g_consumer *cp;
628 off_t offset, length;
632 g_topology_assert_not();
634 sx_assert(&sc->sc_lock, SX_LOCKED);
636 cp = disk->d_consumer;
637 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
638 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
639 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
640 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
642 length = cp->provider->sectorsize;
643 offset = cp->provider->mediasize - length;
644 sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO);
647 * Handle the case, when the size of parent provider reduced.
649 if (offset < md->md_mediasize)
652 mirror_metadata_encode(md, sector);
655 error = g_write_data(cp, offset, sector, length);
656 free(sector, M_MIRROR);
658 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
659 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
660 G_MIRROR_DEBUG(0, "Cannot write metadata on %s "
661 "(device=%s, error=%d).",
662 g_mirror_get_diskname(disk), sc->sc_name, error);
664 G_MIRROR_DEBUG(1, "Cannot write metadata on %s "
665 "(device=%s, error=%d).",
666 g_mirror_get_diskname(disk), sc->sc_name, error);
668 if (g_mirror_disconnect_on_failure &&
669 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) {
670 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
671 g_mirror_event_send(disk,
672 G_MIRROR_DISK_STATE_DISCONNECTED,
673 G_MIRROR_EVENT_DONTWAIT);
680 g_mirror_clear_metadata(struct g_mirror_disk *disk)
684 g_topology_assert_not();
685 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
687 error = g_mirror_write_metadata(disk, NULL);
689 G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
690 g_mirror_get_diskname(disk));
693 "Cannot clear metadata on disk %s (error=%d).",
694 g_mirror_get_diskname(disk), error);
700 g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
701 struct g_mirror_metadata *md)
704 strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
705 md->md_version = G_MIRROR_VERSION;
706 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
707 md->md_mid = sc->sc_id;
708 md->md_all = sc->sc_ndisks;
709 md->md_slice = sc->sc_slice;
710 md->md_balance = sc->sc_balance;
711 md->md_genid = sc->sc_genid;
712 md->md_mediasize = sc->sc_mediasize;
713 md->md_sectorsize = sc->sc_sectorsize;
714 md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
715 bzero(md->md_provider, sizeof(md->md_provider));
717 md->md_did = arc4random();
721 md->md_sync_offset = 0;
724 md->md_did = disk->d_id;
725 md->md_priority = disk->d_priority;
726 md->md_syncid = disk->d_sync.ds_syncid;
727 md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
728 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
729 md->md_sync_offset = disk->d_sync.ds_offset_done;
731 md->md_sync_offset = 0;
732 if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
733 strlcpy(md->md_provider,
734 disk->d_consumer->provider->name,
735 sizeof(md->md_provider));
737 md->md_provsize = disk->d_consumer->provider->mediasize;
742 g_mirror_update_metadata(struct g_mirror_disk *disk)
744 struct g_mirror_softc *sc;
745 struct g_mirror_metadata md;
748 g_topology_assert_not();
750 sx_assert(&sc->sc_lock, SX_LOCKED);
752 g_mirror_fill_metadata(sc, disk, &md);
753 error = g_mirror_write_metadata(disk, &md);
755 G_MIRROR_DEBUG(2, "Metadata on %s updated.",
756 g_mirror_get_diskname(disk));
759 "Cannot update metadata on disk %s (error=%d).",
760 g_mirror_get_diskname(disk), error);
765 g_mirror_bump_syncid(struct g_mirror_softc *sc)
767 struct g_mirror_disk *disk;
769 g_topology_assert_not();
770 sx_assert(&sc->sc_lock, SX_XLOCKED);
771 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
772 ("%s called with no active disks (device=%s).", __func__,
776 G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
778 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
779 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
780 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
781 disk->d_sync.ds_syncid = sc->sc_syncid;
782 g_mirror_update_metadata(disk);
788 g_mirror_bump_genid(struct g_mirror_softc *sc)
790 struct g_mirror_disk *disk;
792 g_topology_assert_not();
793 sx_assert(&sc->sc_lock, SX_XLOCKED);
794 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
795 ("%s called with no active disks (device=%s).", __func__,
799 G_MIRROR_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
801 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
802 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
803 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
804 disk->d_genid = sc->sc_genid;
805 g_mirror_update_metadata(disk);
811 g_mirror_idle(struct g_mirror_softc *sc, int acw)
813 struct g_mirror_disk *disk;
816 g_topology_assert_not();
817 sx_assert(&sc->sc_lock, SX_XLOCKED);
819 if (sc->sc_provider == NULL)
821 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
825 if (sc->sc_writes > 0)
827 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
828 timeout = g_mirror_idletime - (time_uptime - sc->sc_last_write);
829 if (!g_mirror_shutdown && timeout > 0)
833 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
834 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
836 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
837 g_mirror_get_diskname(disk), sc->sc_name);
838 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
839 g_mirror_update_metadata(disk);
845 g_mirror_unidle(struct g_mirror_softc *sc)
847 struct g_mirror_disk *disk;
849 g_topology_assert_not();
850 sx_assert(&sc->sc_lock, SX_XLOCKED);
852 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
855 sc->sc_last_write = time_uptime;
856 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
857 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
859 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
860 g_mirror_get_diskname(disk), sc->sc_name);
861 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
862 g_mirror_update_metadata(disk);
867 g_mirror_flush_done(struct bio *bp)
869 struct g_mirror_softc *sc;
872 pbp = bp->bio_parent;
873 sc = pbp->bio_to->geom->softc;
874 mtx_lock(&sc->sc_done_mtx);
875 if (pbp->bio_error == 0)
876 pbp->bio_error = bp->bio_error;
877 pbp->bio_completed += bp->bio_completed;
879 if (pbp->bio_children == pbp->bio_inbed) {
880 mtx_unlock(&sc->sc_done_mtx);
881 g_io_deliver(pbp, pbp->bio_error);
883 mtx_unlock(&sc->sc_done_mtx);
888 g_mirror_done(struct bio *bp)
890 struct g_mirror_softc *sc;
892 sc = bp->bio_from->geom->softc;
893 bp->bio_cflags = G_MIRROR_BIO_FLAG_REGULAR;
894 mtx_lock(&sc->sc_queue_mtx);
895 bioq_disksort(&sc->sc_queue, bp);
896 mtx_unlock(&sc->sc_queue_mtx);
901 g_mirror_regular_request(struct bio *bp)
903 struct g_mirror_softc *sc;
904 struct g_mirror_disk *disk;
907 g_topology_assert_not();
909 pbp = bp->bio_parent;
910 sc = pbp->bio_to->geom->softc;
911 bp->bio_from->index--;
912 if (bp->bio_cmd == BIO_WRITE)
914 disk = bp->bio_from->private;
917 g_mirror_kill_consumer(sc, bp->bio_from);
922 KASSERT(pbp->bio_inbed <= pbp->bio_children,
923 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
925 if (bp->bio_error == 0 && pbp->bio_error == 0) {
926 G_MIRROR_LOGREQ(3, bp, "Request delivered.");
928 if (pbp->bio_children == pbp->bio_inbed) {
929 G_MIRROR_LOGREQ(3, pbp, "Request delivered.");
930 pbp->bio_completed = pbp->bio_length;
931 if (pbp->bio_cmd == BIO_WRITE ||
932 pbp->bio_cmd == BIO_DELETE) {
933 bioq_remove(&sc->sc_inflight, pbp);
934 /* Release delayed sync requests if possible. */
935 g_mirror_sync_release(sc);
937 g_io_deliver(pbp, pbp->bio_error);
940 } else if (bp->bio_error != 0) {
941 if (pbp->bio_error == 0)
942 pbp->bio_error = bp->bio_error;
944 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
945 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
946 G_MIRROR_LOGREQ(0, bp,
947 "Request failed (error=%d).",
950 G_MIRROR_LOGREQ(1, bp,
951 "Request failed (error=%d).",
954 if (g_mirror_disconnect_on_failure &&
955 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1)
957 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
958 g_mirror_event_send(disk,
959 G_MIRROR_DISK_STATE_DISCONNECTED,
960 G_MIRROR_EVENT_DONTWAIT);
963 switch (pbp->bio_cmd) {
973 switch (pbp->bio_cmd) {
975 if (pbp->bio_inbed < pbp->bio_children)
977 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1)
978 g_io_deliver(pbp, pbp->bio_error);
981 mtx_lock(&sc->sc_queue_mtx);
982 bioq_disksort(&sc->sc_queue, pbp);
983 mtx_unlock(&sc->sc_queue_mtx);
984 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
990 if (pbp->bio_children == 0) {
992 * All requests failed.
994 } else if (pbp->bio_inbed < pbp->bio_children) {
997 } else if (pbp->bio_children == pbp->bio_inbed) {
998 /* Some requests succeeded. */
1000 pbp->bio_completed = pbp->bio_length;
1002 bioq_remove(&sc->sc_inflight, pbp);
1003 /* Release delayed sync requests if possible. */
1004 g_mirror_sync_release(sc);
1005 g_io_deliver(pbp, pbp->bio_error);
1008 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
1014 g_mirror_sync_done(struct bio *bp)
1016 struct g_mirror_softc *sc;
1018 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
1019 sc = bp->bio_from->geom->softc;
1020 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
1021 mtx_lock(&sc->sc_queue_mtx);
1022 bioq_disksort(&sc->sc_queue, bp);
1023 mtx_unlock(&sc->sc_queue_mtx);
1028 g_mirror_kernel_dump(struct bio *bp)
1030 struct g_mirror_softc *sc;
1031 struct g_mirror_disk *disk;
1033 struct g_kerneldump *gkd;
1036 * We configure dumping to the first component, because this component
1037 * will be used for reading with 'prefer' balance algorithm.
1038 * If the component with the higest priority is currently disconnected
1039 * we will not be able to read the dump after the reboot if it will be
1040 * connected and synchronized later. Can we do something better?
1042 sc = bp->bio_to->geom->softc;
1043 disk = LIST_FIRST(&sc->sc_disks);
1045 gkd = (struct g_kerneldump *)bp->bio_data;
1046 if (gkd->length > bp->bio_to->mediasize)
1047 gkd->length = bp->bio_to->mediasize;
1048 cbp = g_clone_bio(bp);
1050 g_io_deliver(bp, ENOMEM);
1053 cbp->bio_done = g_std_done;
1054 g_io_request(cbp, disk->d_consumer);
1055 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1056 g_mirror_get_diskname(disk));
1060 g_mirror_flush(struct g_mirror_softc *sc, struct bio *bp)
1062 struct bio_queue_head queue;
1063 struct g_mirror_disk *disk;
1064 struct g_consumer *cp;
1068 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1069 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1071 cbp = g_clone_bio(bp);
1073 while ((cbp = bioq_takefirst(&queue)) != NULL)
1075 if (bp->bio_error == 0)
1076 bp->bio_error = ENOMEM;
1077 g_io_deliver(bp, bp->bio_error);
1080 bioq_insert_tail(&queue, cbp);
1081 cbp->bio_done = g_mirror_flush_done;
1082 cbp->bio_caller1 = disk;
1083 cbp->bio_to = disk->d_consumer->provider;
1085 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1086 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1087 disk = cbp->bio_caller1;
1088 cbp->bio_caller1 = NULL;
1089 cp = disk->d_consumer;
1090 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1091 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1092 cp->acr, cp->acw, cp->ace));
1093 g_io_request(cbp, disk->d_consumer);
1098 g_mirror_start(struct bio *bp)
1100 struct g_mirror_softc *sc;
1102 sc = bp->bio_to->geom->softc;
1104 * If sc == NULL or there are no valid disks, provider's error
1105 * should be set and g_mirror_start() should not be called at all.
1107 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1108 ("Provider's error should be set (error=%d)(mirror=%s).",
1109 bp->bio_to->error, bp->bio_to->name));
1110 G_MIRROR_LOGREQ(3, bp, "Request received.");
1112 switch (bp->bio_cmd) {
1118 g_mirror_flush(sc, bp);
1121 if (g_handleattr_int(bp, "GEOM::candelete", 1))
1123 else if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1124 g_mirror_kernel_dump(bp);
1129 g_io_deliver(bp, EOPNOTSUPP);
1132 mtx_lock(&sc->sc_queue_mtx);
1133 bioq_disksort(&sc->sc_queue, bp);
1134 mtx_unlock(&sc->sc_queue_mtx);
1135 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1140 * Return TRUE if the given request is colliding with a in-progress
1141 * synchronization request.
1144 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1146 struct g_mirror_disk *disk;
1148 off_t rstart, rend, sstart, send;
1151 if (sc->sc_sync.ds_ndisks == 0)
1153 rstart = bp->bio_offset;
1154 rend = bp->bio_offset + bp->bio_length;
1155 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1156 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1158 for (i = 0; i < g_mirror_syncreqs; i++) {
1159 sbp = disk->d_sync.ds_bios[i];
1162 sstart = sbp->bio_offset;
1163 send = sbp->bio_offset + sbp->bio_length;
1164 if (rend > sstart && rstart < send)
1172 * Return TRUE if the given sync request is colliding with a in-progress regular
1176 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1178 off_t rstart, rend, sstart, send;
1181 if (sc->sc_sync.ds_ndisks == 0)
1183 sstart = sbp->bio_offset;
1184 send = sbp->bio_offset + sbp->bio_length;
1185 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1186 rstart = bp->bio_offset;
1187 rend = bp->bio_offset + bp->bio_length;
1188 if (rend > sstart && rstart < send)
1195 * Puts request onto delayed queue.
1198 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1201 G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1202 bioq_insert_head(&sc->sc_regular_delayed, bp);
1206 * Puts synchronization request onto delayed queue.
1209 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1212 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1213 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1217 * Releases delayed regular requests which don't collide anymore with sync
1221 g_mirror_regular_release(struct g_mirror_softc *sc)
1223 struct bio *bp, *bp2;
1225 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1226 if (g_mirror_sync_collision(sc, bp))
1228 bioq_remove(&sc->sc_regular_delayed, bp);
1229 G_MIRROR_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1230 mtx_lock(&sc->sc_queue_mtx);
1231 bioq_insert_head(&sc->sc_queue, bp);
1234 * wakeup() is not needed, because this function is called from
1235 * the worker thread.
1237 wakeup(&sc->sc_queue);
1239 mtx_unlock(&sc->sc_queue_mtx);
1244 * Releases delayed sync requests which don't collide anymore with regular
1248 g_mirror_sync_release(struct g_mirror_softc *sc)
1250 struct bio *bp, *bp2;
1252 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1253 if (g_mirror_regular_collision(sc, bp))
1255 bioq_remove(&sc->sc_sync_delayed, bp);
1256 G_MIRROR_LOGREQ(2, bp,
1257 "Releasing delayed synchronization request.");
1258 g_io_request(bp, bp->bio_from);
1263 * Handle synchronization requests.
1264 * Every synchronization request is two-steps process: first, READ request is
1265 * send to active provider and then WRITE request (with read data) to the provider
1266 * beeing synchronized. When WRITE is finished, new synchronization request is
1270 g_mirror_sync_request(struct bio *bp)
1272 struct g_mirror_softc *sc;
1273 struct g_mirror_disk *disk;
1275 bp->bio_from->index--;
1276 sc = bp->bio_from->geom->softc;
1277 disk = bp->bio_from->private;
1279 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1281 g_mirror_kill_consumer(sc, bp->bio_from);
1282 g_topology_unlock();
1283 free(bp->bio_data, M_MIRROR);
1285 sx_xlock(&sc->sc_lock);
1290 * Synchronization request.
1292 switch (bp->bio_cmd) {
1295 struct g_consumer *cp;
1297 if (bp->bio_error != 0) {
1298 G_MIRROR_LOGREQ(0, bp,
1299 "Synchronization request failed (error=%d).",
1304 G_MIRROR_LOGREQ(3, bp,
1305 "Synchronization request half-finished.");
1306 bp->bio_cmd = BIO_WRITE;
1308 cp = disk->d_consumer;
1309 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1310 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1311 cp->acr, cp->acw, cp->ace));
1313 g_io_request(bp, cp);
1318 struct g_mirror_disk_sync *sync;
1323 if (bp->bio_error != 0) {
1324 G_MIRROR_LOGREQ(0, bp,
1325 "Synchronization request failed (error=%d).",
1328 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1329 g_mirror_event_send(disk,
1330 G_MIRROR_DISK_STATE_DISCONNECTED,
1331 G_MIRROR_EVENT_DONTWAIT);
1334 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1335 sync = &disk->d_sync;
1336 if (sync->ds_offset >= sc->sc_mediasize ||
1337 sync->ds_consumer == NULL ||
1338 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1339 /* Don't send more synchronization requests. */
1340 sync->ds_inflight--;
1341 if (sync->ds_bios != NULL) {
1342 i = (int)(uintptr_t)bp->bio_caller1;
1343 sync->ds_bios[i] = NULL;
1345 free(bp->bio_data, M_MIRROR);
1347 if (sync->ds_inflight > 0)
1349 if (sync->ds_consumer == NULL ||
1350 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1353 /* Disk up-to-date, activate it. */
1354 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1355 G_MIRROR_EVENT_DONTWAIT);
1359 /* Send next synchronization request. */
1360 data = bp->bio_data;
1361 bzero(bp, sizeof(*bp));
1362 bp->bio_cmd = BIO_READ;
1363 bp->bio_offset = sync->ds_offset;
1364 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1365 sync->ds_offset += bp->bio_length;
1366 bp->bio_done = g_mirror_sync_done;
1367 bp->bio_data = data;
1368 bp->bio_from = sync->ds_consumer;
1369 bp->bio_to = sc->sc_provider;
1370 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1371 sync->ds_consumer->index++;
1373 * Delay the request if it is colliding with a regular request.
1375 if (g_mirror_regular_collision(sc, bp))
1376 g_mirror_sync_delay(sc, bp);
1378 g_io_request(bp, sync->ds_consumer);
1380 /* Release delayed requests if possible. */
1381 g_mirror_regular_release(sc);
1383 /* Find the smallest offset */
1384 offset = sc->sc_mediasize;
1385 for (i = 0; i < g_mirror_syncreqs; i++) {
1386 bp = sync->ds_bios[i];
1387 if (bp->bio_offset < offset)
1388 offset = bp->bio_offset;
1390 if (sync->ds_offset_done + (MAXPHYS * 100) < offset) {
1391 /* Update offset_done on every 100 blocks. */
1392 sync->ds_offset_done = offset;
1393 g_mirror_update_metadata(disk);
1398 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1399 bp->bio_cmd, sc->sc_name));
1405 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1407 struct g_mirror_disk *disk;
1408 struct g_consumer *cp;
1411 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1412 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1416 if (bp->bio_error == 0)
1417 bp->bio_error = ENXIO;
1418 g_io_deliver(bp, bp->bio_error);
1421 cbp = g_clone_bio(bp);
1423 if (bp->bio_error == 0)
1424 bp->bio_error = ENOMEM;
1425 g_io_deliver(bp, bp->bio_error);
1429 * Fill in the component buf structure.
1431 cp = disk->d_consumer;
1432 cbp->bio_done = g_mirror_done;
1433 cbp->bio_to = cp->provider;
1434 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1435 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1436 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1439 g_io_request(cbp, cp);
1443 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1445 struct g_mirror_disk *disk;
1446 struct g_consumer *cp;
1449 disk = g_mirror_get_disk(sc);
1451 if (bp->bio_error == 0)
1452 bp->bio_error = ENXIO;
1453 g_io_deliver(bp, bp->bio_error);
1456 cbp = g_clone_bio(bp);
1458 if (bp->bio_error == 0)
1459 bp->bio_error = ENOMEM;
1460 g_io_deliver(bp, bp->bio_error);
1464 * Fill in the component buf structure.
1466 cp = disk->d_consumer;
1467 cbp->bio_done = g_mirror_done;
1468 cbp->bio_to = cp->provider;
1469 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1470 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1471 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1474 g_io_request(cbp, cp);
1477 #define TRACK_SIZE (1 * 1024 * 1024)
1478 #define LOAD_SCALE 256
1479 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
1482 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1484 struct g_mirror_disk *disk, *dp;
1485 struct g_consumer *cp;
1489 /* Find a disk with the smallest load. */
1492 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1493 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1496 /* If disk head is precisely in position - highly prefer it. */
1497 if (dp->d_last_offset == bp->bio_offset)
1498 prio -= 2 * LOAD_SCALE;
1500 /* If disk head is close to position - prefer it. */
1501 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1502 prio -= 1 * LOAD_SCALE;
1508 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1509 cbp = g_clone_bio(bp);
1511 if (bp->bio_error == 0)
1512 bp->bio_error = ENOMEM;
1513 g_io_deliver(bp, bp->bio_error);
1517 * Fill in the component buf structure.
1519 cp = disk->d_consumer;
1520 cbp->bio_done = g_mirror_done;
1521 cbp->bio_to = cp->provider;
1522 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1523 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1524 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1527 /* Remember last head position */
1528 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1530 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1531 dp->load = (dp->d_consumer->index * LOAD_SCALE +
1534 g_io_request(cbp, cp);
1538 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1540 struct bio_queue_head queue;
1541 struct g_mirror_disk *disk;
1542 struct g_consumer *cp;
1544 off_t left, mod, offset, slice;
1548 if (bp->bio_length <= sc->sc_slice) {
1549 g_mirror_request_round_robin(sc, bp);
1552 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1553 slice = bp->bio_length / ndisks;
1554 mod = slice % sc->sc_provider->sectorsize;
1556 slice += sc->sc_provider->sectorsize - mod;
1558 * Allocate all bios before sending any request, so we can
1559 * return ENOMEM in nice and clean way.
1561 left = bp->bio_length;
1562 offset = bp->bio_offset;
1563 data = bp->bio_data;
1565 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1566 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1568 cbp = g_clone_bio(bp);
1570 while ((cbp = bioq_takefirst(&queue)) != NULL)
1571 bioq_remove(&queue, cbp);
1572 if (bp->bio_error == 0)
1573 bp->bio_error = ENOMEM;
1574 g_io_deliver(bp, bp->bio_error);
1577 bioq_insert_tail(&queue, cbp);
1578 cbp->bio_done = g_mirror_done;
1579 cbp->bio_caller1 = disk;
1580 cbp->bio_to = disk->d_consumer->provider;
1581 cbp->bio_offset = offset;
1582 cbp->bio_data = data;
1583 cbp->bio_length = MIN(left, slice);
1584 left -= cbp->bio_length;
1587 offset += cbp->bio_length;
1588 data += cbp->bio_length;
1590 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1591 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1592 disk = cbp->bio_caller1;
1593 cbp->bio_caller1 = NULL;
1594 cp = disk->d_consumer;
1595 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1596 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1597 cp->acr, cp->acw, cp->ace));
1598 disk->d_consumer->index++;
1599 g_io_request(cbp, disk->d_consumer);
1604 g_mirror_register_request(struct bio *bp)
1606 struct g_mirror_softc *sc;
1608 sc = bp->bio_to->geom->softc;
1609 switch (bp->bio_cmd) {
1611 switch (sc->sc_balance) {
1612 case G_MIRROR_BALANCE_LOAD:
1613 g_mirror_request_load(sc, bp);
1615 case G_MIRROR_BALANCE_PREFER:
1616 g_mirror_request_prefer(sc, bp);
1618 case G_MIRROR_BALANCE_ROUND_ROBIN:
1619 g_mirror_request_round_robin(sc, bp);
1621 case G_MIRROR_BALANCE_SPLIT:
1622 g_mirror_request_split(sc, bp);
1629 struct g_mirror_disk *disk;
1630 struct g_mirror_disk_sync *sync;
1631 struct bio_queue_head queue;
1632 struct g_consumer *cp;
1636 * Delay the request if it is colliding with a synchronization
1639 if (g_mirror_sync_collision(sc, bp)) {
1640 g_mirror_regular_delay(sc, bp);
1645 g_mirror_unidle(sc);
1647 sc->sc_last_write = time_uptime;
1650 * Allocate all bios before sending any request, so we can
1651 * return ENOMEM in nice and clean way.
1654 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1655 sync = &disk->d_sync;
1656 switch (disk->d_state) {
1657 case G_MIRROR_DISK_STATE_ACTIVE:
1659 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1660 if (bp->bio_offset >= sync->ds_offset)
1666 if (bp->bio_cmd == BIO_DELETE &&
1667 (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE) == 0)
1669 cbp = g_clone_bio(bp);
1671 while ((cbp = bioq_takefirst(&queue)) != NULL)
1673 if (bp->bio_error == 0)
1674 bp->bio_error = ENOMEM;
1675 g_io_deliver(bp, bp->bio_error);
1678 bioq_insert_tail(&queue, cbp);
1679 cbp->bio_done = g_mirror_done;
1680 cp = disk->d_consumer;
1681 cbp->bio_caller1 = cp;
1682 cbp->bio_to = cp->provider;
1683 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1684 ("Consumer %s not opened (r%dw%de%d).",
1685 cp->provider->name, cp->acr, cp->acw, cp->ace));
1687 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1688 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1689 cp = cbp->bio_caller1;
1690 cbp->bio_caller1 = NULL;
1693 g_io_request(cbp, cp);
1696 * Put request onto inflight queue, so we can check if new
1697 * synchronization requests don't collide with it.
1699 bioq_insert_tail(&sc->sc_inflight, bp);
1701 * Bump syncid on first write.
1703 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1704 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1705 g_mirror_bump_syncid(sc);
1710 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1711 bp->bio_cmd, sc->sc_name));
1717 g_mirror_can_destroy(struct g_mirror_softc *sc)
1720 struct g_consumer *cp;
1722 g_topology_assert();
1724 if (gp->softc == NULL)
1726 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_TASTING) != 0)
1728 LIST_FOREACH(cp, &gp->consumer, consumer) {
1729 if (g_mirror_is_busy(sc, cp))
1732 gp = sc->sc_sync.ds_geom;
1733 LIST_FOREACH(cp, &gp->consumer, consumer) {
1734 if (g_mirror_is_busy(sc, cp))
1737 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1743 g_mirror_try_destroy(struct g_mirror_softc *sc)
1746 if (sc->sc_rootmount != NULL) {
1747 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1749 root_mount_rel(sc->sc_rootmount);
1750 sc->sc_rootmount = NULL;
1753 if (!g_mirror_can_destroy(sc)) {
1754 g_topology_unlock();
1757 sc->sc_geom->softc = NULL;
1758 sc->sc_sync.ds_geom->softc = NULL;
1759 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WAIT) != 0) {
1760 g_topology_unlock();
1761 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1763 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
1764 sx_xunlock(&sc->sc_lock);
1765 wakeup(&sc->sc_worker);
1766 sc->sc_worker = NULL;
1768 g_topology_unlock();
1769 g_mirror_destroy_device(sc);
1779 g_mirror_worker(void *arg)
1781 struct g_mirror_softc *sc;
1782 struct g_mirror_event *ep;
1787 thread_lock(curthread);
1788 sched_prio(curthread, PRIBIO);
1789 thread_unlock(curthread);
1791 sx_xlock(&sc->sc_lock);
1793 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1795 * First take a look at events.
1796 * This is important to handle events before any I/O requests.
1798 ep = g_mirror_event_get(sc);
1800 g_mirror_event_remove(sc, ep);
1801 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1802 /* Update only device status. */
1804 "Running event for device %s.",
1807 g_mirror_update_device(sc, 1);
1809 /* Update disk status. */
1810 G_MIRROR_DEBUG(3, "Running event for disk %s.",
1811 g_mirror_get_diskname(ep->e_disk));
1812 ep->e_error = g_mirror_update_disk(ep->e_disk,
1814 if (ep->e_error == 0)
1815 g_mirror_update_device(sc, 0);
1817 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1818 KASSERT(ep->e_error == 0,
1819 ("Error cannot be handled."));
1820 g_mirror_event_free(ep);
1822 ep->e_flags |= G_MIRROR_EVENT_DONE;
1823 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1825 mtx_lock(&sc->sc_events_mtx);
1827 mtx_unlock(&sc->sc_events_mtx);
1830 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1831 if (g_mirror_try_destroy(sc)) {
1832 curthread->td_pflags &= ~TDP_GEOM;
1833 G_MIRROR_DEBUG(1, "Thread exiting.");
1837 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1841 * Check if we can mark array as CLEAN and if we can't take
1842 * how much seconds should we wait.
1844 timeout = g_mirror_idle(sc, -1);
1848 /* Get first request from the queue. */
1849 mtx_lock(&sc->sc_queue_mtx);
1850 bp = bioq_first(&sc->sc_queue);
1853 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1854 mtx_unlock(&sc->sc_queue_mtx);
1855 if (g_mirror_try_destroy(sc)) {
1856 curthread->td_pflags &= ~TDP_GEOM;
1857 G_MIRROR_DEBUG(1, "Thread exiting.");
1860 mtx_lock(&sc->sc_queue_mtx);
1862 sx_xunlock(&sc->sc_lock);
1864 * XXX: We can miss an event here, because an event
1865 * can be added without sx-device-lock and without
1866 * mtx-queue-lock. Maybe I should just stop using
1867 * dedicated mutex for events synchronization and
1868 * stick with the queue lock?
1869 * The event will hang here until next I/O request
1870 * or next event is received.
1872 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
1874 sx_xlock(&sc->sc_lock);
1875 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
1878 bioq_remove(&sc->sc_queue, bp);
1879 mtx_unlock(&sc->sc_queue_mtx);
1881 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
1882 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
1883 g_mirror_sync_request(bp); /* READ */
1884 } else if (bp->bio_to != sc->sc_provider) {
1885 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
1886 g_mirror_regular_request(bp);
1887 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
1888 g_mirror_sync_request(bp); /* WRITE */
1891 ("Invalid request cflags=0x%hhx to=%s.",
1892 bp->bio_cflags, bp->bio_to->name));
1895 g_mirror_register_request(bp);
1897 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
1902 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
1905 sx_assert(&sc->sc_lock, SX_LOCKED);
1907 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
1909 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
1910 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
1911 g_mirror_get_diskname(disk), sc->sc_name);
1912 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1913 } else if (sc->sc_idle &&
1914 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
1915 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
1916 g_mirror_get_diskname(disk), sc->sc_name);
1917 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
1922 g_mirror_sync_start(struct g_mirror_disk *disk)
1924 struct g_mirror_softc *sc;
1925 struct g_consumer *cp;
1929 g_topology_assert_not();
1931 sx_assert(&sc->sc_lock, SX_LOCKED);
1933 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1934 ("Disk %s is not marked for synchronization.",
1935 g_mirror_get_diskname(disk)));
1936 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1937 ("Device not in RUNNING state (%s, %u).", sc->sc_name,
1940 sx_xunlock(&sc->sc_lock);
1942 cp = g_new_consumer(sc->sc_sync.ds_geom);
1943 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1944 error = g_attach(cp, sc->sc_provider);
1946 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
1947 error = g_access(cp, 1, 0, 0);
1948 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
1949 g_topology_unlock();
1950 sx_xlock(&sc->sc_lock);
1952 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
1953 g_mirror_get_diskname(disk));
1954 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
1955 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1956 KASSERT(disk->d_sync.ds_consumer == NULL,
1957 ("Sync consumer already exists (device=%s, disk=%s).",
1958 sc->sc_name, g_mirror_get_diskname(disk)));
1960 disk->d_sync.ds_consumer = cp;
1961 disk->d_sync.ds_consumer->private = disk;
1962 disk->d_sync.ds_consumer->index = 0;
1965 * Allocate memory for synchronization bios and initialize them.
1967 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
1968 M_MIRROR, M_WAITOK);
1969 for (i = 0; i < g_mirror_syncreqs; i++) {
1971 disk->d_sync.ds_bios[i] = bp;
1972 bp->bio_parent = NULL;
1973 bp->bio_cmd = BIO_READ;
1974 bp->bio_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK);
1976 bp->bio_offset = disk->d_sync.ds_offset;
1977 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1978 disk->d_sync.ds_offset += bp->bio_length;
1979 bp->bio_done = g_mirror_sync_done;
1980 bp->bio_from = disk->d_sync.ds_consumer;
1981 bp->bio_to = sc->sc_provider;
1982 bp->bio_caller1 = (void *)(uintptr_t)i;
1985 /* Increase the number of disks in SYNCHRONIZING state. */
1986 sc->sc_sync.ds_ndisks++;
1987 /* Set the number of in-flight synchronization requests. */
1988 disk->d_sync.ds_inflight = g_mirror_syncreqs;
1991 * Fire off first synchronization requests.
1993 for (i = 0; i < g_mirror_syncreqs; i++) {
1994 bp = disk->d_sync.ds_bios[i];
1995 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1996 disk->d_sync.ds_consumer->index++;
1998 * Delay the request if it is colliding with a regular request.
2000 if (g_mirror_regular_collision(sc, bp))
2001 g_mirror_sync_delay(sc, bp);
2003 g_io_request(bp, disk->d_sync.ds_consumer);
2008 * Stop synchronization process.
2009 * type: 0 - synchronization finished
2010 * 1 - synchronization stopped
2013 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
2015 struct g_mirror_softc *sc;
2016 struct g_consumer *cp;
2018 g_topology_assert_not();
2020 sx_assert(&sc->sc_lock, SX_LOCKED);
2022 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2023 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2024 g_mirror_disk_state2str(disk->d_state)));
2025 if (disk->d_sync.ds_consumer == NULL)
2029 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2030 sc->sc_name, g_mirror_get_diskname(disk));
2031 } else /* if (type == 1) */ {
2032 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2033 sc->sc_name, g_mirror_get_diskname(disk));
2035 free(disk->d_sync.ds_bios, M_MIRROR);
2036 disk->d_sync.ds_bios = NULL;
2037 cp = disk->d_sync.ds_consumer;
2038 disk->d_sync.ds_consumer = NULL;
2039 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2040 sc->sc_sync.ds_ndisks--;
2041 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2043 g_mirror_kill_consumer(sc, cp);
2044 g_topology_unlock();
2045 sx_xlock(&sc->sc_lock);
2049 g_mirror_launch_provider(struct g_mirror_softc *sc)
2051 struct g_mirror_disk *disk;
2052 struct g_provider *pp, *dp;
2054 sx_assert(&sc->sc_lock, SX_LOCKED);
2057 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2058 pp->flags |= G_PF_DIRECT_RECEIVE;
2059 pp->mediasize = sc->sc_mediasize;
2060 pp->sectorsize = sc->sc_sectorsize;
2062 pp->stripeoffset = 0;
2064 /* Splitting of unmapped BIO's could work but isn't implemented now */
2065 if (sc->sc_balance != G_MIRROR_BALANCE_SPLIT)
2066 pp->flags |= G_PF_ACCEPT_UNMAPPED;
2068 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2069 if (disk->d_consumer && disk->d_consumer->provider) {
2070 dp = disk->d_consumer->provider;
2071 if (dp->stripesize > pp->stripesize) {
2072 pp->stripesize = dp->stripesize;
2073 pp->stripeoffset = dp->stripeoffset;
2075 /* A provider underneath us doesn't support unmapped */
2076 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
2077 G_MIRROR_DEBUG(0, "Cancelling unmapped "
2078 "because of %s.", dp->name);
2079 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
2083 sc->sc_provider = pp;
2084 g_error_provider(pp, 0);
2085 g_topology_unlock();
2086 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2087 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2088 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2089 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2090 g_mirror_sync_start(disk);
2095 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2097 struct g_mirror_disk *disk;
2100 g_topology_assert_not();
2101 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2105 g_error_provider(sc->sc_provider, ENXIO);
2106 mtx_lock(&sc->sc_queue_mtx);
2107 while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL)
2108 g_io_deliver(bp, ENXIO);
2109 mtx_unlock(&sc->sc_queue_mtx);
2110 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2111 sc->sc_provider->name);
2112 sc->sc_provider->flags |= G_PF_WITHER;
2113 g_orphan_provider(sc->sc_provider, ENXIO);
2114 g_topology_unlock();
2115 sc->sc_provider = NULL;
2116 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2117 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2118 g_mirror_sync_stop(disk, 1);
2123 g_mirror_go(void *arg)
2125 struct g_mirror_softc *sc;
2128 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2129 g_mirror_event_send(sc, 0,
2130 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2134 g_mirror_determine_state(struct g_mirror_disk *disk)
2136 struct g_mirror_softc *sc;
2140 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2141 if ((disk->d_flags &
2142 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2143 /* Disk does not need synchronization. */
2144 state = G_MIRROR_DISK_STATE_ACTIVE;
2147 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2149 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2151 * We can start synchronization from
2152 * the stored offset.
2154 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2156 state = G_MIRROR_DISK_STATE_STALE;
2159 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2161 * Reset all synchronization data for this disk,
2162 * because if it even was synchronized, it was
2163 * synchronized to disks with different syncid.
2165 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2166 disk->d_sync.ds_offset = 0;
2167 disk->d_sync.ds_offset_done = 0;
2168 disk->d_sync.ds_syncid = sc->sc_syncid;
2169 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2170 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2171 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2173 state = G_MIRROR_DISK_STATE_STALE;
2175 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2177 * Not good, NOT GOOD!
2178 * It means that mirror was started on stale disks
2179 * and more fresh disk just arrive.
2180 * If there were writes, mirror is broken, sorry.
2181 * I think the best choice here is don't touch
2182 * this disk and inform the user loudly.
2184 G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2185 "disk (%s) arrives!! It will not be connected to the "
2186 "running device.", sc->sc_name,
2187 g_mirror_get_diskname(disk));
2188 g_mirror_destroy_disk(disk);
2189 state = G_MIRROR_DISK_STATE_NONE;
2190 /* Return immediately, because disk was destroyed. */
2193 G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2194 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2199 * Update device state.
2202 g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force)
2204 struct g_mirror_disk *disk;
2207 sx_assert(&sc->sc_lock, SX_XLOCKED);
2209 switch (sc->sc_state) {
2210 case G_MIRROR_DEVICE_STATE_STARTING:
2212 struct g_mirror_disk *pdisk, *tdisk;
2213 u_int dirty, ndisks, genid, syncid;
2215 KASSERT(sc->sc_provider == NULL,
2216 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2218 * Are we ready? We are, if all disks are connected or
2219 * if we have any disks and 'force' is true.
2221 ndisks = g_mirror_ndisks(sc, -1);
2222 if (sc->sc_ndisks == ndisks || (force && ndisks > 0)) {
2224 } else if (ndisks == 0) {
2226 * Disks went down in starting phase, so destroy
2229 callout_drain(&sc->sc_callout);
2230 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2231 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2233 root_mount_rel(sc->sc_rootmount);
2234 sc->sc_rootmount = NULL;
2241 * Activate all disks with the biggest syncid.
2245 * If 'force' is true, we have been called due to
2246 * timeout, so don't bother canceling timeout.
2249 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2250 if ((disk->d_flags &
2251 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2256 /* No valid disks found, destroy device. */
2257 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2258 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2259 __LINE__, sc->sc_rootmount);
2260 root_mount_rel(sc->sc_rootmount);
2261 sc->sc_rootmount = NULL;
2265 /* Cancel timeout. */
2266 callout_drain(&sc->sc_callout);
2270 * Find the biggest genid.
2273 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2274 if (disk->d_genid > genid)
2275 genid = disk->d_genid;
2277 sc->sc_genid = genid;
2279 * Remove all disks without the biggest genid.
2281 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2282 if (disk->d_genid < genid) {
2284 "Component %s (device %s) broken, skipping.",
2285 g_mirror_get_diskname(disk), sc->sc_name);
2286 g_mirror_destroy_disk(disk);
2291 * Find the biggest syncid.
2294 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2295 if (disk->d_sync.ds_syncid > syncid)
2296 syncid = disk->d_sync.ds_syncid;
2300 * Here we need to look for dirty disks and if all disks
2301 * with the biggest syncid are dirty, we have to choose
2302 * one with the biggest priority and rebuild the rest.
2305 * Find the number of dirty disks with the biggest syncid.
2306 * Find the number of disks with the biggest syncid.
2307 * While here, find a disk with the biggest priority.
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) {
2319 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2321 if (pdisk == NULL ||
2322 pdisk->d_priority < disk->d_priority) {
2328 /* No dirty disks at all, great. */
2329 } else if (dirty == ndisks) {
2331 * Force synchronization for all dirty disks except one
2332 * with the biggest priority.
2334 KASSERT(pdisk != NULL, ("pdisk == NULL"));
2335 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2336 "master disk for synchronization.",
2337 g_mirror_get_diskname(pdisk), sc->sc_name);
2338 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2339 if (disk->d_sync.ds_syncid != syncid)
2341 if ((disk->d_flags &
2342 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2345 KASSERT((disk->d_flags &
2346 G_MIRROR_DISK_FLAG_DIRTY) != 0,
2347 ("Disk %s isn't marked as dirty.",
2348 g_mirror_get_diskname(disk)));
2349 /* Skip the disk with the biggest priority. */
2352 disk->d_sync.ds_syncid = 0;
2354 } else if (dirty < ndisks) {
2356 * Force synchronization for all dirty disks.
2357 * We have some non-dirty disks.
2359 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2360 if (disk->d_sync.ds_syncid != syncid)
2362 if ((disk->d_flags &
2363 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2366 if ((disk->d_flags &
2367 G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2370 disk->d_sync.ds_syncid = 0;
2376 sc->sc_syncid = syncid;
2378 /* Remember to bump syncid on first write. */
2379 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2381 state = G_MIRROR_DEVICE_STATE_RUNNING;
2382 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2383 sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2384 g_mirror_device_state2str(state));
2385 sc->sc_state = state;
2386 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2387 state = g_mirror_determine_state(disk);
2388 g_mirror_event_send(disk, state,
2389 G_MIRROR_EVENT_DONTWAIT);
2390 if (state == G_MIRROR_DISK_STATE_STALE)
2391 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2395 case G_MIRROR_DEVICE_STATE_RUNNING:
2396 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2397 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2399 * No active disks or no disks at all,
2400 * so destroy device.
2402 if (sc->sc_provider != NULL)
2403 g_mirror_destroy_provider(sc);
2404 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2406 } else if (g_mirror_ndisks(sc,
2407 G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2408 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2410 * We have active disks, launch provider if it doesn't
2413 if (sc->sc_provider == NULL)
2414 g_mirror_launch_provider(sc);
2415 if (sc->sc_rootmount != NULL) {
2416 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2417 __LINE__, sc->sc_rootmount);
2418 root_mount_rel(sc->sc_rootmount);
2419 sc->sc_rootmount = NULL;
2423 * Genid should be bumped immediately, so do it here.
2425 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2426 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2427 g_mirror_bump_genid(sc);
2431 KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2432 sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2438 * Update disk state and device state if needed.
2440 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \
2441 "Disk %s state changed from %s to %s (device %s).", \
2442 g_mirror_get_diskname(disk), \
2443 g_mirror_disk_state2str(disk->d_state), \
2444 g_mirror_disk_state2str(state), sc->sc_name)
2446 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2448 struct g_mirror_softc *sc;
2451 sx_assert(&sc->sc_lock, SX_XLOCKED);
2454 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2455 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2456 g_mirror_disk_state2str(state));
2458 case G_MIRROR_DISK_STATE_NEW:
2460 * Possible scenarios:
2461 * 1. New disk arrive.
2463 /* Previous state should be NONE. */
2464 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2465 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2466 g_mirror_disk_state2str(disk->d_state)));
2467 DISK_STATE_CHANGED();
2469 disk->d_state = state;
2470 if (LIST_EMPTY(&sc->sc_disks))
2471 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2473 struct g_mirror_disk *dp;
2475 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2476 if (disk->d_priority >= dp->d_priority) {
2477 LIST_INSERT_BEFORE(dp, disk, d_next);
2481 if (LIST_NEXT(dp, d_next) == NULL)
2485 LIST_INSERT_AFTER(dp, disk, d_next);
2487 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2488 sc->sc_name, g_mirror_get_diskname(disk));
2489 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2491 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2492 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2493 g_mirror_device_state2str(sc->sc_state),
2494 g_mirror_get_diskname(disk),
2495 g_mirror_disk_state2str(disk->d_state)));
2496 state = g_mirror_determine_state(disk);
2497 if (state != G_MIRROR_DISK_STATE_NONE)
2500 case G_MIRROR_DISK_STATE_ACTIVE:
2502 * Possible scenarios:
2503 * 1. New disk does not need synchronization.
2504 * 2. Synchronization process finished successfully.
2506 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2507 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2508 g_mirror_device_state2str(sc->sc_state),
2509 g_mirror_get_diskname(disk),
2510 g_mirror_disk_state2str(disk->d_state)));
2511 /* Previous state should be NEW or SYNCHRONIZING. */
2512 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2513 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2514 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2515 g_mirror_disk_state2str(disk->d_state)));
2516 DISK_STATE_CHANGED();
2518 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2519 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2520 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2521 g_mirror_sync_stop(disk, 0);
2523 disk->d_state = state;
2524 disk->d_sync.ds_offset = 0;
2525 disk->d_sync.ds_offset_done = 0;
2526 g_mirror_update_idle(sc, disk);
2527 g_mirror_update_metadata(disk);
2528 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2529 sc->sc_name, g_mirror_get_diskname(disk));
2531 case G_MIRROR_DISK_STATE_STALE:
2533 * Possible scenarios:
2534 * 1. Stale disk was connected.
2536 /* Previous state should be NEW. */
2537 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2538 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2539 g_mirror_disk_state2str(disk->d_state)));
2540 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2541 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2542 g_mirror_device_state2str(sc->sc_state),
2543 g_mirror_get_diskname(disk),
2544 g_mirror_disk_state2str(disk->d_state)));
2546 * STALE state is only possible if device is marked
2549 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2550 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2551 g_mirror_device_state2str(sc->sc_state),
2552 g_mirror_get_diskname(disk),
2553 g_mirror_disk_state2str(disk->d_state)));
2554 DISK_STATE_CHANGED();
2556 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2557 disk->d_state = state;
2558 g_mirror_update_metadata(disk);
2559 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2560 sc->sc_name, g_mirror_get_diskname(disk));
2562 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2564 * Possible scenarios:
2565 * 1. Disk which needs synchronization was connected.
2567 /* Previous state should be NEW. */
2568 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2569 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2570 g_mirror_disk_state2str(disk->d_state)));
2571 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2572 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2573 g_mirror_device_state2str(sc->sc_state),
2574 g_mirror_get_diskname(disk),
2575 g_mirror_disk_state2str(disk->d_state)));
2576 DISK_STATE_CHANGED();
2578 if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2579 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2580 disk->d_state = state;
2581 if (sc->sc_provider != NULL) {
2582 g_mirror_sync_start(disk);
2583 g_mirror_update_metadata(disk);
2586 case G_MIRROR_DISK_STATE_DISCONNECTED:
2588 * Possible scenarios:
2589 * 1. Device wasn't running yet, but disk disappear.
2590 * 2. Disk was active and disapppear.
2591 * 3. Disk disappear during synchronization process.
2593 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2595 * Previous state should be ACTIVE, STALE or
2598 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2599 disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2600 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2601 ("Wrong disk state (%s, %s).",
2602 g_mirror_get_diskname(disk),
2603 g_mirror_disk_state2str(disk->d_state)));
2604 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2605 /* Previous state should be NEW. */
2606 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2607 ("Wrong disk state (%s, %s).",
2608 g_mirror_get_diskname(disk),
2609 g_mirror_disk_state2str(disk->d_state)));
2611 * Reset bumping syncid if disk disappeared in STARTING
2614 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2615 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2618 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2620 g_mirror_device_state2str(sc->sc_state),
2621 g_mirror_get_diskname(disk),
2622 g_mirror_disk_state2str(disk->d_state)));
2625 DISK_STATE_CHANGED();
2626 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2627 sc->sc_name, g_mirror_get_diskname(disk));
2629 g_mirror_destroy_disk(disk);
2631 case G_MIRROR_DISK_STATE_DESTROY:
2635 error = g_mirror_clear_metadata(disk);
2638 DISK_STATE_CHANGED();
2639 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2640 sc->sc_name, g_mirror_get_diskname(disk));
2642 g_mirror_destroy_disk(disk);
2644 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2645 g_mirror_update_metadata(disk);
2650 KASSERT(1 == 0, ("Unknown state (%u).", state));
2655 #undef DISK_STATE_CHANGED
2658 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2660 struct g_provider *pp;
2664 g_topology_assert();
2666 error = g_access(cp, 1, 0, 0);
2670 g_topology_unlock();
2671 /* Metadata are stored on last sector. */
2672 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2675 g_access(cp, -1, 0, 0);
2677 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2678 cp->provider->name, error);
2682 /* Decode metadata. */
2683 error = mirror_metadata_decode(buf, md);
2685 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2687 if (md->md_version > G_MIRROR_VERSION) {
2689 "Kernel module is too old to handle metadata from %s.",
2690 cp->provider->name);
2694 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2695 cp->provider->name);
2703 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2704 struct g_mirror_metadata *md)
2707 if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2708 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2709 pp->name, md->md_did);
2712 if (md->md_all != sc->sc_ndisks) {
2714 "Invalid '%s' field on disk %s (device %s), skipping.",
2715 "md_all", pp->name, sc->sc_name);
2718 if (md->md_slice != sc->sc_slice) {
2720 "Invalid '%s' field on disk %s (device %s), skipping.",
2721 "md_slice", pp->name, sc->sc_name);
2724 if (md->md_balance != sc->sc_balance) {
2726 "Invalid '%s' field on disk %s (device %s), skipping.",
2727 "md_balance", pp->name, sc->sc_name);
2731 if (md->md_mediasize != sc->sc_mediasize) {
2733 "Invalid '%s' field on disk %s (device %s), skipping.",
2734 "md_mediasize", pp->name, sc->sc_name);
2738 if (sc->sc_mediasize > pp->mediasize) {
2740 "Invalid size of disk %s (device %s), skipping.", pp->name,
2744 if (md->md_sectorsize != sc->sc_sectorsize) {
2746 "Invalid '%s' field on disk %s (device %s), skipping.",
2747 "md_sectorsize", pp->name, sc->sc_name);
2750 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2752 "Invalid sector size of disk %s (device %s), skipping.",
2753 pp->name, sc->sc_name);
2756 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2758 "Invalid device flags on disk %s (device %s), skipping.",
2759 pp->name, sc->sc_name);
2762 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2764 "Invalid disk flags on disk %s (device %s), skipping.",
2765 pp->name, sc->sc_name);
2772 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2773 struct g_mirror_metadata *md)
2775 struct g_mirror_disk *disk;
2778 g_topology_assert_not();
2779 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2781 error = g_mirror_check_metadata(sc, pp, md);
2784 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING &&
2785 md->md_genid < sc->sc_genid) {
2786 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
2787 pp->name, sc->sc_name);
2790 disk = g_mirror_init_disk(sc, pp, md, &error);
2793 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
2794 G_MIRROR_EVENT_WAIT);
2797 if (md->md_version < G_MIRROR_VERSION) {
2798 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
2799 pp->name, md->md_version, G_MIRROR_VERSION);
2800 g_mirror_update_metadata(disk);
2806 g_mirror_destroy_delayed(void *arg, int flag)
2808 struct g_mirror_softc *sc;
2811 if (flag == EV_CANCEL) {
2812 G_MIRROR_DEBUG(1, "Destroying canceled.");
2816 g_topology_unlock();
2817 sx_xlock(&sc->sc_lock);
2818 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
2819 ("DESTROY flag set on %s.", sc->sc_name));
2820 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0,
2821 ("DESTROYING flag not set on %s.", sc->sc_name));
2822 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
2823 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
2825 G_MIRROR_DEBUG(0, "Cannot destroy %s.", sc->sc_name);
2826 sx_xunlock(&sc->sc_lock);
2832 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
2834 struct g_mirror_softc *sc;
2835 int dcr, dcw, dce, error = 0;
2837 g_topology_assert();
2838 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2841 sc = pp->geom->softc;
2842 if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
2844 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
2846 dcr = pp->acr + acr;
2847 dcw = pp->acw + acw;
2848 dce = pp->ace + ace;
2850 g_topology_unlock();
2851 sx_xlock(&sc->sc_lock);
2852 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
2853 LIST_EMPTY(&sc->sc_disks)) {
2854 if (acr > 0 || acw > 0 || ace > 0)
2859 g_mirror_idle(sc, dcw);
2860 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0) {
2861 if (acr > 0 || acw > 0 || ace > 0) {
2865 if (dcr == 0 && dcw == 0 && dce == 0) {
2866 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK,
2871 sx_xunlock(&sc->sc_lock);
2876 static struct g_geom *
2877 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
2879 struct g_mirror_softc *sc;
2883 g_topology_assert();
2884 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
2887 /* One disk is minimum. */
2893 gp = g_new_geomf(mp, "%s", md->md_name);
2894 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
2895 gp->start = g_mirror_start;
2896 gp->orphan = g_mirror_orphan;
2897 gp->access = g_mirror_access;
2898 gp->dumpconf = g_mirror_dumpconf;
2900 sc->sc_id = md->md_mid;
2901 sc->sc_slice = md->md_slice;
2902 sc->sc_balance = md->md_balance;
2903 sc->sc_mediasize = md->md_mediasize;
2904 sc->sc_sectorsize = md->md_sectorsize;
2905 sc->sc_ndisks = md->md_all;
2906 sc->sc_flags = md->md_mflags;
2909 sc->sc_last_write = time_uptime;
2911 sx_init(&sc->sc_lock, "gmirror:lock");
2912 bioq_init(&sc->sc_queue);
2913 mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
2914 bioq_init(&sc->sc_regular_delayed);
2915 bioq_init(&sc->sc_inflight);
2916 bioq_init(&sc->sc_sync_delayed);
2917 LIST_INIT(&sc->sc_disks);
2918 TAILQ_INIT(&sc->sc_events);
2919 mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
2920 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
2921 mtx_init(&sc->sc_done_mtx, "gmirror:done", NULL, MTX_DEF);
2922 sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
2925 sc->sc_provider = NULL;
2927 * Synchronization geom.
2929 gp = g_new_geomf(mp, "%s.sync", md->md_name);
2931 gp->orphan = g_mirror_orphan;
2932 sc->sc_sync.ds_geom = gp;
2933 sc->sc_sync.ds_ndisks = 0;
2934 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
2935 "g_mirror %s", md->md_name);
2937 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
2939 g_destroy_geom(sc->sc_sync.ds_geom);
2940 mtx_destroy(&sc->sc_done_mtx);
2941 mtx_destroy(&sc->sc_events_mtx);
2942 mtx_destroy(&sc->sc_queue_mtx);
2943 sx_destroy(&sc->sc_lock);
2944 g_destroy_geom(sc->sc_geom);
2949 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
2950 sc->sc_name, sc->sc_ndisks, sc->sc_id);
2952 sc->sc_rootmount = root_mount_hold("GMIRROR");
2953 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
2957 timeout = g_mirror_timeout * hz;
2958 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
2959 return (sc->sc_geom);
2963 g_mirror_destroy(struct g_mirror_softc *sc, int how)
2965 struct g_mirror_disk *disk;
2966 struct g_provider *pp;
2968 g_topology_assert_not();
2971 sx_assert(&sc->sc_lock, SX_XLOCKED);
2973 pp = sc->sc_provider;
2974 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
2976 case G_MIRROR_DESTROY_SOFT:
2978 "Device %s is still open (r%dw%de%d).", pp->name,
2979 pp->acr, pp->acw, pp->ace);
2981 case G_MIRROR_DESTROY_DELAYED:
2983 "Device %s will be destroyed on last close.",
2985 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2986 if (disk->d_state ==
2987 G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2988 g_mirror_sync_stop(disk, 1);
2991 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROYING;
2993 case G_MIRROR_DESTROY_HARD:
2994 G_MIRROR_DEBUG(1, "Device %s is still open, so it "
2995 "can't be definitely removed.", pp->name);
3000 if (sc->sc_geom->softc == NULL) {
3001 g_topology_unlock();
3004 sc->sc_geom->softc = NULL;
3005 sc->sc_sync.ds_geom->softc = NULL;
3006 g_topology_unlock();
3008 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
3009 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WAIT;
3010 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3011 sx_xunlock(&sc->sc_lock);
3012 mtx_lock(&sc->sc_queue_mtx);
3014 mtx_unlock(&sc->sc_queue_mtx);
3015 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3016 while (sc->sc_worker != NULL)
3017 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
3018 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3019 sx_xlock(&sc->sc_lock);
3020 g_mirror_destroy_device(sc);
3026 g_mirror_taste_orphan(struct g_consumer *cp)
3029 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3030 cp->provider->name));
3033 static struct g_geom *
3034 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3036 struct g_mirror_metadata md;
3037 struct g_mirror_softc *sc;
3038 struct g_consumer *cp;
3042 g_topology_assert();
3043 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3044 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
3046 gp = g_new_geomf(mp, "mirror:taste");
3048 * This orphan function should be never called.
3050 gp->orphan = g_mirror_taste_orphan;
3051 cp = g_new_consumer(gp);
3053 error = g_mirror_read_metadata(cp, &md);
3055 g_destroy_consumer(cp);
3061 if (md.md_provider[0] != '\0' &&
3062 !g_compare_names(md.md_provider, pp->name))
3064 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3066 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3068 "Device %s: provider %s marked as inactive, skipping.",
3069 md.md_name, pp->name);
3072 if (g_mirror_debug >= 2)
3073 mirror_metadata_dump(&md);
3076 * Let's check if device already exists.
3079 LIST_FOREACH(gp, &mp->geom, geom) {
3083 if (sc->sc_sync.ds_geom == gp)
3085 if (strcmp(md.md_name, sc->sc_name) != 0)
3087 if (md.md_mid != sc->sc_id) {
3088 G_MIRROR_DEBUG(0, "Device %s already configured.",
3095 gp = g_mirror_create(mp, &md);
3097 G_MIRROR_DEBUG(0, "Cannot create device %s.",
3103 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3104 g_topology_unlock();
3105 sx_xlock(&sc->sc_lock);
3106 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
3107 error = g_mirror_add_disk(sc, pp, &md);
3109 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3110 pp->name, gp->name, error);
3111 if (LIST_EMPTY(&sc->sc_disks)) {
3113 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3119 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
3120 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3121 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3125 sx_xunlock(&sc->sc_lock);
3131 g_mirror_resize(struct g_consumer *cp)
3133 struct g_mirror_disk *disk;
3135 g_topology_assert();
3136 g_trace(G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name);
3141 g_topology_unlock();
3142 g_mirror_update_metadata(disk);
3147 g_mirror_destroy_geom(struct gctl_req *req __unused,
3148 struct g_class *mp __unused, struct g_geom *gp)
3150 struct g_mirror_softc *sc;
3153 g_topology_unlock();
3155 sx_xlock(&sc->sc_lock);
3157 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3159 sx_xunlock(&sc->sc_lock);
3165 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3166 struct g_consumer *cp, struct g_provider *pp)
3168 struct g_mirror_softc *sc;
3170 g_topology_assert();
3175 /* Skip synchronization geom. */
3176 if (gp == sc->sc_sync.ds_geom)
3180 } else if (cp != NULL) {
3181 struct g_mirror_disk *disk;
3186 g_topology_unlock();
3187 sx_xlock(&sc->sc_lock);
3188 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3189 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3190 sbuf_printf(sb, "%s<Synchronized>", indent);
3191 if (disk->d_sync.ds_offset == 0)
3192 sbuf_printf(sb, "0%%");
3194 sbuf_printf(sb, "%u%%",
3195 (u_int)((disk->d_sync.ds_offset * 100) /
3196 sc->sc_provider->mediasize));
3198 sbuf_printf(sb, "</Synchronized>\n");
3199 if (disk->d_sync.ds_offset > 0) {
3200 sbuf_printf(sb, "%s<BytesSynced>%jd"
3201 "</BytesSynced>\n", indent,
3202 (intmax_t)disk->d_sync.ds_offset);
3205 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3206 disk->d_sync.ds_syncid);
3207 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3209 sbuf_printf(sb, "%s<Flags>", indent);
3210 if (disk->d_flags == 0)
3211 sbuf_printf(sb, "NONE");
3215 #define ADD_FLAG(flag, name) do { \
3216 if ((disk->d_flags & (flag)) != 0) { \
3218 sbuf_printf(sb, ", "); \
3221 sbuf_printf(sb, name); \
3224 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3225 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3226 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3227 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3229 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3230 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3233 sbuf_printf(sb, "</Flags>\n");
3234 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3236 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3237 g_mirror_disk_state2str(disk->d_state));
3238 sx_xunlock(&sc->sc_lock);
3241 g_topology_unlock();
3242 sx_xlock(&sc->sc_lock);
3243 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3244 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3245 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3246 sbuf_printf(sb, "%s<Flags>", indent);
3247 if (sc->sc_flags == 0)
3248 sbuf_printf(sb, "NONE");
3252 #define ADD_FLAG(flag, name) do { \
3253 if ((sc->sc_flags & (flag)) != 0) { \
3255 sbuf_printf(sb, ", "); \
3258 sbuf_printf(sb, name); \
3261 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3262 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3265 sbuf_printf(sb, "</Flags>\n");
3266 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3267 (u_int)sc->sc_slice);
3268 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3269 balance_name(sc->sc_balance));
3270 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3272 sbuf_printf(sb, "%s<State>", indent);
3273 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3274 sbuf_printf(sb, "%s", "STARTING");
3275 else if (sc->sc_ndisks ==
3276 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3277 sbuf_printf(sb, "%s", "COMPLETE");
3279 sbuf_printf(sb, "%s", "DEGRADED");
3280 sbuf_printf(sb, "</State>\n");
3281 sx_xunlock(&sc->sc_lock);
3287 g_mirror_shutdown_post_sync(void *arg, int howto)
3290 struct g_geom *gp, *gp2;
3291 struct g_mirror_softc *sc;
3297 g_mirror_shutdown = 1;
3298 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3299 if ((sc = gp->softc) == NULL)
3301 /* Skip synchronization geom. */
3302 if (gp == sc->sc_sync.ds_geom)
3304 g_topology_unlock();
3305 sx_xlock(&sc->sc_lock);
3306 g_mirror_idle(sc, -1);
3308 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3310 sx_xunlock(&sc->sc_lock);
3313 g_topology_unlock();
3318 g_mirror_init(struct g_class *mp)
3321 g_mirror_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
3322 g_mirror_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
3323 if (g_mirror_post_sync == NULL)
3324 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3328 g_mirror_fini(struct g_class *mp)
3331 if (g_mirror_post_sync != NULL)
3332 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_mirror_post_sync);
3335 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);