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>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
40 #include <sys/eventhandler.h>
42 #include <geom/geom.h>
44 #include <sys/kthread.h>
45 #include <sys/sched.h>
46 #include <geom/raid3/g_raid3.h>
49 static MALLOC_DEFINE(M_RAID3, "raid3_data", "GEOM_RAID3 Data");
51 SYSCTL_DECL(_kern_geom);
52 SYSCTL_NODE(_kern_geom, OID_AUTO, raid3, CTLFLAG_RW, 0, "GEOM_RAID3 stuff");
53 u_int g_raid3_debug = 0;
54 TUNABLE_INT("kern.geom.raid3.debug", &g_raid3_debug);
55 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, debug, CTLFLAG_RW, &g_raid3_debug, 0,
57 static u_int g_raid3_timeout = 4;
58 TUNABLE_INT("kern.geom.raid3.timeout", &g_raid3_timeout);
59 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, timeout, CTLFLAG_RW, &g_raid3_timeout,
60 0, "Time to wait on all raid3 components");
61 static u_int g_raid3_idletime = 5;
62 TUNABLE_INT("kern.geom.raid3.idletime", &g_raid3_idletime);
63 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, idletime, CTLFLAG_RW,
64 &g_raid3_idletime, 0, "Mark components as clean when idling");
65 static u_int g_raid3_disconnect_on_failure = 1;
66 TUNABLE_INT("kern.geom.raid3.disconnect_on_failure",
67 &g_raid3_disconnect_on_failure);
68 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
69 &g_raid3_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
70 static u_int g_raid3_syncreqs = 2;
71 TUNABLE_INT("kern.geom.raid3.sync_requests", &g_raid3_syncreqs);
72 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
73 &g_raid3_syncreqs, 0, "Parallel synchronization I/O requests.");
74 static u_int g_raid3_use_malloc = 0;
75 TUNABLE_INT("kern.geom.raid3.use_malloc", &g_raid3_use_malloc);
76 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, use_malloc, CTLFLAG_RDTUN,
77 &g_raid3_use_malloc, 0, "Use malloc(9) instead of uma(9).");
79 static u_int g_raid3_n64k = 50;
80 TUNABLE_INT("kern.geom.raid3.n64k", &g_raid3_n64k);
81 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n64k, CTLFLAG_RD, &g_raid3_n64k, 0,
82 "Maximum number of 64kB allocations");
83 static u_int g_raid3_n16k = 200;
84 TUNABLE_INT("kern.geom.raid3.n16k", &g_raid3_n16k);
85 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n16k, CTLFLAG_RD, &g_raid3_n16k, 0,
86 "Maximum number of 16kB allocations");
87 static u_int g_raid3_n4k = 1200;
88 TUNABLE_INT("kern.geom.raid3.n4k", &g_raid3_n4k);
89 SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n4k, CTLFLAG_RD, &g_raid3_n4k, 0,
90 "Maximum number of 4kB allocations");
92 SYSCTL_NODE(_kern_geom_raid3, OID_AUTO, stat, CTLFLAG_RW, 0,
93 "GEOM_RAID3 statistics");
94 static u_int g_raid3_parity_mismatch = 0;
95 SYSCTL_UINT(_kern_geom_raid3_stat, OID_AUTO, parity_mismatch, CTLFLAG_RD,
96 &g_raid3_parity_mismatch, 0, "Number of failures in VERIFY mode");
98 #define MSLEEP(ident, mtx, priority, wmesg, timeout) do { \
99 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, (ident)); \
100 msleep((ident), (mtx), (priority), (wmesg), (timeout)); \
101 G_RAID3_DEBUG(4, "%s: Woken up %p.", __func__, (ident)); \
104 static eventhandler_tag g_raid3_pre_sync = NULL;
106 static int g_raid3_destroy_geom(struct gctl_req *req, struct g_class *mp,
108 static g_taste_t g_raid3_taste;
109 static void g_raid3_init(struct g_class *mp);
110 static void g_raid3_fini(struct g_class *mp);
112 struct g_class g_raid3_class = {
113 .name = G_RAID3_CLASS_NAME,
114 .version = G_VERSION,
115 .ctlreq = g_raid3_config,
116 .taste = g_raid3_taste,
117 .destroy_geom = g_raid3_destroy_geom,
118 .init = g_raid3_init,
123 static void g_raid3_destroy_provider(struct g_raid3_softc *sc);
124 static int g_raid3_update_disk(struct g_raid3_disk *disk, u_int state);
125 static void g_raid3_update_device(struct g_raid3_softc *sc, boolean_t force);
126 static void g_raid3_dumpconf(struct sbuf *sb, const char *indent,
127 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
128 static void g_raid3_sync_stop(struct g_raid3_softc *sc, int type);
129 static int g_raid3_register_request(struct bio *pbp);
130 static void g_raid3_sync_release(struct g_raid3_softc *sc);
134 g_raid3_disk_state2str(int state)
138 case G_RAID3_DISK_STATE_NODISK:
140 case G_RAID3_DISK_STATE_NONE:
142 case G_RAID3_DISK_STATE_NEW:
144 case G_RAID3_DISK_STATE_ACTIVE:
146 case G_RAID3_DISK_STATE_STALE:
148 case G_RAID3_DISK_STATE_SYNCHRONIZING:
149 return ("SYNCHRONIZING");
150 case G_RAID3_DISK_STATE_DISCONNECTED:
151 return ("DISCONNECTED");
158 g_raid3_device_state2str(int state)
162 case G_RAID3_DEVICE_STATE_STARTING:
164 case G_RAID3_DEVICE_STATE_DEGRADED:
166 case G_RAID3_DEVICE_STATE_COMPLETE:
174 g_raid3_get_diskname(struct g_raid3_disk *disk)
177 if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
178 return ("[unknown]");
179 return (disk->d_name);
183 g_raid3_alloc(struct g_raid3_softc *sc, size_t size, int flags)
186 enum g_raid3_zones zone;
188 if (g_raid3_use_malloc ||
189 (zone = g_raid3_zone(size)) == G_RAID3_NUM_ZONES)
190 ptr = malloc(size, M_RAID3, flags);
192 ptr = uma_zalloc_arg(sc->sc_zones[zone].sz_zone,
193 &sc->sc_zones[zone], flags);
194 sc->sc_zones[zone].sz_requested++;
196 sc->sc_zones[zone].sz_failed++;
202 g_raid3_free(struct g_raid3_softc *sc, void *ptr, size_t size)
204 enum g_raid3_zones zone;
206 if (g_raid3_use_malloc ||
207 (zone = g_raid3_zone(size)) == G_RAID3_NUM_ZONES)
210 uma_zfree_arg(sc->sc_zones[zone].sz_zone,
211 ptr, &sc->sc_zones[zone]);
216 g_raid3_uma_ctor(void *mem, int size, void *arg, int flags)
218 struct g_raid3_zone *sz = arg;
220 if (sz->sz_max > 0 && sz->sz_inuse == sz->sz_max)
227 g_raid3_uma_dtor(void *mem, int size, void *arg)
229 struct g_raid3_zone *sz = arg;
234 #define g_raid3_xor(src, dst, size) \
235 _g_raid3_xor((uint64_t *)(src), \
236 (uint64_t *)(dst), (size_t)size)
238 _g_raid3_xor(uint64_t *src, uint64_t *dst, size_t size)
241 KASSERT((size % 128) == 0, ("Invalid size: %zu.", size));
242 for (; size > 0; size -= 128) {
263 g_raid3_is_zero(struct bio *bp)
265 static const uint64_t zeros[] = {
266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
271 size = bp->bio_length;
272 addr = (u_char *)bp->bio_data;
273 for (; size > 0; size -= sizeof(zeros), addr += sizeof(zeros)) {
274 if (bcmp(addr, zeros, sizeof(zeros)) != 0)
281 * --- Events handling functions ---
282 * Events in geom_raid3 are used to maintain disks and device status
283 * from one thread to simplify locking.
286 g_raid3_event_free(struct g_raid3_event *ep)
293 g_raid3_event_send(void *arg, int state, int flags)
295 struct g_raid3_softc *sc;
296 struct g_raid3_disk *disk;
297 struct g_raid3_event *ep;
300 ep = malloc(sizeof(*ep), M_RAID3, M_WAITOK);
301 G_RAID3_DEBUG(4, "%s: Sending event %p.", __func__, ep);
302 if ((flags & G_RAID3_EVENT_DEVICE) != 0) {
313 mtx_lock(&sc->sc_events_mtx);
314 TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
315 mtx_unlock(&sc->sc_events_mtx);
316 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
317 mtx_lock(&sc->sc_queue_mtx);
319 wakeup(&sc->sc_queue);
320 mtx_unlock(&sc->sc_queue_mtx);
321 if ((flags & G_RAID3_EVENT_DONTWAIT) != 0)
323 sx_assert(&sc->sc_lock, SX_XLOCKED);
324 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
325 sx_xunlock(&sc->sc_lock);
326 while ((ep->e_flags & G_RAID3_EVENT_DONE) == 0) {
327 mtx_lock(&sc->sc_events_mtx);
328 MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "r3:event",
332 g_raid3_event_free(ep);
333 sx_xlock(&sc->sc_lock);
337 static struct g_raid3_event *
338 g_raid3_event_get(struct g_raid3_softc *sc)
340 struct g_raid3_event *ep;
342 mtx_lock(&sc->sc_events_mtx);
343 ep = TAILQ_FIRST(&sc->sc_events);
344 mtx_unlock(&sc->sc_events_mtx);
349 g_raid3_event_remove(struct g_raid3_softc *sc, struct g_raid3_event *ep)
352 mtx_lock(&sc->sc_events_mtx);
353 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
354 mtx_unlock(&sc->sc_events_mtx);
358 g_raid3_event_cancel(struct g_raid3_disk *disk)
360 struct g_raid3_softc *sc;
361 struct g_raid3_event *ep, *tmpep;
364 sx_assert(&sc->sc_lock, SX_XLOCKED);
366 mtx_lock(&sc->sc_events_mtx);
367 TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
368 if ((ep->e_flags & G_RAID3_EVENT_DEVICE) != 0)
370 if (ep->e_disk != disk)
372 TAILQ_REMOVE(&sc->sc_events, ep, e_next);
373 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0)
374 g_raid3_event_free(ep);
376 ep->e_error = ECANCELED;
380 mtx_unlock(&sc->sc_events_mtx);
384 * Return the number of disks in the given state.
385 * If state is equal to -1, count all connected disks.
388 g_raid3_ndisks(struct g_raid3_softc *sc, int state)
390 struct g_raid3_disk *disk;
393 sx_assert(&sc->sc_lock, SX_LOCKED);
395 for (n = ndisks = 0; n < sc->sc_ndisks; n++) {
396 disk = &sc->sc_disks[n];
397 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
399 if (state == -1 || disk->d_state == state)
406 g_raid3_nrequests(struct g_raid3_softc *sc, struct g_consumer *cp)
411 mtx_lock(&sc->sc_queue_mtx);
412 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
413 if (bp->bio_from == cp)
416 mtx_unlock(&sc->sc_queue_mtx);
421 g_raid3_is_busy(struct g_raid3_softc *sc, struct g_consumer *cp)
426 "I/O requests for %s exist, can't destroy it now.",
430 if (g_raid3_nrequests(sc, cp) > 0) {
432 "I/O requests for %s in queue, can't destroy it now.",
440 g_raid3_destroy_consumer(void *arg, int flags __unused)
442 struct g_consumer *cp;
447 G_RAID3_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
449 g_destroy_consumer(cp);
453 g_raid3_kill_consumer(struct g_raid3_softc *sc, struct g_consumer *cp)
455 struct g_provider *pp;
461 if (g_raid3_is_busy(sc, cp))
463 G_RAID3_DEBUG(2, "Consumer %s destroyed.", cp->provider->name);
467 if ((pp->geom->flags & G_GEOM_WITHER) == 0)
470 G_RAID3_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
471 -cp->acw, -cp->ace, 0);
472 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
473 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
476 * After retaste event was send (inside g_access()), we can send
477 * event to detach and destroy consumer.
478 * A class, which has consumer to the given provider connected
479 * will not receive retaste event for the provider.
480 * This is the way how I ignore retaste events when I close
481 * consumers opened for write: I detach and destroy consumer
482 * after retaste event is sent.
484 g_post_event(g_raid3_destroy_consumer, cp, M_WAITOK, NULL);
487 G_RAID3_DEBUG(1, "Consumer %s destroyed.", pp->name);
489 g_destroy_consumer(cp);
493 g_raid3_connect_disk(struct g_raid3_disk *disk, struct g_provider *pp)
495 struct g_consumer *cp;
498 g_topology_assert_not();
499 KASSERT(disk->d_consumer == NULL,
500 ("Disk already connected (device %s).", disk->d_softc->sc_name));
503 cp = g_new_consumer(disk->d_softc->sc_geom);
504 error = g_attach(cp, pp);
506 g_destroy_consumer(cp);
510 error = g_access(cp, 1, 1, 1);
514 g_destroy_consumer(cp);
515 G_RAID3_DEBUG(0, "Cannot open consumer %s (error=%d).",
519 disk->d_consumer = cp;
520 disk->d_consumer->private = disk;
521 disk->d_consumer->index = 0;
522 G_RAID3_DEBUG(2, "Disk %s connected.", g_raid3_get_diskname(disk));
527 g_raid3_disconnect_consumer(struct g_raid3_softc *sc, struct g_consumer *cp)
534 if (cp->provider != NULL)
535 g_raid3_kill_consumer(sc, cp);
537 g_destroy_consumer(cp);
541 * Initialize disk. This means allocate memory, create consumer, attach it
542 * to the provider and open access (r1w1e1) to it.
544 static struct g_raid3_disk *
545 g_raid3_init_disk(struct g_raid3_softc *sc, struct g_provider *pp,
546 struct g_raid3_metadata *md, int *errorp)
548 struct g_raid3_disk *disk;
551 disk = &sc->sc_disks[md->md_no];
552 error = g_raid3_connect_disk(disk, pp);
558 disk->d_state = G_RAID3_DISK_STATE_NONE;
559 disk->d_flags = md->md_dflags;
560 if (md->md_provider[0] != '\0')
561 disk->d_flags |= G_RAID3_DISK_FLAG_HARDCODED;
562 disk->d_sync.ds_consumer = NULL;
563 disk->d_sync.ds_offset = md->md_sync_offset;
564 disk->d_sync.ds_offset_done = md->md_sync_offset;
565 disk->d_genid = md->md_genid;
566 disk->d_sync.ds_syncid = md->md_syncid;
573 g_raid3_destroy_disk(struct g_raid3_disk *disk)
575 struct g_raid3_softc *sc;
577 g_topology_assert_not();
579 sx_assert(&sc->sc_lock, SX_XLOCKED);
581 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
583 g_raid3_event_cancel(disk);
584 switch (disk->d_state) {
585 case G_RAID3_DISK_STATE_SYNCHRONIZING:
586 if (sc->sc_syncdisk != NULL)
587 g_raid3_sync_stop(sc, 1);
589 case G_RAID3_DISK_STATE_NEW:
590 case G_RAID3_DISK_STATE_STALE:
591 case G_RAID3_DISK_STATE_ACTIVE:
593 g_raid3_disconnect_consumer(sc, disk->d_consumer);
595 disk->d_consumer = NULL;
598 KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
599 g_raid3_get_diskname(disk),
600 g_raid3_disk_state2str(disk->d_state)));
602 disk->d_state = G_RAID3_DISK_STATE_NODISK;
606 g_raid3_destroy_device(struct g_raid3_softc *sc)
608 struct g_raid3_event *ep;
609 struct g_raid3_disk *disk;
611 struct g_consumer *cp;
614 g_topology_assert_not();
615 sx_assert(&sc->sc_lock, SX_XLOCKED);
618 if (sc->sc_provider != NULL)
619 g_raid3_destroy_provider(sc);
620 for (n = 0; n < sc->sc_ndisks; n++) {
621 disk = &sc->sc_disks[n];
622 if (disk->d_state != G_RAID3_DISK_STATE_NODISK) {
623 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
624 g_raid3_update_metadata(disk);
625 g_raid3_destroy_disk(disk);
628 while ((ep = g_raid3_event_get(sc)) != NULL) {
629 g_raid3_event_remove(sc, ep);
630 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0)
631 g_raid3_event_free(ep);
633 ep->e_error = ECANCELED;
634 ep->e_flags |= G_RAID3_EVENT_DONE;
635 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, ep);
636 mtx_lock(&sc->sc_events_mtx);
638 mtx_unlock(&sc->sc_events_mtx);
641 callout_drain(&sc->sc_callout);
642 cp = LIST_FIRST(&sc->sc_sync.ds_geom->consumer);
645 g_raid3_disconnect_consumer(sc, cp);
646 g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
647 G_RAID3_DEBUG(0, "Device %s destroyed.", gp->name);
648 g_wither_geom(gp, ENXIO);
650 if (!g_raid3_use_malloc) {
651 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_64K].sz_zone);
652 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_16K].sz_zone);
653 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_4K].sz_zone);
655 mtx_destroy(&sc->sc_queue_mtx);
656 mtx_destroy(&sc->sc_events_mtx);
657 sx_xunlock(&sc->sc_lock);
658 sx_destroy(&sc->sc_lock);
662 g_raid3_orphan(struct g_consumer *cp)
664 struct g_raid3_disk *disk;
671 disk->d_softc->sc_bump_id = G_RAID3_BUMP_SYNCID;
672 g_raid3_event_send(disk, G_RAID3_DISK_STATE_DISCONNECTED,
673 G_RAID3_EVENT_DONTWAIT);
677 g_raid3_write_metadata(struct g_raid3_disk *disk, struct g_raid3_metadata *md)
679 struct g_raid3_softc *sc;
680 struct g_consumer *cp;
681 off_t offset, length;
685 g_topology_assert_not();
687 sx_assert(&sc->sc_lock, SX_LOCKED);
689 cp = disk->d_consumer;
690 KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
691 KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
692 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
693 ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
695 length = cp->provider->sectorsize;
696 offset = cp->provider->mediasize - length;
697 sector = malloc((size_t)length, M_RAID3, M_WAITOK | M_ZERO);
699 raid3_metadata_encode(md, sector);
700 error = g_write_data(cp, offset, sector, length);
701 free(sector, M_RAID3);
703 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
704 G_RAID3_DEBUG(0, "Cannot write metadata on %s "
705 "(device=%s, error=%d).",
706 g_raid3_get_diskname(disk), sc->sc_name, error);
707 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
709 G_RAID3_DEBUG(1, "Cannot write metadata on %s "
710 "(device=%s, error=%d).",
711 g_raid3_get_diskname(disk), sc->sc_name, error);
713 if (g_raid3_disconnect_on_failure &&
714 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
715 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
716 g_raid3_event_send(disk,
717 G_RAID3_DISK_STATE_DISCONNECTED,
718 G_RAID3_EVENT_DONTWAIT);
725 g_raid3_clear_metadata(struct g_raid3_disk *disk)
729 g_topology_assert_not();
730 sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
732 error = g_raid3_write_metadata(disk, NULL);
734 G_RAID3_DEBUG(2, "Metadata on %s cleared.",
735 g_raid3_get_diskname(disk));
738 "Cannot clear metadata on disk %s (error=%d).",
739 g_raid3_get_diskname(disk), error);
745 g_raid3_fill_metadata(struct g_raid3_disk *disk, struct g_raid3_metadata *md)
747 struct g_raid3_softc *sc;
748 struct g_provider *pp;
751 strlcpy(md->md_magic, G_RAID3_MAGIC, sizeof(md->md_magic));
752 md->md_version = G_RAID3_VERSION;
753 strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
754 md->md_id = sc->sc_id;
755 md->md_all = sc->sc_ndisks;
756 md->md_genid = sc->sc_genid;
757 md->md_mediasize = sc->sc_mediasize;
758 md->md_sectorsize = sc->sc_sectorsize;
759 md->md_mflags = (sc->sc_flags & G_RAID3_DEVICE_FLAG_MASK);
760 md->md_no = disk->d_no;
761 md->md_syncid = disk->d_sync.ds_syncid;
762 md->md_dflags = (disk->d_flags & G_RAID3_DISK_FLAG_MASK);
763 if (disk->d_state != G_RAID3_DISK_STATE_SYNCHRONIZING)
764 md->md_sync_offset = 0;
767 disk->d_sync.ds_offset_done / (sc->sc_ndisks - 1);
769 if (disk->d_consumer != NULL && disk->d_consumer->provider != NULL)
770 pp = disk->d_consumer->provider;
773 if ((disk->d_flags & G_RAID3_DISK_FLAG_HARDCODED) != 0 && pp != NULL)
774 strlcpy(md->md_provider, pp->name, sizeof(md->md_provider));
776 bzero(md->md_provider, sizeof(md->md_provider));
778 md->md_provsize = pp->mediasize;
784 g_raid3_update_metadata(struct g_raid3_disk *disk)
786 struct g_raid3_softc *sc;
787 struct g_raid3_metadata md;
790 g_topology_assert_not();
792 sx_assert(&sc->sc_lock, SX_LOCKED);
794 g_raid3_fill_metadata(disk, &md);
795 error = g_raid3_write_metadata(disk, &md);
797 G_RAID3_DEBUG(2, "Metadata on %s updated.",
798 g_raid3_get_diskname(disk));
801 "Cannot update metadata on disk %s (error=%d).",
802 g_raid3_get_diskname(disk), error);
807 g_raid3_bump_syncid(struct g_raid3_softc *sc)
809 struct g_raid3_disk *disk;
812 g_topology_assert_not();
813 sx_assert(&sc->sc_lock, SX_XLOCKED);
814 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) > 0,
815 ("%s called with no active disks (device=%s).", __func__,
819 G_RAID3_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
821 for (n = 0; n < sc->sc_ndisks; n++) {
822 disk = &sc->sc_disks[n];
823 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
824 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
825 disk->d_sync.ds_syncid = sc->sc_syncid;
826 g_raid3_update_metadata(disk);
832 g_raid3_bump_genid(struct g_raid3_softc *sc)
834 struct g_raid3_disk *disk;
837 g_topology_assert_not();
838 sx_assert(&sc->sc_lock, SX_XLOCKED);
839 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) > 0,
840 ("%s called with no active disks (device=%s).", __func__,
844 G_RAID3_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
846 for (n = 0; n < sc->sc_ndisks; n++) {
847 disk = &sc->sc_disks[n];
848 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
849 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
850 disk->d_genid = sc->sc_genid;
851 g_raid3_update_metadata(disk);
857 g_raid3_idle(struct g_raid3_softc *sc, int acw)
859 struct g_raid3_disk *disk;
863 g_topology_assert_not();
864 sx_assert(&sc->sc_lock, SX_XLOCKED);
866 if (sc->sc_provider == NULL)
868 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
872 if (sc->sc_writes > 0)
874 if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
875 timeout = g_raid3_idletime - (time_uptime - sc->sc_last_write);
880 for (i = 0; i < sc->sc_ndisks; i++) {
881 disk = &sc->sc_disks[i];
882 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
884 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as clean.",
885 g_raid3_get_diskname(disk), sc->sc_name);
886 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
887 g_raid3_update_metadata(disk);
893 g_raid3_unidle(struct g_raid3_softc *sc)
895 struct g_raid3_disk *disk;
898 g_topology_assert_not();
899 sx_assert(&sc->sc_lock, SX_XLOCKED);
901 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
904 sc->sc_last_write = time_uptime;
905 for (i = 0; i < sc->sc_ndisks; i++) {
906 disk = &sc->sc_disks[i];
907 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
909 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as dirty.",
910 g_raid3_get_diskname(disk), sc->sc_name);
911 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
912 g_raid3_update_metadata(disk);
917 * Treat bio_driver1 field in parent bio as list head and field bio_caller1
918 * in child bio as pointer to the next element on the list.
920 #define G_RAID3_HEAD_BIO(pbp) (pbp)->bio_driver1
922 #define G_RAID3_NEXT_BIO(cbp) (cbp)->bio_caller1
924 #define G_RAID3_FOREACH_BIO(pbp, bp) \
925 for ((bp) = G_RAID3_HEAD_BIO(pbp); (bp) != NULL; \
926 (bp) = G_RAID3_NEXT_BIO(bp))
928 #define G_RAID3_FOREACH_SAFE_BIO(pbp, bp, tmpbp) \
929 for ((bp) = G_RAID3_HEAD_BIO(pbp); \
930 (bp) != NULL && ((tmpbp) = G_RAID3_NEXT_BIO(bp), 1); \
934 g_raid3_init_bio(struct bio *pbp)
937 G_RAID3_HEAD_BIO(pbp) = NULL;
941 g_raid3_remove_bio(struct bio *cbp)
943 struct bio *pbp, *bp;
945 pbp = cbp->bio_parent;
946 if (G_RAID3_HEAD_BIO(pbp) == cbp)
947 G_RAID3_HEAD_BIO(pbp) = G_RAID3_NEXT_BIO(cbp);
949 G_RAID3_FOREACH_BIO(pbp, bp) {
950 if (G_RAID3_NEXT_BIO(bp) == cbp) {
951 G_RAID3_NEXT_BIO(bp) = G_RAID3_NEXT_BIO(cbp);
956 G_RAID3_NEXT_BIO(cbp) = NULL;
960 g_raid3_replace_bio(struct bio *sbp, struct bio *dbp)
962 struct bio *pbp, *bp;
964 g_raid3_remove_bio(sbp);
965 pbp = dbp->bio_parent;
966 G_RAID3_NEXT_BIO(sbp) = G_RAID3_NEXT_BIO(dbp);
967 if (G_RAID3_HEAD_BIO(pbp) == dbp)
968 G_RAID3_HEAD_BIO(pbp) = sbp;
970 G_RAID3_FOREACH_BIO(pbp, bp) {
971 if (G_RAID3_NEXT_BIO(bp) == dbp) {
972 G_RAID3_NEXT_BIO(bp) = sbp;
977 G_RAID3_NEXT_BIO(dbp) = NULL;
981 g_raid3_destroy_bio(struct g_raid3_softc *sc, struct bio *cbp)
983 struct bio *bp, *pbp;
986 pbp = cbp->bio_parent;
988 KASSERT(cbp->bio_data != NULL, ("NULL bio_data"));
989 size = pbp->bio_length / (sc->sc_ndisks - 1);
990 g_raid3_free(sc, cbp->bio_data, size);
991 if (G_RAID3_HEAD_BIO(pbp) == cbp) {
992 G_RAID3_HEAD_BIO(pbp) = G_RAID3_NEXT_BIO(cbp);
993 G_RAID3_NEXT_BIO(cbp) = NULL;
996 G_RAID3_FOREACH_BIO(pbp, bp) {
997 if (G_RAID3_NEXT_BIO(bp) == cbp)
1001 KASSERT(G_RAID3_NEXT_BIO(bp) != NULL,
1002 ("NULL bp->bio_driver1"));
1003 G_RAID3_NEXT_BIO(bp) = G_RAID3_NEXT_BIO(cbp);
1004 G_RAID3_NEXT_BIO(cbp) = NULL;
1011 g_raid3_clone_bio(struct g_raid3_softc *sc, struct bio *pbp)
1013 struct bio *bp, *cbp;
1017 cbp = g_clone_bio(pbp);
1020 size = pbp->bio_length / (sc->sc_ndisks - 1);
1021 if ((pbp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR) != 0)
1025 cbp->bio_data = g_raid3_alloc(sc, size, memflag);
1026 if (cbp->bio_data == NULL) {
1027 pbp->bio_children--;
1031 G_RAID3_NEXT_BIO(cbp) = NULL;
1032 if (G_RAID3_HEAD_BIO(pbp) == NULL)
1033 G_RAID3_HEAD_BIO(pbp) = cbp;
1035 G_RAID3_FOREACH_BIO(pbp, bp) {
1036 if (G_RAID3_NEXT_BIO(bp) == NULL) {
1037 G_RAID3_NEXT_BIO(bp) = cbp;
1046 g_raid3_scatter(struct bio *pbp)
1048 struct g_raid3_softc *sc;
1049 struct g_raid3_disk *disk;
1050 struct bio *bp, *cbp, *tmpbp;
1051 off_t atom, cadd, padd, left;
1054 sc = pbp->bio_to->geom->softc;
1056 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_NOPARITY) == 0) {
1058 * Find bio for which we should calculate data.
1060 G_RAID3_FOREACH_BIO(pbp, cbp) {
1061 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0) {
1066 KASSERT(bp != NULL, ("NULL parity bio."));
1068 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1070 for (left = pbp->bio_length; left > 0; left -= sc->sc_sectorsize) {
1071 G_RAID3_FOREACH_BIO(pbp, cbp) {
1074 bcopy(pbp->bio_data + padd, cbp->bio_data + cadd, atom);
1079 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_NOPARITY) == 0) {
1084 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1088 bcopy(cbp->bio_data, bp->bio_data,
1092 g_raid3_xor(cbp->bio_data, bp->bio_data,
1095 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_NODISK) != 0)
1096 g_raid3_destroy_bio(sc, cbp);
1099 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1100 struct g_consumer *cp;
1102 disk = cbp->bio_caller2;
1103 cp = disk->d_consumer;
1104 cbp->bio_to = cp->provider;
1105 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1106 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1107 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1108 cp->acr, cp->acw, cp->ace));
1111 g_io_request(cbp, cp);
1116 g_raid3_gather(struct bio *pbp)
1118 struct g_raid3_softc *sc;
1119 struct g_raid3_disk *disk;
1120 struct bio *xbp, *fbp, *cbp;
1121 off_t atom, cadd, padd, left;
1123 sc = pbp->bio_to->geom->softc;
1125 * Find bio for which we have to calculate data.
1126 * While going through this path, check if all requests
1127 * succeeded, if not, deny whole request.
1128 * If we're in COMPLETE mode, we allow one request to fail,
1129 * so if we find one, we're sending it to the parity consumer.
1130 * If there are more failed requests, we deny whole request.
1133 G_RAID3_FOREACH_BIO(pbp, cbp) {
1134 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0) {
1135 KASSERT(xbp == NULL, ("More than one parity bio."));
1138 if (cbp->bio_error == 0)
1141 * Found failed request.
1144 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_DEGRADED) != 0) {
1146 * We are already in degraded mode, so we can't
1147 * accept any failures.
1149 if (pbp->bio_error == 0)
1150 pbp->bio_error = cbp->bio_error;
1156 * Next failed request, that's too many.
1158 if (pbp->bio_error == 0)
1159 pbp->bio_error = fbp->bio_error;
1161 disk = cbp->bio_caller2;
1164 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
1165 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
1166 G_RAID3_LOGREQ(0, cbp, "Request failed (error=%d).",
1169 G_RAID3_LOGREQ(1, cbp, "Request failed (error=%d).",
1172 if (g_raid3_disconnect_on_failure &&
1173 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1174 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1175 g_raid3_event_send(disk,
1176 G_RAID3_DISK_STATE_DISCONNECTED,
1177 G_RAID3_EVENT_DONTWAIT);
1180 if (pbp->bio_error != 0)
1182 if (fbp != NULL && (pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0) {
1183 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_VERIFY;
1185 g_raid3_replace_bio(xbp, fbp);
1186 g_raid3_destroy_bio(sc, fbp);
1187 } else if (fbp != NULL) {
1188 struct g_consumer *cp;
1191 * One request failed, so send the same request to
1192 * the parity consumer.
1194 disk = pbp->bio_driver2;
1195 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE) {
1196 pbp->bio_error = fbp->bio_error;
1199 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1201 fbp->bio_flags &= ~(BIO_DONE | BIO_ERROR);
1202 if (disk->d_no == sc->sc_ndisks - 1)
1203 fbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1205 fbp->bio_completed = 0;
1206 fbp->bio_children = 0;
1208 cp = disk->d_consumer;
1209 fbp->bio_caller2 = disk;
1210 fbp->bio_to = cp->provider;
1211 G_RAID3_LOGREQ(3, fbp, "Sending request (recover).");
1212 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1213 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1214 cp->acr, cp->acw, cp->ace));
1216 g_io_request(fbp, cp);
1223 G_RAID3_FOREACH_BIO(pbp, cbp) {
1224 if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0)
1226 g_raid3_xor(cbp->bio_data, xbp->bio_data,
1229 xbp->bio_cflags &= ~G_RAID3_BIO_CFLAG_PARITY;
1230 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0) {
1231 if (!g_raid3_is_zero(xbp)) {
1232 g_raid3_parity_mismatch++;
1233 pbp->bio_error = EIO;
1236 g_raid3_destroy_bio(sc, xbp);
1239 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1241 for (left = pbp->bio_length; left > 0; left -= sc->sc_sectorsize) {
1242 G_RAID3_FOREACH_BIO(pbp, cbp) {
1243 bcopy(cbp->bio_data + cadd, pbp->bio_data + padd, atom);
1244 pbp->bio_completed += atom;
1250 if (pbp->bio_error == 0)
1251 G_RAID3_LOGREQ(3, pbp, "Request finished.");
1253 if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0)
1254 G_RAID3_LOGREQ(1, pbp, "Verification error.");
1256 G_RAID3_LOGREQ(0, pbp, "Request failed.");
1258 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_MASK;
1259 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL)
1260 g_raid3_destroy_bio(sc, cbp);
1261 g_io_deliver(pbp, pbp->bio_error);
1265 g_raid3_done(struct bio *bp)
1267 struct g_raid3_softc *sc;
1269 sc = bp->bio_from->geom->softc;
1270 bp->bio_cflags |= G_RAID3_BIO_CFLAG_REGULAR;
1271 G_RAID3_LOGREQ(3, bp, "Regular request done (error=%d).", bp->bio_error);
1272 mtx_lock(&sc->sc_queue_mtx);
1273 bioq_insert_head(&sc->sc_queue, bp);
1274 mtx_unlock(&sc->sc_queue_mtx);
1276 wakeup(&sc->sc_queue);
1280 g_raid3_regular_request(struct bio *cbp)
1282 struct g_raid3_softc *sc;
1283 struct g_raid3_disk *disk;
1286 g_topology_assert_not();
1288 pbp = cbp->bio_parent;
1289 sc = pbp->bio_to->geom->softc;
1290 cbp->bio_from->index--;
1291 if (cbp->bio_cmd == BIO_WRITE)
1293 disk = cbp->bio_from->private;
1296 g_raid3_kill_consumer(sc, cbp->bio_from);
1297 g_topology_unlock();
1300 G_RAID3_LOGREQ(3, cbp, "Request finished.");
1302 KASSERT(pbp->bio_inbed <= pbp->bio_children,
1303 ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
1304 pbp->bio_children));
1305 if (pbp->bio_inbed != pbp->bio_children)
1307 switch (pbp->bio_cmd) {
1309 g_raid3_gather(pbp);
1316 pbp->bio_completed = pbp->bio_length;
1317 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL) {
1318 if (cbp->bio_error == 0) {
1319 g_raid3_destroy_bio(sc, cbp);
1324 error = cbp->bio_error;
1325 else if (pbp->bio_error == 0) {
1327 * Next failed request, that's too many.
1329 pbp->bio_error = error;
1332 disk = cbp->bio_caller2;
1334 g_raid3_destroy_bio(sc, cbp);
1338 if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
1339 disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
1340 G_RAID3_LOGREQ(0, cbp,
1341 "Request failed (error=%d).",
1344 G_RAID3_LOGREQ(1, cbp,
1345 "Request failed (error=%d).",
1348 if (g_raid3_disconnect_on_failure &&
1349 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1350 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1351 g_raid3_event_send(disk,
1352 G_RAID3_DISK_STATE_DISCONNECTED,
1353 G_RAID3_EVENT_DONTWAIT);
1355 g_raid3_destroy_bio(sc, cbp);
1357 if (pbp->bio_error == 0)
1358 G_RAID3_LOGREQ(3, pbp, "Request finished.");
1360 G_RAID3_LOGREQ(0, pbp, "Request failed.");
1361 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_DEGRADED;
1362 pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_NOPARITY;
1363 bioq_remove(&sc->sc_inflight, pbp);
1364 /* Release delayed sync requests if possible. */
1365 g_raid3_sync_release(sc);
1366 g_io_deliver(pbp, pbp->bio_error);
1373 g_raid3_sync_done(struct bio *bp)
1375 struct g_raid3_softc *sc;
1377 G_RAID3_LOGREQ(3, bp, "Synchronization request delivered.");
1378 sc = bp->bio_from->geom->softc;
1379 bp->bio_cflags |= G_RAID3_BIO_CFLAG_SYNC;
1380 mtx_lock(&sc->sc_queue_mtx);
1381 bioq_insert_head(&sc->sc_queue, bp);
1382 mtx_unlock(&sc->sc_queue_mtx);
1384 wakeup(&sc->sc_queue);
1388 g_raid3_flush(struct g_raid3_softc *sc, struct bio *bp)
1390 struct bio_queue_head queue;
1391 struct g_raid3_disk *disk;
1392 struct g_consumer *cp;
1397 for (i = 0; i < sc->sc_ndisks; i++) {
1398 disk = &sc->sc_disks[i];
1399 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
1401 cbp = g_clone_bio(bp);
1403 for (cbp = bioq_first(&queue); cbp != NULL;
1404 cbp = bioq_first(&queue)) {
1405 bioq_remove(&queue, cbp);
1408 if (bp->bio_error == 0)
1409 bp->bio_error = ENOMEM;
1410 g_io_deliver(bp, bp->bio_error);
1413 bioq_insert_tail(&queue, cbp);
1414 cbp->bio_done = g_std_done;
1415 cbp->bio_caller1 = disk;
1416 cbp->bio_to = disk->d_consumer->provider;
1418 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1419 bioq_remove(&queue, cbp);
1420 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1421 disk = cbp->bio_caller1;
1422 cbp->bio_caller1 = NULL;
1423 cp = disk->d_consumer;
1424 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1425 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1426 cp->acr, cp->acw, cp->ace));
1427 g_io_request(cbp, disk->d_consumer);
1432 g_raid3_start(struct bio *bp)
1434 struct g_raid3_softc *sc;
1436 sc = bp->bio_to->geom->softc;
1438 * If sc == NULL or there are no valid disks, provider's error
1439 * should be set and g_raid3_start() should not be called at all.
1441 KASSERT(sc != NULL && (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
1442 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE),
1443 ("Provider's error should be set (error=%d)(device=%s).",
1444 bp->bio_to->error, bp->bio_to->name));
1445 G_RAID3_LOGREQ(3, bp, "Request received.");
1447 switch (bp->bio_cmd) {
1453 g_raid3_flush(sc, bp);
1457 g_io_deliver(bp, EOPNOTSUPP);
1460 mtx_lock(&sc->sc_queue_mtx);
1461 bioq_insert_tail(&sc->sc_queue, bp);
1462 mtx_unlock(&sc->sc_queue_mtx);
1463 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1468 * Return TRUE if the given request is colliding with a in-progress
1469 * synchronization request.
1472 g_raid3_sync_collision(struct g_raid3_softc *sc, struct bio *bp)
1474 struct g_raid3_disk *disk;
1476 off_t rstart, rend, sstart, send;
1479 disk = sc->sc_syncdisk;
1482 rstart = bp->bio_offset;
1483 rend = bp->bio_offset + bp->bio_length;
1484 for (i = 0; i < g_raid3_syncreqs; i++) {
1485 sbp = disk->d_sync.ds_bios[i];
1488 sstart = sbp->bio_offset;
1489 send = sbp->bio_length;
1490 if (sbp->bio_cmd == BIO_WRITE) {
1491 sstart *= sc->sc_ndisks - 1;
1492 send *= sc->sc_ndisks - 1;
1495 if (rend > sstart && rstart < send)
1502 * Return TRUE if the given sync request is colliding with a in-progress regular
1506 g_raid3_regular_collision(struct g_raid3_softc *sc, struct bio *sbp)
1508 off_t rstart, rend, sstart, send;
1511 if (sc->sc_syncdisk == NULL)
1513 sstart = sbp->bio_offset;
1514 send = sstart + sbp->bio_length;
1515 TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1516 rstart = bp->bio_offset;
1517 rend = bp->bio_offset + bp->bio_length;
1518 if (rend > sstart && rstart < send)
1525 * Puts request onto delayed queue.
1528 g_raid3_regular_delay(struct g_raid3_softc *sc, struct bio *bp)
1531 G_RAID3_LOGREQ(2, bp, "Delaying request.");
1532 bioq_insert_head(&sc->sc_regular_delayed, bp);
1536 * Puts synchronization request onto delayed queue.
1539 g_raid3_sync_delay(struct g_raid3_softc *sc, struct bio *bp)
1542 G_RAID3_LOGREQ(2, bp, "Delaying synchronization request.");
1543 bioq_insert_tail(&sc->sc_sync_delayed, bp);
1547 * Releases delayed regular requests which don't collide anymore with sync
1551 g_raid3_regular_release(struct g_raid3_softc *sc)
1553 struct bio *bp, *bp2;
1555 TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1556 if (g_raid3_sync_collision(sc, bp))
1558 bioq_remove(&sc->sc_regular_delayed, bp);
1559 G_RAID3_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1560 mtx_lock(&sc->sc_queue_mtx);
1561 bioq_insert_head(&sc->sc_queue, bp);
1564 * wakeup() is not needed, because this function is called from
1565 * the worker thread.
1567 wakeup(&sc->sc_queue);
1569 mtx_unlock(&sc->sc_queue_mtx);
1574 * Releases delayed sync requests which don't collide anymore with regular
1578 g_raid3_sync_release(struct g_raid3_softc *sc)
1580 struct bio *bp, *bp2;
1582 TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1583 if (g_raid3_regular_collision(sc, bp))
1585 bioq_remove(&sc->sc_sync_delayed, bp);
1586 G_RAID3_LOGREQ(2, bp,
1587 "Releasing delayed synchronization request.");
1588 g_io_request(bp, bp->bio_from);
1593 * Handle synchronization requests.
1594 * Every synchronization request is two-steps process: first, READ request is
1595 * send to active provider and then WRITE request (with read data) to the provider
1596 * beeing synchronized. When WRITE is finished, new synchronization request is
1600 g_raid3_sync_request(struct bio *bp)
1602 struct g_raid3_softc *sc;
1603 struct g_raid3_disk *disk;
1605 bp->bio_from->index--;
1606 sc = bp->bio_from->geom->softc;
1607 disk = bp->bio_from->private;
1609 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1611 g_raid3_kill_consumer(sc, bp->bio_from);
1612 g_topology_unlock();
1613 free(bp->bio_data, M_RAID3);
1615 sx_xlock(&sc->sc_lock);
1620 * Synchronization request.
1622 switch (bp->bio_cmd) {
1625 struct g_consumer *cp;
1630 if (bp->bio_error != 0) {
1631 G_RAID3_LOGREQ(0, bp,
1632 "Synchronization request failed (error=%d).",
1637 G_RAID3_LOGREQ(3, bp, "Synchronization request finished.");
1638 atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1639 dst = src = bp->bio_data;
1640 if (disk->d_no == sc->sc_ndisks - 1) {
1643 /* Parity component. */
1644 for (left = bp->bio_length; left > 0;
1645 left -= sc->sc_sectorsize) {
1646 bcopy(src, dst, atom);
1648 for (n = 1; n < sc->sc_ndisks - 1; n++) {
1649 g_raid3_xor(src, dst, atom);
1655 /* Regular component. */
1656 src += atom * disk->d_no;
1657 for (left = bp->bio_length; left > 0;
1658 left -= sc->sc_sectorsize) {
1659 bcopy(src, dst, atom);
1660 src += sc->sc_sectorsize;
1664 bp->bio_driver1 = bp->bio_driver2 = NULL;
1666 bp->bio_offset /= sc->sc_ndisks - 1;
1667 bp->bio_length /= sc->sc_ndisks - 1;
1668 bp->bio_cmd = BIO_WRITE;
1670 bp->bio_children = bp->bio_inbed = 0;
1671 cp = disk->d_consumer;
1672 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1673 ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1674 cp->acr, cp->acw, cp->ace));
1676 g_io_request(bp, cp);
1681 struct g_raid3_disk_sync *sync;
1682 off_t boffset, moffset;
1686 if (bp->bio_error != 0) {
1687 G_RAID3_LOGREQ(0, bp,
1688 "Synchronization request failed (error=%d).",
1691 sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1692 g_raid3_event_send(disk,
1693 G_RAID3_DISK_STATE_DISCONNECTED,
1694 G_RAID3_EVENT_DONTWAIT);
1697 G_RAID3_LOGREQ(3, bp, "Synchronization request finished.");
1698 sync = &disk->d_sync;
1699 if (sync->ds_offset == sc->sc_mediasize / (sc->sc_ndisks - 1) ||
1700 sync->ds_consumer == NULL ||
1701 (sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
1702 /* Don't send more synchronization requests. */
1703 sync->ds_inflight--;
1704 if (sync->ds_bios != NULL) {
1705 i = (int)(uintptr_t)bp->bio_caller1;
1706 sync->ds_bios[i] = NULL;
1708 free(bp->bio_data, M_RAID3);
1710 if (sync->ds_inflight > 0)
1712 if (sync->ds_consumer == NULL ||
1713 (sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
1717 * Disk up-to-date, activate it.
1719 g_raid3_event_send(disk, G_RAID3_DISK_STATE_ACTIVE,
1720 G_RAID3_EVENT_DONTWAIT);
1724 /* Send next synchronization request. */
1725 data = bp->bio_data;
1726 bzero(bp, sizeof(*bp));
1727 bp->bio_cmd = BIO_READ;
1728 bp->bio_offset = sync->ds_offset * (sc->sc_ndisks - 1);
1729 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1730 sync->ds_offset += bp->bio_length / (sc->sc_ndisks - 1);
1731 bp->bio_done = g_raid3_sync_done;
1732 bp->bio_data = data;
1733 bp->bio_from = sync->ds_consumer;
1734 bp->bio_to = sc->sc_provider;
1735 G_RAID3_LOGREQ(3, bp, "Sending synchronization request.");
1736 sync->ds_consumer->index++;
1738 * Delay the request if it is colliding with a regular request.
1740 if (g_raid3_regular_collision(sc, bp))
1741 g_raid3_sync_delay(sc, bp);
1743 g_io_request(bp, sync->ds_consumer);
1745 /* Release delayed requests if possible. */
1746 g_raid3_regular_release(sc);
1748 /* Find the smallest offset. */
1749 moffset = sc->sc_mediasize;
1750 for (i = 0; i < g_raid3_syncreqs; i++) {
1751 bp = sync->ds_bios[i];
1752 boffset = bp->bio_offset;
1753 if (bp->bio_cmd == BIO_WRITE)
1754 boffset *= sc->sc_ndisks - 1;
1755 if (boffset < moffset)
1758 if (sync->ds_offset_done + (MAXPHYS * 100) < moffset) {
1759 /* Update offset_done on every 100 blocks. */
1760 sync->ds_offset_done = moffset;
1761 g_raid3_update_metadata(disk);
1766 KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1767 bp->bio_cmd, sc->sc_name));
1773 g_raid3_register_request(struct bio *pbp)
1775 struct g_raid3_softc *sc;
1776 struct g_raid3_disk *disk;
1777 struct g_consumer *cp;
1778 struct bio *cbp, *tmpbp;
1779 off_t offset, length;
1781 int round_robin, verify;
1784 sc = pbp->bio_to->geom->softc;
1785 if ((pbp->bio_cflags & G_RAID3_BIO_CFLAG_REGSYNC) != 0 &&
1786 sc->sc_syncdisk == NULL) {
1787 g_io_deliver(pbp, EIO);
1790 g_raid3_init_bio(pbp);
1791 length = pbp->bio_length / (sc->sc_ndisks - 1);
1792 offset = pbp->bio_offset / (sc->sc_ndisks - 1);
1793 round_robin = verify = 0;
1794 switch (pbp->bio_cmd) {
1796 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
1797 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1798 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_VERIFY;
1800 ndisks = sc->sc_ndisks;
1803 ndisks = sc->sc_ndisks - 1;
1805 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0 &&
1806 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1811 KASSERT(!round_robin || !verify,
1812 ("ROUND-ROBIN and VERIFY are mutually exclusive."));
1813 pbp->bio_driver2 = &sc->sc_disks[sc->sc_ndisks - 1];
1818 * Delay the request if it is colliding with a synchronization
1821 if (g_raid3_sync_collision(sc, pbp)) {
1822 g_raid3_regular_delay(sc, pbp);
1829 sc->sc_last_write = time_uptime;
1831 ndisks = sc->sc_ndisks;
1834 for (n = 0; n < ndisks; n++) {
1835 disk = &sc->sc_disks[n];
1836 cbp = g_raid3_clone_bio(sc, pbp);
1838 while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL)
1839 g_raid3_destroy_bio(sc, cbp);
1841 * To prevent deadlock, we must run back up
1842 * with the ENOMEM for failed requests of any
1843 * of our consumers. Our own sync requests
1844 * can stick around, as they are finite.
1846 if ((pbp->bio_cflags &
1847 G_RAID3_BIO_CFLAG_REGULAR) != 0) {
1848 g_io_deliver(pbp, ENOMEM);
1853 cbp->bio_offset = offset;
1854 cbp->bio_length = length;
1855 cbp->bio_done = g_raid3_done;
1856 switch (pbp->bio_cmd) {
1858 if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE) {
1860 * Replace invalid component with the parity
1863 disk = &sc->sc_disks[sc->sc_ndisks - 1];
1864 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1865 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1866 } else if (round_robin &&
1867 disk->d_no == sc->sc_round_robin) {
1869 * In round-robin mode skip one data component
1870 * and use parity component when reading.
1872 pbp->bio_driver2 = disk;
1873 disk = &sc->sc_disks[sc->sc_ndisks - 1];
1874 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1875 sc->sc_round_robin++;
1877 } else if (verify && disk->d_no == sc->sc_ndisks - 1) {
1878 cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1883 if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
1884 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
1885 if (n == ndisks - 1) {
1887 * Active parity component, mark it as such.
1890 G_RAID3_BIO_CFLAG_PARITY;
1893 pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1894 if (n == ndisks - 1) {
1896 * Parity component is not connected,
1897 * so destroy its request.
1900 G_RAID3_BIO_PFLAG_NOPARITY;
1901 g_raid3_destroy_bio(sc, cbp);
1905 G_RAID3_BIO_CFLAG_NODISK;
1912 cbp->bio_caller2 = disk;
1914 switch (pbp->bio_cmd) {
1918 * If we are in round-robin mode and 'round_robin' is
1919 * still 1, it means, that we skipped parity component
1920 * for this read and must reset sc_round_robin field.
1922 sc->sc_round_robin = 0;
1924 G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1925 disk = cbp->bio_caller2;
1926 cp = disk->d_consumer;
1927 cbp->bio_to = cp->provider;
1928 G_RAID3_LOGREQ(3, cbp, "Sending request.");
1929 KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1930 ("Consumer %s not opened (r%dw%de%d).",
1931 cp->provider->name, cp->acr, cp->acw, cp->ace));
1933 g_io_request(cbp, cp);
1939 * Put request onto inflight queue, so we can check if new
1940 * synchronization requests don't collide with it.
1942 bioq_insert_tail(&sc->sc_inflight, pbp);
1945 * Bump syncid on first write.
1947 if ((sc->sc_bump_id & G_RAID3_BUMP_SYNCID) != 0) {
1948 sc->sc_bump_id &= ~G_RAID3_BUMP_SYNCID;
1949 g_raid3_bump_syncid(sc);
1951 g_raid3_scatter(pbp);
1958 g_raid3_can_destroy(struct g_raid3_softc *sc)
1961 struct g_consumer *cp;
1963 g_topology_assert();
1965 if (gp->softc == NULL)
1967 LIST_FOREACH(cp, &gp->consumer, consumer) {
1968 if (g_raid3_is_busy(sc, cp))
1971 gp = sc->sc_sync.ds_geom;
1972 LIST_FOREACH(cp, &gp->consumer, consumer) {
1973 if (g_raid3_is_busy(sc, cp))
1976 G_RAID3_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1982 g_raid3_try_destroy(struct g_raid3_softc *sc)
1985 g_topology_assert_not();
1986 sx_assert(&sc->sc_lock, SX_XLOCKED);
1988 if (sc->sc_rootmount != NULL) {
1989 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1991 root_mount_rel(sc->sc_rootmount);
1992 sc->sc_rootmount = NULL;
1996 if (!g_raid3_can_destroy(sc)) {
1997 g_topology_unlock();
2000 sc->sc_geom->softc = NULL;
2001 sc->sc_sync.ds_geom->softc = NULL;
2002 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_WAIT) != 0) {
2003 g_topology_unlock();
2004 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__,
2006 /* Unlock sc_lock here, as it can be destroyed after wakeup. */
2007 sx_xunlock(&sc->sc_lock);
2008 wakeup(&sc->sc_worker);
2009 sc->sc_worker = NULL;
2011 g_topology_unlock();
2012 g_raid3_destroy_device(sc);
2013 free(sc->sc_disks, M_RAID3);
2023 g_raid3_worker(void *arg)
2025 struct g_raid3_softc *sc;
2026 struct g_raid3_event *ep;
2031 thread_lock(curthread);
2032 sched_prio(curthread, PRIBIO);
2033 thread_unlock(curthread);
2035 sx_xlock(&sc->sc_lock);
2037 G_RAID3_DEBUG(5, "%s: Let's see...", __func__);
2039 * First take a look at events.
2040 * This is important to handle events before any I/O requests.
2042 ep = g_raid3_event_get(sc);
2044 g_raid3_event_remove(sc, ep);
2045 if ((ep->e_flags & G_RAID3_EVENT_DEVICE) != 0) {
2046 /* Update only device status. */
2048 "Running event for device %s.",
2051 g_raid3_update_device(sc, 1);
2053 /* Update disk status. */
2054 G_RAID3_DEBUG(3, "Running event for disk %s.",
2055 g_raid3_get_diskname(ep->e_disk));
2056 ep->e_error = g_raid3_update_disk(ep->e_disk,
2058 if (ep->e_error == 0)
2059 g_raid3_update_device(sc, 0);
2061 if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0) {
2062 KASSERT(ep->e_error == 0,
2063 ("Error cannot be handled."));
2064 g_raid3_event_free(ep);
2066 ep->e_flags |= G_RAID3_EVENT_DONE;
2067 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__,
2069 mtx_lock(&sc->sc_events_mtx);
2071 mtx_unlock(&sc->sc_events_mtx);
2074 G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
2075 if (g_raid3_try_destroy(sc)) {
2076 curthread->td_pflags &= ~TDP_GEOM;
2077 G_RAID3_DEBUG(1, "Thread exiting.");
2081 G_RAID3_DEBUG(5, "%s: I'm here 1.", __func__);
2085 * Check if we can mark array as CLEAN and if we can't take
2086 * how much seconds should we wait.
2088 timeout = g_raid3_idle(sc, -1);
2092 /* Get first request from the queue. */
2093 mtx_lock(&sc->sc_queue_mtx);
2094 bp = bioq_first(&sc->sc_queue);
2097 G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
2098 mtx_unlock(&sc->sc_queue_mtx);
2099 if (g_raid3_try_destroy(sc)) {
2100 curthread->td_pflags &= ~TDP_GEOM;
2101 G_RAID3_DEBUG(1, "Thread exiting.");
2104 mtx_lock(&sc->sc_queue_mtx);
2106 sx_xunlock(&sc->sc_lock);
2108 * XXX: We can miss an event here, because an event
2109 * can be added without sx-device-lock and without
2110 * mtx-queue-lock. Maybe I should just stop using
2111 * dedicated mutex for events synchronization and
2112 * stick with the queue lock?
2113 * The event will hang here until next I/O request
2114 * or next event is received.
2116 MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "r3:w1",
2118 sx_xlock(&sc->sc_lock);
2119 G_RAID3_DEBUG(5, "%s: I'm here 4.", __func__);
2123 bioq_remove(&sc->sc_queue, bp);
2124 mtx_unlock(&sc->sc_queue_mtx);
2126 if (bp->bio_from->geom == sc->sc_sync.ds_geom &&
2127 (bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC) != 0) {
2128 g_raid3_sync_request(bp); /* READ */
2129 } else if (bp->bio_to != sc->sc_provider) {
2130 if ((bp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR) != 0)
2131 g_raid3_regular_request(bp);
2132 else if ((bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC) != 0)
2133 g_raid3_sync_request(bp); /* WRITE */
2136 ("Invalid request cflags=0x%hhx to=%s.",
2137 bp->bio_cflags, bp->bio_to->name));
2139 } else if (g_raid3_register_request(bp) != 0) {
2140 mtx_lock(&sc->sc_queue_mtx);
2141 bioq_insert_head(&sc->sc_queue, bp);
2143 * We are short in memory, let see if there are finished
2144 * request we can free.
2146 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
2147 if (bp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR)
2151 * No finished regular request, so at least keep
2152 * synchronization running.
2154 TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
2155 if (bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC)
2158 sx_xunlock(&sc->sc_lock);
2159 MSLEEP(&sc->sc_queue, &sc->sc_queue_mtx, PRIBIO | PDROP,
2160 "r3:lowmem", hz / 10);
2161 sx_xlock(&sc->sc_lock);
2163 G_RAID3_DEBUG(5, "%s: I'm here 9.", __func__);
2168 g_raid3_update_idle(struct g_raid3_softc *sc, struct g_raid3_disk *disk)
2171 sx_assert(&sc->sc_lock, SX_LOCKED);
2172 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) != 0)
2174 if (!sc->sc_idle && (disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) == 0) {
2175 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as dirty.",
2176 g_raid3_get_diskname(disk), sc->sc_name);
2177 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
2178 } else if (sc->sc_idle &&
2179 (disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) != 0) {
2180 G_RAID3_DEBUG(1, "Disk %s (device %s) marked as clean.",
2181 g_raid3_get_diskname(disk), sc->sc_name);
2182 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2187 g_raid3_sync_start(struct g_raid3_softc *sc)
2189 struct g_raid3_disk *disk;
2190 struct g_consumer *cp;
2195 g_topology_assert_not();
2196 sx_assert(&sc->sc_lock, SX_XLOCKED);
2198 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED,
2199 ("Device not in DEGRADED state (%s, %u).", sc->sc_name,
2201 KASSERT(sc->sc_syncdisk == NULL, ("Syncdisk is not NULL (%s, %u).",
2202 sc->sc_name, sc->sc_state));
2204 for (n = 0; n < sc->sc_ndisks; n++) {
2205 if (sc->sc_disks[n].d_state != G_RAID3_DISK_STATE_SYNCHRONIZING)
2207 disk = &sc->sc_disks[n];
2213 sx_xunlock(&sc->sc_lock);
2215 cp = g_new_consumer(sc->sc_sync.ds_geom);
2216 error = g_attach(cp, sc->sc_provider);
2218 ("Cannot attach to %s (error=%d).", sc->sc_name, error));
2219 error = g_access(cp, 1, 0, 0);
2220 KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
2221 g_topology_unlock();
2222 sx_xlock(&sc->sc_lock);
2224 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
2225 g_raid3_get_diskname(disk));
2226 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOFAILSYNC) == 0)
2227 disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
2228 KASSERT(disk->d_sync.ds_consumer == NULL,
2229 ("Sync consumer already exists (device=%s, disk=%s).",
2230 sc->sc_name, g_raid3_get_diskname(disk)));
2232 disk->d_sync.ds_consumer = cp;
2233 disk->d_sync.ds_consumer->private = disk;
2234 disk->d_sync.ds_consumer->index = 0;
2235 sc->sc_syncdisk = disk;
2238 * Allocate memory for synchronization bios and initialize them.
2240 disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_raid3_syncreqs,
2242 for (n = 0; n < g_raid3_syncreqs; n++) {
2244 disk->d_sync.ds_bios[n] = bp;
2245 bp->bio_parent = NULL;
2246 bp->bio_cmd = BIO_READ;
2247 bp->bio_data = malloc(MAXPHYS, M_RAID3, M_WAITOK);
2249 bp->bio_offset = disk->d_sync.ds_offset * (sc->sc_ndisks - 1);
2250 bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
2251 disk->d_sync.ds_offset += bp->bio_length / (sc->sc_ndisks - 1);
2252 bp->bio_done = g_raid3_sync_done;
2253 bp->bio_from = disk->d_sync.ds_consumer;
2254 bp->bio_to = sc->sc_provider;
2255 bp->bio_caller1 = (void *)(uintptr_t)n;
2258 /* Set the number of in-flight synchronization requests. */
2259 disk->d_sync.ds_inflight = g_raid3_syncreqs;
2262 * Fire off first synchronization requests.
2264 for (n = 0; n < g_raid3_syncreqs; n++) {
2265 bp = disk->d_sync.ds_bios[n];
2266 G_RAID3_LOGREQ(3, bp, "Sending synchronization request.");
2267 disk->d_sync.ds_consumer->index++;
2269 * Delay the request if it is colliding with a regular request.
2271 if (g_raid3_regular_collision(sc, bp))
2272 g_raid3_sync_delay(sc, bp);
2274 g_io_request(bp, disk->d_sync.ds_consumer);
2279 * Stop synchronization process.
2280 * type: 0 - synchronization finished
2281 * 1 - synchronization stopped
2284 g_raid3_sync_stop(struct g_raid3_softc *sc, int type)
2286 struct g_raid3_disk *disk;
2287 struct g_consumer *cp;
2289 g_topology_assert_not();
2290 sx_assert(&sc->sc_lock, SX_LOCKED);
2292 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED,
2293 ("Device not in DEGRADED state (%s, %u).", sc->sc_name,
2295 disk = sc->sc_syncdisk;
2296 sc->sc_syncdisk = NULL;
2297 KASSERT(disk != NULL, ("No disk was synchronized (%s).", sc->sc_name));
2298 KASSERT(disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2299 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2300 g_raid3_disk_state2str(disk->d_state)));
2301 if (disk->d_sync.ds_consumer == NULL)
2305 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2306 sc->sc_name, g_raid3_get_diskname(disk));
2307 } else /* if (type == 1) */ {
2308 G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2309 sc->sc_name, g_raid3_get_diskname(disk));
2311 free(disk->d_sync.ds_bios, M_RAID3);
2312 disk->d_sync.ds_bios = NULL;
2313 cp = disk->d_sync.ds_consumer;
2314 disk->d_sync.ds_consumer = NULL;
2315 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2316 sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2318 g_raid3_kill_consumer(sc, cp);
2319 g_topology_unlock();
2320 sx_xlock(&sc->sc_lock);
2324 g_raid3_launch_provider(struct g_raid3_softc *sc)
2326 struct g_provider *pp;
2327 struct g_raid3_disk *disk;
2330 sx_assert(&sc->sc_lock, SX_LOCKED);
2333 pp = g_new_providerf(sc->sc_geom, "raid3/%s", sc->sc_name);
2334 pp->mediasize = sc->sc_mediasize;
2335 pp->sectorsize = sc->sc_sectorsize;
2337 pp->stripeoffset = 0;
2338 for (n = 0; n < sc->sc_ndisks; n++) {
2339 disk = &sc->sc_disks[n];
2340 if (disk->d_consumer && disk->d_consumer->provider &&
2341 disk->d_consumer->provider->stripesize > pp->stripesize) {
2342 pp->stripesize = disk->d_consumer->provider->stripesize;
2343 pp->stripeoffset = disk->d_consumer->provider->stripeoffset;
2346 pp->stripesize *= sc->sc_ndisks - 1;
2347 pp->stripeoffset *= sc->sc_ndisks - 1;
2348 sc->sc_provider = pp;
2349 g_error_provider(pp, 0);
2350 g_topology_unlock();
2351 G_RAID3_DEBUG(0, "Device %s launched (%u/%u).", pp->name,
2352 g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE), sc->sc_ndisks);
2354 if (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED)
2355 g_raid3_sync_start(sc);
2359 g_raid3_destroy_provider(struct g_raid3_softc *sc)
2363 g_topology_assert_not();
2364 KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2368 g_error_provider(sc->sc_provider, ENXIO);
2369 mtx_lock(&sc->sc_queue_mtx);
2370 while ((bp = bioq_first(&sc->sc_queue)) != NULL) {
2371 bioq_remove(&sc->sc_queue, bp);
2372 g_io_deliver(bp, ENXIO);
2374 mtx_unlock(&sc->sc_queue_mtx);
2375 G_RAID3_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2376 sc->sc_provider->name);
2377 sc->sc_provider->flags |= G_PF_WITHER;
2378 g_orphan_provider(sc->sc_provider, ENXIO);
2379 g_topology_unlock();
2380 sc->sc_provider = NULL;
2381 if (sc->sc_syncdisk != NULL)
2382 g_raid3_sync_stop(sc, 1);
2386 g_raid3_go(void *arg)
2388 struct g_raid3_softc *sc;
2391 G_RAID3_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2392 g_raid3_event_send(sc, 0,
2393 G_RAID3_EVENT_DONTWAIT | G_RAID3_EVENT_DEVICE);
2397 g_raid3_determine_state(struct g_raid3_disk *disk)
2399 struct g_raid3_softc *sc;
2403 if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2404 if ((disk->d_flags &
2405 G_RAID3_DISK_FLAG_SYNCHRONIZING) == 0) {
2406 /* Disk does not need synchronization. */
2407 state = G_RAID3_DISK_STATE_ACTIVE;
2410 G_RAID3_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2412 G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) {
2414 * We can start synchronization from
2415 * the stored offset.
2417 state = G_RAID3_DISK_STATE_SYNCHRONIZING;
2419 state = G_RAID3_DISK_STATE_STALE;
2422 } else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2424 * Reset all synchronization data for this disk,
2425 * because if it even was synchronized, it was
2426 * synchronized to disks with different syncid.
2428 disk->d_flags |= G_RAID3_DISK_FLAG_SYNCHRONIZING;
2429 disk->d_sync.ds_offset = 0;
2430 disk->d_sync.ds_offset_done = 0;
2431 disk->d_sync.ds_syncid = sc->sc_syncid;
2432 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2433 (disk->d_flags & G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) {
2434 state = G_RAID3_DISK_STATE_SYNCHRONIZING;
2436 state = G_RAID3_DISK_STATE_STALE;
2438 } else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2440 * Not good, NOT GOOD!
2441 * It means that device was started on stale disks
2442 * and more fresh disk just arrive.
2443 * If there were writes, device is broken, sorry.
2444 * I think the best choice here is don't touch
2445 * this disk and inform the user loudly.
2447 G_RAID3_DEBUG(0, "Device %s was started before the freshest "
2448 "disk (%s) arrives!! It will not be connected to the "
2449 "running device.", sc->sc_name,
2450 g_raid3_get_diskname(disk));
2451 g_raid3_destroy_disk(disk);
2452 state = G_RAID3_DISK_STATE_NONE;
2453 /* Return immediately, because disk was destroyed. */
2456 G_RAID3_DEBUG(3, "State for %s disk: %s.",
2457 g_raid3_get_diskname(disk), g_raid3_disk_state2str(state));
2462 * Update device state.
2465 g_raid3_update_device(struct g_raid3_softc *sc, boolean_t force)
2467 struct g_raid3_disk *disk;
2470 sx_assert(&sc->sc_lock, SX_XLOCKED);
2472 switch (sc->sc_state) {
2473 case G_RAID3_DEVICE_STATE_STARTING:
2475 u_int n, ndirty, ndisks, genid, syncid;
2477 KASSERT(sc->sc_provider == NULL,
2478 ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2480 * Are we ready? We are, if all disks are connected or
2481 * one disk is missing and 'force' is true.
2483 if (g_raid3_ndisks(sc, -1) + force == sc->sc_ndisks) {
2485 callout_drain(&sc->sc_callout);
2489 * Timeout expired, so destroy device.
2491 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2492 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p",
2493 __LINE__, sc->sc_rootmount);
2494 root_mount_rel(sc->sc_rootmount);
2495 sc->sc_rootmount = NULL;
2501 * Find the biggest genid.
2504 for (n = 0; n < sc->sc_ndisks; n++) {
2505 disk = &sc->sc_disks[n];
2506 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2508 if (disk->d_genid > genid)
2509 genid = disk->d_genid;
2511 sc->sc_genid = genid;
2513 * Remove all disks without the biggest genid.
2515 for (n = 0; n < sc->sc_ndisks; n++) {
2516 disk = &sc->sc_disks[n];
2517 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2519 if (disk->d_genid < genid) {
2521 "Component %s (device %s) broken, skipping.",
2522 g_raid3_get_diskname(disk), sc->sc_name);
2523 g_raid3_destroy_disk(disk);
2528 * There must be at least 'sc->sc_ndisks - 1' components
2529 * with the same syncid and without SYNCHRONIZING flag.
2533 * Find the biggest syncid, number of valid components and
2534 * number of dirty components.
2536 ndirty = ndisks = syncid = 0;
2537 for (n = 0; n < sc->sc_ndisks; n++) {
2538 disk = &sc->sc_disks[n];
2539 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2541 if ((disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) != 0)
2543 if (disk->d_sync.ds_syncid > syncid) {
2544 syncid = disk->d_sync.ds_syncid;
2546 } else if (disk->d_sync.ds_syncid < syncid) {
2549 if ((disk->d_flags &
2550 G_RAID3_DISK_FLAG_SYNCHRONIZING) != 0) {
2556 * Do we have enough valid components?
2558 if (ndisks + 1 < sc->sc_ndisks) {
2560 "Device %s is broken, too few valid components.",
2562 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2566 * If there is one DIRTY component and all disks are present,
2567 * mark it for synchronization. If there is more than one DIRTY
2568 * component, mark parity component for synchronization.
2570 if (ndisks == sc->sc_ndisks && ndirty == 1) {
2571 for (n = 0; n < sc->sc_ndisks; n++) {
2572 disk = &sc->sc_disks[n];
2573 if ((disk->d_flags &
2574 G_RAID3_DISK_FLAG_DIRTY) == 0) {
2578 G_RAID3_DISK_FLAG_SYNCHRONIZING;
2580 } else if (ndisks == sc->sc_ndisks && ndirty > 1) {
2581 disk = &sc->sc_disks[sc->sc_ndisks - 1];
2582 disk->d_flags |= G_RAID3_DISK_FLAG_SYNCHRONIZING;
2585 sc->sc_syncid = syncid;
2587 /* Remember to bump syncid on first write. */
2588 sc->sc_bump_id |= G_RAID3_BUMP_SYNCID;
2590 if (ndisks == sc->sc_ndisks)
2591 state = G_RAID3_DEVICE_STATE_COMPLETE;
2592 else /* if (ndisks == sc->sc_ndisks - 1) */
2593 state = G_RAID3_DEVICE_STATE_DEGRADED;
2594 G_RAID3_DEBUG(1, "Device %s state changed from %s to %s.",
2595 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2596 g_raid3_device_state2str(state));
2597 sc->sc_state = state;
2598 for (n = 0; n < sc->sc_ndisks; n++) {
2599 disk = &sc->sc_disks[n];
2600 if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2602 state = g_raid3_determine_state(disk);
2603 g_raid3_event_send(disk, state, G_RAID3_EVENT_DONTWAIT);
2604 if (state == G_RAID3_DISK_STATE_STALE)
2605 sc->sc_bump_id |= G_RAID3_BUMP_SYNCID;
2609 case G_RAID3_DEVICE_STATE_DEGRADED:
2611 * Genid need to be bumped immediately, so do it here.
2613 if ((sc->sc_bump_id & G_RAID3_BUMP_GENID) != 0) {
2614 sc->sc_bump_id &= ~G_RAID3_BUMP_GENID;
2615 g_raid3_bump_genid(sc);
2618 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NEW) > 0)
2620 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) <
2621 sc->sc_ndisks - 1) {
2622 if (sc->sc_provider != NULL)
2623 g_raid3_destroy_provider(sc);
2624 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2627 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) ==
2629 state = G_RAID3_DEVICE_STATE_COMPLETE;
2631 "Device %s state changed from %s to %s.",
2632 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2633 g_raid3_device_state2str(state));
2634 sc->sc_state = state;
2636 if (sc->sc_provider == NULL)
2637 g_raid3_launch_provider(sc);
2638 if (sc->sc_rootmount != NULL) {
2639 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2641 root_mount_rel(sc->sc_rootmount);
2642 sc->sc_rootmount = NULL;
2645 case G_RAID3_DEVICE_STATE_COMPLETE:
2647 * Genid need to be bumped immediately, so do it here.
2649 if ((sc->sc_bump_id & G_RAID3_BUMP_GENID) != 0) {
2650 sc->sc_bump_id &= ~G_RAID3_BUMP_GENID;
2651 g_raid3_bump_genid(sc);
2654 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NEW) > 0)
2656 KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) >=
2658 ("Too few ACTIVE components in COMPLETE state (device %s).",
2660 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) ==
2661 sc->sc_ndisks - 1) {
2662 state = G_RAID3_DEVICE_STATE_DEGRADED;
2664 "Device %s state changed from %s to %s.",
2665 sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2666 g_raid3_device_state2str(state));
2667 sc->sc_state = state;
2669 if (sc->sc_provider == NULL)
2670 g_raid3_launch_provider(sc);
2671 if (sc->sc_rootmount != NULL) {
2672 G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2674 root_mount_rel(sc->sc_rootmount);
2675 sc->sc_rootmount = NULL;
2679 KASSERT(1 == 0, ("Wrong device state (%s, %s).", sc->sc_name,
2680 g_raid3_device_state2str(sc->sc_state)));
2686 * Update disk state and device state if needed.
2688 #define DISK_STATE_CHANGED() G_RAID3_DEBUG(1, \
2689 "Disk %s state changed from %s to %s (device %s).", \
2690 g_raid3_get_diskname(disk), \
2691 g_raid3_disk_state2str(disk->d_state), \
2692 g_raid3_disk_state2str(state), sc->sc_name)
2694 g_raid3_update_disk(struct g_raid3_disk *disk, u_int state)
2696 struct g_raid3_softc *sc;
2699 sx_assert(&sc->sc_lock, SX_XLOCKED);
2702 G_RAID3_DEBUG(3, "Changing disk %s state from %s to %s.",
2703 g_raid3_get_diskname(disk), g_raid3_disk_state2str(disk->d_state),
2704 g_raid3_disk_state2str(state));
2706 case G_RAID3_DISK_STATE_NEW:
2708 * Possible scenarios:
2709 * 1. New disk arrive.
2711 /* Previous state should be NONE. */
2712 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NONE,
2713 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2714 g_raid3_disk_state2str(disk->d_state)));
2715 DISK_STATE_CHANGED();
2717 disk->d_state = state;
2718 G_RAID3_DEBUG(1, "Device %s: provider %s detected.",
2719 sc->sc_name, g_raid3_get_diskname(disk));
2720 if (sc->sc_state == G_RAID3_DEVICE_STATE_STARTING)
2722 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2723 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2724 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2725 g_raid3_device_state2str(sc->sc_state),
2726 g_raid3_get_diskname(disk),
2727 g_raid3_disk_state2str(disk->d_state)));
2728 state = g_raid3_determine_state(disk);
2729 if (state != G_RAID3_DISK_STATE_NONE)
2732 case G_RAID3_DISK_STATE_ACTIVE:
2734 * Possible scenarios:
2735 * 1. New disk does not need synchronization.
2736 * 2. Synchronization process finished successfully.
2738 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2739 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2740 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2741 g_raid3_device_state2str(sc->sc_state),
2742 g_raid3_get_diskname(disk),
2743 g_raid3_disk_state2str(disk->d_state)));
2744 /* Previous state should be NEW or SYNCHRONIZING. */
2745 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW ||
2746 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2747 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2748 g_raid3_disk_state2str(disk->d_state)));
2749 DISK_STATE_CHANGED();
2751 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
2752 disk->d_flags &= ~G_RAID3_DISK_FLAG_SYNCHRONIZING;
2753 disk->d_flags &= ~G_RAID3_DISK_FLAG_FORCE_SYNC;
2754 g_raid3_sync_stop(sc, 0);
2756 disk->d_state = state;
2757 disk->d_sync.ds_offset = 0;
2758 disk->d_sync.ds_offset_done = 0;
2759 g_raid3_update_idle(sc, disk);
2760 g_raid3_update_metadata(disk);
2761 G_RAID3_DEBUG(1, "Device %s: provider %s activated.",
2762 sc->sc_name, g_raid3_get_diskname(disk));
2764 case G_RAID3_DISK_STATE_STALE:
2766 * Possible scenarios:
2767 * 1. Stale disk was connected.
2769 /* Previous state should be NEW. */
2770 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2771 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2772 g_raid3_disk_state2str(disk->d_state)));
2773 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2774 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2775 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2776 g_raid3_device_state2str(sc->sc_state),
2777 g_raid3_get_diskname(disk),
2778 g_raid3_disk_state2str(disk->d_state)));
2780 * STALE state is only possible if device is marked
2783 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0,
2784 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2785 g_raid3_device_state2str(sc->sc_state),
2786 g_raid3_get_diskname(disk),
2787 g_raid3_disk_state2str(disk->d_state)));
2788 DISK_STATE_CHANGED();
2790 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2791 disk->d_state = state;
2792 g_raid3_update_metadata(disk);
2793 G_RAID3_DEBUG(0, "Device %s: provider %s is stale.",
2794 sc->sc_name, g_raid3_get_diskname(disk));
2796 case G_RAID3_DISK_STATE_SYNCHRONIZING:
2798 * Possible scenarios:
2799 * 1. Disk which needs synchronization was connected.
2801 /* Previous state should be NEW. */
2802 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2803 ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2804 g_raid3_disk_state2str(disk->d_state)));
2805 KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2806 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2807 ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2808 g_raid3_device_state2str(sc->sc_state),
2809 g_raid3_get_diskname(disk),
2810 g_raid3_disk_state2str(disk->d_state)));
2811 DISK_STATE_CHANGED();
2813 if (disk->d_state == G_RAID3_DISK_STATE_NEW)
2814 disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2815 disk->d_state = state;
2816 if (sc->sc_provider != NULL) {
2817 g_raid3_sync_start(sc);
2818 g_raid3_update_metadata(disk);
2821 case G_RAID3_DISK_STATE_DISCONNECTED:
2823 * Possible scenarios:
2824 * 1. Device wasn't running yet, but disk disappear.
2825 * 2. Disk was active and disapppear.
2826 * 3. Disk disappear during synchronization process.
2828 if (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2829 sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
2831 * Previous state should be ACTIVE, STALE or
2834 KASSERT(disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
2835 disk->d_state == G_RAID3_DISK_STATE_STALE ||
2836 disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2837 ("Wrong disk state (%s, %s).",
2838 g_raid3_get_diskname(disk),
2839 g_raid3_disk_state2str(disk->d_state)));
2840 } else if (sc->sc_state == G_RAID3_DEVICE_STATE_STARTING) {
2841 /* Previous state should be NEW. */
2842 KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2843 ("Wrong disk state (%s, %s).",
2844 g_raid3_get_diskname(disk),
2845 g_raid3_disk_state2str(disk->d_state)));
2847 * Reset bumping syncid if disk disappeared in STARTING
2850 if ((sc->sc_bump_id & G_RAID3_BUMP_SYNCID) != 0)
2851 sc->sc_bump_id &= ~G_RAID3_BUMP_SYNCID;
2854 KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2856 g_raid3_device_state2str(sc->sc_state),
2857 g_raid3_get_diskname(disk),
2858 g_raid3_disk_state2str(disk->d_state)));
2861 DISK_STATE_CHANGED();
2862 G_RAID3_DEBUG(0, "Device %s: provider %s disconnected.",
2863 sc->sc_name, g_raid3_get_diskname(disk));
2865 g_raid3_destroy_disk(disk);
2868 KASSERT(1 == 0, ("Unknown state (%u).", state));
2873 #undef DISK_STATE_CHANGED
2876 g_raid3_read_metadata(struct g_consumer *cp, struct g_raid3_metadata *md)
2878 struct g_provider *pp;
2882 g_topology_assert();
2884 error = g_access(cp, 1, 0, 0);
2888 g_topology_unlock();
2889 /* Metadata are stored on last sector. */
2890 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2893 g_access(cp, -1, 0, 0);
2895 G_RAID3_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2896 cp->provider->name, error);
2900 /* Decode metadata. */
2901 error = raid3_metadata_decode(buf, md);
2903 if (strcmp(md->md_magic, G_RAID3_MAGIC) != 0)
2905 if (md->md_version > G_RAID3_VERSION) {
2907 "Kernel module is too old to handle metadata from %s.",
2908 cp->provider->name);
2912 G_RAID3_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2913 cp->provider->name);
2921 g_raid3_check_metadata(struct g_raid3_softc *sc, struct g_provider *pp,
2922 struct g_raid3_metadata *md)
2925 if (md->md_no >= sc->sc_ndisks) {
2926 G_RAID3_DEBUG(1, "Invalid disk %s number (no=%u), skipping.",
2927 pp->name, md->md_no);
2930 if (sc->sc_disks[md->md_no].d_state != G_RAID3_DISK_STATE_NODISK) {
2931 G_RAID3_DEBUG(1, "Disk %s (no=%u) already exists, skipping.",
2932 pp->name, md->md_no);
2935 if (md->md_all != sc->sc_ndisks) {
2937 "Invalid '%s' field on disk %s (device %s), skipping.",
2938 "md_all", pp->name, sc->sc_name);
2941 if ((md->md_mediasize % md->md_sectorsize) != 0) {
2942 G_RAID3_DEBUG(1, "Invalid metadata (mediasize %% sectorsize != "
2943 "0) on disk %s (device %s), skipping.", pp->name,
2947 if (md->md_mediasize != sc->sc_mediasize) {
2949 "Invalid '%s' field on disk %s (device %s), skipping.",
2950 "md_mediasize", pp->name, sc->sc_name);
2953 if ((md->md_mediasize % (sc->sc_ndisks - 1)) != 0) {
2955 "Invalid '%s' field on disk %s (device %s), skipping.",
2956 "md_mediasize", pp->name, sc->sc_name);
2959 if ((sc->sc_mediasize / (sc->sc_ndisks - 1)) > pp->mediasize) {
2961 "Invalid size of disk %s (device %s), skipping.", pp->name,
2965 if ((md->md_sectorsize / pp->sectorsize) < sc->sc_ndisks - 1) {
2967 "Invalid '%s' field on disk %s (device %s), skipping.",
2968 "md_sectorsize", pp->name, sc->sc_name);
2971 if (md->md_sectorsize != sc->sc_sectorsize) {
2973 "Invalid '%s' field on disk %s (device %s), skipping.",
2974 "md_sectorsize", pp->name, sc->sc_name);
2977 if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2979 "Invalid sector size of disk %s (device %s), skipping.",
2980 pp->name, sc->sc_name);
2983 if ((md->md_mflags & ~G_RAID3_DEVICE_FLAG_MASK) != 0) {
2985 "Invalid device flags on disk %s (device %s), skipping.",
2986 pp->name, sc->sc_name);
2989 if ((md->md_mflags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
2990 (md->md_mflags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) {
2992 * VERIFY and ROUND-ROBIN options are mutally exclusive.
2994 G_RAID3_DEBUG(1, "Both VERIFY and ROUND-ROBIN flags exist on "
2995 "disk %s (device %s), skipping.", pp->name, sc->sc_name);
2998 if ((md->md_dflags & ~G_RAID3_DISK_FLAG_MASK) != 0) {
3000 "Invalid disk flags on disk %s (device %s), skipping.",
3001 pp->name, sc->sc_name);
3008 g_raid3_add_disk(struct g_raid3_softc *sc, struct g_provider *pp,
3009 struct g_raid3_metadata *md)
3011 struct g_raid3_disk *disk;
3014 g_topology_assert_not();
3015 G_RAID3_DEBUG(2, "Adding disk %s.", pp->name);
3017 error = g_raid3_check_metadata(sc, pp, md);
3020 if (sc->sc_state != G_RAID3_DEVICE_STATE_STARTING &&
3021 md->md_genid < sc->sc_genid) {
3022 G_RAID3_DEBUG(0, "Component %s (device %s) broken, skipping.",
3023 pp->name, sc->sc_name);
3026 disk = g_raid3_init_disk(sc, pp, md, &error);
3029 error = g_raid3_event_send(disk, G_RAID3_DISK_STATE_NEW,
3030 G_RAID3_EVENT_WAIT);
3033 if (md->md_version < G_RAID3_VERSION) {
3034 G_RAID3_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
3035 pp->name, md->md_version, G_RAID3_VERSION);
3036 g_raid3_update_metadata(disk);
3042 g_raid3_destroy_delayed(void *arg, int flag)
3044 struct g_raid3_softc *sc;
3047 if (flag == EV_CANCEL) {
3048 G_RAID3_DEBUG(1, "Destroying canceled.");
3052 g_topology_unlock();
3053 sx_xlock(&sc->sc_lock);
3054 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) == 0,
3055 ("DESTROY flag set on %s.", sc->sc_name));
3056 KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROYING) != 0,
3057 ("DESTROYING flag not set on %s.", sc->sc_name));
3058 G_RAID3_DEBUG(0, "Destroying %s (delayed).", sc->sc_name);
3059 error = g_raid3_destroy(sc, G_RAID3_DESTROY_SOFT);
3061 G_RAID3_DEBUG(0, "Cannot destroy %s.", sc->sc_name);
3062 sx_xunlock(&sc->sc_lock);
3068 g_raid3_access(struct g_provider *pp, int acr, int acw, int ace)
3070 struct g_raid3_softc *sc;
3071 int dcr, dcw, dce, error = 0;
3073 g_topology_assert();
3074 G_RAID3_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
3077 sc = pp->geom->softc;
3078 if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
3080 KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
3082 dcr = pp->acr + acr;
3083 dcw = pp->acw + acw;
3084 dce = pp->ace + ace;
3086 g_topology_unlock();
3087 sx_xlock(&sc->sc_lock);
3088 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0 ||
3089 g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) < sc->sc_ndisks - 1) {
3090 if (acr > 0 || acw > 0 || ace > 0)
3094 if (dcw == 0 && !sc->sc_idle)
3095 g_raid3_idle(sc, dcw);
3096 if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROYING) != 0) {
3097 if (acr > 0 || acw > 0 || ace > 0) {
3101 if (dcr == 0 && dcw == 0 && dce == 0) {
3102 g_post_event(g_raid3_destroy_delayed, sc, M_WAITOK,
3107 sx_xunlock(&sc->sc_lock);
3112 static struct g_geom *
3113 g_raid3_create(struct g_class *mp, const struct g_raid3_metadata *md)
3115 struct g_raid3_softc *sc;
3120 g_topology_assert();
3121 G_RAID3_DEBUG(1, "Creating device %s (id=%u).", md->md_name, md->md_id);
3123 /* One disk is minimum. */
3129 gp = g_new_geomf(mp, "%s", md->md_name);
3130 sc = malloc(sizeof(*sc), M_RAID3, M_WAITOK | M_ZERO);
3131 sc->sc_disks = malloc(sizeof(struct g_raid3_disk) * md->md_all, M_RAID3,
3133 gp->start = g_raid3_start;
3134 gp->orphan = g_raid3_orphan;
3135 gp->access = g_raid3_access;
3136 gp->dumpconf = g_raid3_dumpconf;
3138 sc->sc_id = md->md_id;
3139 sc->sc_mediasize = md->md_mediasize;
3140 sc->sc_sectorsize = md->md_sectorsize;
3141 sc->sc_ndisks = md->md_all;
3142 sc->sc_round_robin = 0;
3143 sc->sc_flags = md->md_mflags;
3146 sc->sc_last_write = time_uptime;
3148 for (n = 0; n < sc->sc_ndisks; n++) {
3149 sc->sc_disks[n].d_softc = sc;
3150 sc->sc_disks[n].d_no = n;
3151 sc->sc_disks[n].d_state = G_RAID3_DISK_STATE_NODISK;
3153 sx_init(&sc->sc_lock, "graid3:lock");
3154 bioq_init(&sc->sc_queue);
3155 mtx_init(&sc->sc_queue_mtx, "graid3:queue", NULL, MTX_DEF);
3156 bioq_init(&sc->sc_regular_delayed);
3157 bioq_init(&sc->sc_inflight);
3158 bioq_init(&sc->sc_sync_delayed);
3159 TAILQ_INIT(&sc->sc_events);
3160 mtx_init(&sc->sc_events_mtx, "graid3:events", NULL, MTX_DEF);
3161 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
3162 sc->sc_state = G_RAID3_DEVICE_STATE_STARTING;
3165 sc->sc_provider = NULL;
3167 * Synchronization geom.
3169 gp = g_new_geomf(mp, "%s.sync", md->md_name);
3171 gp->orphan = g_raid3_orphan;
3172 sc->sc_sync.ds_geom = gp;
3174 if (!g_raid3_use_malloc) {
3175 sc->sc_zones[G_RAID3_ZONE_64K].sz_zone = uma_zcreate("gr3:64k",
3176 65536, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3178 sc->sc_zones[G_RAID3_ZONE_64K].sz_inuse = 0;
3179 sc->sc_zones[G_RAID3_ZONE_64K].sz_max = g_raid3_n64k;
3180 sc->sc_zones[G_RAID3_ZONE_64K].sz_requested =
3181 sc->sc_zones[G_RAID3_ZONE_64K].sz_failed = 0;
3182 sc->sc_zones[G_RAID3_ZONE_16K].sz_zone = uma_zcreate("gr3:16k",
3183 16384, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3185 sc->sc_zones[G_RAID3_ZONE_16K].sz_inuse = 0;
3186 sc->sc_zones[G_RAID3_ZONE_16K].sz_max = g_raid3_n16k;
3187 sc->sc_zones[G_RAID3_ZONE_16K].sz_requested =
3188 sc->sc_zones[G_RAID3_ZONE_16K].sz_failed = 0;
3189 sc->sc_zones[G_RAID3_ZONE_4K].sz_zone = uma_zcreate("gr3:4k",
3190 4096, g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL,
3192 sc->sc_zones[G_RAID3_ZONE_4K].sz_inuse = 0;
3193 sc->sc_zones[G_RAID3_ZONE_4K].sz_max = g_raid3_n4k;
3194 sc->sc_zones[G_RAID3_ZONE_4K].sz_requested =
3195 sc->sc_zones[G_RAID3_ZONE_4K].sz_failed = 0;
3198 error = kproc_create(g_raid3_worker, sc, &sc->sc_worker, 0, 0,
3199 "g_raid3 %s", md->md_name);
3201 G_RAID3_DEBUG(1, "Cannot create kernel thread for %s.",
3203 if (!g_raid3_use_malloc) {
3204 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_64K].sz_zone);
3205 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_16K].sz_zone);
3206 uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_4K].sz_zone);
3208 g_destroy_geom(sc->sc_sync.ds_geom);
3209 mtx_destroy(&sc->sc_events_mtx);
3210 mtx_destroy(&sc->sc_queue_mtx);
3211 sx_destroy(&sc->sc_lock);
3212 g_destroy_geom(sc->sc_geom);
3213 free(sc->sc_disks, M_RAID3);
3218 G_RAID3_DEBUG(1, "Device %s created (%u components, id=%u).",
3219 sc->sc_name, sc->sc_ndisks, sc->sc_id);
3221 sc->sc_rootmount = root_mount_hold("GRAID3");
3222 G_RAID3_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
3227 timeout = atomic_load_acq_int(&g_raid3_timeout);
3228 callout_reset(&sc->sc_callout, timeout * hz, g_raid3_go, sc);
3229 return (sc->sc_geom);
3233 g_raid3_destroy(struct g_raid3_softc *sc, int how)
3235 struct g_provider *pp;
3237 g_topology_assert_not();
3240 sx_assert(&sc->sc_lock, SX_XLOCKED);
3242 pp = sc->sc_provider;
3243 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
3245 case G_RAID3_DESTROY_SOFT:
3247 "Device %s is still open (r%dw%de%d).", pp->name,
3248 pp->acr, pp->acw, pp->ace);
3250 case G_RAID3_DESTROY_DELAYED:
3252 "Device %s will be destroyed on last close.",
3254 if (sc->sc_syncdisk != NULL)
3255 g_raid3_sync_stop(sc, 1);
3256 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROYING;
3258 case G_RAID3_DESTROY_HARD:
3259 G_RAID3_DEBUG(1, "Device %s is still open, so it "
3260 "can't be definitely removed.", pp->name);
3266 if (sc->sc_geom->softc == NULL) {
3267 g_topology_unlock();
3270 sc->sc_geom->softc = NULL;
3271 sc->sc_sync.ds_geom->softc = NULL;
3272 g_topology_unlock();
3274 sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
3275 sc->sc_flags |= G_RAID3_DEVICE_FLAG_WAIT;
3276 G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3277 sx_xunlock(&sc->sc_lock);
3278 mtx_lock(&sc->sc_queue_mtx);
3280 wakeup(&sc->sc_queue);
3281 mtx_unlock(&sc->sc_queue_mtx);
3282 G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3283 while (sc->sc_worker != NULL)
3284 tsleep(&sc->sc_worker, PRIBIO, "r3:destroy", hz / 5);
3285 G_RAID3_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3286 sx_xlock(&sc->sc_lock);
3287 g_raid3_destroy_device(sc);
3288 free(sc->sc_disks, M_RAID3);
3294 g_raid3_taste_orphan(struct g_consumer *cp)
3297 KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3298 cp->provider->name));
3301 static struct g_geom *
3302 g_raid3_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3304 struct g_raid3_metadata md;
3305 struct g_raid3_softc *sc;
3306 struct g_consumer *cp;
3310 g_topology_assert();
3311 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3312 G_RAID3_DEBUG(2, "Tasting %s.", pp->name);
3314 gp = g_new_geomf(mp, "raid3:taste");
3315 /* This orphan function should be never called. */
3316 gp->orphan = g_raid3_taste_orphan;
3317 cp = g_new_consumer(gp);
3319 error = g_raid3_read_metadata(cp, &md);
3321 g_destroy_consumer(cp);
3327 if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0)
3329 if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3331 if (g_raid3_debug >= 2)
3332 raid3_metadata_dump(&md);
3335 * Let's check if device already exists.
3338 LIST_FOREACH(gp, &mp->geom, geom) {
3342 if (sc->sc_sync.ds_geom == gp)
3344 if (strcmp(md.md_name, sc->sc_name) != 0)
3346 if (md.md_id != sc->sc_id) {
3347 G_RAID3_DEBUG(0, "Device %s already configured.",
3354 gp = g_raid3_create(mp, &md);
3356 G_RAID3_DEBUG(0, "Cannot create device %s.",
3362 G_RAID3_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3363 g_topology_unlock();
3364 sx_xlock(&sc->sc_lock);
3365 error = g_raid3_add_disk(sc, pp, &md);
3367 G_RAID3_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3368 pp->name, gp->name, error);
3369 if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NODISK) ==
3372 g_raid3_destroy(sc, G_RAID3_DESTROY_HARD);
3378 sx_xunlock(&sc->sc_lock);
3384 g_raid3_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
3387 struct g_raid3_softc *sc;
3390 g_topology_unlock();
3392 sx_xlock(&sc->sc_lock);
3394 error = g_raid3_destroy(gp->softc, G_RAID3_DESTROY_SOFT);
3396 sx_xunlock(&sc->sc_lock);
3402 g_raid3_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3403 struct g_consumer *cp, struct g_provider *pp)
3405 struct g_raid3_softc *sc;
3407 g_topology_assert();
3412 /* Skip synchronization geom. */
3413 if (gp == sc->sc_sync.ds_geom)
3417 } else if (cp != NULL) {
3418 struct g_raid3_disk *disk;
3423 g_topology_unlock();
3424 sx_xlock(&sc->sc_lock);
3425 sbuf_printf(sb, "%s<Type>", indent);
3426 if (disk->d_no == sc->sc_ndisks - 1)
3427 sbuf_printf(sb, "PARITY");
3429 sbuf_printf(sb, "DATA");
3430 sbuf_printf(sb, "</Type>\n");
3431 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
3433 if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
3434 sbuf_printf(sb, "%s<Synchronized>", indent);
3435 if (disk->d_sync.ds_offset == 0)
3436 sbuf_printf(sb, "0%%");
3438 sbuf_printf(sb, "%u%%",
3439 (u_int)((disk->d_sync.ds_offset * 100) /
3440 (sc->sc_mediasize / (sc->sc_ndisks - 1))));
3442 sbuf_printf(sb, "</Synchronized>\n");
3444 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3445 disk->d_sync.ds_syncid);
3446 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, disk->d_genid);
3447 sbuf_printf(sb, "%s<Flags>", indent);
3448 if (disk->d_flags == 0)
3449 sbuf_printf(sb, "NONE");
3453 #define ADD_FLAG(flag, name) do { \
3454 if ((disk->d_flags & (flag)) != 0) { \
3456 sbuf_printf(sb, ", "); \
3459 sbuf_printf(sb, name); \
3462 ADD_FLAG(G_RAID3_DISK_FLAG_DIRTY, "DIRTY");
3463 ADD_FLAG(G_RAID3_DISK_FLAG_HARDCODED, "HARDCODED");
3464 ADD_FLAG(G_RAID3_DISK_FLAG_SYNCHRONIZING,
3466 ADD_FLAG(G_RAID3_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3467 ADD_FLAG(G_RAID3_DISK_FLAG_BROKEN, "BROKEN");
3470 sbuf_printf(sb, "</Flags>\n");
3471 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3472 g_raid3_disk_state2str(disk->d_state));
3473 sx_xunlock(&sc->sc_lock);
3476 g_topology_unlock();
3477 sx_xlock(&sc->sc_lock);
3478 if (!g_raid3_use_malloc) {
3480 "%s<Zone4kRequested>%u</Zone4kRequested>\n", indent,
3481 sc->sc_zones[G_RAID3_ZONE_4K].sz_requested);
3483 "%s<Zone4kFailed>%u</Zone4kFailed>\n", indent,
3484 sc->sc_zones[G_RAID3_ZONE_4K].sz_failed);
3486 "%s<Zone16kRequested>%u</Zone16kRequested>\n", indent,
3487 sc->sc_zones[G_RAID3_ZONE_16K].sz_requested);
3489 "%s<Zone16kFailed>%u</Zone16kFailed>\n", indent,
3490 sc->sc_zones[G_RAID3_ZONE_16K].sz_failed);
3492 "%s<Zone64kRequested>%u</Zone64kRequested>\n", indent,
3493 sc->sc_zones[G_RAID3_ZONE_64K].sz_requested);
3495 "%s<Zone64kFailed>%u</Zone64kFailed>\n", indent,
3496 sc->sc_zones[G_RAID3_ZONE_64K].sz_failed);
3498 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3499 sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3500 sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3501 sbuf_printf(sb, "%s<Flags>", indent);
3502 if (sc->sc_flags == 0)
3503 sbuf_printf(sb, "NONE");
3507 #define ADD_FLAG(flag, name) do { \
3508 if ((sc->sc_flags & (flag)) != 0) { \
3510 sbuf_printf(sb, ", "); \
3513 sbuf_printf(sb, name); \
3516 ADD_FLAG(G_RAID3_DEVICE_FLAG_NOFAILSYNC, "NOFAILSYNC");
3517 ADD_FLAG(G_RAID3_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3518 ADD_FLAG(G_RAID3_DEVICE_FLAG_ROUND_ROBIN,
3520 ADD_FLAG(G_RAID3_DEVICE_FLAG_VERIFY, "VERIFY");
3523 sbuf_printf(sb, "</Flags>\n");
3524 sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3526 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3527 g_raid3_device_state2str(sc->sc_state));
3528 sx_xunlock(&sc->sc_lock);
3534 g_raid3_shutdown_pre_sync(void *arg, int howto)
3537 struct g_geom *gp, *gp2;
3538 struct g_raid3_softc *sc;
3544 LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3545 if ((sc = gp->softc) == NULL)
3547 /* Skip synchronization geom. */
3548 if (gp == sc->sc_sync.ds_geom)
3550 g_topology_unlock();
3551 sx_xlock(&sc->sc_lock);
3553 error = g_raid3_destroy(sc, G_RAID3_DESTROY_DELAYED);
3555 sx_xunlock(&sc->sc_lock);
3558 g_topology_unlock();
3563 g_raid3_init(struct g_class *mp)
3566 g_raid3_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
3567 g_raid3_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
3568 if (g_raid3_pre_sync == NULL)
3569 G_RAID3_DEBUG(0, "Warning! Cannot register shutdown event.");
3573 g_raid3_fini(struct g_class *mp)
3576 if (g_raid3_pre_sync != NULL)
3577 EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_raid3_pre_sync);
3580 DECLARE_GEOM_CLASS(g_raid3_class, g_raid3);