2 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/limits.h>
36 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/eventhandler.h>
43 #include <geom/geom.h>
45 #include <sys/kthread.h>
46 #include <sys/sched.h>
47 #include <geom/raid3/g_raid3.h>
49 FEATURE(geom_raid3, "GEOM RAID-3 functionality");
51 static MALLOC_DEFINE(M_RAID3, "raid3_data", "GEOM_RAID3 Data");
53 SYSCTL_DECL(_kern_geom);
54 SYSCTL_NODE(_kern_geom, OID_AUTO, raid3, CTLFLAG_RW, 0, "GEOM_RAID3 stuff");
55 u_int g_raid3_debug = 0;
56 TUNABLE_INT("kern.geom.raid3.debug", &g_raid3_debug);
57 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, debug, CTLFLAG_RW, &g_raid3_debug, 0,
59 static u_int g_raid3_timeout = 4;
60 TUNABLE_INT("kern.geom.raid3.timeout", &g_raid3_timeout);
61 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, timeout, CTLFLAG_RW, &g_raid3_timeout,
62 0, "Time to wait on all raid3 components");
63 static u_int g_raid3_idletime = 5;
64 TUNABLE_INT("kern.geom.raid3.idletime", &g_raid3_idletime);
65 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, idletime, CTLFLAG_RW,
66 &g_raid3_idletime, 0, "Mark components as clean when idling");
67 static u_int g_raid3_disconnect_on_failure = 1;
68 TUNABLE_INT("kern.geom.raid3.disconnect_on_failure",
69 &g_raid3_disconnect_on_failure);
70 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
71 &g_raid3_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
72 static u_int g_raid3_syncreqs = 2;
73 TUNABLE_INT("kern.geom.raid3.sync_requests", &g_raid3_syncreqs);
74 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
75 &g_raid3_syncreqs, 0, "Parallel synchronization I/O requests.");
76 static u_int g_raid3_use_malloc = 0;
77 TUNABLE_INT("kern.geom.raid3.use_malloc", &g_raid3_use_malloc);
78 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, use_malloc, CTLFLAG_RDTUN,
79 &g_raid3_use_malloc, 0, "Use malloc(9) instead of uma(9).");
81 static u_int g_raid3_n64k = 50;
82 TUNABLE_INT("kern.geom.raid3.n64k", &g_raid3_n64k);
83 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n64k, CTLFLAG_RD, &g_raid3_n64k, 0,
84 "Maximum number of 64kB allocations");
85 static u_int g_raid3_n16k = 200;
86 TUNABLE_INT("kern.geom.raid3.n16k", &g_raid3_n16k);
87 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n16k, CTLFLAG_RD, &g_raid3_n16k, 0,
88 "Maximum number of 16kB allocations");
89 static u_int g_raid3_n4k = 1200;
90 TUNABLE_INT("kern.geom.raid3.n4k", &g_raid3_n4k);
91 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n4k, CTLFLAG_RD, &g_raid3_n4k, 0,
92 "Maximum number of 4kB allocations");
94 SYSCTL_NODE(_kern_geom_raid3, OID_AUTO, stat, CTLFLAG_RW, 0,
95 "GEOM_RAID3 statistics");
96 static u_int g_raid3_parity_mismatch = 0;
97 SYSCTL_UINT(_kern_geom_raid3_stat, OID_AUTO, parity_mismatch, CTLFLAG_RD,
98 &g_raid3_parity_mismatch, 0, "Number of failures in VERIFY mode");
100 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \
101 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
102 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
103 G_RAID3_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
106 static eventhandler_tag g_raid3_pre_sync = NULL;
108 static int g_raid3_destroy_geom(struct gctl_req *req, struct g_class *mp,
110 static g_taste_t g_raid3_taste;
111 static void g_raid3_init(struct g_class *mp);
112 static void g_raid3_fini(struct g_class *mp);
114 struct g_class g_raid3_class = {
115 .name = G_RAID3_CLASS_NAME,
116 .version = G_VERSION,
117 .ctlreq = g_raid3_config,
118 .taste = g_raid3_taste,
119 .destroy_geom = g_raid3_destroy_geom,
120 .init = g_raid3_init,
125 static void g_raid3_destroy_provider(struct g_raid3_softc *sc);
126 static int g_raid3_update_disk(struct g_raid3_disk *disk, u_int state);
127 static void g_raid3_update_device(struct g_raid3_softc *sc, boolean_t force);
128 static void g_raid3_dumpconf(struct sbuf *sb, const char *indent,
129 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
130 static void g_raid3_sync_stop(struct g_raid3_softc *sc, int type);
131 static int g_raid3_register_request(struct bio *pbp);
132 static void g_raid3_sync_release(struct g_raid3_softc *sc);
136 g_raid3_disk_state2str(int state)
140 case G_RAID3_DISK_STATE_NODISK:
142 case G_RAID3_DISK_STATE_NONE:
144 case G_RAID3_DISK_STATE_NEW:
146 case G_RAID3_DISK_STATE_ACTIVE:
148 case G_RAID3_DISK_STATE_STALE:
150 case G_RAID3_DISK_STATE_SYNCHRONIZING:
151 return ("SYNCHRONIZING");
152 case G_RAID3_DISK_STATE_DISCONNECTED:
153 return ("DISCONNECTED");
160 g_raid3_device_state2str(int state)
164 case G_RAID3_DEVICE_STATE_STARTING:
166 case G_RAID3_DEVICE_STATE_DEGRADED:
168 case G_RAID3_DEVICE_STATE_COMPLETE:
176 g_raid3_get_diskname(struct g_raid3_disk *disk)
179 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
180 return ("[unknown]");
181 return (disk->d_name);
185 g_raid3_alloc(struct g_raid3_softc *sc, size_t size, int flags)
188 enum g_raid3_zones zone;
190 if (g_raid3_use_malloc ||
191 (zone = g_raid3_zone(size)) == G_RAID3_NUM_ZONES)
192 ptr = malloc(size, M_RAID3, flags);
194 ptr = uma_zalloc_arg(sc->sc_zones[zone].sz_zone,
195 &sc->sc_zones[zone], flags);
196 sc->sc_zones[zone].sz_requested++;
198 sc->sc_zones[zone].sz_failed++;
204 g_raid3_free(struct g_raid3_softc *sc, void *ptr, size_t size)
206 enum g_raid3_zones zone;
208 if (g_raid3_use_malloc ||
209 (zone = g_raid3_zone(size)) == G_RAID3_NUM_ZONES)
212 uma_zfree_arg(sc->sc_zones[zone].sz_zone,
213 ptr, &sc->sc_zones[zone]);
218 g_raid3_uma_ctor(void *mem, int size, void *arg, int flags)
220 struct g_raid3_zone *sz = arg;
222 if (sz->sz_max > 0 && sz->sz_inuse == sz->sz_max)
229 g_raid3_uma_dtor(void *mem, int size, void *arg)
231 struct g_raid3_zone *sz = arg;
236 #define g_raid3_xor(src, dst, size) \
237 _g_raid3_xor((uint64_t *)(src), \
238 (uint64_t *)(dst), (size_t)size)
240 _g_raid3_xor(uint64_t *src, uint64_t *dst, size_t size)
243 KASSERT((size % 128) == 0, ("Invalid size: %zu.", size));
244 for (; size > 0; size -= 128) {
265 g_raid3_is_zero(struct bio *bp)
267 static const uint64_t zeros[] = {
268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
273 size = bp->bio_length;
274 addr = (u_char *)bp->bio_data;
275 for (; size > 0; size -= sizeof(zeros), addr += sizeof(zeros)) {
276 if (bcmp(addr, zeros, sizeof(zeros)) != 0)
283 * --- Events handling functions ---
284 * Events in geom_raid3 are used to maintain disks and device status
285 * from one thread to simplify locking.
288 g_raid3_event_free(struct g_raid3_event *ep)
295 g_raid3_event_send(void *arg, int state, int flags)
297 struct g_raid3_softc *sc;
298 struct g_raid3_disk *disk;
299 struct g_raid3_event *ep;
302 ep = malloc(sizeof(*ep), M_RAID3, M_WAITOK);
303 G_RAID3_DEBUG(4, "%s: Sending event %p.", __func__, ep);
304 if ((flags & G_RAID3_EVENT_DEVICE) != 0) {
315 mtx_lock(&sc->sc_events_mtx);
316 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
317 mtx_unlock(&sc->sc_events_mtx);
318 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
319 mtx_lock(&sc->sc_queue_mtx);
321 wakeup(&sc->sc_queue);
322 mtx_unlock(&sc->sc_queue_mtx);
323 if ((flags & G_RAID3_EVENT_DONTWAIT) != 0)
325 sx_assert(&sc->sc_lock, SX_XLOCKED);
326 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
327 sx_xunlock(&sc->sc_lock);
328 while ((ep->e_flags & G_RAID3_EVENT_DONE) == 0) {
329 mtx_lock(&sc->sc_events_mtx);
330 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "r3:event",
334 g_raid3_event_free(ep);
335 sx_xlock(&sc->sc_lock);
339 static struct g_raid3_event *
340 g_raid3_event_get(struct g_raid3_softc *sc)
342 struct g_raid3_event *ep;
344 mtx_lock(&sc->sc_events_mtx);
345 ep = TAILQ_FIRST(&sc->sc_events);
346 mtx_unlock(&sc->sc_events_mtx);
351 g_raid3_event_remove(struct g_raid3_softc *sc, struct g_raid3_event *ep)
354 mtx_lock(&sc->sc_events_mtx);
355 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
356 mtx_unlock(&sc->sc_events_mtx);
360 g_raid3_event_cancel(struct g_raid3_disk *disk)
362 struct g_raid3_softc *sc;
363 struct g_raid3_event *ep, *tmpep;
366 sx_assert(&sc->sc_lock, SX_XLOCKED);
368 mtx_lock(&sc->sc_events_mtx);
369 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
370 if ((ep->e_flags & G_RAID3_EVENT_DEVICE) != 0)
372 if (ep->e_disk != disk)
374 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
375 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0)
376 g_raid3_event_free(ep);
378 ep->e_error = ECANCELED;
382 mtx_unlock(&sc->sc_events_mtx);
386 * Return the number of disks in the given state.
387 * If state is equal to -1, count all connected disks.
390 g_raid3_ndisks(struct g_raid3_softc *sc, int state)
392 struct g_raid3_disk *disk;
395 sx_assert(&sc->sc_lock, SX_LOCKED);
397 for (n = ndisks = 0; n < sc->sc_ndisks; n++) {
398 disk = &sc->sc_disks[n];
399 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
401 if (state == -1 || disk->d_state == state)
408 g_raid3_nrequests(struct g_raid3_softc *sc, struct g_consumer *cp)
413 mtx_lock(&sc->sc_queue_mtx);
414 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
415 if (bp->bio_from == cp)
418 mtx_unlock(&sc->sc_queue_mtx);
423 g_raid3_is_busy(struct g_raid3_softc *sc, struct g_consumer *cp)
428 "I/O requests for %s exist, can't destroy it now.",
432 if (g_raid3_nrequests(sc, cp) > 0) {
434 "I/O requests for %s in queue, can't destroy it now.",
442 g_raid3_destroy_consumer(void *arg, int flags __unused)
444 struct g_consumer *cp;
449 G_RAID3_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
451 g_destroy_consumer(cp);
455 g_raid3_kill_consumer(struct g_raid3_softc *sc, struct g_consumer *cp)
457 struct g_provider *pp;
463 if (g_raid3_is_busy(sc, cp))
465 G_RAID3_DEBUG(2, "Consumer %s destroyed.", cp->provider->name);
469 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
472 G_RAID3_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
473 -cp->acw, -cp->ace, 0);
474 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
475 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
478 * After retaste event was send (inside g_access()), we can send
479 * event to detach and destroy consumer.
480 * A class, which has consumer to the given provider connected
481 * will not receive retaste event for the provider.
482 * This is the way how I ignore retaste events when I close
483 * consumers opened for write: I detach and destroy consumer
484 * after retaste event is sent.
486 g_post_event(g_raid3_destroy_consumer, cp, M_WAITOK, NULL);
489 G_RAID3_DEBUG(1, "Consumer %s destroyed.", pp->name);
491 g_destroy_consumer(cp);
495 g_raid3_connect_disk(struct g_raid3_disk *disk, struct g_provider *pp)
497 struct g_consumer *cp;
500 g_topology_assert_not();
501 KASSERT(disk->d_consumer == NULL,
502 ("Disk already connected (device %s).", disk->d_softc->sc_name));
505 cp = g_new_consumer(disk->d_softc->sc_geom);
506 error = g_attach(cp, pp);
508 g_destroy_consumer(cp);
512 error = g_access(cp, 1, 1, 1);
516 g_destroy_consumer(cp);
517 G_RAID3_DEBUG(0, "Cannot open consumer %s (error=%d).",
521 disk->d_consumer = cp;
522 disk->d_consumer->private = disk;
523 disk->d_consumer->index = 0;
524 G_RAID3_DEBUG(2, "Disk %s connected.", g_raid3_get_diskname(disk));
529 g_raid3_disconnect_consumer(struct g_raid3_softc *sc, struct g_consumer *cp)
536 if (cp->provider != NULL)
537 g_raid3_kill_consumer(sc, cp);
539 g_destroy_consumer(cp);
543 * Initialize disk. This means allocate memory, create consumer, attach it
544 * to the provider and open access (r1w1e1) to it.
546 static struct g_raid3_disk *
547 g_raid3_init_disk(struct g_raid3_softc *sc, struct g_provider *pp,
548 struct g_raid3_metadata *md, int *errorp)
550 struct g_raid3_disk *disk;
553 disk = &sc->sc_disks[md->md_no];
554 error = g_raid3_connect_disk(disk, pp);
560 disk->d_state = G_RAID3_DISK_STATE_NONE;
561 disk->d_flags = md->md_dflags;
562 if (md->md_provider[0] != '\0')
563 disk->d_flags |= G_RAID3_DISK_FLAG_HARDCODED;
564 disk->d_sync.ds_consumer = NULL;
565 disk->d_sync.ds_offset = md->md_sync_offset;
566 disk->d_sync.ds_offset_done = md->md_sync_offset;
567 disk->d_genid = md->md_genid;
568 disk->d_sync.ds_syncid = md->md_syncid;
575 g_raid3_destroy_disk(struct g_raid3_disk *disk)
577 struct g_raid3_softc *sc;
579 g_topology_assert_not();
581 sx_assert(&sc->sc_lock, SX_XLOCKED);
583 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
585 g_raid3_event_cancel(disk);
586 switch (disk->d_state) {
587 case G_RAID3_DISK_STATE_SYNCHRONIZING:
588 if (sc->sc_syncdisk != NULL)
589 g_raid3_sync_stop(sc, 1);
591 case G_RAID3_DISK_STATE_NEW:
592 case G_RAID3_DISK_STATE_STALE:
593 case G_RAID3_DISK_STATE_ACTIVE:
595 g_raid3_disconnect_consumer(sc, disk->d_consumer);
597 disk->d_consumer = NULL;
600 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
601 g_raid3_get_diskname(disk),
602 g_raid3_disk_state2str(disk->d_state)));
604 disk->d_state = G_RAID3_DISK_STATE_NODISK;
608 g_raid3_destroy_device(struct g_raid3_softc *sc)
610 struct g_raid3_event *ep;
611 struct g_raid3_disk *disk;
613 struct g_consumer *cp;
616 g_topology_assert_not();
617 sx_assert(&sc->sc_lock, SX_XLOCKED);
620 if (sc->sc_provider != NULL)
621 g_raid3_destroy_provider(sc);
622 for (n = 0; n < sc->sc_ndisks; n++) {
623 disk = &sc->sc_disks[n];
624 if (disk->d_state != G_RAID3_DISK_STATE_NODISK) {
625 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
626 g_raid3_update_metadata(disk);
627 g_raid3_destroy_disk(disk);
630 while ((ep = g_raid3_event_get(sc)) != NULL) {
631 g_raid3_event_remove(sc, ep);
632 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0)
633 g_raid3_event_free(ep);
635 ep->e_error = ECANCELED;
636 ep->e_flags |= G_RAID3_EVENT_DONE;
637 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, ep);
638 mtx_lock(&sc->sc_events_mtx);
640 mtx_unlock(&sc->sc_events_mtx);
643 callout_drain(&sc->sc_callout);
644 cp = LIST_FIRST(&sc->sc_sync.ds_geom->consumer);
647 g_raid3_disconnect_consumer(sc, cp);
648 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
649 G_RAID3_DEBUG(0, "Device %s destroyed.", gp->name);
650 g_wither_geom(gp, ENXIO);
652 if (!g_raid3_use_malloc) {
653 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_64K].sz_zone);
654 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_16K].sz_zone);
655 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_4K].sz_zone);
657 mtx_destroy(&sc->sc_queue_mtx);
658 mtx_destroy(&sc->sc_events_mtx);
659 sx_xunlock(&sc->sc_lock);
660 sx_destroy(&sc->sc_lock);
664 g_raid3_orphan(struct g_consumer *cp)
666 struct g_raid3_disk *disk;
673 disk->d_softc->sc_bump_id = G_RAID3_BUMP_SYNCID;
674 g_raid3_event_send(disk, G_RAID3_DISK_STATE_DISCONNECTED,
675 G_RAID3_EVENT_DONTWAIT);
679 g_raid3_write_metadata(struct g_raid3_disk *disk, struct g_raid3_metadata *md)
681 struct g_raid3_softc *sc;
682 struct g_consumer *cp;
683 off_t offset, length;
687 g_topology_assert_not();
689 sx_assert(&sc->sc_lock, SX_LOCKED);
691 cp = disk->d_consumer;
692 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
693 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
694 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
695 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
697 length = cp->provider->sectorsize;
698 offset = cp->provider->mediasize - length;
699 sector = malloc((size_t)length, M_RAID3, M_WAITOK | M_ZERO);
701 raid3_metadata_encode(md, sector);
702 error = g_write_data(cp, offset, sector, length);
703 free(sector, M_RAID3);
705 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
706 G_RAID3_DEBUG(0, "Cannot write metadata on %s "
707 "(device=%s, error=%d).",
708 g_raid3_get_diskname(disk), sc->sc_name, error);
709 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
711 G_RAID3_DEBUG(1, "Cannot write metadata on %s "
712 "(device=%s, error=%d).",
713 g_raid3_get_diskname(disk), sc->sc_name, error);
715 if (g_raid3_disconnect_on_failure &&
716 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
717 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
718 g_raid3_event_send(disk,
719 G_RAID3_DISK_STATE_DISCONNECTED,
720 G_RAID3_EVENT_DONTWAIT);
727 g_raid3_clear_metadata(struct g_raid3_disk *disk)
731 g_topology_assert_not();
732 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
734 error = g_raid3_write_metadata(disk, NULL);
736 G_RAID3_DEBUG(2, "Metadata on %s cleared.",
737 g_raid3_get_diskname(disk));
740 "Cannot clear metadata on disk %s (error=%d).",
741 g_raid3_get_diskname(disk), error);
747 g_raid3_fill_metadata(struct g_raid3_disk *disk, struct g_raid3_metadata *md)
749 struct g_raid3_softc *sc;
750 struct g_provider *pp;
753 strlcpy(md->md_magic, G_RAID3_MAGIC, sizeof(md->md_magic));
754 md->md_version = G_RAID3_VERSION;
755 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
756 md->md_id = sc->sc_id;
757 md->md_all = sc->sc_ndisks;
758 md->md_genid = sc->sc_genid;
759 md->md_mediasize = sc->sc_mediasize;
760 md->md_sectorsize = sc->sc_sectorsize;
761 md->md_mflags = (sc->sc_flags & G_RAID3_DEVICE_FLAG_MASK);
762 md->md_no = disk->d_no;
763 md->md_syncid = disk->d_sync.ds_syncid;
764 md->md_dflags = (disk->d_flags & G_RAID3_DISK_FLAG_MASK);
765 if (disk->d_state != G_RAID3_DISK_STATE_SYNCHRONIZING)
766 md->md_sync_offset = 0;
769 disk->d_sync.ds_offset_done / (sc->sc_ndisks - 1);
771 if (disk->d_consumer != NULL && disk->d_consumer->provider != NULL)
772 pp = disk->d_consumer->provider;
775 if ((disk->d_flags & G_RAID3_DISK_FLAG_HARDCODED) != 0 && pp != NULL)
776 strlcpy(md->md_provider, pp->name, sizeof(md->md_provider));
778 bzero(md->md_provider, sizeof(md->md_provider));
780 md->md_provsize = pp->mediasize;
786 g_raid3_update_metadata(struct g_raid3_disk *disk)
788 struct g_raid3_softc *sc;
789 struct g_raid3_metadata md;
792 g_topology_assert_not();
794 sx_assert(&sc->sc_lock, SX_LOCKED);
796 g_raid3_fill_metadata(disk, &md);
797 error = g_raid3_write_metadata(disk, &md);
799 G_RAID3_DEBUG(2, "Metadata on %s updated.",
800 g_raid3_get_diskname(disk));
803 "Cannot update metadata on disk %s (error=%d).",
804 g_raid3_get_diskname(disk), error);
809 g_raid3_bump_syncid(struct g_raid3_softc *sc)
811 struct g_raid3_disk *disk;
814 g_topology_assert_not();
815 sx_assert(&sc->sc_lock, SX_XLOCKED);
816 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) > 0,
817 ("%s called with no active disks (device=%s).", __func__,
821 G_RAID3_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
823 for (n = 0; n < sc->sc_ndisks; n++) {
824 disk = &sc->sc_disks[n];
825 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
826 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
827 disk->d_sync.ds_syncid = sc->sc_syncid;
828 g_raid3_update_metadata(disk);
834 g_raid3_bump_genid(struct g_raid3_softc *sc)
836 struct g_raid3_disk *disk;
839 g_topology_assert_not();
840 sx_assert(&sc->sc_lock, SX_XLOCKED);
841 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) > 0,
842 ("%s called with no active disks (device=%s).", __func__,
846 G_RAID3_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
848 for (n = 0; n < sc->sc_ndisks; n++) {
849 disk = &sc->sc_disks[n];
850 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
851 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
852 disk->d_genid = sc->sc_genid;
853 g_raid3_update_metadata(disk);
859 g_raid3_idle(struct g_raid3_softc *sc, int acw)
861 struct g_raid3_disk *disk;
865 g_topology_assert_not();
866 sx_assert(&sc->sc_lock, SX_XLOCKED);
868 if (sc->sc_provider == NULL)
870 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
874 if (sc->sc_writes > 0)
876 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
877 timeout = g_raid3_idletime - (time_uptime - sc->sc_last_write);
882 for (i = 0; i < sc->sc_ndisks; i++) {
883 disk = &sc->sc_disks[i];
884 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
886 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as clean.",
887 g_raid3_get_diskname(disk), sc->sc_name);
888 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
889 g_raid3_update_metadata(disk);
895 g_raid3_unidle(struct g_raid3_softc *sc)
897 struct g_raid3_disk *disk;
900 g_topology_assert_not();
901 sx_assert(&sc->sc_lock, SX_XLOCKED);
903 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
906 sc->sc_last_write = time_uptime;
907 for (i = 0; i < sc->sc_ndisks; i++) {
908 disk = &sc->sc_disks[i];
909 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
911 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as dirty.",
912 g_raid3_get_diskname(disk), sc->sc_name);
913 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
914 g_raid3_update_metadata(disk);
919 * Treat bio_driver1 field in parent bio as list head and field bio_caller1
920 * in child bio as pointer to the next element on the list.
922 #define G_RAID3_HEAD_BIO(pbp) (pbp)->bio_driver1
924 #define G_RAID3_NEXT_BIO(cbp) (cbp)->bio_caller1
926 #define G_RAID3_FOREACH_BIO(pbp, bp) \
927 for ((bp) = G_RAID3_HEAD_BIO(pbp); (bp) != NULL; \
928 (bp) = G_RAID3_NEXT_BIO(bp))
930 #define G_RAID3_FOREACH_SAFE_BIO(pbp, bp, tmpbp) \
931 for ((bp) = G_RAID3_HEAD_BIO(pbp); \
932 (bp) != NULL && ((tmpbp) = G_RAID3_NEXT_BIO(bp), 1); \
936 g_raid3_init_bio(struct bio *pbp)
939 G_RAID3_HEAD_BIO(pbp) = NULL;
943 g_raid3_remove_bio(struct bio *cbp)
945 struct bio *pbp, *bp;
947 pbp = cbp->bio_parent;
948 if (G_RAID3_HEAD_BIO(pbp) == cbp)
949 G_RAID3_HEAD_BIO(pbp) = G_RAID3_NEXT_BIO(cbp);
951 G_RAID3_FOREACH_BIO(pbp, bp) {
952 if (G_RAID3_NEXT_BIO(bp) == cbp) {
953 G_RAID3_NEXT_BIO(bp) = G_RAID3_NEXT_BIO(cbp);
958 G_RAID3_NEXT_BIO(cbp) = NULL;
962 g_raid3_replace_bio(struct bio *sbp, struct bio *dbp)
964 struct bio *pbp, *bp;
966 g_raid3_remove_bio(sbp);
967 pbp = dbp->bio_parent;
968 G_RAID3_NEXT_BIO(sbp) = G_RAID3_NEXT_BIO(dbp);
969 if (G_RAID3_HEAD_BIO(pbp) == dbp)
970 G_RAID3_HEAD_BIO(pbp) = sbp;
972 G_RAID3_FOREACH_BIO(pbp, bp) {
973 if (G_RAID3_NEXT_BIO(bp) == dbp) {
974 G_RAID3_NEXT_BIO(bp) = sbp;
979 G_RAID3_NEXT_BIO(dbp) = NULL;
983 g_raid3_destroy_bio(struct g_raid3_softc *sc, struct bio *cbp)
985 struct bio *bp, *pbp;
988 pbp = cbp->bio_parent;
990 KASSERT(cbp->bio_data != NULL, ("NULL bio_data"));
991 size = pbp->bio_length / (sc->sc_ndisks - 1);
992 g_raid3_free(sc, cbp->bio_data, size);
993 if (G_RAID3_HEAD_BIO(pbp) == cbp) {
994 G_RAID3_HEAD_BIO(pbp) = G_RAID3_NEXT_BIO(cbp);
995 G_RAID3_NEXT_BIO(cbp) = NULL;
998 G_RAID3_FOREACH_BIO(pbp, bp) {
999 if (G_RAID3_NEXT_BIO(bp) == cbp)
1003 KASSERT(G_RAID3_NEXT_BIO(bp) != NULL,
1004 ("NULL bp->bio_driver1"));
1005 G_RAID3_NEXT_BIO(bp) = G_RAID3_NEXT_BIO(cbp);
1006 G_RAID3_NEXT_BIO(cbp) = NULL;
1013 g_raid3_clone_bio(struct g_raid3_softc *sc, struct bio *pbp)
1015 struct bio *bp, *cbp;
1019 cbp = g_clone_bio(pbp);
1022 size = pbp->bio_length / (sc->sc_ndisks - 1);
1023 if ((pbp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR) != 0)
1027 cbp->bio_data = g_raid3_alloc(sc, size, memflag);
1028 if (cbp->bio_data == NULL) {
1029 pbp->bio_children--;
1033 G_RAID3_NEXT_BIO(cbp) = NULL;
1034 if (G_RAID3_HEAD_BIO(pbp) == NULL)
1035 G_RAID3_HEAD_BIO(pbp) = cbp;
1037 G_RAID3_FOREACH_BIO(pbp, bp) {
1038 if (G_RAID3_NEXT_BIO(bp) == NULL) {
1039 G_RAID3_NEXT_BIO(bp) = cbp;
1048 g_raid3_scatter(struct bio *pbp)
1050 struct g_raid3_softc *sc;
1051 struct g_raid3_disk *disk;
1052 struct bio *bp, *cbp, *tmpbp;
1053 off_t atom, cadd, padd, left;
1056 sc = pbp->bio_to->geom->softc;
1058 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_NOPARITY) == 0) {
1060 * Find bio for which we should calculate data.
1062 G_RAID3_FOREACH_BIO(pbp, cbp) {
1063 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0) {
1068 KASSERT(bp != NULL, ("NULL parity bio."));
1070 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1072 for (left = pbp->bio_length; left > 0; left -= sc->sc_sectorsize) {
1073 G_RAID3_FOREACH_BIO(pbp, cbp) {
1076 bcopy(pbp->bio_data + padd, cbp->bio_data + cadd, atom);
1081 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_NOPARITY) == 0) {
1086 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1090 bcopy(cbp->bio_data, bp->bio_data,
1094 g_raid3_xor(cbp->bio_data, bp->bio_data,
1097 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_NODISK) != 0)
1098 g_raid3_destroy_bio(sc, cbp);
1101 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1102 struct g_consumer *cp;
1104 disk = cbp->bio_caller2;
1105 cp = disk->d_consumer;
1106 cbp->bio_to = cp->provider;
1107 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1108 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1109 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1110 cp->acr, cp->acw, cp->ace));
1113 g_io_request(cbp, cp);
1118 g_raid3_gather(struct bio *pbp)
1120 struct g_raid3_softc *sc;
1121 struct g_raid3_disk *disk;
1122 struct bio *xbp, *fbp, *cbp;
1123 off_t atom, cadd, padd, left;
1125 sc = pbp->bio_to->geom->softc;
1127 * Find bio for which we have to calculate data.
1128 * While going through this path, check if all requests
1129 * succeeded, if not, deny whole request.
1130 * If we're in COMPLETE mode, we allow one request to fail,
1131 * so if we find one, we're sending it to the parity consumer.
1132 * If there are more failed requests, we deny whole request.
1135 G_RAID3_FOREACH_BIO(pbp, cbp) {
1136 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0) {
1137 KASSERT(xbp == NULL, ("More than one parity bio."));
1140 if (cbp->bio_error == 0)
1143 * Found failed request.
1146 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_DEGRADED) != 0) {
1148 * We are already in degraded mode, so we can't
1149 * accept any failures.
1151 if (pbp->bio_error == 0)
1152 pbp->bio_error = cbp->bio_error;
1158 * Next failed request, that's too many.
1160 if (pbp->bio_error == 0)
1161 pbp->bio_error = fbp->bio_error;
1163 disk = cbp->bio_caller2;
1166 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
1167 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
1168 G_RAID3_LOGREQ(0, cbp, "Request failed (error=%d).",
1171 G_RAID3_LOGREQ(1, cbp, "Request failed (error=%d).",
1174 if (g_raid3_disconnect_on_failure &&
1175 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1176 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1177 g_raid3_event_send(disk,
1178 G_RAID3_DISK_STATE_DISCONNECTED,
1179 G_RAID3_EVENT_DONTWAIT);
1182 if (pbp->bio_error != 0)
1184 if (fbp != NULL && (pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0) {
1185 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_VERIFY;
1187 g_raid3_replace_bio(xbp, fbp);
1188 g_raid3_destroy_bio(sc, fbp);
1189 } else if (fbp != NULL) {
1190 struct g_consumer *cp;
1193 * One request failed, so send the same request to
1194 * the parity consumer.
1196 disk = pbp->bio_driver2;
1197 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE) {
1198 pbp->bio_error = fbp->bio_error;
1201 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1203 fbp->bio_flags &= ~(BIO_DONE | BIO_ERROR);
1204 if (disk->d_no == sc->sc_ndisks - 1)
1205 fbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1207 fbp->bio_completed = 0;
1208 fbp->bio_children = 0;
1210 cp = disk->d_consumer;
1211 fbp->bio_caller2 = disk;
1212 fbp->bio_to = cp->provider;
1213 G_RAID3_LOGREQ(3, fbp, "Sending request (recover).");
1214 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1215 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1216 cp->acr, cp->acw, cp->ace));
1218 g_io_request(fbp, cp);
1225 G_RAID3_FOREACH_BIO(pbp, cbp) {
1226 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0)
1228 g_raid3_xor(cbp->bio_data, xbp->bio_data,
1231 xbp->bio_cflags &= ~G_RAID3_BIO_CFLAG_PARITY;
1232 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0) {
1233 if (!g_raid3_is_zero(xbp)) {
1234 g_raid3_parity_mismatch++;
1235 pbp->bio_error = EIO;
1238 g_raid3_destroy_bio(sc, xbp);
1241 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1243 for (left = pbp->bio_length; left > 0; left -= sc->sc_sectorsize) {
1244 G_RAID3_FOREACH_BIO(pbp, cbp) {
1245 bcopy(cbp->bio_data + cadd, pbp->bio_data + padd, atom);
1246 pbp->bio_completed += atom;
1252 if (pbp->bio_error == 0)
1253 G_RAID3_LOGREQ(3, pbp, "Request finished.");
1255 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0)
1256 G_RAID3_LOGREQ(1, pbp, "Verification error.");
1258 G_RAID3_LOGREQ(0, pbp, "Request failed.");
1260 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_MASK;
1261 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL)
1262 g_raid3_destroy_bio(sc, cbp);
1263 g_io_deliver(pbp, pbp->bio_error);
1267 g_raid3_done(struct bio *bp)
1269 struct g_raid3_softc *sc;
1271 sc = bp->bio_from->geom->softc;
1272 bp->bio_cflags |= G_RAID3_BIO_CFLAG_REGULAR;
1273 G_RAID3_LOGREQ(3, bp, "Regular request done (error=%d).", bp->bio_error);
1274 mtx_lock(&sc->sc_queue_mtx);
1275 bioq_insert_head(&sc->sc_queue, bp);
1276 mtx_unlock(&sc->sc_queue_mtx);
1278 wakeup(&sc->sc_queue);
1282 g_raid3_regular_request(struct bio *cbp)
1284 struct g_raid3_softc *sc;
1285 struct g_raid3_disk *disk;
1288 g_topology_assert_not();
1290 pbp = cbp->bio_parent;
1291 sc = pbp->bio_to->geom->softc;
1292 cbp->bio_from->index--;
1293 if (cbp->bio_cmd == BIO_WRITE)
1295 disk = cbp->bio_from->private;
1298 g_raid3_kill_consumer(sc, cbp->bio_from);
1299 g_topology_unlock();
1302 G_RAID3_LOGREQ(3, cbp, "Request finished.");
1304 KASSERT(pbp->bio_inbed <= pbp->bio_children,
1305 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
1306 pbp->bio_children));
1307 if (pbp->bio_inbed != pbp->bio_children)
1309 switch (pbp->bio_cmd) {
1311 g_raid3_gather(pbp);
1318 pbp->bio_completed = pbp->bio_length;
1319 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL) {
1320 if (cbp->bio_error == 0) {
1321 g_raid3_destroy_bio(sc, cbp);
1326 error = cbp->bio_error;
1327 else if (pbp->bio_error == 0) {
1329 * Next failed request, that's too many.
1331 pbp->bio_error = error;
1334 disk = cbp->bio_caller2;
1336 g_raid3_destroy_bio(sc, cbp);
1340 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
1341 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
1342 G_RAID3_LOGREQ(0, cbp,
1343 "Request failed (error=%d).",
1346 G_RAID3_LOGREQ(1, cbp,
1347 "Request failed (error=%d).",
1350 if (g_raid3_disconnect_on_failure &&
1351 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1352 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1353 g_raid3_event_send(disk,
1354 G_RAID3_DISK_STATE_DISCONNECTED,
1355 G_RAID3_EVENT_DONTWAIT);
1357 g_raid3_destroy_bio(sc, cbp);
1359 if (pbp->bio_error == 0)
1360 G_RAID3_LOGREQ(3, pbp, "Request finished.");
1362 G_RAID3_LOGREQ(0, pbp, "Request failed.");
1363 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_DEGRADED;
1364 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_NOPARITY;
1365 bioq_remove(&sc->sc_inflight, pbp);
1366 /* Release delayed sync requests if possible. */
1367 g_raid3_sync_release(sc);
1368 g_io_deliver(pbp, pbp->bio_error);
1375 g_raid3_sync_done(struct bio *bp)
1377 struct g_raid3_softc *sc;
1379 G_RAID3_LOGREQ(3, bp, "Synchronization request delivered.");
1380 sc = bp->bio_from->geom->softc;
1381 bp->bio_cflags |= G_RAID3_BIO_CFLAG_SYNC;
1382 mtx_lock(&sc->sc_queue_mtx);
1383 bioq_insert_head(&sc->sc_queue, bp);
1384 mtx_unlock(&sc->sc_queue_mtx);
1386 wakeup(&sc->sc_queue);
1390 g_raid3_flush(struct g_raid3_softc *sc, struct bio *bp)
1392 struct bio_queue_head queue;
1393 struct g_raid3_disk *disk;
1394 struct g_consumer *cp;
1399 for (i = 0; i < sc->sc_ndisks; i++) {
1400 disk = &sc->sc_disks[i];
1401 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
1403 cbp = g_clone_bio(bp);
1405 for (cbp = bioq_first(&queue); cbp != NULL;
1406 cbp = bioq_first(&queue)) {
1407 bioq_remove(&queue, cbp);
1410 if (bp->bio_error == 0)
1411 bp->bio_error = ENOMEM;
1412 g_io_deliver(bp, bp->bio_error);
1415 bioq_insert_tail(&queue, cbp);
1416 cbp->bio_done = g_std_done;
1417 cbp->bio_caller1 = disk;
1418 cbp->bio_to = disk->d_consumer->provider;
1420 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1421 bioq_remove(&queue, cbp);
1422 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1423 disk = cbp->bio_caller1;
1424 cbp->bio_caller1 = NULL;
1425 cp = disk->d_consumer;
1426 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1427 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1428 cp->acr, cp->acw, cp->ace));
1429 g_io_request(cbp, disk->d_consumer);
1434 g_raid3_start(struct bio *bp)
1436 struct g_raid3_softc *sc;
1438 sc = bp->bio_to->geom->softc;
1440 * If sc == NULL or there are no valid disks, provider's error
1441 * should be set and g_raid3_start() should not be called at all.
1443 KASSERT(sc != NULL && (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
1444 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE),
1445 ("Provider's error should be set (error=%d)(device=%s).",
1446 bp->bio_to->error, bp->bio_to->name));
1447 G_RAID3_LOGREQ(3, bp, "Request received.");
1449 switch (bp->bio_cmd) {
1455 g_raid3_flush(sc, bp);
1459 g_io_deliver(bp, EOPNOTSUPP);
1462 mtx_lock(&sc->sc_queue_mtx);
1463 bioq_insert_tail(&sc->sc_queue, bp);
1464 mtx_unlock(&sc->sc_queue_mtx);
1465 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1470 * Return TRUE if the given request is colliding with a in-progress
1471 * synchronization request.
1474 g_raid3_sync_collision(struct g_raid3_softc *sc, struct bio *bp)
1476 struct g_raid3_disk *disk;
1478 off_t rstart, rend, sstart, send;
1481 disk = sc->sc_syncdisk;
1484 rstart = bp->bio_offset;
1485 rend = bp->bio_offset + bp->bio_length;
1486 for (i = 0; i < g_raid3_syncreqs; i++) {
1487 sbp = disk->d_sync.ds_bios[i];
1490 sstart = sbp->bio_offset;
1491 send = sbp->bio_length;
1492 if (sbp->bio_cmd == BIO_WRITE) {
1493 sstart *= sc->sc_ndisks - 1;
1494 send *= sc->sc_ndisks - 1;
1497 if (rend > sstart && rstart < send)
1504 * Return TRUE if the given sync request is colliding with a in-progress regular
1508 g_raid3_regular_collision(struct g_raid3_softc *sc, struct bio *sbp)
1510 off_t rstart, rend, sstart, send;
1513 if (sc->sc_syncdisk == NULL)
1515 sstart = sbp->bio_offset;
1516 send = sstart + sbp->bio_length;
1517 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1518 rstart = bp->bio_offset;
1519 rend = bp->bio_offset + bp->bio_length;
1520 if (rend > sstart && rstart < send)
1527 * Puts request onto delayed queue.
1530 g_raid3_regular_delay(struct g_raid3_softc *sc, struct bio *bp)
1533 G_RAID3_LOGREQ(2, bp, "Delaying request.");
1534 bioq_insert_head(&sc->sc_regular_delayed, bp);
1538 * Puts synchronization request onto delayed queue.
1541 g_raid3_sync_delay(struct g_raid3_softc *sc, struct bio *bp)
1544 G_RAID3_LOGREQ(2, bp, "Delaying synchronization request.");
1545 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1549 * Releases delayed regular requests which don't collide anymore with sync
1553 g_raid3_regular_release(struct g_raid3_softc *sc)
1555 struct bio *bp, *bp2;
1557 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1558 if (g_raid3_sync_collision(sc, bp))
1560 bioq_remove(&sc->sc_regular_delayed, bp);
1561 G_RAID3_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1562 mtx_lock(&sc->sc_queue_mtx);
1563 bioq_insert_head(&sc->sc_queue, bp);
1566 * wakeup() is not needed, because this function is called from
1567 * the worker thread.
1569 wakeup(&sc->sc_queue);
1571 mtx_unlock(&sc->sc_queue_mtx);
1576 * Releases delayed sync requests which don't collide anymore with regular
1580 g_raid3_sync_release(struct g_raid3_softc *sc)
1582 struct bio *bp, *bp2;
1584 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1585 if (g_raid3_regular_collision(sc, bp))
1587 bioq_remove(&sc->sc_sync_delayed, bp);
1588 G_RAID3_LOGREQ(2, bp,
1589 "Releasing delayed synchronization request.");
1590 g_io_request(bp, bp->bio_from);
1595 * Handle synchronization requests.
1596 * Every synchronization request is two-steps process: first, READ request is
1597 * send to active provider and then WRITE request (with read data) to the provider
1598 * beeing synchronized. When WRITE is finished, new synchronization request is
1602 g_raid3_sync_request(struct bio *bp)
1604 struct g_raid3_softc *sc;
1605 struct g_raid3_disk *disk;
1607 bp->bio_from->index--;
1608 sc = bp->bio_from->geom->softc;
1609 disk = bp->bio_from->private;
1611 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1613 g_raid3_kill_consumer(sc, bp->bio_from);
1614 g_topology_unlock();
1615 free(bp->bio_data, M_RAID3);
1617 sx_xlock(&sc->sc_lock);
1622 * Synchronization request.
1624 switch (bp->bio_cmd) {
1627 struct g_consumer *cp;
1632 if (bp->bio_error != 0) {
1633 G_RAID3_LOGREQ(0, bp,
1634 "Synchronization request failed (error=%d).",
1639 G_RAID3_LOGREQ(3, bp, "Synchronization request finished.");
1640 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1641 dst = src = bp->bio_data;
1642 if (disk->d_no == sc->sc_ndisks - 1) {
1645 /* Parity component. */
1646 for (left = bp->bio_length; left > 0;
1647 left -= sc->sc_sectorsize) {
1648 bcopy(src, dst, atom);
1650 for (n = 1; n < sc->sc_ndisks - 1; n++) {
1651 g_raid3_xor(src, dst, atom);
1657 /* Regular component. */
1658 src += atom * disk->d_no;
1659 for (left = bp->bio_length; left > 0;
1660 left -= sc->sc_sectorsize) {
1661 bcopy(src, dst, atom);
1662 src += sc->sc_sectorsize;
1666 bp->bio_driver1 = bp->bio_driver2 = NULL;
1668 bp->bio_offset /= sc->sc_ndisks - 1;
1669 bp->bio_length /= sc->sc_ndisks - 1;
1670 bp->bio_cmd = BIO_WRITE;
1672 bp->bio_children = bp->bio_inbed = 0;
1673 cp = disk->d_consumer;
1674 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1675 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1676 cp->acr, cp->acw, cp->ace));
1678 g_io_request(bp, cp);
1683 struct g_raid3_disk_sync *sync;
1684 off_t boffset, moffset;
1688 if (bp->bio_error != 0) {
1689 G_RAID3_LOGREQ(0, bp,
1690 "Synchronization request failed (error=%d).",
1693 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1694 g_raid3_event_send(disk,
1695 G_RAID3_DISK_STATE_DISCONNECTED,
1696 G_RAID3_EVENT_DONTWAIT);
1699 G_RAID3_LOGREQ(3, bp, "Synchronization request finished.");
1700 sync = &disk->d_sync;
1701 if (sync->ds_offset == sc->sc_mediasize / (sc->sc_ndisks - 1) ||
1702 sync->ds_consumer == NULL ||
1703 (sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
1704 /* Don't send more synchronization requests. */
1705 sync->ds_inflight--;
1706 if (sync->ds_bios != NULL) {
1707 i = (int)(uintptr_t)bp->bio_caller1;
1708 sync->ds_bios[i] = NULL;
1710 free(bp->bio_data, M_RAID3);
1712 if (sync->ds_inflight > 0)
1714 if (sync->ds_consumer == NULL ||
1715 (sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
1719 * Disk up-to-date, activate it.
1721 g_raid3_event_send(disk, G_RAID3_DISK_STATE_ACTIVE,
1722 G_RAID3_EVENT_DONTWAIT);
1726 /* Send next synchronization request. */
1727 data = bp->bio_data;
1728 bzero(bp, sizeof(*bp));
1729 bp->bio_cmd = BIO_READ;
1730 bp->bio_offset = sync->ds_offset * (sc->sc_ndisks - 1);
1731 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1732 sync->ds_offset += bp->bio_length / (sc->sc_ndisks - 1);
1733 bp->bio_done = g_raid3_sync_done;
1734 bp->bio_data = data;
1735 bp->bio_from = sync->ds_consumer;
1736 bp->bio_to = sc->sc_provider;
1737 G_RAID3_LOGREQ(3, bp, "Sending synchronization request.");
1738 sync->ds_consumer->index++;
1740 * Delay the request if it is colliding with a regular request.
1742 if (g_raid3_regular_collision(sc, bp))
1743 g_raid3_sync_delay(sc, bp);
1745 g_io_request(bp, sync->ds_consumer);
1747 /* Release delayed requests if possible. */
1748 g_raid3_regular_release(sc);
1750 /* Find the smallest offset. */
1751 moffset = sc->sc_mediasize;
1752 for (i = 0; i < g_raid3_syncreqs; i++) {
1753 bp = sync->ds_bios[i];
1754 boffset = bp->bio_offset;
1755 if (bp->bio_cmd == BIO_WRITE)
1756 boffset *= sc->sc_ndisks - 1;
1757 if (boffset < moffset)
1760 if (sync->ds_offset_done + (MAXPHYS * 100) < moffset) {
1761 /* Update offset_done on every 100 blocks. */
1762 sync->ds_offset_done = moffset;
1763 g_raid3_update_metadata(disk);
1768 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1769 bp->bio_cmd, sc->sc_name));
1775 g_raid3_register_request(struct bio *pbp)
1777 struct g_raid3_softc *sc;
1778 struct g_raid3_disk *disk;
1779 struct g_consumer *cp;
1780 struct bio *cbp, *tmpbp;
1781 off_t offset, length;
1783 int round_robin, verify;
1786 sc = pbp->bio_to->geom->softc;
1787 if ((pbp->bio_cflags & G_RAID3_BIO_CFLAG_REGSYNC) != 0 &&
1788 sc->sc_syncdisk == NULL) {
1789 g_io_deliver(pbp, EIO);
1792 g_raid3_init_bio(pbp);
1793 length = pbp->bio_length / (sc->sc_ndisks - 1);
1794 offset = pbp->bio_offset / (sc->sc_ndisks - 1);
1795 round_robin = verify = 0;
1796 switch (pbp->bio_cmd) {
1798 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
1799 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1800 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_VERIFY;
1802 ndisks = sc->sc_ndisks;
1805 ndisks = sc->sc_ndisks - 1;
1807 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0 &&
1808 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1813 KASSERT(!round_robin || !verify,
1814 ("ROUND-ROBIN and VERIFY are mutually exclusive."));
1815 pbp->bio_driver2 = &sc->sc_disks[sc->sc_ndisks - 1];
1820 * Delay the request if it is colliding with a synchronization
1823 if (g_raid3_sync_collision(sc, pbp)) {
1824 g_raid3_regular_delay(sc, pbp);
1831 sc->sc_last_write = time_uptime;
1833 ndisks = sc->sc_ndisks;
1836 for (n = 0; n < ndisks; n++) {
1837 disk = &sc->sc_disks[n];
1838 cbp = g_raid3_clone_bio(sc, pbp);
1840 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL)
1841 g_raid3_destroy_bio(sc, cbp);
1843 * To prevent deadlock, we must run back up
1844 * with the ENOMEM for failed requests of any
1845 * of our consumers. Our own sync requests
1846 * can stick around, as they are finite.
1848 if ((pbp->bio_cflags &
1849 G_RAID3_BIO_CFLAG_REGULAR) != 0) {
1850 g_io_deliver(pbp, ENOMEM);
1855 cbp->bio_offset = offset;
1856 cbp->bio_length = length;
1857 cbp->bio_done = g_raid3_done;
1858 switch (pbp->bio_cmd) {
1860 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE) {
1862 * Replace invalid component with the parity
1865 disk = &sc->sc_disks[sc->sc_ndisks - 1];
1866 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1867 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1868 } else if (round_robin &&
1869 disk->d_no == sc->sc_round_robin) {
1871 * In round-robin mode skip one data component
1872 * and use parity component when reading.
1874 pbp->bio_driver2 = disk;
1875 disk = &sc->sc_disks[sc->sc_ndisks - 1];
1876 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1877 sc->sc_round_robin++;
1879 } else if (verify && disk->d_no == sc->sc_ndisks - 1) {
1880 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1885 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
1886 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
1887 if (n == ndisks - 1) {
1889 * Active parity component, mark it as such.
1892 G_RAID3_BIO_CFLAG_PARITY;
1895 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1896 if (n == ndisks - 1) {
1898 * Parity component is not connected,
1899 * so destroy its request.
1902 G_RAID3_BIO_PFLAG_NOPARITY;
1903 g_raid3_destroy_bio(sc, cbp);
1907 G_RAID3_BIO_CFLAG_NODISK;
1914 cbp->bio_caller2 = disk;
1916 switch (pbp->bio_cmd) {
1920 * If we are in round-robin mode and 'round_robin' is
1921 * still 1, it means, that we skipped parity component
1922 * for this read and must reset sc_round_robin field.
1924 sc->sc_round_robin = 0;
1926 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1927 disk = cbp->bio_caller2;
1928 cp = disk->d_consumer;
1929 cbp->bio_to = cp->provider;
1930 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1931 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1932 ("Consumer %s not opened (r%dw%de%d).",
1933 cp->provider->name, cp->acr, cp->acw, cp->ace));
1935 g_io_request(cbp, cp);
1941 * Put request onto inflight queue, so we can check if new
1942 * synchronization requests don't collide with it.
1944 bioq_insert_tail(&sc->sc_inflight, pbp);
1947 * Bump syncid on first write.
1949 if ((sc->sc_bump_id & G_RAID3_BUMP_SYNCID) != 0) {
1950 sc->sc_bump_id &= ~G_RAID3_BUMP_SYNCID;
1951 g_raid3_bump_syncid(sc);
1953 g_raid3_scatter(pbp);
1960 g_raid3_can_destroy(struct g_raid3_softc *sc)
1963 struct g_consumer *cp;
1965 g_topology_assert();
1967 if (gp->softc == NULL)
1969 LIST_FOREACH(cp, &gp->consumer, consumer) {
1970 if (g_raid3_is_busy(sc, cp))
1973 gp = sc->sc_sync.ds_geom;
1974 LIST_FOREACH(cp, &gp->consumer, consumer) {
1975 if (g_raid3_is_busy(sc, cp))
1978 G_RAID3_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1984 g_raid3_try_destroy(struct g_raid3_softc *sc)
1987 g_topology_assert_not();
1988 sx_assert(&sc->sc_lock, SX_XLOCKED);
1990 if (sc->sc_rootmount != NULL) {
1991 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1993 root_mount_rel(sc->sc_rootmount);
1994 sc->sc_rootmount = NULL;
1998 if (!g_raid3_can_destroy(sc)) {
1999 g_topology_unlock();
2002 sc->sc_geom->softc = NULL;
2003 sc->sc_sync.ds_geom->softc = NULL;
2004 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_WAIT) != 0) {
2005 g_topology_unlock();
2006 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__,
2008 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
2009 sx_xunlock(&sc->sc_lock);
2010 wakeup(&sc->sc_worker);
2011 sc->sc_worker = NULL;
2013 g_topology_unlock();
2014 g_raid3_destroy_device(sc);
2015 free(sc->sc_disks, M_RAID3);
2025 g_raid3_worker(void *arg)
2027 struct g_raid3_softc *sc;
2028 struct g_raid3_event *ep;
2033 thread_lock(curthread);
2034 sched_prio(curthread, PRIBIO);
2035 thread_unlock(curthread);
2037 sx_xlock(&sc->sc_lock);
2039 G_RAID3_DEBUG(5, "%s: Let's see...", __func__);
2041 * First take a look at events.
2042 * This is important to handle events before any I/O requests.
2044 ep = g_raid3_event_get(sc);
2046 g_raid3_event_remove(sc, ep);
2047 if ((ep->e_flags & G_RAID3_EVENT_DEVICE) != 0) {
2048 /* Update only device status. */
2050 "Running event for device %s.",
2053 g_raid3_update_device(sc, 1);
2055 /* Update disk status. */
2056 G_RAID3_DEBUG(3, "Running event for disk %s.",
2057 g_raid3_get_diskname(ep->e_disk));
2058 ep->e_error = g_raid3_update_disk(ep->e_disk,
2060 if (ep->e_error == 0)
2061 g_raid3_update_device(sc, 0);
2063 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0) {
2064 KASSERT(ep->e_error == 0,
2065 ("Error cannot be handled."));
2066 g_raid3_event_free(ep);
2068 ep->e_flags |= G_RAID3_EVENT_DONE;
2069 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__,
2071 mtx_lock(&sc->sc_events_mtx);
2073 mtx_unlock(&sc->sc_events_mtx);
2076 G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
2077 if (g_raid3_try_destroy(sc)) {
2078 curthread->td_pflags &= ~TDP_GEOM;
2079 G_RAID3_DEBUG(1, "Thread exiting.");
2083 G_RAID3_DEBUG(5, "%s: I'm here 1.", __func__);
2087 * Check if we can mark array as CLEAN and if we can't take
2088 * how much seconds should we wait.
2090 timeout = g_raid3_idle(sc, -1);
2094 /* Get first request from the queue. */
2095 mtx_lock(&sc->sc_queue_mtx);
2096 bp = bioq_first(&sc->sc_queue);
2099 G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
2100 mtx_unlock(&sc->sc_queue_mtx);
2101 if (g_raid3_try_destroy(sc)) {
2102 curthread->td_pflags &= ~TDP_GEOM;
2103 G_RAID3_DEBUG(1, "Thread exiting.");
2106 mtx_lock(&sc->sc_queue_mtx);
2108 sx_xunlock(&sc->sc_lock);
2110 * XXX: We can miss an event here, because an event
2111 * can be added without sx-device-lock and without
2112 * mtx-queue-lock. Maybe I should just stop using
2113 * dedicated mutex for events synchronization and
2114 * stick with the queue lock?
2115 * The event will hang here until next I/O request
2116 * or next event is received.
2118 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "r3:w1",
2120 sx_xlock(&sc->sc_lock);
2121 G_RAID3_DEBUG(5, "%s: I'm here 4.", __func__);
2125 bioq_remove(&sc->sc_queue, bp);
2126 mtx_unlock(&sc->sc_queue_mtx);
2128 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
2129 (bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC) != 0) {
2130 g_raid3_sync_request(bp); /* READ */
2131 } else if (bp->bio_to != sc->sc_provider) {
2132 if ((bp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR) != 0)
2133 g_raid3_regular_request(bp);
2134 else if ((bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC) != 0)
2135 g_raid3_sync_request(bp); /* WRITE */
2138 ("Invalid request cflags=0x%hhx to=%s.",
2139 bp->bio_cflags, bp->bio_to->name));
2141 } else if (g_raid3_register_request(bp) != 0) {
2142 mtx_lock(&sc->sc_queue_mtx);
2143 bioq_insert_head(&sc->sc_queue, bp);
2145 * We are short in memory, let see if there are finished
2146 * request we can free.
2148 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
2149 if (bp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR)
2153 * No finished regular request, so at least keep
2154 * synchronization running.
2156 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
2157 if (bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC)
2160 sx_xunlock(&sc->sc_lock);
2161 MSLEEP(&sc->sc_queue, &sc->sc_queue_mtx, PRIBIO | PDROP,
2162 "r3:lowmem", hz / 10);
2163 sx_xlock(&sc->sc_lock);
2165 G_RAID3_DEBUG(5, "%s: I'm here 9.", __func__);
2170 g_raid3_update_idle(struct g_raid3_softc *sc, struct g_raid3_disk *disk)
2173 sx_assert(&sc->sc_lock, SX_LOCKED);
2174 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
2176 if (!sc->sc_idle && (disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) == 0) {
2177 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as dirty.",
2178 g_raid3_get_diskname(disk), sc->sc_name);
2179 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
2180 } else if (sc->sc_idle &&
2181 (disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) != 0) {
2182 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as clean.",
2183 g_raid3_get_diskname(disk), sc->sc_name);
2184 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2189 g_raid3_sync_start(struct g_raid3_softc *sc)
2191 struct g_raid3_disk *disk;
2192 struct g_consumer *cp;
2197 g_topology_assert_not();
2198 sx_assert(&sc->sc_lock, SX_XLOCKED);
2200 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED,
2201 ("Device not in DEGRADED state (%s, %u).", sc->sc_name,
2203 KASSERT(sc->sc_syncdisk == NULL, ("Syncdisk is not NULL (%s, %u).",
2204 sc->sc_name, sc->sc_state));
2206 for (n = 0; n < sc->sc_ndisks; n++) {
2207 if (sc->sc_disks[n].d_state != G_RAID3_DISK_STATE_SYNCHRONIZING)
2209 disk = &sc->sc_disks[n];
2215 sx_xunlock(&sc->sc_lock);
2217 cp = g_new_consumer(sc->sc_sync.ds_geom);
2218 error = g_attach(cp, sc->sc_provider);
2220 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
2221 error = g_access(cp, 1, 0, 0);
2222 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
2223 g_topology_unlock();
2224 sx_xlock(&sc->sc_lock);
2226 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
2227 g_raid3_get_diskname(disk));
2228 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) == 0)
2229 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
2230 KASSERT(disk->d_sync.ds_consumer == NULL,
2231 ("Sync consumer already exists (device=%s, disk=%s).",
2232 sc->sc_name, g_raid3_get_diskname(disk)));
2234 disk->d_sync.ds_consumer = cp;
2235 disk->d_sync.ds_consumer->private = disk;
2236 disk->d_sync.ds_consumer->index = 0;
2237 sc->sc_syncdisk = disk;
2240 * Allocate memory for synchronization bios and initialize them.
2242 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_raid3_syncreqs,
2244 for (n = 0; n < g_raid3_syncreqs; n++) {
2246 disk->d_sync.ds_bios[n] = bp;
2247 bp->bio_parent = NULL;
2248 bp->bio_cmd = BIO_READ;
2249 bp->bio_data = malloc(MAXPHYS, M_RAID3, M_WAITOK);
2251 bp->bio_offset = disk->d_sync.ds_offset * (sc->sc_ndisks - 1);
2252 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
2253 disk->d_sync.ds_offset += bp->bio_length / (sc->sc_ndisks - 1);
2254 bp->bio_done = g_raid3_sync_done;
2255 bp->bio_from = disk->d_sync.ds_consumer;
2256 bp->bio_to = sc->sc_provider;
2257 bp->bio_caller1 = (void *)(uintptr_t)n;
2260 /* Set the number of in-flight synchronization requests. */
2261 disk->d_sync.ds_inflight = g_raid3_syncreqs;
2264 * Fire off first synchronization requests.
2266 for (n = 0; n < g_raid3_syncreqs; n++) {
2267 bp = disk->d_sync.ds_bios[n];
2268 G_RAID3_LOGREQ(3, bp, "Sending synchronization request.");
2269 disk->d_sync.ds_consumer->index++;
2271 * Delay the request if it is colliding with a regular request.
2273 if (g_raid3_regular_collision(sc, bp))
2274 g_raid3_sync_delay(sc, bp);
2276 g_io_request(bp, disk->d_sync.ds_consumer);
2281 * Stop synchronization process.
2282 * type: 0 - synchronization finished
2283 * 1 - synchronization stopped
2286 g_raid3_sync_stop(struct g_raid3_softc *sc, int type)
2288 struct g_raid3_disk *disk;
2289 struct g_consumer *cp;
2291 g_topology_assert_not();
2292 sx_assert(&sc->sc_lock, SX_LOCKED);
2294 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED,
2295 ("Device not in DEGRADED state (%s, %u).", sc->sc_name,
2297 disk = sc->sc_syncdisk;
2298 sc->sc_syncdisk = NULL;
2299 KASSERT(disk != NULL, ("No disk was synchronized (%s).", sc->sc_name));
2300 KASSERT(disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2301 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2302 g_raid3_disk_state2str(disk->d_state)));
2303 if (disk->d_sync.ds_consumer == NULL)
2307 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2308 sc->sc_name, g_raid3_get_diskname(disk));
2309 } else /* if (type == 1) */ {
2310 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2311 sc->sc_name, g_raid3_get_diskname(disk));
2313 free(disk->d_sync.ds_bios, M_RAID3);
2314 disk->d_sync.ds_bios = NULL;
2315 cp = disk->d_sync.ds_consumer;
2316 disk->d_sync.ds_consumer = NULL;
2317 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2318 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2320 g_raid3_kill_consumer(sc, cp);
2321 g_topology_unlock();
2322 sx_xlock(&sc->sc_lock);
2326 g_raid3_launch_provider(struct g_raid3_softc *sc)
2328 struct g_provider *pp;
2329 struct g_raid3_disk *disk;
2332 sx_assert(&sc->sc_lock, SX_LOCKED);
2335 pp = g_new_providerf(sc->sc_geom, "raid3/%s", sc->sc_name);
2336 pp->mediasize = sc->sc_mediasize;
2337 pp->sectorsize = sc->sc_sectorsize;
2339 pp->stripeoffset = 0;
2340 for (n = 0; n < sc->sc_ndisks; n++) {
2341 disk = &sc->sc_disks[n];
2342 if (disk->d_consumer && disk->d_consumer->provider &&
2343 disk->d_consumer->provider->stripesize > pp->stripesize) {
2344 pp->stripesize = disk->d_consumer->provider->stripesize;
2345 pp->stripeoffset = disk->d_consumer->provider->stripeoffset;
2348 pp->stripesize *= sc->sc_ndisks - 1;
2349 pp->stripeoffset *= sc->sc_ndisks - 1;
2350 sc->sc_provider = pp;
2351 g_error_provider(pp, 0);
2352 g_topology_unlock();
2353 G_RAID3_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2354 g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE), sc->sc_ndisks);
2356 if (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED)
2357 g_raid3_sync_start(sc);
2361 g_raid3_destroy_provider(struct g_raid3_softc *sc)
2365 g_topology_assert_not();
2366 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2370 g_error_provider(sc->sc_provider, ENXIO);
2371 mtx_lock(&sc->sc_queue_mtx);
2372 while ((bp = bioq_first(&sc->sc_queue)) != NULL) {
2373 bioq_remove(&sc->sc_queue, bp);
2374 g_io_deliver(bp, ENXIO);
2376 mtx_unlock(&sc->sc_queue_mtx);
2377 G_RAID3_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2378 sc->sc_provider->name);
2379 sc->sc_provider->flags |= G_PF_WITHER;
2380 g_orphan_provider(sc->sc_provider, ENXIO);
2381 g_topology_unlock();
2382 sc->sc_provider = NULL;
2383 if (sc->sc_syncdisk != NULL)
2384 g_raid3_sync_stop(sc, 1);
2388 g_raid3_go(void *arg)
2390 struct g_raid3_softc *sc;
2393 G_RAID3_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2394 g_raid3_event_send(sc, 0,
2395 G_RAID3_EVENT_DONTWAIT | G_RAID3_EVENT_DEVICE);
2399 g_raid3_determine_state(struct g_raid3_disk *disk)
2401 struct g_raid3_softc *sc;
2405 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2406 if ((disk->d_flags &
2407 G_RAID3_DISK_FLAG_SYNCHRONIZING) == 0) {
2408 /* Disk does not need synchronization. */
2409 state = G_RAID3_DISK_STATE_ACTIVE;
2412 G_RAID3_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2414 G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) {
2416 * We can start synchronization from
2417 * the stored offset.
2419 state = G_RAID3_DISK_STATE_SYNCHRONIZING;
2421 state = G_RAID3_DISK_STATE_STALE;
2424 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2426 * Reset all synchronization data for this disk,
2427 * because if it even was synchronized, it was
2428 * synchronized to disks with different syncid.
2430 disk->d_flags |= G_RAID3_DISK_FLAG_SYNCHRONIZING;
2431 disk->d_sync.ds_offset = 0;
2432 disk->d_sync.ds_offset_done = 0;
2433 disk->d_sync.ds_syncid = sc->sc_syncid;
2434 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2435 (disk->d_flags & G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) {
2436 state = G_RAID3_DISK_STATE_SYNCHRONIZING;
2438 state = G_RAID3_DISK_STATE_STALE;
2440 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2442 * Not good, NOT GOOD!
2443 * It means that device was started on stale disks
2444 * and more fresh disk just arrive.
2445 * If there were writes, device is broken, sorry.
2446 * I think the best choice here is don't touch
2447 * this disk and inform the user loudly.
2449 G_RAID3_DEBUG(0, "Device %s was started before the freshest "
2450 "disk (%s) arrives!! It will not be connected to the "
2451 "running device.", sc->sc_name,
2452 g_raid3_get_diskname(disk));
2453 g_raid3_destroy_disk(disk);
2454 state = G_RAID3_DISK_STATE_NONE;
2455 /* Return immediately, because disk was destroyed. */
2458 G_RAID3_DEBUG(3, "State for %s disk: %s.",
2459 g_raid3_get_diskname(disk), g_raid3_disk_state2str(state));
2464 * Update device state.
2467 g_raid3_update_device(struct g_raid3_softc *sc, boolean_t force)
2469 struct g_raid3_disk *disk;
2472 sx_assert(&sc->sc_lock, SX_XLOCKED);
2474 switch (sc->sc_state) {
2475 case G_RAID3_DEVICE_STATE_STARTING:
2477 u_int n, ndirty, ndisks, genid, syncid;
2479 KASSERT(sc->sc_provider == NULL,
2480 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2482 * Are we ready? We are, if all disks are connected or
2483 * one disk is missing and 'force' is true.
2485 if (g_raid3_ndisks(sc, -1) + force == sc->sc_ndisks) {
2487 callout_drain(&sc->sc_callout);
2491 * Timeout expired, so destroy device.
2493 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2494 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p",
2495 __LINE__, sc->sc_rootmount);
2496 root_mount_rel(sc->sc_rootmount);
2497 sc->sc_rootmount = NULL;
2503 * Find the biggest genid.
2506 for (n = 0; n < sc->sc_ndisks; n++) {
2507 disk = &sc->sc_disks[n];
2508 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2510 if (disk->d_genid > genid)
2511 genid = disk->d_genid;
2513 sc->sc_genid = genid;
2515 * Remove all disks without the biggest genid.
2517 for (n = 0; n < sc->sc_ndisks; n++) {
2518 disk = &sc->sc_disks[n];
2519 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2521 if (disk->d_genid < genid) {
2523 "Component %s (device %s) broken, skipping.",
2524 g_raid3_get_diskname(disk), sc->sc_name);
2525 g_raid3_destroy_disk(disk);
2530 * There must be at least 'sc->sc_ndisks - 1' components
2531 * with the same syncid and without SYNCHRONIZING flag.
2535 * Find the biggest syncid, number of valid components and
2536 * number of dirty components.
2538 ndirty = ndisks = syncid = 0;
2539 for (n = 0; n < sc->sc_ndisks; n++) {
2540 disk = &sc->sc_disks[n];
2541 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2543 if ((disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) != 0)
2545 if (disk->d_sync.ds_syncid > syncid) {
2546 syncid = disk->d_sync.ds_syncid;
2548 } else if (disk->d_sync.ds_syncid < syncid) {
2551 if ((disk->d_flags &
2552 G_RAID3_DISK_FLAG_SYNCHRONIZING) != 0) {
2558 * Do we have enough valid components?
2560 if (ndisks + 1 < sc->sc_ndisks) {
2562 "Device %s is broken, too few valid components.",
2564 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2568 * If there is one DIRTY component and all disks are present,
2569 * mark it for synchronization. If there is more than one DIRTY
2570 * component, mark parity component for synchronization.
2572 if (ndisks == sc->sc_ndisks && ndirty == 1) {
2573 for (n = 0; n < sc->sc_ndisks; n++) {
2574 disk = &sc->sc_disks[n];
2575 if ((disk->d_flags &
2576 G_RAID3_DISK_FLAG_DIRTY) == 0) {
2580 G_RAID3_DISK_FLAG_SYNCHRONIZING;
2582 } else if (ndisks == sc->sc_ndisks && ndirty > 1) {
2583 disk = &sc->sc_disks[sc->sc_ndisks - 1];
2584 disk->d_flags |= G_RAID3_DISK_FLAG_SYNCHRONIZING;
2587 sc->sc_syncid = syncid;
2589 /* Remember to bump syncid on first write. */
2590 sc->sc_bump_id |= G_RAID3_BUMP_SYNCID;
2592 if (ndisks == sc->sc_ndisks)
2593 state = G_RAID3_DEVICE_STATE_COMPLETE;
2594 else /* if (ndisks == sc->sc_ndisks - 1) */
2595 state = G_RAID3_DEVICE_STATE_DEGRADED;
2596 G_RAID3_DEBUG(1, "Device %s state changed from %s to %s.",
2597 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2598 g_raid3_device_state2str(state));
2599 sc->sc_state = state;
2600 for (n = 0; n < sc->sc_ndisks; n++) {
2601 disk = &sc->sc_disks[n];
2602 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2604 state = g_raid3_determine_state(disk);
2605 g_raid3_event_send(disk, state, G_RAID3_EVENT_DONTWAIT);
2606 if (state == G_RAID3_DISK_STATE_STALE)
2607 sc->sc_bump_id |= G_RAID3_BUMP_SYNCID;
2611 case G_RAID3_DEVICE_STATE_DEGRADED:
2613 * Genid need to be bumped immediately, so do it here.
2615 if ((sc->sc_bump_id & G_RAID3_BUMP_GENID) != 0) {
2616 sc->sc_bump_id &= ~G_RAID3_BUMP_GENID;
2617 g_raid3_bump_genid(sc);
2620 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NEW) > 0)
2622 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) <
2623 sc->sc_ndisks - 1) {
2624 if (sc->sc_provider != NULL)
2625 g_raid3_destroy_provider(sc);
2626 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2629 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) ==
2631 state = G_RAID3_DEVICE_STATE_COMPLETE;
2633 "Device %s state changed from %s to %s.",
2634 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2635 g_raid3_device_state2str(state));
2636 sc->sc_state = state;
2638 if (sc->sc_provider == NULL)
2639 g_raid3_launch_provider(sc);
2640 if (sc->sc_rootmount != NULL) {
2641 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2643 root_mount_rel(sc->sc_rootmount);
2644 sc->sc_rootmount = NULL;
2647 case G_RAID3_DEVICE_STATE_COMPLETE:
2649 * Genid need to be bumped immediately, so do it here.
2651 if ((sc->sc_bump_id & G_RAID3_BUMP_GENID) != 0) {
2652 sc->sc_bump_id &= ~G_RAID3_BUMP_GENID;
2653 g_raid3_bump_genid(sc);
2656 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NEW) > 0)
2658 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) >=
2660 ("Too few ACTIVE components in COMPLETE state (device %s).",
2662 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) ==
2663 sc->sc_ndisks - 1) {
2664 state = G_RAID3_DEVICE_STATE_DEGRADED;
2666 "Device %s state changed from %s to %s.",
2667 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2668 g_raid3_device_state2str(state));
2669 sc->sc_state = state;
2671 if (sc->sc_provider == NULL)
2672 g_raid3_launch_provider(sc);
2673 if (sc->sc_rootmount != NULL) {
2674 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2676 root_mount_rel(sc->sc_rootmount);
2677 sc->sc_rootmount = NULL;
2681 KASSERT(1 == 0, ("Wrong device state (%s, %s).", sc->sc_name,
2682 g_raid3_device_state2str(sc->sc_state)));
2688 * Update disk state and device state if needed.
2690 #define DISK_STATE_CHANGED() G_RAID3_DEBUG(1, \
2691 "Disk %s state changed from %s to %s (device %s).", \
2692 g_raid3_get_diskname(disk), \
2693 g_raid3_disk_state2str(disk->d_state), \
2694 g_raid3_disk_state2str(state), sc->sc_name)
2696 g_raid3_update_disk(struct g_raid3_disk *disk, u_int state)
2698 struct g_raid3_softc *sc;
2701 sx_assert(&sc->sc_lock, SX_XLOCKED);
2704 G_RAID3_DEBUG(3, "Changing disk %s state from %s to %s.",
2705 g_raid3_get_diskname(disk), g_raid3_disk_state2str(disk->d_state),
2706 g_raid3_disk_state2str(state));
2708 case G_RAID3_DISK_STATE_NEW:
2710 * Possible scenarios:
2711 * 1. New disk arrive.
2713 /* Previous state should be NONE. */
2714 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NONE,
2715 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2716 g_raid3_disk_state2str(disk->d_state)));
2717 DISK_STATE_CHANGED();
2719 disk->d_state = state;
2720 G_RAID3_DEBUG(1, "Device %s: provider %s detected.",
2721 sc->sc_name, g_raid3_get_diskname(disk));
2722 if (sc->sc_state == G_RAID3_DEVICE_STATE_STARTING)
2724 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2725 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2726 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2727 g_raid3_device_state2str(sc->sc_state),
2728 g_raid3_get_diskname(disk),
2729 g_raid3_disk_state2str(disk->d_state)));
2730 state = g_raid3_determine_state(disk);
2731 if (state != G_RAID3_DISK_STATE_NONE)
2734 case G_RAID3_DISK_STATE_ACTIVE:
2736 * Possible scenarios:
2737 * 1. New disk does not need synchronization.
2738 * 2. Synchronization process finished successfully.
2740 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2741 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2742 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2743 g_raid3_device_state2str(sc->sc_state),
2744 g_raid3_get_diskname(disk),
2745 g_raid3_disk_state2str(disk->d_state)));
2746 /* Previous state should be NEW or SYNCHRONIZING. */
2747 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW ||
2748 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2749 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2750 g_raid3_disk_state2str(disk->d_state)));
2751 DISK_STATE_CHANGED();
2753 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
2754 disk->d_flags &= ~G_RAID3_DISK_FLAG_SYNCHRONIZING;
2755 disk->d_flags &= ~G_RAID3_DISK_FLAG_FORCE_SYNC;
2756 g_raid3_sync_stop(sc, 0);
2758 disk->d_state = state;
2759 disk->d_sync.ds_offset = 0;
2760 disk->d_sync.ds_offset_done = 0;
2761 g_raid3_update_idle(sc, disk);
2762 g_raid3_update_metadata(disk);
2763 G_RAID3_DEBUG(1, "Device %s: provider %s activated.",
2764 sc->sc_name, g_raid3_get_diskname(disk));
2766 case G_RAID3_DISK_STATE_STALE:
2768 * Possible scenarios:
2769 * 1. Stale disk was connected.
2771 /* Previous state should be NEW. */
2772 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2773 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2774 g_raid3_disk_state2str(disk->d_state)));
2775 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2776 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2777 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2778 g_raid3_device_state2str(sc->sc_state),
2779 g_raid3_get_diskname(disk),
2780 g_raid3_disk_state2str(disk->d_state)));
2782 * STALE state is only possible if device is marked
2785 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0,
2786 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2787 g_raid3_device_state2str(sc->sc_state),
2788 g_raid3_get_diskname(disk),
2789 g_raid3_disk_state2str(disk->d_state)));
2790 DISK_STATE_CHANGED();
2792 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2793 disk->d_state = state;
2794 g_raid3_update_metadata(disk);
2795 G_RAID3_DEBUG(0, "Device %s: provider %s is stale.",
2796 sc->sc_name, g_raid3_get_diskname(disk));
2798 case G_RAID3_DISK_STATE_SYNCHRONIZING:
2800 * Possible scenarios:
2801 * 1. Disk which needs synchronization was connected.
2803 /* Previous state should be NEW. */
2804 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2805 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2806 g_raid3_disk_state2str(disk->d_state)));
2807 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2808 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2809 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2810 g_raid3_device_state2str(sc->sc_state),
2811 g_raid3_get_diskname(disk),
2812 g_raid3_disk_state2str(disk->d_state)));
2813 DISK_STATE_CHANGED();
2815 if (disk->d_state == G_RAID3_DISK_STATE_NEW)
2816 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2817 disk->d_state = state;
2818 if (sc->sc_provider != NULL) {
2819 g_raid3_sync_start(sc);
2820 g_raid3_update_metadata(disk);
2823 case G_RAID3_DISK_STATE_DISCONNECTED:
2825 * Possible scenarios:
2826 * 1. Device wasn't running yet, but disk disappear.
2827 * 2. Disk was active and disapppear.
2828 * 3. Disk disappear during synchronization process.
2830 if (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2831 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
2833 * Previous state should be ACTIVE, STALE or
2836 KASSERT(disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
2837 disk->d_state == G_RAID3_DISK_STATE_STALE ||
2838 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2839 ("Wrong disk state (%s, %s).",
2840 g_raid3_get_diskname(disk),
2841 g_raid3_disk_state2str(disk->d_state)));
2842 } else if (sc->sc_state == G_RAID3_DEVICE_STATE_STARTING) {
2843 /* Previous state should be NEW. */
2844 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2845 ("Wrong disk state (%s, %s).",
2846 g_raid3_get_diskname(disk),
2847 g_raid3_disk_state2str(disk->d_state)));
2849 * Reset bumping syncid if disk disappeared in STARTING
2852 if ((sc->sc_bump_id & G_RAID3_BUMP_SYNCID) != 0)
2853 sc->sc_bump_id &= ~G_RAID3_BUMP_SYNCID;
2856 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2858 g_raid3_device_state2str(sc->sc_state),
2859 g_raid3_get_diskname(disk),
2860 g_raid3_disk_state2str(disk->d_state)));
2863 DISK_STATE_CHANGED();
2864 G_RAID3_DEBUG(0, "Device %s: provider %s disconnected.",
2865 sc->sc_name, g_raid3_get_diskname(disk));
2867 g_raid3_destroy_disk(disk);
2870 KASSERT(1 == 0, ("Unknown state (%u).", state));
2875 #undef DISK_STATE_CHANGED
2878 g_raid3_read_metadata(struct g_consumer *cp, struct g_raid3_metadata *md)
2880 struct g_provider *pp;
2884 g_topology_assert();
2886 error = g_access(cp, 1, 0, 0);
2890 g_topology_unlock();
2891 /* Metadata are stored on last sector. */
2892 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2895 g_access(cp, -1, 0, 0);
2897 G_RAID3_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2898 cp->provider->name, error);
2902 /* Decode metadata. */
2903 error = raid3_metadata_decode(buf, md);
2905 if (strcmp(md->md_magic, G_RAID3_MAGIC) != 0)
2907 if (md->md_version > G_RAID3_VERSION) {
2909 "Kernel module is too old to handle metadata from %s.",
2910 cp->provider->name);
2914 G_RAID3_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2915 cp->provider->name);
2918 if (md->md_sectorsize > MAXPHYS) {
2919 G_RAID3_DEBUG(0, "The blocksize is too big.");
2927 g_raid3_check_metadata(struct g_raid3_softc *sc, struct g_provider *pp,
2928 struct g_raid3_metadata *md)
2931 if (md->md_no >= sc->sc_ndisks) {
2932 G_RAID3_DEBUG(1, "Invalid disk %s number (no=%u), skipping.",
2933 pp->name, md->md_no);
2936 if (sc->sc_disks[md->md_no].d_state != G_RAID3_DISK_STATE_NODISK) {
2937 G_RAID3_DEBUG(1, "Disk %s (no=%u) already exists, skipping.",
2938 pp->name, md->md_no);
2941 if (md->md_all != sc->sc_ndisks) {
2943 "Invalid '%s' field on disk %s (device %s), skipping.",
2944 "md_all", pp->name, sc->sc_name);
2947 if ((md->md_mediasize % md->md_sectorsize) != 0) {
2948 G_RAID3_DEBUG(1, "Invalid metadata (mediasize %% sectorsize != "
2949 "0) on disk %s (device %s), skipping.", pp->name,
2953 if (md->md_mediasize != sc->sc_mediasize) {
2955 "Invalid '%s' field on disk %s (device %s), skipping.",
2956 "md_mediasize", pp->name, sc->sc_name);
2959 if ((md->md_mediasize % (sc->sc_ndisks - 1)) != 0) {
2961 "Invalid '%s' field on disk %s (device %s), skipping.",
2962 "md_mediasize", pp->name, sc->sc_name);
2965 if ((sc->sc_mediasize / (sc->sc_ndisks - 1)) > pp->mediasize) {
2967 "Invalid size of disk %s (device %s), skipping.", pp->name,
2971 if ((md->md_sectorsize / pp->sectorsize) < sc->sc_ndisks - 1) {
2973 "Invalid '%s' field on disk %s (device %s), skipping.",
2974 "md_sectorsize", pp->name, sc->sc_name);
2977 if (md->md_sectorsize != sc->sc_sectorsize) {
2979 "Invalid '%s' field on disk %s (device %s), skipping.",
2980 "md_sectorsize", pp->name, sc->sc_name);
2983 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2985 "Invalid sector size of disk %s (device %s), skipping.",
2986 pp->name, sc->sc_name);
2989 if ((md->md_mflags & ~G_RAID3_DEVICE_FLAG_MASK) != 0) {
2991 "Invalid device flags on disk %s (device %s), skipping.",
2992 pp->name, sc->sc_name);
2995 if ((md->md_mflags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
2996 (md->md_mflags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) {
2998 * VERIFY and ROUND-ROBIN options are mutally exclusive.
3000 G_RAID3_DEBUG(1, "Both VERIFY and ROUND-ROBIN flags exist on "
3001 "disk %s (device %s), skipping.", pp->name, sc->sc_name);
3004 if ((md->md_dflags & ~G_RAID3_DISK_FLAG_MASK) != 0) {
3006 "Invalid disk flags on disk %s (device %s), skipping.",
3007 pp->name, sc->sc_name);
3014 g_raid3_add_disk(struct g_raid3_softc *sc, struct g_provider *pp,
3015 struct g_raid3_metadata *md)
3017 struct g_raid3_disk *disk;
3020 g_topology_assert_not();
3021 G_RAID3_DEBUG(2, "Adding disk %s.", pp->name);
3023 error = g_raid3_check_metadata(sc, pp, md);
3026 if (sc->sc_state != G_RAID3_DEVICE_STATE_STARTING &&
3027 md->md_genid < sc->sc_genid) {
3028 G_RAID3_DEBUG(0, "Component %s (device %s) broken, skipping.",
3029 pp->name, sc->sc_name);
3032 disk = g_raid3_init_disk(sc, pp, md, &error);
3035 error = g_raid3_event_send(disk, G_RAID3_DISK_STATE_NEW,
3036 G_RAID3_EVENT_WAIT);
3039 if (md->md_version < G_RAID3_VERSION) {
3040 G_RAID3_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
3041 pp->name, md->md_version, G_RAID3_VERSION);
3042 g_raid3_update_metadata(disk);
3048 g_raid3_destroy_delayed(void *arg, int flag)
3050 struct g_raid3_softc *sc;
3053 if (flag == EV_CANCEL) {
3054 G_RAID3_DEBUG(1, "Destroying canceled.");
3058 g_topology_unlock();
3059 sx_xlock(&sc->sc_lock);
3060 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) == 0,
3061 ("DESTROY flag set on %s.", sc->sc_name));
3062 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROYING) != 0,
3063 ("DESTROYING flag not set on %s.", sc->sc_name));
3064 G_RAID3_DEBUG(0, "Destroying %s (delayed).", sc->sc_name);
3065 error = g_raid3_destroy(sc, G_RAID3_DESTROY_SOFT);
3067 G_RAID3_DEBUG(0, "Cannot destroy %s.", sc->sc_name);
3068 sx_xunlock(&sc->sc_lock);
3074 g_raid3_access(struct g_provider *pp, int acr, int acw, int ace)
3076 struct g_raid3_softc *sc;
3077 int dcr, dcw, dce, error = 0;
3079 g_topology_assert();
3080 G_RAID3_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
3083 sc = pp->geom->softc;
3084 if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
3086 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
3088 dcr = pp->acr + acr;
3089 dcw = pp->acw + acw;
3090 dce = pp->ace + ace;
3092 g_topology_unlock();
3093 sx_xlock(&sc->sc_lock);
3094 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0 ||
3095 g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) < sc->sc_ndisks - 1) {
3096 if (acr > 0 || acw > 0 || ace > 0)
3100 if (dcw == 0 && !sc->sc_idle)
3101 g_raid3_idle(sc, dcw);
3102 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROYING) != 0) {
3103 if (acr > 0 || acw > 0 || ace > 0) {
3107 if (dcr == 0 && dcw == 0 && dce == 0) {
3108 g_post_event(g_raid3_destroy_delayed, sc, M_WAITOK,
3113 sx_xunlock(&sc->sc_lock);
3118 static struct g_geom *
3119 g_raid3_create(struct g_class *mp, const struct g_raid3_metadata *md)
3121 struct g_raid3_softc *sc;
3126 g_topology_assert();
3127 G_RAID3_DEBUG(1, "Creating device %s (id=%u).", md->md_name, md->md_id);
3129 /* One disk is minimum. */
3135 gp = g_new_geomf(mp, "%s", md->md_name);
3136 sc = malloc(sizeof(*sc), M_RAID3, M_WAITOK | M_ZERO);
3137 sc->sc_disks = malloc(sizeof(struct g_raid3_disk) * md->md_all, M_RAID3,
3139 gp->start = g_raid3_start;
3140 gp->orphan = g_raid3_orphan;
3141 gp->access = g_raid3_access;
3142 gp->dumpconf = g_raid3_dumpconf;
3144 sc->sc_id = md->md_id;
3145 sc->sc_mediasize = md->md_mediasize;
3146 sc->sc_sectorsize = md->md_sectorsize;
3147 sc->sc_ndisks = md->md_all;
3148 sc->sc_round_robin = 0;
3149 sc->sc_flags = md->md_mflags;
3152 sc->sc_last_write = time_uptime;
3154 for (n = 0; n < sc->sc_ndisks; n++) {
3155 sc->sc_disks[n].d_softc = sc;
3156 sc->sc_disks[n].d_no = n;
3157 sc->sc_disks[n].d_state = G_RAID3_DISK_STATE_NODISK;
3159 sx_init(&sc->sc_lock, "graid3:lock");
3160 bioq_init(&sc->sc_queue);
3161 mtx_init(&sc->sc_queue_mtx, "graid3:queue", NULL, MTX_DEF);
3162 bioq_init(&sc->sc_regular_delayed);
3163 bioq_init(&sc->sc_inflight);
3164 bioq_init(&sc->sc_sync_delayed);
3165 TAILQ_INIT(&sc->sc_events);
3166 mtx_init(&sc->sc_events_mtx, "graid3:events", NULL, MTX_DEF);
3167 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
3168 sc->sc_state = G_RAID3_DEVICE_STATE_STARTING;
3171 sc->sc_provider = NULL;
3173 * Synchronization geom.
3175 gp = g_new_geomf(mp, "%s.sync", md->md_name);
3177 gp->orphan = g_raid3_orphan;
3178 sc->sc_sync.ds_geom = gp;
3180 if (!g_raid3_use_malloc) {
3181 sc->sc_zones[G_RAID3_ZONE_64K].sz_zone = uma_zcreate("gr3:64k",
3182 65536, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3184 sc->sc_zones[G_RAID3_ZONE_64K].sz_inuse = 0;
3185 sc->sc_zones[G_RAID3_ZONE_64K].sz_max = g_raid3_n64k;
3186 sc->sc_zones[G_RAID3_ZONE_64K].sz_requested =
3187 sc->sc_zones[G_RAID3_ZONE_64K].sz_failed = 0;
3188 sc->sc_zones[G_RAID3_ZONE_16K].sz_zone = uma_zcreate("gr3:16k",
3189 16384, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3191 sc->sc_zones[G_RAID3_ZONE_16K].sz_inuse = 0;
3192 sc->sc_zones[G_RAID3_ZONE_16K].sz_max = g_raid3_n16k;
3193 sc->sc_zones[G_RAID3_ZONE_16K].sz_requested =
3194 sc->sc_zones[G_RAID3_ZONE_16K].sz_failed = 0;
3195 sc->sc_zones[G_RAID3_ZONE_4K].sz_zone = uma_zcreate("gr3:4k",
3196 4096, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3198 sc->sc_zones[G_RAID3_ZONE_4K].sz_inuse = 0;
3199 sc->sc_zones[G_RAID3_ZONE_4K].sz_max = g_raid3_n4k;
3200 sc->sc_zones[G_RAID3_ZONE_4K].sz_requested =
3201 sc->sc_zones[G_RAID3_ZONE_4K].sz_failed = 0;
3204 error = kproc_create(g_raid3_worker, sc, &sc->sc_worker, 0, 0,
3205 "g_raid3 %s", md->md_name);
3207 G_RAID3_DEBUG(1, "Cannot create kernel thread for %s.",
3209 if (!g_raid3_use_malloc) {
3210 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_64K].sz_zone);
3211 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_16K].sz_zone);
3212 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_4K].sz_zone);
3214 g_destroy_geom(sc->sc_sync.ds_geom);
3215 mtx_destroy(&sc->sc_events_mtx);
3216 mtx_destroy(&sc->sc_queue_mtx);
3217 sx_destroy(&sc->sc_lock);
3218 g_destroy_geom(sc->sc_geom);
3219 free(sc->sc_disks, M_RAID3);
3224 G_RAID3_DEBUG(1, "Device %s created (%u components, id=%u).",
3225 sc->sc_name, sc->sc_ndisks, sc->sc_id);
3227 sc->sc_rootmount = root_mount_hold("GRAID3");
3228 G_RAID3_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
3233 timeout = atomic_load_acq_int(&g_raid3_timeout);
3234 callout_reset(&sc->sc_callout, timeout * hz, g_raid3_go, sc);
3235 return (sc->sc_geom);
3239 g_raid3_destroy(struct g_raid3_softc *sc, int how)
3241 struct g_provider *pp;
3243 g_topology_assert_not();
3246 sx_assert(&sc->sc_lock, SX_XLOCKED);
3248 pp = sc->sc_provider;
3249 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
3251 case G_RAID3_DESTROY_SOFT:
3253 "Device %s is still open (r%dw%de%d).", pp->name,
3254 pp->acr, pp->acw, pp->ace);
3256 case G_RAID3_DESTROY_DELAYED:
3258 "Device %s will be destroyed on last close.",
3260 if (sc->sc_syncdisk != NULL)
3261 g_raid3_sync_stop(sc, 1);
3262 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROYING;
3264 case G_RAID3_DESTROY_HARD:
3265 G_RAID3_DEBUG(1, "Device %s is still open, so it "
3266 "can't be definitely removed.", pp->name);
3272 if (sc->sc_geom->softc == NULL) {
3273 g_topology_unlock();
3276 sc->sc_geom->softc = NULL;
3277 sc->sc_sync.ds_geom->softc = NULL;
3278 g_topology_unlock();
3280 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
3281 sc->sc_flags |= G_RAID3_DEVICE_FLAG_WAIT;
3282 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3283 sx_xunlock(&sc->sc_lock);
3284 mtx_lock(&sc->sc_queue_mtx);
3286 wakeup(&sc->sc_queue);
3287 mtx_unlock(&sc->sc_queue_mtx);
3288 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3289 while (sc->sc_worker != NULL)
3290 tsleep(&sc->sc_worker, PRIBIO, "r3:destroy", hz / 5);
3291 G_RAID3_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3292 sx_xlock(&sc->sc_lock);
3293 g_raid3_destroy_device(sc);
3294 free(sc->sc_disks, M_RAID3);
3300 g_raid3_taste_orphan(struct g_consumer *cp)
3303 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3304 cp->provider->name));
3307 static struct g_geom *
3308 g_raid3_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3310 struct g_raid3_metadata md;
3311 struct g_raid3_softc *sc;
3312 struct g_consumer *cp;
3316 g_topology_assert();
3317 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3318 G_RAID3_DEBUG(2, "Tasting %s.", pp->name);
3320 gp = g_new_geomf(mp, "raid3:taste");
3321 /* This orphan function should be never called. */
3322 gp->orphan = g_raid3_taste_orphan;
3323 cp = g_new_consumer(gp);
3325 error = g_raid3_read_metadata(cp, &md);
3327 g_destroy_consumer(cp);
3333 if (md.md_provider[0] != '\0' &&
3334 !g_compare_names(md.md_provider, pp->name))
3336 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3338 if (g_raid3_debug >= 2)
3339 raid3_metadata_dump(&md);
3342 * Let's check if device already exists.
3345 LIST_FOREACH(gp, &mp->geom, geom) {
3349 if (sc->sc_sync.ds_geom == gp)
3351 if (strcmp(md.md_name, sc->sc_name) != 0)
3353 if (md.md_id != sc->sc_id) {
3354 G_RAID3_DEBUG(0, "Device %s already configured.",
3361 gp = g_raid3_create(mp, &md);
3363 G_RAID3_DEBUG(0, "Cannot create device %s.",
3369 G_RAID3_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3370 g_topology_unlock();
3371 sx_xlock(&sc->sc_lock);
3372 error = g_raid3_add_disk(sc, pp, &md);
3374 G_RAID3_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3375 pp->name, gp->name, error);
3376 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NODISK) ==
3379 g_raid3_destroy(sc, G_RAID3_DESTROY_HARD);
3385 sx_xunlock(&sc->sc_lock);
3391 g_raid3_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
3394 struct g_raid3_softc *sc;
3397 g_topology_unlock();
3399 sx_xlock(&sc->sc_lock);
3401 error = g_raid3_destroy(gp->softc, G_RAID3_DESTROY_SOFT);
3403 sx_xunlock(&sc->sc_lock);
3409 g_raid3_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3410 struct g_consumer *cp, struct g_provider *pp)
3412 struct g_raid3_softc *sc;
3414 g_topology_assert();
3419 /* Skip synchronization geom. */
3420 if (gp == sc->sc_sync.ds_geom)
3424 } else if (cp != NULL) {
3425 struct g_raid3_disk *disk;
3430 g_topology_unlock();
3431 sx_xlock(&sc->sc_lock);
3432 sbuf_printf(sb, "%s<Type>", indent);
3433 if (disk->d_no == sc->sc_ndisks - 1)
3434 sbuf_printf(sb, "PARITY");
3436 sbuf_printf(sb, "DATA");
3437 sbuf_printf(sb, "</Type>\n");
3438 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
3440 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
3441 sbuf_printf(sb, "%s<Synchronized>", indent);
3442 if (disk->d_sync.ds_offset == 0)
3443 sbuf_printf(sb, "0%%");
3445 sbuf_printf(sb, "%u%%",
3446 (u_int)((disk->d_sync.ds_offset * 100) /
3447 (sc->sc_mediasize / (sc->sc_ndisks - 1))));
3449 sbuf_printf(sb, "</Synchronized>\n");
3451 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3452 disk->d_sync.ds_syncid);
3453 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, disk->d_genid);
3454 sbuf_printf(sb, "%s<Flags>", indent);
3455 if (disk->d_flags == 0)
3456 sbuf_printf(sb, "NONE");
3460 #define ADD_FLAG(flag, name) do { \
3461 if ((disk->d_flags & (flag)) != 0) { \
3463 sbuf_printf(sb, ", "); \
3466 sbuf_printf(sb, name); \
3469 ADD_FLAG(G_RAID3_DISK_FLAG_DIRTY, "DIRTY");
3470 ADD_FLAG(G_RAID3_DISK_FLAG_HARDCODED, "HARDCODED");
3471 ADD_FLAG(G_RAID3_DISK_FLAG_SYNCHRONIZING,
3473 ADD_FLAG(G_RAID3_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3474 ADD_FLAG(G_RAID3_DISK_FLAG_BROKEN, "BROKEN");
3477 sbuf_printf(sb, "</Flags>\n");
3478 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3479 g_raid3_disk_state2str(disk->d_state));
3480 sx_xunlock(&sc->sc_lock);
3483 g_topology_unlock();
3484 sx_xlock(&sc->sc_lock);
3485 if (!g_raid3_use_malloc) {
3487 "%s<Zone4kRequested>%u</Zone4kRequested>\n", indent,
3488 sc->sc_zones[G_RAID3_ZONE_4K].sz_requested);
3490 "%s<Zone4kFailed>%u</Zone4kFailed>\n", indent,
3491 sc->sc_zones[G_RAID3_ZONE_4K].sz_failed);
3493 "%s<Zone16kRequested>%u</Zone16kRequested>\n", indent,
3494 sc->sc_zones[G_RAID3_ZONE_16K].sz_requested);
3496 "%s<Zone16kFailed>%u</Zone16kFailed>\n", indent,
3497 sc->sc_zones[G_RAID3_ZONE_16K].sz_failed);
3499 "%s<Zone64kRequested>%u</Zone64kRequested>\n", indent,
3500 sc->sc_zones[G_RAID3_ZONE_64K].sz_requested);
3502 "%s<Zone64kFailed>%u</Zone64kFailed>\n", indent,
3503 sc->sc_zones[G_RAID3_ZONE_64K].sz_failed);
3505 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3506 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3507 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3508 sbuf_printf(sb, "%s<Flags>", indent);
3509 if (sc->sc_flags == 0)
3510 sbuf_printf(sb, "NONE");
3514 #define ADD_FLAG(flag, name) do { \
3515 if ((sc->sc_flags & (flag)) != 0) { \
3517 sbuf_printf(sb, ", "); \
3520 sbuf_printf(sb, name); \
3523 ADD_FLAG(G_RAID3_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3524 ADD_FLAG(G_RAID3_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3525 ADD_FLAG(G_RAID3_DEVICE_FLAG_ROUND_ROBIN,
3527 ADD_FLAG(G_RAID3_DEVICE_FLAG_VERIFY, "VERIFY");
3530 sbuf_printf(sb, "</Flags>\n");
3531 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3533 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3534 g_raid3_device_state2str(sc->sc_state));
3535 sx_xunlock(&sc->sc_lock);
3541 g_raid3_shutdown_pre_sync(void *arg, int howto)
3544 struct g_geom *gp, *gp2;
3545 struct g_raid3_softc *sc;
3551 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3552 if ((sc = gp->softc) == NULL)
3554 /* Skip synchronization geom. */
3555 if (gp == sc->sc_sync.ds_geom)
3557 g_topology_unlock();
3558 sx_xlock(&sc->sc_lock);
3560 error = g_raid3_destroy(sc, G_RAID3_DESTROY_DELAYED);
3562 sx_xunlock(&sc->sc_lock);
3565 g_topology_unlock();
3570 g_raid3_init(struct g_class *mp)
3573 g_raid3_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
3574 g_raid3_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
3575 if (g_raid3_pre_sync == NULL)
3576 G_RAID3_DEBUG(0, "Warning! Cannot register shutdown event.");
3580 g_raid3_fini(struct g_class *mp)
3583 if (g_raid3_pre_sync != NULL)
3584 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_raid3_pre_sync);
3587 DECLARE_GEOM_CLASS(g_raid3_class, g_raid3);