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 g_ctl_destroy_geom_t g_mirror_destroy_geom;
88 static g_taste_t g_mirror_taste;
89 static g_init_t g_mirror_init;
90 static g_fini_t g_mirror_fini;
91 static g_provgone_t g_mirror_providergone;
92 static g_resize_t g_mirror_resize;
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 .providergone = g_mirror_providergone,
103 .resize = g_mirror_resize
107 static void g_mirror_destroy_provider(struct g_mirror_softc *sc);
108 static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
109 static void g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force);
110 static void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
111 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
112 static void g_mirror_sync_stop(struct g_mirror_disk *disk, int type);
113 static void g_mirror_register_request(struct bio *bp);
114 static void g_mirror_sync_release(struct g_mirror_softc *sc);
118 g_mirror_disk_state2str(int state)
122 case G_MIRROR_DISK_STATE_NONE:
124 case G_MIRROR_DISK_STATE_NEW:
126 case G_MIRROR_DISK_STATE_ACTIVE:
128 case G_MIRROR_DISK_STATE_STALE:
130 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
131 return ("SYNCHRONIZING");
132 case G_MIRROR_DISK_STATE_DISCONNECTED:
133 return ("DISCONNECTED");
134 case G_MIRROR_DISK_STATE_DESTROY:
142 g_mirror_device_state2str(int state)
146 case G_MIRROR_DEVICE_STATE_STARTING:
148 case G_MIRROR_DEVICE_STATE_RUNNING:
156 g_mirror_get_diskname(struct g_mirror_disk *disk)
159 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
160 return ("[unknown]");
161 return (disk->d_name);
165 * --- Events handling functions ---
166 * Events in geom_mirror are used to maintain disks and device status
167 * from one thread to simplify locking.
170 g_mirror_event_free(struct g_mirror_event *ep)
177 g_mirror_event_send(void *arg, int state, int flags)
179 struct g_mirror_softc *sc;
180 struct g_mirror_disk *disk;
181 struct g_mirror_event *ep;
184 ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
185 G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
186 if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
197 mtx_lock(&sc->sc_events_mtx);
198 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
199 mtx_unlock(&sc->sc_events_mtx);
200 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
201 mtx_lock(&sc->sc_queue_mtx);
203 mtx_unlock(&sc->sc_queue_mtx);
204 if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0)
206 sx_assert(&sc->sc_lock, SX_XLOCKED);
207 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
208 sx_xunlock(&sc->sc_lock);
209 while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) {
210 mtx_lock(&sc->sc_events_mtx);
211 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event",
215 g_mirror_event_free(ep);
216 sx_xlock(&sc->sc_lock);
220 static struct g_mirror_event *
221 g_mirror_event_get(struct g_mirror_softc *sc)
223 struct g_mirror_event *ep;
225 mtx_lock(&sc->sc_events_mtx);
226 ep = TAILQ_FIRST(&sc->sc_events);
227 mtx_unlock(&sc->sc_events_mtx);
232 g_mirror_event_remove(struct g_mirror_softc *sc, struct g_mirror_event *ep)
235 mtx_lock(&sc->sc_events_mtx);
236 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
237 mtx_unlock(&sc->sc_events_mtx);
241 g_mirror_event_cancel(struct g_mirror_disk *disk)
243 struct g_mirror_softc *sc;
244 struct g_mirror_event *ep, *tmpep;
247 sx_assert(&sc->sc_lock, SX_XLOCKED);
249 mtx_lock(&sc->sc_events_mtx);
250 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
251 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0)
253 if (ep->e_disk != disk)
255 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
256 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
257 g_mirror_event_free(ep);
259 ep->e_error = ECANCELED;
263 mtx_unlock(&sc->sc_events_mtx);
267 * Return the number of disks in given state.
268 * If state is equal to -1, count all connected disks.
271 g_mirror_ndisks(struct g_mirror_softc *sc, int state)
273 struct g_mirror_disk *disk;
276 sx_assert(&sc->sc_lock, SX_LOCKED);
278 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
279 if (state == -1 || disk->d_state == state)
286 * Find a disk in mirror by its disk ID.
288 static struct g_mirror_disk *
289 g_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id)
291 struct g_mirror_disk *disk;
293 sx_assert(&sc->sc_lock, SX_XLOCKED);
295 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
296 if (disk->d_id == id)
303 g_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp)
308 mtx_lock(&sc->sc_queue_mtx);
309 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
310 if (bp->bio_from == cp)
313 mtx_unlock(&sc->sc_queue_mtx);
318 g_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp)
323 "I/O requests for %s exist, can't destroy it now.",
327 if (g_mirror_nrequests(sc, cp) > 0) {
329 "I/O requests for %s in queue, can't destroy it now.",
337 g_mirror_destroy_consumer(void *arg, int flags __unused)
339 struct g_consumer *cp;
344 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
346 g_destroy_consumer(cp);
350 g_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
352 struct g_provider *pp;
358 if (g_mirror_is_busy(sc, cp))
363 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
366 G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
367 -cp->acw, -cp->ace, 0);
368 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
369 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
372 * After retaste event was send (inside g_access()), we can send
373 * event to detach and destroy consumer.
374 * A class, which has consumer to the given provider connected
375 * will not receive retaste event for the provider.
376 * This is the way how I ignore retaste events when I close
377 * consumers opened for write: I detach and destroy consumer
378 * after retaste event is sent.
380 g_post_event(g_mirror_destroy_consumer, cp, M_WAITOK, NULL);
383 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", pp->name);
385 g_destroy_consumer(cp);
389 g_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp)
391 struct g_consumer *cp;
394 g_topology_assert_not();
395 KASSERT(disk->d_consumer == NULL,
396 ("Disk already connected (device %s).", disk->d_softc->sc_name));
399 cp = g_new_consumer(disk->d_softc->sc_geom);
400 cp->flags |= G_CF_DIRECT_RECEIVE;
401 error = g_attach(cp, pp);
403 g_destroy_consumer(cp);
407 error = g_access(cp, 1, 1, 1);
410 g_destroy_consumer(cp);
412 G_MIRROR_DEBUG(0, "Cannot open consumer %s (error=%d).",
417 disk->d_consumer = cp;
418 disk->d_consumer->private = disk;
419 disk->d_consumer->index = 0;
421 G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk));
426 g_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
433 if (cp->provider != NULL)
434 g_mirror_kill_consumer(sc, cp);
436 g_destroy_consumer(cp);
440 * Initialize disk. This means allocate memory, create consumer, attach it
441 * to the provider and open access (r1w1e1) to it.
443 static struct g_mirror_disk *
444 g_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp,
445 struct g_mirror_metadata *md, int *errorp)
447 struct g_mirror_disk *disk;
450 disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO);
456 error = g_mirror_connect_disk(disk, pp);
459 disk->d_id = md->md_did;
460 disk->d_state = G_MIRROR_DISK_STATE_NONE;
461 disk->d_priority = md->md_priority;
462 disk->d_flags = md->md_dflags;
463 error = g_getattr("GEOM::candelete", disk->d_consumer, &i);
464 if (error == 0 && i != 0)
465 disk->d_flags |= G_MIRROR_DISK_FLAG_CANDELETE;
466 if (md->md_provider[0] != '\0')
467 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
468 disk->d_sync.ds_consumer = NULL;
469 disk->d_sync.ds_offset = md->md_sync_offset;
470 disk->d_sync.ds_offset_done = md->md_sync_offset;
471 disk->d_genid = md->md_genid;
472 disk->d_sync.ds_syncid = md->md_syncid;
480 free(disk, M_MIRROR);
485 g_mirror_destroy_disk(struct g_mirror_disk *disk)
487 struct g_mirror_softc *sc;
489 g_topology_assert_not();
491 sx_assert(&sc->sc_lock, SX_XLOCKED);
493 LIST_REMOVE(disk, d_next);
494 g_mirror_event_cancel(disk);
495 if (sc->sc_hint == disk)
497 switch (disk->d_state) {
498 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
499 g_mirror_sync_stop(disk, 1);
501 case G_MIRROR_DISK_STATE_NEW:
502 case G_MIRROR_DISK_STATE_STALE:
503 case G_MIRROR_DISK_STATE_ACTIVE:
505 g_mirror_disconnect_consumer(sc, disk->d_consumer);
507 free(disk, M_MIRROR);
510 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
511 g_mirror_get_diskname(disk),
512 g_mirror_disk_state2str(disk->d_state)));
517 g_mirror_free_device(struct g_mirror_softc *sc)
520 mtx_destroy(&sc->sc_queue_mtx);
521 mtx_destroy(&sc->sc_events_mtx);
522 mtx_destroy(&sc->sc_done_mtx);
523 sx_destroy(&sc->sc_lock);
528 g_mirror_providergone(struct g_provider *pp)
530 struct g_mirror_softc *sc = pp->private;
532 if ((--sc->sc_refcnt) == 0)
533 g_mirror_free_device(sc);
537 g_mirror_destroy_device(struct g_mirror_softc *sc)
539 struct g_mirror_disk *disk;
540 struct g_mirror_event *ep;
542 struct g_consumer *cp, *tmpcp;
544 g_topology_assert_not();
545 sx_assert(&sc->sc_lock, SX_XLOCKED);
548 if (sc->sc_provider != NULL)
549 g_mirror_destroy_provider(sc);
550 for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL;
551 disk = LIST_FIRST(&sc->sc_disks)) {
552 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
553 g_mirror_update_metadata(disk);
554 g_mirror_destroy_disk(disk);
556 while ((ep = g_mirror_event_get(sc)) != NULL) {
557 g_mirror_event_remove(sc, ep);
558 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
559 g_mirror_event_free(ep);
561 ep->e_error = ECANCELED;
562 ep->e_flags |= G_MIRROR_EVENT_DONE;
563 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep);
564 mtx_lock(&sc->sc_events_mtx);
566 mtx_unlock(&sc->sc_events_mtx);
569 callout_drain(&sc->sc_callout);
572 LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
573 g_mirror_disconnect_consumer(sc, cp);
575 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
576 G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
577 g_wither_geom(gp, ENXIO);
578 sx_xunlock(&sc->sc_lock);
579 if ((--sc->sc_refcnt) == 0)
580 g_mirror_free_device(sc);
585 g_mirror_orphan(struct g_consumer *cp)
587 struct g_mirror_disk *disk;
594 disk->d_softc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
595 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
596 G_MIRROR_EVENT_DONTWAIT);
600 * Function should return the next active disk on the list.
601 * It is possible that it will be the same disk as given.
602 * If there are no active disks on list, NULL is returned.
604 static __inline struct g_mirror_disk *
605 g_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
607 struct g_mirror_disk *dp;
609 for (dp = LIST_NEXT(disk, d_next); dp != disk;
610 dp = LIST_NEXT(dp, d_next)) {
612 dp = LIST_FIRST(&sc->sc_disks);
613 if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE)
616 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
621 static struct g_mirror_disk *
622 g_mirror_get_disk(struct g_mirror_softc *sc)
624 struct g_mirror_disk *disk;
626 if (sc->sc_hint == NULL) {
627 sc->sc_hint = LIST_FIRST(&sc->sc_disks);
628 if (sc->sc_hint == NULL)
632 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) {
633 disk = g_mirror_find_next(sc, disk);
637 sc->sc_hint = g_mirror_find_next(sc, disk);
642 g_mirror_write_metadata(struct g_mirror_disk *disk,
643 struct g_mirror_metadata *md)
645 struct g_mirror_softc *sc;
646 struct g_consumer *cp;
647 off_t offset, length;
651 g_topology_assert_not();
653 sx_assert(&sc->sc_lock, SX_LOCKED);
655 cp = disk->d_consumer;
656 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
657 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
658 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
659 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
661 length = cp->provider->sectorsize;
662 offset = cp->provider->mediasize - length;
663 sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO);
665 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0) {
667 * Handle the case, when the size of parent provider reduced.
669 if (offset < md->md_mediasize)
672 mirror_metadata_encode(md, sector);
675 error = g_write_data(cp, offset, sector, length);
676 free(sector, M_MIRROR);
678 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
679 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
680 G_MIRROR_DEBUG(0, "Cannot write metadata on %s "
681 "(device=%s, error=%d).",
682 g_mirror_get_diskname(disk), sc->sc_name, error);
684 G_MIRROR_DEBUG(1, "Cannot write metadata on %s "
685 "(device=%s, error=%d).",
686 g_mirror_get_diskname(disk), sc->sc_name, error);
688 if (g_mirror_disconnect_on_failure &&
689 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) {
690 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
691 g_mirror_event_send(disk,
692 G_MIRROR_DISK_STATE_DISCONNECTED,
693 G_MIRROR_EVENT_DONTWAIT);
700 g_mirror_clear_metadata(struct g_mirror_disk *disk)
704 g_topology_assert_not();
705 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
707 error = g_mirror_write_metadata(disk, NULL);
709 G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
710 g_mirror_get_diskname(disk));
713 "Cannot clear metadata on disk %s (error=%d).",
714 g_mirror_get_diskname(disk), error);
720 g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
721 struct g_mirror_metadata *md)
724 strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
725 md->md_version = G_MIRROR_VERSION;
726 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
727 md->md_mid = sc->sc_id;
728 md->md_all = sc->sc_ndisks;
729 md->md_slice = sc->sc_slice;
730 md->md_balance = sc->sc_balance;
731 md->md_genid = sc->sc_genid;
732 md->md_mediasize = sc->sc_mediasize;
733 md->md_sectorsize = sc->sc_sectorsize;
734 md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
735 bzero(md->md_provider, sizeof(md->md_provider));
737 md->md_did = arc4random();
741 md->md_sync_offset = 0;
744 md->md_did = disk->d_id;
745 md->md_priority = disk->d_priority;
746 md->md_syncid = disk->d_sync.ds_syncid;
747 md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
748 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
749 md->md_sync_offset = disk->d_sync.ds_offset_done;
751 md->md_sync_offset = 0;
752 if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
753 strlcpy(md->md_provider,
754 disk->d_consumer->provider->name,
755 sizeof(md->md_provider));
757 md->md_provsize = disk->d_consumer->provider->mediasize;
762 g_mirror_update_metadata(struct g_mirror_disk *disk)
764 struct g_mirror_softc *sc;
765 struct g_mirror_metadata md;
768 g_topology_assert_not();
770 sx_assert(&sc->sc_lock, SX_LOCKED);
772 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0)
773 g_mirror_fill_metadata(sc, disk, &md);
774 error = g_mirror_write_metadata(disk, &md);
776 G_MIRROR_DEBUG(2, "Metadata on %s updated.",
777 g_mirror_get_diskname(disk));
780 "Cannot update metadata on disk %s (error=%d).",
781 g_mirror_get_diskname(disk), error);
786 g_mirror_bump_syncid(struct g_mirror_softc *sc)
788 struct g_mirror_disk *disk;
790 g_topology_assert_not();
791 sx_assert(&sc->sc_lock, SX_XLOCKED);
792 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
793 ("%s called with no active disks (device=%s).", __func__,
797 G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
799 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
800 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
801 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
802 disk->d_sync.ds_syncid = sc->sc_syncid;
803 g_mirror_update_metadata(disk);
809 g_mirror_bump_genid(struct g_mirror_softc *sc)
811 struct g_mirror_disk *disk;
813 g_topology_assert_not();
814 sx_assert(&sc->sc_lock, SX_XLOCKED);
815 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
816 ("%s called with no active disks (device=%s).", __func__,
820 G_MIRROR_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
822 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
823 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
824 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
825 disk->d_genid = sc->sc_genid;
826 g_mirror_update_metadata(disk);
832 g_mirror_idle(struct g_mirror_softc *sc, int acw)
834 struct g_mirror_disk *disk;
837 g_topology_assert_not();
838 sx_assert(&sc->sc_lock, SX_XLOCKED);
840 if (sc->sc_provider == NULL)
842 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
846 if (sc->sc_writes > 0)
848 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
849 timeout = g_mirror_idletime - (time_uptime - sc->sc_last_write);
850 if (!g_mirror_shutdown && timeout > 0)
854 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
855 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
857 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
858 g_mirror_get_diskname(disk), sc->sc_name);
859 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
860 g_mirror_update_metadata(disk);
866 g_mirror_unidle(struct g_mirror_softc *sc)
868 struct g_mirror_disk *disk;
870 g_topology_assert_not();
871 sx_assert(&sc->sc_lock, SX_XLOCKED);
873 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
876 sc->sc_last_write = time_uptime;
877 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
878 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
880 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
881 g_mirror_get_diskname(disk), sc->sc_name);
882 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
883 g_mirror_update_metadata(disk);
888 g_mirror_flush_done(struct bio *bp)
890 struct g_mirror_softc *sc;
893 pbp = bp->bio_parent;
894 sc = pbp->bio_to->private;
895 mtx_lock(&sc->sc_done_mtx);
896 if (pbp->bio_error == 0)
897 pbp->bio_error = bp->bio_error;
898 pbp->bio_completed += bp->bio_completed;
900 if (pbp->bio_children == pbp->bio_inbed) {
901 mtx_unlock(&sc->sc_done_mtx);
902 g_io_deliver(pbp, pbp->bio_error);
904 mtx_unlock(&sc->sc_done_mtx);
909 g_mirror_done(struct bio *bp)
911 struct g_mirror_softc *sc;
913 sc = bp->bio_from->geom->softc;
914 bp->bio_cflags = G_MIRROR_BIO_FLAG_REGULAR;
915 mtx_lock(&sc->sc_queue_mtx);
916 bioq_insert_tail(&sc->sc_queue, bp);
917 mtx_unlock(&sc->sc_queue_mtx);
922 g_mirror_regular_request(struct bio *bp)
924 struct g_mirror_softc *sc;
925 struct g_mirror_disk *disk;
928 g_topology_assert_not();
930 pbp = bp->bio_parent;
931 sc = pbp->bio_to->private;
932 bp->bio_from->index--;
933 if (bp->bio_cmd == BIO_WRITE)
935 disk = bp->bio_from->private;
938 g_mirror_kill_consumer(sc, bp->bio_from);
943 KASSERT(pbp->bio_inbed <= pbp->bio_children,
944 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
946 if (bp->bio_error == 0 && pbp->bio_error == 0) {
947 G_MIRROR_LOGREQ(3, bp, "Request delivered.");
949 if (pbp->bio_children == pbp->bio_inbed) {
950 G_MIRROR_LOGREQ(3, pbp, "Request delivered.");
951 pbp->bio_completed = pbp->bio_length;
952 if (pbp->bio_cmd == BIO_WRITE ||
953 pbp->bio_cmd == BIO_DELETE) {
954 bioq_remove(&sc->sc_inflight, pbp);
955 /* Release delayed sync requests if possible. */
956 g_mirror_sync_release(sc);
958 g_io_deliver(pbp, pbp->bio_error);
961 } else if (bp->bio_error != 0) {
962 if (pbp->bio_error == 0)
963 pbp->bio_error = bp->bio_error;
965 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
966 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
967 G_MIRROR_LOGREQ(0, bp,
968 "Request failed (error=%d).",
971 G_MIRROR_LOGREQ(1, bp,
972 "Request failed (error=%d).",
975 if (g_mirror_disconnect_on_failure &&
976 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1)
978 if (bp->bio_error == ENXIO &&
979 bp->bio_cmd == BIO_READ)
980 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
981 else if (bp->bio_error == ENXIO)
982 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID_NOW;
984 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
985 g_mirror_event_send(disk,
986 G_MIRROR_DISK_STATE_DISCONNECTED,
987 G_MIRROR_EVENT_DONTWAIT);
990 switch (pbp->bio_cmd) {
1000 switch (pbp->bio_cmd) {
1002 if (pbp->bio_inbed < pbp->bio_children)
1004 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1)
1005 g_io_deliver(pbp, pbp->bio_error);
1008 mtx_lock(&sc->sc_queue_mtx);
1009 bioq_insert_tail(&sc->sc_queue, pbp);
1010 mtx_unlock(&sc->sc_queue_mtx);
1011 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1017 if (pbp->bio_children == 0) {
1019 * All requests failed.
1021 } else if (pbp->bio_inbed < pbp->bio_children) {
1024 } else if (pbp->bio_children == pbp->bio_inbed) {
1025 /* Some requests succeeded. */
1027 pbp->bio_completed = pbp->bio_length;
1029 bioq_remove(&sc->sc_inflight, pbp);
1030 /* Release delayed sync requests if possible. */
1031 g_mirror_sync_release(sc);
1032 g_io_deliver(pbp, pbp->bio_error);
1035 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
1041 g_mirror_sync_done(struct bio *bp)
1043 struct g_mirror_softc *sc;
1045 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
1046 sc = bp->bio_from->geom->softc;
1047 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
1048 mtx_lock(&sc->sc_queue_mtx);
1049 bioq_insert_tail(&sc->sc_queue, bp);
1050 mtx_unlock(&sc->sc_queue_mtx);
1055 g_mirror_candelete(struct bio *bp)
1057 struct g_mirror_softc *sc;
1058 struct g_mirror_disk *disk;
1061 sc = bp->bio_to->private;
1062 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1063 if (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE)
1066 val = (int *)bp->bio_data;
1067 *val = (disk != NULL);
1068 g_io_deliver(bp, 0);
1072 g_mirror_kernel_dump(struct bio *bp)
1074 struct g_mirror_softc *sc;
1075 struct g_mirror_disk *disk;
1077 struct g_kerneldump *gkd;
1080 * We configure dumping to the first component, because this component
1081 * will be used for reading with 'prefer' balance algorithm.
1082 * If the component with the higest priority is currently disconnected
1083 * we will not be able to read the dump after the reboot if it will be
1084 * connected and synchronized later. Can we do something better?
1086 sc = bp->bio_to->private;
1087 disk = LIST_FIRST(&sc->sc_disks);
1089 gkd = (struct g_kerneldump *)bp->bio_data;
1090 if (gkd->length > bp->bio_to->mediasize)
1091 gkd->length = bp->bio_to->mediasize;
1092 cbp = g_clone_bio(bp);
1094 g_io_deliver(bp, ENOMEM);
1097 cbp->bio_done = g_std_done;
1098 g_io_request(cbp, disk->d_consumer);
1099 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1100 g_mirror_get_diskname(disk));
1104 g_mirror_flush(struct g_mirror_softc *sc, struct bio *bp)
1106 struct bio_queue_head queue;
1107 struct g_mirror_disk *disk;
1108 struct g_consumer *cp;
1112 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1113 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1115 cbp = g_clone_bio(bp);
1117 while ((cbp = bioq_takefirst(&queue)) != NULL)
1119 if (bp->bio_error == 0)
1120 bp->bio_error = ENOMEM;
1121 g_io_deliver(bp, bp->bio_error);
1124 bioq_insert_tail(&queue, cbp);
1125 cbp->bio_done = g_mirror_flush_done;
1126 cbp->bio_caller1 = disk;
1127 cbp->bio_to = disk->d_consumer->provider;
1129 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1130 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1131 disk = cbp->bio_caller1;
1132 cbp->bio_caller1 = NULL;
1133 cp = disk->d_consumer;
1134 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1135 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1136 cp->acr, cp->acw, cp->ace));
1137 g_io_request(cbp, disk->d_consumer);
1142 g_mirror_start(struct bio *bp)
1144 struct g_mirror_softc *sc;
1146 sc = bp->bio_to->private;
1148 * If sc == NULL or there are no valid disks, provider's error
1149 * should be set and g_mirror_start() should not be called at all.
1151 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1152 ("Provider's error should be set (error=%d)(mirror=%s).",
1153 bp->bio_to->error, bp->bio_to->name));
1154 G_MIRROR_LOGREQ(3, bp, "Request received.");
1156 switch (bp->bio_cmd) {
1162 g_mirror_flush(sc, bp);
1165 if (!strcmp(bp->bio_attribute, "GEOM::candelete")) {
1166 g_mirror_candelete(bp);
1168 } else if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1169 g_mirror_kernel_dump(bp);
1174 g_io_deliver(bp, EOPNOTSUPP);
1177 mtx_lock(&sc->sc_queue_mtx);
1178 bioq_insert_tail(&sc->sc_queue, bp);
1179 mtx_unlock(&sc->sc_queue_mtx);
1180 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1185 * Return TRUE if the given request is colliding with a in-progress
1186 * synchronization request.
1189 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1191 struct g_mirror_disk *disk;
1193 off_t rstart, rend, sstart, send;
1196 if (sc->sc_sync.ds_ndisks == 0)
1198 rstart = bp->bio_offset;
1199 rend = bp->bio_offset + bp->bio_length;
1200 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1201 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1203 for (i = 0; i < g_mirror_syncreqs; i++) {
1204 sbp = disk->d_sync.ds_bios[i];
1207 sstart = sbp->bio_offset;
1208 send = sbp->bio_offset + sbp->bio_length;
1209 if (rend > sstart && rstart < send)
1217 * Return TRUE if the given sync request is colliding with a in-progress regular
1221 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1223 off_t rstart, rend, sstart, send;
1226 if (sc->sc_sync.ds_ndisks == 0)
1228 sstart = sbp->bio_offset;
1229 send = sbp->bio_offset + sbp->bio_length;
1230 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1231 rstart = bp->bio_offset;
1232 rend = bp->bio_offset + bp->bio_length;
1233 if (rend > sstart && rstart < send)
1240 * Puts request onto delayed queue.
1243 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1246 G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1247 bioq_insert_head(&sc->sc_regular_delayed, bp);
1251 * Puts synchronization request onto delayed queue.
1254 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1257 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1258 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1262 * Releases delayed regular requests which don't collide anymore with sync
1266 g_mirror_regular_release(struct g_mirror_softc *sc)
1268 struct bio *bp, *bp2;
1270 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1271 if (g_mirror_sync_collision(sc, bp))
1273 bioq_remove(&sc->sc_regular_delayed, bp);
1274 G_MIRROR_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1275 mtx_lock(&sc->sc_queue_mtx);
1276 bioq_insert_head(&sc->sc_queue, bp);
1279 * wakeup() is not needed, because this function is called from
1280 * the worker thread.
1282 wakeup(&sc->sc_queue);
1284 mtx_unlock(&sc->sc_queue_mtx);
1289 * Releases delayed sync requests which don't collide anymore with regular
1293 g_mirror_sync_release(struct g_mirror_softc *sc)
1295 struct bio *bp, *bp2;
1297 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1298 if (g_mirror_regular_collision(sc, bp))
1300 bioq_remove(&sc->sc_sync_delayed, bp);
1301 G_MIRROR_LOGREQ(2, bp,
1302 "Releasing delayed synchronization request.");
1303 g_io_request(bp, bp->bio_from);
1308 * Handle synchronization requests.
1309 * Every synchronization request is two-steps process: first, READ request is
1310 * send to active provider and then WRITE request (with read data) to the provider
1311 * beeing synchronized. When WRITE is finished, new synchronization request is
1315 g_mirror_sync_request(struct bio *bp)
1317 struct g_mirror_softc *sc;
1318 struct g_mirror_disk *disk;
1320 bp->bio_from->index--;
1321 sc = bp->bio_from->geom->softc;
1322 disk = bp->bio_from->private;
1324 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1326 g_mirror_kill_consumer(sc, bp->bio_from);
1327 g_topology_unlock();
1328 free(bp->bio_data, M_MIRROR);
1330 sx_xlock(&sc->sc_lock);
1335 * Synchronization request.
1337 switch (bp->bio_cmd) {
1340 struct g_consumer *cp;
1342 if (bp->bio_error != 0) {
1343 G_MIRROR_LOGREQ(0, bp,
1344 "Synchronization request failed (error=%d).",
1349 G_MIRROR_LOGREQ(3, bp,
1350 "Synchronization request half-finished.");
1351 bp->bio_cmd = BIO_WRITE;
1353 cp = disk->d_consumer;
1354 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1355 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1356 cp->acr, cp->acw, cp->ace));
1358 g_io_request(bp, cp);
1363 struct g_mirror_disk_sync *sync;
1368 if (bp->bio_error != 0) {
1369 G_MIRROR_LOGREQ(0, bp,
1370 "Synchronization request failed (error=%d).",
1373 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1374 g_mirror_event_send(disk,
1375 G_MIRROR_DISK_STATE_DISCONNECTED,
1376 G_MIRROR_EVENT_DONTWAIT);
1379 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1380 sync = &disk->d_sync;
1381 if (sync->ds_offset >= sc->sc_mediasize ||
1382 sync->ds_consumer == NULL ||
1383 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1384 /* Don't send more synchronization requests. */
1385 sync->ds_inflight--;
1386 if (sync->ds_bios != NULL) {
1387 i = (int)(uintptr_t)bp->bio_caller1;
1388 sync->ds_bios[i] = NULL;
1390 free(bp->bio_data, M_MIRROR);
1392 if (sync->ds_inflight > 0)
1394 if (sync->ds_consumer == NULL ||
1395 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1398 /* Disk up-to-date, activate it. */
1399 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1400 G_MIRROR_EVENT_DONTWAIT);
1404 /* Send next synchronization request. */
1405 data = bp->bio_data;
1406 bzero(bp, sizeof(*bp));
1407 bp->bio_cmd = BIO_READ;
1408 bp->bio_offset = sync->ds_offset;
1409 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1410 sync->ds_offset += bp->bio_length;
1411 bp->bio_done = g_mirror_sync_done;
1412 bp->bio_data = data;
1413 bp->bio_from = sync->ds_consumer;
1414 bp->bio_to = sc->sc_provider;
1415 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1416 sync->ds_consumer->index++;
1418 * Delay the request if it is colliding with a regular request.
1420 if (g_mirror_regular_collision(sc, bp))
1421 g_mirror_sync_delay(sc, bp);
1423 g_io_request(bp, sync->ds_consumer);
1425 /* Release delayed requests if possible. */
1426 g_mirror_regular_release(sc);
1428 /* Find the smallest offset */
1429 offset = sc->sc_mediasize;
1430 for (i = 0; i < g_mirror_syncreqs; i++) {
1431 bp = sync->ds_bios[i];
1432 if (bp->bio_offset < offset)
1433 offset = bp->bio_offset;
1435 if (sync->ds_offset_done + (MAXPHYS * 100) < offset) {
1436 /* Update offset_done on every 100 blocks. */
1437 sync->ds_offset_done = offset;
1438 g_mirror_update_metadata(disk);
1443 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1444 bp->bio_cmd, sc->sc_name));
1450 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1452 struct g_mirror_disk *disk;
1453 struct g_consumer *cp;
1456 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1457 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1461 if (bp->bio_error == 0)
1462 bp->bio_error = ENXIO;
1463 g_io_deliver(bp, bp->bio_error);
1466 cbp = g_clone_bio(bp);
1468 if (bp->bio_error == 0)
1469 bp->bio_error = ENOMEM;
1470 g_io_deliver(bp, bp->bio_error);
1474 * Fill in the component buf structure.
1476 cp = disk->d_consumer;
1477 cbp->bio_done = g_mirror_done;
1478 cbp->bio_to = cp->provider;
1479 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1480 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1481 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1484 g_io_request(cbp, cp);
1488 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1490 struct g_mirror_disk *disk;
1491 struct g_consumer *cp;
1494 disk = g_mirror_get_disk(sc);
1496 if (bp->bio_error == 0)
1497 bp->bio_error = ENXIO;
1498 g_io_deliver(bp, bp->bio_error);
1501 cbp = g_clone_bio(bp);
1503 if (bp->bio_error == 0)
1504 bp->bio_error = ENOMEM;
1505 g_io_deliver(bp, bp->bio_error);
1509 * Fill in the component buf structure.
1511 cp = disk->d_consumer;
1512 cbp->bio_done = g_mirror_done;
1513 cbp->bio_to = cp->provider;
1514 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1515 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1516 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1519 g_io_request(cbp, cp);
1522 #define TRACK_SIZE (1 * 1024 * 1024)
1523 #define LOAD_SCALE 256
1524 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
1527 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1529 struct g_mirror_disk *disk, *dp;
1530 struct g_consumer *cp;
1534 /* Find a disk with the smallest load. */
1537 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1538 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1541 /* If disk head is precisely in position - highly prefer it. */
1542 if (dp->d_last_offset == bp->bio_offset)
1543 prio -= 2 * LOAD_SCALE;
1545 /* If disk head is close to position - prefer it. */
1546 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1547 prio -= 1 * LOAD_SCALE;
1553 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1554 cbp = g_clone_bio(bp);
1556 if (bp->bio_error == 0)
1557 bp->bio_error = ENOMEM;
1558 g_io_deliver(bp, bp->bio_error);
1562 * Fill in the component buf structure.
1564 cp = disk->d_consumer;
1565 cbp->bio_done = g_mirror_done;
1566 cbp->bio_to = cp->provider;
1567 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1568 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1569 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1572 /* Remember last head position */
1573 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1575 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1576 dp->load = (dp->d_consumer->index * LOAD_SCALE +
1579 g_io_request(cbp, cp);
1583 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1585 struct bio_queue_head queue;
1586 struct g_mirror_disk *disk;
1587 struct g_consumer *cp;
1589 off_t left, mod, offset, slice;
1593 if (bp->bio_length <= sc->sc_slice) {
1594 g_mirror_request_round_robin(sc, bp);
1597 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1598 slice = bp->bio_length / ndisks;
1599 mod = slice % sc->sc_provider->sectorsize;
1601 slice += sc->sc_provider->sectorsize - mod;
1603 * Allocate all bios before sending any request, so we can
1604 * return ENOMEM in nice and clean way.
1606 left = bp->bio_length;
1607 offset = bp->bio_offset;
1608 data = bp->bio_data;
1610 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1611 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1613 cbp = g_clone_bio(bp);
1615 while ((cbp = bioq_takefirst(&queue)) != NULL)
1617 if (bp->bio_error == 0)
1618 bp->bio_error = ENOMEM;
1619 g_io_deliver(bp, bp->bio_error);
1622 bioq_insert_tail(&queue, cbp);
1623 cbp->bio_done = g_mirror_done;
1624 cbp->bio_caller1 = disk;
1625 cbp->bio_to = disk->d_consumer->provider;
1626 cbp->bio_offset = offset;
1627 cbp->bio_data = data;
1628 cbp->bio_length = MIN(left, slice);
1629 left -= cbp->bio_length;
1632 offset += cbp->bio_length;
1633 data += cbp->bio_length;
1635 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1636 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1637 disk = cbp->bio_caller1;
1638 cbp->bio_caller1 = NULL;
1639 cp = disk->d_consumer;
1640 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1641 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1642 cp->acr, cp->acw, cp->ace));
1643 disk->d_consumer->index++;
1644 g_io_request(cbp, disk->d_consumer);
1649 g_mirror_register_request(struct bio *bp)
1651 struct g_mirror_softc *sc;
1653 sc = bp->bio_to->private;
1654 switch (bp->bio_cmd) {
1656 switch (sc->sc_balance) {
1657 case G_MIRROR_BALANCE_LOAD:
1658 g_mirror_request_load(sc, bp);
1660 case G_MIRROR_BALANCE_PREFER:
1661 g_mirror_request_prefer(sc, bp);
1663 case G_MIRROR_BALANCE_ROUND_ROBIN:
1664 g_mirror_request_round_robin(sc, bp);
1666 case G_MIRROR_BALANCE_SPLIT:
1667 g_mirror_request_split(sc, bp);
1674 struct g_mirror_disk *disk;
1675 struct g_mirror_disk_sync *sync;
1676 struct bio_queue_head queue;
1677 struct g_consumer *cp;
1681 * Delay the request if it is colliding with a synchronization
1684 if (g_mirror_sync_collision(sc, bp)) {
1685 g_mirror_regular_delay(sc, bp);
1690 g_mirror_unidle(sc);
1692 sc->sc_last_write = time_uptime;
1695 * Allocate all bios before sending any request, so we can
1696 * return ENOMEM in nice and clean way.
1699 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1700 sync = &disk->d_sync;
1701 switch (disk->d_state) {
1702 case G_MIRROR_DISK_STATE_ACTIVE:
1704 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1705 if (bp->bio_offset >= sync->ds_offset)
1711 if (bp->bio_cmd == BIO_DELETE &&
1712 (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE) == 0)
1714 cbp = g_clone_bio(bp);
1716 while ((cbp = bioq_takefirst(&queue)) != NULL)
1718 if (bp->bio_error == 0)
1719 bp->bio_error = ENOMEM;
1720 g_io_deliver(bp, bp->bio_error);
1723 bioq_insert_tail(&queue, cbp);
1724 cbp->bio_done = g_mirror_done;
1725 cp = disk->d_consumer;
1726 cbp->bio_caller1 = cp;
1727 cbp->bio_to = cp->provider;
1728 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1729 ("Consumer %s not opened (r%dw%de%d).",
1730 cp->provider->name, cp->acr, cp->acw, cp->ace));
1732 if (bioq_first(&queue) == NULL) {
1733 g_io_deliver(bp, EOPNOTSUPP);
1736 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1737 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1738 cp = cbp->bio_caller1;
1739 cbp->bio_caller1 = NULL;
1742 g_io_request(cbp, cp);
1745 * Put request onto inflight queue, so we can check if new
1746 * synchronization requests don't collide with it.
1748 bioq_insert_tail(&sc->sc_inflight, bp);
1750 * Bump syncid on first write.
1752 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1753 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1754 g_mirror_bump_syncid(sc);
1759 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1760 bp->bio_cmd, sc->sc_name));
1766 g_mirror_can_destroy(struct g_mirror_softc *sc)
1769 struct g_consumer *cp;
1771 g_topology_assert();
1773 if (gp->softc == NULL)
1775 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_TASTING) != 0)
1777 LIST_FOREACH(cp, &gp->consumer, consumer) {
1778 if (g_mirror_is_busy(sc, cp))
1781 gp = sc->sc_sync.ds_geom;
1782 LIST_FOREACH(cp, &gp->consumer, consumer) {
1783 if (g_mirror_is_busy(sc, cp))
1786 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1792 g_mirror_try_destroy(struct g_mirror_softc *sc)
1795 if (sc->sc_rootmount != NULL) {
1796 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1798 root_mount_rel(sc->sc_rootmount);
1799 sc->sc_rootmount = NULL;
1802 if (!g_mirror_can_destroy(sc)) {
1803 g_topology_unlock();
1806 sc->sc_geom->softc = NULL;
1807 sc->sc_sync.ds_geom->softc = NULL;
1808 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WAIT) != 0) {
1809 g_topology_unlock();
1810 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1812 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
1813 sx_xunlock(&sc->sc_lock);
1814 wakeup(&sc->sc_worker);
1815 sc->sc_worker = NULL;
1817 g_topology_unlock();
1818 g_mirror_destroy_device(sc);
1827 g_mirror_worker(void *arg)
1829 struct g_mirror_softc *sc;
1830 struct g_mirror_event *ep;
1835 thread_lock(curthread);
1836 sched_prio(curthread, PRIBIO);
1837 thread_unlock(curthread);
1839 sx_xlock(&sc->sc_lock);
1841 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1843 * First take a look at events.
1844 * This is important to handle events before any I/O requests.
1846 ep = g_mirror_event_get(sc);
1848 g_mirror_event_remove(sc, ep);
1849 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1850 /* Update only device status. */
1852 "Running event for device %s.",
1855 g_mirror_update_device(sc, 1);
1857 /* Update disk status. */
1858 G_MIRROR_DEBUG(3, "Running event for disk %s.",
1859 g_mirror_get_diskname(ep->e_disk));
1860 ep->e_error = g_mirror_update_disk(ep->e_disk,
1862 if (ep->e_error == 0)
1863 g_mirror_update_device(sc, 0);
1865 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1866 KASSERT(ep->e_error == 0,
1867 ("Error cannot be handled."));
1868 g_mirror_event_free(ep);
1870 ep->e_flags |= G_MIRROR_EVENT_DONE;
1871 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1873 mtx_lock(&sc->sc_events_mtx);
1875 mtx_unlock(&sc->sc_events_mtx);
1878 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1879 if (g_mirror_try_destroy(sc)) {
1880 curthread->td_pflags &= ~TDP_GEOM;
1881 G_MIRROR_DEBUG(1, "Thread exiting.");
1885 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1889 * Check if we can mark array as CLEAN and if we can't take
1890 * how much seconds should we wait.
1892 timeout = g_mirror_idle(sc, -1);
1896 /* Get first request from the queue. */
1897 mtx_lock(&sc->sc_queue_mtx);
1898 bp = bioq_takefirst(&sc->sc_queue);
1901 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1902 mtx_unlock(&sc->sc_queue_mtx);
1903 if (g_mirror_try_destroy(sc)) {
1904 curthread->td_pflags &= ~TDP_GEOM;
1905 G_MIRROR_DEBUG(1, "Thread exiting.");
1908 mtx_lock(&sc->sc_queue_mtx);
1910 sx_xunlock(&sc->sc_lock);
1912 * XXX: We can miss an event here, because an event
1913 * can be added without sx-device-lock and without
1914 * mtx-queue-lock. Maybe I should just stop using
1915 * dedicated mutex for events synchronization and
1916 * stick with the queue lock?
1917 * The event will hang here until next I/O request
1918 * or next event is received.
1920 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
1922 sx_xlock(&sc->sc_lock);
1923 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
1926 mtx_unlock(&sc->sc_queue_mtx);
1928 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
1929 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
1930 g_mirror_sync_request(bp); /* READ */
1931 } else if (bp->bio_to != sc->sc_provider) {
1932 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
1933 g_mirror_regular_request(bp);
1934 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
1935 g_mirror_sync_request(bp); /* WRITE */
1938 ("Invalid request cflags=0x%hhx to=%s.",
1939 bp->bio_cflags, bp->bio_to->name));
1942 g_mirror_register_request(bp);
1944 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
1949 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
1952 sx_assert(&sc->sc_lock, SX_LOCKED);
1954 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
1956 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
1957 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
1958 g_mirror_get_diskname(disk), sc->sc_name);
1959 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1960 } else if (sc->sc_idle &&
1961 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
1962 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
1963 g_mirror_get_diskname(disk), sc->sc_name);
1964 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
1969 g_mirror_sync_start(struct g_mirror_disk *disk)
1971 struct g_mirror_softc *sc;
1972 struct g_consumer *cp;
1976 g_topology_assert_not();
1978 sx_assert(&sc->sc_lock, SX_LOCKED);
1980 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1981 ("Disk %s is not marked for synchronization.",
1982 g_mirror_get_diskname(disk)));
1983 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1984 ("Device not in RUNNING state (%s, %u).", sc->sc_name,
1987 sx_xunlock(&sc->sc_lock);
1989 cp = g_new_consumer(sc->sc_sync.ds_geom);
1990 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1991 error = g_attach(cp, sc->sc_provider);
1993 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
1994 error = g_access(cp, 1, 0, 0);
1995 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
1996 g_topology_unlock();
1997 sx_xlock(&sc->sc_lock);
1999 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
2000 g_mirror_get_diskname(disk));
2001 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
2002 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
2003 KASSERT(disk->d_sync.ds_consumer == NULL,
2004 ("Sync consumer already exists (device=%s, disk=%s).",
2005 sc->sc_name, g_mirror_get_diskname(disk)));
2007 disk->d_sync.ds_consumer = cp;
2008 disk->d_sync.ds_consumer->private = disk;
2009 disk->d_sync.ds_consumer->index = 0;
2012 * Allocate memory for synchronization bios and initialize them.
2014 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
2015 M_MIRROR, M_WAITOK);
2016 for (i = 0; i < g_mirror_syncreqs; i++) {
2018 disk->d_sync.ds_bios[i] = bp;
2019 bp->bio_parent = NULL;
2020 bp->bio_cmd = BIO_READ;
2021 bp->bio_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK);
2023 bp->bio_offset = disk->d_sync.ds_offset;
2024 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
2025 disk->d_sync.ds_offset += bp->bio_length;
2026 bp->bio_done = g_mirror_sync_done;
2027 bp->bio_from = disk->d_sync.ds_consumer;
2028 bp->bio_to = sc->sc_provider;
2029 bp->bio_caller1 = (void *)(uintptr_t)i;
2032 /* Increase the number of disks in SYNCHRONIZING state. */
2033 sc->sc_sync.ds_ndisks++;
2034 /* Set the number of in-flight synchronization requests. */
2035 disk->d_sync.ds_inflight = g_mirror_syncreqs;
2038 * Fire off first synchronization requests.
2040 for (i = 0; i < g_mirror_syncreqs; i++) {
2041 bp = disk->d_sync.ds_bios[i];
2042 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
2043 disk->d_sync.ds_consumer->index++;
2045 * Delay the request if it is colliding with a regular request.
2047 if (g_mirror_regular_collision(sc, bp))
2048 g_mirror_sync_delay(sc, bp);
2050 g_io_request(bp, disk->d_sync.ds_consumer);
2055 * Stop synchronization process.
2056 * type: 0 - synchronization finished
2057 * 1 - synchronization stopped
2060 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
2062 struct g_mirror_softc *sc;
2063 struct g_consumer *cp;
2065 g_topology_assert_not();
2067 sx_assert(&sc->sc_lock, SX_LOCKED);
2069 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2070 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2071 g_mirror_disk_state2str(disk->d_state)));
2072 if (disk->d_sync.ds_consumer == NULL)
2076 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2077 sc->sc_name, g_mirror_get_diskname(disk));
2078 } else /* if (type == 1) */ {
2079 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2080 sc->sc_name, g_mirror_get_diskname(disk));
2082 free(disk->d_sync.ds_bios, M_MIRROR);
2083 disk->d_sync.ds_bios = NULL;
2084 cp = disk->d_sync.ds_consumer;
2085 disk->d_sync.ds_consumer = NULL;
2086 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2087 sc->sc_sync.ds_ndisks--;
2088 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2090 g_mirror_kill_consumer(sc, cp);
2091 g_topology_unlock();
2092 sx_xlock(&sc->sc_lock);
2096 g_mirror_launch_provider(struct g_mirror_softc *sc)
2098 struct g_mirror_disk *disk;
2099 struct g_provider *pp, *dp;
2101 sx_assert(&sc->sc_lock, SX_LOCKED);
2104 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2105 pp->flags |= G_PF_DIRECT_RECEIVE;
2106 pp->mediasize = sc->sc_mediasize;
2107 pp->sectorsize = sc->sc_sectorsize;
2109 pp->stripeoffset = 0;
2111 /* Splitting of unmapped BIO's could work but isn't implemented now */
2112 if (sc->sc_balance != G_MIRROR_BALANCE_SPLIT)
2113 pp->flags |= G_PF_ACCEPT_UNMAPPED;
2115 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2116 if (disk->d_consumer && disk->d_consumer->provider) {
2117 dp = disk->d_consumer->provider;
2118 if (dp->stripesize > pp->stripesize) {
2119 pp->stripesize = dp->stripesize;
2120 pp->stripeoffset = dp->stripeoffset;
2122 /* A provider underneath us doesn't support unmapped */
2123 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
2124 G_MIRROR_DEBUG(0, "Cancelling unmapped "
2125 "because of %s.", dp->name);
2126 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
2132 sc->sc_provider = pp;
2133 g_error_provider(pp, 0);
2134 g_topology_unlock();
2135 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2136 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2137 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2138 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2139 g_mirror_sync_start(disk);
2144 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2146 struct g_mirror_disk *disk;
2149 g_topology_assert_not();
2150 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2154 g_error_provider(sc->sc_provider, ENXIO);
2155 mtx_lock(&sc->sc_queue_mtx);
2156 while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) {
2158 * Abort any pending I/O that wasn't generated by us.
2159 * Synchronization requests and requests destined for individual
2160 * mirror components can be destroyed immediately.
2162 if (bp->bio_to == sc->sc_provider &&
2163 bp->bio_from->geom != sc->sc_sync.ds_geom) {
2164 g_io_deliver(bp, ENXIO);
2166 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
2167 free(bp->bio_data, M_MIRROR);
2171 mtx_unlock(&sc->sc_queue_mtx);
2172 g_wither_provider(sc->sc_provider, ENXIO);
2173 sc->sc_provider = NULL;
2174 G_MIRROR_DEBUG(0, "Device %s: provider destroyed.", sc->sc_name);
2175 g_topology_unlock();
2176 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2177 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2178 g_mirror_sync_stop(disk, 1);
2183 g_mirror_go(void *arg)
2185 struct g_mirror_softc *sc;
2188 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2189 g_mirror_event_send(sc, 0,
2190 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2194 g_mirror_determine_state(struct g_mirror_disk *disk)
2196 struct g_mirror_softc *sc;
2200 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2201 if ((disk->d_flags &
2202 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2203 /* Disk does not need synchronization. */
2204 state = G_MIRROR_DISK_STATE_ACTIVE;
2207 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2209 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2211 * We can start synchronization from
2212 * the stored offset.
2214 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2216 state = G_MIRROR_DISK_STATE_STALE;
2219 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2221 * Reset all synchronization data for this disk,
2222 * because if it even was synchronized, it was
2223 * synchronized to disks with different syncid.
2225 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2226 disk->d_sync.ds_offset = 0;
2227 disk->d_sync.ds_offset_done = 0;
2228 disk->d_sync.ds_syncid = sc->sc_syncid;
2229 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2230 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2231 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2233 state = G_MIRROR_DISK_STATE_STALE;
2235 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2237 * Not good, NOT GOOD!
2238 * It means that mirror was started on stale disks
2239 * and more fresh disk just arrive.
2240 * If there were writes, mirror is broken, sorry.
2241 * I think the best choice here is don't touch
2242 * this disk and inform the user loudly.
2244 G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2245 "disk (%s) arrives!! It will not be connected to the "
2246 "running device.", sc->sc_name,
2247 g_mirror_get_diskname(disk));
2248 g_mirror_destroy_disk(disk);
2249 state = G_MIRROR_DISK_STATE_NONE;
2250 /* Return immediately, because disk was destroyed. */
2253 G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2254 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2259 * Update device state.
2262 g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force)
2264 struct g_mirror_disk *disk;
2267 sx_assert(&sc->sc_lock, SX_XLOCKED);
2269 switch (sc->sc_state) {
2270 case G_MIRROR_DEVICE_STATE_STARTING:
2272 struct g_mirror_disk *pdisk, *tdisk;
2273 u_int dirty, ndisks, genid, syncid;
2275 KASSERT(sc->sc_provider == NULL,
2276 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2278 * Are we ready? We are, if all disks are connected or
2279 * if we have any disks and 'force' is true.
2281 ndisks = g_mirror_ndisks(sc, -1);
2282 if (sc->sc_ndisks == ndisks || (force && ndisks > 0)) {
2284 } else if (ndisks == 0) {
2286 * Disks went down in starting phase, so destroy
2289 callout_drain(&sc->sc_callout);
2290 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2291 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2293 root_mount_rel(sc->sc_rootmount);
2294 sc->sc_rootmount = NULL;
2301 * Activate all disks with the biggest syncid.
2305 * If 'force' is true, we have been called due to
2306 * timeout, so don't bother canceling timeout.
2309 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2310 if ((disk->d_flags &
2311 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2316 /* No valid disks found, destroy device. */
2317 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2318 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2319 __LINE__, sc->sc_rootmount);
2320 root_mount_rel(sc->sc_rootmount);
2321 sc->sc_rootmount = NULL;
2325 /* Cancel timeout. */
2326 callout_drain(&sc->sc_callout);
2330 * Find the biggest genid.
2333 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2334 if (disk->d_genid > genid)
2335 genid = disk->d_genid;
2337 sc->sc_genid = genid;
2339 * Remove all disks without the biggest genid.
2341 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2342 if (disk->d_genid < genid) {
2344 "Component %s (device %s) broken, skipping.",
2345 g_mirror_get_diskname(disk), sc->sc_name);
2346 g_mirror_destroy_disk(disk);
2351 * Find the biggest syncid.
2354 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2355 if (disk->d_sync.ds_syncid > syncid)
2356 syncid = disk->d_sync.ds_syncid;
2360 * Here we need to look for dirty disks and if all disks
2361 * with the biggest syncid are dirty, we have to choose
2362 * one with the biggest priority and rebuild the rest.
2365 * Find the number of dirty disks with the biggest syncid.
2366 * Find the number of disks with the biggest syncid.
2367 * While here, find a disk with the biggest priority.
2371 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2372 if (disk->d_sync.ds_syncid != syncid)
2374 if ((disk->d_flags &
2375 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2379 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2381 if (pdisk == NULL ||
2382 pdisk->d_priority < disk->d_priority) {
2388 /* No dirty disks at all, great. */
2389 } else if (dirty == ndisks) {
2391 * Force synchronization for all dirty disks except one
2392 * with the biggest priority.
2394 KASSERT(pdisk != NULL, ("pdisk == NULL"));
2395 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2396 "master disk for synchronization.",
2397 g_mirror_get_diskname(pdisk), sc->sc_name);
2398 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2399 if (disk->d_sync.ds_syncid != syncid)
2401 if ((disk->d_flags &
2402 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2405 KASSERT((disk->d_flags &
2406 G_MIRROR_DISK_FLAG_DIRTY) != 0,
2407 ("Disk %s isn't marked as dirty.",
2408 g_mirror_get_diskname(disk)));
2409 /* Skip the disk with the biggest priority. */
2412 disk->d_sync.ds_syncid = 0;
2414 } else if (dirty < ndisks) {
2416 * Force synchronization for all dirty disks.
2417 * We have some non-dirty disks.
2419 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2420 if (disk->d_sync.ds_syncid != syncid)
2422 if ((disk->d_flags &
2423 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2426 if ((disk->d_flags &
2427 G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2430 disk->d_sync.ds_syncid = 0;
2436 sc->sc_syncid = syncid;
2438 /* Remember to bump syncid on first write. */
2439 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2441 state = G_MIRROR_DEVICE_STATE_RUNNING;
2442 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2443 sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2444 g_mirror_device_state2str(state));
2445 sc->sc_state = state;
2446 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2447 state = g_mirror_determine_state(disk);
2448 g_mirror_event_send(disk, state,
2449 G_MIRROR_EVENT_DONTWAIT);
2450 if (state == G_MIRROR_DISK_STATE_STALE)
2451 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2455 case G_MIRROR_DEVICE_STATE_RUNNING:
2456 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2457 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2459 * No active disks or no disks at all,
2460 * so destroy device.
2462 if (sc->sc_provider != NULL)
2463 g_mirror_destroy_provider(sc);
2464 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2466 } else if (g_mirror_ndisks(sc,
2467 G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2468 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2470 * We have active disks, launch provider if it doesn't
2473 if (sc->sc_provider == NULL)
2474 g_mirror_launch_provider(sc);
2475 if (sc->sc_rootmount != NULL) {
2476 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2477 __LINE__, sc->sc_rootmount);
2478 root_mount_rel(sc->sc_rootmount);
2479 sc->sc_rootmount = NULL;
2483 * Genid should be bumped immediately, so do it here.
2485 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2486 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2487 g_mirror_bump_genid(sc);
2489 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID_NOW) != 0) {
2490 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID_NOW;
2491 g_mirror_bump_syncid(sc);
2495 KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2496 sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2502 * Update disk state and device state if needed.
2504 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \
2505 "Disk %s state changed from %s to %s (device %s).", \
2506 g_mirror_get_diskname(disk), \
2507 g_mirror_disk_state2str(disk->d_state), \
2508 g_mirror_disk_state2str(state), sc->sc_name)
2510 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2512 struct g_mirror_softc *sc;
2515 sx_assert(&sc->sc_lock, SX_XLOCKED);
2518 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2519 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2520 g_mirror_disk_state2str(state));
2522 case G_MIRROR_DISK_STATE_NEW:
2524 * Possible scenarios:
2525 * 1. New disk arrive.
2527 /* Previous state should be NONE. */
2528 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2529 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2530 g_mirror_disk_state2str(disk->d_state)));
2531 DISK_STATE_CHANGED();
2533 disk->d_state = state;
2534 if (LIST_EMPTY(&sc->sc_disks))
2535 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2537 struct g_mirror_disk *dp;
2539 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2540 if (disk->d_priority >= dp->d_priority) {
2541 LIST_INSERT_BEFORE(dp, disk, d_next);
2545 if (LIST_NEXT(dp, d_next) == NULL)
2549 LIST_INSERT_AFTER(dp, disk, d_next);
2551 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2552 sc->sc_name, g_mirror_get_diskname(disk));
2553 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2555 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2556 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2557 g_mirror_device_state2str(sc->sc_state),
2558 g_mirror_get_diskname(disk),
2559 g_mirror_disk_state2str(disk->d_state)));
2560 state = g_mirror_determine_state(disk);
2561 if (state != G_MIRROR_DISK_STATE_NONE)
2564 case G_MIRROR_DISK_STATE_ACTIVE:
2566 * Possible scenarios:
2567 * 1. New disk does not need synchronization.
2568 * 2. Synchronization process finished successfully.
2570 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2571 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2572 g_mirror_device_state2str(sc->sc_state),
2573 g_mirror_get_diskname(disk),
2574 g_mirror_disk_state2str(disk->d_state)));
2575 /* Previous state should be NEW or SYNCHRONIZING. */
2576 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2577 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2578 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2579 g_mirror_disk_state2str(disk->d_state)));
2580 DISK_STATE_CHANGED();
2582 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2583 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2584 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2585 g_mirror_sync_stop(disk, 0);
2587 disk->d_state = state;
2588 disk->d_sync.ds_offset = 0;
2589 disk->d_sync.ds_offset_done = 0;
2590 g_mirror_update_idle(sc, disk);
2591 g_mirror_update_metadata(disk);
2592 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2593 sc->sc_name, g_mirror_get_diskname(disk));
2595 case G_MIRROR_DISK_STATE_STALE:
2597 * Possible scenarios:
2598 * 1. Stale disk was connected.
2600 /* Previous state should be NEW. */
2601 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2602 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2603 g_mirror_disk_state2str(disk->d_state)));
2604 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2605 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2606 g_mirror_device_state2str(sc->sc_state),
2607 g_mirror_get_diskname(disk),
2608 g_mirror_disk_state2str(disk->d_state)));
2610 * STALE state is only possible if device is marked
2613 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2614 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2615 g_mirror_device_state2str(sc->sc_state),
2616 g_mirror_get_diskname(disk),
2617 g_mirror_disk_state2str(disk->d_state)));
2618 DISK_STATE_CHANGED();
2620 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2621 disk->d_state = state;
2622 g_mirror_update_metadata(disk);
2623 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2624 sc->sc_name, g_mirror_get_diskname(disk));
2626 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2628 * Possible scenarios:
2629 * 1. Disk which needs synchronization was connected.
2631 /* Previous state should be NEW. */
2632 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2633 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2634 g_mirror_disk_state2str(disk->d_state)));
2635 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2636 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2637 g_mirror_device_state2str(sc->sc_state),
2638 g_mirror_get_diskname(disk),
2639 g_mirror_disk_state2str(disk->d_state)));
2640 DISK_STATE_CHANGED();
2642 if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2643 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2644 disk->d_state = state;
2645 if (sc->sc_provider != NULL) {
2646 g_mirror_sync_start(disk);
2647 g_mirror_update_metadata(disk);
2650 case G_MIRROR_DISK_STATE_DISCONNECTED:
2652 * Possible scenarios:
2653 * 1. Device wasn't running yet, but disk disappear.
2654 * 2. Disk was active and disapppear.
2655 * 3. Disk disappear during synchronization process.
2657 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2659 * Previous state should be ACTIVE, STALE or
2662 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2663 disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2664 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2665 ("Wrong disk state (%s, %s).",
2666 g_mirror_get_diskname(disk),
2667 g_mirror_disk_state2str(disk->d_state)));
2668 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2669 /* Previous state should be NEW. */
2670 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2671 ("Wrong disk state (%s, %s).",
2672 g_mirror_get_diskname(disk),
2673 g_mirror_disk_state2str(disk->d_state)));
2675 * Reset bumping syncid if disk disappeared in STARTING
2678 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2679 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2682 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2684 g_mirror_device_state2str(sc->sc_state),
2685 g_mirror_get_diskname(disk),
2686 g_mirror_disk_state2str(disk->d_state)));
2689 DISK_STATE_CHANGED();
2690 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2691 sc->sc_name, g_mirror_get_diskname(disk));
2693 g_mirror_destroy_disk(disk);
2695 case G_MIRROR_DISK_STATE_DESTROY:
2699 error = g_mirror_clear_metadata(disk);
2702 "Device %s: failed to clear metadata on %s: %d.",
2703 sc->sc_name, g_mirror_get_diskname(disk), error);
2706 DISK_STATE_CHANGED();
2707 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2708 sc->sc_name, g_mirror_get_diskname(disk));
2710 g_mirror_destroy_disk(disk);
2712 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2713 g_mirror_update_metadata(disk);
2718 KASSERT(1 == 0, ("Unknown state (%u).", state));
2723 #undef DISK_STATE_CHANGED
2726 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2728 struct g_provider *pp;
2732 g_topology_assert();
2734 error = g_access(cp, 1, 0, 0);
2738 g_topology_unlock();
2739 /* Metadata are stored on last sector. */
2740 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2743 g_access(cp, -1, 0, 0);
2745 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2746 cp->provider->name, error);
2750 /* Decode metadata. */
2751 error = mirror_metadata_decode(buf, md);
2753 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2755 if (md->md_version > G_MIRROR_VERSION) {
2757 "Kernel module is too old to handle metadata from %s.",
2758 cp->provider->name);
2762 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2763 cp->provider->name);
2771 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2772 struct g_mirror_metadata *md)
2775 if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2776 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2777 pp->name, md->md_did);
2780 if (md->md_all != sc->sc_ndisks) {
2782 "Invalid '%s' field on disk %s (device %s), skipping.",
2783 "md_all", pp->name, sc->sc_name);
2786 if (md->md_slice != sc->sc_slice) {
2788 "Invalid '%s' field on disk %s (device %s), skipping.",
2789 "md_slice", pp->name, sc->sc_name);
2792 if (md->md_balance != sc->sc_balance) {
2794 "Invalid '%s' field on disk %s (device %s), skipping.",
2795 "md_balance", pp->name, sc->sc_name);
2799 if (md->md_mediasize != sc->sc_mediasize) {
2801 "Invalid '%s' field on disk %s (device %s), skipping.",
2802 "md_mediasize", pp->name, sc->sc_name);
2806 if (sc->sc_mediasize > pp->mediasize) {
2808 "Invalid size of disk %s (device %s), skipping.", pp->name,
2812 if (md->md_sectorsize != sc->sc_sectorsize) {
2814 "Invalid '%s' field on disk %s (device %s), skipping.",
2815 "md_sectorsize", pp->name, sc->sc_name);
2818 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2820 "Invalid sector size of disk %s (device %s), skipping.",
2821 pp->name, sc->sc_name);
2824 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2826 "Invalid device flags on disk %s (device %s), skipping.",
2827 pp->name, sc->sc_name);
2830 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2832 "Invalid disk flags on disk %s (device %s), skipping.",
2833 pp->name, sc->sc_name);
2840 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2841 struct g_mirror_metadata *md)
2843 struct g_mirror_disk *disk;
2846 g_topology_assert_not();
2847 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2849 error = g_mirror_check_metadata(sc, pp, md);
2852 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING &&
2853 md->md_genid < sc->sc_genid) {
2854 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
2855 pp->name, sc->sc_name);
2858 disk = g_mirror_init_disk(sc, pp, md, &error);
2861 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
2862 G_MIRROR_EVENT_WAIT);
2865 if (md->md_version < G_MIRROR_VERSION) {
2866 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
2867 pp->name, md->md_version, G_MIRROR_VERSION);
2868 g_mirror_update_metadata(disk);
2874 g_mirror_destroy_delayed(void *arg, int flag)
2876 struct g_mirror_softc *sc;
2879 if (flag == EV_CANCEL) {
2880 G_MIRROR_DEBUG(1, "Destroying canceled.");
2884 g_topology_unlock();
2885 sx_xlock(&sc->sc_lock);
2886 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
2887 ("DESTROY flag set on %s.", sc->sc_name));
2888 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0,
2889 ("DESTROYING flag not set on %s.", sc->sc_name));
2890 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
2891 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
2893 G_MIRROR_DEBUG(0, "Cannot destroy %s (error=%d).",
2894 sc->sc_name, error);
2895 sx_xunlock(&sc->sc_lock);
2901 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
2903 struct g_mirror_softc *sc;
2906 g_topology_assert();
2907 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2911 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
2913 g_topology_unlock();
2914 sx_xlock(&sc->sc_lock);
2915 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
2916 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0 ||
2917 LIST_EMPTY(&sc->sc_disks)) {
2918 if (acr > 0 || acw > 0 || ace > 0)
2922 sc->sc_provider_open += acr + acw + ace;
2923 if (pp->acw + acw == 0)
2924 g_mirror_idle(sc, 0);
2925 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0 &&
2926 sc->sc_provider_open == 0)
2927 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK, sc, NULL);
2929 sx_xunlock(&sc->sc_lock);
2934 static struct g_geom *
2935 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
2937 struct g_mirror_softc *sc;
2941 g_topology_assert();
2942 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
2945 /* One disk is minimum. */
2951 gp = g_new_geomf(mp, "%s", md->md_name);
2952 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
2953 gp->start = g_mirror_start;
2954 gp->orphan = g_mirror_orphan;
2955 gp->access = g_mirror_access;
2956 gp->dumpconf = g_mirror_dumpconf;
2958 sc->sc_id = md->md_mid;
2959 sc->sc_slice = md->md_slice;
2960 sc->sc_balance = md->md_balance;
2961 sc->sc_mediasize = md->md_mediasize;
2962 sc->sc_sectorsize = md->md_sectorsize;
2963 sc->sc_ndisks = md->md_all;
2964 sc->sc_flags = md->md_mflags;
2967 sc->sc_last_write = time_uptime;
2970 sx_init(&sc->sc_lock, "gmirror:lock");
2971 bioq_init(&sc->sc_queue);
2972 mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
2973 bioq_init(&sc->sc_regular_delayed);
2974 bioq_init(&sc->sc_inflight);
2975 bioq_init(&sc->sc_sync_delayed);
2976 LIST_INIT(&sc->sc_disks);
2977 TAILQ_INIT(&sc->sc_events);
2978 mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
2979 callout_init(&sc->sc_callout, 1);
2980 mtx_init(&sc->sc_done_mtx, "gmirror:done", NULL, MTX_DEF);
2981 sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
2984 sc->sc_provider = NULL;
2985 sc->sc_provider_open = 0;
2987 * Synchronization geom.
2989 gp = g_new_geomf(mp, "%s.sync", md->md_name);
2991 gp->orphan = g_mirror_orphan;
2992 sc->sc_sync.ds_geom = gp;
2993 sc->sc_sync.ds_ndisks = 0;
2994 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
2995 "g_mirror %s", md->md_name);
2997 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
2999 g_destroy_geom(sc->sc_sync.ds_geom);
3000 g_destroy_geom(sc->sc_geom);
3001 g_mirror_free_device(sc);
3005 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
3006 sc->sc_name, sc->sc_ndisks, sc->sc_id);
3008 sc->sc_rootmount = root_mount_hold("GMIRROR");
3009 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
3013 timeout = g_mirror_timeout * hz;
3014 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
3015 return (sc->sc_geom);
3019 g_mirror_destroy(struct g_mirror_softc *sc, int how)
3021 struct g_mirror_disk *disk;
3023 g_topology_assert_not();
3024 sx_assert(&sc->sc_lock, SX_XLOCKED);
3026 if (sc->sc_provider_open != 0) {
3028 case G_MIRROR_DESTROY_SOFT:
3030 "Device %s is still open (%d).", sc->sc_name,
3031 sc->sc_provider_open);
3033 case G_MIRROR_DESTROY_DELAYED:
3035 "Device %s will be destroyed on last close.",
3037 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
3038 if (disk->d_state ==
3039 G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3040 g_mirror_sync_stop(disk, 1);
3043 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROYING;
3045 case G_MIRROR_DESTROY_HARD:
3046 G_MIRROR_DEBUG(1, "Device %s is still open, so it "
3047 "can't be definitely removed.", sc->sc_name);
3052 if (sc->sc_geom->softc == NULL) {
3053 g_topology_unlock();
3056 sc->sc_geom->softc = NULL;
3057 sc->sc_sync.ds_geom->softc = NULL;
3058 g_topology_unlock();
3060 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
3061 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WAIT;
3062 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3063 sx_xunlock(&sc->sc_lock);
3064 mtx_lock(&sc->sc_queue_mtx);
3066 mtx_unlock(&sc->sc_queue_mtx);
3067 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3068 while (sc->sc_worker != NULL)
3069 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
3070 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3071 sx_xlock(&sc->sc_lock);
3072 g_mirror_destroy_device(sc);
3077 g_mirror_taste_orphan(struct g_consumer *cp)
3080 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3081 cp->provider->name));
3084 static struct g_geom *
3085 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3087 struct g_mirror_metadata md;
3088 struct g_mirror_softc *sc;
3089 struct g_consumer *cp;
3093 g_topology_assert();
3094 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3095 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
3097 gp = g_new_geomf(mp, "mirror:taste");
3099 * This orphan function should be never called.
3101 gp->orphan = g_mirror_taste_orphan;
3102 cp = g_new_consumer(gp);
3104 error = g_mirror_read_metadata(cp, &md);
3106 g_destroy_consumer(cp);
3112 if (md.md_provider[0] != '\0' &&
3113 !g_compare_names(md.md_provider, pp->name))
3115 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3117 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3119 "Device %s: provider %s marked as inactive, skipping.",
3120 md.md_name, pp->name);
3123 if (g_mirror_debug >= 2)
3124 mirror_metadata_dump(&md);
3127 * Let's check if device already exists.
3130 LIST_FOREACH(gp, &mp->geom, geom) {
3134 if (sc->sc_sync.ds_geom == gp)
3136 if (strcmp(md.md_name, sc->sc_name) != 0)
3138 if (md.md_mid != sc->sc_id) {
3139 G_MIRROR_DEBUG(0, "Device %s already configured.",
3146 gp = g_mirror_create(mp, &md);
3148 G_MIRROR_DEBUG(0, "Cannot create device %s.",
3154 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3155 g_topology_unlock();
3156 sx_xlock(&sc->sc_lock);
3157 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
3158 error = g_mirror_add_disk(sc, pp, &md);
3160 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3161 pp->name, gp->name, error);
3162 if (LIST_EMPTY(&sc->sc_disks)) {
3164 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3170 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
3171 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3172 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3176 sx_xunlock(&sc->sc_lock);
3182 g_mirror_resize(struct g_consumer *cp)
3184 struct g_mirror_disk *disk;
3186 g_topology_assert();
3187 g_trace(G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name);
3192 g_topology_unlock();
3193 g_mirror_update_metadata(disk);
3198 g_mirror_destroy_geom(struct gctl_req *req __unused,
3199 struct g_class *mp __unused, struct g_geom *gp)
3201 struct g_mirror_softc *sc;
3204 g_topology_unlock();
3206 sx_xlock(&sc->sc_lock);
3208 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3210 sx_xunlock(&sc->sc_lock);
3216 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3217 struct g_consumer *cp, struct g_provider *pp)
3219 struct g_mirror_softc *sc;
3221 g_topology_assert();
3226 /* Skip synchronization geom. */
3227 if (gp == sc->sc_sync.ds_geom)
3231 } else if (cp != NULL) {
3232 struct g_mirror_disk *disk;
3237 g_topology_unlock();
3238 sx_xlock(&sc->sc_lock);
3239 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3240 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3241 sbuf_printf(sb, "%s<Synchronized>", indent);
3242 if (disk->d_sync.ds_offset == 0)
3243 sbuf_printf(sb, "0%%");
3245 sbuf_printf(sb, "%u%%",
3246 (u_int)((disk->d_sync.ds_offset * 100) /
3247 sc->sc_provider->mediasize));
3249 sbuf_printf(sb, "</Synchronized>\n");
3250 if (disk->d_sync.ds_offset > 0) {
3251 sbuf_printf(sb, "%s<BytesSynced>%jd"
3252 "</BytesSynced>\n", indent,
3253 (intmax_t)disk->d_sync.ds_offset);
3256 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3257 disk->d_sync.ds_syncid);
3258 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3260 sbuf_printf(sb, "%s<Flags>", indent);
3261 if (disk->d_flags == 0)
3262 sbuf_printf(sb, "NONE");
3266 #define ADD_FLAG(flag, name) do { \
3267 if ((disk->d_flags & (flag)) != 0) { \
3269 sbuf_printf(sb, ", "); \
3272 sbuf_printf(sb, name); \
3275 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3276 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3277 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3278 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3280 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3281 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3284 sbuf_printf(sb, "</Flags>\n");
3285 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3287 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3288 g_mirror_disk_state2str(disk->d_state));
3289 sx_xunlock(&sc->sc_lock);
3292 g_topology_unlock();
3293 sx_xlock(&sc->sc_lock);
3294 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3295 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3296 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3297 sbuf_printf(sb, "%s<Flags>", indent);
3298 if (sc->sc_flags == 0)
3299 sbuf_printf(sb, "NONE");
3303 #define ADD_FLAG(flag, name) do { \
3304 if ((sc->sc_flags & (flag)) != 0) { \
3306 sbuf_printf(sb, ", "); \
3309 sbuf_printf(sb, name); \
3312 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3313 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3316 sbuf_printf(sb, "</Flags>\n");
3317 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3318 (u_int)sc->sc_slice);
3319 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3320 balance_name(sc->sc_balance));
3321 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3323 sbuf_printf(sb, "%s<State>", indent);
3324 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3325 sbuf_printf(sb, "%s", "STARTING");
3326 else if (sc->sc_ndisks ==
3327 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3328 sbuf_printf(sb, "%s", "COMPLETE");
3330 sbuf_printf(sb, "%s", "DEGRADED");
3331 sbuf_printf(sb, "</State>\n");
3332 sx_xunlock(&sc->sc_lock);
3338 g_mirror_shutdown_post_sync(void *arg, int howto)
3341 struct g_geom *gp, *gp2;
3342 struct g_mirror_softc *sc;
3348 g_mirror_shutdown = 1;
3349 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3350 if ((sc = gp->softc) == NULL)
3352 /* Skip synchronization geom. */
3353 if (gp == sc->sc_sync.ds_geom)
3355 g_topology_unlock();
3356 sx_xlock(&sc->sc_lock);
3357 g_mirror_idle(sc, -1);
3359 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3361 sx_xunlock(&sc->sc_lock);
3364 g_topology_unlock();
3369 g_mirror_init(struct g_class *mp)
3372 g_mirror_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
3373 g_mirror_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
3374 if (g_mirror_post_sync == NULL)
3375 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3379 g_mirror_fini(struct g_class *mp)
3382 if (g_mirror_post_sync != NULL)
3383 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_mirror_post_sync);
3386 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);