2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/limits.h>
39 #include <sys/mutex.h>
42 #include <sys/sysctl.h>
43 #include <sys/malloc.h>
44 #include <sys/eventhandler.h>
46 #include <geom/geom.h>
48 #include <sys/kthread.h>
49 #include <sys/sched.h>
50 #include <geom/mirror/g_mirror.h>
52 FEATURE(geom_mirror, "GEOM mirroring support");
54 static MALLOC_DEFINE(M_MIRROR, "mirror_data", "GEOM_MIRROR Data");
56 SYSCTL_DECL(_kern_geom);
57 static SYSCTL_NODE(_kern_geom, OID_AUTO, mirror, CTLFLAG_RW, 0,
59 int g_mirror_debug = 0;
60 SYSCTL_INT(_kern_geom_mirror, OID_AUTO, debug, CTLFLAG_RWTUN, &g_mirror_debug, 0,
62 static u_int g_mirror_timeout = 4;
63 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, timeout, CTLFLAG_RWTUN, &g_mirror_timeout,
64 0, "Time to wait on all mirror components");
65 static u_int g_mirror_idletime = 5;
66 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, idletime, CTLFLAG_RWTUN,
67 &g_mirror_idletime, 0, "Mark components as clean when idling");
68 static u_int g_mirror_disconnect_on_failure = 1;
69 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, disconnect_on_failure, CTLFLAG_RWTUN,
70 &g_mirror_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
71 static u_int g_mirror_syncreqs = 2;
72 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
73 &g_mirror_syncreqs, 0, "Parallel synchronization I/O requests.");
74 static u_int g_mirror_sync_period = 5;
75 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, sync_update_period, CTLFLAG_RWTUN,
76 &g_mirror_sync_period, 0,
77 "Metadata update period during synchronization, in seconds");
79 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \
80 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
81 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
82 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
85 static eventhandler_tag g_mirror_post_sync = NULL;
86 static int g_mirror_shutdown = 0;
88 static g_ctl_destroy_geom_t g_mirror_destroy_geom;
89 static g_taste_t g_mirror_taste;
90 static g_init_t g_mirror_init;
91 static g_fini_t g_mirror_fini;
92 static g_provgone_t g_mirror_providergone;
93 static g_resize_t g_mirror_resize;
95 struct g_class g_mirror_class = {
96 .name = G_MIRROR_CLASS_NAME,
98 .ctlreq = g_mirror_config,
99 .taste = g_mirror_taste,
100 .destroy_geom = g_mirror_destroy_geom,
101 .init = g_mirror_init,
102 .fini = g_mirror_fini,
103 .providergone = g_mirror_providergone,
104 .resize = g_mirror_resize
108 static void g_mirror_destroy_provider(struct g_mirror_softc *sc);
109 static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
110 static void g_mirror_update_device(struct g_mirror_softc *sc, bool force);
111 static void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
112 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
113 static void g_mirror_sync_stop(struct g_mirror_disk *disk, int type);
114 static void g_mirror_register_request(struct bio *bp);
115 static void g_mirror_sync_release(struct g_mirror_softc *sc);
119 g_mirror_disk_state2str(int state)
123 case G_MIRROR_DISK_STATE_NONE:
125 case G_MIRROR_DISK_STATE_NEW:
127 case G_MIRROR_DISK_STATE_ACTIVE:
129 case G_MIRROR_DISK_STATE_STALE:
131 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
132 return ("SYNCHRONIZING");
133 case G_MIRROR_DISK_STATE_DISCONNECTED:
134 return ("DISCONNECTED");
135 case G_MIRROR_DISK_STATE_DESTROY:
143 g_mirror_device_state2str(int state)
147 case G_MIRROR_DEVICE_STATE_STARTING:
149 case G_MIRROR_DEVICE_STATE_RUNNING:
157 g_mirror_get_diskname(struct g_mirror_disk *disk)
160 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
161 return ("[unknown]");
162 return (disk->d_name);
166 * --- Events handling functions ---
167 * Events in geom_mirror are used to maintain disks and device status
168 * from one thread to simplify locking.
171 g_mirror_event_free(struct g_mirror_event *ep)
178 g_mirror_event_send(void *arg, int state, int flags)
180 struct g_mirror_softc *sc;
181 struct g_mirror_disk *disk;
182 struct g_mirror_event *ep;
185 ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
186 G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
187 if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
198 mtx_lock(&sc->sc_events_mtx);
199 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
200 mtx_unlock(&sc->sc_events_mtx);
201 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
202 mtx_lock(&sc->sc_queue_mtx);
204 mtx_unlock(&sc->sc_queue_mtx);
205 if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0)
207 sx_assert(&sc->sc_lock, SX_XLOCKED);
208 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
209 sx_xunlock(&sc->sc_lock);
210 while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) {
211 mtx_lock(&sc->sc_events_mtx);
212 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event",
216 g_mirror_event_free(ep);
217 sx_xlock(&sc->sc_lock);
221 static struct g_mirror_event *
222 g_mirror_event_first(struct g_mirror_softc *sc)
224 struct g_mirror_event *ep;
226 mtx_lock(&sc->sc_events_mtx);
227 ep = TAILQ_FIRST(&sc->sc_events);
228 mtx_unlock(&sc->sc_events_mtx);
233 g_mirror_event_remove(struct g_mirror_softc *sc, struct g_mirror_event *ep)
236 mtx_lock(&sc->sc_events_mtx);
237 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
238 mtx_unlock(&sc->sc_events_mtx);
242 g_mirror_event_cancel(struct g_mirror_disk *disk)
244 struct g_mirror_softc *sc;
245 struct g_mirror_event *ep, *tmpep;
248 sx_assert(&sc->sc_lock, SX_XLOCKED);
250 mtx_lock(&sc->sc_events_mtx);
251 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
252 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0)
254 if (ep->e_disk != disk)
256 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
257 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
258 g_mirror_event_free(ep);
260 ep->e_error = ECANCELED;
264 mtx_unlock(&sc->sc_events_mtx);
268 * Return the number of disks in given state.
269 * If state is equal to -1, count all connected disks.
272 g_mirror_ndisks(struct g_mirror_softc *sc, int state)
274 struct g_mirror_disk *disk;
277 sx_assert(&sc->sc_lock, SX_LOCKED);
279 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
280 if (state == -1 || disk->d_state == state)
287 * Find a disk in mirror by its disk ID.
289 static struct g_mirror_disk *
290 g_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id)
292 struct g_mirror_disk *disk;
294 sx_assert(&sc->sc_lock, SX_XLOCKED);
296 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
297 if (disk->d_id == id)
304 g_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp)
309 mtx_lock(&sc->sc_queue_mtx);
310 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
311 if (bp->bio_from == cp)
314 mtx_unlock(&sc->sc_queue_mtx);
319 g_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp)
324 "I/O requests for %s exist, can't destroy it now.",
328 if (g_mirror_nrequests(sc, cp) > 0) {
330 "I/O requests for %s in queue, can't destroy it now.",
338 g_mirror_destroy_consumer(void *arg, int flags __unused)
340 struct g_consumer *cp;
345 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
347 g_destroy_consumer(cp);
351 g_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
353 struct g_provider *pp;
359 if (g_mirror_is_busy(sc, cp))
364 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
367 G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
368 -cp->acw, -cp->ace, 0);
369 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
370 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
373 * After retaste event was send (inside g_access()), we can send
374 * event to detach and destroy consumer.
375 * A class, which has consumer to the given provider connected
376 * will not receive retaste event for the provider.
377 * This is the way how I ignore retaste events when I close
378 * consumers opened for write: I detach and destroy consumer
379 * after retaste event is sent.
381 g_post_event(g_mirror_destroy_consumer, cp, M_WAITOK, NULL);
384 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", pp->name);
386 g_destroy_consumer(cp);
390 g_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp)
392 struct g_consumer *cp;
395 g_topology_assert_not();
396 KASSERT(disk->d_consumer == NULL,
397 ("Disk already connected (device %s).", disk->d_softc->sc_name));
400 cp = g_new_consumer(disk->d_softc->sc_geom);
401 cp->flags |= G_CF_DIRECT_RECEIVE;
402 error = g_attach(cp, pp);
404 g_destroy_consumer(cp);
408 error = g_access(cp, 1, 1, 1);
411 g_destroy_consumer(cp);
413 G_MIRROR_DEBUG(0, "Cannot open consumer %s (error=%d).",
418 disk->d_consumer = cp;
419 disk->d_consumer->private = disk;
420 disk->d_consumer->index = 0;
422 G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk));
427 g_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
434 if (cp->provider != NULL)
435 g_mirror_kill_consumer(sc, cp);
437 g_destroy_consumer(cp);
441 * Initialize disk. This means allocate memory, create consumer, attach it
442 * to the provider and open access (r1w1e1) to it.
444 static struct g_mirror_disk *
445 g_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp,
446 struct g_mirror_metadata *md, int *errorp)
448 struct g_mirror_disk *disk;
451 disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO);
457 error = g_mirror_connect_disk(disk, pp);
460 disk->d_id = md->md_did;
461 disk->d_state = G_MIRROR_DISK_STATE_NONE;
462 disk->d_priority = md->md_priority;
463 disk->d_flags = md->md_dflags;
464 error = g_getattr("GEOM::candelete", disk->d_consumer, &i);
465 if (error == 0 && i != 0)
466 disk->d_flags |= G_MIRROR_DISK_FLAG_CANDELETE;
467 if (md->md_provider[0] != '\0')
468 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
469 disk->d_sync.ds_consumer = NULL;
470 disk->d_sync.ds_offset = md->md_sync_offset;
471 disk->d_sync.ds_offset_done = md->md_sync_offset;
472 disk->d_sync.ds_update_ts = time_uptime;
473 disk->d_genid = md->md_genid;
474 disk->d_sync.ds_syncid = md->md_syncid;
482 free(disk, M_MIRROR);
487 g_mirror_destroy_disk(struct g_mirror_disk *disk)
489 struct g_mirror_softc *sc;
491 g_topology_assert_not();
493 sx_assert(&sc->sc_lock, SX_XLOCKED);
495 LIST_REMOVE(disk, d_next);
496 g_mirror_event_cancel(disk);
497 if (sc->sc_hint == disk)
499 switch (disk->d_state) {
500 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
501 g_mirror_sync_stop(disk, 1);
503 case G_MIRROR_DISK_STATE_NEW:
504 case G_MIRROR_DISK_STATE_STALE:
505 case G_MIRROR_DISK_STATE_ACTIVE:
507 g_mirror_disconnect_consumer(sc, disk->d_consumer);
509 free(disk, M_MIRROR);
512 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
513 g_mirror_get_diskname(disk),
514 g_mirror_disk_state2str(disk->d_state)));
519 g_mirror_free_device(struct g_mirror_softc *sc)
522 mtx_destroy(&sc->sc_queue_mtx);
523 mtx_destroy(&sc->sc_events_mtx);
524 mtx_destroy(&sc->sc_done_mtx);
525 sx_destroy(&sc->sc_lock);
530 g_mirror_providergone(struct g_provider *pp)
532 struct g_mirror_softc *sc = pp->private;
534 if ((--sc->sc_refcnt) == 0)
535 g_mirror_free_device(sc);
539 g_mirror_destroy_device(struct g_mirror_softc *sc)
541 struct g_mirror_disk *disk;
542 struct g_mirror_event *ep;
544 struct g_consumer *cp, *tmpcp;
546 g_topology_assert_not();
547 sx_assert(&sc->sc_lock, SX_XLOCKED);
550 if (sc->sc_provider != NULL)
551 g_mirror_destroy_provider(sc);
552 for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL;
553 disk = LIST_FIRST(&sc->sc_disks)) {
554 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
555 g_mirror_update_metadata(disk);
556 g_mirror_destroy_disk(disk);
558 while ((ep = g_mirror_event_first(sc)) != NULL) {
559 g_mirror_event_remove(sc, ep);
560 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
561 g_mirror_event_free(ep);
563 ep->e_error = ECANCELED;
564 ep->e_flags |= G_MIRROR_EVENT_DONE;
565 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep);
566 mtx_lock(&sc->sc_events_mtx);
568 mtx_unlock(&sc->sc_events_mtx);
571 callout_drain(&sc->sc_callout);
574 LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
575 g_mirror_disconnect_consumer(sc, cp);
577 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
578 G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
579 g_wither_geom(gp, ENXIO);
580 sx_xunlock(&sc->sc_lock);
581 if ((--sc->sc_refcnt) == 0)
582 g_mirror_free_device(sc);
587 g_mirror_orphan(struct g_consumer *cp)
589 struct g_mirror_disk *disk;
596 disk->d_softc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
597 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
598 G_MIRROR_EVENT_DONTWAIT);
602 * Function should return the next active disk on the list.
603 * It is possible that it will be the same disk as given.
604 * If there are no active disks on list, NULL is returned.
606 static __inline struct g_mirror_disk *
607 g_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
609 struct g_mirror_disk *dp;
611 for (dp = LIST_NEXT(disk, d_next); dp != disk;
612 dp = LIST_NEXT(dp, d_next)) {
614 dp = LIST_FIRST(&sc->sc_disks);
615 if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE)
618 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
623 static struct g_mirror_disk *
624 g_mirror_get_disk(struct g_mirror_softc *sc)
626 struct g_mirror_disk *disk;
628 if (sc->sc_hint == NULL) {
629 sc->sc_hint = LIST_FIRST(&sc->sc_disks);
630 if (sc->sc_hint == NULL)
634 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) {
635 disk = g_mirror_find_next(sc, disk);
639 sc->sc_hint = g_mirror_find_next(sc, disk);
644 g_mirror_write_metadata(struct g_mirror_disk *disk,
645 struct g_mirror_metadata *md)
647 struct g_mirror_softc *sc;
648 struct g_consumer *cp;
649 off_t offset, length;
653 g_topology_assert_not();
655 sx_assert(&sc->sc_lock, SX_LOCKED);
657 cp = disk->d_consumer;
658 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
659 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
660 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
661 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
663 length = cp->provider->sectorsize;
664 offset = cp->provider->mediasize - length;
665 sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO);
667 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0) {
669 * Handle the case, when the size of parent provider reduced.
671 if (offset < md->md_mediasize)
674 mirror_metadata_encode(md, sector);
676 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_metadata_write, error);
678 error = g_write_data(cp, offset, sector, length);
679 free(sector, M_MIRROR);
681 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
682 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
683 G_MIRROR_DEBUG(0, "Cannot write metadata on %s "
684 "(device=%s, error=%d).",
685 g_mirror_get_diskname(disk), sc->sc_name, error);
687 G_MIRROR_DEBUG(1, "Cannot write metadata on %s "
688 "(device=%s, error=%d).",
689 g_mirror_get_diskname(disk), sc->sc_name, error);
691 if (g_mirror_disconnect_on_failure &&
692 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) {
693 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
694 g_mirror_event_send(disk,
695 G_MIRROR_DISK_STATE_DISCONNECTED,
696 G_MIRROR_EVENT_DONTWAIT);
703 g_mirror_clear_metadata(struct g_mirror_disk *disk)
707 g_topology_assert_not();
708 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
710 if (disk->d_softc->sc_type != G_MIRROR_TYPE_AUTOMATIC)
712 error = g_mirror_write_metadata(disk, NULL);
714 G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
715 g_mirror_get_diskname(disk));
718 "Cannot clear metadata on disk %s (error=%d).",
719 g_mirror_get_diskname(disk), error);
725 g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
726 struct g_mirror_metadata *md)
729 strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
730 md->md_version = G_MIRROR_VERSION;
731 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
732 md->md_mid = sc->sc_id;
733 md->md_all = sc->sc_ndisks;
734 md->md_slice = sc->sc_slice;
735 md->md_balance = sc->sc_balance;
736 md->md_genid = sc->sc_genid;
737 md->md_mediasize = sc->sc_mediasize;
738 md->md_sectorsize = sc->sc_sectorsize;
739 md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
740 bzero(md->md_provider, sizeof(md->md_provider));
742 md->md_did = arc4random();
746 md->md_sync_offset = 0;
749 md->md_did = disk->d_id;
750 md->md_priority = disk->d_priority;
751 md->md_syncid = disk->d_sync.ds_syncid;
752 md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
753 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
754 md->md_sync_offset = disk->d_sync.ds_offset_done;
756 md->md_sync_offset = 0;
757 if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
758 strlcpy(md->md_provider,
759 disk->d_consumer->provider->name,
760 sizeof(md->md_provider));
762 md->md_provsize = disk->d_consumer->provider->mediasize;
767 g_mirror_update_metadata(struct g_mirror_disk *disk)
769 struct g_mirror_softc *sc;
770 struct g_mirror_metadata md;
773 g_topology_assert_not();
775 sx_assert(&sc->sc_lock, SX_LOCKED);
777 if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC)
779 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0)
780 g_mirror_fill_metadata(sc, disk, &md);
781 error = g_mirror_write_metadata(disk, &md);
783 G_MIRROR_DEBUG(2, "Metadata on %s updated.",
784 g_mirror_get_diskname(disk));
787 "Cannot update metadata on disk %s (error=%d).",
788 g_mirror_get_diskname(disk), error);
793 g_mirror_bump_syncid(struct g_mirror_softc *sc)
795 struct g_mirror_disk *disk;
797 g_topology_assert_not();
798 sx_assert(&sc->sc_lock, SX_XLOCKED);
799 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
800 ("%s called with no active disks (device=%s).", __func__,
804 G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
806 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
807 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
808 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
809 disk->d_sync.ds_syncid = sc->sc_syncid;
810 g_mirror_update_metadata(disk);
816 g_mirror_bump_genid(struct g_mirror_softc *sc)
818 struct g_mirror_disk *disk;
820 g_topology_assert_not();
821 sx_assert(&sc->sc_lock, SX_XLOCKED);
822 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
823 ("%s called with no active disks (device=%s).", __func__,
827 G_MIRROR_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
829 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
830 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
831 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
832 disk->d_genid = sc->sc_genid;
833 g_mirror_update_metadata(disk);
839 g_mirror_idle(struct g_mirror_softc *sc, int acw)
841 struct g_mirror_disk *disk;
844 g_topology_assert_not();
845 sx_assert(&sc->sc_lock, SX_XLOCKED);
847 if (sc->sc_provider == NULL)
849 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
853 if (sc->sc_writes > 0)
855 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
856 timeout = g_mirror_idletime - (time_uptime - sc->sc_last_write);
857 if (!g_mirror_shutdown && timeout > 0)
861 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
862 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
864 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as clean.",
865 g_mirror_get_diskname(disk), sc->sc_name);
866 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
867 g_mirror_update_metadata(disk);
873 g_mirror_unidle(struct g_mirror_softc *sc)
875 struct g_mirror_disk *disk;
877 g_topology_assert_not();
878 sx_assert(&sc->sc_lock, SX_XLOCKED);
880 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
883 sc->sc_last_write = time_uptime;
884 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
885 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
887 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as dirty.",
888 g_mirror_get_diskname(disk), sc->sc_name);
889 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
890 g_mirror_update_metadata(disk);
895 g_mirror_flush_done(struct bio *bp)
897 struct g_mirror_softc *sc;
900 pbp = bp->bio_parent;
901 sc = pbp->bio_to->private;
902 mtx_lock(&sc->sc_done_mtx);
903 if (pbp->bio_error == 0)
904 pbp->bio_error = bp->bio_error;
905 pbp->bio_completed += bp->bio_completed;
907 if (pbp->bio_children == pbp->bio_inbed) {
908 mtx_unlock(&sc->sc_done_mtx);
909 g_io_deliver(pbp, pbp->bio_error);
911 mtx_unlock(&sc->sc_done_mtx);
916 g_mirror_done(struct bio *bp)
918 struct g_mirror_softc *sc;
920 sc = bp->bio_from->geom->softc;
921 bp->bio_cflags = G_MIRROR_BIO_FLAG_REGULAR;
922 mtx_lock(&sc->sc_queue_mtx);
923 bioq_insert_tail(&sc->sc_queue, bp);
924 mtx_unlock(&sc->sc_queue_mtx);
929 g_mirror_regular_request(struct bio *bp)
931 struct g_mirror_softc *sc;
932 struct g_mirror_disk *disk;
935 g_topology_assert_not();
937 pbp = bp->bio_parent;
938 sc = pbp->bio_to->private;
939 bp->bio_from->index--;
940 if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE)
942 disk = bp->bio_from->private;
945 g_mirror_kill_consumer(sc, bp->bio_from);
949 if (bp->bio_cmd == BIO_READ)
950 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_regular_request_read,
952 else if (bp->bio_cmd == BIO_WRITE)
953 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_regular_request_write,
957 KASSERT(pbp->bio_inbed <= pbp->bio_children,
958 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
960 if (bp->bio_error == 0 && pbp->bio_error == 0) {
961 G_MIRROR_LOGREQ(3, bp, "Request delivered.");
963 if (pbp->bio_children == pbp->bio_inbed) {
964 G_MIRROR_LOGREQ(3, pbp, "Request delivered.");
965 pbp->bio_completed = pbp->bio_length;
966 if (pbp->bio_cmd == BIO_WRITE ||
967 pbp->bio_cmd == BIO_DELETE) {
968 bioq_remove(&sc->sc_inflight, pbp);
969 /* Release delayed sync requests if possible. */
970 g_mirror_sync_release(sc);
972 g_io_deliver(pbp, pbp->bio_error);
975 } else if (bp->bio_error != 0) {
976 if (pbp->bio_error == 0)
977 pbp->bio_error = bp->bio_error;
979 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
980 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
981 G_MIRROR_LOGREQ(0, bp,
982 "Request failed (error=%d).",
985 G_MIRROR_LOGREQ(1, bp,
986 "Request failed (error=%d).",
989 if (g_mirror_disconnect_on_failure &&
990 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1)
992 if (bp->bio_error == ENXIO &&
993 bp->bio_cmd == BIO_READ)
994 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
995 else if (bp->bio_error == ENXIO)
996 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID_NOW;
998 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
999 g_mirror_event_send(disk,
1000 G_MIRROR_DISK_STATE_DISCONNECTED,
1001 G_MIRROR_EVENT_DONTWAIT);
1004 switch (pbp->bio_cmd) {
1008 pbp->bio_children--;
1014 switch (pbp->bio_cmd) {
1016 if (pbp->bio_inbed < pbp->bio_children)
1018 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1)
1019 g_io_deliver(pbp, pbp->bio_error);
1022 mtx_lock(&sc->sc_queue_mtx);
1023 bioq_insert_tail(&sc->sc_queue, pbp);
1024 mtx_unlock(&sc->sc_queue_mtx);
1025 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1031 if (pbp->bio_children == 0) {
1033 * All requests failed.
1035 } else if (pbp->bio_inbed < pbp->bio_children) {
1038 } else if (pbp->bio_children == pbp->bio_inbed) {
1039 /* Some requests succeeded. */
1041 pbp->bio_completed = pbp->bio_length;
1043 bioq_remove(&sc->sc_inflight, pbp);
1044 /* Release delayed sync requests if possible. */
1045 g_mirror_sync_release(sc);
1046 g_io_deliver(pbp, pbp->bio_error);
1049 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
1055 g_mirror_sync_done(struct bio *bp)
1057 struct g_mirror_softc *sc;
1059 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
1060 sc = bp->bio_from->geom->softc;
1061 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
1062 mtx_lock(&sc->sc_queue_mtx);
1063 bioq_insert_tail(&sc->sc_queue, bp);
1064 mtx_unlock(&sc->sc_queue_mtx);
1069 g_mirror_candelete(struct bio *bp)
1071 struct g_mirror_softc *sc;
1072 struct g_mirror_disk *disk;
1075 sc = bp->bio_to->private;
1076 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1077 if (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE)
1080 val = (int *)bp->bio_data;
1081 *val = (disk != NULL);
1082 g_io_deliver(bp, 0);
1086 g_mirror_kernel_dump(struct bio *bp)
1088 struct g_mirror_softc *sc;
1089 struct g_mirror_disk *disk;
1091 struct g_kerneldump *gkd;
1094 * We configure dumping to the first component, because this component
1095 * will be used for reading with 'prefer' balance algorithm.
1096 * If the component with the highest priority is currently disconnected
1097 * we will not be able to read the dump after the reboot if it will be
1098 * connected and synchronized later. Can we do something better?
1100 sc = bp->bio_to->private;
1101 disk = LIST_FIRST(&sc->sc_disks);
1103 gkd = (struct g_kerneldump *)bp->bio_data;
1104 if (gkd->length > bp->bio_to->mediasize)
1105 gkd->length = bp->bio_to->mediasize;
1106 cbp = g_clone_bio(bp);
1108 g_io_deliver(bp, ENOMEM);
1111 cbp->bio_done = g_std_done;
1112 g_io_request(cbp, disk->d_consumer);
1113 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1114 g_mirror_get_diskname(disk));
1118 g_mirror_flush(struct g_mirror_softc *sc, struct bio *bp)
1120 struct bio_queue_head queue;
1121 struct g_mirror_disk *disk;
1122 struct g_consumer *cp;
1126 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1127 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1129 cbp = g_clone_bio(bp);
1131 while ((cbp = bioq_takefirst(&queue)) != NULL)
1133 if (bp->bio_error == 0)
1134 bp->bio_error = ENOMEM;
1135 g_io_deliver(bp, bp->bio_error);
1138 bioq_insert_tail(&queue, cbp);
1139 cbp->bio_done = g_mirror_flush_done;
1140 cbp->bio_caller1 = disk;
1141 cbp->bio_to = disk->d_consumer->provider;
1143 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1144 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1145 disk = cbp->bio_caller1;
1146 cbp->bio_caller1 = NULL;
1147 cp = disk->d_consumer;
1148 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1149 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1150 cp->acr, cp->acw, cp->ace));
1151 g_io_request(cbp, disk->d_consumer);
1156 g_mirror_start(struct bio *bp)
1158 struct g_mirror_softc *sc;
1160 sc = bp->bio_to->private;
1162 * If sc == NULL or there are no valid disks, provider's error
1163 * should be set and g_mirror_start() should not be called at all.
1165 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1166 ("Provider's error should be set (error=%d)(mirror=%s).",
1167 bp->bio_to->error, bp->bio_to->name));
1168 G_MIRROR_LOGREQ(3, bp, "Request received.");
1170 switch (bp->bio_cmd) {
1176 g_mirror_flush(sc, bp);
1179 if (!strcmp(bp->bio_attribute, "GEOM::candelete")) {
1180 g_mirror_candelete(bp);
1182 } else if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1183 g_mirror_kernel_dump(bp);
1188 g_io_deliver(bp, EOPNOTSUPP);
1191 mtx_lock(&sc->sc_queue_mtx);
1192 if (bp->bio_to->error != 0) {
1193 mtx_unlock(&sc->sc_queue_mtx);
1194 g_io_deliver(bp, bp->bio_to->error);
1197 bioq_insert_tail(&sc->sc_queue, bp);
1198 mtx_unlock(&sc->sc_queue_mtx);
1199 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1204 * Return TRUE if the given request is colliding with a in-progress
1205 * synchronization request.
1208 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1210 struct g_mirror_disk *disk;
1212 off_t rstart, rend, sstart, send;
1215 if (sc->sc_sync.ds_ndisks == 0)
1217 rstart = bp->bio_offset;
1218 rend = bp->bio_offset + bp->bio_length;
1219 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1220 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1222 for (i = 0; i < g_mirror_syncreqs; i++) {
1223 sbp = disk->d_sync.ds_bios[i];
1226 sstart = sbp->bio_offset;
1227 send = sbp->bio_offset + sbp->bio_length;
1228 if (rend > sstart && rstart < send)
1236 * Return TRUE if the given sync request is colliding with a in-progress regular
1240 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1242 off_t rstart, rend, sstart, send;
1245 if (sc->sc_sync.ds_ndisks == 0)
1247 sstart = sbp->bio_offset;
1248 send = sbp->bio_offset + sbp->bio_length;
1249 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1250 rstart = bp->bio_offset;
1251 rend = bp->bio_offset + bp->bio_length;
1252 if (rend > sstart && rstart < send)
1259 * Puts request onto delayed queue.
1262 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1265 G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1266 bioq_insert_head(&sc->sc_regular_delayed, bp);
1270 * Puts synchronization request onto delayed queue.
1273 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1276 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1277 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1281 * Releases delayed regular requests which don't collide anymore with sync
1285 g_mirror_regular_release(struct g_mirror_softc *sc)
1287 struct bio *bp, *bp2;
1289 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1290 if (g_mirror_sync_collision(sc, bp))
1292 bioq_remove(&sc->sc_regular_delayed, bp);
1293 G_MIRROR_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1294 mtx_lock(&sc->sc_queue_mtx);
1295 bioq_insert_head(&sc->sc_queue, bp);
1296 mtx_unlock(&sc->sc_queue_mtx);
1301 * Releases delayed sync requests which don't collide anymore with regular
1305 g_mirror_sync_release(struct g_mirror_softc *sc)
1307 struct bio *bp, *bp2;
1309 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1310 if (g_mirror_regular_collision(sc, bp))
1312 bioq_remove(&sc->sc_sync_delayed, bp);
1313 G_MIRROR_LOGREQ(2, bp,
1314 "Releasing delayed synchronization request.");
1315 g_io_request(bp, bp->bio_from);
1320 * Free a synchronization request and clear its slot in the array.
1323 g_mirror_sync_request_free(struct g_mirror_disk *disk, struct bio *bp)
1327 if (disk != NULL && disk->d_sync.ds_bios != NULL) {
1328 idx = (int)(uintptr_t)bp->bio_caller1;
1329 KASSERT(disk->d_sync.ds_bios[idx] == bp,
1330 ("unexpected sync BIO at %p:%d", disk, idx));
1331 disk->d_sync.ds_bios[idx] = NULL;
1333 free(bp->bio_data, M_MIRROR);
1338 * Handle synchronization requests.
1339 * Every synchronization request is two-steps process: first, READ request is
1340 * send to active provider and then WRITE request (with read data) to the provider
1341 * being synchronized. When WRITE is finished, new synchronization request is
1345 g_mirror_sync_request(struct bio *bp)
1347 struct g_mirror_softc *sc;
1348 struct g_mirror_disk *disk;
1349 struct g_mirror_disk_sync *sync;
1351 bp->bio_from->index--;
1352 sc = bp->bio_from->geom->softc;
1353 disk = bp->bio_from->private;
1355 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1357 g_mirror_kill_consumer(sc, bp->bio_from);
1358 g_topology_unlock();
1359 g_mirror_sync_request_free(NULL, bp);
1360 sx_xlock(&sc->sc_lock);
1365 * Synchronization request.
1367 switch (bp->bio_cmd) {
1370 struct g_consumer *cp;
1372 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_sync_request_read,
1375 if (bp->bio_error != 0) {
1376 G_MIRROR_LOGREQ(0, bp,
1377 "Synchronization request failed (error=%d).",
1379 g_mirror_sync_request_free(disk, bp);
1382 G_MIRROR_LOGREQ(3, bp,
1383 "Synchronization request half-finished.");
1384 bp->bio_cmd = BIO_WRITE;
1386 cp = disk->d_consumer;
1387 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1388 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1389 cp->acr, cp->acw, cp->ace));
1391 g_io_request(bp, cp);
1400 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_sync_request_write,
1403 if (bp->bio_error != 0) {
1404 G_MIRROR_LOGREQ(0, bp,
1405 "Synchronization request failed (error=%d).",
1407 g_mirror_sync_request_free(disk, bp);
1408 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1409 g_mirror_event_send(disk,
1410 G_MIRROR_DISK_STATE_DISCONNECTED,
1411 G_MIRROR_EVENT_DONTWAIT);
1414 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1415 sync = &disk->d_sync;
1416 if (sync->ds_offset >= sc->sc_mediasize ||
1417 sync->ds_consumer == NULL ||
1418 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1419 /* Don't send more synchronization requests. */
1420 sync->ds_inflight--;
1421 g_mirror_sync_request_free(disk, bp);
1422 if (sync->ds_inflight > 0)
1424 if (sync->ds_consumer == NULL ||
1425 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1428 /* Disk up-to-date, activate it. */
1429 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1430 G_MIRROR_EVENT_DONTWAIT);
1434 /* Send next synchronization request. */
1435 data = bp->bio_data;
1436 idx = (int)(uintptr_t)bp->bio_caller1;
1438 bp->bio_cmd = BIO_READ;
1439 bp->bio_offset = sync->ds_offset;
1440 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1441 sync->ds_offset += bp->bio_length;
1442 bp->bio_done = g_mirror_sync_done;
1443 bp->bio_data = data;
1444 bp->bio_from = sync->ds_consumer;
1445 bp->bio_to = sc->sc_provider;
1446 bp->bio_caller1 = (void *)(uintptr_t)idx;
1447 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1448 sync->ds_consumer->index++;
1450 * Delay the request if it is colliding with a regular request.
1452 if (g_mirror_regular_collision(sc, bp))
1453 g_mirror_sync_delay(sc, bp);
1455 g_io_request(bp, sync->ds_consumer);
1457 /* Release delayed requests if possible. */
1458 g_mirror_regular_release(sc);
1460 /* Find the smallest offset */
1461 offset = sc->sc_mediasize;
1462 for (i = 0; i < g_mirror_syncreqs; i++) {
1463 bp = sync->ds_bios[i];
1464 if (bp != NULL && bp->bio_offset < offset)
1465 offset = bp->bio_offset;
1467 if (g_mirror_sync_period > 0 &&
1468 time_uptime - sync->ds_update_ts > g_mirror_sync_period) {
1469 sync->ds_offset_done = offset;
1470 g_mirror_update_metadata(disk);
1471 sync->ds_update_ts = time_uptime;
1476 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1477 bp->bio_cmd, sc->sc_name));
1483 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1485 struct g_mirror_disk *disk;
1486 struct g_consumer *cp;
1489 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1490 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1494 if (bp->bio_error == 0)
1495 bp->bio_error = ENXIO;
1496 g_io_deliver(bp, bp->bio_error);
1499 cbp = g_clone_bio(bp);
1501 if (bp->bio_error == 0)
1502 bp->bio_error = ENOMEM;
1503 g_io_deliver(bp, bp->bio_error);
1507 * Fill in the component buf structure.
1509 cp = disk->d_consumer;
1510 cbp->bio_done = g_mirror_done;
1511 cbp->bio_to = cp->provider;
1512 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1513 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1514 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1517 g_io_request(cbp, cp);
1521 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1523 struct g_mirror_disk *disk;
1524 struct g_consumer *cp;
1527 disk = g_mirror_get_disk(sc);
1529 if (bp->bio_error == 0)
1530 bp->bio_error = ENXIO;
1531 g_io_deliver(bp, bp->bio_error);
1534 cbp = g_clone_bio(bp);
1536 if (bp->bio_error == 0)
1537 bp->bio_error = ENOMEM;
1538 g_io_deliver(bp, bp->bio_error);
1542 * Fill in the component buf structure.
1544 cp = disk->d_consumer;
1545 cbp->bio_done = g_mirror_done;
1546 cbp->bio_to = cp->provider;
1547 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1548 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1549 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1552 g_io_request(cbp, cp);
1555 #define TRACK_SIZE (1 * 1024 * 1024)
1556 #define LOAD_SCALE 256
1557 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
1560 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1562 struct g_mirror_disk *disk, *dp;
1563 struct g_consumer *cp;
1567 /* Find a disk with the smallest load. */
1570 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1571 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1574 /* If disk head is precisely in position - highly prefer it. */
1575 if (dp->d_last_offset == bp->bio_offset)
1576 prio -= 2 * LOAD_SCALE;
1578 /* If disk head is close to position - prefer it. */
1579 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1580 prio -= 1 * LOAD_SCALE;
1586 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1587 cbp = g_clone_bio(bp);
1589 if (bp->bio_error == 0)
1590 bp->bio_error = ENOMEM;
1591 g_io_deliver(bp, bp->bio_error);
1595 * Fill in the component buf structure.
1597 cp = disk->d_consumer;
1598 cbp->bio_done = g_mirror_done;
1599 cbp->bio_to = cp->provider;
1600 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1601 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1602 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1605 /* Remember last head position */
1606 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1608 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1609 dp->load = (dp->d_consumer->index * LOAD_SCALE +
1612 g_io_request(cbp, cp);
1616 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1618 struct bio_queue_head queue;
1619 struct g_mirror_disk *disk;
1620 struct g_consumer *cp;
1622 off_t left, mod, offset, slice;
1626 if (bp->bio_length <= sc->sc_slice) {
1627 g_mirror_request_round_robin(sc, bp);
1630 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1631 slice = bp->bio_length / ndisks;
1632 mod = slice % sc->sc_provider->sectorsize;
1634 slice += sc->sc_provider->sectorsize - mod;
1636 * Allocate all bios before sending any request, so we can
1637 * return ENOMEM in nice and clean way.
1639 left = bp->bio_length;
1640 offset = bp->bio_offset;
1641 data = bp->bio_data;
1643 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1644 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1646 cbp = g_clone_bio(bp);
1648 while ((cbp = bioq_takefirst(&queue)) != NULL)
1650 if (bp->bio_error == 0)
1651 bp->bio_error = ENOMEM;
1652 g_io_deliver(bp, bp->bio_error);
1655 bioq_insert_tail(&queue, cbp);
1656 cbp->bio_done = g_mirror_done;
1657 cbp->bio_caller1 = disk;
1658 cbp->bio_to = disk->d_consumer->provider;
1659 cbp->bio_offset = offset;
1660 cbp->bio_data = data;
1661 cbp->bio_length = MIN(left, slice);
1662 left -= cbp->bio_length;
1665 offset += cbp->bio_length;
1666 data += cbp->bio_length;
1668 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1669 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1670 disk = cbp->bio_caller1;
1671 cbp->bio_caller1 = NULL;
1672 cp = disk->d_consumer;
1673 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1674 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1675 cp->acr, cp->acw, cp->ace));
1676 disk->d_consumer->index++;
1677 g_io_request(cbp, disk->d_consumer);
1682 g_mirror_register_request(struct bio *bp)
1684 struct g_mirror_softc *sc;
1686 sc = bp->bio_to->private;
1687 switch (bp->bio_cmd) {
1689 switch (sc->sc_balance) {
1690 case G_MIRROR_BALANCE_LOAD:
1691 g_mirror_request_load(sc, bp);
1693 case G_MIRROR_BALANCE_PREFER:
1694 g_mirror_request_prefer(sc, bp);
1696 case G_MIRROR_BALANCE_ROUND_ROBIN:
1697 g_mirror_request_round_robin(sc, bp);
1699 case G_MIRROR_BALANCE_SPLIT:
1700 g_mirror_request_split(sc, bp);
1707 struct g_mirror_disk *disk;
1708 struct g_mirror_disk_sync *sync;
1709 struct bio_queue_head queue;
1710 struct g_consumer *cp;
1714 * Delay the request if it is colliding with a synchronization
1717 if (g_mirror_sync_collision(sc, bp)) {
1718 g_mirror_regular_delay(sc, bp);
1723 g_mirror_unidle(sc);
1725 sc->sc_last_write = time_uptime;
1728 * Bump syncid on first write.
1730 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1731 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1732 g_mirror_bump_syncid(sc);
1736 * Allocate all bios before sending any request, so we can
1737 * return ENOMEM in nice and clean way.
1740 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1741 sync = &disk->d_sync;
1742 switch (disk->d_state) {
1743 case G_MIRROR_DISK_STATE_ACTIVE:
1745 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1746 if (bp->bio_offset >= sync->ds_offset)
1752 if (bp->bio_cmd == BIO_DELETE &&
1753 (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE) == 0)
1755 cbp = g_clone_bio(bp);
1757 while ((cbp = bioq_takefirst(&queue)) != NULL)
1759 if (bp->bio_error == 0)
1760 bp->bio_error = ENOMEM;
1761 g_io_deliver(bp, bp->bio_error);
1764 bioq_insert_tail(&queue, cbp);
1765 cbp->bio_done = g_mirror_done;
1766 cp = disk->d_consumer;
1767 cbp->bio_caller1 = cp;
1768 cbp->bio_to = cp->provider;
1769 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1770 ("Consumer %s not opened (r%dw%de%d).",
1771 cp->provider->name, cp->acr, cp->acw, cp->ace));
1773 if (bioq_first(&queue) == NULL) {
1774 g_io_deliver(bp, EOPNOTSUPP);
1777 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1778 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1779 cp = cbp->bio_caller1;
1780 cbp->bio_caller1 = NULL;
1783 g_io_request(cbp, cp);
1786 * Put request onto inflight queue, so we can check if new
1787 * synchronization requests don't collide with it.
1789 bioq_insert_tail(&sc->sc_inflight, bp);
1793 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1794 bp->bio_cmd, sc->sc_name));
1800 g_mirror_can_destroy(struct g_mirror_softc *sc)
1803 struct g_consumer *cp;
1805 g_topology_assert();
1807 if (gp->softc == NULL)
1809 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_TASTING) != 0)
1811 LIST_FOREACH(cp, &gp->consumer, consumer) {
1812 if (g_mirror_is_busy(sc, cp))
1815 gp = sc->sc_sync.ds_geom;
1816 LIST_FOREACH(cp, &gp->consumer, consumer) {
1817 if (g_mirror_is_busy(sc, cp))
1820 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1826 g_mirror_try_destroy(struct g_mirror_softc *sc)
1829 if (sc->sc_rootmount != NULL) {
1830 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1832 root_mount_rel(sc->sc_rootmount);
1833 sc->sc_rootmount = NULL;
1836 if (!g_mirror_can_destroy(sc)) {
1837 g_topology_unlock();
1840 sc->sc_geom->softc = NULL;
1841 sc->sc_sync.ds_geom->softc = NULL;
1842 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DRAIN) != 0) {
1843 g_topology_unlock();
1844 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1846 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
1847 sx_xunlock(&sc->sc_lock);
1848 wakeup(&sc->sc_worker);
1849 sc->sc_worker = NULL;
1851 g_topology_unlock();
1852 g_mirror_destroy_device(sc);
1861 g_mirror_worker(void *arg)
1863 struct g_mirror_softc *sc;
1864 struct g_mirror_event *ep;
1869 thread_lock(curthread);
1870 sched_prio(curthread, PRIBIO);
1871 thread_unlock(curthread);
1873 sx_xlock(&sc->sc_lock);
1875 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1877 * First take a look at events.
1878 * This is important to handle events before any I/O requests.
1880 ep = g_mirror_event_first(sc);
1882 g_mirror_event_remove(sc, ep);
1883 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1884 /* Update only device status. */
1886 "Running event for device %s.",
1889 g_mirror_update_device(sc, true);
1891 /* Update disk status. */
1892 G_MIRROR_DEBUG(3, "Running event for disk %s.",
1893 g_mirror_get_diskname(ep->e_disk));
1894 ep->e_error = g_mirror_update_disk(ep->e_disk,
1896 if (ep->e_error == 0)
1897 g_mirror_update_device(sc, false);
1899 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1900 KASSERT(ep->e_error == 0,
1901 ("Error cannot be handled."));
1902 g_mirror_event_free(ep);
1904 ep->e_flags |= G_MIRROR_EVENT_DONE;
1905 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1907 mtx_lock(&sc->sc_events_mtx);
1909 mtx_unlock(&sc->sc_events_mtx);
1912 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1913 if (g_mirror_try_destroy(sc)) {
1914 curthread->td_pflags &= ~TDP_GEOM;
1915 G_MIRROR_DEBUG(1, "Thread exiting.");
1919 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1923 * Check if we can mark array as CLEAN and if we can't take
1924 * how much seconds should we wait.
1926 timeout = g_mirror_idle(sc, -1);
1930 /* Get first request from the queue. */
1931 mtx_lock(&sc->sc_queue_mtx);
1932 bp = bioq_takefirst(&sc->sc_queue);
1935 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1936 mtx_unlock(&sc->sc_queue_mtx);
1937 if (g_mirror_try_destroy(sc)) {
1938 curthread->td_pflags &= ~TDP_GEOM;
1939 G_MIRROR_DEBUG(1, "Thread exiting.");
1942 mtx_lock(&sc->sc_queue_mtx);
1943 if (bioq_first(&sc->sc_queue) != NULL) {
1944 mtx_unlock(&sc->sc_queue_mtx);
1948 if (g_mirror_event_first(sc) != NULL)
1950 sx_xunlock(&sc->sc_lock);
1951 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
1953 sx_xlock(&sc->sc_lock);
1954 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
1957 mtx_unlock(&sc->sc_queue_mtx);
1959 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
1960 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
1961 g_mirror_sync_request(bp); /* READ */
1962 } else if (bp->bio_to != sc->sc_provider) {
1963 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
1964 g_mirror_regular_request(bp);
1965 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
1966 g_mirror_sync_request(bp); /* WRITE */
1969 ("Invalid request cflags=0x%hx to=%s.",
1970 bp->bio_cflags, bp->bio_to->name));
1973 g_mirror_register_request(bp);
1975 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
1980 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
1983 sx_assert(&sc->sc_lock, SX_LOCKED);
1985 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
1987 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
1988 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as dirty.",
1989 g_mirror_get_diskname(disk), sc->sc_name);
1990 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1991 } else if (sc->sc_idle &&
1992 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
1993 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as clean.",
1994 g_mirror_get_diskname(disk), sc->sc_name);
1995 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2000 g_mirror_sync_start(struct g_mirror_disk *disk)
2002 struct g_mirror_softc *sc;
2003 struct g_consumer *cp;
2007 g_topology_assert_not();
2009 sx_assert(&sc->sc_lock, SX_LOCKED);
2011 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2012 ("Disk %s is not marked for synchronization.",
2013 g_mirror_get_diskname(disk)));
2014 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2015 ("Device not in RUNNING state (%s, %u).", sc->sc_name,
2018 sx_xunlock(&sc->sc_lock);
2020 cp = g_new_consumer(sc->sc_sync.ds_geom);
2021 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
2022 error = g_attach(cp, sc->sc_provider);
2024 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
2025 error = g_access(cp, 1, 0, 0);
2026 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
2027 g_topology_unlock();
2028 sx_xlock(&sc->sc_lock);
2030 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
2031 g_mirror_get_diskname(disk));
2032 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
2033 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
2034 KASSERT(disk->d_sync.ds_consumer == NULL,
2035 ("Sync consumer already exists (device=%s, disk=%s).",
2036 sc->sc_name, g_mirror_get_diskname(disk)));
2038 disk->d_sync.ds_consumer = cp;
2039 disk->d_sync.ds_consumer->private = disk;
2040 disk->d_sync.ds_consumer->index = 0;
2043 * Allocate memory for synchronization bios and initialize them.
2045 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
2046 M_MIRROR, M_WAITOK);
2047 for (i = 0; i < g_mirror_syncreqs; i++) {
2049 disk->d_sync.ds_bios[i] = bp;
2050 bp->bio_parent = NULL;
2051 bp->bio_cmd = BIO_READ;
2052 bp->bio_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK);
2054 bp->bio_offset = disk->d_sync.ds_offset;
2055 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
2056 disk->d_sync.ds_offset += bp->bio_length;
2057 bp->bio_done = g_mirror_sync_done;
2058 bp->bio_from = disk->d_sync.ds_consumer;
2059 bp->bio_to = sc->sc_provider;
2060 bp->bio_caller1 = (void *)(uintptr_t)i;
2063 /* Increase the number of disks in SYNCHRONIZING state. */
2064 sc->sc_sync.ds_ndisks++;
2065 /* Set the number of in-flight synchronization requests. */
2066 disk->d_sync.ds_inflight = g_mirror_syncreqs;
2069 * Fire off first synchronization requests.
2071 for (i = 0; i < g_mirror_syncreqs; i++) {
2072 bp = disk->d_sync.ds_bios[i];
2073 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
2074 disk->d_sync.ds_consumer->index++;
2076 * Delay the request if it is colliding with a regular request.
2078 if (g_mirror_regular_collision(sc, bp))
2079 g_mirror_sync_delay(sc, bp);
2081 g_io_request(bp, disk->d_sync.ds_consumer);
2086 * Stop synchronization process.
2087 * type: 0 - synchronization finished
2088 * 1 - synchronization stopped
2091 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
2093 struct g_mirror_softc *sc;
2094 struct g_consumer *cp;
2096 g_topology_assert_not();
2098 sx_assert(&sc->sc_lock, SX_LOCKED);
2100 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2101 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2102 g_mirror_disk_state2str(disk->d_state)));
2103 if (disk->d_sync.ds_consumer == NULL)
2107 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2108 sc->sc_name, g_mirror_get_diskname(disk));
2109 } else /* if (type == 1) */ {
2110 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2111 sc->sc_name, g_mirror_get_diskname(disk));
2113 g_mirror_regular_release(sc);
2114 free(disk->d_sync.ds_bios, M_MIRROR);
2115 disk->d_sync.ds_bios = NULL;
2116 cp = disk->d_sync.ds_consumer;
2117 disk->d_sync.ds_consumer = NULL;
2118 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2119 sc->sc_sync.ds_ndisks--;
2120 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2122 g_mirror_kill_consumer(sc, cp);
2123 g_topology_unlock();
2124 sx_xlock(&sc->sc_lock);
2128 g_mirror_launch_provider(struct g_mirror_softc *sc)
2130 struct g_mirror_disk *disk;
2131 struct g_provider *pp, *dp;
2133 sx_assert(&sc->sc_lock, SX_LOCKED);
2136 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2137 pp->flags |= G_PF_DIRECT_RECEIVE;
2138 pp->mediasize = sc->sc_mediasize;
2139 pp->sectorsize = sc->sc_sectorsize;
2141 pp->stripeoffset = 0;
2143 /* Splitting of unmapped BIO's could work but isn't implemented now */
2144 if (sc->sc_balance != G_MIRROR_BALANCE_SPLIT)
2145 pp->flags |= G_PF_ACCEPT_UNMAPPED;
2147 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2148 if (disk->d_consumer && disk->d_consumer->provider) {
2149 dp = disk->d_consumer->provider;
2150 if (dp->stripesize > pp->stripesize) {
2151 pp->stripesize = dp->stripesize;
2152 pp->stripeoffset = dp->stripeoffset;
2154 /* A provider underneath us doesn't support unmapped */
2155 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
2156 G_MIRROR_DEBUG(0, "Cancelling unmapped "
2157 "because of %s.", dp->name);
2158 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
2164 sc->sc_provider = pp;
2165 g_error_provider(pp, 0);
2166 g_topology_unlock();
2167 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2168 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2169 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2170 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2171 g_mirror_sync_start(disk);
2176 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2178 struct g_mirror_disk *disk;
2181 g_topology_assert_not();
2182 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2185 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2186 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2187 g_mirror_sync_stop(disk, 1);
2191 g_error_provider(sc->sc_provider, ENXIO);
2192 mtx_lock(&sc->sc_queue_mtx);
2193 while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) {
2195 * Abort any pending I/O that wasn't generated by us.
2196 * Synchronization requests and requests destined for individual
2197 * mirror components can be destroyed immediately.
2199 if (bp->bio_to == sc->sc_provider &&
2200 bp->bio_from->geom != sc->sc_sync.ds_geom) {
2201 g_io_deliver(bp, ENXIO);
2203 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
2204 free(bp->bio_data, M_MIRROR);
2208 mtx_unlock(&sc->sc_queue_mtx);
2209 g_wither_provider(sc->sc_provider, ENXIO);
2210 sc->sc_provider = NULL;
2211 G_MIRROR_DEBUG(0, "Device %s: provider destroyed.", sc->sc_name);
2212 g_topology_unlock();
2216 g_mirror_go(void *arg)
2218 struct g_mirror_softc *sc;
2221 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2222 g_mirror_event_send(sc, 0,
2223 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2227 g_mirror_determine_state(struct g_mirror_disk *disk)
2229 struct g_mirror_softc *sc;
2233 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2234 if ((disk->d_flags &
2235 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0 &&
2236 (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 ||
2237 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0)) {
2238 /* Disk does not need synchronization. */
2239 state = G_MIRROR_DISK_STATE_ACTIVE;
2242 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2244 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2246 * We can start synchronization from
2247 * the stored offset.
2249 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2251 state = G_MIRROR_DISK_STATE_STALE;
2254 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2256 * Reset all synchronization data for this disk,
2257 * because if it even was synchronized, it was
2258 * synchronized to disks with different syncid.
2260 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2261 disk->d_sync.ds_offset = 0;
2262 disk->d_sync.ds_offset_done = 0;
2263 disk->d_sync.ds_syncid = sc->sc_syncid;
2264 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2265 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2266 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2268 state = G_MIRROR_DISK_STATE_STALE;
2270 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2272 * Not good, NOT GOOD!
2273 * It means that mirror was started on stale disks
2274 * and more fresh disk just arrive.
2275 * If there were writes, mirror is broken, sorry.
2276 * I think the best choice here is don't touch
2277 * this disk and inform the user loudly.
2279 G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2280 "disk (%s) arrives!! It will not be connected to the "
2281 "running device.", sc->sc_name,
2282 g_mirror_get_diskname(disk));
2283 g_mirror_destroy_disk(disk);
2284 state = G_MIRROR_DISK_STATE_NONE;
2285 /* Return immediately, because disk was destroyed. */
2288 G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2289 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2294 * Update device state.
2297 g_mirror_update_device(struct g_mirror_softc *sc, bool force)
2299 struct g_mirror_disk *disk;
2302 sx_assert(&sc->sc_lock, SX_XLOCKED);
2304 switch (sc->sc_state) {
2305 case G_MIRROR_DEVICE_STATE_STARTING:
2307 struct g_mirror_disk *pdisk, *tdisk;
2308 u_int dirty, ndisks, genid, syncid;
2311 KASSERT(sc->sc_provider == NULL,
2312 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2314 * Are we ready? We are, if all disks are connected or
2315 * if we have any disks and 'force' is true.
2317 ndisks = g_mirror_ndisks(sc, -1);
2318 if (sc->sc_ndisks == ndisks || (force && ndisks > 0)) {
2320 } else if (ndisks == 0) {
2322 * Disks went down in starting phase, so destroy
2325 callout_drain(&sc->sc_callout);
2326 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2327 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2329 root_mount_rel(sc->sc_rootmount);
2330 sc->sc_rootmount = NULL;
2337 * Activate all disks with the biggest syncid.
2341 * If 'force' is true, we have been called due to
2342 * timeout, so don't bother canceling timeout.
2345 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2346 if ((disk->d_flags &
2347 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2352 /* No valid disks found, destroy device. */
2353 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2354 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2355 __LINE__, sc->sc_rootmount);
2356 root_mount_rel(sc->sc_rootmount);
2357 sc->sc_rootmount = NULL;
2361 /* Cancel timeout. */
2362 callout_drain(&sc->sc_callout);
2366 * Find the biggest genid.
2369 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2370 if (disk->d_genid > genid)
2371 genid = disk->d_genid;
2373 sc->sc_genid = genid;
2375 * Remove all disks without the biggest genid.
2378 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2379 if (disk->d_genid < genid) {
2381 "Component %s (device %s) broken, skipping.",
2382 g_mirror_get_diskname(disk), sc->sc_name);
2383 g_mirror_destroy_disk(disk);
2385 * Bump the syncid in case we discover a healthy
2386 * replacement disk after starting the mirror.
2393 * Find the biggest syncid.
2396 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2397 if (disk->d_sync.ds_syncid > syncid)
2398 syncid = disk->d_sync.ds_syncid;
2402 * Here we need to look for dirty disks and if all disks
2403 * with the biggest syncid are dirty, we have to choose
2404 * one with the biggest priority and rebuild the rest.
2407 * Find the number of dirty disks with the biggest syncid.
2408 * Find the number of disks with the biggest syncid.
2409 * While here, find a disk with the biggest priority.
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) {
2421 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2423 if (pdisk == NULL ||
2424 pdisk->d_priority < disk->d_priority) {
2430 /* No dirty disks at all, great. */
2431 } else if (dirty == ndisks) {
2433 * Force synchronization for all dirty disks except one
2434 * with the biggest priority.
2436 KASSERT(pdisk != NULL, ("pdisk == NULL"));
2437 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2438 "master disk for synchronization.",
2439 g_mirror_get_diskname(pdisk), sc->sc_name);
2440 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2441 if (disk->d_sync.ds_syncid != syncid)
2443 if ((disk->d_flags &
2444 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2447 KASSERT((disk->d_flags &
2448 G_MIRROR_DISK_FLAG_DIRTY) != 0,
2449 ("Disk %s isn't marked as dirty.",
2450 g_mirror_get_diskname(disk)));
2451 /* Skip the disk with the biggest priority. */
2454 disk->d_sync.ds_syncid = 0;
2456 } else if (dirty < ndisks) {
2458 * Force synchronization for all dirty disks.
2459 * We have some non-dirty disks.
2461 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2462 if (disk->d_sync.ds_syncid != syncid)
2464 if ((disk->d_flags &
2465 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2468 if ((disk->d_flags &
2469 G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2472 disk->d_sync.ds_syncid = 0;
2478 sc->sc_syncid = syncid;
2479 if (force || broken) {
2480 /* Remember to bump syncid on first write. */
2481 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2483 state = G_MIRROR_DEVICE_STATE_RUNNING;
2484 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2485 sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2486 g_mirror_device_state2str(state));
2487 sc->sc_state = state;
2488 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2489 state = g_mirror_determine_state(disk);
2490 g_mirror_event_send(disk, state,
2491 G_MIRROR_EVENT_DONTWAIT);
2492 if (state == G_MIRROR_DISK_STATE_STALE)
2493 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2497 case G_MIRROR_DEVICE_STATE_RUNNING:
2498 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2499 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2501 * No usable disks, so destroy the device.
2503 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2505 } else if (g_mirror_ndisks(sc,
2506 G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2507 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2509 * We have active disks, launch provider if it doesn't
2512 if (sc->sc_provider == NULL)
2513 g_mirror_launch_provider(sc);
2514 if (sc->sc_rootmount != NULL) {
2515 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2516 __LINE__, sc->sc_rootmount);
2517 root_mount_rel(sc->sc_rootmount);
2518 sc->sc_rootmount = NULL;
2522 * Genid should be bumped immediately, so do it here.
2524 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2525 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2526 g_mirror_bump_genid(sc);
2528 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID_NOW) != 0) {
2529 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID_NOW;
2530 g_mirror_bump_syncid(sc);
2534 KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2535 sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2541 * Update disk state and device state if needed.
2543 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \
2544 "Disk %s state changed from %s to %s (device %s).", \
2545 g_mirror_get_diskname(disk), \
2546 g_mirror_disk_state2str(disk->d_state), \
2547 g_mirror_disk_state2str(state), sc->sc_name)
2549 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2551 struct g_mirror_softc *sc;
2554 sx_assert(&sc->sc_lock, SX_XLOCKED);
2557 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2558 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2559 g_mirror_disk_state2str(state));
2561 case G_MIRROR_DISK_STATE_NEW:
2563 * Possible scenarios:
2564 * 1. New disk arrive.
2566 /* Previous state should be NONE. */
2567 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2568 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2569 g_mirror_disk_state2str(disk->d_state)));
2570 DISK_STATE_CHANGED();
2572 disk->d_state = state;
2573 if (LIST_EMPTY(&sc->sc_disks))
2574 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2576 struct g_mirror_disk *dp;
2578 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2579 if (disk->d_priority >= dp->d_priority) {
2580 LIST_INSERT_BEFORE(dp, disk, d_next);
2584 if (LIST_NEXT(dp, d_next) == NULL)
2588 LIST_INSERT_AFTER(dp, disk, d_next);
2590 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2591 sc->sc_name, g_mirror_get_diskname(disk));
2592 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
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)));
2599 state = g_mirror_determine_state(disk);
2600 if (state != G_MIRROR_DISK_STATE_NONE)
2603 case G_MIRROR_DISK_STATE_ACTIVE:
2605 * Possible scenarios:
2606 * 1. New disk does not need synchronization.
2607 * 2. Synchronization process finished successfully.
2609 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2610 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2611 g_mirror_device_state2str(sc->sc_state),
2612 g_mirror_get_diskname(disk),
2613 g_mirror_disk_state2str(disk->d_state)));
2614 /* Previous state should be NEW or SYNCHRONIZING. */
2615 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2616 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2617 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2618 g_mirror_disk_state2str(disk->d_state)));
2619 DISK_STATE_CHANGED();
2621 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2622 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2623 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2624 g_mirror_sync_stop(disk, 0);
2626 disk->d_state = state;
2627 disk->d_sync.ds_offset = 0;
2628 disk->d_sync.ds_offset_done = 0;
2629 g_mirror_update_idle(sc, disk);
2630 g_mirror_update_metadata(disk);
2631 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2632 sc->sc_name, g_mirror_get_diskname(disk));
2634 case G_MIRROR_DISK_STATE_STALE:
2636 * Possible scenarios:
2637 * 1. Stale disk was connected.
2639 /* Previous state should be NEW. */
2640 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2641 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2642 g_mirror_disk_state2str(disk->d_state)));
2643 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2644 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2645 g_mirror_device_state2str(sc->sc_state),
2646 g_mirror_get_diskname(disk),
2647 g_mirror_disk_state2str(disk->d_state)));
2649 * STALE state is only possible if device is marked
2652 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2653 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2654 g_mirror_device_state2str(sc->sc_state),
2655 g_mirror_get_diskname(disk),
2656 g_mirror_disk_state2str(disk->d_state)));
2657 DISK_STATE_CHANGED();
2659 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2660 disk->d_state = state;
2661 g_mirror_update_metadata(disk);
2662 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2663 sc->sc_name, g_mirror_get_diskname(disk));
2665 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2667 * Possible scenarios:
2668 * 1. Disk which needs synchronization was connected.
2670 /* Previous state should be NEW. */
2671 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2672 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2673 g_mirror_disk_state2str(disk->d_state)));
2674 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2675 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2676 g_mirror_device_state2str(sc->sc_state),
2677 g_mirror_get_diskname(disk),
2678 g_mirror_disk_state2str(disk->d_state)));
2679 DISK_STATE_CHANGED();
2681 if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2682 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2683 disk->d_state = state;
2684 if (sc->sc_provider != NULL) {
2685 g_mirror_sync_start(disk);
2686 g_mirror_update_metadata(disk);
2689 case G_MIRROR_DISK_STATE_DISCONNECTED:
2691 * Possible scenarios:
2692 * 1. Device wasn't running yet, but disk disappear.
2693 * 2. Disk was active and disapppear.
2694 * 3. Disk disappear during synchronization process.
2696 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2698 * Previous state should be ACTIVE, STALE or
2701 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2702 disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2703 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2704 ("Wrong disk state (%s, %s).",
2705 g_mirror_get_diskname(disk),
2706 g_mirror_disk_state2str(disk->d_state)));
2707 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2708 /* Previous state should be NEW. */
2709 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2710 ("Wrong disk state (%s, %s).",
2711 g_mirror_get_diskname(disk),
2712 g_mirror_disk_state2str(disk->d_state)));
2714 * Reset bumping syncid if disk disappeared in STARTING
2717 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2718 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2721 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2723 g_mirror_device_state2str(sc->sc_state),
2724 g_mirror_get_diskname(disk),
2725 g_mirror_disk_state2str(disk->d_state)));
2728 DISK_STATE_CHANGED();
2729 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2730 sc->sc_name, g_mirror_get_diskname(disk));
2732 g_mirror_destroy_disk(disk);
2734 case G_MIRROR_DISK_STATE_DESTROY:
2738 error = g_mirror_clear_metadata(disk);
2741 "Device %s: failed to clear metadata on %s: %d.",
2742 sc->sc_name, g_mirror_get_diskname(disk), error);
2745 DISK_STATE_CHANGED();
2746 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2747 sc->sc_name, g_mirror_get_diskname(disk));
2749 g_mirror_destroy_disk(disk);
2751 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2752 g_mirror_update_metadata(disk);
2757 KASSERT(1 == 0, ("Unknown state (%u).", state));
2762 #undef DISK_STATE_CHANGED
2765 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2767 struct g_provider *pp;
2771 g_topology_assert();
2773 error = g_access(cp, 1, 0, 0);
2777 g_topology_unlock();
2778 /* Metadata are stored on last sector. */
2779 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2782 g_access(cp, -1, 0, 0);
2784 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2785 cp->provider->name, error);
2789 /* Decode metadata. */
2790 error = mirror_metadata_decode(buf, md);
2792 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2794 if (md->md_version > G_MIRROR_VERSION) {
2796 "Kernel module is too old to handle metadata from %s.",
2797 cp->provider->name);
2801 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2802 cp->provider->name);
2810 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2811 struct g_mirror_metadata *md)
2814 if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2815 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2816 pp->name, md->md_did);
2819 if (md->md_all != sc->sc_ndisks) {
2821 "Invalid '%s' field on disk %s (device %s), skipping.",
2822 "md_all", pp->name, sc->sc_name);
2825 if (md->md_slice != sc->sc_slice) {
2827 "Invalid '%s' field on disk %s (device %s), skipping.",
2828 "md_slice", pp->name, sc->sc_name);
2831 if (md->md_balance != sc->sc_balance) {
2833 "Invalid '%s' field on disk %s (device %s), skipping.",
2834 "md_balance", pp->name, sc->sc_name);
2838 if (md->md_mediasize != sc->sc_mediasize) {
2840 "Invalid '%s' field on disk %s (device %s), skipping.",
2841 "md_mediasize", pp->name, sc->sc_name);
2845 if (sc->sc_mediasize > pp->mediasize) {
2847 "Invalid size of disk %s (device %s), skipping.", pp->name,
2851 if (md->md_sectorsize != sc->sc_sectorsize) {
2853 "Invalid '%s' field on disk %s (device %s), skipping.",
2854 "md_sectorsize", pp->name, sc->sc_name);
2857 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2859 "Invalid sector size of disk %s (device %s), skipping.",
2860 pp->name, sc->sc_name);
2863 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2865 "Invalid device flags on disk %s (device %s), skipping.",
2866 pp->name, sc->sc_name);
2869 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2871 "Invalid disk flags on disk %s (device %s), skipping.",
2872 pp->name, sc->sc_name);
2879 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2880 struct g_mirror_metadata *md)
2882 struct g_mirror_disk *disk;
2885 g_topology_assert_not();
2886 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2888 error = g_mirror_check_metadata(sc, pp, md);
2891 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING &&
2892 md->md_genid < sc->sc_genid) {
2893 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
2894 pp->name, sc->sc_name);
2897 disk = g_mirror_init_disk(sc, pp, md, &error);
2900 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
2901 G_MIRROR_EVENT_WAIT);
2904 if (md->md_version < G_MIRROR_VERSION) {
2905 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
2906 pp->name, md->md_version, G_MIRROR_VERSION);
2907 g_mirror_update_metadata(disk);
2913 g_mirror_destroy_delayed(void *arg, int flag)
2915 struct g_mirror_softc *sc;
2918 if (flag == EV_CANCEL) {
2919 G_MIRROR_DEBUG(1, "Destroying canceled.");
2923 g_topology_unlock();
2924 sx_xlock(&sc->sc_lock);
2925 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
2926 ("DESTROY flag set on %s.", sc->sc_name));
2927 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_CLOSEWAIT) != 0,
2928 ("CLOSEWAIT flag not set on %s.", sc->sc_name));
2929 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
2930 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
2932 G_MIRROR_DEBUG(0, "Cannot destroy %s (error=%d).",
2933 sc->sc_name, error);
2934 sx_xunlock(&sc->sc_lock);
2940 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
2942 struct g_mirror_softc *sc;
2945 g_topology_assert();
2946 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2950 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
2952 g_topology_unlock();
2953 sx_xlock(&sc->sc_lock);
2954 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
2955 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_CLOSEWAIT) != 0 ||
2956 LIST_EMPTY(&sc->sc_disks)) {
2957 if (acr > 0 || acw > 0 || ace > 0)
2961 sc->sc_provider_open += acr + acw + ace;
2962 if (pp->acw + acw == 0)
2963 g_mirror_idle(sc, 0);
2964 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_CLOSEWAIT) != 0 &&
2965 sc->sc_provider_open == 0)
2966 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK, sc, NULL);
2968 sx_xunlock(&sc->sc_lock);
2974 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md,
2977 struct g_mirror_softc *sc;
2981 g_topology_assert();
2982 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
2985 /* One disk is minimum. */
2991 gp = g_new_geomf(mp, "%s", md->md_name);
2992 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
2993 gp->start = g_mirror_start;
2994 gp->orphan = g_mirror_orphan;
2995 gp->access = g_mirror_access;
2996 gp->dumpconf = g_mirror_dumpconf;
2999 sc->sc_id = md->md_mid;
3000 sc->sc_slice = md->md_slice;
3001 sc->sc_balance = md->md_balance;
3002 sc->sc_mediasize = md->md_mediasize;
3003 sc->sc_sectorsize = md->md_sectorsize;
3004 sc->sc_ndisks = md->md_all;
3005 sc->sc_flags = md->md_mflags;
3008 sc->sc_last_write = time_uptime;
3011 sx_init(&sc->sc_lock, "gmirror:lock");
3012 bioq_init(&sc->sc_queue);
3013 mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
3014 bioq_init(&sc->sc_regular_delayed);
3015 bioq_init(&sc->sc_inflight);
3016 bioq_init(&sc->sc_sync_delayed);
3017 LIST_INIT(&sc->sc_disks);
3018 TAILQ_INIT(&sc->sc_events);
3019 mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
3020 callout_init(&sc->sc_callout, 1);
3021 mtx_init(&sc->sc_done_mtx, "gmirror:done", NULL, MTX_DEF);
3022 sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
3025 sc->sc_provider = NULL;
3026 sc->sc_provider_open = 0;
3028 * Synchronization geom.
3030 gp = g_new_geomf(mp, "%s.sync", md->md_name);
3032 gp->orphan = g_mirror_orphan;
3033 sc->sc_sync.ds_geom = gp;
3034 sc->sc_sync.ds_ndisks = 0;
3035 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
3036 "g_mirror %s", md->md_name);
3038 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
3040 g_destroy_geom(sc->sc_sync.ds_geom);
3041 g_destroy_geom(sc->sc_geom);
3042 g_mirror_free_device(sc);
3046 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
3047 sc->sc_name, sc->sc_ndisks, sc->sc_id);
3049 sc->sc_rootmount = root_mount_hold("GMIRROR");
3050 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
3054 timeout = g_mirror_timeout * hz;
3055 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
3056 return (sc->sc_geom);
3060 g_mirror_destroy(struct g_mirror_softc *sc, int how)
3062 struct g_mirror_disk *disk;
3064 g_topology_assert_not();
3065 sx_assert(&sc->sc_lock, SX_XLOCKED);
3067 if (sc->sc_provider_open != 0) {
3069 case G_MIRROR_DESTROY_SOFT:
3071 "Device %s is still open (%d).", sc->sc_name,
3072 sc->sc_provider_open);
3074 case G_MIRROR_DESTROY_DELAYED:
3076 "Device %s will be destroyed on last close.",
3078 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
3079 if (disk->d_state ==
3080 G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3081 g_mirror_sync_stop(disk, 1);
3084 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_CLOSEWAIT;
3086 case G_MIRROR_DESTROY_HARD:
3087 G_MIRROR_DEBUG(1, "Device %s is still open, so it "
3088 "can't be definitely removed.", sc->sc_name);
3092 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3093 sx_xunlock(&sc->sc_lock);
3096 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
3097 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DRAIN;
3098 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3099 sx_xunlock(&sc->sc_lock);
3100 mtx_lock(&sc->sc_queue_mtx);
3102 mtx_unlock(&sc->sc_queue_mtx);
3103 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3104 while (sc->sc_worker != NULL)
3105 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
3106 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3107 sx_xlock(&sc->sc_lock);
3108 g_mirror_destroy_device(sc);
3113 g_mirror_taste_orphan(struct g_consumer *cp)
3116 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3117 cp->provider->name));
3120 static struct g_geom *
3121 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3123 struct g_mirror_metadata md;
3124 struct g_mirror_softc *sc;
3125 struct g_consumer *cp;
3129 g_topology_assert();
3130 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3131 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
3133 gp = g_new_geomf(mp, "mirror:taste");
3135 * This orphan function should be never called.
3137 gp->orphan = g_mirror_taste_orphan;
3138 cp = g_new_consumer(gp);
3140 error = g_mirror_read_metadata(cp, &md);
3142 g_destroy_consumer(cp);
3148 if (md.md_provider[0] != '\0' &&
3149 !g_compare_names(md.md_provider, pp->name))
3151 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3153 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3155 "Device %s: provider %s marked as inactive, skipping.",
3156 md.md_name, pp->name);
3159 if (g_mirror_debug >= 2)
3160 mirror_metadata_dump(&md);
3163 * Let's check if device already exists.
3166 LIST_FOREACH(gp, &mp->geom, geom) {
3170 if (sc->sc_type != G_MIRROR_TYPE_AUTOMATIC)
3172 if (sc->sc_sync.ds_geom == gp)
3174 if (strcmp(md.md_name, sc->sc_name) != 0)
3176 if (md.md_mid != sc->sc_id) {
3177 G_MIRROR_DEBUG(0, "Device %s already configured.",
3184 gp = g_mirror_create(mp, &md, G_MIRROR_TYPE_AUTOMATIC);
3186 G_MIRROR_DEBUG(0, "Cannot create device %s.",
3192 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3193 g_topology_unlock();
3194 sx_xlock(&sc->sc_lock);
3195 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
3196 error = g_mirror_add_disk(sc, pp, &md);
3198 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3199 pp->name, gp->name, error);
3200 if (LIST_EMPTY(&sc->sc_disks)) {
3202 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3208 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
3209 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3210 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3214 sx_xunlock(&sc->sc_lock);
3220 g_mirror_resize(struct g_consumer *cp)
3222 struct g_mirror_disk *disk;
3224 g_topology_assert();
3225 g_trace(G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name);
3230 g_topology_unlock();
3231 g_mirror_update_metadata(disk);
3236 g_mirror_destroy_geom(struct gctl_req *req __unused,
3237 struct g_class *mp __unused, struct g_geom *gp)
3239 struct g_mirror_softc *sc;
3242 g_topology_unlock();
3244 sx_xlock(&sc->sc_lock);
3246 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3248 sx_xunlock(&sc->sc_lock);
3254 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3255 struct g_consumer *cp, struct g_provider *pp)
3257 struct g_mirror_softc *sc;
3259 g_topology_assert();
3264 /* Skip synchronization geom. */
3265 if (gp == sc->sc_sync.ds_geom)
3269 } else if (cp != NULL) {
3270 struct g_mirror_disk *disk;
3275 g_topology_unlock();
3276 sx_xlock(&sc->sc_lock);
3277 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3278 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3279 sbuf_printf(sb, "%s<Synchronized>", indent);
3280 if (disk->d_sync.ds_offset == 0)
3281 sbuf_printf(sb, "0%%");
3283 sbuf_printf(sb, "%u%%",
3284 (u_int)((disk->d_sync.ds_offset * 100) /
3285 sc->sc_provider->mediasize));
3287 sbuf_printf(sb, "</Synchronized>\n");
3288 if (disk->d_sync.ds_offset > 0) {
3289 sbuf_printf(sb, "%s<BytesSynced>%jd"
3290 "</BytesSynced>\n", indent,
3291 (intmax_t)disk->d_sync.ds_offset);
3294 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3295 disk->d_sync.ds_syncid);
3296 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3298 sbuf_printf(sb, "%s<Flags>", indent);
3299 if (disk->d_flags == 0)
3300 sbuf_printf(sb, "NONE");
3304 #define ADD_FLAG(flag, name) do { \
3305 if ((disk->d_flags & (flag)) != 0) { \
3307 sbuf_printf(sb, ", "); \
3310 sbuf_printf(sb, name); \
3313 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3314 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3315 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3316 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3318 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3319 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3322 sbuf_printf(sb, "</Flags>\n");
3323 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3325 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3326 g_mirror_disk_state2str(disk->d_state));
3327 sx_xunlock(&sc->sc_lock);
3330 g_topology_unlock();
3331 sx_xlock(&sc->sc_lock);
3332 sbuf_printf(sb, "%s<Type>", indent);
3333 switch (sc->sc_type) {
3334 case G_MIRROR_TYPE_AUTOMATIC:
3335 sbuf_printf(sb, "AUTOMATIC");
3337 case G_MIRROR_TYPE_MANUAL:
3338 sbuf_printf(sb, "MANUAL");
3341 sbuf_printf(sb, "UNKNOWN");
3344 sbuf_printf(sb, "</Type>\n");
3345 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3346 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3347 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3348 sbuf_printf(sb, "%s<Flags>", indent);
3349 if (sc->sc_flags == 0)
3350 sbuf_printf(sb, "NONE");
3354 #define ADD_FLAG(flag, name) do { \
3355 if ((sc->sc_flags & (flag)) != 0) { \
3357 sbuf_printf(sb, ", "); \
3360 sbuf_printf(sb, name); \
3363 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3364 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3367 sbuf_printf(sb, "</Flags>\n");
3368 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3369 (u_int)sc->sc_slice);
3370 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3371 balance_name(sc->sc_balance));
3372 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3374 sbuf_printf(sb, "%s<State>", indent);
3375 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3376 sbuf_printf(sb, "%s", "STARTING");
3377 else if (sc->sc_ndisks ==
3378 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3379 sbuf_printf(sb, "%s", "COMPLETE");
3381 sbuf_printf(sb, "%s", "DEGRADED");
3382 sbuf_printf(sb, "</State>\n");
3383 sx_xunlock(&sc->sc_lock);
3389 g_mirror_shutdown_post_sync(void *arg, int howto)
3392 struct g_geom *gp, *gp2;
3393 struct g_mirror_softc *sc;
3396 if (panicstr != NULL)
3401 g_mirror_shutdown = 1;
3402 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3403 if ((sc = gp->softc) == NULL)
3405 /* Skip synchronization geom. */
3406 if (gp == sc->sc_sync.ds_geom)
3408 g_topology_unlock();
3409 sx_xlock(&sc->sc_lock);
3410 g_mirror_idle(sc, -1);
3412 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3414 sx_xunlock(&sc->sc_lock);
3417 g_topology_unlock();
3421 g_mirror_init(struct g_class *mp)
3424 g_mirror_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
3425 g_mirror_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
3426 if (g_mirror_post_sync == NULL)
3427 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3431 g_mirror_fini(struct g_class *mp)
3434 if (g_mirror_post_sync != NULL)
3435 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_mirror_post_sync);
3438 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);