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 static SYSCTL_NODE(_kern_geom, OID_AUTO, raid3, CTLFLAG_RW, 0,
56 u_int g_raid3_debug = 0;
57 TUNABLE_INT("kern.geom.raid3.debug", &g_raid3_debug);
58 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, debug, CTLFLAG_RW, &g_raid3_debug, 0,
60 static u_int g_raid3_timeout = 4;
61 TUNABLE_INT("kern.geom.raid3.timeout", &g_raid3_timeout);
62 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, timeout, CTLFLAG_RW, &g_raid3_timeout,
63 0, "Time to wait on all raid3 components");
64 static u_int g_raid3_idletime = 5;
65 TUNABLE_INT("kern.geom.raid3.idletime", &g_raid3_idletime);
66 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, idletime, CTLFLAG_RW,
67 &g_raid3_idletime, 0, "Mark components as clean when idling");
68 static u_int g_raid3_disconnect_on_failure = 1;
69 TUNABLE_INT("kern.geom.raid3.disconnect_on_failure",
70 &g_raid3_disconnect_on_failure);
71 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
72 &g_raid3_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
73 static u_int g_raid3_syncreqs = 2;
74 TUNABLE_INT("kern.geom.raid3.sync_requests", &g_raid3_syncreqs);
75 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
76 &g_raid3_syncreqs, 0, "Parallel synchronization I/O requests.");
77 static u_int g_raid3_use_malloc = 0;
78 TUNABLE_INT("kern.geom.raid3.use_malloc", &g_raid3_use_malloc);
79 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, use_malloc, CTLFLAG_RDTUN,
80 &g_raid3_use_malloc, 0, "Use malloc(9) instead of uma(9).");
82 static u_int g_raid3_n64k = 50;
83 TUNABLE_INT("kern.geom.raid3.n64k", &g_raid3_n64k);
84 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n64k, CTLFLAG_RD, &g_raid3_n64k, 0,
85 "Maximum number of 64kB allocations");
86 static u_int g_raid3_n16k = 200;
87 TUNABLE_INT("kern.geom.raid3.n16k", &g_raid3_n16k);
88 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n16k, CTLFLAG_RD, &g_raid3_n16k, 0,
89 "Maximum number of 16kB allocations");
90 static u_int g_raid3_n4k = 1200;
91 TUNABLE_INT("kern.geom.raid3.n4k", &g_raid3_n4k);
92 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n4k, CTLFLAG_RD, &g_raid3_n4k, 0,
93 "Maximum number of 4kB allocations");
95 static SYSCTL_NODE(_kern_geom_raid3, OID_AUTO, stat, CTLFLAG_RW, 0,
96 "GEOM_RAID3 statistics");
97 static u_int g_raid3_parity_mismatch = 0;
98 SYSCTL_UINT(_kern_geom_raid3_stat, OID_AUTO, parity_mismatch, CTLFLAG_RD,
99 &g_raid3_parity_mismatch, 0, "Number of failures in VERIFY mode");
101 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \
102 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
103 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
104 G_RAID3_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
107 static eventhandler_tag g_raid3_post_sync = NULL;
108 static int g_raid3_shutdown = 0;
110 static int g_raid3_destroy_geom(struct gctl_req *req, struct g_class *mp,
112 static g_taste_t g_raid3_taste;
113 static void g_raid3_init(struct g_class *mp);
114 static void g_raid3_fini(struct g_class *mp);
116 struct g_class g_raid3_class = {
117 .name = G_RAID3_CLASS_NAME,
118 .version = G_VERSION,
119 .ctlreq = g_raid3_config,
120 .taste = g_raid3_taste,
121 .destroy_geom = g_raid3_destroy_geom,
122 .init = g_raid3_init,
127 static void g_raid3_destroy_provider(struct g_raid3_softc *sc);
128 static int g_raid3_update_disk(struct g_raid3_disk *disk, u_int state);
129 static void g_raid3_update_device(struct g_raid3_softc *sc, boolean_t force);
130 static void g_raid3_dumpconf(struct sbuf *sb, const char *indent,
131 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
132 static void g_raid3_sync_stop(struct g_raid3_softc *sc, int type);
133 static int g_raid3_register_request(struct bio *pbp);
134 static void g_raid3_sync_release(struct g_raid3_softc *sc);
138 g_raid3_disk_state2str(int state)
142 case G_RAID3_DISK_STATE_NODISK:
144 case G_RAID3_DISK_STATE_NONE:
146 case G_RAID3_DISK_STATE_NEW:
148 case G_RAID3_DISK_STATE_ACTIVE:
150 case G_RAID3_DISK_STATE_STALE:
152 case G_RAID3_DISK_STATE_SYNCHRONIZING:
153 return ("SYNCHRONIZING");
154 case G_RAID3_DISK_STATE_DISCONNECTED:
155 return ("DISCONNECTED");
162 g_raid3_device_state2str(int state)
166 case G_RAID3_DEVICE_STATE_STARTING:
168 case G_RAID3_DEVICE_STATE_DEGRADED:
170 case G_RAID3_DEVICE_STATE_COMPLETE:
178 g_raid3_get_diskname(struct g_raid3_disk *disk)
181 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
182 return ("[unknown]");
183 return (disk->d_name);
187 g_raid3_alloc(struct g_raid3_softc *sc, size_t size, int flags)
190 enum g_raid3_zones zone;
192 if (g_raid3_use_malloc ||
193 (zone = g_raid3_zone(size)) == G_RAID3_NUM_ZONES)
194 ptr = malloc(size, M_RAID3, flags);
196 ptr = uma_zalloc_arg(sc->sc_zones[zone].sz_zone,
197 &sc->sc_zones[zone], flags);
198 sc->sc_zones[zone].sz_requested++;
200 sc->sc_zones[zone].sz_failed++;
206 g_raid3_free(struct g_raid3_softc *sc, void *ptr, size_t size)
208 enum g_raid3_zones zone;
210 if (g_raid3_use_malloc ||
211 (zone = g_raid3_zone(size)) == G_RAID3_NUM_ZONES)
214 uma_zfree_arg(sc->sc_zones[zone].sz_zone,
215 ptr, &sc->sc_zones[zone]);
220 g_raid3_uma_ctor(void *mem, int size, void *arg, int flags)
222 struct g_raid3_zone *sz = arg;
224 if (sz->sz_max > 0 && sz->sz_inuse == sz->sz_max)
231 g_raid3_uma_dtor(void *mem, int size, void *arg)
233 struct g_raid3_zone *sz = arg;
238 #define g_raid3_xor(src, dst, size) \
239 _g_raid3_xor((uint64_t *)(src), \
240 (uint64_t *)(dst), (size_t)size)
242 _g_raid3_xor(uint64_t *src, uint64_t *dst, size_t size)
245 KASSERT((size % 128) == 0, ("Invalid size: %zu.", size));
246 for (; size > 0; size -= 128) {
267 g_raid3_is_zero(struct bio *bp)
269 static const uint64_t zeros[] = {
270 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
275 size = bp->bio_length;
276 addr = (u_char *)bp->bio_data;
277 for (; size > 0; size -= sizeof(zeros), addr += sizeof(zeros)) {
278 if (bcmp(addr, zeros, sizeof(zeros)) != 0)
285 * --- Events handling functions ---
286 * Events in geom_raid3 are used to maintain disks and device status
287 * from one thread to simplify locking.
290 g_raid3_event_free(struct g_raid3_event *ep)
297 g_raid3_event_send(void *arg, int state, int flags)
299 struct g_raid3_softc *sc;
300 struct g_raid3_disk *disk;
301 struct g_raid3_event *ep;
304 ep = malloc(sizeof(*ep), M_RAID3, M_WAITOK);
305 G_RAID3_DEBUG(4, "%s: Sending event %p.", __func__, ep);
306 if ((flags & G_RAID3_EVENT_DEVICE) != 0) {
317 mtx_lock(&sc->sc_events_mtx);
318 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
319 mtx_unlock(&sc->sc_events_mtx);
320 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
321 mtx_lock(&sc->sc_queue_mtx);
323 wakeup(&sc->sc_queue);
324 mtx_unlock(&sc->sc_queue_mtx);
325 if ((flags & G_RAID3_EVENT_DONTWAIT) != 0)
327 sx_assert(&sc->sc_lock, SX_XLOCKED);
328 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
329 sx_xunlock(&sc->sc_lock);
330 while ((ep->e_flags & G_RAID3_EVENT_DONE) == 0) {
331 mtx_lock(&sc->sc_events_mtx);
332 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "r3:event",
336 g_raid3_event_free(ep);
337 sx_xlock(&sc->sc_lock);
341 static struct g_raid3_event *
342 g_raid3_event_get(struct g_raid3_softc *sc)
344 struct g_raid3_event *ep;
346 mtx_lock(&sc->sc_events_mtx);
347 ep = TAILQ_FIRST(&sc->sc_events);
348 mtx_unlock(&sc->sc_events_mtx);
353 g_raid3_event_remove(struct g_raid3_softc *sc, struct g_raid3_event *ep)
356 mtx_lock(&sc->sc_events_mtx);
357 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
358 mtx_unlock(&sc->sc_events_mtx);
362 g_raid3_event_cancel(struct g_raid3_disk *disk)
364 struct g_raid3_softc *sc;
365 struct g_raid3_event *ep, *tmpep;
368 sx_assert(&sc->sc_lock, SX_XLOCKED);
370 mtx_lock(&sc->sc_events_mtx);
371 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
372 if ((ep->e_flags & G_RAID3_EVENT_DEVICE) != 0)
374 if (ep->e_disk != disk)
376 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
377 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0)
378 g_raid3_event_free(ep);
380 ep->e_error = ECANCELED;
384 mtx_unlock(&sc->sc_events_mtx);
388 * Return the number of disks in the given state.
389 * If state is equal to -1, count all connected disks.
392 g_raid3_ndisks(struct g_raid3_softc *sc, int state)
394 struct g_raid3_disk *disk;
397 sx_assert(&sc->sc_lock, SX_LOCKED);
399 for (n = ndisks = 0; n < sc->sc_ndisks; n++) {
400 disk = &sc->sc_disks[n];
401 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
403 if (state == -1 || disk->d_state == state)
410 g_raid3_nrequests(struct g_raid3_softc *sc, struct g_consumer *cp)
415 mtx_lock(&sc->sc_queue_mtx);
416 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
417 if (bp->bio_from == cp)
420 mtx_unlock(&sc->sc_queue_mtx);
425 g_raid3_is_busy(struct g_raid3_softc *sc, struct g_consumer *cp)
430 "I/O requests for %s exist, can't destroy it now.",
434 if (g_raid3_nrequests(sc, cp) > 0) {
436 "I/O requests for %s in queue, can't destroy it now.",
444 g_raid3_destroy_consumer(void *arg, int flags __unused)
446 struct g_consumer *cp;
451 G_RAID3_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
453 g_destroy_consumer(cp);
457 g_raid3_kill_consumer(struct g_raid3_softc *sc, struct g_consumer *cp)
459 struct g_provider *pp;
465 if (g_raid3_is_busy(sc, cp))
467 G_RAID3_DEBUG(2, "Consumer %s destroyed.", cp->provider->name);
471 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
474 G_RAID3_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
475 -cp->acw, -cp->ace, 0);
476 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
477 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
480 * After retaste event was send (inside g_access()), we can send
481 * event to detach and destroy consumer.
482 * A class, which has consumer to the given provider connected
483 * will not receive retaste event for the provider.
484 * This is the way how I ignore retaste events when I close
485 * consumers opened for write: I detach and destroy consumer
486 * after retaste event is sent.
488 g_post_event(g_raid3_destroy_consumer, cp, M_WAITOK, NULL);
491 G_RAID3_DEBUG(1, "Consumer %s destroyed.", pp->name);
493 g_destroy_consumer(cp);
497 g_raid3_connect_disk(struct g_raid3_disk *disk, struct g_provider *pp)
499 struct g_consumer *cp;
502 g_topology_assert_not();
503 KASSERT(disk->d_consumer == NULL,
504 ("Disk already connected (device %s).", disk->d_softc->sc_name));
507 cp = g_new_consumer(disk->d_softc->sc_geom);
508 error = g_attach(cp, pp);
510 g_destroy_consumer(cp);
514 error = g_access(cp, 1, 1, 1);
518 g_destroy_consumer(cp);
519 G_RAID3_DEBUG(0, "Cannot open consumer %s (error=%d).",
523 disk->d_consumer = cp;
524 disk->d_consumer->private = disk;
525 disk->d_consumer->index = 0;
526 G_RAID3_DEBUG(2, "Disk %s connected.", g_raid3_get_diskname(disk));
531 g_raid3_disconnect_consumer(struct g_raid3_softc *sc, struct g_consumer *cp)
538 if (cp->provider != NULL)
539 g_raid3_kill_consumer(sc, cp);
541 g_destroy_consumer(cp);
545 * Initialize disk. This means allocate memory, create consumer, attach it
546 * to the provider and open access (r1w1e1) to it.
548 static struct g_raid3_disk *
549 g_raid3_init_disk(struct g_raid3_softc *sc, struct g_provider *pp,
550 struct g_raid3_metadata *md, int *errorp)
552 struct g_raid3_disk *disk;
555 disk = &sc->sc_disks[md->md_no];
556 error = g_raid3_connect_disk(disk, pp);
562 disk->d_state = G_RAID3_DISK_STATE_NONE;
563 disk->d_flags = md->md_dflags;
564 if (md->md_provider[0] != '\0')
565 disk->d_flags |= G_RAID3_DISK_FLAG_HARDCODED;
566 disk->d_sync.ds_consumer = NULL;
567 disk->d_sync.ds_offset = md->md_sync_offset;
568 disk->d_sync.ds_offset_done = md->md_sync_offset;
569 disk->d_genid = md->md_genid;
570 disk->d_sync.ds_syncid = md->md_syncid;
577 g_raid3_destroy_disk(struct g_raid3_disk *disk)
579 struct g_raid3_softc *sc;
581 g_topology_assert_not();
583 sx_assert(&sc->sc_lock, SX_XLOCKED);
585 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
587 g_raid3_event_cancel(disk);
588 switch (disk->d_state) {
589 case G_RAID3_DISK_STATE_SYNCHRONIZING:
590 if (sc->sc_syncdisk != NULL)
591 g_raid3_sync_stop(sc, 1);
593 case G_RAID3_DISK_STATE_NEW:
594 case G_RAID3_DISK_STATE_STALE:
595 case G_RAID3_DISK_STATE_ACTIVE:
597 g_raid3_disconnect_consumer(sc, disk->d_consumer);
599 disk->d_consumer = NULL;
602 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
603 g_raid3_get_diskname(disk),
604 g_raid3_disk_state2str(disk->d_state)));
606 disk->d_state = G_RAID3_DISK_STATE_NODISK;
610 g_raid3_destroy_device(struct g_raid3_softc *sc)
612 struct g_raid3_event *ep;
613 struct g_raid3_disk *disk;
615 struct g_consumer *cp;
618 g_topology_assert_not();
619 sx_assert(&sc->sc_lock, SX_XLOCKED);
622 if (sc->sc_provider != NULL)
623 g_raid3_destroy_provider(sc);
624 for (n = 0; n < sc->sc_ndisks; n++) {
625 disk = &sc->sc_disks[n];
626 if (disk->d_state != G_RAID3_DISK_STATE_NODISK) {
627 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
628 g_raid3_update_metadata(disk);
629 g_raid3_destroy_disk(disk);
632 while ((ep = g_raid3_event_get(sc)) != NULL) {
633 g_raid3_event_remove(sc, ep);
634 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0)
635 g_raid3_event_free(ep);
637 ep->e_error = ECANCELED;
638 ep->e_flags |= G_RAID3_EVENT_DONE;
639 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, ep);
640 mtx_lock(&sc->sc_events_mtx);
642 mtx_unlock(&sc->sc_events_mtx);
645 callout_drain(&sc->sc_callout);
646 cp = LIST_FIRST(&sc->sc_sync.ds_geom->consumer);
649 g_raid3_disconnect_consumer(sc, cp);
650 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
651 G_RAID3_DEBUG(0, "Device %s destroyed.", gp->name);
652 g_wither_geom(gp, ENXIO);
654 if (!g_raid3_use_malloc) {
655 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_64K].sz_zone);
656 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_16K].sz_zone);
657 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_4K].sz_zone);
659 mtx_destroy(&sc->sc_queue_mtx);
660 mtx_destroy(&sc->sc_events_mtx);
661 sx_xunlock(&sc->sc_lock);
662 sx_destroy(&sc->sc_lock);
666 g_raid3_orphan(struct g_consumer *cp)
668 struct g_raid3_disk *disk;
675 disk->d_softc->sc_bump_id = G_RAID3_BUMP_SYNCID;
676 g_raid3_event_send(disk, G_RAID3_DISK_STATE_DISCONNECTED,
677 G_RAID3_EVENT_DONTWAIT);
681 g_raid3_write_metadata(struct g_raid3_disk *disk, struct g_raid3_metadata *md)
683 struct g_raid3_softc *sc;
684 struct g_consumer *cp;
685 off_t offset, length;
689 g_topology_assert_not();
691 sx_assert(&sc->sc_lock, SX_LOCKED);
693 cp = disk->d_consumer;
694 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
695 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
696 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
697 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
699 length = cp->provider->sectorsize;
700 offset = cp->provider->mediasize - length;
701 sector = malloc((size_t)length, M_RAID3, M_WAITOK | M_ZERO);
703 raid3_metadata_encode(md, sector);
704 error = g_write_data(cp, offset, sector, length);
705 free(sector, M_RAID3);
707 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
708 G_RAID3_DEBUG(0, "Cannot write metadata on %s "
709 "(device=%s, error=%d).",
710 g_raid3_get_diskname(disk), sc->sc_name, error);
711 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
713 G_RAID3_DEBUG(1, "Cannot write metadata on %s "
714 "(device=%s, error=%d).",
715 g_raid3_get_diskname(disk), sc->sc_name, error);
717 if (g_raid3_disconnect_on_failure &&
718 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
719 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
720 g_raid3_event_send(disk,
721 G_RAID3_DISK_STATE_DISCONNECTED,
722 G_RAID3_EVENT_DONTWAIT);
729 g_raid3_clear_metadata(struct g_raid3_disk *disk)
733 g_topology_assert_not();
734 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
736 error = g_raid3_write_metadata(disk, NULL);
738 G_RAID3_DEBUG(2, "Metadata on %s cleared.",
739 g_raid3_get_diskname(disk));
742 "Cannot clear metadata on disk %s (error=%d).",
743 g_raid3_get_diskname(disk), error);
749 g_raid3_fill_metadata(struct g_raid3_disk *disk, struct g_raid3_metadata *md)
751 struct g_raid3_softc *sc;
752 struct g_provider *pp;
755 strlcpy(md->md_magic, G_RAID3_MAGIC, sizeof(md->md_magic));
756 md->md_version = G_RAID3_VERSION;
757 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
758 md->md_id = sc->sc_id;
759 md->md_all = sc->sc_ndisks;
760 md->md_genid = sc->sc_genid;
761 md->md_mediasize = sc->sc_mediasize;
762 md->md_sectorsize = sc->sc_sectorsize;
763 md->md_mflags = (sc->sc_flags & G_RAID3_DEVICE_FLAG_MASK);
764 md->md_no = disk->d_no;
765 md->md_syncid = disk->d_sync.ds_syncid;
766 md->md_dflags = (disk->d_flags & G_RAID3_DISK_FLAG_MASK);
767 if (disk->d_state != G_RAID3_DISK_STATE_SYNCHRONIZING)
768 md->md_sync_offset = 0;
771 disk->d_sync.ds_offset_done / (sc->sc_ndisks - 1);
773 if (disk->d_consumer != NULL && disk->d_consumer->provider != NULL)
774 pp = disk->d_consumer->provider;
777 if ((disk->d_flags & G_RAID3_DISK_FLAG_HARDCODED) != 0 && pp != NULL)
778 strlcpy(md->md_provider, pp->name, sizeof(md->md_provider));
780 bzero(md->md_provider, sizeof(md->md_provider));
782 md->md_provsize = pp->mediasize;
788 g_raid3_update_metadata(struct g_raid3_disk *disk)
790 struct g_raid3_softc *sc;
791 struct g_raid3_metadata md;
794 g_topology_assert_not();
796 sx_assert(&sc->sc_lock, SX_LOCKED);
798 g_raid3_fill_metadata(disk, &md);
799 error = g_raid3_write_metadata(disk, &md);
801 G_RAID3_DEBUG(2, "Metadata on %s updated.",
802 g_raid3_get_diskname(disk));
805 "Cannot update metadata on disk %s (error=%d).",
806 g_raid3_get_diskname(disk), error);
811 g_raid3_bump_syncid(struct g_raid3_softc *sc)
813 struct g_raid3_disk *disk;
816 g_topology_assert_not();
817 sx_assert(&sc->sc_lock, SX_XLOCKED);
818 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) > 0,
819 ("%s called with no active disks (device=%s).", __func__,
823 G_RAID3_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
825 for (n = 0; n < sc->sc_ndisks; n++) {
826 disk = &sc->sc_disks[n];
827 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
828 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
829 disk->d_sync.ds_syncid = sc->sc_syncid;
830 g_raid3_update_metadata(disk);
836 g_raid3_bump_genid(struct g_raid3_softc *sc)
838 struct g_raid3_disk *disk;
841 g_topology_assert_not();
842 sx_assert(&sc->sc_lock, SX_XLOCKED);
843 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) > 0,
844 ("%s called with no active disks (device=%s).", __func__,
848 G_RAID3_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
850 for (n = 0; n < sc->sc_ndisks; n++) {
851 disk = &sc->sc_disks[n];
852 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
853 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
854 disk->d_genid = sc->sc_genid;
855 g_raid3_update_metadata(disk);
861 g_raid3_idle(struct g_raid3_softc *sc, int acw)
863 struct g_raid3_disk *disk;
867 g_topology_assert_not();
868 sx_assert(&sc->sc_lock, SX_XLOCKED);
870 if (sc->sc_provider == NULL)
872 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
876 if (sc->sc_writes > 0)
878 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
879 timeout = g_raid3_idletime - (time_uptime - sc->sc_last_write);
880 if (!g_raid3_shutdown && timeout > 0)
884 for (i = 0; i < sc->sc_ndisks; i++) {
885 disk = &sc->sc_disks[i];
886 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
888 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as clean.",
889 g_raid3_get_diskname(disk), sc->sc_name);
890 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
891 g_raid3_update_metadata(disk);
897 g_raid3_unidle(struct g_raid3_softc *sc)
899 struct g_raid3_disk *disk;
902 g_topology_assert_not();
903 sx_assert(&sc->sc_lock, SX_XLOCKED);
905 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
908 sc->sc_last_write = time_uptime;
909 for (i = 0; i < sc->sc_ndisks; i++) {
910 disk = &sc->sc_disks[i];
911 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
913 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as dirty.",
914 g_raid3_get_diskname(disk), sc->sc_name);
915 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
916 g_raid3_update_metadata(disk);
921 * Treat bio_driver1 field in parent bio as list head and field bio_caller1
922 * in child bio as pointer to the next element on the list.
924 #define G_RAID3_HEAD_BIO(pbp) (pbp)->bio_driver1
926 #define G_RAID3_NEXT_BIO(cbp) (cbp)->bio_caller1
928 #define G_RAID3_FOREACH_BIO(pbp, bp) \
929 for ((bp) = G_RAID3_HEAD_BIO(pbp); (bp) != NULL; \
930 (bp) = G_RAID3_NEXT_BIO(bp))
932 #define G_RAID3_FOREACH_SAFE_BIO(pbp, bp, tmpbp) \
933 for ((bp) = G_RAID3_HEAD_BIO(pbp); \
934 (bp) != NULL && ((tmpbp) = G_RAID3_NEXT_BIO(bp), 1); \
938 g_raid3_init_bio(struct bio *pbp)
941 G_RAID3_HEAD_BIO(pbp) = NULL;
945 g_raid3_remove_bio(struct bio *cbp)
947 struct bio *pbp, *bp;
949 pbp = cbp->bio_parent;
950 if (G_RAID3_HEAD_BIO(pbp) == cbp)
951 G_RAID3_HEAD_BIO(pbp) = G_RAID3_NEXT_BIO(cbp);
953 G_RAID3_FOREACH_BIO(pbp, bp) {
954 if (G_RAID3_NEXT_BIO(bp) == cbp) {
955 G_RAID3_NEXT_BIO(bp) = G_RAID3_NEXT_BIO(cbp);
960 G_RAID3_NEXT_BIO(cbp) = NULL;
964 g_raid3_replace_bio(struct bio *sbp, struct bio *dbp)
966 struct bio *pbp, *bp;
968 g_raid3_remove_bio(sbp);
969 pbp = dbp->bio_parent;
970 G_RAID3_NEXT_BIO(sbp) = G_RAID3_NEXT_BIO(dbp);
971 if (G_RAID3_HEAD_BIO(pbp) == dbp)
972 G_RAID3_HEAD_BIO(pbp) = sbp;
974 G_RAID3_FOREACH_BIO(pbp, bp) {
975 if (G_RAID3_NEXT_BIO(bp) == dbp) {
976 G_RAID3_NEXT_BIO(bp) = sbp;
981 G_RAID3_NEXT_BIO(dbp) = NULL;
985 g_raid3_destroy_bio(struct g_raid3_softc *sc, struct bio *cbp)
987 struct bio *bp, *pbp;
990 pbp = cbp->bio_parent;
992 KASSERT(cbp->bio_data != NULL, ("NULL bio_data"));
993 size = pbp->bio_length / (sc->sc_ndisks - 1);
994 g_raid3_free(sc, cbp->bio_data, size);
995 if (G_RAID3_HEAD_BIO(pbp) == cbp) {
996 G_RAID3_HEAD_BIO(pbp) = G_RAID3_NEXT_BIO(cbp);
997 G_RAID3_NEXT_BIO(cbp) = NULL;
1000 G_RAID3_FOREACH_BIO(pbp, bp) {
1001 if (G_RAID3_NEXT_BIO(bp) == cbp)
1005 KASSERT(G_RAID3_NEXT_BIO(bp) != NULL,
1006 ("NULL bp->bio_driver1"));
1007 G_RAID3_NEXT_BIO(bp) = G_RAID3_NEXT_BIO(cbp);
1008 G_RAID3_NEXT_BIO(cbp) = NULL;
1015 g_raid3_clone_bio(struct g_raid3_softc *sc, struct bio *pbp)
1017 struct bio *bp, *cbp;
1021 cbp = g_clone_bio(pbp);
1024 size = pbp->bio_length / (sc->sc_ndisks - 1);
1025 if ((pbp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR) != 0)
1029 cbp->bio_data = g_raid3_alloc(sc, size, memflag);
1030 if (cbp->bio_data == NULL) {
1031 pbp->bio_children--;
1035 G_RAID3_NEXT_BIO(cbp) = NULL;
1036 if (G_RAID3_HEAD_BIO(pbp) == NULL)
1037 G_RAID3_HEAD_BIO(pbp) = cbp;
1039 G_RAID3_FOREACH_BIO(pbp, bp) {
1040 if (G_RAID3_NEXT_BIO(bp) == NULL) {
1041 G_RAID3_NEXT_BIO(bp) = cbp;
1050 g_raid3_scatter(struct bio *pbp)
1052 struct g_raid3_softc *sc;
1053 struct g_raid3_disk *disk;
1054 struct bio *bp, *cbp, *tmpbp;
1055 off_t atom, cadd, padd, left;
1058 sc = pbp->bio_to->geom->softc;
1060 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_NOPARITY) == 0) {
1062 * Find bio for which we should calculate data.
1064 G_RAID3_FOREACH_BIO(pbp, cbp) {
1065 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0) {
1070 KASSERT(bp != NULL, ("NULL parity bio."));
1072 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1074 for (left = pbp->bio_length; left > 0; left -= sc->sc_sectorsize) {
1075 G_RAID3_FOREACH_BIO(pbp, cbp) {
1078 bcopy(pbp->bio_data + padd, cbp->bio_data + cadd, atom);
1083 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_NOPARITY) == 0) {
1088 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1092 bcopy(cbp->bio_data, bp->bio_data,
1096 g_raid3_xor(cbp->bio_data, bp->bio_data,
1099 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_NODISK) != 0)
1100 g_raid3_destroy_bio(sc, cbp);
1103 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1104 struct g_consumer *cp;
1106 disk = cbp->bio_caller2;
1107 cp = disk->d_consumer;
1108 cbp->bio_to = cp->provider;
1109 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1110 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1111 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1112 cp->acr, cp->acw, cp->ace));
1115 g_io_request(cbp, cp);
1120 g_raid3_gather(struct bio *pbp)
1122 struct g_raid3_softc *sc;
1123 struct g_raid3_disk *disk;
1124 struct bio *xbp, *fbp, *cbp;
1125 off_t atom, cadd, padd, left;
1127 sc = pbp->bio_to->geom->softc;
1129 * Find bio for which we have to calculate data.
1130 * While going through this path, check if all requests
1131 * succeeded, if not, deny whole request.
1132 * If we're in COMPLETE mode, we allow one request to fail,
1133 * so if we find one, we're sending it to the parity consumer.
1134 * If there are more failed requests, we deny whole request.
1137 G_RAID3_FOREACH_BIO(pbp, cbp) {
1138 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0) {
1139 KASSERT(xbp == NULL, ("More than one parity bio."));
1142 if (cbp->bio_error == 0)
1145 * Found failed request.
1148 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_DEGRADED) != 0) {
1150 * We are already in degraded mode, so we can't
1151 * accept any failures.
1153 if (pbp->bio_error == 0)
1154 pbp->bio_error = cbp->bio_error;
1160 * Next failed request, that's too many.
1162 if (pbp->bio_error == 0)
1163 pbp->bio_error = fbp->bio_error;
1165 disk = cbp->bio_caller2;
1168 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
1169 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
1170 G_RAID3_LOGREQ(0, cbp, "Request failed (error=%d).",
1173 G_RAID3_LOGREQ(1, cbp, "Request failed (error=%d).",
1176 if (g_raid3_disconnect_on_failure &&
1177 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1178 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1179 g_raid3_event_send(disk,
1180 G_RAID3_DISK_STATE_DISCONNECTED,
1181 G_RAID3_EVENT_DONTWAIT);
1184 if (pbp->bio_error != 0)
1186 if (fbp != NULL && (pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0) {
1187 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_VERIFY;
1189 g_raid3_replace_bio(xbp, fbp);
1190 g_raid3_destroy_bio(sc, fbp);
1191 } else if (fbp != NULL) {
1192 struct g_consumer *cp;
1195 * One request failed, so send the same request to
1196 * the parity consumer.
1198 disk = pbp->bio_driver2;
1199 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE) {
1200 pbp->bio_error = fbp->bio_error;
1203 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1205 fbp->bio_flags &= ~(BIO_DONE | BIO_ERROR);
1206 if (disk->d_no == sc->sc_ndisks - 1)
1207 fbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1209 fbp->bio_completed = 0;
1210 fbp->bio_children = 0;
1212 cp = disk->d_consumer;
1213 fbp->bio_caller2 = disk;
1214 fbp->bio_to = cp->provider;
1215 G_RAID3_LOGREQ(3, fbp, "Sending request (recover).");
1216 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1217 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1218 cp->acr, cp->acw, cp->ace));
1220 g_io_request(fbp, cp);
1227 G_RAID3_FOREACH_BIO(pbp, cbp) {
1228 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0)
1230 g_raid3_xor(cbp->bio_data, xbp->bio_data,
1233 xbp->bio_cflags &= ~G_RAID3_BIO_CFLAG_PARITY;
1234 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0) {
1235 if (!g_raid3_is_zero(xbp)) {
1236 g_raid3_parity_mismatch++;
1237 pbp->bio_error = EIO;
1240 g_raid3_destroy_bio(sc, xbp);
1243 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1245 for (left = pbp->bio_length; left > 0; left -= sc->sc_sectorsize) {
1246 G_RAID3_FOREACH_BIO(pbp, cbp) {
1247 bcopy(cbp->bio_data + cadd, pbp->bio_data + padd, atom);
1248 pbp->bio_completed += atom;
1254 if (pbp->bio_error == 0)
1255 G_RAID3_LOGREQ(3, pbp, "Request finished.");
1257 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0)
1258 G_RAID3_LOGREQ(1, pbp, "Verification error.");
1260 G_RAID3_LOGREQ(0, pbp, "Request failed.");
1262 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_MASK;
1263 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL)
1264 g_raid3_destroy_bio(sc, cbp);
1265 g_io_deliver(pbp, pbp->bio_error);
1269 g_raid3_done(struct bio *bp)
1271 struct g_raid3_softc *sc;
1273 sc = bp->bio_from->geom->softc;
1274 bp->bio_cflags |= G_RAID3_BIO_CFLAG_REGULAR;
1275 G_RAID3_LOGREQ(3, bp, "Regular request done (error=%d).", bp->bio_error);
1276 mtx_lock(&sc->sc_queue_mtx);
1277 bioq_insert_head(&sc->sc_queue, bp);
1278 mtx_unlock(&sc->sc_queue_mtx);
1280 wakeup(&sc->sc_queue);
1284 g_raid3_regular_request(struct bio *cbp)
1286 struct g_raid3_softc *sc;
1287 struct g_raid3_disk *disk;
1290 g_topology_assert_not();
1292 pbp = cbp->bio_parent;
1293 sc = pbp->bio_to->geom->softc;
1294 cbp->bio_from->index--;
1295 if (cbp->bio_cmd == BIO_WRITE)
1297 disk = cbp->bio_from->private;
1300 g_raid3_kill_consumer(sc, cbp->bio_from);
1301 g_topology_unlock();
1304 G_RAID3_LOGREQ(3, cbp, "Request finished.");
1306 KASSERT(pbp->bio_inbed <= pbp->bio_children,
1307 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
1308 pbp->bio_children));
1309 if (pbp->bio_inbed != pbp->bio_children)
1311 switch (pbp->bio_cmd) {
1313 g_raid3_gather(pbp);
1320 pbp->bio_completed = pbp->bio_length;
1321 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL) {
1322 if (cbp->bio_error == 0) {
1323 g_raid3_destroy_bio(sc, cbp);
1328 error = cbp->bio_error;
1329 else if (pbp->bio_error == 0) {
1331 * Next failed request, that's too many.
1333 pbp->bio_error = error;
1336 disk = cbp->bio_caller2;
1338 g_raid3_destroy_bio(sc, cbp);
1342 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
1343 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
1344 G_RAID3_LOGREQ(0, cbp,
1345 "Request failed (error=%d).",
1348 G_RAID3_LOGREQ(1, cbp,
1349 "Request failed (error=%d).",
1352 if (g_raid3_disconnect_on_failure &&
1353 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1354 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1355 g_raid3_event_send(disk,
1356 G_RAID3_DISK_STATE_DISCONNECTED,
1357 G_RAID3_EVENT_DONTWAIT);
1359 g_raid3_destroy_bio(sc, cbp);
1361 if (pbp->bio_error == 0)
1362 G_RAID3_LOGREQ(3, pbp, "Request finished.");
1364 G_RAID3_LOGREQ(0, pbp, "Request failed.");
1365 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_DEGRADED;
1366 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_NOPARITY;
1367 bioq_remove(&sc->sc_inflight, pbp);
1368 /* Release delayed sync requests if possible. */
1369 g_raid3_sync_release(sc);
1370 g_io_deliver(pbp, pbp->bio_error);
1377 g_raid3_sync_done(struct bio *bp)
1379 struct g_raid3_softc *sc;
1381 G_RAID3_LOGREQ(3, bp, "Synchronization request delivered.");
1382 sc = bp->bio_from->geom->softc;
1383 bp->bio_cflags |= G_RAID3_BIO_CFLAG_SYNC;
1384 mtx_lock(&sc->sc_queue_mtx);
1385 bioq_insert_head(&sc->sc_queue, bp);
1386 mtx_unlock(&sc->sc_queue_mtx);
1388 wakeup(&sc->sc_queue);
1392 g_raid3_flush(struct g_raid3_softc *sc, struct bio *bp)
1394 struct bio_queue_head queue;
1395 struct g_raid3_disk *disk;
1396 struct g_consumer *cp;
1401 for (i = 0; i < sc->sc_ndisks; i++) {
1402 disk = &sc->sc_disks[i];
1403 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
1405 cbp = g_clone_bio(bp);
1407 for (cbp = bioq_first(&queue); cbp != NULL;
1408 cbp = bioq_first(&queue)) {
1409 bioq_remove(&queue, cbp);
1412 if (bp->bio_error == 0)
1413 bp->bio_error = ENOMEM;
1414 g_io_deliver(bp, bp->bio_error);
1417 bioq_insert_tail(&queue, cbp);
1418 cbp->bio_done = g_std_done;
1419 cbp->bio_caller1 = disk;
1420 cbp->bio_to = disk->d_consumer->provider;
1422 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1423 bioq_remove(&queue, cbp);
1424 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1425 disk = cbp->bio_caller1;
1426 cbp->bio_caller1 = NULL;
1427 cp = disk->d_consumer;
1428 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1429 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1430 cp->acr, cp->acw, cp->ace));
1431 g_io_request(cbp, disk->d_consumer);
1436 g_raid3_start(struct bio *bp)
1438 struct g_raid3_softc *sc;
1440 sc = bp->bio_to->geom->softc;
1442 * If sc == NULL or there are no valid disks, provider's error
1443 * should be set and g_raid3_start() should not be called at all.
1445 KASSERT(sc != NULL && (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
1446 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE),
1447 ("Provider's error should be set (error=%d)(device=%s).",
1448 bp->bio_to->error, bp->bio_to->name));
1449 G_RAID3_LOGREQ(3, bp, "Request received.");
1451 switch (bp->bio_cmd) {
1457 g_raid3_flush(sc, bp);
1461 g_io_deliver(bp, EOPNOTSUPP);
1464 mtx_lock(&sc->sc_queue_mtx);
1465 bioq_insert_tail(&sc->sc_queue, bp);
1466 mtx_unlock(&sc->sc_queue_mtx);
1467 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1472 * Return TRUE if the given request is colliding with a in-progress
1473 * synchronization request.
1476 g_raid3_sync_collision(struct g_raid3_softc *sc, struct bio *bp)
1478 struct g_raid3_disk *disk;
1480 off_t rstart, rend, sstart, send;
1483 disk = sc->sc_syncdisk;
1486 rstart = bp->bio_offset;
1487 rend = bp->bio_offset + bp->bio_length;
1488 for (i = 0; i < g_raid3_syncreqs; i++) {
1489 sbp = disk->d_sync.ds_bios[i];
1492 sstart = sbp->bio_offset;
1493 send = sbp->bio_length;
1494 if (sbp->bio_cmd == BIO_WRITE) {
1495 sstart *= sc->sc_ndisks - 1;
1496 send *= sc->sc_ndisks - 1;
1499 if (rend > sstart && rstart < send)
1506 * Return TRUE if the given sync request is colliding with a in-progress regular
1510 g_raid3_regular_collision(struct g_raid3_softc *sc, struct bio *sbp)
1512 off_t rstart, rend, sstart, send;
1515 if (sc->sc_syncdisk == NULL)
1517 sstart = sbp->bio_offset;
1518 send = sstart + sbp->bio_length;
1519 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1520 rstart = bp->bio_offset;
1521 rend = bp->bio_offset + bp->bio_length;
1522 if (rend > sstart && rstart < send)
1529 * Puts request onto delayed queue.
1532 g_raid3_regular_delay(struct g_raid3_softc *sc, struct bio *bp)
1535 G_RAID3_LOGREQ(2, bp, "Delaying request.");
1536 bioq_insert_head(&sc->sc_regular_delayed, bp);
1540 * Puts synchronization request onto delayed queue.
1543 g_raid3_sync_delay(struct g_raid3_softc *sc, struct bio *bp)
1546 G_RAID3_LOGREQ(2, bp, "Delaying synchronization request.");
1547 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1551 * Releases delayed regular requests which don't collide anymore with sync
1555 g_raid3_regular_release(struct g_raid3_softc *sc)
1557 struct bio *bp, *bp2;
1559 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1560 if (g_raid3_sync_collision(sc, bp))
1562 bioq_remove(&sc->sc_regular_delayed, bp);
1563 G_RAID3_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1564 mtx_lock(&sc->sc_queue_mtx);
1565 bioq_insert_head(&sc->sc_queue, bp);
1568 * wakeup() is not needed, because this function is called from
1569 * the worker thread.
1571 wakeup(&sc->sc_queue);
1573 mtx_unlock(&sc->sc_queue_mtx);
1578 * Releases delayed sync requests which don't collide anymore with regular
1582 g_raid3_sync_release(struct g_raid3_softc *sc)
1584 struct bio *bp, *bp2;
1586 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1587 if (g_raid3_regular_collision(sc, bp))
1589 bioq_remove(&sc->sc_sync_delayed, bp);
1590 G_RAID3_LOGREQ(2, bp,
1591 "Releasing delayed synchronization request.");
1592 g_io_request(bp, bp->bio_from);
1597 * Handle synchronization requests.
1598 * Every synchronization request is two-steps process: first, READ request is
1599 * send to active provider and then WRITE request (with read data) to the provider
1600 * beeing synchronized. When WRITE is finished, new synchronization request is
1604 g_raid3_sync_request(struct bio *bp)
1606 struct g_raid3_softc *sc;
1607 struct g_raid3_disk *disk;
1609 bp->bio_from->index--;
1610 sc = bp->bio_from->geom->softc;
1611 disk = bp->bio_from->private;
1613 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1615 g_raid3_kill_consumer(sc, bp->bio_from);
1616 g_topology_unlock();
1617 free(bp->bio_data, M_RAID3);
1619 sx_xlock(&sc->sc_lock);
1624 * Synchronization request.
1626 switch (bp->bio_cmd) {
1629 struct g_consumer *cp;
1634 if (bp->bio_error != 0) {
1635 G_RAID3_LOGREQ(0, bp,
1636 "Synchronization request failed (error=%d).",
1641 G_RAID3_LOGREQ(3, bp, "Synchronization request finished.");
1642 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1643 dst = src = bp->bio_data;
1644 if (disk->d_no == sc->sc_ndisks - 1) {
1647 /* Parity component. */
1648 for (left = bp->bio_length; left > 0;
1649 left -= sc->sc_sectorsize) {
1650 bcopy(src, dst, atom);
1652 for (n = 1; n < sc->sc_ndisks - 1; n++) {
1653 g_raid3_xor(src, dst, atom);
1659 /* Regular component. */
1660 src += atom * disk->d_no;
1661 for (left = bp->bio_length; left > 0;
1662 left -= sc->sc_sectorsize) {
1663 bcopy(src, dst, atom);
1664 src += sc->sc_sectorsize;
1668 bp->bio_driver1 = bp->bio_driver2 = NULL;
1670 bp->bio_offset /= sc->sc_ndisks - 1;
1671 bp->bio_length /= sc->sc_ndisks - 1;
1672 bp->bio_cmd = BIO_WRITE;
1674 bp->bio_children = bp->bio_inbed = 0;
1675 cp = disk->d_consumer;
1676 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1677 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1678 cp->acr, cp->acw, cp->ace));
1680 g_io_request(bp, cp);
1685 struct g_raid3_disk_sync *sync;
1686 off_t boffset, moffset;
1690 if (bp->bio_error != 0) {
1691 G_RAID3_LOGREQ(0, bp,
1692 "Synchronization request failed (error=%d).",
1695 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1696 g_raid3_event_send(disk,
1697 G_RAID3_DISK_STATE_DISCONNECTED,
1698 G_RAID3_EVENT_DONTWAIT);
1701 G_RAID3_LOGREQ(3, bp, "Synchronization request finished.");
1702 sync = &disk->d_sync;
1703 if (sync->ds_offset == sc->sc_mediasize / (sc->sc_ndisks - 1) ||
1704 sync->ds_consumer == NULL ||
1705 (sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
1706 /* Don't send more synchronization requests. */
1707 sync->ds_inflight--;
1708 if (sync->ds_bios != NULL) {
1709 i = (int)(uintptr_t)bp->bio_caller1;
1710 sync->ds_bios[i] = NULL;
1712 free(bp->bio_data, M_RAID3);
1714 if (sync->ds_inflight > 0)
1716 if (sync->ds_consumer == NULL ||
1717 (sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
1721 * Disk up-to-date, activate it.
1723 g_raid3_event_send(disk, G_RAID3_DISK_STATE_ACTIVE,
1724 G_RAID3_EVENT_DONTWAIT);
1728 /* Send next synchronization request. */
1729 data = bp->bio_data;
1730 bzero(bp, sizeof(*bp));
1731 bp->bio_cmd = BIO_READ;
1732 bp->bio_offset = sync->ds_offset * (sc->sc_ndisks - 1);
1733 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1734 sync->ds_offset += bp->bio_length / (sc->sc_ndisks - 1);
1735 bp->bio_done = g_raid3_sync_done;
1736 bp->bio_data = data;
1737 bp->bio_from = sync->ds_consumer;
1738 bp->bio_to = sc->sc_provider;
1739 G_RAID3_LOGREQ(3, bp, "Sending synchronization request.");
1740 sync->ds_consumer->index++;
1742 * Delay the request if it is colliding with a regular request.
1744 if (g_raid3_regular_collision(sc, bp))
1745 g_raid3_sync_delay(sc, bp);
1747 g_io_request(bp, sync->ds_consumer);
1749 /* Release delayed requests if possible. */
1750 g_raid3_regular_release(sc);
1752 /* Find the smallest offset. */
1753 moffset = sc->sc_mediasize;
1754 for (i = 0; i < g_raid3_syncreqs; i++) {
1755 bp = sync->ds_bios[i];
1756 boffset = bp->bio_offset;
1757 if (bp->bio_cmd == BIO_WRITE)
1758 boffset *= sc->sc_ndisks - 1;
1759 if (boffset < moffset)
1762 if (sync->ds_offset_done + (MAXPHYS * 100) < moffset) {
1763 /* Update offset_done on every 100 blocks. */
1764 sync->ds_offset_done = moffset;
1765 g_raid3_update_metadata(disk);
1770 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1771 bp->bio_cmd, sc->sc_name));
1777 g_raid3_register_request(struct bio *pbp)
1779 struct g_raid3_softc *sc;
1780 struct g_raid3_disk *disk;
1781 struct g_consumer *cp;
1782 struct bio *cbp, *tmpbp;
1783 off_t offset, length;
1785 int round_robin, verify;
1788 sc = pbp->bio_to->geom->softc;
1789 if ((pbp->bio_cflags & G_RAID3_BIO_CFLAG_REGSYNC) != 0 &&
1790 sc->sc_syncdisk == NULL) {
1791 g_io_deliver(pbp, EIO);
1794 g_raid3_init_bio(pbp);
1795 length = pbp->bio_length / (sc->sc_ndisks - 1);
1796 offset = pbp->bio_offset / (sc->sc_ndisks - 1);
1797 round_robin = verify = 0;
1798 switch (pbp->bio_cmd) {
1800 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
1801 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1802 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_VERIFY;
1804 ndisks = sc->sc_ndisks;
1807 ndisks = sc->sc_ndisks - 1;
1809 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0 &&
1810 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1815 KASSERT(!round_robin || !verify,
1816 ("ROUND-ROBIN and VERIFY are mutually exclusive."));
1817 pbp->bio_driver2 = &sc->sc_disks[sc->sc_ndisks - 1];
1822 * Delay the request if it is colliding with a synchronization
1825 if (g_raid3_sync_collision(sc, pbp)) {
1826 g_raid3_regular_delay(sc, pbp);
1833 sc->sc_last_write = time_uptime;
1835 ndisks = sc->sc_ndisks;
1838 for (n = 0; n < ndisks; n++) {
1839 disk = &sc->sc_disks[n];
1840 cbp = g_raid3_clone_bio(sc, pbp);
1842 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL)
1843 g_raid3_destroy_bio(sc, cbp);
1845 * To prevent deadlock, we must run back up
1846 * with the ENOMEM for failed requests of any
1847 * of our consumers. Our own sync requests
1848 * can stick around, as they are finite.
1850 if ((pbp->bio_cflags &
1851 G_RAID3_BIO_CFLAG_REGULAR) != 0) {
1852 g_io_deliver(pbp, ENOMEM);
1857 cbp->bio_offset = offset;
1858 cbp->bio_length = length;
1859 cbp->bio_done = g_raid3_done;
1860 switch (pbp->bio_cmd) {
1862 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE) {
1864 * Replace invalid component with the parity
1867 disk = &sc->sc_disks[sc->sc_ndisks - 1];
1868 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1869 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1870 } else if (round_robin &&
1871 disk->d_no == sc->sc_round_robin) {
1873 * In round-robin mode skip one data component
1874 * and use parity component when reading.
1876 pbp->bio_driver2 = disk;
1877 disk = &sc->sc_disks[sc->sc_ndisks - 1];
1878 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1879 sc->sc_round_robin++;
1881 } else if (verify && disk->d_no == sc->sc_ndisks - 1) {
1882 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1887 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
1888 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
1889 if (n == ndisks - 1) {
1891 * Active parity component, mark it as such.
1894 G_RAID3_BIO_CFLAG_PARITY;
1897 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1898 if (n == ndisks - 1) {
1900 * Parity component is not connected,
1901 * so destroy its request.
1904 G_RAID3_BIO_PFLAG_NOPARITY;
1905 g_raid3_destroy_bio(sc, cbp);
1909 G_RAID3_BIO_CFLAG_NODISK;
1916 cbp->bio_caller2 = disk;
1918 switch (pbp->bio_cmd) {
1922 * If we are in round-robin mode and 'round_robin' is
1923 * still 1, it means, that we skipped parity component
1924 * for this read and must reset sc_round_robin field.
1926 sc->sc_round_robin = 0;
1928 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1929 disk = cbp->bio_caller2;
1930 cp = disk->d_consumer;
1931 cbp->bio_to = cp->provider;
1932 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1933 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1934 ("Consumer %s not opened (r%dw%de%d).",
1935 cp->provider->name, cp->acr, cp->acw, cp->ace));
1937 g_io_request(cbp, cp);
1943 * Put request onto inflight queue, so we can check if new
1944 * synchronization requests don't collide with it.
1946 bioq_insert_tail(&sc->sc_inflight, pbp);
1949 * Bump syncid on first write.
1951 if ((sc->sc_bump_id & G_RAID3_BUMP_SYNCID) != 0) {
1952 sc->sc_bump_id &= ~G_RAID3_BUMP_SYNCID;
1953 g_raid3_bump_syncid(sc);
1955 g_raid3_scatter(pbp);
1962 g_raid3_can_destroy(struct g_raid3_softc *sc)
1965 struct g_consumer *cp;
1967 g_topology_assert();
1969 if (gp->softc == NULL)
1971 LIST_FOREACH(cp, &gp->consumer, consumer) {
1972 if (g_raid3_is_busy(sc, cp))
1975 gp = sc->sc_sync.ds_geom;
1976 LIST_FOREACH(cp, &gp->consumer, consumer) {
1977 if (g_raid3_is_busy(sc, cp))
1980 G_RAID3_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1986 g_raid3_try_destroy(struct g_raid3_softc *sc)
1989 g_topology_assert_not();
1990 sx_assert(&sc->sc_lock, SX_XLOCKED);
1992 if (sc->sc_rootmount != NULL) {
1993 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1995 root_mount_rel(sc->sc_rootmount);
1996 sc->sc_rootmount = NULL;
2000 if (!g_raid3_can_destroy(sc)) {
2001 g_topology_unlock();
2004 sc->sc_geom->softc = NULL;
2005 sc->sc_sync.ds_geom->softc = NULL;
2006 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_WAIT) != 0) {
2007 g_topology_unlock();
2008 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__,
2010 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
2011 sx_xunlock(&sc->sc_lock);
2012 wakeup(&sc->sc_worker);
2013 sc->sc_worker = NULL;
2015 g_topology_unlock();
2016 g_raid3_destroy_device(sc);
2017 free(sc->sc_disks, M_RAID3);
2027 g_raid3_worker(void *arg)
2029 struct g_raid3_softc *sc;
2030 struct g_raid3_event *ep;
2035 thread_lock(curthread);
2036 sched_prio(curthread, PRIBIO);
2037 thread_unlock(curthread);
2039 sx_xlock(&sc->sc_lock);
2041 G_RAID3_DEBUG(5, "%s: Let's see...", __func__);
2043 * First take a look at events.
2044 * This is important to handle events before any I/O requests.
2046 ep = g_raid3_event_get(sc);
2048 g_raid3_event_remove(sc, ep);
2049 if ((ep->e_flags & G_RAID3_EVENT_DEVICE) != 0) {
2050 /* Update only device status. */
2052 "Running event for device %s.",
2055 g_raid3_update_device(sc, 1);
2057 /* Update disk status. */
2058 G_RAID3_DEBUG(3, "Running event for disk %s.",
2059 g_raid3_get_diskname(ep->e_disk));
2060 ep->e_error = g_raid3_update_disk(ep->e_disk,
2062 if (ep->e_error == 0)
2063 g_raid3_update_device(sc, 0);
2065 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0) {
2066 KASSERT(ep->e_error == 0,
2067 ("Error cannot be handled."));
2068 g_raid3_event_free(ep);
2070 ep->e_flags |= G_RAID3_EVENT_DONE;
2071 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__,
2073 mtx_lock(&sc->sc_events_mtx);
2075 mtx_unlock(&sc->sc_events_mtx);
2078 G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
2079 if (g_raid3_try_destroy(sc)) {
2080 curthread->td_pflags &= ~TDP_GEOM;
2081 G_RAID3_DEBUG(1, "Thread exiting.");
2085 G_RAID3_DEBUG(5, "%s: I'm here 1.", __func__);
2089 * Check if we can mark array as CLEAN and if we can't take
2090 * how much seconds should we wait.
2092 timeout = g_raid3_idle(sc, -1);
2096 /* Get first request from the queue. */
2097 mtx_lock(&sc->sc_queue_mtx);
2098 bp = bioq_first(&sc->sc_queue);
2101 G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
2102 mtx_unlock(&sc->sc_queue_mtx);
2103 if (g_raid3_try_destroy(sc)) {
2104 curthread->td_pflags &= ~TDP_GEOM;
2105 G_RAID3_DEBUG(1, "Thread exiting.");
2108 mtx_lock(&sc->sc_queue_mtx);
2110 sx_xunlock(&sc->sc_lock);
2112 * XXX: We can miss an event here, because an event
2113 * can be added without sx-device-lock and without
2114 * mtx-queue-lock. Maybe I should just stop using
2115 * dedicated mutex for events synchronization and
2116 * stick with the queue lock?
2117 * The event will hang here until next I/O request
2118 * or next event is received.
2120 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "r3:w1",
2122 sx_xlock(&sc->sc_lock);
2123 G_RAID3_DEBUG(5, "%s: I'm here 4.", __func__);
2127 bioq_remove(&sc->sc_queue, bp);
2128 mtx_unlock(&sc->sc_queue_mtx);
2130 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
2131 (bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC) != 0) {
2132 g_raid3_sync_request(bp); /* READ */
2133 } else if (bp->bio_to != sc->sc_provider) {
2134 if ((bp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR) != 0)
2135 g_raid3_regular_request(bp);
2136 else if ((bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC) != 0)
2137 g_raid3_sync_request(bp); /* WRITE */
2140 ("Invalid request cflags=0x%hhx to=%s.",
2141 bp->bio_cflags, bp->bio_to->name));
2143 } else if (g_raid3_register_request(bp) != 0) {
2144 mtx_lock(&sc->sc_queue_mtx);
2145 bioq_insert_head(&sc->sc_queue, bp);
2147 * We are short in memory, let see if there are finished
2148 * request we can free.
2150 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
2151 if (bp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR)
2155 * No finished regular request, so at least keep
2156 * synchronization running.
2158 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
2159 if (bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC)
2162 sx_xunlock(&sc->sc_lock);
2163 MSLEEP(&sc->sc_queue, &sc->sc_queue_mtx, PRIBIO | PDROP,
2164 "r3:lowmem", hz / 10);
2165 sx_xlock(&sc->sc_lock);
2167 G_RAID3_DEBUG(5, "%s: I'm here 9.", __func__);
2172 g_raid3_update_idle(struct g_raid3_softc *sc, struct g_raid3_disk *disk)
2175 sx_assert(&sc->sc_lock, SX_LOCKED);
2176 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
2178 if (!sc->sc_idle && (disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) == 0) {
2179 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as dirty.",
2180 g_raid3_get_diskname(disk), sc->sc_name);
2181 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
2182 } else if (sc->sc_idle &&
2183 (disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) != 0) {
2184 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as clean.",
2185 g_raid3_get_diskname(disk), sc->sc_name);
2186 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2191 g_raid3_sync_start(struct g_raid3_softc *sc)
2193 struct g_raid3_disk *disk;
2194 struct g_consumer *cp;
2199 g_topology_assert_not();
2200 sx_assert(&sc->sc_lock, SX_XLOCKED);
2202 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED,
2203 ("Device not in DEGRADED state (%s, %u).", sc->sc_name,
2205 KASSERT(sc->sc_syncdisk == NULL, ("Syncdisk is not NULL (%s, %u).",
2206 sc->sc_name, sc->sc_state));
2208 for (n = 0; n < sc->sc_ndisks; n++) {
2209 if (sc->sc_disks[n].d_state != G_RAID3_DISK_STATE_SYNCHRONIZING)
2211 disk = &sc->sc_disks[n];
2217 sx_xunlock(&sc->sc_lock);
2219 cp = g_new_consumer(sc->sc_sync.ds_geom);
2220 error = g_attach(cp, sc->sc_provider);
2222 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
2223 error = g_access(cp, 1, 0, 0);
2224 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
2225 g_topology_unlock();
2226 sx_xlock(&sc->sc_lock);
2228 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
2229 g_raid3_get_diskname(disk));
2230 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) == 0)
2231 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
2232 KASSERT(disk->d_sync.ds_consumer == NULL,
2233 ("Sync consumer already exists (device=%s, disk=%s).",
2234 sc->sc_name, g_raid3_get_diskname(disk)));
2236 disk->d_sync.ds_consumer = cp;
2237 disk->d_sync.ds_consumer->private = disk;
2238 disk->d_sync.ds_consumer->index = 0;
2239 sc->sc_syncdisk = disk;
2242 * Allocate memory for synchronization bios and initialize them.
2244 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_raid3_syncreqs,
2246 for (n = 0; n < g_raid3_syncreqs; n++) {
2248 disk->d_sync.ds_bios[n] = bp;
2249 bp->bio_parent = NULL;
2250 bp->bio_cmd = BIO_READ;
2251 bp->bio_data = malloc(MAXPHYS, M_RAID3, M_WAITOK);
2253 bp->bio_offset = disk->d_sync.ds_offset * (sc->sc_ndisks - 1);
2254 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
2255 disk->d_sync.ds_offset += bp->bio_length / (sc->sc_ndisks - 1);
2256 bp->bio_done = g_raid3_sync_done;
2257 bp->bio_from = disk->d_sync.ds_consumer;
2258 bp->bio_to = sc->sc_provider;
2259 bp->bio_caller1 = (void *)(uintptr_t)n;
2262 /* Set the number of in-flight synchronization requests. */
2263 disk->d_sync.ds_inflight = g_raid3_syncreqs;
2266 * Fire off first synchronization requests.
2268 for (n = 0; n < g_raid3_syncreqs; n++) {
2269 bp = disk->d_sync.ds_bios[n];
2270 G_RAID3_LOGREQ(3, bp, "Sending synchronization request.");
2271 disk->d_sync.ds_consumer->index++;
2273 * Delay the request if it is colliding with a regular request.
2275 if (g_raid3_regular_collision(sc, bp))
2276 g_raid3_sync_delay(sc, bp);
2278 g_io_request(bp, disk->d_sync.ds_consumer);
2283 * Stop synchronization process.
2284 * type: 0 - synchronization finished
2285 * 1 - synchronization stopped
2288 g_raid3_sync_stop(struct g_raid3_softc *sc, int type)
2290 struct g_raid3_disk *disk;
2291 struct g_consumer *cp;
2293 g_topology_assert_not();
2294 sx_assert(&sc->sc_lock, SX_LOCKED);
2296 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED,
2297 ("Device not in DEGRADED state (%s, %u).", sc->sc_name,
2299 disk = sc->sc_syncdisk;
2300 sc->sc_syncdisk = NULL;
2301 KASSERT(disk != NULL, ("No disk was synchronized (%s).", sc->sc_name));
2302 KASSERT(disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2303 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2304 g_raid3_disk_state2str(disk->d_state)));
2305 if (disk->d_sync.ds_consumer == NULL)
2309 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2310 sc->sc_name, g_raid3_get_diskname(disk));
2311 } else /* if (type == 1) */ {
2312 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2313 sc->sc_name, g_raid3_get_diskname(disk));
2315 free(disk->d_sync.ds_bios, M_RAID3);
2316 disk->d_sync.ds_bios = NULL;
2317 cp = disk->d_sync.ds_consumer;
2318 disk->d_sync.ds_consumer = NULL;
2319 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2320 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2322 g_raid3_kill_consumer(sc, cp);
2323 g_topology_unlock();
2324 sx_xlock(&sc->sc_lock);
2328 g_raid3_launch_provider(struct g_raid3_softc *sc)
2330 struct g_provider *pp;
2331 struct g_raid3_disk *disk;
2334 sx_assert(&sc->sc_lock, SX_LOCKED);
2337 pp = g_new_providerf(sc->sc_geom, "raid3/%s", sc->sc_name);
2338 pp->mediasize = sc->sc_mediasize;
2339 pp->sectorsize = sc->sc_sectorsize;
2341 pp->stripeoffset = 0;
2342 for (n = 0; n < sc->sc_ndisks; n++) {
2343 disk = &sc->sc_disks[n];
2344 if (disk->d_consumer && disk->d_consumer->provider &&
2345 disk->d_consumer->provider->stripesize > pp->stripesize) {
2346 pp->stripesize = disk->d_consumer->provider->stripesize;
2347 pp->stripeoffset = disk->d_consumer->provider->stripeoffset;
2350 pp->stripesize *= sc->sc_ndisks - 1;
2351 pp->stripeoffset *= sc->sc_ndisks - 1;
2352 sc->sc_provider = pp;
2353 g_error_provider(pp, 0);
2354 g_topology_unlock();
2355 G_RAID3_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2356 g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE), sc->sc_ndisks);
2358 if (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED)
2359 g_raid3_sync_start(sc);
2363 g_raid3_destroy_provider(struct g_raid3_softc *sc)
2367 g_topology_assert_not();
2368 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2372 g_error_provider(sc->sc_provider, ENXIO);
2373 mtx_lock(&sc->sc_queue_mtx);
2374 while ((bp = bioq_first(&sc->sc_queue)) != NULL) {
2375 bioq_remove(&sc->sc_queue, bp);
2376 g_io_deliver(bp, ENXIO);
2378 mtx_unlock(&sc->sc_queue_mtx);
2379 G_RAID3_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2380 sc->sc_provider->name);
2381 sc->sc_provider->flags |= G_PF_WITHER;
2382 g_orphan_provider(sc->sc_provider, ENXIO);
2383 g_topology_unlock();
2384 sc->sc_provider = NULL;
2385 if (sc->sc_syncdisk != NULL)
2386 g_raid3_sync_stop(sc, 1);
2390 g_raid3_go(void *arg)
2392 struct g_raid3_softc *sc;
2395 G_RAID3_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2396 g_raid3_event_send(sc, 0,
2397 G_RAID3_EVENT_DONTWAIT | G_RAID3_EVENT_DEVICE);
2401 g_raid3_determine_state(struct g_raid3_disk *disk)
2403 struct g_raid3_softc *sc;
2407 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2408 if ((disk->d_flags &
2409 G_RAID3_DISK_FLAG_SYNCHRONIZING) == 0) {
2410 /* Disk does not need synchronization. */
2411 state = G_RAID3_DISK_STATE_ACTIVE;
2414 G_RAID3_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2416 G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) {
2418 * We can start synchronization from
2419 * the stored offset.
2421 state = G_RAID3_DISK_STATE_SYNCHRONIZING;
2423 state = G_RAID3_DISK_STATE_STALE;
2426 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2428 * Reset all synchronization data for this disk,
2429 * because if it even was synchronized, it was
2430 * synchronized to disks with different syncid.
2432 disk->d_flags |= G_RAID3_DISK_FLAG_SYNCHRONIZING;
2433 disk->d_sync.ds_offset = 0;
2434 disk->d_sync.ds_offset_done = 0;
2435 disk->d_sync.ds_syncid = sc->sc_syncid;
2436 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2437 (disk->d_flags & G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) {
2438 state = G_RAID3_DISK_STATE_SYNCHRONIZING;
2440 state = G_RAID3_DISK_STATE_STALE;
2442 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2444 * Not good, NOT GOOD!
2445 * It means that device was started on stale disks
2446 * and more fresh disk just arrive.
2447 * If there were writes, device is broken, sorry.
2448 * I think the best choice here is don't touch
2449 * this disk and inform the user loudly.
2451 G_RAID3_DEBUG(0, "Device %s was started before the freshest "
2452 "disk (%s) arrives!! It will not be connected to the "
2453 "running device.", sc->sc_name,
2454 g_raid3_get_diskname(disk));
2455 g_raid3_destroy_disk(disk);
2456 state = G_RAID3_DISK_STATE_NONE;
2457 /* Return immediately, because disk was destroyed. */
2460 G_RAID3_DEBUG(3, "State for %s disk: %s.",
2461 g_raid3_get_diskname(disk), g_raid3_disk_state2str(state));
2466 * Update device state.
2469 g_raid3_update_device(struct g_raid3_softc *sc, boolean_t force)
2471 struct g_raid3_disk *disk;
2474 sx_assert(&sc->sc_lock, SX_XLOCKED);
2476 switch (sc->sc_state) {
2477 case G_RAID3_DEVICE_STATE_STARTING:
2479 u_int n, ndirty, ndisks, genid, syncid;
2481 KASSERT(sc->sc_provider == NULL,
2482 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2484 * Are we ready? We are, if all disks are connected or
2485 * one disk is missing and 'force' is true.
2487 if (g_raid3_ndisks(sc, -1) + force == sc->sc_ndisks) {
2489 callout_drain(&sc->sc_callout);
2493 * Timeout expired, so destroy device.
2495 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2496 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p",
2497 __LINE__, sc->sc_rootmount);
2498 root_mount_rel(sc->sc_rootmount);
2499 sc->sc_rootmount = NULL;
2505 * Find the biggest genid.
2508 for (n = 0; n < sc->sc_ndisks; n++) {
2509 disk = &sc->sc_disks[n];
2510 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2512 if (disk->d_genid > genid)
2513 genid = disk->d_genid;
2515 sc->sc_genid = genid;
2517 * Remove all disks without the biggest genid.
2519 for (n = 0; n < sc->sc_ndisks; n++) {
2520 disk = &sc->sc_disks[n];
2521 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2523 if (disk->d_genid < genid) {
2525 "Component %s (device %s) broken, skipping.",
2526 g_raid3_get_diskname(disk), sc->sc_name);
2527 g_raid3_destroy_disk(disk);
2532 * There must be at least 'sc->sc_ndisks - 1' components
2533 * with the same syncid and without SYNCHRONIZING flag.
2537 * Find the biggest syncid, number of valid components and
2538 * number of dirty components.
2540 ndirty = ndisks = syncid = 0;
2541 for (n = 0; n < sc->sc_ndisks; n++) {
2542 disk = &sc->sc_disks[n];
2543 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2545 if ((disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) != 0)
2547 if (disk->d_sync.ds_syncid > syncid) {
2548 syncid = disk->d_sync.ds_syncid;
2550 } else if (disk->d_sync.ds_syncid < syncid) {
2553 if ((disk->d_flags &
2554 G_RAID3_DISK_FLAG_SYNCHRONIZING) != 0) {
2560 * Do we have enough valid components?
2562 if (ndisks + 1 < sc->sc_ndisks) {
2564 "Device %s is broken, too few valid components.",
2566 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2570 * If there is one DIRTY component and all disks are present,
2571 * mark it for synchronization. If there is more than one DIRTY
2572 * component, mark parity component for synchronization.
2574 if (ndisks == sc->sc_ndisks && ndirty == 1) {
2575 for (n = 0; n < sc->sc_ndisks; n++) {
2576 disk = &sc->sc_disks[n];
2577 if ((disk->d_flags &
2578 G_RAID3_DISK_FLAG_DIRTY) == 0) {
2582 G_RAID3_DISK_FLAG_SYNCHRONIZING;
2584 } else if (ndisks == sc->sc_ndisks && ndirty > 1) {
2585 disk = &sc->sc_disks[sc->sc_ndisks - 1];
2586 disk->d_flags |= G_RAID3_DISK_FLAG_SYNCHRONIZING;
2589 sc->sc_syncid = syncid;
2591 /* Remember to bump syncid on first write. */
2592 sc->sc_bump_id |= G_RAID3_BUMP_SYNCID;
2594 if (ndisks == sc->sc_ndisks)
2595 state = G_RAID3_DEVICE_STATE_COMPLETE;
2596 else /* if (ndisks == sc->sc_ndisks - 1) */
2597 state = G_RAID3_DEVICE_STATE_DEGRADED;
2598 G_RAID3_DEBUG(1, "Device %s state changed from %s to %s.",
2599 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2600 g_raid3_device_state2str(state));
2601 sc->sc_state = state;
2602 for (n = 0; n < sc->sc_ndisks; n++) {
2603 disk = &sc->sc_disks[n];
2604 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2606 state = g_raid3_determine_state(disk);
2607 g_raid3_event_send(disk, state, G_RAID3_EVENT_DONTWAIT);
2608 if (state == G_RAID3_DISK_STATE_STALE)
2609 sc->sc_bump_id |= G_RAID3_BUMP_SYNCID;
2613 case G_RAID3_DEVICE_STATE_DEGRADED:
2615 * Genid need to be bumped immediately, so do it here.
2617 if ((sc->sc_bump_id & G_RAID3_BUMP_GENID) != 0) {
2618 sc->sc_bump_id &= ~G_RAID3_BUMP_GENID;
2619 g_raid3_bump_genid(sc);
2622 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NEW) > 0)
2624 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) <
2625 sc->sc_ndisks - 1) {
2626 if (sc->sc_provider != NULL)
2627 g_raid3_destroy_provider(sc);
2628 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2631 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) ==
2633 state = G_RAID3_DEVICE_STATE_COMPLETE;
2635 "Device %s state changed from %s to %s.",
2636 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2637 g_raid3_device_state2str(state));
2638 sc->sc_state = state;
2640 if (sc->sc_provider == NULL)
2641 g_raid3_launch_provider(sc);
2642 if (sc->sc_rootmount != NULL) {
2643 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2645 root_mount_rel(sc->sc_rootmount);
2646 sc->sc_rootmount = NULL;
2649 case G_RAID3_DEVICE_STATE_COMPLETE:
2651 * Genid need to be bumped immediately, so do it here.
2653 if ((sc->sc_bump_id & G_RAID3_BUMP_GENID) != 0) {
2654 sc->sc_bump_id &= ~G_RAID3_BUMP_GENID;
2655 g_raid3_bump_genid(sc);
2658 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NEW) > 0)
2660 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) >=
2662 ("Too few ACTIVE components in COMPLETE state (device %s).",
2664 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) ==
2665 sc->sc_ndisks - 1) {
2666 state = G_RAID3_DEVICE_STATE_DEGRADED;
2668 "Device %s state changed from %s to %s.",
2669 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2670 g_raid3_device_state2str(state));
2671 sc->sc_state = state;
2673 if (sc->sc_provider == NULL)
2674 g_raid3_launch_provider(sc);
2675 if (sc->sc_rootmount != NULL) {
2676 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2678 root_mount_rel(sc->sc_rootmount);
2679 sc->sc_rootmount = NULL;
2683 KASSERT(1 == 0, ("Wrong device state (%s, %s).", sc->sc_name,
2684 g_raid3_device_state2str(sc->sc_state)));
2690 * Update disk state and device state if needed.
2692 #define DISK_STATE_CHANGED() G_RAID3_DEBUG(1, \
2693 "Disk %s state changed from %s to %s (device %s).", \
2694 g_raid3_get_diskname(disk), \
2695 g_raid3_disk_state2str(disk->d_state), \
2696 g_raid3_disk_state2str(state), sc->sc_name)
2698 g_raid3_update_disk(struct g_raid3_disk *disk, u_int state)
2700 struct g_raid3_softc *sc;
2703 sx_assert(&sc->sc_lock, SX_XLOCKED);
2706 G_RAID3_DEBUG(3, "Changing disk %s state from %s to %s.",
2707 g_raid3_get_diskname(disk), g_raid3_disk_state2str(disk->d_state),
2708 g_raid3_disk_state2str(state));
2710 case G_RAID3_DISK_STATE_NEW:
2712 * Possible scenarios:
2713 * 1. New disk arrive.
2715 /* Previous state should be NONE. */
2716 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NONE,
2717 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2718 g_raid3_disk_state2str(disk->d_state)));
2719 DISK_STATE_CHANGED();
2721 disk->d_state = state;
2722 G_RAID3_DEBUG(1, "Device %s: provider %s detected.",
2723 sc->sc_name, g_raid3_get_diskname(disk));
2724 if (sc->sc_state == G_RAID3_DEVICE_STATE_STARTING)
2726 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2727 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2728 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2729 g_raid3_device_state2str(sc->sc_state),
2730 g_raid3_get_diskname(disk),
2731 g_raid3_disk_state2str(disk->d_state)));
2732 state = g_raid3_determine_state(disk);
2733 if (state != G_RAID3_DISK_STATE_NONE)
2736 case G_RAID3_DISK_STATE_ACTIVE:
2738 * Possible scenarios:
2739 * 1. New disk does not need synchronization.
2740 * 2. Synchronization process finished successfully.
2742 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2743 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2744 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2745 g_raid3_device_state2str(sc->sc_state),
2746 g_raid3_get_diskname(disk),
2747 g_raid3_disk_state2str(disk->d_state)));
2748 /* Previous state should be NEW or SYNCHRONIZING. */
2749 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW ||
2750 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2751 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2752 g_raid3_disk_state2str(disk->d_state)));
2753 DISK_STATE_CHANGED();
2755 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
2756 disk->d_flags &= ~G_RAID3_DISK_FLAG_SYNCHRONIZING;
2757 disk->d_flags &= ~G_RAID3_DISK_FLAG_FORCE_SYNC;
2758 g_raid3_sync_stop(sc, 0);
2760 disk->d_state = state;
2761 disk->d_sync.ds_offset = 0;
2762 disk->d_sync.ds_offset_done = 0;
2763 g_raid3_update_idle(sc, disk);
2764 g_raid3_update_metadata(disk);
2765 G_RAID3_DEBUG(1, "Device %s: provider %s activated.",
2766 sc->sc_name, g_raid3_get_diskname(disk));
2768 case G_RAID3_DISK_STATE_STALE:
2770 * Possible scenarios:
2771 * 1. Stale disk was connected.
2773 /* Previous state should be NEW. */
2774 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2775 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2776 g_raid3_disk_state2str(disk->d_state)));
2777 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2778 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2779 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2780 g_raid3_device_state2str(sc->sc_state),
2781 g_raid3_get_diskname(disk),
2782 g_raid3_disk_state2str(disk->d_state)));
2784 * STALE state is only possible if device is marked
2787 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0,
2788 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2789 g_raid3_device_state2str(sc->sc_state),
2790 g_raid3_get_diskname(disk),
2791 g_raid3_disk_state2str(disk->d_state)));
2792 DISK_STATE_CHANGED();
2794 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2795 disk->d_state = state;
2796 g_raid3_update_metadata(disk);
2797 G_RAID3_DEBUG(0, "Device %s: provider %s is stale.",
2798 sc->sc_name, g_raid3_get_diskname(disk));
2800 case G_RAID3_DISK_STATE_SYNCHRONIZING:
2802 * Possible scenarios:
2803 * 1. Disk which needs synchronization was connected.
2805 /* Previous state should be NEW. */
2806 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2807 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2808 g_raid3_disk_state2str(disk->d_state)));
2809 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2810 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2811 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2812 g_raid3_device_state2str(sc->sc_state),
2813 g_raid3_get_diskname(disk),
2814 g_raid3_disk_state2str(disk->d_state)));
2815 DISK_STATE_CHANGED();
2817 if (disk->d_state == G_RAID3_DISK_STATE_NEW)
2818 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2819 disk->d_state = state;
2820 if (sc->sc_provider != NULL) {
2821 g_raid3_sync_start(sc);
2822 g_raid3_update_metadata(disk);
2825 case G_RAID3_DISK_STATE_DISCONNECTED:
2827 * Possible scenarios:
2828 * 1. Device wasn't running yet, but disk disappear.
2829 * 2. Disk was active and disapppear.
2830 * 3. Disk disappear during synchronization process.
2832 if (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2833 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
2835 * Previous state should be ACTIVE, STALE or
2838 KASSERT(disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
2839 disk->d_state == G_RAID3_DISK_STATE_STALE ||
2840 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2841 ("Wrong disk state (%s, %s).",
2842 g_raid3_get_diskname(disk),
2843 g_raid3_disk_state2str(disk->d_state)));
2844 } else if (sc->sc_state == G_RAID3_DEVICE_STATE_STARTING) {
2845 /* Previous state should be NEW. */
2846 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2847 ("Wrong disk state (%s, %s).",
2848 g_raid3_get_diskname(disk),
2849 g_raid3_disk_state2str(disk->d_state)));
2851 * Reset bumping syncid if disk disappeared in STARTING
2854 if ((sc->sc_bump_id & G_RAID3_BUMP_SYNCID) != 0)
2855 sc->sc_bump_id &= ~G_RAID3_BUMP_SYNCID;
2858 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2860 g_raid3_device_state2str(sc->sc_state),
2861 g_raid3_get_diskname(disk),
2862 g_raid3_disk_state2str(disk->d_state)));
2865 DISK_STATE_CHANGED();
2866 G_RAID3_DEBUG(0, "Device %s: provider %s disconnected.",
2867 sc->sc_name, g_raid3_get_diskname(disk));
2869 g_raid3_destroy_disk(disk);
2872 KASSERT(1 == 0, ("Unknown state (%u).", state));
2877 #undef DISK_STATE_CHANGED
2880 g_raid3_read_metadata(struct g_consumer *cp, struct g_raid3_metadata *md)
2882 struct g_provider *pp;
2886 g_topology_assert();
2888 error = g_access(cp, 1, 0, 0);
2892 g_topology_unlock();
2893 /* Metadata are stored on last sector. */
2894 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2897 g_access(cp, -1, 0, 0);
2899 G_RAID3_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2900 cp->provider->name, error);
2904 /* Decode metadata. */
2905 error = raid3_metadata_decode(buf, md);
2907 if (strcmp(md->md_magic, G_RAID3_MAGIC) != 0)
2909 if (md->md_version > G_RAID3_VERSION) {
2911 "Kernel module is too old to handle metadata from %s.",
2912 cp->provider->name);
2916 G_RAID3_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2917 cp->provider->name);
2920 if (md->md_sectorsize > MAXPHYS) {
2921 G_RAID3_DEBUG(0, "The blocksize is too big.");
2929 g_raid3_check_metadata(struct g_raid3_softc *sc, struct g_provider *pp,
2930 struct g_raid3_metadata *md)
2933 if (md->md_no >= sc->sc_ndisks) {
2934 G_RAID3_DEBUG(1, "Invalid disk %s number (no=%u), skipping.",
2935 pp->name, md->md_no);
2938 if (sc->sc_disks[md->md_no].d_state != G_RAID3_DISK_STATE_NODISK) {
2939 G_RAID3_DEBUG(1, "Disk %s (no=%u) already exists, skipping.",
2940 pp->name, md->md_no);
2943 if (md->md_all != sc->sc_ndisks) {
2945 "Invalid '%s' field on disk %s (device %s), skipping.",
2946 "md_all", pp->name, sc->sc_name);
2949 if ((md->md_mediasize % md->md_sectorsize) != 0) {
2950 G_RAID3_DEBUG(1, "Invalid metadata (mediasize %% sectorsize != "
2951 "0) on disk %s (device %s), skipping.", pp->name,
2955 if (md->md_mediasize != sc->sc_mediasize) {
2957 "Invalid '%s' field on disk %s (device %s), skipping.",
2958 "md_mediasize", pp->name, sc->sc_name);
2961 if ((md->md_mediasize % (sc->sc_ndisks - 1)) != 0) {
2963 "Invalid '%s' field on disk %s (device %s), skipping.",
2964 "md_mediasize", pp->name, sc->sc_name);
2967 if ((sc->sc_mediasize / (sc->sc_ndisks - 1)) > pp->mediasize) {
2969 "Invalid size of disk %s (device %s), skipping.", pp->name,
2973 if ((md->md_sectorsize / pp->sectorsize) < sc->sc_ndisks - 1) {
2975 "Invalid '%s' field on disk %s (device %s), skipping.",
2976 "md_sectorsize", pp->name, sc->sc_name);
2979 if (md->md_sectorsize != sc->sc_sectorsize) {
2981 "Invalid '%s' field on disk %s (device %s), skipping.",
2982 "md_sectorsize", pp->name, sc->sc_name);
2985 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2987 "Invalid sector size of disk %s (device %s), skipping.",
2988 pp->name, sc->sc_name);
2991 if ((md->md_mflags & ~G_RAID3_DEVICE_FLAG_MASK) != 0) {
2993 "Invalid device flags on disk %s (device %s), skipping.",
2994 pp->name, sc->sc_name);
2997 if ((md->md_mflags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
2998 (md->md_mflags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) {
3000 * VERIFY and ROUND-ROBIN options are mutally exclusive.
3002 G_RAID3_DEBUG(1, "Both VERIFY and ROUND-ROBIN flags exist on "
3003 "disk %s (device %s), skipping.", pp->name, sc->sc_name);
3006 if ((md->md_dflags & ~G_RAID3_DISK_FLAG_MASK) != 0) {
3008 "Invalid disk flags on disk %s (device %s), skipping.",
3009 pp->name, sc->sc_name);
3016 g_raid3_add_disk(struct g_raid3_softc *sc, struct g_provider *pp,
3017 struct g_raid3_metadata *md)
3019 struct g_raid3_disk *disk;
3022 g_topology_assert_not();
3023 G_RAID3_DEBUG(2, "Adding disk %s.", pp->name);
3025 error = g_raid3_check_metadata(sc, pp, md);
3028 if (sc->sc_state != G_RAID3_DEVICE_STATE_STARTING &&
3029 md->md_genid < sc->sc_genid) {
3030 G_RAID3_DEBUG(0, "Component %s (device %s) broken, skipping.",
3031 pp->name, sc->sc_name);
3034 disk = g_raid3_init_disk(sc, pp, md, &error);
3037 error = g_raid3_event_send(disk, G_RAID3_DISK_STATE_NEW,
3038 G_RAID3_EVENT_WAIT);
3041 if (md->md_version < G_RAID3_VERSION) {
3042 G_RAID3_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
3043 pp->name, md->md_version, G_RAID3_VERSION);
3044 g_raid3_update_metadata(disk);
3050 g_raid3_destroy_delayed(void *arg, int flag)
3052 struct g_raid3_softc *sc;
3055 if (flag == EV_CANCEL) {
3056 G_RAID3_DEBUG(1, "Destroying canceled.");
3060 g_topology_unlock();
3061 sx_xlock(&sc->sc_lock);
3062 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) == 0,
3063 ("DESTROY flag set on %s.", sc->sc_name));
3064 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROYING) != 0,
3065 ("DESTROYING flag not set on %s.", sc->sc_name));
3066 G_RAID3_DEBUG(0, "Destroying %s (delayed).", sc->sc_name);
3067 error = g_raid3_destroy(sc, G_RAID3_DESTROY_SOFT);
3069 G_RAID3_DEBUG(0, "Cannot destroy %s.", sc->sc_name);
3070 sx_xunlock(&sc->sc_lock);
3076 g_raid3_access(struct g_provider *pp, int acr, int acw, int ace)
3078 struct g_raid3_softc *sc;
3079 int dcr, dcw, dce, error = 0;
3081 g_topology_assert();
3082 G_RAID3_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
3085 sc = pp->geom->softc;
3086 if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
3088 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
3090 dcr = pp->acr + acr;
3091 dcw = pp->acw + acw;
3092 dce = pp->ace + ace;
3094 g_topology_unlock();
3095 sx_xlock(&sc->sc_lock);
3096 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0 ||
3097 g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) < sc->sc_ndisks - 1) {
3098 if (acr > 0 || acw > 0 || ace > 0)
3103 g_raid3_idle(sc, dcw);
3104 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROYING) != 0) {
3105 if (acr > 0 || acw > 0 || ace > 0) {
3109 if (dcr == 0 && dcw == 0 && dce == 0) {
3110 g_post_event(g_raid3_destroy_delayed, sc, M_WAITOK,
3115 sx_xunlock(&sc->sc_lock);
3120 static struct g_geom *
3121 g_raid3_create(struct g_class *mp, const struct g_raid3_metadata *md)
3123 struct g_raid3_softc *sc;
3128 g_topology_assert();
3129 G_RAID3_DEBUG(1, "Creating device %s (id=%u).", md->md_name, md->md_id);
3131 /* One disk is minimum. */
3137 gp = g_new_geomf(mp, "%s", md->md_name);
3138 sc = malloc(sizeof(*sc), M_RAID3, M_WAITOK | M_ZERO);
3139 sc->sc_disks = malloc(sizeof(struct g_raid3_disk) * md->md_all, M_RAID3,
3141 gp->start = g_raid3_start;
3142 gp->orphan = g_raid3_orphan;
3143 gp->access = g_raid3_access;
3144 gp->dumpconf = g_raid3_dumpconf;
3146 sc->sc_id = md->md_id;
3147 sc->sc_mediasize = md->md_mediasize;
3148 sc->sc_sectorsize = md->md_sectorsize;
3149 sc->sc_ndisks = md->md_all;
3150 sc->sc_round_robin = 0;
3151 sc->sc_flags = md->md_mflags;
3154 sc->sc_last_write = time_uptime;
3156 for (n = 0; n < sc->sc_ndisks; n++) {
3157 sc->sc_disks[n].d_softc = sc;
3158 sc->sc_disks[n].d_no = n;
3159 sc->sc_disks[n].d_state = G_RAID3_DISK_STATE_NODISK;
3161 sx_init(&sc->sc_lock, "graid3:lock");
3162 bioq_init(&sc->sc_queue);
3163 mtx_init(&sc->sc_queue_mtx, "graid3:queue", NULL, MTX_DEF);
3164 bioq_init(&sc->sc_regular_delayed);
3165 bioq_init(&sc->sc_inflight);
3166 bioq_init(&sc->sc_sync_delayed);
3167 TAILQ_INIT(&sc->sc_events);
3168 mtx_init(&sc->sc_events_mtx, "graid3:events", NULL, MTX_DEF);
3169 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
3170 sc->sc_state = G_RAID3_DEVICE_STATE_STARTING;
3173 sc->sc_provider = NULL;
3175 * Synchronization geom.
3177 gp = g_new_geomf(mp, "%s.sync", md->md_name);
3179 gp->orphan = g_raid3_orphan;
3180 sc->sc_sync.ds_geom = gp;
3182 if (!g_raid3_use_malloc) {
3183 sc->sc_zones[G_RAID3_ZONE_64K].sz_zone = uma_zcreate("gr3:64k",
3184 65536, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3186 sc->sc_zones[G_RAID3_ZONE_64K].sz_inuse = 0;
3187 sc->sc_zones[G_RAID3_ZONE_64K].sz_max = g_raid3_n64k;
3188 sc->sc_zones[G_RAID3_ZONE_64K].sz_requested =
3189 sc->sc_zones[G_RAID3_ZONE_64K].sz_failed = 0;
3190 sc->sc_zones[G_RAID3_ZONE_16K].sz_zone = uma_zcreate("gr3:16k",
3191 16384, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3193 sc->sc_zones[G_RAID3_ZONE_16K].sz_inuse = 0;
3194 sc->sc_zones[G_RAID3_ZONE_16K].sz_max = g_raid3_n16k;
3195 sc->sc_zones[G_RAID3_ZONE_16K].sz_requested =
3196 sc->sc_zones[G_RAID3_ZONE_16K].sz_failed = 0;
3197 sc->sc_zones[G_RAID3_ZONE_4K].sz_zone = uma_zcreate("gr3:4k",
3198 4096, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3200 sc->sc_zones[G_RAID3_ZONE_4K].sz_inuse = 0;
3201 sc->sc_zones[G_RAID3_ZONE_4K].sz_max = g_raid3_n4k;
3202 sc->sc_zones[G_RAID3_ZONE_4K].sz_requested =
3203 sc->sc_zones[G_RAID3_ZONE_4K].sz_failed = 0;
3206 error = kproc_create(g_raid3_worker, sc, &sc->sc_worker, 0, 0,
3207 "g_raid3 %s", md->md_name);
3209 G_RAID3_DEBUG(1, "Cannot create kernel thread for %s.",
3211 if (!g_raid3_use_malloc) {
3212 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_64K].sz_zone);
3213 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_16K].sz_zone);
3214 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_4K].sz_zone);
3216 g_destroy_geom(sc->sc_sync.ds_geom);
3217 mtx_destroy(&sc->sc_events_mtx);
3218 mtx_destroy(&sc->sc_queue_mtx);
3219 sx_destroy(&sc->sc_lock);
3220 g_destroy_geom(sc->sc_geom);
3221 free(sc->sc_disks, M_RAID3);
3226 G_RAID3_DEBUG(1, "Device %s created (%u components, id=%u).",
3227 sc->sc_name, sc->sc_ndisks, sc->sc_id);
3229 sc->sc_rootmount = root_mount_hold("GRAID3");
3230 G_RAID3_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
3235 timeout = atomic_load_acq_int(&g_raid3_timeout);
3236 callout_reset(&sc->sc_callout, timeout * hz, g_raid3_go, sc);
3237 return (sc->sc_geom);
3241 g_raid3_destroy(struct g_raid3_softc *sc, int how)
3243 struct g_provider *pp;
3245 g_topology_assert_not();
3248 sx_assert(&sc->sc_lock, SX_XLOCKED);
3250 pp = sc->sc_provider;
3251 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
3253 case G_RAID3_DESTROY_SOFT:
3255 "Device %s is still open (r%dw%de%d).", pp->name,
3256 pp->acr, pp->acw, pp->ace);
3258 case G_RAID3_DESTROY_DELAYED:
3260 "Device %s will be destroyed on last close.",
3262 if (sc->sc_syncdisk != NULL)
3263 g_raid3_sync_stop(sc, 1);
3264 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROYING;
3266 case G_RAID3_DESTROY_HARD:
3267 G_RAID3_DEBUG(1, "Device %s is still open, so it "
3268 "can't be definitely removed.", pp->name);
3274 if (sc->sc_geom->softc == NULL) {
3275 g_topology_unlock();
3278 sc->sc_geom->softc = NULL;
3279 sc->sc_sync.ds_geom->softc = NULL;
3280 g_topology_unlock();
3282 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
3283 sc->sc_flags |= G_RAID3_DEVICE_FLAG_WAIT;
3284 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3285 sx_xunlock(&sc->sc_lock);
3286 mtx_lock(&sc->sc_queue_mtx);
3288 wakeup(&sc->sc_queue);
3289 mtx_unlock(&sc->sc_queue_mtx);
3290 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3291 while (sc->sc_worker != NULL)
3292 tsleep(&sc->sc_worker, PRIBIO, "r3:destroy", hz / 5);
3293 G_RAID3_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3294 sx_xlock(&sc->sc_lock);
3295 g_raid3_destroy_device(sc);
3296 free(sc->sc_disks, M_RAID3);
3302 g_raid3_taste_orphan(struct g_consumer *cp)
3305 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3306 cp->provider->name));
3309 static struct g_geom *
3310 g_raid3_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3312 struct g_raid3_metadata md;
3313 struct g_raid3_softc *sc;
3314 struct g_consumer *cp;
3318 g_topology_assert();
3319 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3320 G_RAID3_DEBUG(2, "Tasting %s.", pp->name);
3322 gp = g_new_geomf(mp, "raid3:taste");
3323 /* This orphan function should be never called. */
3324 gp->orphan = g_raid3_taste_orphan;
3325 cp = g_new_consumer(gp);
3327 error = g_raid3_read_metadata(cp, &md);
3329 g_destroy_consumer(cp);
3335 if (md.md_provider[0] != '\0' &&
3336 !g_compare_names(md.md_provider, pp->name))
3338 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3340 if (g_raid3_debug >= 2)
3341 raid3_metadata_dump(&md);
3344 * Let's check if device already exists.
3347 LIST_FOREACH(gp, &mp->geom, geom) {
3351 if (sc->sc_sync.ds_geom == gp)
3353 if (strcmp(md.md_name, sc->sc_name) != 0)
3355 if (md.md_id != sc->sc_id) {
3356 G_RAID3_DEBUG(0, "Device %s already configured.",
3363 gp = g_raid3_create(mp, &md);
3365 G_RAID3_DEBUG(0, "Cannot create device %s.",
3371 G_RAID3_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3372 g_topology_unlock();
3373 sx_xlock(&sc->sc_lock);
3374 error = g_raid3_add_disk(sc, pp, &md);
3376 G_RAID3_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3377 pp->name, gp->name, error);
3378 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NODISK) ==
3381 g_raid3_destroy(sc, G_RAID3_DESTROY_HARD);
3387 sx_xunlock(&sc->sc_lock);
3393 g_raid3_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
3396 struct g_raid3_softc *sc;
3399 g_topology_unlock();
3401 sx_xlock(&sc->sc_lock);
3403 error = g_raid3_destroy(gp->softc, G_RAID3_DESTROY_SOFT);
3405 sx_xunlock(&sc->sc_lock);
3411 g_raid3_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3412 struct g_consumer *cp, struct g_provider *pp)
3414 struct g_raid3_softc *sc;
3416 g_topology_assert();
3421 /* Skip synchronization geom. */
3422 if (gp == sc->sc_sync.ds_geom)
3426 } else if (cp != NULL) {
3427 struct g_raid3_disk *disk;
3432 g_topology_unlock();
3433 sx_xlock(&sc->sc_lock);
3434 sbuf_printf(sb, "%s<Type>", indent);
3435 if (disk->d_no == sc->sc_ndisks - 1)
3436 sbuf_printf(sb, "PARITY");
3438 sbuf_printf(sb, "DATA");
3439 sbuf_printf(sb, "</Type>\n");
3440 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
3442 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
3443 sbuf_printf(sb, "%s<Synchronized>", indent);
3444 if (disk->d_sync.ds_offset == 0)
3445 sbuf_printf(sb, "0%%");
3447 sbuf_printf(sb, "%u%%",
3448 (u_int)((disk->d_sync.ds_offset * 100) /
3449 (sc->sc_mediasize / (sc->sc_ndisks - 1))));
3451 sbuf_printf(sb, "</Synchronized>\n");
3452 if (disk->d_sync.ds_offset > 0) {
3453 sbuf_printf(sb, "%s<BytesSynced>%jd"
3454 "</BytesSynced>\n", indent,
3455 (intmax_t)disk->d_sync.ds_offset);
3458 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3459 disk->d_sync.ds_syncid);
3460 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, disk->d_genid);
3461 sbuf_printf(sb, "%s<Flags>", indent);
3462 if (disk->d_flags == 0)
3463 sbuf_printf(sb, "NONE");
3467 #define ADD_FLAG(flag, name) do { \
3468 if ((disk->d_flags & (flag)) != 0) { \
3470 sbuf_printf(sb, ", "); \
3473 sbuf_printf(sb, name); \
3476 ADD_FLAG(G_RAID3_DISK_FLAG_DIRTY, "DIRTY");
3477 ADD_FLAG(G_RAID3_DISK_FLAG_HARDCODED, "HARDCODED");
3478 ADD_FLAG(G_RAID3_DISK_FLAG_SYNCHRONIZING,
3480 ADD_FLAG(G_RAID3_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3481 ADD_FLAG(G_RAID3_DISK_FLAG_BROKEN, "BROKEN");
3484 sbuf_printf(sb, "</Flags>\n");
3485 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3486 g_raid3_disk_state2str(disk->d_state));
3487 sx_xunlock(&sc->sc_lock);
3490 g_topology_unlock();
3491 sx_xlock(&sc->sc_lock);
3492 if (!g_raid3_use_malloc) {
3494 "%s<Zone4kRequested>%u</Zone4kRequested>\n", indent,
3495 sc->sc_zones[G_RAID3_ZONE_4K].sz_requested);
3497 "%s<Zone4kFailed>%u</Zone4kFailed>\n", indent,
3498 sc->sc_zones[G_RAID3_ZONE_4K].sz_failed);
3500 "%s<Zone16kRequested>%u</Zone16kRequested>\n", indent,
3501 sc->sc_zones[G_RAID3_ZONE_16K].sz_requested);
3503 "%s<Zone16kFailed>%u</Zone16kFailed>\n", indent,
3504 sc->sc_zones[G_RAID3_ZONE_16K].sz_failed);
3506 "%s<Zone64kRequested>%u</Zone64kRequested>\n", indent,
3507 sc->sc_zones[G_RAID3_ZONE_64K].sz_requested);
3509 "%s<Zone64kFailed>%u</Zone64kFailed>\n", indent,
3510 sc->sc_zones[G_RAID3_ZONE_64K].sz_failed);
3512 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3513 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3514 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3515 sbuf_printf(sb, "%s<Flags>", indent);
3516 if (sc->sc_flags == 0)
3517 sbuf_printf(sb, "NONE");
3521 #define ADD_FLAG(flag, name) do { \
3522 if ((sc->sc_flags & (flag)) != 0) { \
3524 sbuf_printf(sb, ", "); \
3527 sbuf_printf(sb, name); \
3530 ADD_FLAG(G_RAID3_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3531 ADD_FLAG(G_RAID3_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3532 ADD_FLAG(G_RAID3_DEVICE_FLAG_ROUND_ROBIN,
3534 ADD_FLAG(G_RAID3_DEVICE_FLAG_VERIFY, "VERIFY");
3537 sbuf_printf(sb, "</Flags>\n");
3538 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3540 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3541 g_raid3_device_state2str(sc->sc_state));
3542 sx_xunlock(&sc->sc_lock);
3548 g_raid3_shutdown_post_sync(void *arg, int howto)
3551 struct g_geom *gp, *gp2;
3552 struct g_raid3_softc *sc;
3558 g_raid3_shutdown = 1;
3559 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3560 if ((sc = gp->softc) == NULL)
3562 /* Skip synchronization geom. */
3563 if (gp == sc->sc_sync.ds_geom)
3565 g_topology_unlock();
3566 sx_xlock(&sc->sc_lock);
3567 g_raid3_idle(sc, -1);
3569 error = g_raid3_destroy(sc, G_RAID3_DESTROY_DELAYED);
3571 sx_xunlock(&sc->sc_lock);
3574 g_topology_unlock();
3579 g_raid3_init(struct g_class *mp)
3582 g_raid3_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
3583 g_raid3_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
3584 if (g_raid3_post_sync == NULL)
3585 G_RAID3_DEBUG(0, "Warning! Cannot register shutdown event.");
3589 g_raid3_fini(struct g_class *mp)
3592 if (g_raid3_post_sync != NULL)
3593 EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_raid3_post_sync);
3596 DECLARE_GEOM_CLASS(g_raid3_class, g_raid3);