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