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