g_raid3.c revision 160081
1/*-
2 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/raid3/g_raid3.c 160081 2006-07-03 10:32:38Z pjd $");
29
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>
35#include <sys/lock.h>
36#include <sys/mutex.h>
37#include <sys/bio.h>
38#include <sys/sysctl.h>
39#include <sys/malloc.h>
40#include <sys/eventhandler.h>
41#include <vm/uma.h>
42#include <geom/geom.h>
43#include <sys/proc.h>
44#include <sys/kthread.h>
45#include <sys/sched.h>
46#include <geom/raid3/g_raid3.h>
47
48
49static MALLOC_DEFINE(M_RAID3, "raid3_data", "GEOM_RAID3 Data");
50
51SYSCTL_DECL(_kern_geom);
52SYSCTL_NODE(_kern_geom, OID_AUTO, raid3, CTLFLAG_RW, 0, "GEOM_RAID3 stuff");
53u_int g_raid3_debug = 0;
54TUNABLE_INT("kern.geom.raid3.debug", &g_raid3_debug);
55SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, debug, CTLFLAG_RW, &g_raid3_debug, 0,
56    "Debug level");
57static u_int g_raid3_timeout = 4;
58TUNABLE_INT("kern.geom.raid3.timeout", &g_raid3_timeout);
59SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, timeout, CTLFLAG_RW, &g_raid3_timeout,
60    0, "Time to wait on all raid3 components");
61static u_int g_raid3_idletime = 5;
62TUNABLE_INT("kern.geom.raid3.idletime", &g_raid3_idletime);
63SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, idletime, CTLFLAG_RW,
64    &g_raid3_idletime, 0, "Mark components as clean when idling");
65static u_int g_raid3_disconnect_on_failure = 1;
66TUNABLE_INT("kern.geom.raid3.disconnect_on_failure",
67    &g_raid3_disconnect_on_failure);
68SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
69    &g_raid3_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
70static u_int g_raid3_syncreqs = 2;
71TUNABLE_INT("kern.geom.raid3.sync_requests", &g_raid3_syncreqs);
72SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, sync_requests, CTLFLAG_RDTUN,
73    &g_raid3_syncreqs, 0, "Parallel synchronization I/O requests.");
74
75static u_int g_raid3_n64k = 50;
76TUNABLE_INT("kern.geom.raid3.n64k", &g_raid3_n64k);
77SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n64k, CTLFLAG_RD, &g_raid3_n64k, 0,
78    "Maximum number of 64kB allocations");
79static u_int g_raid3_n16k = 200;
80TUNABLE_INT("kern.geom.raid3.n16k", &g_raid3_n16k);
81SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n16k, CTLFLAG_RD, &g_raid3_n16k, 0,
82    "Maximum number of 16kB allocations");
83static u_int g_raid3_n4k = 1200;
84TUNABLE_INT("kern.geom.raid3.n4k", &g_raid3_n4k);
85SYSCTL_UINT(_kern_geom_raid3, OID_AUTO, n4k, CTLFLAG_RD, &g_raid3_n4k, 0,
86    "Maximum number of 4kB allocations");
87
88SYSCTL_NODE(_kern_geom_raid3, OID_AUTO, stat, CTLFLAG_RW, 0,
89    "GEOM_RAID3 statistics");
90static u_int g_raid3_parity_mismatch = 0;
91SYSCTL_UINT(_kern_geom_raid3_stat, OID_AUTO, parity_mismatch, CTLFLAG_RD,
92    &g_raid3_parity_mismatch, 0, "Number of failures in VERIFY mode");
93
94#define	MSLEEP(ident, mtx, priority, wmesg, timeout)	do {		\
95	G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, (ident));	\
96	msleep((ident), (mtx), (priority), (wmesg), (timeout));		\
97	G_RAID3_DEBUG(4, "%s: Woken up %p.", __func__, (ident));	\
98} while (0)
99
100static eventhandler_tag g_raid3_pre_sync = NULL;
101
102static int g_raid3_destroy_geom(struct gctl_req *req, struct g_class *mp,
103    struct g_geom *gp);
104static g_taste_t g_raid3_taste;
105static void g_raid3_init(struct g_class *mp);
106static void g_raid3_fini(struct g_class *mp);
107
108struct g_class g_raid3_class = {
109	.name = G_RAID3_CLASS_NAME,
110	.version = G_VERSION,
111	.ctlreq = g_raid3_config,
112	.taste = g_raid3_taste,
113	.destroy_geom = g_raid3_destroy_geom,
114	.init = g_raid3_init,
115	.fini = g_raid3_fini
116};
117
118
119static void g_raid3_destroy_provider(struct g_raid3_softc *sc);
120static int g_raid3_update_disk(struct g_raid3_disk *disk, u_int state);
121static void g_raid3_update_device(struct g_raid3_softc *sc, boolean_t force);
122static void g_raid3_dumpconf(struct sbuf *sb, const char *indent,
123    struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
124static void g_raid3_sync_stop(struct g_raid3_softc *sc, int type);
125static int g_raid3_register_request(struct bio *pbp);
126static void g_raid3_sync_release(struct g_raid3_softc *sc);
127
128
129static const char *
130g_raid3_disk_state2str(int state)
131{
132
133	switch (state) {
134	case G_RAID3_DISK_STATE_NODISK:
135		return ("NODISK");
136	case G_RAID3_DISK_STATE_NONE:
137		return ("NONE");
138	case G_RAID3_DISK_STATE_NEW:
139		return ("NEW");
140	case G_RAID3_DISK_STATE_ACTIVE:
141		return ("ACTIVE");
142	case G_RAID3_DISK_STATE_STALE:
143		return ("STALE");
144	case G_RAID3_DISK_STATE_SYNCHRONIZING:
145		return ("SYNCHRONIZING");
146	case G_RAID3_DISK_STATE_DISCONNECTED:
147		return ("DISCONNECTED");
148	default:
149		return ("INVALID");
150	}
151}
152
153static const char *
154g_raid3_device_state2str(int state)
155{
156
157	switch (state) {
158	case G_RAID3_DEVICE_STATE_STARTING:
159		return ("STARTING");
160	case G_RAID3_DEVICE_STATE_DEGRADED:
161		return ("DEGRADED");
162	case G_RAID3_DEVICE_STATE_COMPLETE:
163		return ("COMPLETE");
164	default:
165		return ("INVALID");
166	}
167}
168
169const char *
170g_raid3_get_diskname(struct g_raid3_disk *disk)
171{
172
173	if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
174		return ("[unknown]");
175	return (disk->d_name);
176}
177
178static int
179g_raid3_uma_ctor(void *mem, int size, void *arg, int flags)
180{
181	struct g_raid3_zone *sz = arg;
182
183	if (sz->sz_max > 0 && sz->sz_inuse == sz->sz_max)
184		return (ENOMEM);
185	sz->sz_inuse++;
186	return (0);
187}
188
189static void
190g_raid3_uma_dtor(void *mem, int size, void *arg)
191{
192	struct g_raid3_zone *sz = arg;
193
194	sz->sz_inuse--;
195}
196
197#define	g_raid3_xor(src1, src2, dst, size)				\
198	_g_raid3_xor((uint64_t *)(src1), (uint64_t *)(src2),		\
199	    (uint64_t *)(dst), (size_t)size)
200static void
201_g_raid3_xor(uint64_t *src1, uint64_t *src2, uint64_t *dst, size_t size)
202{
203
204	KASSERT((size % 128) == 0, ("Invalid size: %zu.", size));
205	for (; size > 0; size -= 128) {
206		*dst++ = (*src1++) ^ (*src2++);
207		*dst++ = (*src1++) ^ (*src2++);
208		*dst++ = (*src1++) ^ (*src2++);
209		*dst++ = (*src1++) ^ (*src2++);
210		*dst++ = (*src1++) ^ (*src2++);
211		*dst++ = (*src1++) ^ (*src2++);
212		*dst++ = (*src1++) ^ (*src2++);
213		*dst++ = (*src1++) ^ (*src2++);
214		*dst++ = (*src1++) ^ (*src2++);
215		*dst++ = (*src1++) ^ (*src2++);
216		*dst++ = (*src1++) ^ (*src2++);
217		*dst++ = (*src1++) ^ (*src2++);
218		*dst++ = (*src1++) ^ (*src2++);
219		*dst++ = (*src1++) ^ (*src2++);
220		*dst++ = (*src1++) ^ (*src2++);
221		*dst++ = (*src1++) ^ (*src2++);
222	}
223}
224
225static int
226g_raid3_is_zero(struct bio *bp)
227{
228	static const uint64_t zeros[] = {
229	    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
230	};
231	u_char *addr;
232	ssize_t size;
233
234	size = bp->bio_length;
235	addr = (u_char *)bp->bio_data;
236	for (; size > 0; size -= sizeof(zeros), addr += sizeof(zeros)) {
237		if (bcmp(addr, zeros, sizeof(zeros)) != 0)
238			return (0);
239	}
240	return (1);
241}
242
243/*
244 * --- Events handling functions ---
245 * Events in geom_raid3 are used to maintain disks and device status
246 * from one thread to simplify locking.
247 */
248static void
249g_raid3_event_free(struct g_raid3_event *ep)
250{
251
252	free(ep, M_RAID3);
253}
254
255int
256g_raid3_event_send(void *arg, int state, int flags)
257{
258	struct g_raid3_softc *sc;
259	struct g_raid3_disk *disk;
260	struct g_raid3_event *ep;
261	int error;
262
263	ep = malloc(sizeof(*ep), M_RAID3, M_WAITOK);
264	G_RAID3_DEBUG(4, "%s: Sending event %p.", __func__, ep);
265	if ((flags & G_RAID3_EVENT_DEVICE) != 0) {
266		disk = NULL;
267		sc = arg;
268	} else {
269		disk = arg;
270		sc = disk->d_softc;
271	}
272	ep->e_disk = disk;
273	ep->e_state = state;
274	ep->e_flags = flags;
275	ep->e_error = 0;
276	mtx_lock(&sc->sc_events_mtx);
277	TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
278	mtx_unlock(&sc->sc_events_mtx);
279	G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
280	mtx_lock(&sc->sc_queue_mtx);
281	wakeup(sc);
282	wakeup(&sc->sc_queue);
283	mtx_unlock(&sc->sc_queue_mtx);
284	if ((flags & G_RAID3_EVENT_DONTWAIT) != 0)
285		return (0);
286	sx_assert(&sc->sc_lock, SX_XLOCKED);
287	G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
288	sx_xunlock(&sc->sc_lock);
289	while ((ep->e_flags & G_RAID3_EVENT_DONE) == 0) {
290		mtx_lock(&sc->sc_events_mtx);
291		MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "r3:event",
292		    hz * 5);
293	}
294	error = ep->e_error;
295	g_raid3_event_free(ep);
296	sx_xlock(&sc->sc_lock);
297	return (error);
298}
299
300static struct g_raid3_event *
301g_raid3_event_get(struct g_raid3_softc *sc)
302{
303	struct g_raid3_event *ep;
304
305	mtx_lock(&sc->sc_events_mtx);
306	ep = TAILQ_FIRST(&sc->sc_events);
307	mtx_unlock(&sc->sc_events_mtx);
308	return (ep);
309}
310
311static void
312g_raid3_event_remove(struct g_raid3_softc *sc, struct g_raid3_event *ep)
313{
314
315	mtx_lock(&sc->sc_events_mtx);
316	TAILQ_REMOVE(&sc->sc_events, ep, e_next);
317	mtx_unlock(&sc->sc_events_mtx);
318}
319
320static void
321g_raid3_event_cancel(struct g_raid3_disk *disk)
322{
323	struct g_raid3_softc *sc;
324	struct g_raid3_event *ep, *tmpep;
325
326	sc = disk->d_softc;
327	sx_assert(&sc->sc_lock, SX_XLOCKED);
328
329	mtx_lock(&sc->sc_events_mtx);
330	TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
331		if ((ep->e_flags & G_RAID3_EVENT_DEVICE) != 0)
332			continue;
333		if (ep->e_disk != disk)
334			continue;
335		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
336		if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0)
337			g_raid3_event_free(ep);
338		else {
339			ep->e_error = ECANCELED;
340			wakeup(ep);
341		}
342	}
343	mtx_unlock(&sc->sc_events_mtx);
344}
345
346/*
347 * Return the number of disks in the given state.
348 * If state is equal to -1, count all connected disks.
349 */
350u_int
351g_raid3_ndisks(struct g_raid3_softc *sc, int state)
352{
353	struct g_raid3_disk *disk;
354	u_int n, ndisks;
355
356	sx_assert(&sc->sc_lock, SX_LOCKED);
357
358	for (n = ndisks = 0; n < sc->sc_ndisks; n++) {
359		disk = &sc->sc_disks[n];
360		if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
361			continue;
362		if (state == -1 || disk->d_state == state)
363			ndisks++;
364	}
365	return (ndisks);
366}
367
368static u_int
369g_raid3_nrequests(struct g_raid3_softc *sc, struct g_consumer *cp)
370{
371	struct bio *bp;
372	u_int nreqs = 0;
373
374	mtx_lock(&sc->sc_queue_mtx);
375	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
376		if (bp->bio_from == cp)
377			nreqs++;
378	}
379	mtx_unlock(&sc->sc_queue_mtx);
380	return (nreqs);
381}
382
383static int
384g_raid3_is_busy(struct g_raid3_softc *sc, struct g_consumer *cp)
385{
386
387	if (cp->index > 0) {
388		G_RAID3_DEBUG(2,
389		    "I/O requests for %s exist, can't destroy it now.",
390		    cp->provider->name);
391		return (1);
392	}
393	if (g_raid3_nrequests(sc, cp) > 0) {
394		G_RAID3_DEBUG(2,
395		    "I/O requests for %s in queue, can't destroy it now.",
396		    cp->provider->name);
397		return (1);
398	}
399	return (0);
400}
401
402static void
403g_raid3_destroy_consumer(void *arg, int flags __unused)
404{
405	struct g_consumer *cp;
406
407	g_topology_assert();
408
409	cp = arg;
410	G_RAID3_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
411	g_detach(cp);
412	g_destroy_consumer(cp);
413}
414
415static void
416g_raid3_kill_consumer(struct g_raid3_softc *sc, struct g_consumer *cp)
417{
418	struct g_provider *pp;
419	int retaste_wait;
420
421	g_topology_assert();
422
423	cp->private = NULL;
424	if (g_raid3_is_busy(sc, cp))
425		return;
426	G_RAID3_DEBUG(2, "Consumer %s destroyed.", cp->provider->name);
427	pp = cp->provider;
428	retaste_wait = 0;
429	if (cp->acw == 1) {
430		if ((pp->geom->flags & G_GEOM_WITHER) == 0)
431			retaste_wait = 1;
432	}
433	G_RAID3_DEBUG(2, "Access %s r%dw%de%d = %d", pp->name, -cp->acr,
434	    -cp->acw, -cp->ace, 0);
435	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
436		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
437	if (retaste_wait) {
438		/*
439		 * After retaste event was send (inside g_access()), we can send
440		 * event to detach and destroy consumer.
441		 * A class, which has consumer to the given provider connected
442		 * will not receive retaste event for the provider.
443		 * This is the way how I ignore retaste events when I close
444		 * consumers opened for write: I detach and destroy consumer
445		 * after retaste event is sent.
446		 */
447		g_post_event(g_raid3_destroy_consumer, cp, M_WAITOK, NULL);
448		return;
449	}
450	G_RAID3_DEBUG(1, "Consumer %s destroyed.", pp->name);
451	g_detach(cp);
452	g_destroy_consumer(cp);
453}
454
455static int
456g_raid3_connect_disk(struct g_raid3_disk *disk, struct g_provider *pp)
457{
458	struct g_consumer *cp;
459	int error;
460
461	g_topology_assert_not();
462	KASSERT(disk->d_consumer == NULL,
463	    ("Disk already connected (device %s).", disk->d_softc->sc_name));
464
465	g_topology_lock();
466	cp = g_new_consumer(disk->d_softc->sc_geom);
467	error = g_attach(cp, pp);
468	if (error != 0) {
469		g_destroy_consumer(cp);
470		g_topology_unlock();
471		return (error);
472	}
473	error = g_access(cp, 1, 1, 1);
474		g_topology_unlock();
475	if (error != 0) {
476		g_detach(cp);
477		g_destroy_consumer(cp);
478		G_RAID3_DEBUG(0, "Cannot open consumer %s (error=%d).",
479		    pp->name, error);
480		return (error);
481	}
482	disk->d_consumer = cp;
483	disk->d_consumer->private = disk;
484	disk->d_consumer->index = 0;
485	G_RAID3_DEBUG(2, "Disk %s connected.", g_raid3_get_diskname(disk));
486	return (0);
487}
488
489static void
490g_raid3_disconnect_consumer(struct g_raid3_softc *sc, struct g_consumer *cp)
491{
492
493	g_topology_assert();
494
495	if (cp == NULL)
496		return;
497	if (cp->provider != NULL)
498		g_raid3_kill_consumer(sc, cp);
499	else
500		g_destroy_consumer(cp);
501}
502
503/*
504 * Initialize disk. This means allocate memory, create consumer, attach it
505 * to the provider and open access (r1w1e1) to it.
506 */
507static struct g_raid3_disk *
508g_raid3_init_disk(struct g_raid3_softc *sc, struct g_provider *pp,
509    struct g_raid3_metadata *md, int *errorp)
510{
511	struct g_raid3_disk *disk;
512	int error;
513
514	disk = &sc->sc_disks[md->md_no];
515	error = g_raid3_connect_disk(disk, pp);
516	if (error != 0) {
517		if (errorp != NULL)
518			*errorp = error;
519		return (NULL);
520	}
521	disk->d_state = G_RAID3_DISK_STATE_NONE;
522	disk->d_flags = md->md_dflags;
523	if (md->md_provider[0] != '\0')
524		disk->d_flags |= G_RAID3_DISK_FLAG_HARDCODED;
525	disk->d_sync.ds_consumer = NULL;
526	disk->d_sync.ds_offset = md->md_sync_offset;
527	disk->d_sync.ds_offset_done = md->md_sync_offset;
528	disk->d_genid = md->md_genid;
529	disk->d_sync.ds_syncid = md->md_syncid;
530	if (errorp != NULL)
531		*errorp = 0;
532	return (disk);
533}
534
535static void
536g_raid3_destroy_disk(struct g_raid3_disk *disk)
537{
538	struct g_raid3_softc *sc;
539
540	g_topology_assert_not();
541	sc = disk->d_softc;
542	sx_assert(&sc->sc_lock, SX_XLOCKED);
543
544	if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
545		return;
546	g_raid3_event_cancel(disk);
547	switch (disk->d_state) {
548	case G_RAID3_DISK_STATE_SYNCHRONIZING:
549		if (sc->sc_syncdisk != NULL)
550			g_raid3_sync_stop(sc, 1);
551		/* FALLTHROUGH */
552	case G_RAID3_DISK_STATE_NEW:
553	case G_RAID3_DISK_STATE_STALE:
554	case G_RAID3_DISK_STATE_ACTIVE:
555		g_topology_lock();
556		g_raid3_disconnect_consumer(sc, disk->d_consumer);
557		g_topology_unlock();
558		disk->d_consumer = NULL;
559		break;
560	default:
561		KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
562		    g_raid3_get_diskname(disk),
563		    g_raid3_disk_state2str(disk->d_state)));
564	}
565	disk->d_state = G_RAID3_DISK_STATE_NODISK;
566}
567
568static void
569g_raid3_destroy_device(struct g_raid3_softc *sc)
570{
571	struct g_raid3_event *ep;
572	struct g_raid3_disk *disk;
573	struct g_geom *gp;
574	struct g_consumer *cp;
575	u_int n;
576
577	g_topology_assert_not();
578	sx_assert(&sc->sc_lock, SX_XLOCKED);
579
580	gp = sc->sc_geom;
581	if (sc->sc_provider != NULL)
582		g_raid3_destroy_provider(sc);
583	for (n = 0; n < sc->sc_ndisks; n++) {
584		disk = &sc->sc_disks[n];
585		if (disk->d_state != G_RAID3_DISK_STATE_NODISK) {
586			disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
587			g_raid3_update_metadata(disk);
588			g_raid3_destroy_disk(disk);
589		}
590	}
591	while ((ep = g_raid3_event_get(sc)) != NULL) {
592		g_raid3_event_remove(sc, ep);
593		if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0)
594			g_raid3_event_free(ep);
595		else {
596			ep->e_error = ECANCELED;
597			ep->e_flags |= G_RAID3_EVENT_DONE;
598			G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, ep);
599			mtx_lock(&sc->sc_events_mtx);
600			wakeup(ep);
601			mtx_unlock(&sc->sc_events_mtx);
602		}
603	}
604	callout_drain(&sc->sc_callout);
605	cp = LIST_FIRST(&sc->sc_sync.ds_geom->consumer);
606	g_topology_lock();
607	if (cp != NULL)
608		g_raid3_disconnect_consumer(sc, cp);
609	g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
610	G_RAID3_DEBUG(0, "Device %s destroyed.", gp->name);
611	g_wither_geom(gp, ENXIO);
612	g_topology_unlock();
613	uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_64K].sz_zone);
614	uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_16K].sz_zone);
615	uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_4K].sz_zone);
616	mtx_destroy(&sc->sc_queue_mtx);
617	mtx_destroy(&sc->sc_events_mtx);
618	sx_xunlock(&sc->sc_lock);
619	sx_destroy(&sc->sc_lock);
620}
621
622static void
623g_raid3_orphan(struct g_consumer *cp)
624{
625	struct g_raid3_disk *disk;
626
627	g_topology_assert();
628
629	disk = cp->private;
630	if (disk == NULL)
631		return;
632	disk->d_softc->sc_bump_id = G_RAID3_BUMP_SYNCID;
633	g_raid3_event_send(disk, G_RAID3_DISK_STATE_DISCONNECTED,
634	    G_RAID3_EVENT_DONTWAIT);
635}
636
637static int
638g_raid3_write_metadata(struct g_raid3_disk *disk, struct g_raid3_metadata *md)
639{
640	struct g_raid3_softc *sc;
641	struct g_consumer *cp;
642	off_t offset, length;
643	u_char *sector;
644	int error = 0;
645
646	g_topology_assert_not();
647	sc = disk->d_softc;
648	sx_assert(&sc->sc_lock, SX_LOCKED);
649
650	cp = disk->d_consumer;
651	KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
652	KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
653	KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
654	    ("Consumer %s closed? (r%dw%de%d).", cp->provider->name, cp->acr,
655	    cp->acw, cp->ace));
656	length = cp->provider->sectorsize;
657	offset = cp->provider->mediasize - length;
658	sector = malloc((size_t)length, M_RAID3, M_WAITOK | M_ZERO);
659	if (md != NULL)
660		raid3_metadata_encode(md, sector);
661	error = g_write_data(cp, offset, sector, length);
662	free(sector, M_RAID3);
663	if (error != 0) {
664		if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
665			G_RAID3_DEBUG(0, "Cannot write metadata on %s "
666			    "(device=%s, error=%d).",
667			    g_raid3_get_diskname(disk), sc->sc_name, error);
668			disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
669		} else {
670			G_RAID3_DEBUG(1, "Cannot write metadata on %s "
671			    "(device=%s, error=%d).",
672			    g_raid3_get_diskname(disk), sc->sc_name, error);
673		}
674		if (g_raid3_disconnect_on_failure &&
675		    sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
676			sc->sc_bump_id |= G_RAID3_BUMP_GENID;
677			g_raid3_event_send(disk,
678			    G_RAID3_DISK_STATE_DISCONNECTED,
679			    G_RAID3_EVENT_DONTWAIT);
680		}
681	}
682	return (error);
683}
684
685int
686g_raid3_clear_metadata(struct g_raid3_disk *disk)
687{
688	int error;
689
690	g_topology_assert_not();
691	sx_assert(&disk->d_softc->sc_lock, SX_LOCKED);
692
693	error = g_raid3_write_metadata(disk, NULL);
694	if (error == 0) {
695		G_RAID3_DEBUG(2, "Metadata on %s cleared.",
696		    g_raid3_get_diskname(disk));
697	} else {
698		G_RAID3_DEBUG(0,
699		    "Cannot clear metadata on disk %s (error=%d).",
700		    g_raid3_get_diskname(disk), error);
701	}
702	return (error);
703}
704
705void
706g_raid3_fill_metadata(struct g_raid3_disk *disk, struct g_raid3_metadata *md)
707{
708	struct g_raid3_softc *sc;
709	struct g_provider *pp;
710
711	sc = disk->d_softc;
712	strlcpy(md->md_magic, G_RAID3_MAGIC, sizeof(md->md_magic));
713	md->md_version = G_RAID3_VERSION;
714	strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
715	md->md_id = sc->sc_id;
716	md->md_all = sc->sc_ndisks;
717	md->md_genid = sc->sc_genid;
718	md->md_mediasize = sc->sc_mediasize;
719	md->md_sectorsize = sc->sc_sectorsize;
720	md->md_mflags = (sc->sc_flags & G_RAID3_DEVICE_FLAG_MASK);
721	md->md_no = disk->d_no;
722	md->md_syncid = disk->d_sync.ds_syncid;
723	md->md_dflags = (disk->d_flags & G_RAID3_DISK_FLAG_MASK);
724	if (disk->d_state != G_RAID3_DISK_STATE_SYNCHRONIZING)
725		md->md_sync_offset = 0;
726	else {
727		md->md_sync_offset =
728		    disk->d_sync.ds_offset_done / (sc->sc_ndisks - 1);
729	}
730	if (disk->d_consumer != NULL && disk->d_consumer->provider != NULL)
731		pp = disk->d_consumer->provider;
732	else
733		pp = NULL;
734	if ((disk->d_flags & G_RAID3_DISK_FLAG_HARDCODED) != 0 && pp != NULL)
735		strlcpy(md->md_provider, pp->name, sizeof(md->md_provider));
736	else
737		bzero(md->md_provider, sizeof(md->md_provider));
738	if (pp != NULL)
739		md->md_provsize = pp->mediasize;
740	else
741		md->md_provsize = 0;
742}
743
744void
745g_raid3_update_metadata(struct g_raid3_disk *disk)
746{
747	struct g_raid3_softc *sc;
748	struct g_raid3_metadata md;
749	int error;
750
751	g_topology_assert_not();
752	sc = disk->d_softc;
753	sx_assert(&sc->sc_lock, SX_LOCKED);
754
755	g_raid3_fill_metadata(disk, &md);
756	error = g_raid3_write_metadata(disk, &md);
757	if (error == 0) {
758		G_RAID3_DEBUG(2, "Metadata on %s updated.",
759		    g_raid3_get_diskname(disk));
760	} else {
761		G_RAID3_DEBUG(0,
762		    "Cannot update metadata on disk %s (error=%d).",
763		    g_raid3_get_diskname(disk), error);
764	}
765}
766
767static void
768g_raid3_bump_syncid(struct g_raid3_softc *sc)
769{
770	struct g_raid3_disk *disk;
771	u_int n;
772
773	g_topology_assert_not();
774	sx_assert(&sc->sc_lock, SX_XLOCKED);
775	KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) > 0,
776	    ("%s called with no active disks (device=%s).", __func__,
777	    sc->sc_name));
778
779	sc->sc_syncid++;
780	G_RAID3_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
781	    sc->sc_syncid);
782	for (n = 0; n < sc->sc_ndisks; n++) {
783		disk = &sc->sc_disks[n];
784		if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
785		    disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
786			disk->d_sync.ds_syncid = sc->sc_syncid;
787			g_raid3_update_metadata(disk);
788		}
789	}
790}
791
792static void
793g_raid3_bump_genid(struct g_raid3_softc *sc)
794{
795	struct g_raid3_disk *disk;
796	u_int n;
797
798	g_topology_assert_not();
799	sx_assert(&sc->sc_lock, SX_XLOCKED);
800	KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) > 0,
801	    ("%s called with no active disks (device=%s).", __func__,
802	    sc->sc_name));
803
804	sc->sc_genid++;
805	G_RAID3_DEBUG(1, "Device %s: genid bumped to %u.", sc->sc_name,
806	    sc->sc_genid);
807	for (n = 0; n < sc->sc_ndisks; n++) {
808		disk = &sc->sc_disks[n];
809		if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
810		    disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
811			disk->d_genid = sc->sc_genid;
812			g_raid3_update_metadata(disk);
813		}
814	}
815}
816
817static int
818g_raid3_idle(struct g_raid3_softc *sc, int acw)
819{
820	struct g_raid3_disk *disk;
821	u_int i;
822	int timeout;
823
824	g_topology_assert_not();
825	sx_assert(&sc->sc_lock, SX_XLOCKED);
826
827	if (sc->sc_provider == NULL)
828		return (0);
829	if (sc->sc_idle)
830		return (0);
831	if (sc->sc_writes > 0)
832		return (0);
833	if (acw > 0 || (acw == -1 && sc->sc_provider->acw > 0)) {
834		timeout = g_raid3_idletime - (time_uptime - sc->sc_last_write);
835		if (timeout > 0)
836			return (timeout);
837	}
838	sc->sc_idle = 1;
839	for (i = 0; i < sc->sc_ndisks; i++) {
840		disk = &sc->sc_disks[i];
841		if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
842			continue;
843		G_RAID3_DEBUG(1, "Disk %s (device %s) marked as clean.",
844		    g_raid3_get_diskname(disk), sc->sc_name);
845		disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
846		g_raid3_update_metadata(disk);
847	}
848	return (0);
849}
850
851static void
852g_raid3_unidle(struct g_raid3_softc *sc)
853{
854	struct g_raid3_disk *disk;
855	u_int i;
856
857	g_topology_assert_not();
858	sx_assert(&sc->sc_lock, SX_XLOCKED);
859
860	sc->sc_idle = 0;
861	sc->sc_last_write = time_uptime;
862	for (i = 0; i < sc->sc_ndisks; i++) {
863		disk = &sc->sc_disks[i];
864		if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE)
865			continue;
866		G_RAID3_DEBUG(1, "Disk %s (device %s) marked as dirty.",
867		    g_raid3_get_diskname(disk), sc->sc_name);
868		disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
869		g_raid3_update_metadata(disk);
870	}
871}
872
873/*
874 * Treat bio_driver1 field in parent bio as list head and field bio_caller1
875 * in child bio as pointer to the next element on the list.
876 */
877#define	G_RAID3_HEAD_BIO(pbp)	(pbp)->bio_driver1
878
879#define	G_RAID3_NEXT_BIO(cbp)	(cbp)->bio_caller1
880
881#define	G_RAID3_FOREACH_BIO(pbp, bp)					\
882	for ((bp) = G_RAID3_HEAD_BIO(pbp); (bp) != NULL;		\
883	    (bp) = G_RAID3_NEXT_BIO(bp))
884
885#define	G_RAID3_FOREACH_SAFE_BIO(pbp, bp, tmpbp)			\
886	for ((bp) = G_RAID3_HEAD_BIO(pbp);				\
887	    (bp) != NULL && ((tmpbp) = G_RAID3_NEXT_BIO(bp), 1);	\
888	    (bp) = (tmpbp))
889
890static void
891g_raid3_init_bio(struct bio *pbp)
892{
893
894	G_RAID3_HEAD_BIO(pbp) = NULL;
895}
896
897static void
898g_raid3_remove_bio(struct bio *cbp)
899{
900	struct bio *pbp, *bp;
901
902	pbp = cbp->bio_parent;
903	if (G_RAID3_HEAD_BIO(pbp) == cbp)
904		G_RAID3_HEAD_BIO(pbp) = G_RAID3_NEXT_BIO(cbp);
905	else {
906		G_RAID3_FOREACH_BIO(pbp, bp) {
907			if (G_RAID3_NEXT_BIO(bp) == cbp) {
908				G_RAID3_NEXT_BIO(bp) = G_RAID3_NEXT_BIO(cbp);
909				break;
910			}
911		}
912	}
913	G_RAID3_NEXT_BIO(cbp) = NULL;
914}
915
916static void
917g_raid3_replace_bio(struct bio *sbp, struct bio *dbp)
918{
919	struct bio *pbp, *bp;
920
921	g_raid3_remove_bio(sbp);
922	pbp = dbp->bio_parent;
923	G_RAID3_NEXT_BIO(sbp) = G_RAID3_NEXT_BIO(dbp);
924	if (G_RAID3_HEAD_BIO(pbp) == dbp)
925		G_RAID3_HEAD_BIO(pbp) = sbp;
926	else {
927		G_RAID3_FOREACH_BIO(pbp, bp) {
928			if (G_RAID3_NEXT_BIO(bp) == dbp) {
929				G_RAID3_NEXT_BIO(bp) = sbp;
930				break;
931			}
932		}
933	}
934	G_RAID3_NEXT_BIO(dbp) = NULL;
935}
936
937static void
938g_raid3_destroy_bio(struct g_raid3_softc *sc, struct bio *cbp)
939{
940	struct bio *bp, *pbp;
941	size_t size;
942
943	pbp = cbp->bio_parent;
944	pbp->bio_children--;
945	KASSERT(cbp->bio_data != NULL, ("NULL bio_data"));
946	size = pbp->bio_length / (sc->sc_ndisks - 1);
947	uma_zfree_arg(sc->sc_zones[g_raid3_zone(size)].sz_zone,
948	    cbp->bio_data,
949	    &sc->sc_zones[g_raid3_zone(size)]);
950	if (G_RAID3_HEAD_BIO(pbp) == cbp) {
951		G_RAID3_HEAD_BIO(pbp) = G_RAID3_NEXT_BIO(cbp);
952		G_RAID3_NEXT_BIO(cbp) = NULL;
953		g_destroy_bio(cbp);
954	} else {
955		G_RAID3_FOREACH_BIO(pbp, bp) {
956			if (G_RAID3_NEXT_BIO(bp) == cbp)
957				break;
958		}
959		if (bp != NULL) {
960			KASSERT(G_RAID3_NEXT_BIO(bp) != NULL,
961			    ("NULL bp->bio_driver1"));
962			G_RAID3_NEXT_BIO(bp) = G_RAID3_NEXT_BIO(cbp);
963			G_RAID3_NEXT_BIO(cbp) = NULL;
964		}
965		g_destroy_bio(cbp);
966	}
967}
968
969static struct bio *
970g_raid3_clone_bio(struct g_raid3_softc *sc, struct bio *pbp)
971{
972	struct bio *bp, *cbp;
973	size_t size;
974	int memflag;
975
976	cbp = g_clone_bio(pbp);
977	if (cbp == NULL)
978		return (NULL);
979	size = pbp->bio_length / (sc->sc_ndisks - 1);
980	if ((pbp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR) != 0)
981		memflag = M_WAITOK;
982	else
983		memflag = M_NOWAIT;
984	cbp->bio_data = uma_zalloc_arg(sc->sc_zones[g_raid3_zone(size)].sz_zone,
985	   &sc->sc_zones[g_raid3_zone(size)], memflag);
986	sc->sc_zones[g_raid3_zone(size)].sz_requested++;
987	if (cbp->bio_data == NULL) {
988		sc->sc_zones[g_raid3_zone(size)].sz_failed++;
989		pbp->bio_children--;
990		g_destroy_bio(cbp);
991		return (NULL);
992	}
993	G_RAID3_NEXT_BIO(cbp) = NULL;
994	if (G_RAID3_HEAD_BIO(pbp) == NULL)
995		G_RAID3_HEAD_BIO(pbp) = cbp;
996	else {
997		G_RAID3_FOREACH_BIO(pbp, bp) {
998			if (G_RAID3_NEXT_BIO(bp) == NULL) {
999				G_RAID3_NEXT_BIO(bp) = cbp;
1000				break;
1001			}
1002		}
1003	}
1004	return (cbp);
1005}
1006
1007static void
1008g_raid3_scatter(struct bio *pbp)
1009{
1010	struct g_raid3_softc *sc;
1011	struct g_raid3_disk *disk;
1012	struct bio *bp, *cbp, *tmpbp;
1013	off_t atom, cadd, padd, left;
1014
1015	sc = pbp->bio_to->geom->softc;
1016	bp = NULL;
1017	if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_NOPARITY) == 0) {
1018		/*
1019		 * Find bio for which we should calculate data.
1020		 */
1021		G_RAID3_FOREACH_BIO(pbp, cbp) {
1022			if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0) {
1023				bp = cbp;
1024				break;
1025			}
1026		}
1027		KASSERT(bp != NULL, ("NULL parity bio."));
1028	}
1029	atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1030	cadd = padd = 0;
1031	for (left = pbp->bio_length; left > 0; left -= sc->sc_sectorsize) {
1032		G_RAID3_FOREACH_BIO(pbp, cbp) {
1033			if (cbp == bp)
1034				continue;
1035			bcopy(pbp->bio_data + padd, cbp->bio_data + cadd, atom);
1036			padd += atom;
1037		}
1038		cadd += atom;
1039	}
1040	if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_NOPARITY) == 0) {
1041		/*
1042		 * Calculate parity.
1043		 */
1044		bzero(bp->bio_data, bp->bio_length);
1045		G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1046			if (cbp == bp)
1047				continue;
1048			g_raid3_xor(cbp->bio_data, bp->bio_data, bp->bio_data,
1049			    bp->bio_length);
1050			if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_NODISK) != 0)
1051				g_raid3_destroy_bio(sc, cbp);
1052		}
1053	}
1054	G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1055		struct g_consumer *cp;
1056
1057		disk = cbp->bio_caller2;
1058		cp = disk->d_consumer;
1059		cbp->bio_to = cp->provider;
1060		G_RAID3_LOGREQ(3, cbp, "Sending request.");
1061		KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1062		    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1063		    cp->acr, cp->acw, cp->ace));
1064		cp->index++;
1065		sc->sc_writes++;
1066		g_io_request(cbp, cp);
1067	}
1068}
1069
1070static void
1071g_raid3_gather(struct bio *pbp)
1072{
1073	struct g_raid3_softc *sc;
1074	struct g_raid3_disk *disk;
1075	struct bio *xbp, *fbp, *cbp;
1076	off_t atom, cadd, padd, left;
1077
1078	sc = pbp->bio_to->geom->softc;
1079	/*
1080	 * Find bio for which we have to calculate data.
1081	 * While going through this path, check if all requests
1082	 * succeeded, if not, deny whole request.
1083	 * If we're in COMPLETE mode, we allow one request to fail,
1084	 * so if we find one, we're sending it to the parity consumer.
1085	 * If there are more failed requests, we deny whole request.
1086	 */
1087	xbp = fbp = NULL;
1088	G_RAID3_FOREACH_BIO(pbp, cbp) {
1089		if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0) {
1090			KASSERT(xbp == NULL, ("More than one parity bio."));
1091			xbp = cbp;
1092		}
1093		if (cbp->bio_error == 0)
1094			continue;
1095		/*
1096		 * Found failed request.
1097		 */
1098		if (fbp == NULL) {
1099			if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_DEGRADED) != 0) {
1100				/*
1101				 * We are already in degraded mode, so we can't
1102				 * accept any failures.
1103				 */
1104				if (pbp->bio_error == 0)
1105					pbp->bio_error = cbp->bio_error;
1106			} else {
1107				fbp = cbp;
1108			}
1109		} else {
1110			/*
1111			 * Next failed request, that's too many.
1112			 */
1113			if (pbp->bio_error == 0)
1114				pbp->bio_error = fbp->bio_error;
1115		}
1116		disk = cbp->bio_caller2;
1117		if (disk == NULL)
1118			continue;
1119		if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
1120			disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
1121			G_RAID3_LOGREQ(0, cbp, "Request failed (error=%d).",
1122			    cbp->bio_error);
1123		} else {
1124			G_RAID3_LOGREQ(1, cbp, "Request failed (error=%d).",
1125			    cbp->bio_error);
1126		}
1127		if (g_raid3_disconnect_on_failure &&
1128		    sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1129			sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1130			g_raid3_event_send(disk,
1131			    G_RAID3_DISK_STATE_DISCONNECTED,
1132			    G_RAID3_EVENT_DONTWAIT);
1133		}
1134	}
1135	if (pbp->bio_error != 0)
1136		goto finish;
1137	if (fbp != NULL && (pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0) {
1138		pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_VERIFY;
1139		if (xbp != fbp)
1140			g_raid3_replace_bio(xbp, fbp);
1141		g_raid3_destroy_bio(sc, fbp);
1142	} else if (fbp != NULL) {
1143		struct g_consumer *cp;
1144
1145		/*
1146		 * One request failed, so send the same request to
1147		 * the parity consumer.
1148		 */
1149		disk = pbp->bio_driver2;
1150		if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE) {
1151			pbp->bio_error = fbp->bio_error;
1152			goto finish;
1153		}
1154		pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1155		pbp->bio_inbed--;
1156		fbp->bio_flags &= ~(BIO_DONE | BIO_ERROR);
1157		if (disk->d_no == sc->sc_ndisks - 1)
1158			fbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1159		fbp->bio_error = 0;
1160		fbp->bio_completed = 0;
1161		fbp->bio_children = 0;
1162		fbp->bio_inbed = 0;
1163		cp = disk->d_consumer;
1164		fbp->bio_caller2 = disk;
1165		fbp->bio_to = cp->provider;
1166		G_RAID3_LOGREQ(3, fbp, "Sending request (recover).");
1167		KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1168		    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1169		    cp->acr, cp->acw, cp->ace));
1170		cp->index++;
1171		g_io_request(fbp, cp);
1172		return;
1173	}
1174	if (xbp != NULL) {
1175		/*
1176		 * Calculate parity.
1177		 */
1178		G_RAID3_FOREACH_BIO(pbp, cbp) {
1179			if ((cbp->bio_cflags & G_RAID3_BIO_CFLAG_PARITY) != 0)
1180				continue;
1181			g_raid3_xor(cbp->bio_data, xbp->bio_data, xbp->bio_data,
1182			    xbp->bio_length);
1183		}
1184		xbp->bio_cflags &= ~G_RAID3_BIO_CFLAG_PARITY;
1185		if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0) {
1186			if (!g_raid3_is_zero(xbp)) {
1187				g_raid3_parity_mismatch++;
1188				pbp->bio_error = EIO;
1189				goto finish;
1190			}
1191			g_raid3_destroy_bio(sc, xbp);
1192		}
1193	}
1194	atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1195	cadd = padd = 0;
1196	for (left = pbp->bio_length; left > 0; left -= sc->sc_sectorsize) {
1197		G_RAID3_FOREACH_BIO(pbp, cbp) {
1198			bcopy(cbp->bio_data + cadd, pbp->bio_data + padd, atom);
1199			pbp->bio_completed += atom;
1200			padd += atom;
1201		}
1202		cadd += atom;
1203	}
1204finish:
1205	if (pbp->bio_error == 0)
1206		G_RAID3_LOGREQ(3, pbp, "Request finished.");
1207	else {
1208		if ((pbp->bio_pflags & G_RAID3_BIO_PFLAG_VERIFY) != 0)
1209			G_RAID3_LOGREQ(1, pbp, "Verification error.");
1210		else
1211			G_RAID3_LOGREQ(0, pbp, "Request failed.");
1212	}
1213	pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_MASK;
1214	while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL)
1215		g_raid3_destroy_bio(sc, cbp);
1216	g_io_deliver(pbp, pbp->bio_error);
1217}
1218
1219static void
1220g_raid3_done(struct bio *bp)
1221{
1222	struct g_raid3_softc *sc;
1223
1224	sc = bp->bio_from->geom->softc;
1225	bp->bio_cflags |= G_RAID3_BIO_CFLAG_REGULAR;
1226	G_RAID3_LOGREQ(3, bp, "Regular request done (error=%d).", bp->bio_error);
1227	mtx_lock(&sc->sc_queue_mtx);
1228	bioq_insert_head(&sc->sc_queue, bp);
1229	wakeup(sc);
1230	wakeup(&sc->sc_queue);
1231	mtx_unlock(&sc->sc_queue_mtx);
1232}
1233
1234static void
1235g_raid3_regular_request(struct bio *cbp)
1236{
1237	struct g_raid3_softc *sc;
1238	struct g_raid3_disk *disk;
1239	struct bio *pbp;
1240
1241	g_topology_assert_not();
1242
1243	pbp = cbp->bio_parent;
1244	sc = pbp->bio_to->geom->softc;
1245	cbp->bio_from->index--;
1246	if (cbp->bio_cmd == BIO_WRITE)
1247		sc->sc_writes--;
1248	disk = cbp->bio_from->private;
1249	if (disk == NULL) {
1250		g_topology_lock();
1251		g_raid3_kill_consumer(sc, cbp->bio_from);
1252		g_topology_unlock();
1253	}
1254
1255	G_RAID3_LOGREQ(3, cbp, "Request finished.");
1256	pbp->bio_inbed++;
1257	KASSERT(pbp->bio_inbed <= pbp->bio_children,
1258	    ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
1259	    pbp->bio_children));
1260	if (pbp->bio_inbed != pbp->bio_children)
1261		return;
1262	switch (pbp->bio_cmd) {
1263	case BIO_READ:
1264		g_raid3_gather(pbp);
1265		break;
1266	case BIO_WRITE:
1267	case BIO_DELETE:
1268	    {
1269		int error = 0;
1270
1271		pbp->bio_completed = pbp->bio_length;
1272		while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL) {
1273			if (cbp->bio_error == 0) {
1274				g_raid3_destroy_bio(sc, cbp);
1275				continue;
1276			}
1277
1278			if (error == 0)
1279				error = cbp->bio_error;
1280			else if (pbp->bio_error == 0) {
1281				/*
1282				 * Next failed request, that's too many.
1283				 */
1284				pbp->bio_error = error;
1285			}
1286
1287			disk = cbp->bio_caller2;
1288			if (disk == NULL) {
1289				g_raid3_destroy_bio(sc, cbp);
1290				continue;
1291			}
1292
1293			if ((disk->d_flags & G_RAID3_DISK_FLAG_BROKEN) == 0) {
1294				disk->d_flags |= G_RAID3_DISK_FLAG_BROKEN;
1295				G_RAID3_LOGREQ(0, cbp,
1296				    "Request failed (error=%d).",
1297				    cbp->bio_error);
1298			} else {
1299				G_RAID3_LOGREQ(1, cbp,
1300				    "Request failed (error=%d).",
1301				    cbp->bio_error);
1302			}
1303			if (g_raid3_disconnect_on_failure &&
1304			    sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1305				sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1306				g_raid3_event_send(disk,
1307				    G_RAID3_DISK_STATE_DISCONNECTED,
1308				    G_RAID3_EVENT_DONTWAIT);
1309			}
1310			g_raid3_destroy_bio(sc, cbp);
1311		}
1312		if (pbp->bio_error == 0)
1313			G_RAID3_LOGREQ(3, pbp, "Request finished.");
1314		else
1315			G_RAID3_LOGREQ(0, pbp, "Request failed.");
1316		pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_DEGRADED;
1317		pbp->bio_pflags &= ~G_RAID3_BIO_PFLAG_NOPARITY;
1318		bioq_remove(&sc->sc_inflight, pbp);
1319		/* Release delayed sync requests if possible. */
1320		g_raid3_sync_release(sc);
1321		g_io_deliver(pbp, pbp->bio_error);
1322		break;
1323	    }
1324	}
1325}
1326
1327static void
1328g_raid3_sync_done(struct bio *bp)
1329{
1330	struct g_raid3_softc *sc;
1331
1332	G_RAID3_LOGREQ(3, bp, "Synchronization request delivered.");
1333	sc = bp->bio_from->geom->softc;
1334	bp->bio_cflags |= G_RAID3_BIO_CFLAG_SYNC;
1335	mtx_lock(&sc->sc_queue_mtx);
1336	bioq_insert_head(&sc->sc_queue, bp);
1337	wakeup(sc);
1338	wakeup(&sc->sc_queue);
1339	mtx_unlock(&sc->sc_queue_mtx);
1340}
1341
1342static void
1343g_raid3_start(struct bio *bp)
1344{
1345	struct g_raid3_softc *sc;
1346
1347	sc = bp->bio_to->geom->softc;
1348	/*
1349	 * If sc == NULL or there are no valid disks, provider's error
1350	 * should be set and g_raid3_start() should not be called at all.
1351	 */
1352	KASSERT(sc != NULL && (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
1353	    sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE),
1354	    ("Provider's error should be set (error=%d)(device=%s).",
1355	    bp->bio_to->error, bp->bio_to->name));
1356	G_RAID3_LOGREQ(3, bp, "Request received.");
1357
1358	switch (bp->bio_cmd) {
1359	case BIO_READ:
1360	case BIO_WRITE:
1361	case BIO_DELETE:
1362		break;
1363	case BIO_GETATTR:
1364	default:
1365		g_io_deliver(bp, EOPNOTSUPP);
1366		return;
1367	}
1368	mtx_lock(&sc->sc_queue_mtx);
1369	bioq_insert_tail(&sc->sc_queue, bp);
1370	G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
1371	wakeup(sc);
1372	mtx_unlock(&sc->sc_queue_mtx);
1373}
1374
1375/*
1376 * Return TRUE if the given request is colliding with a in-progress
1377 * synchronization request.
1378 */
1379static int
1380g_raid3_sync_collision(struct g_raid3_softc *sc, struct bio *bp)
1381{
1382	struct g_raid3_disk *disk;
1383	struct bio *sbp;
1384	off_t rstart, rend, sstart, send;
1385	int i;
1386
1387	disk = sc->sc_syncdisk;
1388	if (disk == NULL)
1389		return (0);
1390	rstart = bp->bio_offset;
1391	rend = bp->bio_offset + bp->bio_length;
1392	for (i = 0; i < g_raid3_syncreqs; i++) {
1393		sbp = disk->d_sync.ds_bios[i];
1394		if (sbp == NULL)
1395			continue;
1396		sstart = sbp->bio_offset;
1397		send = sbp->bio_length;
1398		if (sbp->bio_cmd == BIO_WRITE) {
1399			sstart *= sc->sc_ndisks - 1;
1400			send *= sc->sc_ndisks - 1;
1401		}
1402		send += sstart;
1403		if (rend > sstart && rstart < send)
1404			return (1);
1405	}
1406	return (0);
1407}
1408
1409/*
1410 * Return TRUE if the given sync request is colliding with a in-progress regular
1411 * request.
1412 */
1413static int
1414g_raid3_regular_collision(struct g_raid3_softc *sc, struct bio *sbp)
1415{
1416	off_t rstart, rend, sstart, send;
1417	struct bio *bp;
1418
1419	if (sc->sc_syncdisk == NULL)
1420		return (0);
1421	sstart = sbp->bio_offset;
1422	send = sstart + sbp->bio_length;
1423	TAILQ_FOREACH(bp, &sc->sc_inflight.queue, bio_queue) {
1424		rstart = bp->bio_offset;
1425		rend = bp->bio_offset + bp->bio_length;
1426		if (rend > sstart && rstart < send)
1427			return (1);
1428	}
1429	return (0);
1430}
1431
1432/*
1433 * Puts request onto delayed queue.
1434 */
1435static void
1436g_raid3_regular_delay(struct g_raid3_softc *sc, struct bio *bp)
1437{
1438
1439        G_RAID3_LOGREQ(2, bp, "Delaying request.");
1440        bioq_insert_head(&sc->sc_regular_delayed, bp);
1441}
1442
1443/*
1444 * Puts synchronization request onto delayed queue.
1445 */
1446static void
1447g_raid3_sync_delay(struct g_raid3_softc *sc, struct bio *bp)
1448{
1449
1450        G_RAID3_LOGREQ(2, bp, "Delaying synchronization request.");
1451        bioq_insert_tail(&sc->sc_sync_delayed, bp);
1452}
1453
1454/*
1455 * Releases delayed regular requests which don't collide anymore with sync
1456 * requests.
1457 */
1458static void
1459g_raid3_regular_release(struct g_raid3_softc *sc)
1460{
1461        struct bio *bp, *bp2;
1462
1463        TAILQ_FOREACH_SAFE(bp, &sc->sc_regular_delayed.queue, bio_queue, bp2) {
1464                if (g_raid3_sync_collision(sc, bp))
1465                        continue;
1466                bioq_remove(&sc->sc_regular_delayed, bp);
1467                G_RAID3_LOGREQ(2, bp, "Releasing delayed request (%p).", bp);
1468		mtx_lock(&sc->sc_queue_mtx);
1469		bioq_insert_head(&sc->sc_queue, bp);
1470#if 0
1471		/*
1472		 * wakeup() is not needed, because this function is called from
1473		 * the worker thread.
1474		 */
1475		wakeup(&sc->sc_queue);
1476#endif
1477		mtx_unlock(&sc->sc_queue_mtx);
1478        }
1479}
1480
1481/*
1482 * Releases delayed sync requests which don't collide anymore with regular
1483 * requests.
1484 */
1485static void
1486g_raid3_sync_release(struct g_raid3_softc *sc)
1487{
1488        struct bio *bp, *bp2;
1489
1490        TAILQ_FOREACH_SAFE(bp, &sc->sc_sync_delayed.queue, bio_queue, bp2) {
1491                if (g_raid3_regular_collision(sc, bp))
1492                        continue;
1493                bioq_remove(&sc->sc_sync_delayed, bp);
1494                G_RAID3_LOGREQ(2, bp,
1495                    "Releasing delayed synchronization request.");
1496                g_io_request(bp, bp->bio_from);
1497        }
1498}
1499
1500/*
1501 * Handle synchronization requests.
1502 * Every synchronization request is two-steps process: first, READ request is
1503 * send to active provider and then WRITE request (with read data) to the provider
1504 * beeing synchronized. When WRITE is finished, new synchronization request is
1505 * send.
1506 */
1507static void
1508g_raid3_sync_request(struct bio *bp)
1509{
1510	struct g_raid3_softc *sc;
1511	struct g_raid3_disk *disk;
1512
1513	bp->bio_from->index--;
1514	sc = bp->bio_from->geom->softc;
1515	disk = bp->bio_from->private;
1516	if (disk == NULL) {
1517		sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
1518		g_topology_lock();
1519		g_raid3_kill_consumer(sc, bp->bio_from);
1520		g_topology_unlock();
1521		free(bp->bio_data, M_RAID3);
1522		g_destroy_bio(bp);
1523		sx_xlock(&sc->sc_lock);
1524		return;
1525	}
1526
1527	/*
1528	 * Synchronization request.
1529	 */
1530	switch (bp->bio_cmd) {
1531	case BIO_READ:
1532	    {
1533		struct g_consumer *cp;
1534		u_char *dst, *src;
1535		off_t left;
1536		u_int atom;
1537
1538		if (bp->bio_error != 0) {
1539			G_RAID3_LOGREQ(0, bp,
1540			    "Synchronization request failed (error=%d).",
1541			    bp->bio_error);
1542			g_destroy_bio(bp);
1543			return;
1544		}
1545		G_RAID3_LOGREQ(3, bp, "Synchronization request finished.");
1546		atom = sc->sc_sectorsize / (sc->sc_ndisks - 1);
1547		dst = src = bp->bio_data;
1548		if (disk->d_no == sc->sc_ndisks - 1) {
1549			u_int n;
1550
1551			/* Parity component. */
1552			for (left = bp->bio_length; left > 0;
1553			    left -= sc->sc_sectorsize) {
1554				bcopy(src, dst, atom);
1555				src += atom;
1556				for (n = 1; n < sc->sc_ndisks - 1; n++) {
1557					g_raid3_xor(src, dst, dst, atom);
1558					src += atom;
1559				}
1560				dst += atom;
1561			}
1562		} else {
1563			/* Regular component. */
1564			src += atom * disk->d_no;
1565			for (left = bp->bio_length; left > 0;
1566			    left -= sc->sc_sectorsize) {
1567				bcopy(src, dst, atom);
1568				src += sc->sc_sectorsize;
1569				dst += atom;
1570			}
1571		}
1572		bp->bio_driver1 = bp->bio_driver2 = NULL;
1573		bp->bio_pflags = 0;
1574		bp->bio_offset /= sc->sc_ndisks - 1;
1575		bp->bio_length /= sc->sc_ndisks - 1;
1576		bp->bio_cmd = BIO_WRITE;
1577		bp->bio_cflags = 0;
1578		bp->bio_children = bp->bio_inbed = 0;
1579		cp = disk->d_consumer;
1580		KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1581		    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
1582		    cp->acr, cp->acw, cp->ace));
1583		cp->index++;
1584		g_io_request(bp, cp);
1585		return;
1586	    }
1587	case BIO_WRITE:
1588	    {
1589		struct g_raid3_disk_sync *sync;
1590		off_t boffset, moffset;
1591		void *data;
1592		int i;
1593
1594		if (bp->bio_error != 0) {
1595			G_RAID3_LOGREQ(0, bp,
1596			    "Synchronization request failed (error=%d).",
1597			    bp->bio_error);
1598			g_destroy_bio(bp);
1599			sc->sc_bump_id |= G_RAID3_BUMP_GENID;
1600			g_raid3_event_send(disk,
1601			    G_RAID3_DISK_STATE_DISCONNECTED,
1602			    G_RAID3_EVENT_DONTWAIT);
1603			return;
1604		}
1605		G_RAID3_LOGREQ(3, bp, "Synchronization request finished.");
1606		sync = &disk->d_sync;
1607		if (sync->ds_offset == sc->sc_mediasize / (sc->sc_ndisks - 1) ||
1608		    sync->ds_consumer == NULL ||
1609		    (sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
1610			/* Don't send more synchronization requests. */
1611			sync->ds_inflight--;
1612			if (sync->ds_bios != NULL) {
1613				i = (int)(uintptr_t)bp->bio_caller1;
1614				sync->ds_bios[i] = NULL;
1615			}
1616			free(bp->bio_data, M_RAID3);
1617			g_destroy_bio(bp);
1618			if (sync->ds_inflight > 0)
1619				return;
1620			if (sync->ds_consumer == NULL ||
1621			    (sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
1622				return;
1623			}
1624			/*
1625			 * Disk up-to-date, activate it.
1626			 */
1627			g_raid3_event_send(disk, G_RAID3_DISK_STATE_ACTIVE,
1628			    G_RAID3_EVENT_DONTWAIT);
1629			return;
1630		}
1631
1632		/* Send next synchronization request. */
1633		data = bp->bio_data;
1634		bzero(bp, sizeof(*bp));
1635		bp->bio_cmd = BIO_READ;
1636		bp->bio_offset = sync->ds_offset * (sc->sc_ndisks - 1);
1637		bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
1638		sync->ds_offset += bp->bio_length / (sc->sc_ndisks - 1);
1639		bp->bio_done = g_raid3_sync_done;
1640		bp->bio_data = data;
1641		bp->bio_from = sync->ds_consumer;
1642		bp->bio_to = sc->sc_provider;
1643		G_RAID3_LOGREQ(3, bp, "Sending synchronization request.");
1644		sync->ds_consumer->index++;
1645		/*
1646		 * Delay the request if it is colliding with a regular request.
1647		 */
1648		if (g_raid3_regular_collision(sc, bp))
1649			g_raid3_sync_delay(sc, bp);
1650		else
1651			g_io_request(bp, sync->ds_consumer);
1652
1653		/* Release delayed requests if possible. */
1654		g_raid3_regular_release(sc);
1655
1656		/* Find the smallest offset. */
1657		moffset = sc->sc_mediasize;
1658		for (i = 0; i < g_raid3_syncreqs; i++) {
1659			bp = sync->ds_bios[i];
1660			boffset = bp->bio_offset;
1661			if (bp->bio_cmd == BIO_WRITE)
1662				boffset *= sc->sc_ndisks - 1;
1663			if (boffset < moffset)
1664				moffset = boffset;
1665		}
1666		if (sync->ds_offset_done + (MAXPHYS * 100) < moffset) {
1667			/* Update offset_done on every 100 blocks. */
1668			sync->ds_offset_done = moffset;
1669			g_raid3_update_metadata(disk);
1670		}
1671		return;
1672	    }
1673	default:
1674		KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
1675		    bp->bio_cmd, sc->sc_name));
1676		break;
1677	}
1678}
1679
1680static int
1681g_raid3_register_request(struct bio *pbp)
1682{
1683	struct g_raid3_softc *sc;
1684	struct g_raid3_disk *disk;
1685	struct g_consumer *cp;
1686	struct bio *cbp, *tmpbp;
1687	off_t offset, length;
1688	u_int n, ndisks;
1689	int round_robin, verify;
1690
1691	ndisks = 0;
1692	sc = pbp->bio_to->geom->softc;
1693	if ((pbp->bio_cflags & G_RAID3_BIO_CFLAG_REGSYNC) != 0 &&
1694	    sc->sc_syncdisk == NULL) {
1695		g_io_deliver(pbp, EIO);
1696		return (0);
1697	}
1698	g_raid3_init_bio(pbp);
1699	length = pbp->bio_length / (sc->sc_ndisks - 1);
1700	offset = pbp->bio_offset / (sc->sc_ndisks - 1);
1701	round_robin = verify = 0;
1702	switch (pbp->bio_cmd) {
1703	case BIO_READ:
1704		if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
1705		    sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1706			pbp->bio_pflags |= G_RAID3_BIO_PFLAG_VERIFY;
1707			verify = 1;
1708			ndisks = sc->sc_ndisks;
1709		} else {
1710			verify = 0;
1711			ndisks = sc->sc_ndisks - 1;
1712		}
1713		if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0 &&
1714		    sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
1715			round_robin = 1;
1716		} else {
1717			round_robin = 0;
1718		}
1719		KASSERT(!round_robin || !verify,
1720		    ("ROUND-ROBIN and VERIFY are mutually exclusive."));
1721		pbp->bio_driver2 = &sc->sc_disks[sc->sc_ndisks - 1];
1722		break;
1723	case BIO_WRITE:
1724	case BIO_DELETE:
1725		/*
1726		 * Delay the request if it is colliding with a synchronization
1727		 * request.
1728		 */
1729		if (g_raid3_sync_collision(sc, pbp)) {
1730			g_raid3_regular_delay(sc, pbp);
1731			return (0);
1732		}
1733
1734		if (sc->sc_idle)
1735			g_raid3_unidle(sc);
1736		else
1737			sc->sc_last_write = time_uptime;
1738
1739		ndisks = sc->sc_ndisks;
1740		break;
1741	}
1742	for (n = 0; n < ndisks; n++) {
1743		disk = &sc->sc_disks[n];
1744		cbp = g_raid3_clone_bio(sc, pbp);
1745		if (cbp == NULL) {
1746			while ((cbp = G_RAID3_HEAD_BIO(pbp)) != NULL)
1747				g_raid3_destroy_bio(sc, cbp);
1748			/*
1749			 * To prevent deadlock, we must run back up
1750			 * with the ENOMEM for failed requests of any
1751			 * of our consumers.  Our own sync requests
1752			 * can stick around, as they are finite.
1753			 */
1754			if ((pbp->bio_cflags &
1755			    G_RAID3_BIO_CFLAG_REGULAR) != 0) {
1756				g_io_deliver(pbp, ENOMEM);
1757				return (0);
1758			}
1759			return (ENOMEM);
1760		}
1761		cbp->bio_offset = offset;
1762		cbp->bio_length = length;
1763		cbp->bio_done = g_raid3_done;
1764		switch (pbp->bio_cmd) {
1765		case BIO_READ:
1766			if (disk->d_state != G_RAID3_DISK_STATE_ACTIVE) {
1767				/*
1768				 * Replace invalid component with the parity
1769				 * component.
1770				 */
1771				disk = &sc->sc_disks[sc->sc_ndisks - 1];
1772				cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1773				pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1774			} else if (round_robin &&
1775			    disk->d_no == sc->sc_round_robin) {
1776				/*
1777				 * In round-robin mode skip one data component
1778				 * and use parity component when reading.
1779				 */
1780				pbp->bio_driver2 = disk;
1781				disk = &sc->sc_disks[sc->sc_ndisks - 1];
1782				cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1783				sc->sc_round_robin++;
1784				round_robin = 0;
1785			} else if (verify && disk->d_no == sc->sc_ndisks - 1) {
1786				cbp->bio_cflags |= G_RAID3_BIO_CFLAG_PARITY;
1787			}
1788			break;
1789		case BIO_WRITE:
1790		case BIO_DELETE:
1791			if (disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
1792			    disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
1793				if (n == ndisks - 1) {
1794					/*
1795					 * Active parity component, mark it as such.
1796					 */
1797					cbp->bio_cflags |=
1798					    G_RAID3_BIO_CFLAG_PARITY;
1799				}
1800			} else {
1801				pbp->bio_pflags |= G_RAID3_BIO_PFLAG_DEGRADED;
1802				if (n == ndisks - 1) {
1803					/*
1804					 * Parity component is not connected,
1805					 * so destroy its request.
1806					 */
1807					pbp->bio_pflags |=
1808					    G_RAID3_BIO_PFLAG_NOPARITY;
1809					g_raid3_destroy_bio(sc, cbp);
1810					cbp = NULL;
1811				} else {
1812					cbp->bio_cflags |=
1813					    G_RAID3_BIO_CFLAG_NODISK;
1814					disk = NULL;
1815				}
1816			}
1817			break;
1818		}
1819		if (cbp != NULL)
1820			cbp->bio_caller2 = disk;
1821	}
1822	switch (pbp->bio_cmd) {
1823	case BIO_READ:
1824		if (round_robin) {
1825			/*
1826			 * If we are in round-robin mode and 'round_robin' is
1827			 * still 1, it means, that we skipped parity component
1828			 * for this read and must reset sc_round_robin field.
1829			 */
1830			sc->sc_round_robin = 0;
1831		}
1832		G_RAID3_FOREACH_SAFE_BIO(pbp, cbp, tmpbp) {
1833			disk = cbp->bio_caller2;
1834			cp = disk->d_consumer;
1835			cbp->bio_to = cp->provider;
1836			G_RAID3_LOGREQ(3, cbp, "Sending request.");
1837			KASSERT(cp->acr >= 1 && cp->acw >= 1 && cp->ace >= 1,
1838			    ("Consumer %s not opened (r%dw%de%d).",
1839			    cp->provider->name, cp->acr, cp->acw, cp->ace));
1840			cp->index++;
1841			g_io_request(cbp, cp);
1842		}
1843		break;
1844	case BIO_WRITE:
1845	case BIO_DELETE:
1846		/*
1847		 * Put request onto inflight queue, so we can check if new
1848		 * synchronization requests don't collide with it.
1849		 */
1850		bioq_insert_tail(&sc->sc_inflight, pbp);
1851
1852		/*
1853		 * Bump syncid on first write.
1854		 */
1855		if ((sc->sc_bump_id & G_RAID3_BUMP_SYNCID) != 0) {
1856			sc->sc_bump_id &= ~G_RAID3_BUMP_SYNCID;
1857			g_raid3_bump_syncid(sc);
1858		}
1859		g_raid3_scatter(pbp);
1860		break;
1861	}
1862	return (0);
1863}
1864
1865static int
1866g_raid3_can_destroy(struct g_raid3_softc *sc)
1867{
1868	struct g_geom *gp;
1869	struct g_consumer *cp;
1870
1871	g_topology_assert();
1872	gp = sc->sc_geom;
1873	if (gp->softc == NULL)
1874		return (1);
1875	LIST_FOREACH(cp, &gp->consumer, consumer) {
1876		if (g_raid3_is_busy(sc, cp))
1877			return (0);
1878	}
1879	gp = sc->sc_sync.ds_geom;
1880	LIST_FOREACH(cp, &gp->consumer, consumer) {
1881		if (g_raid3_is_busy(sc, cp))
1882			return (0);
1883	}
1884	G_RAID3_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
1885	    sc->sc_name);
1886	return (1);
1887}
1888
1889static int
1890g_raid3_try_destroy(struct g_raid3_softc *sc)
1891{
1892
1893	g_topology_assert_not();
1894	sx_assert(&sc->sc_lock, SX_XLOCKED);
1895
1896	if (sc->sc_rootmount != NULL) {
1897		G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
1898		    sc->sc_rootmount);
1899		root_mount_rel(sc->sc_rootmount);
1900		sc->sc_rootmount = NULL;
1901	}
1902
1903	g_topology_lock();
1904	if (!g_raid3_can_destroy(sc)) {
1905		g_topology_unlock();
1906		return (0);
1907	}
1908	sc->sc_geom->softc = NULL;
1909	sc->sc_sync.ds_geom->softc = NULL;
1910	if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_WAIT) != 0) {
1911		g_topology_unlock();
1912		G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__,
1913		    &sc->sc_worker);
1914		/* Unlock sc_lock here, as it can be destroyed after wakeup. */
1915		sx_xunlock(&sc->sc_lock);
1916		wakeup(&sc->sc_worker);
1917		sc->sc_worker = NULL;
1918	} else {
1919		g_topology_unlock();
1920		g_raid3_destroy_device(sc);
1921		free(sc->sc_disks, M_RAID3);
1922		free(sc, M_RAID3);
1923	}
1924	return (1);
1925}
1926
1927/*
1928 * Worker thread.
1929 */
1930static void
1931g_raid3_worker(void *arg)
1932{
1933	struct g_raid3_softc *sc;
1934	struct g_raid3_event *ep;
1935	struct bio *bp;
1936	int timeout;
1937
1938	sc = arg;
1939	mtx_lock_spin(&sched_lock);
1940	sched_prio(curthread, PRIBIO);
1941	mtx_unlock_spin(&sched_lock);
1942
1943	sx_xlock(&sc->sc_lock);
1944	for (;;) {
1945		G_RAID3_DEBUG(5, "%s: Let's see...", __func__);
1946		/*
1947		 * First take a look at events.
1948		 * This is important to handle events before any I/O requests.
1949		 */
1950		ep = g_raid3_event_get(sc);
1951		if (ep != NULL) {
1952			g_raid3_event_remove(sc, ep);
1953			if ((ep->e_flags & G_RAID3_EVENT_DEVICE) != 0) {
1954				/* Update only device status. */
1955				G_RAID3_DEBUG(3,
1956				    "Running event for device %s.",
1957				    sc->sc_name);
1958				ep->e_error = 0;
1959				g_raid3_update_device(sc, 1);
1960			} else {
1961				/* Update disk status. */
1962				G_RAID3_DEBUG(3, "Running event for disk %s.",
1963				     g_raid3_get_diskname(ep->e_disk));
1964				ep->e_error = g_raid3_update_disk(ep->e_disk,
1965				    ep->e_state);
1966				if (ep->e_error == 0)
1967					g_raid3_update_device(sc, 0);
1968			}
1969			if ((ep->e_flags & G_RAID3_EVENT_DONTWAIT) != 0) {
1970				KASSERT(ep->e_error == 0,
1971				    ("Error cannot be handled."));
1972				g_raid3_event_free(ep);
1973			} else {
1974				ep->e_flags |= G_RAID3_EVENT_DONE;
1975				G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__,
1976				    ep);
1977				mtx_lock(&sc->sc_events_mtx);
1978				wakeup(ep);
1979				mtx_unlock(&sc->sc_events_mtx);
1980			}
1981			if ((sc->sc_flags &
1982			    G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
1983				if (g_raid3_try_destroy(sc)) {
1984					curthread->td_pflags &= ~TDP_GEOM;
1985					G_RAID3_DEBUG(1, "Thread exiting.");
1986					kthread_exit(0);
1987				}
1988			}
1989			G_RAID3_DEBUG(5, "%s: I'm here 1.", __func__);
1990			continue;
1991		}
1992		/*
1993		 * Check if we can mark array as CLEAN and if we can't take
1994		 * how much seconds should we wait.
1995		 */
1996		timeout = g_raid3_idle(sc, -1);
1997		/*
1998		 * Now I/O requests.
1999		 */
2000		/* Get first request from the queue. */
2001		mtx_lock(&sc->sc_queue_mtx);
2002		bp = bioq_first(&sc->sc_queue);
2003		if (bp == NULL) {
2004			if ((sc->sc_flags &
2005			    G_RAID3_DEVICE_FLAG_DESTROY) != 0) {
2006				mtx_unlock(&sc->sc_queue_mtx);
2007				if (g_raid3_try_destroy(sc)) {
2008					curthread->td_pflags &= ~TDP_GEOM;
2009					G_RAID3_DEBUG(1, "Thread exiting.");
2010					kthread_exit(0);
2011				}
2012				mtx_lock(&sc->sc_queue_mtx);
2013			}
2014			sx_xunlock(&sc->sc_lock);
2015			/*
2016			 * XXX: We can miss an event here, because an event
2017			 *      can be added without sx-device-lock and without
2018			 *      mtx-queue-lock. Maybe I should just stop using
2019			 *      dedicated mutex for events synchronization and
2020			 *      stick with the queue lock?
2021			 *      The event will hang here until next I/O request
2022			 *      or next event is received.
2023			 */
2024			MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "r3:w1",
2025			    timeout * hz);
2026			sx_xlock(&sc->sc_lock);
2027			G_RAID3_DEBUG(5, "%s: I'm here 4.", __func__);
2028			continue;
2029		}
2030process:
2031		bioq_remove(&sc->sc_queue, bp);
2032		mtx_unlock(&sc->sc_queue_mtx);
2033
2034		if ((bp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR) != 0)
2035			g_raid3_regular_request(bp);
2036		else if ((bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC) != 0)
2037			g_raid3_sync_request(bp);
2038		else if (g_raid3_register_request(bp) != 0) {
2039			mtx_lock(&sc->sc_queue_mtx);
2040			bioq_insert_head(&sc->sc_queue, bp);
2041			/*
2042			 * We are short in memory, let see if there are finished
2043			 * request we can free.
2044			 */
2045			TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
2046				if (bp->bio_cflags & G_RAID3_BIO_CFLAG_REGULAR)
2047					goto process;
2048			}
2049			/*
2050			 * No finished regular request, so at least keep
2051			 * synchronization running.
2052			 */
2053			TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
2054				if (bp->bio_cflags & G_RAID3_BIO_CFLAG_SYNC)
2055					goto process;
2056			}
2057			sx_xunlock(&sc->sc_lock);
2058			MSLEEP(&sc->sc_queue, &sc->sc_queue_mtx, PRIBIO | PDROP,
2059			    "r3:lowmem", hz / 10);
2060			sx_xlock(&sc->sc_lock);
2061		}
2062		G_RAID3_DEBUG(5, "%s: I'm here 9.", __func__);
2063	}
2064}
2065
2066static void
2067g_raid3_update_idle(struct g_raid3_softc *sc, struct g_raid3_disk *disk)
2068{
2069
2070	sx_assert(&sc->sc_lock, SX_LOCKED);
2071	if (!sc->sc_idle && (disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) == 0) {
2072		G_RAID3_DEBUG(1, "Disk %s (device %s) marked as dirty.",
2073		    g_raid3_get_diskname(disk), sc->sc_name);
2074		disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
2075	} else if (sc->sc_idle &&
2076	    (disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) != 0) {
2077		G_RAID3_DEBUG(1, "Disk %s (device %s) marked as clean.",
2078		    g_raid3_get_diskname(disk), sc->sc_name);
2079		disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2080	}
2081}
2082
2083static void
2084g_raid3_sync_start(struct g_raid3_softc *sc)
2085{
2086	struct g_raid3_disk *disk;
2087	struct g_consumer *cp;
2088	struct bio *bp;
2089	int error;
2090	u_int n;
2091
2092	g_topology_assert_not();
2093	sx_assert(&sc->sc_lock, SX_XLOCKED);
2094
2095	KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED,
2096	    ("Device not in DEGRADED state (%s, %u).", sc->sc_name,
2097	    sc->sc_state));
2098	KASSERT(sc->sc_syncdisk == NULL, ("Syncdisk is not NULL (%s, %u).",
2099	    sc->sc_name, sc->sc_state));
2100	disk = NULL;
2101	for (n = 0; n < sc->sc_ndisks; n++) {
2102		if (sc->sc_disks[n].d_state != G_RAID3_DISK_STATE_SYNCHRONIZING)
2103			continue;
2104		disk = &sc->sc_disks[n];
2105		break;
2106	}
2107	if (disk == NULL)
2108		return;
2109
2110	sx_xunlock(&sc->sc_lock);
2111	g_topology_lock();
2112	cp = g_new_consumer(sc->sc_sync.ds_geom);
2113	error = g_attach(cp, sc->sc_provider);
2114	KASSERT(error == 0,
2115	    ("Cannot attach to %s (error=%d).", sc->sc_name, error));
2116	error = g_access(cp, 1, 0, 0);
2117	KASSERT(error == 0, ("Cannot open %s (error=%d).", sc->sc_name, error));
2118	g_topology_unlock();
2119	sx_xlock(&sc->sc_lock);
2120
2121	G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
2122	    g_raid3_get_diskname(disk));
2123	disk->d_flags |= G_RAID3_DISK_FLAG_DIRTY;
2124	KASSERT(disk->d_sync.ds_consumer == NULL,
2125	    ("Sync consumer already exists (device=%s, disk=%s).",
2126	    sc->sc_name, g_raid3_get_diskname(disk)));
2127
2128	disk->d_sync.ds_consumer = cp;
2129	disk->d_sync.ds_consumer->private = disk;
2130	disk->d_sync.ds_consumer->index = 0;
2131	sc->sc_syncdisk = disk;
2132
2133	/*
2134	 * Allocate memory for synchronization bios and initialize them.
2135	 */
2136	disk->d_sync.ds_bios = malloc(sizeof(struct bio *) * g_raid3_syncreqs,
2137	    M_RAID3, M_WAITOK);
2138	for (n = 0; n < g_raid3_syncreqs; n++) {
2139		bp = g_alloc_bio();
2140		disk->d_sync.ds_bios[n] = bp;
2141		bp->bio_parent = NULL;
2142		bp->bio_cmd = BIO_READ;
2143		bp->bio_data = malloc(MAXPHYS, M_RAID3, M_WAITOK);
2144		bp->bio_cflags = 0;
2145		bp->bio_offset = disk->d_sync.ds_offset * (sc->sc_ndisks - 1);
2146		bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
2147		disk->d_sync.ds_offset += bp->bio_length / (sc->sc_ndisks - 1);
2148		bp->bio_done = g_raid3_sync_done;
2149		bp->bio_from = disk->d_sync.ds_consumer;
2150		bp->bio_to = sc->sc_provider;
2151		bp->bio_caller1 = (void *)(uintptr_t)n;
2152	}
2153
2154	/* Set the number of in-flight synchronization requests. */
2155	disk->d_sync.ds_inflight = g_raid3_syncreqs;
2156
2157	/*
2158	 * Fire off first synchronization requests.
2159	 */
2160	for (n = 0; n < g_raid3_syncreqs; n++) {
2161		bp = disk->d_sync.ds_bios[n];
2162		G_RAID3_LOGREQ(3, bp, "Sending synchronization request.");
2163		disk->d_sync.ds_consumer->index++;
2164		/*
2165		 * Delay the request if it is colliding with a regular request.
2166		 */
2167		if (g_raid3_regular_collision(sc, bp))
2168			g_raid3_sync_delay(sc, bp);
2169		else
2170			g_io_request(bp, disk->d_sync.ds_consumer);
2171	}
2172}
2173
2174/*
2175 * Stop synchronization process.
2176 * type: 0 - synchronization finished
2177 *       1 - synchronization stopped
2178 */
2179static void
2180g_raid3_sync_stop(struct g_raid3_softc *sc, int type)
2181{
2182	struct g_raid3_disk *disk;
2183	struct g_consumer *cp;
2184
2185	g_topology_assert_not();
2186	sx_assert(&sc->sc_lock, SX_LOCKED);
2187
2188	KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED,
2189	    ("Device not in DEGRADED state (%s, %u).", sc->sc_name,
2190	    sc->sc_state));
2191	disk = sc->sc_syncdisk;
2192	sc->sc_syncdisk = NULL;
2193	KASSERT(disk != NULL, ("No disk was synchronized (%s).", sc->sc_name));
2194	KASSERT(disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2195	    ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2196	    g_raid3_disk_state2str(disk->d_state)));
2197	if (disk->d_sync.ds_consumer == NULL)
2198		return;
2199
2200	if (type == 0) {
2201		G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s finished.",
2202		    sc->sc_name, g_raid3_get_diskname(disk));
2203	} else /* if (type == 1) */ {
2204		G_RAID3_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
2205		    sc->sc_name, g_raid3_get_diskname(disk));
2206	}
2207	free(disk->d_sync.ds_bios, M_RAID3);
2208	disk->d_sync.ds_bios = NULL;
2209	cp = disk->d_sync.ds_consumer;
2210	disk->d_sync.ds_consumer = NULL;
2211	disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2212	sx_xunlock(&sc->sc_lock); /* Avoid recursion on sc_lock. */
2213	g_topology_lock();
2214	g_raid3_kill_consumer(sc, cp);
2215	g_topology_unlock();
2216	sx_xlock(&sc->sc_lock);
2217}
2218
2219static void
2220g_raid3_launch_provider(struct g_raid3_softc *sc)
2221{
2222	struct g_provider *pp;
2223
2224	sx_assert(&sc->sc_lock, SX_LOCKED);
2225
2226	g_topology_lock();
2227	pp = g_new_providerf(sc->sc_geom, "raid3/%s", sc->sc_name);
2228	pp->mediasize = sc->sc_mediasize;
2229	pp->sectorsize = sc->sc_sectorsize;
2230	sc->sc_provider = pp;
2231	g_error_provider(pp, 0);
2232	g_topology_unlock();
2233	G_RAID3_DEBUG(0, "Device %s: provider %s launched.", sc->sc_name,
2234	    pp->name);
2235	if (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED)
2236		g_raid3_sync_start(sc);
2237}
2238
2239static void
2240g_raid3_destroy_provider(struct g_raid3_softc *sc)
2241{
2242	struct bio *bp;
2243
2244	g_topology_assert_not();
2245	KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
2246	    sc->sc_name));
2247
2248	g_topology_lock();
2249	g_error_provider(sc->sc_provider, ENXIO);
2250	mtx_lock(&sc->sc_queue_mtx);
2251	while ((bp = bioq_first(&sc->sc_queue)) != NULL) {
2252		bioq_remove(&sc->sc_queue, bp);
2253		g_io_deliver(bp, ENXIO);
2254	}
2255	mtx_unlock(&sc->sc_queue_mtx);
2256	G_RAID3_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
2257	    sc->sc_provider->name);
2258	sc->sc_provider->flags |= G_PF_WITHER;
2259	g_orphan_provider(sc->sc_provider, ENXIO);
2260	g_topology_unlock();
2261	sc->sc_provider = NULL;
2262	if (sc->sc_syncdisk != NULL)
2263		g_raid3_sync_stop(sc, 1);
2264}
2265
2266static void
2267g_raid3_go(void *arg)
2268{
2269	struct g_raid3_softc *sc;
2270
2271	sc = arg;
2272	G_RAID3_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
2273	g_raid3_event_send(sc, 0,
2274	    G_RAID3_EVENT_DONTWAIT | G_RAID3_EVENT_DEVICE);
2275}
2276
2277static u_int
2278g_raid3_determine_state(struct g_raid3_disk *disk)
2279{
2280	struct g_raid3_softc *sc;
2281	u_int state;
2282
2283	sc = disk->d_softc;
2284	if (sc->sc_syncid == disk->d_sync.ds_syncid) {
2285		if ((disk->d_flags &
2286		    G_RAID3_DISK_FLAG_SYNCHRONIZING) == 0) {
2287			/* Disk does not need synchronization. */
2288			state = G_RAID3_DISK_STATE_ACTIVE;
2289		} else {
2290			if ((sc->sc_flags &
2291			     G_RAID3_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2292			    (disk->d_flags &
2293			     G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) {
2294				/*
2295				 * We can start synchronization from
2296				 * the stored offset.
2297				 */
2298				state = G_RAID3_DISK_STATE_SYNCHRONIZING;
2299			} else {
2300				state = G_RAID3_DISK_STATE_STALE;
2301			}
2302		}
2303	} else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
2304		/*
2305		 * Reset all synchronization data for this disk,
2306		 * because if it even was synchronized, it was
2307		 * synchronized to disks with different syncid.
2308		 */
2309		disk->d_flags |= G_RAID3_DISK_FLAG_SYNCHRONIZING;
2310		disk->d_sync.ds_offset = 0;
2311		disk->d_sync.ds_offset_done = 0;
2312		disk->d_sync.ds_syncid = sc->sc_syncid;
2313		if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
2314		    (disk->d_flags & G_RAID3_DISK_FLAG_FORCE_SYNC) != 0) {
2315			state = G_RAID3_DISK_STATE_SYNCHRONIZING;
2316		} else {
2317			state = G_RAID3_DISK_STATE_STALE;
2318		}
2319	} else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
2320		/*
2321		 * Not good, NOT GOOD!
2322		 * It means that device was started on stale disks
2323		 * and more fresh disk just arrive.
2324		 * If there were writes, device is fucked up, sorry.
2325		 * I think the best choice here is don't touch
2326		 * this disk and inform the user laudly.
2327		 */
2328		G_RAID3_DEBUG(0, "Device %s was started before the freshest "
2329		    "disk (%s) arrives!! It will not be connected to the "
2330		    "running device.", sc->sc_name,
2331		    g_raid3_get_diskname(disk));
2332		g_raid3_destroy_disk(disk);
2333		state = G_RAID3_DISK_STATE_NONE;
2334		/* Return immediately, because disk was destroyed. */
2335		return (state);
2336	}
2337	G_RAID3_DEBUG(3, "State for %s disk: %s.",
2338	    g_raid3_get_diskname(disk), g_raid3_disk_state2str(state));
2339	return (state);
2340}
2341
2342/*
2343 * Update device state.
2344 */
2345static void
2346g_raid3_update_device(struct g_raid3_softc *sc, boolean_t force)
2347{
2348	struct g_raid3_disk *disk;
2349	u_int state;
2350
2351	sx_assert(&sc->sc_lock, SX_XLOCKED);
2352
2353	switch (sc->sc_state) {
2354	case G_RAID3_DEVICE_STATE_STARTING:
2355	    {
2356		u_int n, ndirty, ndisks, genid, syncid;
2357
2358		KASSERT(sc->sc_provider == NULL,
2359		    ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
2360		/*
2361		 * Are we ready? We are, if all disks are connected or
2362		 * one disk is missing and 'force' is true.
2363		 */
2364		if (g_raid3_ndisks(sc, -1) + force == sc->sc_ndisks) {
2365			if (!force)
2366				callout_drain(&sc->sc_callout);
2367		} else {
2368			if (force) {
2369				/*
2370				 * Timeout expired, so destroy device.
2371				 */
2372				sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2373				G_RAID3_DEBUG(1, "root_mount_rel[%u] %p",
2374				    __LINE__, sc->sc_rootmount);
2375				root_mount_rel(sc->sc_rootmount);
2376				sc->sc_rootmount = NULL;
2377			}
2378			return;
2379		}
2380
2381		/*
2382		 * Find the biggest genid.
2383		 */
2384		genid = 0;
2385		for (n = 0; n < sc->sc_ndisks; n++) {
2386			disk = &sc->sc_disks[n];
2387			if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2388				continue;
2389			if (disk->d_genid > genid)
2390				genid = disk->d_genid;
2391		}
2392		sc->sc_genid = genid;
2393		/*
2394		 * Remove all disks without the biggest genid.
2395		 */
2396		for (n = 0; n < sc->sc_ndisks; n++) {
2397			disk = &sc->sc_disks[n];
2398			if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2399				continue;
2400			if (disk->d_genid < genid) {
2401				G_RAID3_DEBUG(0,
2402				    "Component %s (device %s) broken, skipping.",
2403				    g_raid3_get_diskname(disk), sc->sc_name);
2404				g_raid3_destroy_disk(disk);
2405			}
2406		}
2407
2408		/*
2409		 * There must be at least 'sc->sc_ndisks - 1' components
2410		 * with the same syncid and without SYNCHRONIZING flag.
2411		 */
2412
2413		/*
2414		 * Find the biggest syncid, number of valid components and
2415		 * number of dirty components.
2416		 */
2417		ndirty = ndisks = syncid = 0;
2418		for (n = 0; n < sc->sc_ndisks; n++) {
2419			disk = &sc->sc_disks[n];
2420			if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2421				continue;
2422			if ((disk->d_flags & G_RAID3_DISK_FLAG_DIRTY) != 0)
2423				ndirty++;
2424			if (disk->d_sync.ds_syncid > syncid) {
2425				syncid = disk->d_sync.ds_syncid;
2426				ndisks = 0;
2427			} else if (disk->d_sync.ds_syncid < syncid) {
2428				continue;
2429			}
2430			if ((disk->d_flags &
2431			    G_RAID3_DISK_FLAG_SYNCHRONIZING) != 0) {
2432				continue;
2433			}
2434			ndisks++;
2435		}
2436		/*
2437		 * Do we have enough valid components?
2438		 */
2439		if (ndisks + 1 < sc->sc_ndisks) {
2440			G_RAID3_DEBUG(0,
2441			    "Device %s is broken, too few valid components.",
2442			    sc->sc_name);
2443			sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2444			return;
2445		}
2446		/*
2447		 * If there is one DIRTY component and all disks are present,
2448		 * mark it for synchronization. If there is more than one DIRTY
2449		 * component, mark parity component for synchronization.
2450		 */
2451		if (ndisks == sc->sc_ndisks && ndirty == 1) {
2452			for (n = 0; n < sc->sc_ndisks; n++) {
2453				disk = &sc->sc_disks[n];
2454				if ((disk->d_flags &
2455				    G_RAID3_DISK_FLAG_DIRTY) == 0) {
2456					continue;
2457				}
2458				disk->d_flags |=
2459				    G_RAID3_DISK_FLAG_SYNCHRONIZING;
2460			}
2461		} else if (ndisks == sc->sc_ndisks && ndirty > 1) {
2462			disk = &sc->sc_disks[sc->sc_ndisks - 1];
2463			disk->d_flags |= G_RAID3_DISK_FLAG_SYNCHRONIZING;
2464		}
2465
2466		sc->sc_syncid = syncid;
2467		if (force) {
2468			/* Remember to bump syncid on first write. */
2469			sc->sc_bump_id |= G_RAID3_BUMP_SYNCID;
2470		}
2471		if (ndisks == sc->sc_ndisks)
2472			state = G_RAID3_DEVICE_STATE_COMPLETE;
2473		else /* if (ndisks == sc->sc_ndisks - 1) */
2474			state = G_RAID3_DEVICE_STATE_DEGRADED;
2475		G_RAID3_DEBUG(1, "Device %s state changed from %s to %s.",
2476		    sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2477		    g_raid3_device_state2str(state));
2478		sc->sc_state = state;
2479		for (n = 0; n < sc->sc_ndisks; n++) {
2480			disk = &sc->sc_disks[n];
2481			if (disk->d_state == G_RAID3_DISK_STATE_NODISK)
2482				continue;
2483			state = g_raid3_determine_state(disk);
2484			g_raid3_event_send(disk, state, G_RAID3_EVENT_DONTWAIT);
2485			if (state == G_RAID3_DISK_STATE_STALE)
2486				sc->sc_bump_id |= G_RAID3_BUMP_SYNCID;
2487		}
2488		break;
2489	    }
2490	case G_RAID3_DEVICE_STATE_DEGRADED:
2491		/*
2492		 * Genid need to be bumped immediately, so do it here.
2493		 */
2494		if ((sc->sc_bump_id & G_RAID3_BUMP_GENID) != 0) {
2495			sc->sc_bump_id &= ~G_RAID3_BUMP_GENID;
2496			g_raid3_bump_genid(sc);
2497		}
2498
2499		if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NEW) > 0)
2500			return;
2501		if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) <
2502		    sc->sc_ndisks - 1) {
2503			if (sc->sc_provider != NULL)
2504				g_raid3_destroy_provider(sc);
2505			sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
2506			return;
2507		}
2508		if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) ==
2509		    sc->sc_ndisks) {
2510			state = G_RAID3_DEVICE_STATE_COMPLETE;
2511			G_RAID3_DEBUG(1,
2512			    "Device %s state changed from %s to %s.",
2513			    sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2514			    g_raid3_device_state2str(state));
2515			sc->sc_state = state;
2516		}
2517		if (sc->sc_provider == NULL)
2518			g_raid3_launch_provider(sc);
2519		if (sc->sc_rootmount != NULL) {
2520			G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2521			    sc->sc_rootmount);
2522			root_mount_rel(sc->sc_rootmount);
2523			sc->sc_rootmount = NULL;
2524		}
2525		break;
2526	case G_RAID3_DEVICE_STATE_COMPLETE:
2527		/*
2528		 * Genid need to be bumped immediately, so do it here.
2529		 */
2530		if ((sc->sc_bump_id & G_RAID3_BUMP_GENID) != 0) {
2531			sc->sc_bump_id &= ~G_RAID3_BUMP_GENID;
2532			g_raid3_bump_genid(sc);
2533		}
2534
2535		if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NEW) > 0)
2536			return;
2537		KASSERT(g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) >=
2538		    sc->sc_ndisks - 1,
2539		    ("Too few ACTIVE components in COMPLETE state (device %s).",
2540		    sc->sc_name));
2541		if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) ==
2542		    sc->sc_ndisks - 1) {
2543			state = G_RAID3_DEVICE_STATE_DEGRADED;
2544			G_RAID3_DEBUG(1,
2545			    "Device %s state changed from %s to %s.",
2546			    sc->sc_name, g_raid3_device_state2str(sc->sc_state),
2547			    g_raid3_device_state2str(state));
2548			sc->sc_state = state;
2549		}
2550		if (sc->sc_provider == NULL)
2551			g_raid3_launch_provider(sc);
2552		if (sc->sc_rootmount != NULL) {
2553			G_RAID3_DEBUG(1, "root_mount_rel[%u] %p", __LINE__,
2554			    sc->sc_rootmount);
2555			root_mount_rel(sc->sc_rootmount);
2556			sc->sc_rootmount = NULL;
2557		}
2558		break;
2559	default:
2560		KASSERT(1 == 0, ("Wrong device state (%s, %s).", sc->sc_name,
2561		    g_raid3_device_state2str(sc->sc_state)));
2562		break;
2563	}
2564}
2565
2566/*
2567 * Update disk state and device state if needed.
2568 */
2569#define	DISK_STATE_CHANGED()	G_RAID3_DEBUG(1,			\
2570	"Disk %s state changed from %s to %s (device %s).",		\
2571	g_raid3_get_diskname(disk),					\
2572	g_raid3_disk_state2str(disk->d_state),				\
2573	g_raid3_disk_state2str(state), sc->sc_name)
2574static int
2575g_raid3_update_disk(struct g_raid3_disk *disk, u_int state)
2576{
2577	struct g_raid3_softc *sc;
2578
2579	sc = disk->d_softc;
2580	sx_assert(&sc->sc_lock, SX_XLOCKED);
2581
2582again:
2583	G_RAID3_DEBUG(3, "Changing disk %s state from %s to %s.",
2584	    g_raid3_get_diskname(disk), g_raid3_disk_state2str(disk->d_state),
2585	    g_raid3_disk_state2str(state));
2586	switch (state) {
2587	case G_RAID3_DISK_STATE_NEW:
2588		/*
2589		 * Possible scenarios:
2590		 * 1. New disk arrive.
2591		 */
2592		/* Previous state should be NONE. */
2593		KASSERT(disk->d_state == G_RAID3_DISK_STATE_NONE,
2594		    ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2595		    g_raid3_disk_state2str(disk->d_state)));
2596		DISK_STATE_CHANGED();
2597
2598		disk->d_state = state;
2599		G_RAID3_DEBUG(0, "Device %s: provider %s detected.",
2600		    sc->sc_name, g_raid3_get_diskname(disk));
2601		if (sc->sc_state == G_RAID3_DEVICE_STATE_STARTING)
2602			break;
2603		KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2604		    sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2605		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2606		    g_raid3_device_state2str(sc->sc_state),
2607		    g_raid3_get_diskname(disk),
2608		    g_raid3_disk_state2str(disk->d_state)));
2609		state = g_raid3_determine_state(disk);
2610		if (state != G_RAID3_DISK_STATE_NONE)
2611			goto again;
2612		break;
2613	case G_RAID3_DISK_STATE_ACTIVE:
2614		/*
2615		 * Possible scenarios:
2616		 * 1. New disk does not need synchronization.
2617		 * 2. Synchronization process finished successfully.
2618		 */
2619		KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2620		    sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2621		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2622		    g_raid3_device_state2str(sc->sc_state),
2623		    g_raid3_get_diskname(disk),
2624		    g_raid3_disk_state2str(disk->d_state)));
2625		/* Previous state should be NEW or SYNCHRONIZING. */
2626		KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW ||
2627		    disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2628		    ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2629		    g_raid3_disk_state2str(disk->d_state)));
2630		DISK_STATE_CHANGED();
2631
2632		if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
2633			disk->d_flags &= ~G_RAID3_DISK_FLAG_SYNCHRONIZING;
2634			disk->d_flags &= ~G_RAID3_DISK_FLAG_FORCE_SYNC;
2635			g_raid3_sync_stop(sc, 0);
2636		}
2637		disk->d_state = state;
2638		disk->d_sync.ds_offset = 0;
2639		disk->d_sync.ds_offset_done = 0;
2640		g_raid3_update_idle(sc, disk);
2641		g_raid3_update_metadata(disk);
2642		G_RAID3_DEBUG(0, "Device %s: provider %s activated.",
2643		    sc->sc_name, g_raid3_get_diskname(disk));
2644		break;
2645	case G_RAID3_DISK_STATE_STALE:
2646		/*
2647		 * Possible scenarios:
2648		 * 1. Stale disk was connected.
2649		 */
2650		/* Previous state should be NEW. */
2651		KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2652		    ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2653		    g_raid3_disk_state2str(disk->d_state)));
2654		KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2655		    sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2656		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2657		    g_raid3_device_state2str(sc->sc_state),
2658		    g_raid3_get_diskname(disk),
2659		    g_raid3_disk_state2str(disk->d_state)));
2660		/*
2661		 * STALE state is only possible if device is marked
2662		 * NOAUTOSYNC.
2663		 */
2664		KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_NOAUTOSYNC) != 0,
2665		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2666		    g_raid3_device_state2str(sc->sc_state),
2667		    g_raid3_get_diskname(disk),
2668		    g_raid3_disk_state2str(disk->d_state)));
2669		DISK_STATE_CHANGED();
2670
2671		disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2672		disk->d_state = state;
2673		g_raid3_update_metadata(disk);
2674		G_RAID3_DEBUG(0, "Device %s: provider %s is stale.",
2675		    sc->sc_name, g_raid3_get_diskname(disk));
2676		break;
2677	case G_RAID3_DISK_STATE_SYNCHRONIZING:
2678		/*
2679		 * Possible scenarios:
2680		 * 1. Disk which needs synchronization was connected.
2681		 */
2682		/* Previous state should be NEW. */
2683		KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2684		    ("Wrong disk state (%s, %s).", g_raid3_get_diskname(disk),
2685		    g_raid3_disk_state2str(disk->d_state)));
2686		KASSERT(sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2687		    sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE,
2688		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2689		    g_raid3_device_state2str(sc->sc_state),
2690		    g_raid3_get_diskname(disk),
2691		    g_raid3_disk_state2str(disk->d_state)));
2692		DISK_STATE_CHANGED();
2693
2694		if (disk->d_state == G_RAID3_DISK_STATE_NEW)
2695			disk->d_flags &= ~G_RAID3_DISK_FLAG_DIRTY;
2696		disk->d_state = state;
2697		if (sc->sc_provider != NULL) {
2698			g_raid3_sync_start(sc);
2699			g_raid3_update_metadata(disk);
2700		}
2701		break;
2702	case G_RAID3_DISK_STATE_DISCONNECTED:
2703		/*
2704		 * Possible scenarios:
2705		 * 1. Device wasn't running yet, but disk disappear.
2706		 * 2. Disk was active and disapppear.
2707		 * 3. Disk disappear during synchronization process.
2708		 */
2709		if (sc->sc_state == G_RAID3_DEVICE_STATE_DEGRADED ||
2710		    sc->sc_state == G_RAID3_DEVICE_STATE_COMPLETE) {
2711			/*
2712			 * Previous state should be ACTIVE, STALE or
2713			 * SYNCHRONIZING.
2714			 */
2715			KASSERT(disk->d_state == G_RAID3_DISK_STATE_ACTIVE ||
2716			    disk->d_state == G_RAID3_DISK_STATE_STALE ||
2717			    disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING,
2718			    ("Wrong disk state (%s, %s).",
2719			    g_raid3_get_diskname(disk),
2720			    g_raid3_disk_state2str(disk->d_state)));
2721		} else if (sc->sc_state == G_RAID3_DEVICE_STATE_STARTING) {
2722			/* Previous state should be NEW. */
2723			KASSERT(disk->d_state == G_RAID3_DISK_STATE_NEW,
2724			    ("Wrong disk state (%s, %s).",
2725			    g_raid3_get_diskname(disk),
2726			    g_raid3_disk_state2str(disk->d_state)));
2727			/*
2728			 * Reset bumping syncid if disk disappeared in STARTING
2729			 * state.
2730			 */
2731			if ((sc->sc_bump_id & G_RAID3_BUMP_SYNCID) != 0)
2732				sc->sc_bump_id &= ~G_RAID3_BUMP_SYNCID;
2733#ifdef	INVARIANTS
2734		} else {
2735			KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2736			    sc->sc_name,
2737			    g_raid3_device_state2str(sc->sc_state),
2738			    g_raid3_get_diskname(disk),
2739			    g_raid3_disk_state2str(disk->d_state)));
2740#endif
2741		}
2742		DISK_STATE_CHANGED();
2743		G_RAID3_DEBUG(0, "Device %s: provider %s disconnected.",
2744		    sc->sc_name, g_raid3_get_diskname(disk));
2745
2746		g_raid3_destroy_disk(disk);
2747		break;
2748	default:
2749		KASSERT(1 == 0, ("Unknown state (%u).", state));
2750		break;
2751	}
2752	return (0);
2753}
2754#undef	DISK_STATE_CHANGED
2755
2756int
2757g_raid3_read_metadata(struct g_consumer *cp, struct g_raid3_metadata *md)
2758{
2759	struct g_provider *pp;
2760	u_char *buf;
2761	int error;
2762
2763	g_topology_assert();
2764
2765	error = g_access(cp, 1, 0, 0);
2766	if (error != 0)
2767		return (error);
2768	pp = cp->provider;
2769	g_topology_unlock();
2770	/* Metadata are stored on last sector. */
2771	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2772	    &error);
2773	g_topology_lock();
2774	g_access(cp, -1, 0, 0);
2775	if (buf == NULL) {
2776		G_RAID3_DEBUG(1, "Cannot read metadata from %s (error=%d).",
2777		    cp->provider->name, error);
2778		return (error);
2779	}
2780
2781	/* Decode metadata. */
2782	error = raid3_metadata_decode(buf, md);
2783	g_free(buf);
2784	if (strcmp(md->md_magic, G_RAID3_MAGIC) != 0)
2785		return (EINVAL);
2786	if (md->md_version > G_RAID3_VERSION) {
2787		G_RAID3_DEBUG(0,
2788		    "Kernel module is too old to handle metadata from %s.",
2789		    cp->provider->name);
2790		return (EINVAL);
2791	}
2792	if (error != 0) {
2793		G_RAID3_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2794		    cp->provider->name);
2795		return (error);
2796	}
2797
2798	return (0);
2799}
2800
2801static int
2802g_raid3_check_metadata(struct g_raid3_softc *sc, struct g_provider *pp,
2803    struct g_raid3_metadata *md)
2804{
2805
2806	if (md->md_no >= sc->sc_ndisks) {
2807		G_RAID3_DEBUG(1, "Invalid disk %s number (no=%u), skipping.",
2808		    pp->name, md->md_no);
2809		return (EINVAL);
2810	}
2811	if (sc->sc_disks[md->md_no].d_state != G_RAID3_DISK_STATE_NODISK) {
2812		G_RAID3_DEBUG(1, "Disk %s (no=%u) already exists, skipping.",
2813		    pp->name, md->md_no);
2814		return (EEXIST);
2815	}
2816	if (md->md_all != sc->sc_ndisks) {
2817		G_RAID3_DEBUG(1,
2818		    "Invalid '%s' field on disk %s (device %s), skipping.",
2819		    "md_all", pp->name, sc->sc_name);
2820		return (EINVAL);
2821	}
2822	if (md->md_mediasize != sc->sc_mediasize) {
2823		G_RAID3_DEBUG(1,
2824		    "Invalid '%s' field on disk %s (device %s), skipping.",
2825		    "md_mediasize", pp->name, sc->sc_name);
2826		return (EINVAL);
2827	}
2828	if ((md->md_mediasize % (sc->sc_ndisks - 1)) != 0) {
2829		G_RAID3_DEBUG(1,
2830		    "Invalid '%s' field on disk %s (device %s), skipping.",
2831		    "md_mediasize", pp->name, sc->sc_name);
2832		return (EINVAL);
2833	}
2834	if ((sc->sc_mediasize / (sc->sc_ndisks - 1)) > pp->mediasize) {
2835		G_RAID3_DEBUG(1,
2836		    "Invalid size of disk %s (device %s), skipping.", pp->name,
2837		    sc->sc_name);
2838		return (EINVAL);
2839	}
2840	if ((md->md_sectorsize / pp->sectorsize) < sc->sc_ndisks - 1) {
2841		G_RAID3_DEBUG(1,
2842		    "Invalid '%s' field on disk %s (device %s), skipping.",
2843		    "md_sectorsize", pp->name, sc->sc_name);
2844		return (EINVAL);
2845	}
2846	if (md->md_sectorsize != sc->sc_sectorsize) {
2847		G_RAID3_DEBUG(1,
2848		    "Invalid '%s' field on disk %s (device %s), skipping.",
2849		    "md_sectorsize", pp->name, sc->sc_name);
2850		return (EINVAL);
2851	}
2852	if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2853		G_RAID3_DEBUG(1,
2854		    "Invalid sector size of disk %s (device %s), skipping.",
2855		    pp->name, sc->sc_name);
2856		return (EINVAL);
2857	}
2858	if ((md->md_mflags & ~G_RAID3_DEVICE_FLAG_MASK) != 0) {
2859		G_RAID3_DEBUG(1,
2860		    "Invalid device flags on disk %s (device %s), skipping.",
2861		    pp->name, sc->sc_name);
2862		return (EINVAL);
2863	}
2864	if ((md->md_mflags & G_RAID3_DEVICE_FLAG_VERIFY) != 0 &&
2865	    (md->md_mflags & G_RAID3_DEVICE_FLAG_ROUND_ROBIN) != 0) {
2866		/*
2867		 * VERIFY and ROUND-ROBIN options are mutally exclusive.
2868		 */
2869		G_RAID3_DEBUG(1, "Both VERIFY and ROUND-ROBIN flags exist on "
2870		    "disk %s (device %s), skipping.", pp->name, sc->sc_name);
2871		return (EINVAL);
2872	}
2873	if ((md->md_dflags & ~G_RAID3_DISK_FLAG_MASK) != 0) {
2874		G_RAID3_DEBUG(1,
2875		    "Invalid disk flags on disk %s (device %s), skipping.",
2876		    pp->name, sc->sc_name);
2877		return (EINVAL);
2878	}
2879	return (0);
2880}
2881
2882int
2883g_raid3_add_disk(struct g_raid3_softc *sc, struct g_provider *pp,
2884    struct g_raid3_metadata *md)
2885{
2886	struct g_raid3_disk *disk;
2887	int error;
2888
2889	g_topology_assert_not();
2890	G_RAID3_DEBUG(2, "Adding disk %s.", pp->name);
2891
2892	error = g_raid3_check_metadata(sc, pp, md);
2893	if (error != 0)
2894		return (error);
2895	if (sc->sc_state != G_RAID3_DEVICE_STATE_STARTING &&
2896	    md->md_genid < sc->sc_genid) {
2897		G_RAID3_DEBUG(0, "Component %s (device %s) broken, skipping.",
2898		    pp->name, sc->sc_name);
2899		return (EINVAL);
2900	}
2901	disk = g_raid3_init_disk(sc, pp, md, &error);
2902	if (disk == NULL)
2903		return (error);
2904	error = g_raid3_event_send(disk, G_RAID3_DISK_STATE_NEW,
2905	    G_RAID3_EVENT_WAIT);
2906	if (error != 0)
2907		return (error);
2908	if (md->md_version < G_RAID3_VERSION) {
2909		G_RAID3_DEBUG(0, "Upgrading metadata on %s (v%d->v%d).",
2910		    pp->name, md->md_version, G_RAID3_VERSION);
2911		g_raid3_update_metadata(disk);
2912	}
2913	return (0);
2914}
2915
2916static void
2917g_raid3_destroy_delayed(void *arg, int flag)
2918{
2919	struct g_raid3_softc *sc;
2920	int error;
2921
2922	if (flag == EV_CANCEL) {
2923		G_RAID3_DEBUG(1, "Destroying canceled.");
2924		return;
2925	}
2926	sc = arg;
2927	g_topology_unlock();
2928	sx_xlock(&sc->sc_lock);
2929	KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) == 0,
2930	    ("DESTROY flag set on %s.", sc->sc_name));
2931	KASSERT((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROYING) != 0,
2932	    ("DESTROYING flag not set on %s.", sc->sc_name));
2933	G_RAID3_DEBUG(0, "Destroying %s (delayed).", sc->sc_name);
2934	error = g_raid3_destroy(sc, G_RAID3_DESTROY_SOFT);
2935	if (error != 0) {
2936		G_RAID3_DEBUG(0, "Cannot destroy %s.", sc->sc_name);
2937		sx_xunlock(&sc->sc_lock);
2938	}
2939	g_topology_lock();
2940}
2941
2942static int
2943g_raid3_access(struct g_provider *pp, int acr, int acw, int ace)
2944{
2945	struct g_raid3_softc *sc;
2946	int dcr, dcw, dce, error = 0;
2947
2948	g_topology_assert();
2949	G_RAID3_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2950	    acw, ace);
2951
2952	sc = pp->geom->softc;
2953	if (sc == NULL && acr <= 0 && acw <= 0 && ace <= 0)
2954		return (0);
2955	KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
2956
2957	dcr = pp->acr + acr;
2958	dcw = pp->acw + acw;
2959	dce = pp->ace + ace;
2960
2961	g_topology_unlock();
2962	sx_xlock(&sc->sc_lock);
2963	if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROY) != 0 ||
2964	    g_raid3_ndisks(sc, G_RAID3_DISK_STATE_ACTIVE) < sc->sc_ndisks - 1) {
2965		if (acr > 0 || acw > 0 || ace > 0)
2966			error = ENXIO;
2967		goto end;
2968	}
2969	if (dcw == 0 && !sc->sc_idle)
2970		g_raid3_idle(sc, dcw);
2971	if ((sc->sc_flags & G_RAID3_DEVICE_FLAG_DESTROYING) != 0) {
2972		if (acr > 0 || acw > 0 || ace > 0) {
2973			error = ENXIO;
2974			goto end;
2975		}
2976		if (dcr == 0 && dcw == 0 && dce == 0) {
2977			g_post_event(g_raid3_destroy_delayed, sc, M_WAITOK,
2978			    sc, NULL);
2979		}
2980	}
2981end:
2982	sx_xunlock(&sc->sc_lock);
2983	g_topology_lock();
2984	return (error);
2985}
2986
2987static struct g_geom *
2988g_raid3_create(struct g_class *mp, const struct g_raid3_metadata *md)
2989{
2990	struct g_raid3_softc *sc;
2991	struct g_geom *gp;
2992	int error, timeout;
2993	u_int n;
2994
2995	g_topology_assert();
2996	G_RAID3_DEBUG(1, "Creating device %s (id=%u).", md->md_name, md->md_id);
2997
2998	/* One disk is minimum. */
2999	if (md->md_all < 1)
3000		return (NULL);
3001	/*
3002	 * Action geom.
3003	 */
3004	gp = g_new_geomf(mp, "%s", md->md_name);
3005	sc = malloc(sizeof(*sc), M_RAID3, M_WAITOK | M_ZERO);
3006	sc->sc_disks = malloc(sizeof(struct g_raid3_disk) * md->md_all, M_RAID3,
3007	    M_WAITOK | M_ZERO);
3008	gp->start = g_raid3_start;
3009	gp->orphan = g_raid3_orphan;
3010	gp->access = g_raid3_access;
3011	gp->dumpconf = g_raid3_dumpconf;
3012
3013	sc->sc_id = md->md_id;
3014	sc->sc_mediasize = md->md_mediasize;
3015	sc->sc_sectorsize = md->md_sectorsize;
3016	sc->sc_ndisks = md->md_all;
3017	sc->sc_round_robin = 0;
3018	sc->sc_flags = md->md_mflags;
3019	sc->sc_bump_id = 0;
3020	sc->sc_idle = 1;
3021	sc->sc_last_write = time_uptime;
3022	sc->sc_writes = 0;
3023	for (n = 0; n < sc->sc_ndisks; n++) {
3024		sc->sc_disks[n].d_softc = sc;
3025		sc->sc_disks[n].d_no = n;
3026		sc->sc_disks[n].d_state = G_RAID3_DISK_STATE_NODISK;
3027	}
3028	sx_init(&sc->sc_lock, "graid3:lock");
3029	bioq_init(&sc->sc_queue);
3030	mtx_init(&sc->sc_queue_mtx, "graid3:queue", NULL, MTX_DEF);
3031	bioq_init(&sc->sc_regular_delayed);
3032	bioq_init(&sc->sc_inflight);
3033	bioq_init(&sc->sc_sync_delayed);
3034	TAILQ_INIT(&sc->sc_events);
3035	mtx_init(&sc->sc_events_mtx, "graid3:events", NULL, MTX_DEF);
3036	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
3037	sc->sc_state = G_RAID3_DEVICE_STATE_STARTING;
3038	gp->softc = sc;
3039	sc->sc_geom = gp;
3040	sc->sc_provider = NULL;
3041	/*
3042	 * Synchronization geom.
3043	 */
3044	gp = g_new_geomf(mp, "%s.sync", md->md_name);
3045	gp->softc = sc;
3046	gp->orphan = g_raid3_orphan;
3047	sc->sc_sync.ds_geom = gp;
3048
3049	sc->sc_zones[G_RAID3_ZONE_64K].sz_zone = uma_zcreate("gr3:64k", 65536,
3050	    g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
3051	sc->sc_zones[G_RAID3_ZONE_64K].sz_inuse = 0;
3052	sc->sc_zones[G_RAID3_ZONE_64K].sz_max = g_raid3_n64k;
3053	sc->sc_zones[G_RAID3_ZONE_64K].sz_requested =
3054	    sc->sc_zones[G_RAID3_ZONE_64K].sz_failed = 0;
3055	sc->sc_zones[G_RAID3_ZONE_16K].sz_zone = uma_zcreate("gr3:16k", 16384,
3056	    g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
3057	sc->sc_zones[G_RAID3_ZONE_16K].sz_inuse = 0;
3058	sc->sc_zones[G_RAID3_ZONE_16K].sz_max = g_raid3_n16k;
3059	sc->sc_zones[G_RAID3_ZONE_16K].sz_requested =
3060	    sc->sc_zones[G_RAID3_ZONE_16K].sz_failed = 0;
3061	sc->sc_zones[G_RAID3_ZONE_4K].sz_zone = uma_zcreate("gr3:4k", 4096,
3062	    g_raid3_uma_ctor, g_raid3_uma_dtor, NULL, NULL, UMA_ALIGN_PTR, 0);
3063	sc->sc_zones[G_RAID3_ZONE_4K].sz_inuse = 0;
3064	sc->sc_zones[G_RAID3_ZONE_4K].sz_max = g_raid3_n4k;
3065	sc->sc_zones[G_RAID3_ZONE_4K].sz_requested =
3066	    sc->sc_zones[G_RAID3_ZONE_4K].sz_failed = 0;
3067
3068	error = kthread_create(g_raid3_worker, sc, &sc->sc_worker, 0, 0,
3069	    "g_raid3 %s", md->md_name);
3070	if (error != 0) {
3071		G_RAID3_DEBUG(1, "Cannot create kernel thread for %s.",
3072		    sc->sc_name);
3073		uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_64K].sz_zone);
3074		uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_16K].sz_zone);
3075		uma_zdestroy(sc->sc_zones[G_RAID3_ZONE_4K].sz_zone);
3076		g_destroy_geom(sc->sc_sync.ds_geom);
3077		mtx_destroy(&sc->sc_events_mtx);
3078		mtx_destroy(&sc->sc_queue_mtx);
3079		sx_destroy(&sc->sc_lock);
3080		g_destroy_geom(sc->sc_geom);
3081		free(sc->sc_disks, M_RAID3);
3082		free(sc, M_RAID3);
3083		return (NULL);
3084	}
3085
3086	G_RAID3_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
3087
3088	sc->sc_rootmount = root_mount_hold("GRAID3");
3089	G_RAID3_DEBUG(1, "root_mount_hold %p", sc->sc_rootmount);
3090
3091	/*
3092	 * Run timeout.
3093	 */
3094	timeout = atomic_load_acq_int(&g_raid3_timeout);
3095	callout_reset(&sc->sc_callout, timeout * hz, g_raid3_go, sc);
3096	return (sc->sc_geom);
3097}
3098
3099int
3100g_raid3_destroy(struct g_raid3_softc *sc, int how)
3101{
3102	struct g_provider *pp;
3103
3104	g_topology_assert_not();
3105	if (sc == NULL)
3106		return (ENXIO);
3107	sx_assert(&sc->sc_lock, SX_XLOCKED);
3108
3109	pp = sc->sc_provider;
3110	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
3111		switch (how) {
3112		case G_RAID3_DESTROY_SOFT:
3113			G_RAID3_DEBUG(1,
3114			    "Device %s is still open (r%dw%de%d).", pp->name,
3115			    pp->acr, pp->acw, pp->ace);
3116			return (EBUSY);
3117		case G_RAID3_DESTROY_DELAYED:
3118			G_RAID3_DEBUG(1,
3119			    "Device %s will be destroyed on last close.",
3120			    pp->name);
3121			if (sc->sc_syncdisk != NULL)
3122				g_raid3_sync_stop(sc, 1);
3123			sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROYING;
3124			return (EBUSY);
3125		case G_RAID3_DESTROY_HARD:
3126			G_RAID3_DEBUG(1, "Device %s is still open, so it "
3127			    "can't be definitely removed.", pp->name);
3128			break;
3129		}
3130	}
3131
3132	g_topology_lock();
3133	if (sc->sc_geom->softc == NULL) {
3134		g_topology_unlock();
3135		return (0);
3136	}
3137	sc->sc_geom->softc = NULL;
3138	sc->sc_sync.ds_geom->softc = NULL;
3139	g_topology_unlock();
3140
3141	sc->sc_flags |= G_RAID3_DEVICE_FLAG_DESTROY;
3142	sc->sc_flags |= G_RAID3_DEVICE_FLAG_WAIT;
3143	G_RAID3_DEBUG(4, "%s: Waking up %p.", __func__, sc);
3144	sx_xunlock(&sc->sc_lock);
3145	mtx_lock(&sc->sc_queue_mtx);
3146	wakeup(sc);
3147	wakeup(&sc->sc_queue);
3148	mtx_unlock(&sc->sc_queue_mtx);
3149	G_RAID3_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
3150	while (sc->sc_worker != NULL)
3151		tsleep(&sc->sc_worker, PRIBIO, "r3:destroy", hz / 5);
3152	G_RAID3_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
3153	sx_xlock(&sc->sc_lock);
3154	g_raid3_destroy_device(sc);
3155	free(sc->sc_disks, M_RAID3);
3156	free(sc, M_RAID3);
3157	return (0);
3158}
3159
3160static void
3161g_raid3_taste_orphan(struct g_consumer *cp)
3162{
3163
3164	KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
3165	    cp->provider->name));
3166}
3167
3168static struct g_geom *
3169g_raid3_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
3170{
3171	struct g_raid3_metadata md;
3172	struct g_raid3_softc *sc;
3173	struct g_consumer *cp;
3174	struct g_geom *gp;
3175	int error;
3176
3177	g_topology_assert();
3178	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
3179	G_RAID3_DEBUG(2, "Tasting %s.", pp->name);
3180
3181	gp = g_new_geomf(mp, "raid3:taste");
3182	/* This orphan function should be never called. */
3183	gp->orphan = g_raid3_taste_orphan;
3184	cp = g_new_consumer(gp);
3185	g_attach(cp, pp);
3186	error = g_raid3_read_metadata(cp, &md);
3187	g_detach(cp);
3188	g_destroy_consumer(cp);
3189	g_destroy_geom(gp);
3190	if (error != 0)
3191		return (NULL);
3192	gp = NULL;
3193
3194	if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0)
3195		return (NULL);
3196	if (md.md_provsize != 0 && md.md_provsize != pp->mediasize)
3197		return (NULL);
3198	if (g_raid3_debug >= 2)
3199		raid3_metadata_dump(&md);
3200
3201	/*
3202	 * Let's check if device already exists.
3203	 */
3204	sc = NULL;
3205	LIST_FOREACH(gp, &mp->geom, geom) {
3206		sc = gp->softc;
3207		if (sc == NULL)
3208			continue;
3209		if (sc->sc_sync.ds_geom == gp)
3210			continue;
3211		if (strcmp(md.md_name, sc->sc_name) != 0)
3212			continue;
3213		if (md.md_id != sc->sc_id) {
3214			G_RAID3_DEBUG(0, "Device %s already configured.",
3215			    sc->sc_name);
3216			return (NULL);
3217		}
3218		break;
3219	}
3220	if (gp == NULL) {
3221		gp = g_raid3_create(mp, &md);
3222		if (gp == NULL) {
3223			G_RAID3_DEBUG(0, "Cannot create device %s.",
3224			    md.md_name);
3225			return (NULL);
3226		}
3227		sc = gp->softc;
3228	}
3229	G_RAID3_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
3230	g_topology_unlock();
3231	sx_xlock(&sc->sc_lock);
3232	error = g_raid3_add_disk(sc, pp, &md);
3233	if (error != 0) {
3234		G_RAID3_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
3235		    pp->name, gp->name, error);
3236		if (g_raid3_ndisks(sc, G_RAID3_DISK_STATE_NODISK) ==
3237		    sc->sc_ndisks) {
3238			g_cancel_event(sc);
3239			g_raid3_destroy(sc, 1);
3240			g_topology_lock();
3241			return (NULL);
3242		}
3243		gp = NULL;
3244	}
3245	sx_xunlock(&sc->sc_lock);
3246	g_topology_lock();
3247	return (gp);
3248}
3249
3250static int
3251g_raid3_destroy_geom(struct gctl_req *req __unused, struct g_class *mp __unused,
3252    struct g_geom *gp)
3253{
3254	struct g_raid3_softc *sc;
3255	int error;
3256
3257	g_topology_unlock();
3258	sc = gp->softc;
3259	sx_xlock(&sc->sc_lock);
3260	g_cancel_event(sc);
3261	error = g_raid3_destroy(gp->softc, 0);
3262	if (error != 0)
3263		sx_xunlock(&sc->sc_lock);
3264	g_topology_lock();
3265	return (error);
3266}
3267
3268static void
3269g_raid3_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
3270    struct g_consumer *cp, struct g_provider *pp)
3271{
3272	struct g_raid3_softc *sc;
3273
3274	g_topology_assert();
3275
3276	sc = gp->softc;
3277	if (sc == NULL)
3278		return;
3279	/* Skip synchronization geom. */
3280	if (gp == sc->sc_sync.ds_geom)
3281		return;
3282	if (pp != NULL) {
3283		/* Nothing here. */
3284	} else if (cp != NULL) {
3285		struct g_raid3_disk *disk;
3286
3287		disk = cp->private;
3288		if (disk == NULL)
3289			return;
3290		g_topology_unlock();
3291		sx_xlock(&sc->sc_lock);
3292		sbuf_printf(sb, "%s<Type>", indent);
3293		if (disk->d_no == sc->sc_ndisks - 1)
3294			sbuf_printf(sb, "PARITY");
3295		else
3296			sbuf_printf(sb, "DATA");
3297		sbuf_printf(sb, "</Type>\n");
3298		sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
3299		    (u_int)disk->d_no);
3300		if (disk->d_state == G_RAID3_DISK_STATE_SYNCHRONIZING) {
3301			sbuf_printf(sb, "%s<Synchronized>", indent);
3302			if (disk->d_sync.ds_offset == 0)
3303				sbuf_printf(sb, "0%%");
3304			else {
3305				sbuf_printf(sb, "%u%%",
3306				    (u_int)((disk->d_sync.ds_offset * 100) /
3307				    (sc->sc_mediasize / (sc->sc_ndisks - 1))));
3308			}
3309			sbuf_printf(sb, "</Synchronized>\n");
3310		}
3311		sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
3312		    disk->d_sync.ds_syncid);
3313		sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, disk->d_genid);
3314		sbuf_printf(sb, "%s<Flags>", indent);
3315		if (disk->d_flags == 0)
3316			sbuf_printf(sb, "NONE");
3317		else {
3318			int first = 1;
3319
3320#define	ADD_FLAG(flag, name)	do {					\
3321	if ((disk->d_flags & (flag)) != 0) {				\
3322		if (!first)						\
3323			sbuf_printf(sb, ", ");				\
3324		else							\
3325			first = 0;					\
3326		sbuf_printf(sb, name);					\
3327	}								\
3328} while (0)
3329			ADD_FLAG(G_RAID3_DISK_FLAG_DIRTY, "DIRTY");
3330			ADD_FLAG(G_RAID3_DISK_FLAG_HARDCODED, "HARDCODED");
3331			ADD_FLAG(G_RAID3_DISK_FLAG_SYNCHRONIZING,
3332			    "SYNCHRONIZING");
3333			ADD_FLAG(G_RAID3_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
3334			ADD_FLAG(G_RAID3_DISK_FLAG_BROKEN, "BROKEN");
3335#undef	ADD_FLAG
3336		}
3337		sbuf_printf(sb, "</Flags>\n");
3338		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3339		    g_raid3_disk_state2str(disk->d_state));
3340		sx_xunlock(&sc->sc_lock);
3341		g_topology_lock();
3342	} else {
3343		g_topology_unlock();
3344		sx_xlock(&sc->sc_lock);
3345		sbuf_printf(sb, "%s<Zone4kRequested>%u</Zone4kRequested>\n",
3346		    indent, sc->sc_zones[G_RAID3_ZONE_4K].sz_requested);
3347		sbuf_printf(sb, "%s<Zone4kFailed>%u</Zone4kFailed>\n",
3348		    indent, sc->sc_zones[G_RAID3_ZONE_4K].sz_failed);
3349		sbuf_printf(sb, "%s<Zone16kRequested>%u</Zone16kRequested>\n",
3350		    indent, sc->sc_zones[G_RAID3_ZONE_16K].sz_requested);
3351		sbuf_printf(sb, "%s<Zone16kFailed>%u</Zone16kFailed>\n",
3352		    indent, sc->sc_zones[G_RAID3_ZONE_16K].sz_failed);
3353		sbuf_printf(sb, "%s<Zone64kRequested>%u</Zone64kRequested>\n",
3354		    indent, sc->sc_zones[G_RAID3_ZONE_64K].sz_requested);
3355		sbuf_printf(sb, "%s<Zone64kFailed>%u</Zone64kFailed>\n",
3356		    indent, sc->sc_zones[G_RAID3_ZONE_64K].sz_failed);
3357		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
3358		sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
3359		sbuf_printf(sb, "%s<GenID>%u</GenID>\n", indent, sc->sc_genid);
3360		sbuf_printf(sb, "%s<Flags>", indent);
3361		if (sc->sc_flags == 0)
3362			sbuf_printf(sb, "NONE");
3363		else {
3364			int first = 1;
3365
3366#define	ADD_FLAG(flag, name)	do {					\
3367	if ((sc->sc_flags & (flag)) != 0) {				\
3368		if (!first)						\
3369			sbuf_printf(sb, ", ");				\
3370		else							\
3371			first = 0;					\
3372		sbuf_printf(sb, name);					\
3373	}								\
3374} while (0)
3375			ADD_FLAG(G_RAID3_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
3376			ADD_FLAG(G_RAID3_DEVICE_FLAG_ROUND_ROBIN,
3377			    "ROUND-ROBIN");
3378			ADD_FLAG(G_RAID3_DEVICE_FLAG_VERIFY, "VERIFY");
3379#undef	ADD_FLAG
3380		}
3381		sbuf_printf(sb, "</Flags>\n");
3382		sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
3383		    sc->sc_ndisks);
3384		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
3385		    g_raid3_device_state2str(sc->sc_state));
3386		sx_xunlock(&sc->sc_lock);
3387		g_topology_lock();
3388	}
3389}
3390
3391static void
3392g_raid3_shutdown_pre_sync(void *arg, int howto)
3393{
3394	struct g_class *mp;
3395	struct g_geom *gp, *gp2;
3396	struct g_raid3_softc *sc;
3397	int error;
3398
3399	mp = arg;
3400	DROP_GIANT();
3401	g_topology_lock();
3402	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
3403		if ((sc = gp->softc) == NULL)
3404			continue;
3405		/* Skip synchronization geom. */
3406		if (gp == sc->sc_sync.ds_geom)
3407			continue;
3408		g_topology_unlock();
3409		sx_xlock(&sc->sc_lock);
3410		g_cancel_event(sc);
3411		error = g_raid3_destroy(sc, G_RAID3_DESTROY_DELAYED);
3412		if (error != 0)
3413			sx_xunlock(&sc->sc_lock);
3414		g_topology_lock();
3415	}
3416	g_topology_unlock();
3417	PICKUP_GIANT();
3418}
3419
3420static void
3421g_raid3_init(struct g_class *mp)
3422{
3423
3424	g_raid3_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
3425	    g_raid3_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
3426	if (g_raid3_pre_sync == NULL)
3427		G_RAID3_DEBUG(0, "Warning! Cannot register shutdown event.");
3428}
3429
3430static void
3431g_raid3_fini(struct g_class *mp)
3432{
3433
3434	if (g_raid3_pre_sync != NULL)
3435		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_raid3_pre_sync);
3436}
3437
3438DECLARE_GEOM_CLASS(g_raid3_class, g_raid3);
3439