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 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
979 g_mirror_event_send(disk,
980 G_MIRROR_DISK_STATE_DISCONNECTED,
981 G_MIRROR_EVENT_DONTWAIT);
984 switch (pbp->bio_cmd) {
994 switch (pbp->bio_cmd) {
996 if (pbp->bio_inbed < pbp->bio_children)
998 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1)
999 g_io_deliver(pbp, pbp->bio_error);
1002 mtx_lock(&sc->sc_queue_mtx);
1003 bioq_insert_tail(&sc->sc_queue, pbp);
1004 mtx_unlock(&sc->sc_queue_mtx);
1005 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1011 if (pbp->bio_children == 0) {
1013 * All requests failed.
1015 } else if (pbp->bio_inbed < pbp->bio_children) {
1018 } else if (pbp->bio_children == pbp->bio_inbed) {
1019 /* Some requests succeeded. */
1021 pbp->bio_completed = pbp->bio_length;
1023 bioq_remove(&sc->sc_inflight, pbp);
1024 /* Release delayed sync requests if possible. */
1025 g_mirror_sync_release(sc);
1026 g_io_deliver(pbp, pbp->bio_error);
1029 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
1035 g_mirror_sync_done(struct bio *bp)
1037 struct g_mirror_softc *sc;
1039 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
1040 sc = bp->bio_from->geom->softc;
1041 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
1042 mtx_lock(&sc->sc_queue_mtx);
1043 bioq_insert_tail(&sc->sc_queue, bp);
1044 mtx_unlock(&sc->sc_queue_mtx);
1049 g_mirror_candelete(struct bio *bp)
1051 struct g_mirror_softc *sc;
1052 struct g_mirror_disk *disk;
1055 sc = bp->bio_to->private;
1056 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1057 if (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE)
1060 val = (int *)bp->bio_data;
1061 *val = (disk != NULL);
1062 g_io_deliver(bp, 0);
1066 g_mirror_kernel_dump(struct bio *bp)
1068 struct g_mirror_softc *sc;
1069 struct g_mirror_disk *disk;
1071 struct g_kerneldump *gkd;
1074 * We configure dumping to the first component, because this component
1075 * will be used for reading with 'prefer' balance algorithm.
1076 * If the component with the higest priority is currently disconnected
1077 * we will not be able to read the dump after the reboot if it will be
1078 * connected and synchronized later. Can we do something better?
1080 sc = bp->bio_to->private;
1081 disk = LIST_FIRST(&sc->sc_disks);
1083 gkd = (struct g_kerneldump *)bp->bio_data;
1084 if (gkd->length > bp->bio_to->mediasize)
1085 gkd->length = bp->bio_to->mediasize;
1086 cbp = g_clone_bio(bp);
1088 g_io_deliver(bp, ENOMEM);
1091 cbp->bio_done = g_std_done;
1092 g_io_request(cbp, disk->d_consumer);
1093 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1094 g_mirror_get_diskname(disk));
1098 g_mirror_flush(struct g_mirror_softc *sc, struct bio *bp)
1100 struct bio_queue_head queue;
1101 struct g_mirror_disk *disk;
1102 struct g_consumer *cp;
1106 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1107 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1109 cbp = g_clone_bio(bp);
1111 while ((cbp = bioq_takefirst(&queue)) != NULL)
1113 if (bp->bio_error == 0)
1114 bp->bio_error = ENOMEM;
1115 g_io_deliver(bp, bp->bio_error);
1118 bioq_insert_tail(&queue, cbp);
1119 cbp->bio_done = g_mirror_flush_done;
1120 cbp->bio_caller1 = disk;
1121 cbp->bio_to = disk->d_consumer->provider;
1123 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1124 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1125 disk = cbp->bio_caller1;
1126 cbp->bio_caller1 = NULL;
1127 cp = disk->d_consumer;
1128 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1129 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1130 cp->acr, cp->acw, cp->ace));
1131 g_io_request(cbp, disk->d_consumer);
1136 g_mirror_start(struct bio *bp)
1138 struct g_mirror_softc *sc;
1140 sc = bp->bio_to->private;
1142 * If sc == NULL or there are no valid disks, provider's error
1143 * should be set and g_mirror_start() should not be called at all.
1145 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1146 ("Provider's error should be set (error=%d)(mirror=%s).",
1147 bp->bio_to->error, bp->bio_to->name));
1148 G_MIRROR_LOGREQ(3, bp, "Request received.");
1150 switch (bp->bio_cmd) {
1156 g_mirror_flush(sc, bp);
1159 if (!strcmp(bp->bio_attribute, "GEOM::candelete")) {
1160 g_mirror_candelete(bp);
1162 } else if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1163 g_mirror_kernel_dump(bp);
1168 g_io_deliver(bp, EOPNOTSUPP);
1171 mtx_lock(&sc->sc_queue_mtx);
1172 bioq_insert_tail(&sc->sc_queue, bp);
1173 mtx_unlock(&sc->sc_queue_mtx);
1174 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1179 * Return TRUE if the given request is colliding with a in-progress
1180 * synchronization request.
1183 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1185 struct g_mirror_disk *disk;
1187 off_t rstart, rend, sstart, send;
1190 if (sc->sc_sync.ds_ndisks == 0)
1192 rstart = bp->bio_offset;
1193 rend = bp->bio_offset + bp->bio_length;
1194 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1195 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1197 for (i = 0; i < g_mirror_syncreqs; i++) {
1198 sbp = disk->d_sync.ds_bios[i];
1201 sstart = sbp->bio_offset;
1202 send = sbp->bio_offset + sbp->bio_length;
1203 if (rend > sstart && rstart < send)
1211 * Return TRUE if the given sync request is colliding with a in-progress regular
1215 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1217 off_t rstart, rend, sstart, send;
1220 if (sc->sc_sync.ds_ndisks == 0)
1222 sstart = sbp->bio_offset;
1223 send = sbp->bio_offset + sbp->bio_length;
1224 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1225 rstart = bp->bio_offset;
1226 rend = bp->bio_offset + bp->bio_length;
1227 if (rend > sstart && rstart < send)
1234 * Puts request onto delayed queue.
1237 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1240 G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1241 bioq_insert_head(&sc->sc_regular_delayed, bp);
1245 * Puts synchronization request onto delayed queue.
1248 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1251 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1252 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1256 * Releases delayed regular requests which don't collide anymore with sync
1260 g_mirror_regular_release(struct g_mirror_softc *sc)
1262 struct bio *bp, *bp2;
1264 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1265 if (g_mirror_sync_collision(sc, bp))
1267 bioq_remove(&sc->sc_regular_delayed, bp);
1268 G_MIRROR_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1269 mtx_lock(&sc->sc_queue_mtx);
1270 bioq_insert_head(&sc->sc_queue, bp);
1273 * wakeup() is not needed, because this function is called from
1274 * the worker thread.
1276 wakeup(&sc->sc_queue);
1278 mtx_unlock(&sc->sc_queue_mtx);
1283 * Releases delayed sync requests which don't collide anymore with regular
1287 g_mirror_sync_release(struct g_mirror_softc *sc)
1289 struct bio *bp, *bp2;
1291 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1292 if (g_mirror_regular_collision(sc, bp))
1294 bioq_remove(&sc->sc_sync_delayed, bp);
1295 G_MIRROR_LOGREQ(2, bp,
1296 "Releasing delayed synchronization request.");
1297 g_io_request(bp, bp->bio_from);
1302 * Handle synchronization requests.
1303 * Every synchronization request is two-steps process: first, READ request is
1304 * send to active provider and then WRITE request (with read data) to the provider
1305 * beeing synchronized. When WRITE is finished, new synchronization request is
1309 g_mirror_sync_request(struct bio *bp)
1311 struct g_mirror_softc *sc;
1312 struct g_mirror_disk *disk;
1314 bp->bio_from->index--;
1315 sc = bp->bio_from->geom->softc;
1316 disk = bp->bio_from->private;
1318 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1320 g_mirror_kill_consumer(sc, bp->bio_from);
1321 g_topology_unlock();
1322 free(bp->bio_data, M_MIRROR);
1324 sx_xlock(&sc->sc_lock);
1329 * Synchronization request.
1331 switch (bp->bio_cmd) {
1334 struct g_consumer *cp;
1336 if (bp->bio_error != 0) {
1337 G_MIRROR_LOGREQ(0, bp,
1338 "Synchronization request failed (error=%d).",
1343 G_MIRROR_LOGREQ(3, bp,
1344 "Synchronization request half-finished.");
1345 bp->bio_cmd = BIO_WRITE;
1347 cp = disk->d_consumer;
1348 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1349 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1350 cp->acr, cp->acw, cp->ace));
1352 g_io_request(bp, cp);
1357 struct g_mirror_disk_sync *sync;
1362 if (bp->bio_error != 0) {
1363 G_MIRROR_LOGREQ(0, bp,
1364 "Synchronization request failed (error=%d).",
1367 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1368 g_mirror_event_send(disk,
1369 G_MIRROR_DISK_STATE_DISCONNECTED,
1370 G_MIRROR_EVENT_DONTWAIT);
1373 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1374 sync = &disk->d_sync;
1375 if (sync->ds_offset >= sc->sc_mediasize ||
1376 sync->ds_consumer == NULL ||
1377 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1378 /* Don't send more synchronization requests. */
1379 sync->ds_inflight--;
1380 if (sync->ds_bios != NULL) {
1381 i = (int)(uintptr_t)bp->bio_caller1;
1382 sync->ds_bios[i] = NULL;
1384 free(bp->bio_data, M_MIRROR);
1386 if (sync->ds_inflight > 0)
1388 if (sync->ds_consumer == NULL ||
1389 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1392 /* Disk up-to-date, activate it. */
1393 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1394 G_MIRROR_EVENT_DONTWAIT);
1398 /* Send next synchronization request. */
1399 data = bp->bio_data;
1400 bzero(bp, sizeof(*bp));
1401 bp->bio_cmd = BIO_READ;
1402 bp->bio_offset = sync->ds_offset;
1403 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1404 sync->ds_offset += bp->bio_length;
1405 bp->bio_done = g_mirror_sync_done;
1406 bp->bio_data = data;
1407 bp->bio_from = sync->ds_consumer;
1408 bp->bio_to = sc->sc_provider;
1409 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1410 sync->ds_consumer->index++;
1412 * Delay the request if it is colliding with a regular request.
1414 if (g_mirror_regular_collision(sc, bp))
1415 g_mirror_sync_delay(sc, bp);
1417 g_io_request(bp, sync->ds_consumer);
1419 /* Release delayed requests if possible. */
1420 g_mirror_regular_release(sc);
1422 /* Find the smallest offset */
1423 offset = sc->sc_mediasize;
1424 for (i = 0; i < g_mirror_syncreqs; i++) {
1425 bp = sync->ds_bios[i];
1426 if (bp->bio_offset < offset)
1427 offset = bp->bio_offset;
1429 if (sync->ds_offset_done + (MAXPHYS * 100) < offset) {
1430 /* Update offset_done on every 100 blocks. */
1431 sync->ds_offset_done = offset;
1432 g_mirror_update_metadata(disk);
1437 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1438 bp->bio_cmd, sc->sc_name));
1444 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1446 struct g_mirror_disk *disk;
1447 struct g_consumer *cp;
1450 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1451 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1455 if (bp->bio_error == 0)
1456 bp->bio_error = ENXIO;
1457 g_io_deliver(bp, bp->bio_error);
1460 cbp = g_clone_bio(bp);
1462 if (bp->bio_error == 0)
1463 bp->bio_error = ENOMEM;
1464 g_io_deliver(bp, bp->bio_error);
1468 * Fill in the component buf structure.
1470 cp = disk->d_consumer;
1471 cbp->bio_done = g_mirror_done;
1472 cbp->bio_to = cp->provider;
1473 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1474 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1475 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1478 g_io_request(cbp, cp);
1482 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1484 struct g_mirror_disk *disk;
1485 struct g_consumer *cp;
1488 disk = g_mirror_get_disk(sc);
1490 if (bp->bio_error == 0)
1491 bp->bio_error = ENXIO;
1492 g_io_deliver(bp, bp->bio_error);
1495 cbp = g_clone_bio(bp);
1497 if (bp->bio_error == 0)
1498 bp->bio_error = ENOMEM;
1499 g_io_deliver(bp, bp->bio_error);
1503 * Fill in the component buf structure.
1505 cp = disk->d_consumer;
1506 cbp->bio_done = g_mirror_done;
1507 cbp->bio_to = cp->provider;
1508 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1509 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1510 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1513 g_io_request(cbp, cp);
1516 #define TRACK_SIZE (1 * 1024 * 1024)
1517 #define LOAD_SCALE 256
1518 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
1521 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1523 struct g_mirror_disk *disk, *dp;
1524 struct g_consumer *cp;
1528 /* Find a disk with the smallest load. */
1531 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1532 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1535 /* If disk head is precisely in position - highly prefer it. */
1536 if (dp->d_last_offset == bp->bio_offset)
1537 prio -= 2 * LOAD_SCALE;
1539 /* If disk head is close to position - prefer it. */
1540 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1541 prio -= 1 * LOAD_SCALE;
1547 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1548 cbp = g_clone_bio(bp);
1550 if (bp->bio_error == 0)
1551 bp->bio_error = ENOMEM;
1552 g_io_deliver(bp, bp->bio_error);
1556 * Fill in the component buf structure.
1558 cp = disk->d_consumer;
1559 cbp->bio_done = g_mirror_done;
1560 cbp->bio_to = cp->provider;
1561 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1562 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1563 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1566 /* Remember last head position */
1567 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1569 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1570 dp->load = (dp->d_consumer->index * LOAD_SCALE +
1573 g_io_request(cbp, cp);
1577 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1579 struct bio_queue_head queue;
1580 struct g_mirror_disk *disk;
1581 struct g_consumer *cp;
1583 off_t left, mod, offset, slice;
1587 if (bp->bio_length <= sc->sc_slice) {
1588 g_mirror_request_round_robin(sc, bp);
1591 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1592 slice = bp->bio_length / ndisks;
1593 mod = slice % sc->sc_provider->sectorsize;
1595 slice += sc->sc_provider->sectorsize - mod;
1597 * Allocate all bios before sending any request, so we can
1598 * return ENOMEM in nice and clean way.
1600 left = bp->bio_length;
1601 offset = bp->bio_offset;
1602 data = bp->bio_data;
1604 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1605 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1607 cbp = g_clone_bio(bp);
1609 while ((cbp = bioq_takefirst(&queue)) != NULL)
1611 if (bp->bio_error == 0)
1612 bp->bio_error = ENOMEM;
1613 g_io_deliver(bp, bp->bio_error);
1616 bioq_insert_tail(&queue, cbp);
1617 cbp->bio_done = g_mirror_done;
1618 cbp->bio_caller1 = disk;
1619 cbp->bio_to = disk->d_consumer->provider;
1620 cbp->bio_offset = offset;
1621 cbp->bio_data = data;
1622 cbp->bio_length = MIN(left, slice);
1623 left -= cbp->bio_length;
1626 offset += cbp->bio_length;
1627 data += cbp->bio_length;
1629 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1630 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1631 disk = cbp->bio_caller1;
1632 cbp->bio_caller1 = NULL;
1633 cp = disk->d_consumer;
1634 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1635 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1636 cp->acr, cp->acw, cp->ace));
1637 disk->d_consumer->index++;
1638 g_io_request(cbp, disk->d_consumer);
1643 g_mirror_register_request(struct bio *bp)
1645 struct g_mirror_softc *sc;
1647 sc = bp->bio_to->private;
1648 switch (bp->bio_cmd) {
1650 switch (sc->sc_balance) {
1651 case G_MIRROR_BALANCE_LOAD:
1652 g_mirror_request_load(sc, bp);
1654 case G_MIRROR_BALANCE_PREFER:
1655 g_mirror_request_prefer(sc, bp);
1657 case G_MIRROR_BALANCE_ROUND_ROBIN:
1658 g_mirror_request_round_robin(sc, bp);
1660 case G_MIRROR_BALANCE_SPLIT:
1661 g_mirror_request_split(sc, bp);
1668 struct g_mirror_disk *disk;
1669 struct g_mirror_disk_sync *sync;
1670 struct bio_queue_head queue;
1671 struct g_consumer *cp;
1675 * Delay the request if it is colliding with a synchronization
1678 if (g_mirror_sync_collision(sc, bp)) {
1679 g_mirror_regular_delay(sc, bp);
1684 g_mirror_unidle(sc);
1686 sc->sc_last_write = time_uptime;
1689 * Allocate all bios before sending any request, so we can
1690 * return ENOMEM in nice and clean way.
1693 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1694 sync = &disk->d_sync;
1695 switch (disk->d_state) {
1696 case G_MIRROR_DISK_STATE_ACTIVE:
1698 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1699 if (bp->bio_offset >= sync->ds_offset)
1705 if (bp->bio_cmd == BIO_DELETE &&
1706 (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE) == 0)
1708 cbp = g_clone_bio(bp);
1710 while ((cbp = bioq_takefirst(&queue)) != NULL)
1712 if (bp->bio_error == 0)
1713 bp->bio_error = ENOMEM;
1714 g_io_deliver(bp, bp->bio_error);
1717 bioq_insert_tail(&queue, cbp);
1718 cbp->bio_done = g_mirror_done;
1719 cp = disk->d_consumer;
1720 cbp->bio_caller1 = cp;
1721 cbp->bio_to = cp->provider;
1722 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1723 ("Consumer %s not opened (r%dw%de%d).",
1724 cp->provider->name, cp->acr, cp->acw, cp->ace));
1726 if (bioq_first(&queue) == NULL) {
1727 g_io_deliver(bp, EOPNOTSUPP);
1730 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1731 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1732 cp = cbp->bio_caller1;
1733 cbp->bio_caller1 = NULL;
1736 g_io_request(cbp, cp);
1739 * Put request onto inflight queue, so we can check if new
1740 * synchronization requests don't collide with it.
1742 bioq_insert_tail(&sc->sc_inflight, bp);
1744 * Bump syncid on first write.
1746 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1747 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1748 g_mirror_bump_syncid(sc);
1753 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1754 bp->bio_cmd, sc->sc_name));
1760 g_mirror_can_destroy(struct g_mirror_softc *sc)
1763 struct g_consumer *cp;
1765 g_topology_assert();
1767 if (gp->softc == NULL)
1769 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_TASTING) != 0)
1771 LIST_FOREACH(cp, &gp->consumer, consumer) {
1772 if (g_mirror_is_busy(sc, cp))
1775 gp = sc->sc_sync.ds_geom;
1776 LIST_FOREACH(cp, &gp->consumer, consumer) {
1777 if (g_mirror_is_busy(sc, cp))
1780 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1786 g_mirror_try_destroy(struct g_mirror_softc *sc)
1789 if (sc->sc_rootmount != NULL) {
1790 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1792 root_mount_rel(sc->sc_rootmount);
1793 sc->sc_rootmount = NULL;
1796 if (!g_mirror_can_destroy(sc)) {
1797 g_topology_unlock();
1800 sc->sc_geom->softc = NULL;
1801 sc->sc_sync.ds_geom->softc = NULL;
1802 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WAIT) != 0) {
1803 g_topology_unlock();
1804 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1806 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
1807 sx_xunlock(&sc->sc_lock);
1808 wakeup(&sc->sc_worker);
1809 sc->sc_worker = NULL;
1811 g_topology_unlock();
1812 g_mirror_destroy_device(sc);
1821 g_mirror_worker(void *arg)
1823 struct g_mirror_softc *sc;
1824 struct g_mirror_event *ep;
1829 thread_lock(curthread);
1830 sched_prio(curthread, PRIBIO);
1831 thread_unlock(curthread);
1833 sx_xlock(&sc->sc_lock);
1835 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1837 * First take a look at events.
1838 * This is important to handle events before any I/O requests.
1840 ep = g_mirror_event_get(sc);
1842 g_mirror_event_remove(sc, ep);
1843 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1844 /* Update only device status. */
1846 "Running event for device %s.",
1849 g_mirror_update_device(sc, 1);
1851 /* Update disk status. */
1852 G_MIRROR_DEBUG(3, "Running event for disk %s.",
1853 g_mirror_get_diskname(ep->e_disk));
1854 ep->e_error = g_mirror_update_disk(ep->e_disk,
1856 if (ep->e_error == 0)
1857 g_mirror_update_device(sc, 0);
1859 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1860 KASSERT(ep->e_error == 0,
1861 ("Error cannot be handled."));
1862 g_mirror_event_free(ep);
1864 ep->e_flags |= G_MIRROR_EVENT_DONE;
1865 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1867 mtx_lock(&sc->sc_events_mtx);
1869 mtx_unlock(&sc->sc_events_mtx);
1872 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1873 if (g_mirror_try_destroy(sc)) {
1874 curthread->td_pflags &= ~TDP_GEOM;
1875 G_MIRROR_DEBUG(1, "Thread exiting.");
1879 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1883 * Check if we can mark array as CLEAN and if we can't take
1884 * how much seconds should we wait.
1886 timeout = g_mirror_idle(sc, -1);
1890 /* Get first request from the queue. */
1891 mtx_lock(&sc->sc_queue_mtx);
1892 bp = bioq_takefirst(&sc->sc_queue);
1895 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1896 mtx_unlock(&sc->sc_queue_mtx);
1897 if (g_mirror_try_destroy(sc)) {
1898 curthread->td_pflags &= ~TDP_GEOM;
1899 G_MIRROR_DEBUG(1, "Thread exiting.");
1902 mtx_lock(&sc->sc_queue_mtx);
1904 sx_xunlock(&sc->sc_lock);
1906 * XXX: We can miss an event here, because an event
1907 * can be added without sx-device-lock and without
1908 * mtx-queue-lock. Maybe I should just stop using
1909 * dedicated mutex for events synchronization and
1910 * stick with the queue lock?
1911 * The event will hang here until next I/O request
1912 * or next event is received.
1914 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
1916 sx_xlock(&sc->sc_lock);
1917 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
1920 mtx_unlock(&sc->sc_queue_mtx);
1922 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
1923 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
1924 g_mirror_sync_request(bp); /* READ */
1925 } else if (bp->bio_to != sc->sc_provider) {
1926 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
1927 g_mirror_regular_request(bp);
1928 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
1929 g_mirror_sync_request(bp); /* WRITE */
1932 ("Invalid request cflags=0x%hhx to=%s.",
1933 bp->bio_cflags, bp->bio_to->name));
1936 g_mirror_register_request(bp);
1938 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
1943 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
1946 sx_assert(&sc->sc_lock, SX_LOCKED);
1948 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
1950 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
1951 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
1952 g_mirror_get_diskname(disk), sc->sc_name);
1953 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1954 } else if (sc->sc_idle &&
1955 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
1956 G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
1957 g_mirror_get_diskname(disk), sc->sc_name);
1958 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
1963 g_mirror_sync_start(struct g_mirror_disk *disk)
1965 struct g_mirror_softc *sc;
1966 struct g_consumer *cp;
1970 g_topology_assert_not();
1972 sx_assert(&sc->sc_lock, SX_LOCKED);
1974 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1975 ("Disk %s is not marked for synchronization.",
1976 g_mirror_get_diskname(disk)));
1977 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1978 ("Device not in RUNNING state (%s, %u).", sc->sc_name,
1981 sx_xunlock(&sc->sc_lock);
1983 cp = g_new_consumer(sc->sc_sync.ds_geom);
1984 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1985 error = g_attach(cp, sc->sc_provider);
1987 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
1988 error = g_access(cp, 1, 0, 0);
1989 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
1990 g_topology_unlock();
1991 sx_xlock(&sc->sc_lock);
1993 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
1994 g_mirror_get_diskname(disk));
1995 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
1996 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1997 KASSERT(disk->d_sync.ds_consumer == NULL,
1998 ("Sync consumer already exists (device=%s, disk=%s).",
1999 sc->sc_name, g_mirror_get_diskname(disk)));
2001 disk->d_sync.ds_consumer = cp;
2002 disk->d_sync.ds_consumer->private = disk;
2003 disk->d_sync.ds_consumer->index = 0;
2006 * Allocate memory for synchronization bios and initialize them.
2008 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
2009 M_MIRROR, M_WAITOK);
2010 for (i = 0; i < g_mirror_syncreqs; i++) {
2012 disk->d_sync.ds_bios[i] = bp;
2013 bp->bio_parent = NULL;
2014 bp->bio_cmd = BIO_READ;
2015 bp->bio_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK);
2017 bp->bio_offset = disk->d_sync.ds_offset;
2018 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
2019 disk->d_sync.ds_offset += bp->bio_length;
2020 bp->bio_done = g_mirror_sync_done;
2021 bp->bio_from = disk->d_sync.ds_consumer;
2022 bp->bio_to = sc->sc_provider;
2023 bp->bio_caller1 = (void *)(uintptr_t)i;
2026 /* Increase the number of disks in SYNCHRONIZING state. */
2027 sc->sc_sync.ds_ndisks++;
2028 /* Set the number of in-flight synchronization requests. */
2029 disk->d_sync.ds_inflight = g_mirror_syncreqs;
2032 * Fire off first synchronization requests.
2034 for (i = 0; i < g_mirror_syncreqs; i++) {
2035 bp = disk->d_sync.ds_bios[i];
2036 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
2037 disk->d_sync.ds_consumer->index++;
2039 * Delay the request if it is colliding with a regular request.
2041 if (g_mirror_regular_collision(sc, bp))
2042 g_mirror_sync_delay(sc, bp);
2044 g_io_request(bp, disk->d_sync.ds_consumer);
2049 * Stop synchronization process.
2050 * type: 0 - synchronization finished
2051 * 1 - synchronization stopped
2054 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
2056 struct g_mirror_softc *sc;
2057 struct g_consumer *cp;
2059 g_topology_assert_not();
2061 sx_assert(&sc->sc_lock, SX_LOCKED);
2063 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2064 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2065 g_mirror_disk_state2str(disk->d_state)));
2066 if (disk->d_sync.ds_consumer == NULL)
2070 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2071 sc->sc_name, g_mirror_get_diskname(disk));
2072 } else /* if (type == 1) */ {
2073 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2074 sc->sc_name, g_mirror_get_diskname(disk));
2076 free(disk->d_sync.ds_bios, M_MIRROR);
2077 disk->d_sync.ds_bios = NULL;
2078 cp = disk->d_sync.ds_consumer;
2079 disk->d_sync.ds_consumer = NULL;
2080 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2081 sc->sc_sync.ds_ndisks--;
2082 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2084 g_mirror_kill_consumer(sc, cp);
2085 g_topology_unlock();
2086 sx_xlock(&sc->sc_lock);
2090 g_mirror_launch_provider(struct g_mirror_softc *sc)
2092 struct g_mirror_disk *disk;
2093 struct g_provider *pp, *dp;
2095 sx_assert(&sc->sc_lock, SX_LOCKED);
2098 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2099 pp->flags |= G_PF_DIRECT_RECEIVE;
2100 pp->mediasize = sc->sc_mediasize;
2101 pp->sectorsize = sc->sc_sectorsize;
2103 pp->stripeoffset = 0;
2105 /* Splitting of unmapped BIO's could work but isn't implemented now */
2106 if (sc->sc_balance != G_MIRROR_BALANCE_SPLIT)
2107 pp->flags |= G_PF_ACCEPT_UNMAPPED;
2109 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2110 if (disk->d_consumer && disk->d_consumer->provider) {
2111 dp = disk->d_consumer->provider;
2112 if (dp->stripesize > pp->stripesize) {
2113 pp->stripesize = dp->stripesize;
2114 pp->stripeoffset = dp->stripeoffset;
2116 /* A provider underneath us doesn't support unmapped */
2117 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
2118 G_MIRROR_DEBUG(0, "Cancelling unmapped "
2119 "because of %s.", dp->name);
2120 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
2126 sc->sc_provider = pp;
2127 g_error_provider(pp, 0);
2128 g_topology_unlock();
2129 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2130 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2131 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2132 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2133 g_mirror_sync_start(disk);
2138 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2140 struct g_mirror_disk *disk;
2143 g_topology_assert_not();
2144 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2148 g_error_provider(sc->sc_provider, ENXIO);
2149 mtx_lock(&sc->sc_queue_mtx);
2150 while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) {
2152 * Abort any pending I/O that wasn't generated by us.
2153 * Synchronization requests and requests destined for individual
2154 * mirror components can be destroyed immediately.
2156 if (bp->bio_to == sc->sc_provider &&
2157 bp->bio_from->geom != sc->sc_sync.ds_geom) {
2158 g_io_deliver(bp, ENXIO);
2160 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
2161 free(bp->bio_data, M_MIRROR);
2165 mtx_unlock(&sc->sc_queue_mtx);
2166 g_wither_provider(sc->sc_provider, ENXIO);
2167 sc->sc_provider = NULL;
2168 G_MIRROR_DEBUG(0, "Device %s: provider destroyed.", sc->sc_name);
2169 g_topology_unlock();
2170 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2171 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2172 g_mirror_sync_stop(disk, 1);
2177 g_mirror_go(void *arg)
2179 struct g_mirror_softc *sc;
2182 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2183 g_mirror_event_send(sc, 0,
2184 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2188 g_mirror_determine_state(struct g_mirror_disk *disk)
2190 struct g_mirror_softc *sc;
2194 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2195 if ((disk->d_flags &
2196 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2197 /* Disk does not need synchronization. */
2198 state = G_MIRROR_DISK_STATE_ACTIVE;
2201 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2203 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2205 * We can start synchronization from
2206 * the stored offset.
2208 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2210 state = G_MIRROR_DISK_STATE_STALE;
2213 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2215 * Reset all synchronization data for this disk,
2216 * because if it even was synchronized, it was
2217 * synchronized to disks with different syncid.
2219 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2220 disk->d_sync.ds_offset = 0;
2221 disk->d_sync.ds_offset_done = 0;
2222 disk->d_sync.ds_syncid = sc->sc_syncid;
2223 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2224 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2225 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2227 state = G_MIRROR_DISK_STATE_STALE;
2229 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2231 * Not good, NOT GOOD!
2232 * It means that mirror was started on stale disks
2233 * and more fresh disk just arrive.
2234 * If there were writes, mirror is broken, sorry.
2235 * I think the best choice here is don't touch
2236 * this disk and inform the user loudly.
2238 G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2239 "disk (%s) arrives!! It will not be connected to the "
2240 "running device.", sc->sc_name,
2241 g_mirror_get_diskname(disk));
2242 g_mirror_destroy_disk(disk);
2243 state = G_MIRROR_DISK_STATE_NONE;
2244 /* Return immediately, because disk was destroyed. */
2247 G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2248 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2253 * Update device state.
2256 g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force)
2258 struct g_mirror_disk *disk;
2261 sx_assert(&sc->sc_lock, SX_XLOCKED);
2263 switch (sc->sc_state) {
2264 case G_MIRROR_DEVICE_STATE_STARTING:
2266 struct g_mirror_disk *pdisk, *tdisk;
2267 u_int dirty, ndisks, genid, syncid;
2269 KASSERT(sc->sc_provider == NULL,
2270 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2272 * Are we ready? We are, if all disks are connected or
2273 * if we have any disks and 'force' is true.
2275 ndisks = g_mirror_ndisks(sc, -1);
2276 if (sc->sc_ndisks == ndisks || (force && ndisks > 0)) {
2278 } else if (ndisks == 0) {
2280 * Disks went down in starting phase, so destroy
2283 callout_drain(&sc->sc_callout);
2284 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2285 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2287 root_mount_rel(sc->sc_rootmount);
2288 sc->sc_rootmount = NULL;
2295 * Activate all disks with the biggest syncid.
2299 * If 'force' is true, we have been called due to
2300 * timeout, so don't bother canceling timeout.
2303 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2304 if ((disk->d_flags &
2305 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2310 /* No valid disks found, destroy device. */
2311 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2312 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2313 __LINE__, sc->sc_rootmount);
2314 root_mount_rel(sc->sc_rootmount);
2315 sc->sc_rootmount = NULL;
2319 /* Cancel timeout. */
2320 callout_drain(&sc->sc_callout);
2324 * Find the biggest genid.
2327 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2328 if (disk->d_genid > genid)
2329 genid = disk->d_genid;
2331 sc->sc_genid = genid;
2333 * Remove all disks without the biggest genid.
2335 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2336 if (disk->d_genid < genid) {
2338 "Component %s (device %s) broken, skipping.",
2339 g_mirror_get_diskname(disk), sc->sc_name);
2340 g_mirror_destroy_disk(disk);
2345 * Find the biggest syncid.
2348 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2349 if (disk->d_sync.ds_syncid > syncid)
2350 syncid = disk->d_sync.ds_syncid;
2354 * Here we need to look for dirty disks and if all disks
2355 * with the biggest syncid are dirty, we have to choose
2356 * one with the biggest priority and rebuild the rest.
2359 * Find the number of dirty disks with the biggest syncid.
2360 * Find the number of disks with the biggest syncid.
2361 * While here, find a disk with the biggest priority.
2365 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2366 if (disk->d_sync.ds_syncid != syncid)
2368 if ((disk->d_flags &
2369 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2373 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2375 if (pdisk == NULL ||
2376 pdisk->d_priority < disk->d_priority) {
2382 /* No dirty disks at all, great. */
2383 } else if (dirty == ndisks) {
2385 * Force synchronization for all dirty disks except one
2386 * with the biggest priority.
2388 KASSERT(pdisk != NULL, ("pdisk == NULL"));
2389 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2390 "master disk for synchronization.",
2391 g_mirror_get_diskname(pdisk), sc->sc_name);
2392 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2393 if (disk->d_sync.ds_syncid != syncid)
2395 if ((disk->d_flags &
2396 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2399 KASSERT((disk->d_flags &
2400 G_MIRROR_DISK_FLAG_DIRTY) != 0,
2401 ("Disk %s isn't marked as dirty.",
2402 g_mirror_get_diskname(disk)));
2403 /* Skip the disk with the biggest priority. */
2406 disk->d_sync.ds_syncid = 0;
2408 } else if (dirty < ndisks) {
2410 * Force synchronization for all dirty disks.
2411 * We have some non-dirty disks.
2413 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2414 if (disk->d_sync.ds_syncid != syncid)
2416 if ((disk->d_flags &
2417 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2420 if ((disk->d_flags &
2421 G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2424 disk->d_sync.ds_syncid = 0;
2430 sc->sc_syncid = syncid;
2432 /* Remember to bump syncid on first write. */
2433 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2435 state = G_MIRROR_DEVICE_STATE_RUNNING;
2436 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2437 sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2438 g_mirror_device_state2str(state));
2439 sc->sc_state = state;
2440 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2441 state = g_mirror_determine_state(disk);
2442 g_mirror_event_send(disk, state,
2443 G_MIRROR_EVENT_DONTWAIT);
2444 if (state == G_MIRROR_DISK_STATE_STALE)
2445 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2449 case G_MIRROR_DEVICE_STATE_RUNNING:
2450 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2451 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2453 * No active disks or no disks at all,
2454 * so destroy device.
2456 if (sc->sc_provider != NULL)
2457 g_mirror_destroy_provider(sc);
2458 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2460 } else if (g_mirror_ndisks(sc,
2461 G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2462 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2464 * We have active disks, launch provider if it doesn't
2467 if (sc->sc_provider == NULL)
2468 g_mirror_launch_provider(sc);
2469 if (sc->sc_rootmount != NULL) {
2470 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2471 __LINE__, sc->sc_rootmount);
2472 root_mount_rel(sc->sc_rootmount);
2473 sc->sc_rootmount = NULL;
2477 * Genid should be bumped immediately, so do it here.
2479 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2480 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2481 g_mirror_bump_genid(sc);
2485 KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2486 sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2492 * Update disk state and device state if needed.
2494 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \
2495 "Disk %s state changed from %s to %s (device %s).", \
2496 g_mirror_get_diskname(disk), \
2497 g_mirror_disk_state2str(disk->d_state), \
2498 g_mirror_disk_state2str(state), sc->sc_name)
2500 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2502 struct g_mirror_softc *sc;
2505 sx_assert(&sc->sc_lock, SX_XLOCKED);
2508 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2509 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2510 g_mirror_disk_state2str(state));
2512 case G_MIRROR_DISK_STATE_NEW:
2514 * Possible scenarios:
2515 * 1. New disk arrive.
2517 /* Previous state should be NONE. */
2518 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2519 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2520 g_mirror_disk_state2str(disk->d_state)));
2521 DISK_STATE_CHANGED();
2523 disk->d_state = state;
2524 if (LIST_EMPTY(&sc->sc_disks))
2525 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2527 struct g_mirror_disk *dp;
2529 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2530 if (disk->d_priority >= dp->d_priority) {
2531 LIST_INSERT_BEFORE(dp, disk, d_next);
2535 if (LIST_NEXT(dp, d_next) == NULL)
2539 LIST_INSERT_AFTER(dp, disk, d_next);
2541 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2542 sc->sc_name, g_mirror_get_diskname(disk));
2543 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2545 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2546 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2547 g_mirror_device_state2str(sc->sc_state),
2548 g_mirror_get_diskname(disk),
2549 g_mirror_disk_state2str(disk->d_state)));
2550 state = g_mirror_determine_state(disk);
2551 if (state != G_MIRROR_DISK_STATE_NONE)
2554 case G_MIRROR_DISK_STATE_ACTIVE:
2556 * Possible scenarios:
2557 * 1. New disk does not need synchronization.
2558 * 2. Synchronization process finished successfully.
2560 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2561 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2562 g_mirror_device_state2str(sc->sc_state),
2563 g_mirror_get_diskname(disk),
2564 g_mirror_disk_state2str(disk->d_state)));
2565 /* Previous state should be NEW or SYNCHRONIZING. */
2566 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2567 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2568 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2569 g_mirror_disk_state2str(disk->d_state)));
2570 DISK_STATE_CHANGED();
2572 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2573 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2574 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2575 g_mirror_sync_stop(disk, 0);
2577 disk->d_state = state;
2578 disk->d_sync.ds_offset = 0;
2579 disk->d_sync.ds_offset_done = 0;
2580 g_mirror_update_idle(sc, disk);
2581 g_mirror_update_metadata(disk);
2582 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2583 sc->sc_name, g_mirror_get_diskname(disk));
2585 case G_MIRROR_DISK_STATE_STALE:
2587 * Possible scenarios:
2588 * 1. Stale disk was connected.
2590 /* Previous state should be NEW. */
2591 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2592 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2593 g_mirror_disk_state2str(disk->d_state)));
2594 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2595 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2596 g_mirror_device_state2str(sc->sc_state),
2597 g_mirror_get_diskname(disk),
2598 g_mirror_disk_state2str(disk->d_state)));
2600 * STALE state is only possible if device is marked
2603 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2604 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2605 g_mirror_device_state2str(sc->sc_state),
2606 g_mirror_get_diskname(disk),
2607 g_mirror_disk_state2str(disk->d_state)));
2608 DISK_STATE_CHANGED();
2610 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2611 disk->d_state = state;
2612 g_mirror_update_metadata(disk);
2613 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2614 sc->sc_name, g_mirror_get_diskname(disk));
2616 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2618 * Possible scenarios:
2619 * 1. Disk which needs synchronization was connected.
2621 /* Previous state should be NEW. */
2622 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2623 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2624 g_mirror_disk_state2str(disk->d_state)));
2625 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2626 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2627 g_mirror_device_state2str(sc->sc_state),
2628 g_mirror_get_diskname(disk),
2629 g_mirror_disk_state2str(disk->d_state)));
2630 DISK_STATE_CHANGED();
2632 if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2633 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2634 disk->d_state = state;
2635 if (sc->sc_provider != NULL) {
2636 g_mirror_sync_start(disk);
2637 g_mirror_update_metadata(disk);
2640 case G_MIRROR_DISK_STATE_DISCONNECTED:
2642 * Possible scenarios:
2643 * 1. Device wasn't running yet, but disk disappear.
2644 * 2. Disk was active and disapppear.
2645 * 3. Disk disappear during synchronization process.
2647 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2649 * Previous state should be ACTIVE, STALE or
2652 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2653 disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2654 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2655 ("Wrong disk state (%s, %s).",
2656 g_mirror_get_diskname(disk),
2657 g_mirror_disk_state2str(disk->d_state)));
2658 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2659 /* Previous state should be NEW. */
2660 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2661 ("Wrong disk state (%s, %s).",
2662 g_mirror_get_diskname(disk),
2663 g_mirror_disk_state2str(disk->d_state)));
2665 * Reset bumping syncid if disk disappeared in STARTING
2668 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2669 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2672 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2674 g_mirror_device_state2str(sc->sc_state),
2675 g_mirror_get_diskname(disk),
2676 g_mirror_disk_state2str(disk->d_state)));
2679 DISK_STATE_CHANGED();
2680 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2681 sc->sc_name, g_mirror_get_diskname(disk));
2683 g_mirror_destroy_disk(disk);
2685 case G_MIRROR_DISK_STATE_DESTROY:
2689 error = g_mirror_clear_metadata(disk);
2692 "Device %s: failed to clear metadata on %s: %d.",
2693 sc->sc_name, g_mirror_get_diskname(disk), error);
2696 DISK_STATE_CHANGED();
2697 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2698 sc->sc_name, g_mirror_get_diskname(disk));
2700 g_mirror_destroy_disk(disk);
2702 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2703 g_mirror_update_metadata(disk);
2708 KASSERT(1 == 0, ("Unknown state (%u).", state));
2713 #undef DISK_STATE_CHANGED
2716 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2718 struct g_provider *pp;
2722 g_topology_assert();
2724 error = g_access(cp, 1, 0, 0);
2728 g_topology_unlock();
2729 /* Metadata are stored on last sector. */
2730 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2733 g_access(cp, -1, 0, 0);
2735 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2736 cp->provider->name, error);
2740 /* Decode metadata. */
2741 error = mirror_metadata_decode(buf, md);
2743 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2745 if (md->md_version > G_MIRROR_VERSION) {
2747 "Kernel module is too old to handle metadata from %s.",
2748 cp->provider->name);
2752 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2753 cp->provider->name);
2761 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2762 struct g_mirror_metadata *md)
2765 if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2766 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2767 pp->name, md->md_did);
2770 if (md->md_all != sc->sc_ndisks) {
2772 "Invalid '%s' field on disk %s (device %s), skipping.",
2773 "md_all", pp->name, sc->sc_name);
2776 if (md->md_slice != sc->sc_slice) {
2778 "Invalid '%s' field on disk %s (device %s), skipping.",
2779 "md_slice", pp->name, sc->sc_name);
2782 if (md->md_balance != sc->sc_balance) {
2784 "Invalid '%s' field on disk %s (device %s), skipping.",
2785 "md_balance", pp->name, sc->sc_name);
2789 if (md->md_mediasize != sc->sc_mediasize) {
2791 "Invalid '%s' field on disk %s (device %s), skipping.",
2792 "md_mediasize", pp->name, sc->sc_name);
2796 if (sc->sc_mediasize > pp->mediasize) {
2798 "Invalid size of disk %s (device %s), skipping.", pp->name,
2802 if (md->md_sectorsize != sc->sc_sectorsize) {
2804 "Invalid '%s' field on disk %s (device %s), skipping.",
2805 "md_sectorsize", pp->name, sc->sc_name);
2808 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2810 "Invalid sector size of disk %s (device %s), skipping.",
2811 pp->name, sc->sc_name);
2814 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2816 "Invalid device flags on disk %s (device %s), skipping.",
2817 pp->name, sc->sc_name);
2820 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2822 "Invalid disk flags on disk %s (device %s), skipping.",
2823 pp->name, sc->sc_name);
2830 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2831 struct g_mirror_metadata *md)
2833 struct g_mirror_disk *disk;
2836 g_topology_assert_not();
2837 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2839 error = g_mirror_check_metadata(sc, pp, md);
2842 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING &&
2843 md->md_genid < sc->sc_genid) {
2844 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
2845 pp->name, sc->sc_name);
2848 disk = g_mirror_init_disk(sc, pp, md, &error);
2851 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
2852 G_MIRROR_EVENT_WAIT);
2855 if (md->md_version < G_MIRROR_VERSION) {
2856 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
2857 pp->name, md->md_version, G_MIRROR_VERSION);
2858 g_mirror_update_metadata(disk);
2864 g_mirror_destroy_delayed(void *arg, int flag)
2866 struct g_mirror_softc *sc;
2869 if (flag == EV_CANCEL) {
2870 G_MIRROR_DEBUG(1, "Destroying canceled.");
2874 g_topology_unlock();
2875 sx_xlock(&sc->sc_lock);
2876 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
2877 ("DESTROY flag set on %s.", sc->sc_name));
2878 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0,
2879 ("DESTROYING flag not set on %s.", sc->sc_name));
2880 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
2881 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
2883 G_MIRROR_DEBUG(0, "Cannot destroy %s (error=%d).",
2884 sc->sc_name, error);
2885 sx_xunlock(&sc->sc_lock);
2891 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
2893 struct g_mirror_softc *sc;
2896 g_topology_assert();
2897 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2901 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
2903 g_topology_unlock();
2904 sx_xlock(&sc->sc_lock);
2905 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
2906 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0 ||
2907 LIST_EMPTY(&sc->sc_disks)) {
2908 if (acr > 0 || acw > 0 || ace > 0)
2912 sc->sc_provider_open += acr + acw + ace;
2913 if (pp->acw + acw == 0)
2914 g_mirror_idle(sc, 0);
2915 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0 &&
2916 sc->sc_provider_open == 0)
2917 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK, sc, NULL);
2919 sx_xunlock(&sc->sc_lock);
2924 static struct g_geom *
2925 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
2927 struct g_mirror_softc *sc;
2931 g_topology_assert();
2932 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
2935 /* One disk is minimum. */
2941 gp = g_new_geomf(mp, "%s", md->md_name);
2942 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
2943 gp->start = g_mirror_start;
2944 gp->orphan = g_mirror_orphan;
2945 gp->access = g_mirror_access;
2946 gp->dumpconf = g_mirror_dumpconf;
2948 sc->sc_id = md->md_mid;
2949 sc->sc_slice = md->md_slice;
2950 sc->sc_balance = md->md_balance;
2951 sc->sc_mediasize = md->md_mediasize;
2952 sc->sc_sectorsize = md->md_sectorsize;
2953 sc->sc_ndisks = md->md_all;
2954 sc->sc_flags = md->md_mflags;
2957 sc->sc_last_write = time_uptime;
2960 sx_init(&sc->sc_lock, "gmirror:lock");
2961 bioq_init(&sc->sc_queue);
2962 mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
2963 bioq_init(&sc->sc_regular_delayed);
2964 bioq_init(&sc->sc_inflight);
2965 bioq_init(&sc->sc_sync_delayed);
2966 LIST_INIT(&sc->sc_disks);
2967 TAILQ_INIT(&sc->sc_events);
2968 mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
2969 callout_init(&sc->sc_callout, 1);
2970 mtx_init(&sc->sc_done_mtx, "gmirror:done", NULL, MTX_DEF);
2971 sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
2974 sc->sc_provider = NULL;
2975 sc->sc_provider_open = 0;
2977 * Synchronization geom.
2979 gp = g_new_geomf(mp, "%s.sync", md->md_name);
2981 gp->orphan = g_mirror_orphan;
2982 sc->sc_sync.ds_geom = gp;
2983 sc->sc_sync.ds_ndisks = 0;
2984 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
2985 "g_mirror %s", md->md_name);
2987 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
2989 g_destroy_geom(sc->sc_sync.ds_geom);
2990 g_destroy_geom(sc->sc_geom);
2991 g_mirror_free_device(sc);
2995 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
2996 sc->sc_name, sc->sc_ndisks, sc->sc_id);
2998 sc->sc_rootmount = root_mount_hold("GMIRROR");
2999 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
3003 timeout = g_mirror_timeout * hz;
3004 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
3005 return (sc->sc_geom);
3009 g_mirror_destroy(struct g_mirror_softc *sc, int how)
3011 struct g_mirror_disk *disk;
3013 g_topology_assert_not();
3014 sx_assert(&sc->sc_lock, SX_XLOCKED);
3016 if (sc->sc_provider_open != 0) {
3018 case G_MIRROR_DESTROY_SOFT:
3020 "Device %s is still open (%d).", sc->sc_name,
3021 sc->sc_provider_open);
3023 case G_MIRROR_DESTROY_DELAYED:
3025 "Device %s will be destroyed on last close.",
3027 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
3028 if (disk->d_state ==
3029 G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3030 g_mirror_sync_stop(disk, 1);
3033 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROYING;
3035 case G_MIRROR_DESTROY_HARD:
3036 G_MIRROR_DEBUG(1, "Device %s is still open, so it "
3037 "can't be definitely removed.", sc->sc_name);
3042 if (sc->sc_geom->softc == NULL) {
3043 g_topology_unlock();
3046 sc->sc_geom->softc = NULL;
3047 sc->sc_sync.ds_geom->softc = NULL;
3048 g_topology_unlock();
3050 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
3051 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WAIT;
3052 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3053 sx_xunlock(&sc->sc_lock);
3054 mtx_lock(&sc->sc_queue_mtx);
3056 mtx_unlock(&sc->sc_queue_mtx);
3057 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3058 while (sc->sc_worker != NULL)
3059 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
3060 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3061 sx_xlock(&sc->sc_lock);
3062 g_mirror_destroy_device(sc);
3067 g_mirror_taste_orphan(struct g_consumer *cp)
3070 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3071 cp->provider->name));
3074 static struct g_geom *
3075 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3077 struct g_mirror_metadata md;
3078 struct g_mirror_softc *sc;
3079 struct g_consumer *cp;
3083 g_topology_assert();
3084 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3085 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
3087 gp = g_new_geomf(mp, "mirror:taste");
3089 * This orphan function should be never called.
3091 gp->orphan = g_mirror_taste_orphan;
3092 cp = g_new_consumer(gp);
3094 error = g_mirror_read_metadata(cp, &md);
3096 g_destroy_consumer(cp);
3102 if (md.md_provider[0] != '\0' &&
3103 !g_compare_names(md.md_provider, pp->name))
3105 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3107 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3109 "Device %s: provider %s marked as inactive, skipping.",
3110 md.md_name, pp->name);
3113 if (g_mirror_debug >= 2)
3114 mirror_metadata_dump(&md);
3117 * Let's check if device already exists.
3120 LIST_FOREACH(gp, &mp->geom, geom) {
3124 if (sc->sc_sync.ds_geom == gp)
3126 if (strcmp(md.md_name, sc->sc_name) != 0)
3128 if (md.md_mid != sc->sc_id) {
3129 G_MIRROR_DEBUG(0, "Device %s already configured.",
3136 gp = g_mirror_create(mp, &md);
3138 G_MIRROR_DEBUG(0, "Cannot create device %s.",
3144 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3145 g_topology_unlock();
3146 sx_xlock(&sc->sc_lock);
3147 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
3148 error = g_mirror_add_disk(sc, pp, &md);
3150 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3151 pp->name, gp->name, error);
3152 if (LIST_EMPTY(&sc->sc_disks)) {
3154 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3160 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
3161 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3162 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3166 sx_xunlock(&sc->sc_lock);
3172 g_mirror_resize(struct g_consumer *cp)
3174 struct g_mirror_disk *disk;
3176 g_topology_assert();
3177 g_trace(G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name);
3182 g_topology_unlock();
3183 g_mirror_update_metadata(disk);
3188 g_mirror_destroy_geom(struct gctl_req *req __unused,
3189 struct g_class *mp __unused, struct g_geom *gp)
3191 struct g_mirror_softc *sc;
3194 g_topology_unlock();
3196 sx_xlock(&sc->sc_lock);
3198 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3200 sx_xunlock(&sc->sc_lock);
3206 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3207 struct g_consumer *cp, struct g_provider *pp)
3209 struct g_mirror_softc *sc;
3211 g_topology_assert();
3216 /* Skip synchronization geom. */
3217 if (gp == sc->sc_sync.ds_geom)
3221 } else if (cp != NULL) {
3222 struct g_mirror_disk *disk;
3227 g_topology_unlock();
3228 sx_xlock(&sc->sc_lock);
3229 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3230 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3231 sbuf_printf(sb, "%s<Synchronized>", indent);
3232 if (disk->d_sync.ds_offset == 0)
3233 sbuf_printf(sb, "0%%");
3235 sbuf_printf(sb, "%u%%",
3236 (u_int)((disk->d_sync.ds_offset * 100) /
3237 sc->sc_provider->mediasize));
3239 sbuf_printf(sb, "</Synchronized>\n");
3240 if (disk->d_sync.ds_offset > 0) {
3241 sbuf_printf(sb, "%s<BytesSynced>%jd"
3242 "</BytesSynced>\n", indent,
3243 (intmax_t)disk->d_sync.ds_offset);
3246 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3247 disk->d_sync.ds_syncid);
3248 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3250 sbuf_printf(sb, "%s<Flags>", indent);
3251 if (disk->d_flags == 0)
3252 sbuf_printf(sb, "NONE");
3256 #define ADD_FLAG(flag, name) do { \
3257 if ((disk->d_flags & (flag)) != 0) { \
3259 sbuf_printf(sb, ", "); \
3262 sbuf_printf(sb, name); \
3265 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3266 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3267 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3268 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3270 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3271 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3274 sbuf_printf(sb, "</Flags>\n");
3275 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3277 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3278 g_mirror_disk_state2str(disk->d_state));
3279 sx_xunlock(&sc->sc_lock);
3282 g_topology_unlock();
3283 sx_xlock(&sc->sc_lock);
3284 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3285 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3286 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3287 sbuf_printf(sb, "%s<Flags>", indent);
3288 if (sc->sc_flags == 0)
3289 sbuf_printf(sb, "NONE");
3293 #define ADD_FLAG(flag, name) do { \
3294 if ((sc->sc_flags & (flag)) != 0) { \
3296 sbuf_printf(sb, ", "); \
3299 sbuf_printf(sb, name); \
3302 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3303 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3306 sbuf_printf(sb, "</Flags>\n");
3307 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3308 (u_int)sc->sc_slice);
3309 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3310 balance_name(sc->sc_balance));
3311 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3313 sbuf_printf(sb, "%s<State>", indent);
3314 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3315 sbuf_printf(sb, "%s", "STARTING");
3316 else if (sc->sc_ndisks ==
3317 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3318 sbuf_printf(sb, "%s", "COMPLETE");
3320 sbuf_printf(sb, "%s", "DEGRADED");
3321 sbuf_printf(sb, "</State>\n");
3322 sx_xunlock(&sc->sc_lock);
3328 g_mirror_shutdown_post_sync(void *arg, int howto)
3331 struct g_geom *gp, *gp2;
3332 struct g_mirror_softc *sc;
3338 g_mirror_shutdown = 1;
3339 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3340 if ((sc = gp->softc) == NULL)
3342 /* Skip synchronization geom. */
3343 if (gp == sc->sc_sync.ds_geom)
3345 g_topology_unlock();
3346 sx_xlock(&sc->sc_lock);
3347 g_mirror_idle(sc, -1);
3349 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3351 sx_xunlock(&sc->sc_lock);
3354 g_topology_unlock();
3359 g_mirror_init(struct g_class *mp)
3362 g_mirror_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
3363 g_mirror_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
3364 if (g_mirror_post_sync == NULL)
3365 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3369 g_mirror_fini(struct g_class *mp)
3372 if (g_mirror_post_sync != NULL)
3373 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_mirror_post_sync);
3376 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);