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