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>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/limits.h>
37 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <sys/eventhandler.h>
44 #include <geom/geom.h>
46 #include <sys/kthread.h>
47 #include <sys/sched.h>
48 #include <geom/mirror/g_mirror.h>
50 FEATURE(geom_mirror, "GEOM mirroring support");
52 static MALLOC_DEFINE(M_MIRROR, "mirror_data", "GEOM_MIRROR Data");
54 SYSCTL_DECL(_kern_geom);
55 static SYSCTL_NODE(_kern_geom, OID_AUTO, mirror, CTLFLAG_RW, 0,
57 u_int g_mirror_debug = 0;
58 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, debug, CTLFLAG_RWTUN, &g_mirror_debug, 0,
60 static u_int g_mirror_timeout = 4;
61 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, timeout, CTLFLAG_RWTUN, &g_mirror_timeout,
62 0, "Time to wait on all mirror components");
63 static u_int g_mirror_idletime = 5;
64 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, idletime, CTLFLAG_RWTUN,
65 &g_mirror_idletime, 0, "Mark components as clean when idling");
66 static u_int g_mirror_disconnect_on_failure = 1;
67 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, disconnect_on_failure, CTLFLAG_RWTUN,
68 &g_mirror_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
69 static u_int g_mirror_syncreqs = 2;
70 SYSCTL_UINT(_kern_geom_mirror, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
71 &g_mirror_syncreqs, 0, "Parallel synchronization I/O requests.");
73 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \
74 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
75 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
76 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
79 static eventhandler_tag g_mirror_post_sync = NULL;
80 static int g_mirror_shutdown = 0;
82 static int g_mirror_destroy_geom(struct gctl_req *req, struct g_class *mp,
84 static g_taste_t g_mirror_taste;
85 static g_resize_t g_mirror_resize;
86 static void g_mirror_init(struct g_class *mp);
87 static void g_mirror_fini(struct g_class *mp);
89 struct g_class g_mirror_class = {
90 .name = G_MIRROR_CLASS_NAME,
92 .ctlreq = g_mirror_config,
93 .taste = g_mirror_taste,
94 .destroy_geom = g_mirror_destroy_geom,
95 .init = g_mirror_init,
96 .fini = g_mirror_fini,
97 .resize = g_mirror_resize
101 static void g_mirror_destroy_provider(struct g_mirror_softc *sc);
102 static int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
103 static void g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force);
104 static void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
105 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
106 static void g_mirror_sync_stop(struct g_mirror_disk *disk, int type);
107 static void g_mirror_register_request(struct bio *bp);
108 static void g_mirror_sync_release(struct g_mirror_softc *sc);
112 g_mirror_disk_state2str(int state)
116 case G_MIRROR_DISK_STATE_NONE:
118 case G_MIRROR_DISK_STATE_NEW:
120 case G_MIRROR_DISK_STATE_ACTIVE:
122 case G_MIRROR_DISK_STATE_STALE:
124 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
125 return ("SYNCHRONIZING");
126 case G_MIRROR_DISK_STATE_DISCONNECTED:
127 return ("DISCONNECTED");
128 case G_MIRROR_DISK_STATE_DESTROY:
136 g_mirror_device_state2str(int state)
140 case G_MIRROR_DEVICE_STATE_STARTING:
142 case G_MIRROR_DEVICE_STATE_RUNNING:
150 g_mirror_get_diskname(struct g_mirror_disk *disk)
153 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
154 return ("[unknown]");
155 return (disk->d_name);
159 * --- Events handling functions ---
160 * Events in geom_mirror are used to maintain disks and device status
161 * from one thread to simplify locking.
164 g_mirror_event_free(struct g_mirror_event *ep)
171 g_mirror_event_send(void *arg, int state, int flags)
173 struct g_mirror_softc *sc;
174 struct g_mirror_disk *disk;
175 struct g_mirror_event *ep;
178 ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
179 G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
180 if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
191 mtx_lock(&sc->sc_events_mtx);
192 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
193 mtx_unlock(&sc->sc_events_mtx);
194 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
195 mtx_lock(&sc->sc_queue_mtx);
197 mtx_unlock(&sc->sc_queue_mtx);
198 if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0)
200 sx_assert(&sc->sc_lock, SX_XLOCKED);
201 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
202 sx_xunlock(&sc->sc_lock);
203 while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) {
204 mtx_lock(&sc->sc_events_mtx);
205 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event",
209 g_mirror_event_free(ep);
210 sx_xlock(&sc->sc_lock);
214 static struct g_mirror_event *
215 g_mirror_event_get(struct g_mirror_softc *sc)
217 struct g_mirror_event *ep;
219 mtx_lock(&sc->sc_events_mtx);
220 ep = TAILQ_FIRST(&sc->sc_events);
221 mtx_unlock(&sc->sc_events_mtx);
226 g_mirror_event_remove(struct g_mirror_softc *sc, struct g_mirror_event *ep)
229 mtx_lock(&sc->sc_events_mtx);
230 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
231 mtx_unlock(&sc->sc_events_mtx);
235 g_mirror_event_cancel(struct g_mirror_disk *disk)
237 struct g_mirror_softc *sc;
238 struct g_mirror_event *ep, *tmpep;
241 sx_assert(&sc->sc_lock, SX_XLOCKED);
243 mtx_lock(&sc->sc_events_mtx);
244 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
245 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0)
247 if (ep->e_disk != disk)
249 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
250 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
251 g_mirror_event_free(ep);
253 ep->e_error = ECANCELED;
257 mtx_unlock(&sc->sc_events_mtx);
261 * Return the number of disks in given state.
262 * If state is equal to -1, count all connected disks.
265 g_mirror_ndisks(struct g_mirror_softc *sc, int state)
267 struct g_mirror_disk *disk;
270 sx_assert(&sc->sc_lock, SX_LOCKED);
272 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
273 if (state == -1 || disk->d_state == state)
280 * Find a disk in mirror by its disk ID.
282 static struct g_mirror_disk *
283 g_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id)
285 struct g_mirror_disk *disk;
287 sx_assert(&sc->sc_lock, SX_XLOCKED);
289 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
290 if (disk->d_id == id)
297 g_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp)
302 mtx_lock(&sc->sc_queue_mtx);
303 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
304 if (bp->bio_from == cp)
307 mtx_unlock(&sc->sc_queue_mtx);
312 g_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp)
317 "I/O requests for %s exist, can't destroy it now.",
321 if (g_mirror_nrequests(sc, cp) > 0) {
323 "I/O requests for %s in queue, can't destroy it now.",
331 g_mirror_destroy_consumer(void *arg, int flags __unused)
333 struct g_consumer *cp;
338 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
340 g_destroy_consumer(cp);
344 g_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
346 struct g_provider *pp;
352 if (g_mirror_is_busy(sc, cp))
357 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
360 G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
361 -cp->acw, -cp->ace, 0);
362 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
363 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
366 * After retaste event was send (inside g_access()), we can send
367 * event to detach and destroy consumer.
368 * A class, which has consumer to the given provider connected
369 * will not receive retaste event for the provider.
370 * This is the way how I ignore retaste events when I close
371 * consumers opened for write: I detach and destroy consumer
372 * after retaste event is sent.
374 g_post_event(g_mirror_destroy_consumer, cp, M_WAITOK, NULL);
377 G_MIRROR_DEBUG(1, "Consumer %s destroyed.", pp->name);
379 g_destroy_consumer(cp);
383 g_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp)
385 struct g_consumer *cp;
388 g_topology_assert_not();
389 KASSERT(disk->d_consumer == NULL,
390 ("Disk already connected (device %s).", disk->d_softc->sc_name));
393 cp = g_new_consumer(disk->d_softc->sc_geom);
394 cp->flags |= G_CF_DIRECT_RECEIVE;
395 error = g_attach(cp, pp);
397 g_destroy_consumer(cp);
401 error = g_access(cp, 1, 1, 1);
404 g_destroy_consumer(cp);
406 G_MIRROR_DEBUG(0, "Cannot open consumer %s (error=%d).",
411 disk->d_consumer = cp;
412 disk->d_consumer->private = disk;
413 disk->d_consumer->index = 0;
415 G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk));
420 g_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
427 if (cp->provider != NULL)
428 g_mirror_kill_consumer(sc, cp);
430 g_destroy_consumer(cp);
434 * Initialize disk. This means allocate memory, create consumer, attach it
435 * to the provider and open access (r1w1e1) to it.
437 static struct g_mirror_disk *
438 g_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp,
439 struct g_mirror_metadata *md, int *errorp)
441 struct g_mirror_disk *disk;
444 disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO);
450 error = g_mirror_connect_disk(disk, pp);
453 disk->d_id = md->md_did;
454 disk->d_state = G_MIRROR_DISK_STATE_NONE;
455 disk->d_priority = md->md_priority;
456 disk->d_flags = md->md_dflags;
457 error = g_getattr("GEOM::candelete", disk->d_consumer, &i);
458 if (error == 0 && i != 0)
459 disk->d_flags |= G_MIRROR_DISK_FLAG_CANDELETE;
460 if (md->md_provider[0] != '\0')
461 disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
462 disk->d_sync.ds_consumer = NULL;
463 disk->d_sync.ds_offset = md->md_sync_offset;
464 disk->d_sync.ds_offset_done = md->md_sync_offset;
465 disk->d_genid = md->md_genid;
466 disk->d_sync.ds_syncid = md->md_syncid;
474 free(disk, M_MIRROR);
479 g_mirror_destroy_disk(struct g_mirror_disk *disk)
481 struct g_mirror_softc *sc;
483 g_topology_assert_not();
485 sx_assert(&sc->sc_lock, SX_XLOCKED);
487 LIST_REMOVE(disk, d_next);
488 g_mirror_event_cancel(disk);
489 if (sc->sc_hint == disk)
491 switch (disk->d_state) {
492 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
493 g_mirror_sync_stop(disk, 1);
495 case G_MIRROR_DISK_STATE_NEW:
496 case G_MIRROR_DISK_STATE_STALE:
497 case G_MIRROR_DISK_STATE_ACTIVE:
499 g_mirror_disconnect_consumer(sc, disk->d_consumer);
501 free(disk, M_MIRROR);
504 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
505 g_mirror_get_diskname(disk),
506 g_mirror_disk_state2str(disk->d_state)));
511 g_mirror_destroy_device(struct g_mirror_softc *sc)
513 struct g_mirror_disk *disk;
514 struct g_mirror_event *ep;
516 struct g_consumer *cp, *tmpcp;
518 g_topology_assert_not();
519 sx_assert(&sc->sc_lock, SX_XLOCKED);
522 if (sc->sc_provider != NULL)
523 g_mirror_destroy_provider(sc);
524 for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL;
525 disk = LIST_FIRST(&sc->sc_disks)) {
526 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
527 g_mirror_update_metadata(disk);
528 g_mirror_destroy_disk(disk);
530 while ((ep = g_mirror_event_get(sc)) != NULL) {
531 g_mirror_event_remove(sc, ep);
532 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
533 g_mirror_event_free(ep);
535 ep->e_error = ECANCELED;
536 ep->e_flags |= G_MIRROR_EVENT_DONE;
537 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep);
538 mtx_lock(&sc->sc_events_mtx);
540 mtx_unlock(&sc->sc_events_mtx);
543 callout_drain(&sc->sc_callout);
546 LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
547 g_mirror_disconnect_consumer(sc, cp);
549 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
550 G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
551 g_wither_geom(gp, ENXIO);
553 mtx_destroy(&sc->sc_queue_mtx);
554 mtx_destroy(&sc->sc_events_mtx);
555 mtx_destroy(&sc->sc_done_mtx);
556 sx_xunlock(&sc->sc_lock);
557 sx_destroy(&sc->sc_lock);
561 g_mirror_orphan(struct g_consumer *cp)
563 struct g_mirror_disk *disk;
570 disk->d_softc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
571 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
572 G_MIRROR_EVENT_DONTWAIT);
576 * Function should return the next active disk on the list.
577 * It is possible that it will be the same disk as given.
578 * If there are no active disks on list, NULL is returned.
580 static __inline struct g_mirror_disk *
581 g_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
583 struct g_mirror_disk *dp;
585 for (dp = LIST_NEXT(disk, d_next); dp != disk;
586 dp = LIST_NEXT(dp, d_next)) {
588 dp = LIST_FIRST(&sc->sc_disks);
589 if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE)
592 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
597 static struct g_mirror_disk *
598 g_mirror_get_disk(struct g_mirror_softc *sc)
600 struct g_mirror_disk *disk;
602 if (sc->sc_hint == NULL) {
603 sc->sc_hint = LIST_FIRST(&sc->sc_disks);
604 if (sc->sc_hint == NULL)
608 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) {
609 disk = g_mirror_find_next(sc, disk);
613 sc->sc_hint = g_mirror_find_next(sc, disk);
618 g_mirror_write_metadata(struct g_mirror_disk *disk,
619 struct g_mirror_metadata *md)
621 struct g_mirror_softc *sc;
622 struct g_consumer *cp;
623 off_t offset, length;
627 g_topology_assert_not();
629 sx_assert(&sc->sc_lock, SX_LOCKED);
631 cp = disk->d_consumer;
632 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
633 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
634 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
635 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
637 length = cp->provider->sectorsize;
638 offset = cp->provider->mediasize - length;
639 sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO);
641 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0) {
643 * Handle the case, when the size of parent provider reduced.
645 if (offset < md->md_mediasize)
648 mirror_metadata_encode(md, sector);
650 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_metadata_write, error);
652 error = g_write_data(cp, offset, sector, length);
653 free(sector, M_MIRROR);
655 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
656 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
657 G_MIRROR_DEBUG(0, "Cannot write metadata on %s "
658 "(device=%s, error=%d).",
659 g_mirror_get_diskname(disk), sc->sc_name, error);
661 G_MIRROR_DEBUG(1, "Cannot write metadata on %s "
662 "(device=%s, error=%d).",
663 g_mirror_get_diskname(disk), sc->sc_name, error);
665 if (g_mirror_disconnect_on_failure &&
666 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1) {
667 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
668 g_mirror_event_send(disk,
669 G_MIRROR_DISK_STATE_DISCONNECTED,
670 G_MIRROR_EVENT_DONTWAIT);
677 g_mirror_clear_metadata(struct g_mirror_disk *disk)
681 g_topology_assert_not();
682 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
684 error = g_mirror_write_metadata(disk, NULL);
686 G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
687 g_mirror_get_diskname(disk));
690 "Cannot clear metadata on disk %s (error=%d).",
691 g_mirror_get_diskname(disk), error);
697 g_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
698 struct g_mirror_metadata *md)
701 strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
702 md->md_version = G_MIRROR_VERSION;
703 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
704 md->md_mid = sc->sc_id;
705 md->md_all = sc->sc_ndisks;
706 md->md_slice = sc->sc_slice;
707 md->md_balance = sc->sc_balance;
708 md->md_genid = sc->sc_genid;
709 md->md_mediasize = sc->sc_mediasize;
710 md->md_sectorsize = sc->sc_sectorsize;
711 md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
712 bzero(md->md_provider, sizeof(md->md_provider));
714 md->md_did = arc4random();
718 md->md_sync_offset = 0;
721 md->md_did = disk->d_id;
722 md->md_priority = disk->d_priority;
723 md->md_syncid = disk->d_sync.ds_syncid;
724 md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
725 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
726 md->md_sync_offset = disk->d_sync.ds_offset_done;
728 md->md_sync_offset = 0;
729 if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
730 strlcpy(md->md_provider,
731 disk->d_consumer->provider->name,
732 sizeof(md->md_provider));
734 md->md_provsize = disk->d_consumer->provider->mediasize;
739 g_mirror_update_metadata(struct g_mirror_disk *disk)
741 struct g_mirror_softc *sc;
742 struct g_mirror_metadata md;
745 g_topology_assert_not();
747 sx_assert(&sc->sc_lock, SX_LOCKED);
749 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WIPE) == 0)
750 g_mirror_fill_metadata(sc, disk, &md);
751 error = g_mirror_write_metadata(disk, &md);
753 G_MIRROR_DEBUG(2, "Metadata on %s updated.",
754 g_mirror_get_diskname(disk));
757 "Cannot update metadata on disk %s (error=%d).",
758 g_mirror_get_diskname(disk), error);
763 g_mirror_bump_syncid(struct g_mirror_softc *sc)
765 struct g_mirror_disk *disk;
767 g_topology_assert_not();
768 sx_assert(&sc->sc_lock, SX_XLOCKED);
769 KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
770 ("%s called with no active disks (device=%s).", __func__,
774 G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
776 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
777 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
778 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
779 disk->d_sync.ds_syncid = sc->sc_syncid;
780 g_mirror_update_metadata(disk);
786 g_mirror_bump_genid(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: genid 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_genid = sc->sc_genid;
803 g_mirror_update_metadata(disk);
809 g_mirror_idle(struct g_mirror_softc *sc, int acw)
811 struct g_mirror_disk *disk;
814 g_topology_assert_not();
815 sx_assert(&sc->sc_lock, SX_XLOCKED);
817 if (sc->sc_provider == NULL)
819 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
823 if (sc->sc_writes > 0)
825 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
826 timeout = g_mirror_idletime - (time_uptime - sc->sc_last_write);
827 if (!g_mirror_shutdown && timeout > 0)
831 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
832 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
834 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as clean.",
835 g_mirror_get_diskname(disk), sc->sc_name);
836 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
837 g_mirror_update_metadata(disk);
843 g_mirror_unidle(struct g_mirror_softc *sc)
845 struct g_mirror_disk *disk;
847 g_topology_assert_not();
848 sx_assert(&sc->sc_lock, SX_XLOCKED);
850 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
853 sc->sc_last_write = time_uptime;
854 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
855 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
857 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as dirty.",
858 g_mirror_get_diskname(disk), sc->sc_name);
859 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
860 g_mirror_update_metadata(disk);
865 g_mirror_flush_done(struct bio *bp)
867 struct g_mirror_softc *sc;
870 pbp = bp->bio_parent;
871 sc = pbp->bio_to->geom->softc;
872 mtx_lock(&sc->sc_done_mtx);
873 if (pbp->bio_error == 0)
874 pbp->bio_error = bp->bio_error;
875 pbp->bio_completed += bp->bio_completed;
877 if (pbp->bio_children == pbp->bio_inbed) {
878 mtx_unlock(&sc->sc_done_mtx);
879 g_io_deliver(pbp, pbp->bio_error);
881 mtx_unlock(&sc->sc_done_mtx);
886 g_mirror_done(struct bio *bp)
888 struct g_mirror_softc *sc;
890 sc = bp->bio_from->geom->softc;
891 bp->bio_cflags = G_MIRROR_BIO_FLAG_REGULAR;
892 mtx_lock(&sc->sc_queue_mtx);
893 bioq_insert_tail(&sc->sc_queue, bp);
894 mtx_unlock(&sc->sc_queue_mtx);
899 g_mirror_regular_request(struct bio *bp)
901 struct g_mirror_softc *sc;
902 struct g_mirror_disk *disk;
905 g_topology_assert_not();
907 pbp = bp->bio_parent;
908 sc = pbp->bio_to->geom->softc;
909 bp->bio_from->index--;
910 if (bp->bio_cmd == BIO_WRITE)
912 disk = bp->bio_from->private;
915 g_mirror_kill_consumer(sc, bp->bio_from);
919 if (bp->bio_cmd == BIO_READ)
920 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_regular_request_read,
922 else if (bp->bio_cmd == BIO_WRITE)
923 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_regular_request_write,
927 KASSERT(pbp->bio_inbed <= pbp->bio_children,
928 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
930 if (bp->bio_error == 0 && pbp->bio_error == 0) {
931 G_MIRROR_LOGREQ(3, bp, "Request delivered.");
933 if (pbp->bio_children == pbp->bio_inbed) {
934 G_MIRROR_LOGREQ(3, pbp, "Request delivered.");
935 pbp->bio_completed = pbp->bio_length;
936 if (pbp->bio_cmd == BIO_WRITE ||
937 pbp->bio_cmd == BIO_DELETE) {
938 bioq_remove(&sc->sc_inflight, pbp);
939 /* Release delayed sync requests if possible. */
940 g_mirror_sync_release(sc);
942 g_io_deliver(pbp, pbp->bio_error);
945 } else if (bp->bio_error != 0) {
946 if (pbp->bio_error == 0)
947 pbp->bio_error = bp->bio_error;
949 if ((disk->d_flags & G_MIRROR_DISK_FLAG_BROKEN) == 0) {
950 disk->d_flags |= G_MIRROR_DISK_FLAG_BROKEN;
951 G_MIRROR_LOGREQ(0, bp,
952 "Request failed (error=%d).",
955 G_MIRROR_LOGREQ(1, bp,
956 "Request failed (error=%d).",
959 if (g_mirror_disconnect_on_failure &&
960 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 1)
962 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
963 g_mirror_event_send(disk,
964 G_MIRROR_DISK_STATE_DISCONNECTED,
965 G_MIRROR_EVENT_DONTWAIT);
968 switch (pbp->bio_cmd) {
978 switch (pbp->bio_cmd) {
980 if (pbp->bio_inbed < pbp->bio_children)
982 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 1)
983 g_io_deliver(pbp, pbp->bio_error);
986 mtx_lock(&sc->sc_queue_mtx);
987 bioq_insert_tail(&sc->sc_queue, pbp);
988 mtx_unlock(&sc->sc_queue_mtx);
989 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
995 if (pbp->bio_children == 0) {
997 * All requests failed.
999 } else if (pbp->bio_inbed < pbp->bio_children) {
1002 } else if (pbp->bio_children == pbp->bio_inbed) {
1003 /* Some requests succeeded. */
1005 pbp->bio_completed = pbp->bio_length;
1007 bioq_remove(&sc->sc_inflight, pbp);
1008 /* Release delayed sync requests if possible. */
1009 g_mirror_sync_release(sc);
1010 g_io_deliver(pbp, pbp->bio_error);
1013 KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
1019 g_mirror_sync_done(struct bio *bp)
1021 struct g_mirror_softc *sc;
1023 G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
1024 sc = bp->bio_from->geom->softc;
1025 bp->bio_cflags = G_MIRROR_BIO_FLAG_SYNC;
1026 mtx_lock(&sc->sc_queue_mtx);
1027 bioq_insert_tail(&sc->sc_queue, bp);
1028 mtx_unlock(&sc->sc_queue_mtx);
1033 g_mirror_candelete(struct bio *bp)
1035 struct g_mirror_softc *sc;
1036 struct g_mirror_disk *disk;
1039 sc = bp->bio_to->geom->softc;
1040 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1041 if (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE)
1044 val = (int *)bp->bio_data;
1045 *val = (disk != NULL);
1046 g_io_deliver(bp, 0);
1050 g_mirror_kernel_dump(struct bio *bp)
1052 struct g_mirror_softc *sc;
1053 struct g_mirror_disk *disk;
1055 struct g_kerneldump *gkd;
1058 * We configure dumping to the first component, because this component
1059 * will be used for reading with 'prefer' balance algorithm.
1060 * If the component with the highest priority is currently disconnected
1061 * we will not be able to read the dump after the reboot if it will be
1062 * connected and synchronized later. Can we do something better?
1064 sc = bp->bio_to->geom->softc;
1065 disk = LIST_FIRST(&sc->sc_disks);
1067 gkd = (struct g_kerneldump *)bp->bio_data;
1068 if (gkd->length > bp->bio_to->mediasize)
1069 gkd->length = bp->bio_to->mediasize;
1070 cbp = g_clone_bio(bp);
1072 g_io_deliver(bp, ENOMEM);
1075 cbp->bio_done = g_std_done;
1076 g_io_request(cbp, disk->d_consumer);
1077 G_MIRROR_DEBUG(1, "Kernel dump will go to %s.",
1078 g_mirror_get_diskname(disk));
1082 g_mirror_flush(struct g_mirror_softc *sc, struct bio *bp)
1084 struct bio_queue_head queue;
1085 struct g_mirror_disk *disk;
1086 struct g_consumer *cp;
1090 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1091 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1093 cbp = g_clone_bio(bp);
1095 while ((cbp = bioq_takefirst(&queue)) != NULL)
1097 if (bp->bio_error == 0)
1098 bp->bio_error = ENOMEM;
1099 g_io_deliver(bp, bp->bio_error);
1102 bioq_insert_tail(&queue, cbp);
1103 cbp->bio_done = g_mirror_flush_done;
1104 cbp->bio_caller1 = disk;
1105 cbp->bio_to = disk->d_consumer->provider;
1107 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1108 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1109 disk = cbp->bio_caller1;
1110 cbp->bio_caller1 = NULL;
1111 cp = disk->d_consumer;
1112 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1113 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1114 cp->acr, cp->acw, cp->ace));
1115 g_io_request(cbp, disk->d_consumer);
1120 g_mirror_start(struct bio *bp)
1122 struct g_mirror_softc *sc;
1124 sc = bp->bio_to->geom->softc;
1126 * If sc == NULL or there are no valid disks, provider's error
1127 * should be set and g_mirror_start() should not be called at all.
1129 KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1130 ("Provider's error should be set (error=%d)(mirror=%s).",
1131 bp->bio_to->error, bp->bio_to->name));
1132 G_MIRROR_LOGREQ(3, bp, "Request received.");
1134 switch (bp->bio_cmd) {
1140 g_mirror_flush(sc, bp);
1143 if (!strcmp(bp->bio_attribute, "GEOM::candelete")) {
1144 g_mirror_candelete(bp);
1146 } else if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
1147 g_mirror_kernel_dump(bp);
1152 g_io_deliver(bp, EOPNOTSUPP);
1155 mtx_lock(&sc->sc_queue_mtx);
1156 bioq_insert_tail(&sc->sc_queue, bp);
1157 mtx_unlock(&sc->sc_queue_mtx);
1158 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1163 * Return TRUE if the given request is colliding with a in-progress
1164 * synchronization request.
1167 g_mirror_sync_collision(struct g_mirror_softc *sc, struct bio *bp)
1169 struct g_mirror_disk *disk;
1171 off_t rstart, rend, sstart, send;
1174 if (sc->sc_sync.ds_ndisks == 0)
1176 rstart = bp->bio_offset;
1177 rend = bp->bio_offset + bp->bio_length;
1178 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1179 if (disk->d_state != G_MIRROR_DISK_STATE_SYNCHRONIZING)
1181 for (i = 0; i < g_mirror_syncreqs; i++) {
1182 sbp = disk->d_sync.ds_bios[i];
1185 sstart = sbp->bio_offset;
1186 send = sbp->bio_offset + sbp->bio_length;
1187 if (rend > sstart && rstart < send)
1195 * Return TRUE if the given sync request is colliding with a in-progress regular
1199 g_mirror_regular_collision(struct g_mirror_softc *sc, struct bio *sbp)
1201 off_t rstart, rend, sstart, send;
1204 if (sc->sc_sync.ds_ndisks == 0)
1206 sstart = sbp->bio_offset;
1207 send = sbp->bio_offset + sbp->bio_length;
1208 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1209 rstart = bp->bio_offset;
1210 rend = bp->bio_offset + bp->bio_length;
1211 if (rend > sstart && rstart < send)
1218 * Puts request onto delayed queue.
1221 g_mirror_regular_delay(struct g_mirror_softc *sc, struct bio *bp)
1224 G_MIRROR_LOGREQ(2, bp, "Delaying request.");
1225 bioq_insert_head(&sc->sc_regular_delayed, bp);
1229 * Puts synchronization request onto delayed queue.
1232 g_mirror_sync_delay(struct g_mirror_softc *sc, struct bio *bp)
1235 G_MIRROR_LOGREQ(2, bp, "Delaying synchronization request.");
1236 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1240 * Releases delayed regular requests which don't collide anymore with sync
1244 g_mirror_regular_release(struct g_mirror_softc *sc)
1246 struct bio *bp, *bp2;
1248 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1249 if (g_mirror_sync_collision(sc, bp))
1251 bioq_remove(&sc->sc_regular_delayed, bp);
1252 G_MIRROR_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1253 mtx_lock(&sc->sc_queue_mtx);
1254 bioq_insert_head(&sc->sc_queue, bp);
1257 * wakeup() is not needed, because this function is called from
1258 * the worker thread.
1260 wakeup(&sc->sc_queue);
1262 mtx_unlock(&sc->sc_queue_mtx);
1267 * Releases delayed sync requests which don't collide anymore with regular
1271 g_mirror_sync_release(struct g_mirror_softc *sc)
1273 struct bio *bp, *bp2;
1275 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1276 if (g_mirror_regular_collision(sc, bp))
1278 bioq_remove(&sc->sc_sync_delayed, bp);
1279 G_MIRROR_LOGREQ(2, bp,
1280 "Releasing delayed synchronization request.");
1281 g_io_request(bp, bp->bio_from);
1286 * Handle synchronization requests.
1287 * Every synchronization request is two-steps process: first, READ request is
1288 * send to active provider and then WRITE request (with read data) to the provider
1289 * being synchronized. When WRITE is finished, new synchronization request is
1293 g_mirror_sync_request(struct bio *bp)
1295 struct g_mirror_softc *sc;
1296 struct g_mirror_disk *disk;
1298 bp->bio_from->index--;
1299 sc = bp->bio_from->geom->softc;
1300 disk = bp->bio_from->private;
1302 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1304 g_mirror_kill_consumer(sc, bp->bio_from);
1305 g_topology_unlock();
1306 free(bp->bio_data, M_MIRROR);
1308 sx_xlock(&sc->sc_lock);
1313 * Synchronization request.
1315 switch (bp->bio_cmd) {
1318 struct g_consumer *cp;
1320 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_sync_request_read,
1323 if (bp->bio_error != 0) {
1324 G_MIRROR_LOGREQ(0, bp,
1325 "Synchronization request failed (error=%d).",
1330 G_MIRROR_LOGREQ(3, bp,
1331 "Synchronization request half-finished.");
1332 bp->bio_cmd = BIO_WRITE;
1334 cp = disk->d_consumer;
1335 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1336 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1337 cp->acr, cp->acw, cp->ace));
1339 g_io_request(bp, cp);
1344 struct g_mirror_disk_sync *sync;
1349 KFAIL_POINT_ERROR(DEBUG_FP, g_mirror_sync_request_write,
1352 if (bp->bio_error != 0) {
1353 G_MIRROR_LOGREQ(0, bp,
1354 "Synchronization request failed (error=%d).",
1357 sc->sc_bump_id |= G_MIRROR_BUMP_GENID;
1358 g_mirror_event_send(disk,
1359 G_MIRROR_DISK_STATE_DISCONNECTED,
1360 G_MIRROR_EVENT_DONTWAIT);
1363 G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
1364 sync = &disk->d_sync;
1365 if (sync->ds_offset >= sc->sc_mediasize ||
1366 sync->ds_consumer == NULL ||
1367 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1368 /* Don't send more synchronization requests. */
1369 sync->ds_inflight--;
1370 if (sync->ds_bios != NULL) {
1371 i = (int)(uintptr_t)bp->bio_caller1;
1372 sync->ds_bios[i] = NULL;
1374 free(bp->bio_data, M_MIRROR);
1376 if (sync->ds_inflight > 0)
1378 if (sync->ds_consumer == NULL ||
1379 (sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1382 /* Disk up-to-date, activate it. */
1383 g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
1384 G_MIRROR_EVENT_DONTWAIT);
1388 /* Send next synchronization request. */
1389 data = bp->bio_data;
1391 bp->bio_cmd = BIO_READ;
1392 bp->bio_offset = sync->ds_offset;
1393 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1394 sync->ds_offset += bp->bio_length;
1395 bp->bio_done = g_mirror_sync_done;
1396 bp->bio_data = data;
1397 bp->bio_from = sync->ds_consumer;
1398 bp->bio_to = sc->sc_provider;
1399 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
1400 sync->ds_consumer->index++;
1402 * Delay the request if it is colliding with a regular request.
1404 if (g_mirror_regular_collision(sc, bp))
1405 g_mirror_sync_delay(sc, bp);
1407 g_io_request(bp, sync->ds_consumer);
1409 /* Release delayed requests if possible. */
1410 g_mirror_regular_release(sc);
1412 /* Find the smallest offset */
1413 offset = sc->sc_mediasize;
1414 for (i = 0; i < g_mirror_syncreqs; i++) {
1415 bp = sync->ds_bios[i];
1416 if (bp->bio_offset < offset)
1417 offset = bp->bio_offset;
1419 if (sync->ds_offset_done + (MAXPHYS * 100) < offset) {
1420 /* Update offset_done on every 100 blocks. */
1421 sync->ds_offset_done = offset;
1422 g_mirror_update_metadata(disk);
1427 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1428 bp->bio_cmd, sc->sc_name));
1434 g_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
1436 struct g_mirror_disk *disk;
1437 struct g_consumer *cp;
1440 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1441 if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
1445 if (bp->bio_error == 0)
1446 bp->bio_error = ENXIO;
1447 g_io_deliver(bp, bp->bio_error);
1450 cbp = g_clone_bio(bp);
1452 if (bp->bio_error == 0)
1453 bp->bio_error = ENOMEM;
1454 g_io_deliver(bp, bp->bio_error);
1458 * Fill in the component buf structure.
1460 cp = disk->d_consumer;
1461 cbp->bio_done = g_mirror_done;
1462 cbp->bio_to = cp->provider;
1463 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1464 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1465 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1468 g_io_request(cbp, cp);
1472 g_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
1474 struct g_mirror_disk *disk;
1475 struct g_consumer *cp;
1478 disk = g_mirror_get_disk(sc);
1480 if (bp->bio_error == 0)
1481 bp->bio_error = ENXIO;
1482 g_io_deliver(bp, bp->bio_error);
1485 cbp = g_clone_bio(bp);
1487 if (bp->bio_error == 0)
1488 bp->bio_error = ENOMEM;
1489 g_io_deliver(bp, bp->bio_error);
1493 * Fill in the component buf structure.
1495 cp = disk->d_consumer;
1496 cbp->bio_done = g_mirror_done;
1497 cbp->bio_to = cp->provider;
1498 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1499 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1500 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1503 g_io_request(cbp, cp);
1506 #define TRACK_SIZE (1 * 1024 * 1024)
1507 #define LOAD_SCALE 256
1508 #define ABS(x) (((x) >= 0) ? (x) : (-(x)))
1511 g_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
1513 struct g_mirror_disk *disk, *dp;
1514 struct g_consumer *cp;
1518 /* Find a disk with the smallest load. */
1521 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1522 if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1525 /* If disk head is precisely in position - highly prefer it. */
1526 if (dp->d_last_offset == bp->bio_offset)
1527 prio -= 2 * LOAD_SCALE;
1529 /* If disk head is close to position - prefer it. */
1530 if (ABS(dp->d_last_offset - bp->bio_offset) < TRACK_SIZE)
1531 prio -= 1 * LOAD_SCALE;
1537 KASSERT(disk != NULL, ("NULL disk for %s.", sc->sc_name));
1538 cbp = g_clone_bio(bp);
1540 if (bp->bio_error == 0)
1541 bp->bio_error = ENOMEM;
1542 g_io_deliver(bp, bp->bio_error);
1546 * Fill in the component buf structure.
1548 cp = disk->d_consumer;
1549 cbp->bio_done = g_mirror_done;
1550 cbp->bio_to = cp->provider;
1551 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1552 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1553 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1556 /* Remember last head position */
1557 disk->d_last_offset = bp->bio_offset + bp->bio_length;
1559 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1560 dp->load = (dp->d_consumer->index * LOAD_SCALE +
1563 g_io_request(cbp, cp);
1567 g_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
1569 struct bio_queue_head queue;
1570 struct g_mirror_disk *disk;
1571 struct g_consumer *cp;
1573 off_t left, mod, offset, slice;
1577 if (bp->bio_length <= sc->sc_slice) {
1578 g_mirror_request_round_robin(sc, bp);
1581 ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
1582 slice = bp->bio_length / ndisks;
1583 mod = slice % sc->sc_provider->sectorsize;
1585 slice += sc->sc_provider->sectorsize - mod;
1587 * Allocate all bios before sending any request, so we can
1588 * return ENOMEM in nice and clean way.
1590 left = bp->bio_length;
1591 offset = bp->bio_offset;
1592 data = bp->bio_data;
1594 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1595 if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
1597 cbp = g_clone_bio(bp);
1599 while ((cbp = bioq_takefirst(&queue)) != NULL)
1601 if (bp->bio_error == 0)
1602 bp->bio_error = ENOMEM;
1603 g_io_deliver(bp, bp->bio_error);
1606 bioq_insert_tail(&queue, cbp);
1607 cbp->bio_done = g_mirror_done;
1608 cbp->bio_caller1 = disk;
1609 cbp->bio_to = disk->d_consumer->provider;
1610 cbp->bio_offset = offset;
1611 cbp->bio_data = data;
1612 cbp->bio_length = MIN(left, slice);
1613 left -= cbp->bio_length;
1616 offset += cbp->bio_length;
1617 data += cbp->bio_length;
1619 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1620 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1621 disk = cbp->bio_caller1;
1622 cbp->bio_caller1 = NULL;
1623 cp = disk->d_consumer;
1624 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1625 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1626 cp->acr, cp->acw, cp->ace));
1627 disk->d_consumer->index++;
1628 g_io_request(cbp, disk->d_consumer);
1633 g_mirror_register_request(struct bio *bp)
1635 struct g_mirror_softc *sc;
1637 sc = bp->bio_to->geom->softc;
1638 switch (bp->bio_cmd) {
1640 switch (sc->sc_balance) {
1641 case G_MIRROR_BALANCE_LOAD:
1642 g_mirror_request_load(sc, bp);
1644 case G_MIRROR_BALANCE_PREFER:
1645 g_mirror_request_prefer(sc, bp);
1647 case G_MIRROR_BALANCE_ROUND_ROBIN:
1648 g_mirror_request_round_robin(sc, bp);
1650 case G_MIRROR_BALANCE_SPLIT:
1651 g_mirror_request_split(sc, bp);
1658 struct g_mirror_disk *disk;
1659 struct g_mirror_disk_sync *sync;
1660 struct bio_queue_head queue;
1661 struct g_consumer *cp;
1665 * Delay the request if it is colliding with a synchronization
1668 if (g_mirror_sync_collision(sc, bp)) {
1669 g_mirror_regular_delay(sc, bp);
1674 g_mirror_unidle(sc);
1676 sc->sc_last_write = time_uptime;
1679 * Allocate all bios before sending any request, so we can
1680 * return ENOMEM in nice and clean way.
1683 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1684 sync = &disk->d_sync;
1685 switch (disk->d_state) {
1686 case G_MIRROR_DISK_STATE_ACTIVE:
1688 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1689 if (bp->bio_offset >= sync->ds_offset)
1695 if (bp->bio_cmd == BIO_DELETE &&
1696 (disk->d_flags & G_MIRROR_DISK_FLAG_CANDELETE) == 0)
1698 cbp = g_clone_bio(bp);
1700 while ((cbp = bioq_takefirst(&queue)) != NULL)
1702 if (bp->bio_error == 0)
1703 bp->bio_error = ENOMEM;
1704 g_io_deliver(bp, bp->bio_error);
1707 bioq_insert_tail(&queue, cbp);
1708 cbp->bio_done = g_mirror_done;
1709 cp = disk->d_consumer;
1710 cbp->bio_caller1 = cp;
1711 cbp->bio_to = cp->provider;
1712 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1713 ("Consumer %s not opened (r%dw%de%d).",
1714 cp->provider->name, cp->acr, cp->acw, cp->ace));
1716 if (bioq_first(&queue) == NULL) {
1717 g_io_deliver(bp, EOPNOTSUPP);
1720 while ((cbp = bioq_takefirst(&queue)) != NULL) {
1721 G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1722 cp = cbp->bio_caller1;
1723 cbp->bio_caller1 = NULL;
1726 g_io_request(cbp, cp);
1729 * Put request onto inflight queue, so we can check if new
1730 * synchronization requests don't collide with it.
1732 bioq_insert_tail(&sc->sc_inflight, bp);
1734 * Bump syncid on first write.
1736 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0) {
1737 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
1738 g_mirror_bump_syncid(sc);
1743 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1744 bp->bio_cmd, sc->sc_name));
1750 g_mirror_can_destroy(struct g_mirror_softc *sc)
1753 struct g_consumer *cp;
1755 g_topology_assert();
1757 if (gp->softc == NULL)
1759 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_TASTING) != 0)
1761 LIST_FOREACH(cp, &gp->consumer, consumer) {
1762 if (g_mirror_is_busy(sc, cp))
1765 gp = sc->sc_sync.ds_geom;
1766 LIST_FOREACH(cp, &gp->consumer, consumer) {
1767 if (g_mirror_is_busy(sc, cp))
1770 G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1776 g_mirror_try_destroy(struct g_mirror_softc *sc)
1779 if (sc->sc_rootmount != NULL) {
1780 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1782 root_mount_rel(sc->sc_rootmount);
1783 sc->sc_rootmount = NULL;
1786 if (!g_mirror_can_destroy(sc)) {
1787 g_topology_unlock();
1790 sc->sc_geom->softc = NULL;
1791 sc->sc_sync.ds_geom->softc = NULL;
1792 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WAIT) != 0) {
1793 g_topology_unlock();
1794 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1796 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
1797 sx_xunlock(&sc->sc_lock);
1798 wakeup(&sc->sc_worker);
1799 sc->sc_worker = NULL;
1801 g_topology_unlock();
1802 g_mirror_destroy_device(sc);
1812 g_mirror_worker(void *arg)
1814 struct g_mirror_softc *sc;
1815 struct g_mirror_event *ep;
1820 thread_lock(curthread);
1821 sched_prio(curthread, PRIBIO);
1822 thread_unlock(curthread);
1824 sx_xlock(&sc->sc_lock);
1826 G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
1828 * First take a look at events.
1829 * This is important to handle events before any I/O requests.
1831 ep = g_mirror_event_get(sc);
1833 g_mirror_event_remove(sc, ep);
1834 if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
1835 /* Update only device status. */
1837 "Running event for device %s.",
1840 g_mirror_update_device(sc, 1);
1842 /* Update disk status. */
1843 G_MIRROR_DEBUG(3, "Running event for disk %s.",
1844 g_mirror_get_diskname(ep->e_disk));
1845 ep->e_error = g_mirror_update_disk(ep->e_disk,
1847 if (ep->e_error == 0)
1848 g_mirror_update_device(sc, 0);
1850 if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
1851 KASSERT(ep->e_error == 0,
1852 ("Error cannot be handled."));
1853 g_mirror_event_free(ep);
1855 ep->e_flags |= G_MIRROR_EVENT_DONE;
1856 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
1858 mtx_lock(&sc->sc_events_mtx);
1860 mtx_unlock(&sc->sc_events_mtx);
1863 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1864 if (g_mirror_try_destroy(sc)) {
1865 curthread->td_pflags &= ~TDP_GEOM;
1866 G_MIRROR_DEBUG(1, "Thread exiting.");
1870 G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
1874 * Check if we can mark array as CLEAN and if we can't take
1875 * how much seconds should we wait.
1877 timeout = g_mirror_idle(sc, -1);
1881 /* Get first request from the queue. */
1882 mtx_lock(&sc->sc_queue_mtx);
1883 bp = bioq_takefirst(&sc->sc_queue);
1886 G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
1887 mtx_unlock(&sc->sc_queue_mtx);
1888 if (g_mirror_try_destroy(sc)) {
1889 curthread->td_pflags &= ~TDP_GEOM;
1890 G_MIRROR_DEBUG(1, "Thread exiting.");
1893 mtx_lock(&sc->sc_queue_mtx);
1895 sx_xunlock(&sc->sc_lock);
1897 * XXX: We can miss an event here, because an event
1898 * can be added without sx-device-lock and without
1899 * mtx-queue-lock. Maybe I should just stop using
1900 * dedicated mutex for events synchronization and
1901 * stick with the queue lock?
1902 * The event will hang here until next I/O request
1903 * or next event is received.
1905 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1",
1907 sx_xlock(&sc->sc_lock);
1908 G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
1911 mtx_unlock(&sc->sc_queue_mtx);
1913 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
1914 (bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
1915 g_mirror_sync_request(bp); /* READ */
1916 } else if (bp->bio_to != sc->sc_provider) {
1917 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0)
1918 g_mirror_regular_request(bp);
1919 else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
1920 g_mirror_sync_request(bp); /* WRITE */
1923 ("Invalid request cflags=0x%hx to=%s.",
1924 bp->bio_cflags, bp->bio_to->name));
1927 g_mirror_register_request(bp);
1929 G_MIRROR_DEBUG(5, "%s: I'm here 9.", __func__);
1934 g_mirror_update_idle(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
1937 sx_assert(&sc->sc_lock, SX_LOCKED);
1939 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) != 0)
1941 if (!sc->sc_idle && (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
1942 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as dirty.",
1943 g_mirror_get_diskname(disk), sc->sc_name);
1944 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1945 } else if (sc->sc_idle &&
1946 (disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
1947 G_MIRROR_DEBUG(2, "Disk %s (device %s) marked as clean.",
1948 g_mirror_get_diskname(disk), sc->sc_name);
1949 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
1954 g_mirror_sync_start(struct g_mirror_disk *disk)
1956 struct g_mirror_softc *sc;
1957 struct g_consumer *cp;
1961 g_topology_assert_not();
1963 sx_assert(&sc->sc_lock, SX_LOCKED);
1965 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1966 ("Disk %s is not marked for synchronization.",
1967 g_mirror_get_diskname(disk)));
1968 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1969 ("Device not in RUNNING state (%s, %u).", sc->sc_name,
1972 sx_xunlock(&sc->sc_lock);
1974 cp = g_new_consumer(sc->sc_sync.ds_geom);
1975 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1976 error = g_attach(cp, sc->sc_provider);
1978 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
1979 error = g_access(cp, 1, 0, 0);
1980 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
1981 g_topology_unlock();
1982 sx_xlock(&sc->sc_lock);
1984 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
1985 g_mirror_get_diskname(disk));
1986 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOFAILSYNC) == 0)
1987 disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1988 KASSERT(disk->d_sync.ds_consumer == NULL,
1989 ("Sync consumer already exists (device=%s, disk=%s).",
1990 sc->sc_name, g_mirror_get_diskname(disk)));
1992 disk->d_sync.ds_consumer = cp;
1993 disk->d_sync.ds_consumer->private = disk;
1994 disk->d_sync.ds_consumer->index = 0;
1997 * Allocate memory for synchronization bios and initialize them.
1999 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_mirror_syncreqs,
2000 M_MIRROR, M_WAITOK);
2001 for (i = 0; i < g_mirror_syncreqs; i++) {
2003 disk->d_sync.ds_bios[i] = bp;
2004 bp->bio_parent = NULL;
2005 bp->bio_cmd = BIO_READ;
2006 bp->bio_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK);
2008 bp->bio_offset = disk->d_sync.ds_offset;
2009 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
2010 disk->d_sync.ds_offset += bp->bio_length;
2011 bp->bio_done = g_mirror_sync_done;
2012 bp->bio_from = disk->d_sync.ds_consumer;
2013 bp->bio_to = sc->sc_provider;
2014 bp->bio_caller1 = (void *)(uintptr_t)i;
2017 /* Increase the number of disks in SYNCHRONIZING state. */
2018 sc->sc_sync.ds_ndisks++;
2019 /* Set the number of in-flight synchronization requests. */
2020 disk->d_sync.ds_inflight = g_mirror_syncreqs;
2023 * Fire off first synchronization requests.
2025 for (i = 0; i < g_mirror_syncreqs; i++) {
2026 bp = disk->d_sync.ds_bios[i];
2027 G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
2028 disk->d_sync.ds_consumer->index++;
2030 * Delay the request if it is colliding with a regular request.
2032 if (g_mirror_regular_collision(sc, bp))
2033 g_mirror_sync_delay(sc, bp);
2035 g_io_request(bp, disk->d_sync.ds_consumer);
2040 * Stop synchronization process.
2041 * type: 0 - synchronization finished
2042 * 1 - synchronization stopped
2045 g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
2047 struct g_mirror_softc *sc;
2048 struct g_consumer *cp;
2050 g_topology_assert_not();
2052 sx_assert(&sc->sc_lock, SX_LOCKED);
2054 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2055 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2056 g_mirror_disk_state2str(disk->d_state)));
2057 if (disk->d_sync.ds_consumer == NULL)
2061 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2062 sc->sc_name, g_mirror_get_diskname(disk));
2063 } else /* if (type == 1) */ {
2064 G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2065 sc->sc_name, g_mirror_get_diskname(disk));
2067 free(disk->d_sync.ds_bios, M_MIRROR);
2068 disk->d_sync.ds_bios = NULL;
2069 cp = disk->d_sync.ds_consumer;
2070 disk->d_sync.ds_consumer = NULL;
2071 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2072 sc->sc_sync.ds_ndisks--;
2073 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2075 g_mirror_kill_consumer(sc, cp);
2076 g_topology_unlock();
2077 sx_xlock(&sc->sc_lock);
2081 g_mirror_launch_provider(struct g_mirror_softc *sc)
2083 struct g_mirror_disk *disk;
2084 struct g_provider *pp, *dp;
2086 sx_assert(&sc->sc_lock, SX_LOCKED);
2089 pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
2090 pp->flags |= G_PF_DIRECT_RECEIVE;
2091 pp->mediasize = sc->sc_mediasize;
2092 pp->sectorsize = sc->sc_sectorsize;
2094 pp->stripeoffset = 0;
2096 /* Splitting of unmapped BIO's could work but isn't implemented now */
2097 if (sc->sc_balance != G_MIRROR_BALANCE_SPLIT)
2098 pp->flags |= G_PF_ACCEPT_UNMAPPED;
2100 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2101 if (disk->d_consumer && disk->d_consumer->provider) {
2102 dp = disk->d_consumer->provider;
2103 if (dp->stripesize > pp->stripesize) {
2104 pp->stripesize = dp->stripesize;
2105 pp->stripeoffset = dp->stripeoffset;
2107 /* A provider underneath us doesn't support unmapped */
2108 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
2109 G_MIRROR_DEBUG(0, "Cancelling unmapped "
2110 "because of %s.", dp->name);
2111 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
2115 sc->sc_provider = pp;
2116 g_error_provider(pp, 0);
2117 g_topology_unlock();
2118 G_MIRROR_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2119 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE), sc->sc_ndisks);
2120 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2121 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2122 g_mirror_sync_start(disk);
2127 g_mirror_destroy_provider(struct g_mirror_softc *sc)
2129 struct g_mirror_disk *disk;
2132 g_topology_assert_not();
2133 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2137 g_error_provider(sc->sc_provider, ENXIO);
2138 mtx_lock(&sc->sc_queue_mtx);
2139 while ((bp = bioq_takefirst(&sc->sc_queue)) != NULL) {
2141 * Abort any pending I/O that wasn't generated by us.
2142 * Synchronization requests and requests destined for individual
2143 * mirror components can be destroyed immediately.
2145 if (bp->bio_to == sc->sc_provider &&
2146 bp->bio_from->geom != sc->sc_sync.ds_geom) {
2147 g_io_deliver(bp, ENXIO);
2149 if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0)
2150 free(bp->bio_data, M_MIRROR);
2154 mtx_unlock(&sc->sc_queue_mtx);
2155 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2156 sc->sc_provider->name);
2157 g_wither_provider(sc->sc_provider, ENXIO);
2158 sc->sc_provider = NULL;
2159 g_topology_unlock();
2160 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2161 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
2162 g_mirror_sync_stop(disk, 1);
2167 g_mirror_go(void *arg)
2169 struct g_mirror_softc *sc;
2172 G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2173 g_mirror_event_send(sc, 0,
2174 G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
2178 g_mirror_determine_state(struct g_mirror_disk *disk)
2180 struct g_mirror_softc *sc;
2184 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2185 if ((disk->d_flags &
2186 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2187 /* Disk does not need synchronization. */
2188 state = G_MIRROR_DISK_STATE_ACTIVE;
2191 G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2193 G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2195 * We can start synchronization from
2196 * the stored offset.
2198 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2200 state = G_MIRROR_DISK_STATE_STALE;
2203 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2205 * Reset all synchronization data for this disk,
2206 * because if it even was synchronized, it was
2207 * synchronized to disks with different syncid.
2209 disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2210 disk->d_sync.ds_offset = 0;
2211 disk->d_sync.ds_offset_done = 0;
2212 disk->d_sync.ds_syncid = sc->sc_syncid;
2213 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2214 (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
2215 state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
2217 state = G_MIRROR_DISK_STATE_STALE;
2219 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2221 * Not good, NOT GOOD!
2222 * It means that mirror was started on stale disks
2223 * and more fresh disk just arrive.
2224 * If there were writes, mirror is broken, sorry.
2225 * I think the best choice here is don't touch
2226 * this disk and inform the user loudly.
2228 G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
2229 "disk (%s) arrives!! It will not be connected to the "
2230 "running device.", sc->sc_name,
2231 g_mirror_get_diskname(disk));
2232 g_mirror_destroy_disk(disk);
2233 state = G_MIRROR_DISK_STATE_NONE;
2234 /* Return immediately, because disk was destroyed. */
2237 G_MIRROR_DEBUG(3, "State for %s disk: %s.",
2238 g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
2243 * Update device state.
2246 g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force)
2248 struct g_mirror_disk *disk;
2251 sx_assert(&sc->sc_lock, SX_XLOCKED);
2253 switch (sc->sc_state) {
2254 case G_MIRROR_DEVICE_STATE_STARTING:
2256 struct g_mirror_disk *pdisk, *tdisk;
2257 u_int dirty, ndisks, genid, syncid;
2259 KASSERT(sc->sc_provider == NULL,
2260 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2262 * Are we ready? We are, if all disks are connected or
2263 * if we have any disks and 'force' is true.
2265 ndisks = g_mirror_ndisks(sc, -1);
2266 if (sc->sc_ndisks == ndisks || (force && ndisks > 0)) {
2268 } else if (ndisks == 0) {
2270 * Disks went down in starting phase, so destroy
2273 callout_drain(&sc->sc_callout);
2274 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2275 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2277 root_mount_rel(sc->sc_rootmount);
2278 sc->sc_rootmount = NULL;
2285 * Activate all disks with the biggest syncid.
2289 * If 'force' is true, we have been called due to
2290 * timeout, so don't bother canceling timeout.
2293 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2294 if ((disk->d_flags &
2295 G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
2300 /* No valid disks found, destroy device. */
2301 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2302 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2303 __LINE__, sc->sc_rootmount);
2304 root_mount_rel(sc->sc_rootmount);
2305 sc->sc_rootmount = NULL;
2309 /* Cancel timeout. */
2310 callout_drain(&sc->sc_callout);
2314 * Find the biggest genid.
2317 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2318 if (disk->d_genid > genid)
2319 genid = disk->d_genid;
2321 sc->sc_genid = genid;
2323 * Remove all disks without the biggest genid.
2325 LIST_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tdisk) {
2326 if (disk->d_genid < genid) {
2328 "Component %s (device %s) broken, skipping.",
2329 g_mirror_get_diskname(disk), sc->sc_name);
2330 g_mirror_destroy_disk(disk);
2335 * Find the biggest syncid.
2338 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2339 if (disk->d_sync.ds_syncid > syncid)
2340 syncid = disk->d_sync.ds_syncid;
2344 * Here we need to look for dirty disks and if all disks
2345 * with the biggest syncid are dirty, we have to choose
2346 * one with the biggest priority and rebuild the rest.
2349 * Find the number of dirty disks with the biggest syncid.
2350 * Find the number of disks with the biggest syncid.
2351 * While here, find a disk with the biggest priority.
2355 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2356 if (disk->d_sync.ds_syncid != syncid)
2358 if ((disk->d_flags &
2359 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2363 if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
2365 if (pdisk == NULL ||
2366 pdisk->d_priority < disk->d_priority) {
2372 /* No dirty disks at all, great. */
2373 } else if (dirty == ndisks) {
2375 * Force synchronization for all dirty disks except one
2376 * with the biggest priority.
2378 KASSERT(pdisk != NULL, ("pdisk == NULL"));
2379 G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
2380 "master disk for synchronization.",
2381 g_mirror_get_diskname(pdisk), sc->sc_name);
2382 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2383 if (disk->d_sync.ds_syncid != syncid)
2385 if ((disk->d_flags &
2386 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2389 KASSERT((disk->d_flags &
2390 G_MIRROR_DISK_FLAG_DIRTY) != 0,
2391 ("Disk %s isn't marked as dirty.",
2392 g_mirror_get_diskname(disk)));
2393 /* Skip the disk with the biggest priority. */
2396 disk->d_sync.ds_syncid = 0;
2398 } else if (dirty < ndisks) {
2400 * Force synchronization for all dirty disks.
2401 * We have some non-dirty disks.
2403 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2404 if (disk->d_sync.ds_syncid != syncid)
2406 if ((disk->d_flags &
2407 G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
2410 if ((disk->d_flags &
2411 G_MIRROR_DISK_FLAG_DIRTY) == 0) {
2414 disk->d_sync.ds_syncid = 0;
2420 sc->sc_syncid = syncid;
2422 /* Remember to bump syncid on first write. */
2423 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2425 state = G_MIRROR_DEVICE_STATE_RUNNING;
2426 G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
2427 sc->sc_name, g_mirror_device_state2str(sc->sc_state),
2428 g_mirror_device_state2str(state));
2429 sc->sc_state = state;
2430 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2431 state = g_mirror_determine_state(disk);
2432 g_mirror_event_send(disk, state,
2433 G_MIRROR_EVENT_DONTWAIT);
2434 if (state == G_MIRROR_DISK_STATE_STALE)
2435 sc->sc_bump_id |= G_MIRROR_BUMP_SYNCID;
2439 case G_MIRROR_DEVICE_STATE_RUNNING:
2440 if (g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
2441 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2443 * No active disks or no disks at all,
2444 * so destroy device.
2446 if (sc->sc_provider != NULL)
2447 g_mirror_destroy_provider(sc);
2448 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2450 } else if (g_mirror_ndisks(sc,
2451 G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
2452 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
2454 * We have active disks, launch provider if it doesn't
2457 if (sc->sc_provider == NULL)
2458 g_mirror_launch_provider(sc);
2459 if (sc->sc_rootmount != NULL) {
2460 G_MIRROR_DEBUG(1, "root_mount_rel[%u] %p",
2461 __LINE__, sc->sc_rootmount);
2462 root_mount_rel(sc->sc_rootmount);
2463 sc->sc_rootmount = NULL;
2467 * Genid should be bumped immediately, so do it here.
2469 if ((sc->sc_bump_id & G_MIRROR_BUMP_GENID) != 0) {
2470 sc->sc_bump_id &= ~G_MIRROR_BUMP_GENID;
2471 g_mirror_bump_genid(sc);
2475 KASSERT(1 == 0, ("Wrong device state (%s, %s).",
2476 sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
2482 * Update disk state and device state if needed.
2484 #define DISK_STATE_CHANGED() G_MIRROR_DEBUG(1, \
2485 "Disk %s state changed from %s to %s (device %s).", \
2486 g_mirror_get_diskname(disk), \
2487 g_mirror_disk_state2str(disk->d_state), \
2488 g_mirror_disk_state2str(state), sc->sc_name)
2490 g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
2492 struct g_mirror_softc *sc;
2495 sx_assert(&sc->sc_lock, SX_XLOCKED);
2498 G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
2499 g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
2500 g_mirror_disk_state2str(state));
2502 case G_MIRROR_DISK_STATE_NEW:
2504 * Possible scenarios:
2505 * 1. New disk arrive.
2507 /* Previous state should be NONE. */
2508 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
2509 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2510 g_mirror_disk_state2str(disk->d_state)));
2511 DISK_STATE_CHANGED();
2513 disk->d_state = state;
2514 if (LIST_EMPTY(&sc->sc_disks))
2515 LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
2517 struct g_mirror_disk *dp;
2519 LIST_FOREACH(dp, &sc->sc_disks, d_next) {
2520 if (disk->d_priority >= dp->d_priority) {
2521 LIST_INSERT_BEFORE(dp, disk, d_next);
2525 if (LIST_NEXT(dp, d_next) == NULL)
2529 LIST_INSERT_AFTER(dp, disk, d_next);
2531 G_MIRROR_DEBUG(1, "Device %s: provider %s detected.",
2532 sc->sc_name, g_mirror_get_diskname(disk));
2533 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2535 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2536 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2537 g_mirror_device_state2str(sc->sc_state),
2538 g_mirror_get_diskname(disk),
2539 g_mirror_disk_state2str(disk->d_state)));
2540 state = g_mirror_determine_state(disk);
2541 if (state != G_MIRROR_DISK_STATE_NONE)
2544 case G_MIRROR_DISK_STATE_ACTIVE:
2546 * Possible scenarios:
2547 * 1. New disk does not need synchronization.
2548 * 2. Synchronization process finished successfully.
2550 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2551 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2552 g_mirror_device_state2str(sc->sc_state),
2553 g_mirror_get_diskname(disk),
2554 g_mirror_disk_state2str(disk->d_state)));
2555 /* Previous state should be NEW or SYNCHRONIZING. */
2556 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2557 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2558 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2559 g_mirror_disk_state2str(disk->d_state)));
2560 DISK_STATE_CHANGED();
2562 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2563 disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2564 disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2565 g_mirror_sync_stop(disk, 0);
2567 disk->d_state = state;
2568 disk->d_sync.ds_offset = 0;
2569 disk->d_sync.ds_offset_done = 0;
2570 g_mirror_update_idle(sc, disk);
2571 g_mirror_update_metadata(disk);
2572 G_MIRROR_DEBUG(1, "Device %s: provider %s activated.",
2573 sc->sc_name, g_mirror_get_diskname(disk));
2575 case G_MIRROR_DISK_STATE_STALE:
2577 * Possible scenarios:
2578 * 1. Stale disk was connected.
2580 /* Previous state should be NEW. */
2581 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2582 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2583 g_mirror_disk_state2str(disk->d_state)));
2584 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2585 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2586 g_mirror_device_state2str(sc->sc_state),
2587 g_mirror_get_diskname(disk),
2588 g_mirror_disk_state2str(disk->d_state)));
2590 * STALE state is only possible if device is marked
2593 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2594 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2595 g_mirror_device_state2str(sc->sc_state),
2596 g_mirror_get_diskname(disk),
2597 g_mirror_disk_state2str(disk->d_state)));
2598 DISK_STATE_CHANGED();
2600 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2601 disk->d_state = state;
2602 g_mirror_update_metadata(disk);
2603 G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2604 sc->sc_name, g_mirror_get_diskname(disk));
2606 case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2608 * Possible scenarios:
2609 * 1. Disk which needs synchronization was connected.
2611 /* Previous state should be NEW. */
2612 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2613 ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2614 g_mirror_disk_state2str(disk->d_state)));
2615 KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2616 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2617 g_mirror_device_state2str(sc->sc_state),
2618 g_mirror_get_diskname(disk),
2619 g_mirror_disk_state2str(disk->d_state)));
2620 DISK_STATE_CHANGED();
2622 if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2623 disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2624 disk->d_state = state;
2625 if (sc->sc_provider != NULL) {
2626 g_mirror_sync_start(disk);
2627 g_mirror_update_metadata(disk);
2630 case G_MIRROR_DISK_STATE_DISCONNECTED:
2632 * Possible scenarios:
2633 * 1. Device wasn't running yet, but disk disappear.
2634 * 2. Disk was active and disapppear.
2635 * 3. Disk disappear during synchronization process.
2637 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2639 * Previous state should be ACTIVE, STALE or
2642 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2643 disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2644 disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2645 ("Wrong disk state (%s, %s).",
2646 g_mirror_get_diskname(disk),
2647 g_mirror_disk_state2str(disk->d_state)));
2648 } else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2649 /* Previous state should be NEW. */
2650 KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2651 ("Wrong disk state (%s, %s).",
2652 g_mirror_get_diskname(disk),
2653 g_mirror_disk_state2str(disk->d_state)));
2655 * Reset bumping syncid if disk disappeared in STARTING
2658 if ((sc->sc_bump_id & G_MIRROR_BUMP_SYNCID) != 0)
2659 sc->sc_bump_id &= ~G_MIRROR_BUMP_SYNCID;
2662 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2664 g_mirror_device_state2str(sc->sc_state),
2665 g_mirror_get_diskname(disk),
2666 g_mirror_disk_state2str(disk->d_state)));
2669 DISK_STATE_CHANGED();
2670 G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2671 sc->sc_name, g_mirror_get_diskname(disk));
2673 g_mirror_destroy_disk(disk);
2675 case G_MIRROR_DISK_STATE_DESTROY:
2679 error = g_mirror_clear_metadata(disk);
2682 "Device %s: failed to clear metadata on %s: %d.",
2683 sc->sc_name, g_mirror_get_diskname(disk), error);
2686 DISK_STATE_CHANGED();
2687 G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2688 sc->sc_name, g_mirror_get_diskname(disk));
2690 g_mirror_destroy_disk(disk);
2692 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2693 g_mirror_update_metadata(disk);
2698 KASSERT(1 == 0, ("Unknown state (%u).", state));
2703 #undef DISK_STATE_CHANGED
2706 g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2708 struct g_provider *pp;
2712 g_topology_assert();
2714 error = g_access(cp, 1, 0, 0);
2718 g_topology_unlock();
2719 /* Metadata are stored on last sector. */
2720 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2723 g_access(cp, -1, 0, 0);
2725 G_MIRROR_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2726 cp->provider->name, error);
2730 /* Decode metadata. */
2731 error = mirror_metadata_decode(buf, md);
2733 if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2735 if (md->md_version > G_MIRROR_VERSION) {
2737 "Kernel module is too old to handle metadata from %s.",
2738 cp->provider->name);
2742 G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2743 cp->provider->name);
2751 g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2752 struct g_mirror_metadata *md)
2755 if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2756 G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2757 pp->name, md->md_did);
2760 if (md->md_all != sc->sc_ndisks) {
2762 "Invalid '%s' field on disk %s (device %s), skipping.",
2763 "md_all", pp->name, sc->sc_name);
2766 if (md->md_slice != sc->sc_slice) {
2768 "Invalid '%s' field on disk %s (device %s), skipping.",
2769 "md_slice", pp->name, sc->sc_name);
2772 if (md->md_balance != sc->sc_balance) {
2774 "Invalid '%s' field on disk %s (device %s), skipping.",
2775 "md_balance", pp->name, sc->sc_name);
2779 if (md->md_mediasize != sc->sc_mediasize) {
2781 "Invalid '%s' field on disk %s (device %s), skipping.",
2782 "md_mediasize", pp->name, sc->sc_name);
2786 if (sc->sc_mediasize > pp->mediasize) {
2788 "Invalid size of disk %s (device %s), skipping.", pp->name,
2792 if (md->md_sectorsize != sc->sc_sectorsize) {
2794 "Invalid '%s' field on disk %s (device %s), skipping.",
2795 "md_sectorsize", pp->name, sc->sc_name);
2798 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2800 "Invalid sector size of disk %s (device %s), skipping.",
2801 pp->name, sc->sc_name);
2804 if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2806 "Invalid device flags on disk %s (device %s), skipping.",
2807 pp->name, sc->sc_name);
2810 if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2812 "Invalid disk flags on disk %s (device %s), skipping.",
2813 pp->name, sc->sc_name);
2820 g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2821 struct g_mirror_metadata *md)
2823 struct g_mirror_disk *disk;
2826 g_topology_assert_not();
2827 G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2829 error = g_mirror_check_metadata(sc, pp, md);
2832 if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING &&
2833 md->md_genid < sc->sc_genid) {
2834 G_MIRROR_DEBUG(0, "Component %s (device %s) broken, skipping.",
2835 pp->name, sc->sc_name);
2838 disk = g_mirror_init_disk(sc, pp, md, &error);
2841 error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
2842 G_MIRROR_EVENT_WAIT);
2845 if (md->md_version < G_MIRROR_VERSION) {
2846 G_MIRROR_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
2847 pp->name, md->md_version, G_MIRROR_VERSION);
2848 g_mirror_update_metadata(disk);
2854 g_mirror_destroy_delayed(void *arg, int flag)
2856 struct g_mirror_softc *sc;
2859 if (flag == EV_CANCEL) {
2860 G_MIRROR_DEBUG(1, "Destroying canceled.");
2864 g_topology_unlock();
2865 sx_xlock(&sc->sc_lock);
2866 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) == 0,
2867 ("DESTROY flag set on %s.", sc->sc_name));
2868 KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0,
2869 ("DESTROYING flag not set on %s.", sc->sc_name));
2870 G_MIRROR_DEBUG(1, "Destroying %s (delayed).", sc->sc_name);
2871 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_SOFT);
2873 G_MIRROR_DEBUG(0, "Cannot destroy %s (error=%d).",
2874 sc->sc_name, error);
2875 sx_xunlock(&sc->sc_lock);
2881 g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
2883 struct g_mirror_softc *sc;
2884 int dcr, dcw, dce, error = 0;
2886 g_topology_assert();
2887 G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2890 sc = pp->geom->softc;
2891 if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
2893 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
2895 dcr = pp->acr + acr;
2896 dcw = pp->acw + acw;
2897 dce = pp->ace + ace;
2899 g_topology_unlock();
2900 sx_xlock(&sc->sc_lock);
2901 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0 ||
2902 LIST_EMPTY(&sc->sc_disks)) {
2903 if (acr > 0 || acw > 0 || ace > 0)
2908 g_mirror_idle(sc, dcw);
2909 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROYING) != 0) {
2910 if (acr > 0 || acw > 0 || ace > 0) {
2914 if (dcr == 0 && dcw == 0 && dce == 0) {
2915 g_post_event(g_mirror_destroy_delayed, sc, M_WAITOK,
2920 sx_xunlock(&sc->sc_lock);
2925 static struct g_geom *
2926 g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
2928 struct g_mirror_softc *sc;
2932 g_topology_assert();
2933 G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
2936 /* One disk is minimum. */
2942 gp = g_new_geomf(mp, "%s", md->md_name);
2943 sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
2944 gp->start = g_mirror_start;
2945 gp->orphan = g_mirror_orphan;
2946 gp->access = g_mirror_access;
2947 gp->dumpconf = g_mirror_dumpconf;
2949 sc->sc_id = md->md_mid;
2950 sc->sc_slice = md->md_slice;
2951 sc->sc_balance = md->md_balance;
2952 sc->sc_mediasize = md->md_mediasize;
2953 sc->sc_sectorsize = md->md_sectorsize;
2954 sc->sc_ndisks = md->md_all;
2955 sc->sc_flags = md->md_mflags;
2958 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;
2976 * Synchronization geom.
2978 gp = g_new_geomf(mp, "%s.sync", md->md_name);
2980 gp->orphan = g_mirror_orphan;
2981 sc->sc_sync.ds_geom = gp;
2982 sc->sc_sync.ds_ndisks = 0;
2983 error = kproc_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
2984 "g_mirror %s", md->md_name);
2986 G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
2988 g_destroy_geom(sc->sc_sync.ds_geom);
2989 mtx_destroy(&sc->sc_done_mtx);
2990 mtx_destroy(&sc->sc_events_mtx);
2991 mtx_destroy(&sc->sc_queue_mtx);
2992 sx_destroy(&sc->sc_lock);
2993 g_destroy_geom(sc->sc_geom);
2998 G_MIRROR_DEBUG(1, "Device %s created (%u components, id=%u).",
2999 sc->sc_name, sc->sc_ndisks, sc->sc_id);
3001 sc->sc_rootmount = root_mount_hold("GMIRROR");
3002 G_MIRROR_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
3006 timeout = g_mirror_timeout * hz;
3007 callout_reset(&sc->sc_callout, timeout, g_mirror_go, sc);
3008 return (sc->sc_geom);
3012 g_mirror_destroy(struct g_mirror_softc *sc, int how)
3014 struct g_mirror_disk *disk;
3015 struct g_provider *pp;
3017 g_topology_assert_not();
3020 sx_assert(&sc->sc_lock, SX_XLOCKED);
3022 pp = sc->sc_provider;
3023 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0 ||
3024 SCHEDULER_STOPPED())) {
3026 case G_MIRROR_DESTROY_SOFT:
3028 "Device %s is still open (r%dw%de%d).", pp->name,
3029 pp->acr, pp->acw, pp->ace);
3031 case G_MIRROR_DESTROY_DELAYED:
3033 "Device %s will be destroyed on last close.",
3035 LIST_FOREACH(disk, &sc->sc_disks, d_next) {
3036 if (disk->d_state ==
3037 G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3038 g_mirror_sync_stop(disk, 1);
3041 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROYING;
3043 case G_MIRROR_DESTROY_HARD:
3044 G_MIRROR_DEBUG(1, "Device %s is still open, so it "
3045 "can't be definitely removed.", pp->name);
3050 if (sc->sc_geom->softc == NULL) {
3051 g_topology_unlock();
3054 sc->sc_geom->softc = NULL;
3055 sc->sc_sync.ds_geom->softc = NULL;
3056 g_topology_unlock();
3058 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
3059 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WAIT;
3060 G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3061 sx_xunlock(&sc->sc_lock);
3062 mtx_lock(&sc->sc_queue_mtx);
3064 mtx_unlock(&sc->sc_queue_mtx);
3065 G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3066 while (sc->sc_worker != NULL)
3067 tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
3068 G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3069 sx_xlock(&sc->sc_lock);
3070 g_mirror_destroy_device(sc);
3076 g_mirror_taste_orphan(struct g_consumer *cp)
3079 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3080 cp->provider->name));
3083 static struct g_geom *
3084 g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3086 struct g_mirror_metadata md;
3087 struct g_mirror_softc *sc;
3088 struct g_consumer *cp;
3092 g_topology_assert();
3093 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3094 G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
3096 gp = g_new_geomf(mp, "mirror:taste");
3098 * This orphan function should be never called.
3100 gp->orphan = g_mirror_taste_orphan;
3101 cp = g_new_consumer(gp);
3103 error = g_mirror_read_metadata(cp, &md);
3105 g_destroy_consumer(cp);
3111 if (md.md_provider[0] != '\0' &&
3112 !g_compare_names(md.md_provider, pp->name))
3114 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3116 if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
3118 "Device %s: provider %s marked as inactive, skipping.",
3119 md.md_name, pp->name);
3122 if (g_mirror_debug >= 2)
3123 mirror_metadata_dump(&md);
3126 * Let's check if device already exists.
3129 LIST_FOREACH(gp, &mp->geom, geom) {
3133 if (sc->sc_sync.ds_geom == gp)
3135 if (strcmp(md.md_name, sc->sc_name) != 0)
3137 if (md.md_mid != sc->sc_id) {
3138 G_MIRROR_DEBUG(0, "Device %s already configured.",
3145 gp = g_mirror_create(mp, &md);
3147 G_MIRROR_DEBUG(0, "Cannot create device %s.",
3153 G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3154 g_topology_unlock();
3155 sx_xlock(&sc->sc_lock);
3156 sc->sc_flags |= G_MIRROR_DEVICE_FLAG_TASTING;
3157 error = g_mirror_add_disk(sc, pp, &md);
3159 G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3160 pp->name, gp->name, error);
3161 if (LIST_EMPTY(&sc->sc_disks)) {
3163 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3169 sc->sc_flags &= ~G_MIRROR_DEVICE_FLAG_TASTING;
3170 if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
3171 g_mirror_destroy(sc, G_MIRROR_DESTROY_HARD);
3175 sx_xunlock(&sc->sc_lock);
3181 g_mirror_resize(struct g_consumer *cp)
3183 struct g_mirror_disk *disk;
3185 g_topology_assert();
3186 g_trace(G_T_TOPOLOGY, "%s(%s)", __func__, cp->provider->name);
3191 g_topology_unlock();
3192 g_mirror_update_metadata(disk);
3197 g_mirror_destroy_geom(struct gctl_req *req __unused,
3198 struct g_class *mp __unused, struct g_geom *gp)
3200 struct g_mirror_softc *sc;
3203 g_topology_unlock();
3205 sx_xlock(&sc->sc_lock);
3207 error = g_mirror_destroy(gp->softc, G_MIRROR_DESTROY_SOFT);
3209 sx_xunlock(&sc->sc_lock);
3215 g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3216 struct g_consumer *cp, struct g_provider *pp)
3218 struct g_mirror_softc *sc;
3220 g_topology_assert();
3225 /* Skip synchronization geom. */
3226 if (gp == sc->sc_sync.ds_geom)
3230 } else if (cp != NULL) {
3231 struct g_mirror_disk *disk;
3236 g_topology_unlock();
3237 sx_xlock(&sc->sc_lock);
3238 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
3239 if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
3240 sbuf_printf(sb, "%s<Synchronized>", indent);
3241 if (disk->d_sync.ds_offset == 0)
3242 sbuf_printf(sb, "0%%");
3244 sbuf_printf(sb, "%u%%",
3245 (u_int)((disk->d_sync.ds_offset * 100) /
3246 sc->sc_provider->mediasize));
3248 sbuf_printf(sb, "</Synchronized>\n");
3249 if (disk->d_sync.ds_offset > 0) {
3250 sbuf_printf(sb, "%s<BytesSynced>%jd"
3251 "</BytesSynced>\n", indent,
3252 (intmax_t)disk->d_sync.ds_offset);
3255 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3256 disk->d_sync.ds_syncid);
3257 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent,
3259 sbuf_printf(sb, "%s<Flags>", indent);
3260 if (disk->d_flags == 0)
3261 sbuf_printf(sb, "NONE");
3265 #define ADD_FLAG(flag, name) do { \
3266 if ((disk->d_flags & (flag)) != 0) { \
3268 sbuf_printf(sb, ", "); \
3271 sbuf_printf(sb, name); \
3274 ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
3275 ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
3276 ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
3277 ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
3279 ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3280 ADD_FLAG(G_MIRROR_DISK_FLAG_BROKEN, "BROKEN");
3283 sbuf_printf(sb, "</Flags>\n");
3284 sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
3286 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3287 g_mirror_disk_state2str(disk->d_state));
3288 sx_xunlock(&sc->sc_lock);
3291 g_topology_unlock();
3292 sx_xlock(&sc->sc_lock);
3293 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3294 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3295 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3296 sbuf_printf(sb, "%s<Flags>", indent);
3297 if (sc->sc_flags == 0)
3298 sbuf_printf(sb, "NONE");
3302 #define ADD_FLAG(flag, name) do { \
3303 if ((sc->sc_flags & (flag)) != 0) { \
3305 sbuf_printf(sb, ", "); \
3308 sbuf_printf(sb, name); \
3311 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3312 ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3315 sbuf_printf(sb, "</Flags>\n");
3316 sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
3317 (u_int)sc->sc_slice);
3318 sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
3319 balance_name(sc->sc_balance));
3320 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3322 sbuf_printf(sb, "%s<State>", indent);
3323 if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
3324 sbuf_printf(sb, "%s", "STARTING");
3325 else if (sc->sc_ndisks ==
3326 g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
3327 sbuf_printf(sb, "%s", "COMPLETE");
3329 sbuf_printf(sb, "%s", "DEGRADED");
3330 sbuf_printf(sb, "</State>\n");
3331 sx_xunlock(&sc->sc_lock);
3337 g_mirror_shutdown_post_sync(void *arg, int howto)
3340 struct g_geom *gp, *gp2;
3341 struct g_mirror_softc *sc;
3346 g_mirror_shutdown = 1;
3347 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3348 if ((sc = gp->softc) == NULL)
3350 /* Skip synchronization geom. */
3351 if (gp == sc->sc_sync.ds_geom)
3353 g_topology_unlock();
3354 sx_xlock(&sc->sc_lock);
3355 g_mirror_idle(sc, -1);
3357 error = g_mirror_destroy(sc, G_MIRROR_DESTROY_DELAYED);
3359 sx_xunlock(&sc->sc_lock);
3362 g_topology_unlock();
3366 g_mirror_init(struct g_class *mp)
3369 g_mirror_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
3370 g_mirror_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
3371 if (g_mirror_post_sync == NULL)
3372 G_MIRROR_DEBUG(0, "Warning! Cannot register shutdown event.");
3376 g_mirror_fini(struct g_class *mp)
3379 if (g_mirror_post_sync != NULL)
3380 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_mirror_post_sync);
3383 DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);