g_mirror.c revision 136504
121308Sache/*-
221308Sache * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
321308Sache * All rights reserved.
421308Sache *
521308Sache * Redistribution and use in source and binary forms, with or without
621308Sache * modification, are permitted provided that the following conditions
721308Sache * are met:
821308Sache * 1. Redistributions of source code must retain the above copyright
921308Sache *    notice, this list of conditions and the following disclaimer.
1021308Sache * 2. Redistributions in binary form must reproduce the above copyright
1158310Sache *    notice, this list of conditions and the following disclaimer in the
1221308Sache *    documentation and/or other materials provided with the distribution.
1321308Sache *
1421308Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1521308Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1621308Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1721308Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
1821308Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1921308Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2021308Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2121308Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2258310Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2321308Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2421308Sache * SUCH DAMAGE.
2521308Sache */
2621308Sache
2721308Sache#include <sys/cdefs.h>
2821308Sache__FBSDID("$FreeBSD: head/sys/geom/mirror/g_mirror.c 136504 2004-10-14 07:55:29Z pjd $");
2921308Sache
3021308Sache#include <sys/param.h>
3121308Sache#include <sys/systm.h>
3221308Sache#include <sys/kernel.h>
3321308Sache#include <sys/module.h>
3421308Sache#include <sys/limits.h>
3521308Sache#include <sys/lock.h>
3621308Sache#include <sys/mutex.h>
3721308Sache#include <sys/bio.h>
3821308Sache#include <sys/sysctl.h>
3921308Sache#include <sys/malloc.h>
4021308Sache#include <sys/bitstring.h>
4121308Sache#include <vm/uma.h>
4221308Sache#include <machine/atomic.h>
4321308Sache#include <geom/geom.h>
4421308Sache#include <sys/proc.h>
4521308Sache#include <sys/kthread.h>
4621308Sache#include <geom/mirror/g_mirror.h>
4721308Sache
4821308Sache
4921308Sachestatic MALLOC_DEFINE(M_MIRROR, "mirror data", "GEOM_MIRROR Data");
5021308Sache
5121308SacheSYSCTL_DECL(_kern_geom);
5221308SacheSYSCTL_NODE(_kern_geom, OID_AUTO, mirror, CTLFLAG_RW, 0, "GEOM_MIRROR stuff");
5321308Sacheu_int g_mirror_debug = 0;
54119610SacheTUNABLE_INT("kern.geom.mirror.debug", &g_mirror_debug);
55119610SacheSYSCTL_UINT(_kern_geom_mirror, OID_AUTO, debug, CTLFLAG_RW, &g_mirror_debug, 0,
5621308Sache    "Debug level");
5721308Sachestatic u_int g_mirror_timeout = 4;
5821308SacheTUNABLE_INT("kern.geom.mirror.timeout", &g_mirror_timeout);
5958310SacheSYSCTL_UINT(_kern_geom_mirror, OID_AUTO, timeout, CTLFLAG_RW, &g_mirror_timeout,
6058310Sache    0, "Time to wait on all mirror components");
6158310Sachestatic u_int g_mirror_reqs_per_sync = 5;
6221308SacheSYSCTL_UINT(_kern_geom_mirror, OID_AUTO, reqs_per_sync, CTLFLAG_RW,
6321308Sache    &g_mirror_reqs_per_sync, 0,
6421308Sache    "Number of regular I/O requests per synchronization request");
6521308Sachestatic u_int g_mirror_syncs_per_sec = 100;
6621308SacheSYSCTL_UINT(_kern_geom_mirror, OID_AUTO, syncs_per_sec, CTLFLAG_RW,
6721308Sache    &g_mirror_syncs_per_sec, 0,
6821308Sache    "Number of synchronizations requests per second");
6921308Sache
70119610Sache#define	MSLEEP(ident, mtx, priority, wmesg, timeout)	do {		\
7121308Sache	G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, (ident));	\
7221308Sache	msleep((ident), (mtx), (priority), (wmesg), (timeout));		\
7321308Sache	G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, (ident));	\
7421308Sache} while (0)
7521308Sache
7621308Sache
7721308Sachestatic int g_mirror_destroy_geom(struct gctl_req *req, struct g_class *mp,
7821308Sache    struct g_geom *gp);
7921308Sachestatic g_taste_t g_mirror_taste;
8021308Sache
8121308Sachestruct g_class g_mirror_class = {
8221308Sache	.name = G_MIRROR_CLASS_NAME,
8321308Sache	.version = G_VERSION,
8421308Sache	.ctlreq = g_mirror_config,
8521308Sache	.taste = g_mirror_taste,
8621308Sache	.destroy_geom = g_mirror_destroy_geom
8721308Sache};
8821308Sache
8921308Sache
90119610Sachestatic void g_mirror_destroy_provider(struct g_mirror_softc *sc);
91119610Sachestatic int g_mirror_update_disk(struct g_mirror_disk *disk, u_int state);
92119610Sachestatic void g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force);
9321308Sachestatic void g_mirror_dumpconf(struct sbuf *sb, const char *indent,
94119610Sache    struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
9521308Sachestatic void g_mirror_sync_stop(struct g_mirror_disk *disk, int type);
9621308Sache
9721308Sache
9821308Sachestatic const char *
9921308Sacheg_mirror_disk_state2str(int state)
10021308Sache{
10121308Sache
10275406Sache	switch (state) {
10321308Sache	case G_MIRROR_DISK_STATE_NONE:
10421308Sache		return ("NONE");
105119610Sache	case G_MIRROR_DISK_STATE_NEW:
10621308Sache		return ("NEW");
107119610Sache	case G_MIRROR_DISK_STATE_ACTIVE:
108119610Sache		return ("ACTIVE");
109119610Sache	case G_MIRROR_DISK_STATE_STALE:
11021308Sache		return ("STALE");
11121308Sache	case G_MIRROR_DISK_STATE_SYNCHRONIZING:
11221308Sache		return ("SYNCHRONIZING");
11321308Sache	case G_MIRROR_DISK_STATE_DISCONNECTED:
11421308Sache		return ("DISCONNECTED");
11521308Sache	case G_MIRROR_DISK_STATE_DESTROY:
11621308Sache		return ("DESTROY");
11721308Sache	default:
11821308Sache		return ("INVALID");
11921308Sache	}
12021308Sache}
12121308Sache
12221308Sachestatic const char *
12321308Sacheg_mirror_device_state2str(int state)
12421308Sache{
12521308Sache
12621308Sache	switch (state) {
12721308Sache	case G_MIRROR_DEVICE_STATE_STARTING:
12821308Sache		return ("STARTING");
12921308Sache	case G_MIRROR_DEVICE_STATE_RUNNING:
13021308Sache		return ("RUNNING");
13121308Sache	default:
13221308Sache		return ("INVALID");
13321308Sache	}
13421308Sache}
13521308Sache
13621308Sachestatic const char *
13721308Sacheg_mirror_get_diskname(struct g_mirror_disk *disk)
13821308Sache{
13921308Sache
14021308Sache	if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
14121308Sache		return ("[unknown]");
14221308Sache	return (disk->d_name);
14321308Sache}
14421308Sache
14521308Sache/*
14621308Sache * --- Events handling functions ---
14721308Sache * Events in geom_mirror are used to maintain disks and device status
14821308Sache * from one thread to simplify locking.
14921308Sache */
15021308Sachestatic void
15121308Sacheg_mirror_event_free(struct g_mirror_event *ep)
15221308Sache{
15321308Sache
15421308Sache	free(ep, M_MIRROR);
15521308Sache}
15621308Sache
15721308Sacheint
15821308Sacheg_mirror_event_send(void *arg, int state, int flags)
15921308Sache{
16021308Sache	struct g_mirror_softc *sc;
16121308Sache	struct g_mirror_disk *disk;
16221308Sache	struct g_mirror_event *ep;
163119610Sache	int error;
164119610Sache
16521308Sache	ep = malloc(sizeof(*ep), M_MIRROR, M_WAITOK);
16621308Sache	G_MIRROR_DEBUG(4, "%s: Sending event %p.", __func__, ep);
16721308Sache	if ((flags & G_MIRROR_EVENT_DEVICE) != 0) {
16821308Sache		disk = NULL;
16921308Sache		sc = arg;
17021308Sache	} else {
171119610Sache		disk = arg;
17221308Sache		sc = disk->d_softc;
17321308Sache	}
17421308Sache	ep->e_disk = disk;
17521308Sache	ep->e_state = state;
17621308Sache	ep->e_flags = flags;
17721308Sache	ep->e_error = 0;
17821308Sache	mtx_lock(&sc->sc_events_mtx);
17921308Sache	TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
18021308Sache	mtx_unlock(&sc->sc_events_mtx);
18121308Sache	G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
18221308Sache	mtx_lock(&sc->sc_queue_mtx);
183119610Sache	wakeup(sc);
18421308Sache	mtx_unlock(&sc->sc_queue_mtx);
18521308Sache	if ((flags & G_MIRROR_EVENT_DONTWAIT) != 0)
186119610Sache		return (0);
18721308Sache	g_topology_assert();
18821308Sache	G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, ep);
18921308Sache	g_topology_unlock();
19021308Sache	while ((ep->e_flags & G_MIRROR_EVENT_DONE) == 0) {
19121308Sache		mtx_lock(&sc->sc_events_mtx);
19221308Sache		MSLEEP(ep, &sc->sc_events_mtx, PRIBIO | PDROP, "m:event",
19321308Sache		    hz * 5);
19421308Sache	}
19521308Sache	/* Don't even try to use 'sc' here, because it could be already dead. */
19621308Sache	g_topology_lock();
19721308Sache	error = ep->e_error;
19821308Sache	g_mirror_event_free(ep);
19921308Sache	return (error);
20021308Sache}
20121308Sache
20221308Sachestatic struct g_mirror_event *
20321308Sacheg_mirror_event_get(struct g_mirror_softc *sc)
20421308Sache{
20521308Sache	struct g_mirror_event *ep;
20621308Sache
20721308Sache	mtx_lock(&sc->sc_events_mtx);
20821308Sache	ep = TAILQ_FIRST(&sc->sc_events);
20921308Sache	if (ep != NULL)
21021308Sache		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
21121308Sache	mtx_unlock(&sc->sc_events_mtx);
21221308Sache	return (ep);
21321308Sache}
21421308Sache
21521308Sachestatic void
21621308Sacheg_mirror_event_cancel(struct g_mirror_disk *disk)
21721308Sache{
21821308Sache	struct g_mirror_softc *sc;
21921308Sache	struct g_mirror_event *ep, *tmpep;
22021308Sache
22121308Sache	g_topology_assert();
22221308Sache
22321308Sache	sc = disk->d_softc;
22421308Sache	mtx_lock(&sc->sc_events_mtx);
22521308Sache	TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
22621308Sache		if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0)
22721308Sache			continue;
22821308Sache		if (ep->e_disk != disk)
22921308Sache			continue;
23021308Sache		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
23121308Sache		if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
23221308Sache			g_mirror_event_free(ep);
23321308Sache		else {
23421308Sache			ep->e_error = ECANCELED;
23521308Sache			wakeup(ep);
23621308Sache		}
23721308Sache	}
23821308Sache	mtx_unlock(&sc->sc_events_mtx);
23921308Sache}
24021308Sache
24121308Sache/*
24221308Sache * Return the number of disks in given state.
24321308Sache * If state is equal to -1, count all connected disks.
24421308Sache */
24521308Sacheu_int
24621308Sacheg_mirror_ndisks(struct g_mirror_softc *sc, int state)
24721308Sache{
24821308Sache	struct g_mirror_disk *disk;
24921308Sache	u_int n = 0;
25021308Sache
25121308Sache	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
25221308Sache		if (state == -1 || disk->d_state == state)
25321308Sache			n++;
25421308Sache	}
25521308Sache	return (n);
25621308Sache}
25721308Sache
25821308Sache/*
25921308Sache * Find a disk in mirror by its disk ID.
26021308Sache */
26121308Sachestatic struct g_mirror_disk *
26221308Sacheg_mirror_id2disk(struct g_mirror_softc *sc, uint32_t id)
26321308Sache{
26421308Sache	struct g_mirror_disk *disk;
26521308Sache
26621308Sache	g_topology_assert();
26721308Sache
26821308Sache	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
26921308Sache		if (disk->d_id == id)
27021308Sache			return (disk);
27121308Sache	}
27275406Sache	return (NULL);
27321308Sache}
27421308Sache
27521308Sachestatic u_int
27621308Sacheg_mirror_nrequests(struct g_mirror_softc *sc, struct g_consumer *cp)
27721308Sache{
27821308Sache	struct bio *bp;
27921308Sache	u_int nreqs = 0;
28021308Sache
28121308Sache	mtx_lock(&sc->sc_queue_mtx);
28221308Sache	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
28321308Sache		if (bp->bio_from == cp)
28421308Sache			nreqs++;
28521308Sache	}
28621308Sache	mtx_unlock(&sc->sc_queue_mtx);
28721308Sache	return (nreqs);
28821308Sache}
28921308Sache
29021308Sachestatic int
29121308Sacheg_mirror_is_busy(struct g_mirror_softc *sc, struct g_consumer *cp)
29221308Sache{
29321308Sache
29421308Sache	if (cp->nstart != cp->nend) {
29521308Sache		G_MIRROR_DEBUG(2,
29621308Sache		    "I/O requests for %s exist, can't destroy it now.",
29721308Sache		    cp->provider->name);
29821308Sache		return (1);
29921308Sache	}
30021308Sache	if (g_mirror_nrequests(sc, cp) > 0) {
30121308Sache		G_MIRROR_DEBUG(2,
30221308Sache		    "I/O requests for %s in queue, can't destroy it now.",
30321308Sache		    cp->provider->name);
30421308Sache		return (1);
30521308Sache	}
30621308Sache	return (0);
30721308Sache}
30821308Sache
30921308Sachestatic void
31021308Sacheg_mirror_kill_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
31121308Sache{
31221308Sache
31321308Sache	g_topology_assert();
31421308Sache
31521308Sache	cp->private = NULL;
31621308Sache	if (g_mirror_is_busy(sc, cp))
31721308Sache		return;
31821308Sache	G_MIRROR_DEBUG(2, "Consumer %s destroyed.", cp->provider->name);
31921308Sache	g_detach(cp);
32021308Sache	g_destroy_consumer(cp);
32121308Sache}
32221308Sache
32321308Sachestatic int
32421308Sacheg_mirror_connect_disk(struct g_mirror_disk *disk, struct g_provider *pp)
32521308Sache{
32621308Sache	int error;
32721308Sache
32875406Sache	g_topology_assert();
32921308Sache	KASSERT(disk->d_consumer == NULL,
33021308Sache	    ("Disk already connected (device %s).", disk->d_softc->sc_name));
33121308Sache
33221308Sache	disk->d_consumer = g_new_consumer(disk->d_softc->sc_geom);
33347558Sache	disk->d_consumer->private = disk;
33421308Sache	error = g_attach(disk->d_consumer, pp);
33547558Sache	if (error != 0)
33621308Sache		return (error);
33721308Sache	G_MIRROR_DEBUG(2, "Disk %s connected.", g_mirror_get_diskname(disk));
33821308Sache	return (0);
33921308Sache}
34021308Sache
34121308Sachestatic void
34221308Sacheg_mirror_disconnect_consumer(struct g_mirror_softc *sc, struct g_consumer *cp)
34321308Sache{
34421308Sache
34521308Sache	g_topology_assert();
34621308Sache
34721308Sache	if (cp == NULL)
34821308Sache		return;
34921308Sache	if (cp->provider != NULL) {
35075406Sache		G_MIRROR_DEBUG(2, "Disk %s disconnected.", cp->provider->name);
35121308Sache		if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) {
35221308Sache			G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d",
35321308Sache			    cp->provider->name, -cp->acr, -cp->acw, -cp->ace,
35421308Sache			    0);
35547558Sache			g_access(cp, -cp->acr, -cp->acw, -cp->ace);
35621308Sache		}
35747558Sache		g_mirror_kill_consumer(sc, cp);
35821308Sache	} else {
35921308Sache		g_destroy_consumer(cp);
36021308Sache	}
36121308Sache}
36221308Sache
36321308Sache/*
36421308Sache * Initialize disk. This means allocate memory, create consumer, attach it
36521308Sache * to the provider and open access (r1w1e1) to it.
36621308Sache */
36721308Sachestatic struct g_mirror_disk *
36875406Sacheg_mirror_init_disk(struct g_mirror_softc *sc, struct g_provider *pp,
36921308Sache    struct g_mirror_metadata *md, int *errorp)
37021308Sache{
37121308Sache	struct g_mirror_disk *disk;
37221308Sache	int error;
37347558Sache
37421308Sache	disk = malloc(sizeof(*disk), M_MIRROR, M_NOWAIT | M_ZERO);
37547558Sache	if (disk == NULL) {
37621308Sache		error = ENOMEM;
37721308Sache		goto fail;
37821308Sache	}
37921308Sache	disk->d_softc = sc;
38021308Sache	error = g_mirror_connect_disk(disk, pp);
38147558Sache	if (error != 0)
38247558Sache		goto fail;
38321308Sache	disk->d_id = md->md_did;
38421308Sache	disk->d_state = G_MIRROR_DISK_STATE_NONE;
38521308Sache	disk->d_priority = md->md_priority;
38621308Sache	disk->d_delay.sec = 0;
38721308Sache	disk->d_delay.frac = 0;
38821308Sache	binuptime(&disk->d_last_used);
38921308Sache	disk->d_flags = md->md_dflags;
39021308Sache	if (md->md_provider[0] != '\0')
39121308Sache		disk->d_flags |= G_MIRROR_DISK_FLAG_HARDCODED;
39221308Sache	disk->d_sync.ds_consumer = NULL;
39321308Sache	disk->d_sync.ds_offset = md->md_sync_offset;
39421308Sache	disk->d_sync.ds_offset_done = md->md_sync_offset;
39521308Sache	disk->d_sync.ds_resync = -1;
39621308Sache	disk->d_sync.ds_syncid = md->md_syncid;
39721308Sache	if (errorp != NULL)
39847558Sache		*errorp = 0;
39947558Sache	return (disk);
40021308Sachefail:
40121308Sache	if (errorp != NULL)
40221308Sache		*errorp = error;
40321308Sache	if (disk != NULL) {
40421308Sache		g_mirror_disconnect_consumer(sc, disk->d_consumer);
40521308Sache		free(disk, M_MIRROR);
40621308Sache	}
40721308Sache	return (NULL);
40821308Sache}
40921308Sache
41021308Sachestatic void
41121308Sacheg_mirror_destroy_disk(struct g_mirror_disk *disk)
41221308Sache{
41321308Sache	struct g_mirror_softc *sc;
41421308Sache
41521308Sache	g_topology_assert();
41621308Sache
41721308Sache	LIST_REMOVE(disk, d_next);
41821308Sache	g_mirror_event_cancel(disk);
41921308Sache	sc = disk->d_softc;
42021308Sache	if (sc->sc_hint == disk)
42121308Sache		sc->sc_hint = NULL;
42247558Sache	switch (disk->d_state) {
42347558Sache	case G_MIRROR_DISK_STATE_SYNCHRONIZING:
42421308Sache		g_mirror_sync_stop(disk, 1);
42521308Sache		/* FALLTHROUGH */
42621308Sache	case G_MIRROR_DISK_STATE_NEW:
42721308Sache	case G_MIRROR_DISK_STATE_STALE:
42821308Sache	case G_MIRROR_DISK_STATE_ACTIVE:
42921308Sache		g_mirror_disconnect_consumer(sc, disk->d_consumer);
43021308Sache		free(disk, M_MIRROR);
43121308Sache		break;
43221308Sache	default:
43321308Sache		KASSERT(0 == 1, ("Wrong disk state (%s, %s).",
43421308Sache		    g_mirror_get_diskname(disk),
43521308Sache		    g_mirror_disk_state2str(disk->d_state)));
43621308Sache	}
43721308Sache}
43821308Sache
43921308Sachestatic void
44021308Sacheg_mirror_destroy_device(struct g_mirror_softc *sc)
44121308Sache{
44221308Sache	struct g_mirror_disk *disk;
44321308Sache	struct g_mirror_event *ep;
44421308Sache	struct g_geom *gp;
44521308Sache	struct g_consumer *cp, *tmpcp;
44621308Sache
44721308Sache	g_topology_assert();
44821308Sache
44921308Sache	gp = sc->sc_geom;
45021308Sache	if (sc->sc_provider != NULL)
45121308Sache		g_mirror_destroy_provider(sc);
45247558Sache	for (disk = LIST_FIRST(&sc->sc_disks); disk != NULL;
45347558Sache	    disk = LIST_FIRST(&sc->sc_disks)) {
45421308Sache		g_mirror_destroy_disk(disk);
45521308Sache	}
45621308Sache	while ((ep = g_mirror_event_get(sc)) != NULL) {
45721308Sache		if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0)
458119610Sache			g_mirror_event_free(ep);
45921308Sache		else {
460119610Sache			ep->e_error = ECANCELED;
46121308Sache			ep->e_flags |= G_MIRROR_EVENT_DONE;
46221308Sache			G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, ep);
46321308Sache			mtx_lock(&sc->sc_events_mtx);
46421308Sache			wakeup(ep);
465119610Sache			mtx_unlock(&sc->sc_events_mtx);
46621308Sache		}
46721308Sache	}
46821308Sache	callout_drain(&sc->sc_callout);
46921308Sache	gp->softc = NULL;
47021308Sache
47121308Sache	LIST_FOREACH_SAFE(cp, &sc->sc_sync.ds_geom->consumer, consumer, tmpcp) {
47221308Sache		g_mirror_disconnect_consumer(sc, cp);
47321308Sache	}
47421308Sache	sc->sc_sync.ds_geom->softc = NULL;
47521308Sache	g_wither_geom(sc->sc_sync.ds_geom, ENXIO);
47621308Sache	mtx_destroy(&sc->sc_queue_mtx);
47721308Sache	mtx_destroy(&sc->sc_events_mtx);
47847558Sache	G_MIRROR_DEBUG(0, "Device %s destroyed.", gp->name);
47947558Sache	g_wither_geom(gp, ENXIO);
48021308Sache}
48121308Sache
48221308Sachestatic void
48321308Sacheg_mirror_orphan(struct g_consumer *cp)
48421308Sache{
48521308Sache	struct g_mirror_disk *disk;
48621308Sache
48721308Sache	g_topology_assert();
48821308Sache
48921308Sache	disk = cp->private;
49021308Sache	if (disk == NULL)
49121308Sache		return;
49221308Sache	disk->d_softc->sc_bump_syncid = G_MIRROR_BUMP_ON_FIRST_WRITE;
49321308Sache	g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
49421308Sache	    G_MIRROR_EVENT_DONTWAIT);
495119610Sache}
496119610Sache
497119610Sachestatic void
49821308Sacheg_mirror_spoiled(struct g_consumer *cp)
49921308Sache{
50021308Sache	struct g_mirror_disk *disk;
50121308Sache
50221308Sache	g_topology_assert();
50321308Sache
50421308Sache	disk = cp->private;
505119610Sache	if (disk == NULL)
506119610Sache		return;
50721308Sache	disk->d_softc->sc_bump_syncid = G_MIRROR_BUMP_IMMEDIATELY;
508119610Sache	g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
50921308Sache	    G_MIRROR_EVENT_DONTWAIT);
51021308Sache}
51121308Sache
51221308Sache/*
51321308Sache * Function should return the next active disk on the list.
51421308Sache * It is possible that it will be the same disk as given.
51521308Sache * If there are no active disks on list, NULL is returned.
51621308Sache */
51747558Sachestatic __inline struct g_mirror_disk *
51847558Sacheg_mirror_find_next(struct g_mirror_softc *sc, struct g_mirror_disk *disk)
51921308Sache{
52021308Sache	struct g_mirror_disk *dp;
52121308Sache
52221308Sache	for (dp = LIST_NEXT(disk, d_next); dp != disk;
52321308Sache	    dp = LIST_NEXT(dp, d_next)) {
52421308Sache		if (dp == NULL)
52521308Sache			dp = LIST_FIRST(&sc->sc_disks);
52621308Sache		if (dp->d_state == G_MIRROR_DISK_STATE_ACTIVE)
52721308Sache			break;
52821308Sache	}
52921308Sache	if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
530119610Sache		return (NULL);
531119610Sache	return (dp);
53221308Sache}
533119610Sache
53421308Sachestatic struct g_mirror_disk *
53521308Sacheg_mirror_get_disk(struct g_mirror_softc *sc)
53621308Sache{
53721308Sache	struct g_mirror_disk *disk;
53821308Sache
53921308Sache	if (sc->sc_hint == NULL) {
54021308Sache		sc->sc_hint = LIST_FIRST(&sc->sc_disks);
54121308Sache		if (sc->sc_hint == NULL)
54221308Sache			return (NULL);
54321308Sache	}
54421308Sache	disk = sc->sc_hint;
54521308Sache	if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE) {
54621308Sache		disk = g_mirror_find_next(sc, disk);
54721308Sache		if (disk == NULL)
54821308Sache			return (NULL);
54921308Sache	}
55021308Sache	sc->sc_hint = g_mirror_find_next(sc, disk);
55121308Sache	return (disk);
55221308Sache}
55321308Sache
55421308Sachestatic int
555119610Sacheg_mirror_write_metadata(struct g_mirror_disk *disk,
556119610Sache    struct g_mirror_metadata *md)
557119610Sache{
558119610Sache	struct g_mirror_softc *sc;
559119610Sache	struct g_consumer *cp;
560119610Sache	off_t offset, length;
561119610Sache	u_char *sector;
562119610Sache	int close = 0, error = 0;
563119610Sache
564119610Sache	g_topology_assert();
565119610Sache
56621308Sache	sc = disk->d_softc;
56721308Sache	cp = disk->d_consumer;
56821308Sache	KASSERT(cp != NULL, ("NULL consumer (%s).", sc->sc_name));
56921308Sache	KASSERT(cp->provider != NULL, ("NULL provider (%s).", sc->sc_name));
57021308Sache	length = cp->provider->sectorsize;
57121308Sache	offset = cp->provider->mediasize - length;
57221308Sache	sector = malloc((size_t)length, M_MIRROR, M_WAITOK | M_ZERO);
57321308Sache	/*
57421308Sache	 * Open consumer if it wasn't opened and remember to close it.
57521308Sache	 */
57621308Sache	if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) == 0) {
57721308Sache		error = g_access(cp, 0, 1, 1);
57821308Sache		G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d",
57921308Sache		    cp->provider->name, 0, 1, 1, error);
58021308Sache		if (error == 0)
58121308Sache			close = 1;
58221308Sache#ifdef	INVARIANTS
58321308Sache	} else {
58421308Sache		KASSERT(cp->acw > 0 && cp->ace > 0,
58521308Sache		    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
58621308Sache		    cp->acr, cp->acw, cp->ace));
58721308Sache#endif
58821308Sache	}
58921308Sache	if (error == 0) {
59021308Sache		if (md != NULL)
59121308Sache			mirror_metadata_encode(md, sector);
59221308Sache		g_topology_unlock();
59321308Sache		error = g_write_data(cp, offset, sector, length);
59421308Sache		g_topology_lock();
59521308Sache	}
59621308Sache	free(sector, M_MIRROR);
59721308Sache	if (close) {
59821308Sache		g_access(cp, 0, -1, -1);
59921308Sache		G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d",
60021308Sache		    cp->provider->name, 0, -1, -1, 0);
60121308Sache	}
60221308Sache	if (error != 0) {
60321308Sache		disk->d_softc->sc_bump_syncid = G_MIRROR_BUMP_IMMEDIATELY;
60421308Sache		g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
60521308Sache		    G_MIRROR_EVENT_DONTWAIT);
60635486Sache	}
60735486Sache	return (error);
60835486Sache}
60935486Sache
61035486Sachestatic int
61135486Sacheg_mirror_clear_metadata(struct g_mirror_disk *disk)
61235486Sache{
61321308Sache	int error;
61421308Sache
61521308Sache	g_topology_assert();
61621308Sache	error = g_mirror_write_metadata(disk, NULL);
61721308Sache	if (error == 0) {
61821308Sache		G_MIRROR_DEBUG(2, "Metadata on %s cleared.",
619119610Sache		    g_mirror_get_diskname(disk));
62021308Sache	} else {
62121308Sache		G_MIRROR_DEBUG(0,
62221308Sache		    "Cannot clear metadata on disk %s (error=%d).",
62321308Sache		    g_mirror_get_diskname(disk), error);
62421308Sache	}
62521308Sache	return (error);
62621308Sache}
62721308Sache
62821308Sachevoid
62921308Sacheg_mirror_fill_metadata(struct g_mirror_softc *sc, struct g_mirror_disk *disk,
630119610Sache    struct g_mirror_metadata *md)
63121308Sache{
63221308Sache
63321308Sache	strlcpy(md->md_magic, G_MIRROR_MAGIC, sizeof(md->md_magic));
63421308Sache	md->md_version = G_MIRROR_VERSION;
63521308Sache	strlcpy(md->md_name, sc->sc_name, sizeof(md->md_name));
63621308Sache	md->md_mid = sc->sc_id;
63721308Sache	md->md_all = sc->sc_ndisks;
63821308Sache	md->md_slice = sc->sc_slice;
63921308Sache	md->md_balance = sc->sc_balance;
64021308Sache	md->md_mediasize = sc->sc_mediasize;
64121308Sache	md->md_sectorsize = sc->sc_sectorsize;
64221308Sache	md->md_mflags = (sc->sc_flags & G_MIRROR_DEVICE_FLAG_MASK);
64321308Sache	bzero(md->md_provider, sizeof(md->md_provider));
64421308Sache	if (disk == NULL) {
64521308Sache		md->md_did = arc4random();
64621308Sache		md->md_priority = 0;
64721308Sache		md->md_syncid = 0;
64821308Sache		md->md_dflags = 0;
64921308Sache		md->md_sync_offset = 0;
65021308Sache	} else {
65121308Sache		md->md_did = disk->d_id;
65221308Sache		md->md_priority = disk->d_priority;
65321308Sache		md->md_syncid = disk->d_sync.ds_syncid;
65421308Sache		md->md_dflags = (disk->d_flags & G_MIRROR_DISK_FLAG_MASK);
65521308Sache		if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
65621308Sache			md->md_sync_offset = disk->d_sync.ds_offset_done;
65721308Sache		else
65821308Sache			md->md_sync_offset = 0;
659119610Sache		if ((disk->d_flags & G_MIRROR_DISK_FLAG_HARDCODED) != 0) {
66021308Sache			strlcpy(md->md_provider,
66121308Sache			    disk->d_consumer->provider->name,
66221308Sache			    sizeof(md->md_provider));
66321308Sache		}
66421308Sache	}
66521308Sache}
66621308Sache
66721308Sachevoid
66821308Sacheg_mirror_update_metadata(struct g_mirror_disk *disk)
66921308Sache{
67021308Sache	struct g_mirror_metadata md;
67121308Sache	int error;
67221308Sache
67321308Sache	g_topology_assert();
67421308Sache	g_mirror_fill_metadata(disk->d_softc, disk, &md);
67521308Sache	error = g_mirror_write_metadata(disk, &md);
676119610Sache	if (error == 0) {
677119610Sache		G_MIRROR_DEBUG(2, "Metadata on %s updated.",
678119610Sache		    g_mirror_get_diskname(disk));
679119610Sache	} else {
680119610Sache		G_MIRROR_DEBUG(0,
681119610Sache		    "Cannot update metadata on disk %s (error=%d).",
682119610Sache		    g_mirror_get_diskname(disk), error);
683119610Sache	}
684119610Sache}
685119610Sache
686119610Sachestatic void
687119610Sacheg_mirror_bump_syncid(struct g_mirror_softc *sc)
688119610Sache{
689119610Sache	struct g_mirror_disk *disk;
690119610Sache
691119610Sache	g_topology_assert();
692119610Sache	KASSERT(g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE) > 0,
693119610Sache	    ("%s called with no active disks (device=%s).", __func__,
694119610Sache	    sc->sc_name));
695119610Sache
696119610Sache	sc->sc_syncid++;
697119610Sache	G_MIRROR_DEBUG(1, "Device %s: syncid bumped to %u.", sc->sc_name,
698119610Sache	    sc->sc_syncid);
699119610Sache	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
700119610Sache		if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
701119610Sache		    disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
702119610Sache			disk->d_sync.ds_syncid = sc->sc_syncid;
703119610Sache			g_mirror_update_metadata(disk);
704119610Sache		}
705119610Sache	}
706119610Sache}
707119610Sache
708119610Sachestatic __inline int
709119610Sachebintime_cmp(struct bintime *bt1, struct bintime *bt2)
710119610Sache{
711119610Sache
712119610Sache	if (bt1->sec < bt2->sec)
713119610Sache		return (-1);
714119610Sache	else if (bt1->sec > bt2->sec)
715119610Sache		return (1);
716119610Sache	if (bt1->frac < bt2->frac)
717119610Sache		return (-1);
718119610Sache	else if (bt1->frac > bt2->frac)
719119610Sache		return (1);
720119610Sache	return (0);
72121308Sache}
72221308Sache
72321308Sachestatic void
72421308Sacheg_mirror_update_delay(struct g_mirror_disk *disk, struct bio *bp)
72521308Sache{
72621308Sache
72721308Sache	if (disk->d_softc->sc_balance != G_MIRROR_BALANCE_LOAD)
72821308Sache		return;
72921308Sache	binuptime(&disk->d_delay);
73021308Sache	bintime_sub(&disk->d_delay, &bp->bio_t0);
731119610Sache}
732119610Sache
733119610Sachestatic void
734119610Sacheg_mirror_done(struct bio *bp)
735119610Sache{
73621308Sache	struct g_mirror_softc *sc;
73721308Sache
73821308Sache	sc = bp->bio_from->geom->softc;
73921308Sache	bp->bio_cflags |= G_MIRROR_BIO_FLAG_REGULAR;
74021308Sache	mtx_lock(&sc->sc_queue_mtx);
74121308Sache	bioq_disksort(&sc->sc_queue, bp);
74221308Sache	wakeup(sc);
74321308Sache	mtx_unlock(&sc->sc_queue_mtx);
74421308Sache}
745119610Sache
74621308Sachestatic void
74721308Sacheg_mirror_regular_request(struct bio *bp)
74821308Sache{
74921308Sache	struct g_mirror_softc *sc;
75021308Sache	struct g_mirror_disk *disk;
75121308Sache	struct bio *pbp;
75221308Sache
75321308Sache	g_topology_assert_not();
754119610Sache
75521308Sache	pbp = bp->bio_parent;
75621308Sache	sc = pbp->bio_to->geom->softc;
75721308Sache	disk = bp->bio_from->private;
75821308Sache	if (disk == NULL) {
759119610Sache		g_topology_lock();
76021308Sache		g_mirror_kill_consumer(sc, bp->bio_from);
76121308Sache		g_topology_unlock();
76221308Sache	} else {
76321308Sache		g_mirror_update_delay(disk, bp);
76421308Sache	}
76521308Sache
76621308Sache	pbp->bio_inbed++;
76721308Sache	KASSERT(pbp->bio_inbed <= pbp->bio_children,
76821308Sache	    ("bio_inbed (%u) is bigger than bio_children (%u).", pbp->bio_inbed,
769119610Sache	    pbp->bio_children));
77021308Sache	if (bp->bio_error == 0 && pbp->bio_error == 0) {
77147558Sache		G_MIRROR_LOGREQ(3, bp, "Request delivered.");
772119610Sache		g_destroy_bio(bp);
77321308Sache		if (pbp->bio_children == pbp->bio_inbed) {
77421308Sache			G_MIRROR_LOGREQ(3, pbp, "Request delivered.");
77521308Sache			pbp->bio_completed = pbp->bio_length;
77621308Sache			g_io_deliver(pbp, pbp->bio_error);
77721308Sache		}
77821308Sache		return;
77921308Sache	} else if (bp->bio_error != 0) {
780119610Sache		if (pbp->bio_error == 0)
781119610Sache			pbp->bio_error = bp->bio_error;
782119610Sache		G_MIRROR_LOGREQ(0, bp, "Request failed (error=%d).",
783119610Sache		    bp->bio_error);
784119610Sache		if (disk != NULL) {
785119610Sache			sc->sc_bump_syncid = G_MIRROR_BUMP_IMMEDIATELY;
78621308Sache			g_mirror_event_send(disk,
78721308Sache			    G_MIRROR_DISK_STATE_DISCONNECTED,
78821308Sache			    G_MIRROR_EVENT_DONTWAIT);
78921308Sache		}
79021308Sache		switch (pbp->bio_cmd) {
79121308Sache		case BIO_DELETE:
79221308Sache		case BIO_WRITE:
79321308Sache			pbp->bio_inbed--;
79421308Sache			pbp->bio_children--;
79521308Sache			break;
79621308Sache		}
79721308Sache	}
79821308Sache	g_destroy_bio(bp);
79921308Sache
80021308Sache	switch (pbp->bio_cmd) {
80121308Sache	case BIO_READ:
80221308Sache		if (pbp->bio_children == pbp->bio_inbed) {
80321308Sache			pbp->bio_error = 0;
80421308Sache			mtx_lock(&sc->sc_queue_mtx);
80521308Sache			bioq_disksort(&sc->sc_queue, pbp);
80621308Sache			G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
80721308Sache			wakeup(sc);
80875406Sache			mtx_unlock(&sc->sc_queue_mtx);
80921308Sache		}
81075406Sache		break;
81121308Sache	case BIO_DELETE:
81221308Sache	case BIO_WRITE:
81321308Sache		if (pbp->bio_children == 0) {
81421308Sache			/*
81521308Sache			 * All requests failed.
81621308Sache			 */
81721308Sache		} else if (pbp->bio_inbed < pbp->bio_children) {
81821308Sache			/* Do nothing. */
81921308Sache			break;
82021308Sache		} else if (pbp->bio_children == pbp->bio_inbed) {
82175406Sache			/* Some requests succeeded. */
82221308Sache			pbp->bio_error = 0;
82375406Sache			pbp->bio_completed = pbp->bio_length;
82421308Sache		}
82521308Sache		g_io_deliver(pbp, pbp->bio_error);
82621308Sache		break;
82721308Sache	default:
82821308Sache		KASSERT(1 == 0, ("Invalid request: %u.", pbp->bio_cmd));
82921308Sache		break;
83021308Sache	}
83121308Sache}
83221308Sache
83321308Sachestatic void
83421308Sacheg_mirror_sync_done(struct bio *bp)
83521308Sache{
83621308Sache	struct g_mirror_softc *sc;
83721308Sache
83821308Sache	G_MIRROR_LOGREQ(3, bp, "Synchronization request delivered.");
83921308Sache	sc = bp->bio_from->geom->softc;
84021308Sache	bp->bio_cflags |= G_MIRROR_BIO_FLAG_SYNC;
84121308Sache	mtx_lock(&sc->sc_queue_mtx);
84221308Sache	bioq_disksort(&sc->sc_queue, bp);
84321308Sache	wakeup(sc);
84421308Sache	mtx_unlock(&sc->sc_queue_mtx);
84521308Sache}
84621308Sache
84721308Sachestatic void
84821308Sacheg_mirror_start(struct bio *bp)
84921308Sache{
85021308Sache	struct g_mirror_softc *sc;
85121308Sache
85221308Sache	sc = bp->bio_to->geom->softc;
85321308Sache	/*
85421308Sache	 * If sc == NULL or there are no valid disks, provider's error
85521308Sache	 * should be set and g_mirror_start() should not be called at all.
85621308Sache	 */
85721308Sache	KASSERT(sc != NULL && sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
85821308Sache	    ("Provider's error should be set (error=%d)(mirror=%s).",
85921308Sache	    bp->bio_to->error, bp->bio_to->name));
86021308Sache	G_MIRROR_LOGREQ(3, bp, "Request received.");
86121308Sache
86221308Sache	switch (bp->bio_cmd) {
86321308Sache	case BIO_READ:
86421308Sache	case BIO_WRITE:
86521308Sache	case BIO_DELETE:
86621308Sache		break;
86721308Sache	case BIO_GETATTR:
86821308Sache	default:
86921308Sache		g_io_deliver(bp, EOPNOTSUPP);
87021308Sache		return;
87121308Sache	}
87221308Sache	mtx_lock(&sc->sc_queue_mtx);
87321308Sache	bioq_disksort(&sc->sc_queue, bp);
87421308Sache	G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
87521308Sache	wakeup(sc);
87621308Sache	mtx_unlock(&sc->sc_queue_mtx);
87721308Sache}
87821308Sache
87921308Sache/*
88021308Sache * Send one synchronization request.
88121308Sache */
88221308Sachestatic void
88321308Sacheg_mirror_sync_one(struct g_mirror_disk *disk)
88421308Sache{
88521308Sache	struct g_mirror_softc *sc;
88621308Sache	struct bio *bp;
887119610Sache
88821308Sache	sc = disk->d_softc;
88921308Sache	KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
89021308Sache	    ("Disk %s is not marked for synchronization.",
89121308Sache	    g_mirror_get_diskname(disk)));
89221308Sache
89375406Sache	bp = g_new_bio();
89475406Sache	if (bp == NULL)
89521308Sache		return;
89621308Sache	bp->bio_parent = NULL;
89721308Sache	bp->bio_cmd = BIO_READ;
89821308Sache	bp->bio_offset = disk->d_sync.ds_offset;
89921308Sache	bp->bio_length = MIN(MAXPHYS, sc->sc_mediasize - bp->bio_offset);
90075406Sache	bp->bio_cflags = 0;
90121308Sache	bp->bio_done = g_mirror_sync_done;
90221308Sache	bp->bio_data = disk->d_sync.ds_data;
90375406Sache	if (bp->bio_data == NULL) {
90475406Sache		g_destroy_bio(bp);
90575406Sache		return;
90675406Sache	}
90775406Sache	disk->d_sync.ds_offset += bp->bio_length;
90875406Sache	bp->bio_to = sc->sc_provider;
90975406Sache	G_MIRROR_LOGREQ(3, bp, "Sending synchronization request.");
91075406Sache	g_io_request(bp, disk->d_sync.ds_consumer);
911119610Sache}
91275406Sache
91321308Sachestatic void
91475406Sacheg_mirror_sync_request(struct bio *bp)
91521308Sache{
916119610Sache	struct g_mirror_softc *sc;
91721308Sache	struct g_mirror_disk *disk;
91821308Sache
91921308Sache	sc = bp->bio_from->geom->softc;
92021308Sache	disk = bp->bio_from->private;
92121308Sache	if (disk == NULL) {
92221308Sache		g_topology_lock();
92321308Sache		g_mirror_kill_consumer(sc, bp->bio_from);
92421308Sache		g_topology_unlock();
92521308Sache		g_destroy_bio(bp);
92621308Sache		return;
92721308Sache	}
92821308Sache
92921308Sache	/*
93021308Sache	 * Synchronization request.
93121308Sache	 */
93221308Sache	switch (bp->bio_cmd) {
93321308Sache	case BIO_READ:
93421308Sache	    {
93521308Sache		struct g_consumer *cp;
93621308Sache
93721308Sache		if (bp->bio_error != 0) {
93821308Sache			G_MIRROR_LOGREQ(0, bp,
93975406Sache			    "Synchronization request failed (error=%d).",
94075406Sache			    bp->bio_error);
94121308Sache			g_destroy_bio(bp);
94221308Sache			return;
94321308Sache		}
94421308Sache		bp->bio_cmd = BIO_WRITE;
94521308Sache		bp->bio_cflags = 0;
94621308Sache		G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
94721308Sache		cp = disk->d_consumer;
94821308Sache		KASSERT(cp->acr == 0 && cp->acw == 1 && cp->ace == 1,
94921308Sache		    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
95021308Sache		    cp->acr, cp->acw, cp->ace));
95121308Sache		g_io_request(bp, cp);
95221308Sache		return;
95321308Sache	    }
95421308Sache	case BIO_WRITE:
95521308Sache	    {
95621308Sache		struct g_mirror_disk_sync *sync;
95775406Sache
95821308Sache		if (bp->bio_error != 0) {
95921308Sache			G_MIRROR_LOGREQ(0, bp,
96021308Sache			    "Synchronization request failed (error=%d).",
96121308Sache			    bp->bio_error);
96221308Sache			g_destroy_bio(bp);
96321308Sache			sc->sc_bump_syncid = G_MIRROR_BUMP_IMMEDIATELY;
96421308Sache			g_mirror_event_send(disk,
96521308Sache			    G_MIRROR_DISK_STATE_DISCONNECTED,
96621308Sache			    G_MIRROR_EVENT_DONTWAIT);
96721308Sache			return;
96821308Sache		}
96921308Sache		G_MIRROR_LOGREQ(3, bp, "Synchronization request finished.");
97021308Sache		sync = &disk->d_sync;
97121308Sache		sync->ds_offset_done = bp->bio_offset + bp->bio_length;
97221308Sache		g_destroy_bio(bp);
97321308Sache		if (sync->ds_resync != -1)
97421308Sache			break;
97521308Sache		if (sync->ds_offset_done == sc->sc_provider->mediasize) {
97621308Sache			/*
97721308Sache			 * Disk up-to-date, activate it.
97821308Sache			 */
97921308Sache			g_mirror_event_send(disk, G_MIRROR_DISK_STATE_ACTIVE,
98021308Sache			    G_MIRROR_EVENT_DONTWAIT);
98121308Sache			return;
98221308Sache		} else if (sync->ds_offset_done % (MAXPHYS * 100) == 0) {
98321308Sache			/*
98421308Sache			 * Update offset_done on every 100 blocks.
98575406Sache			 * XXX: This should be configurable.
98621308Sache			 */
98721308Sache			g_topology_lock();
98821308Sache			g_mirror_update_metadata(disk);
98921308Sache			g_topology_unlock();
99021308Sache		}
99121308Sache		return;
99221308Sache	    }
99321308Sache	default:
99421308Sache		KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
99521308Sache		    bp->bio_cmd, sc->sc_name));
99621308Sache		break;
99721308Sache	}
99821308Sache}
99921308Sache
100021308Sachestatic void
100121308Sacheg_mirror_request_prefer(struct g_mirror_softc *sc, struct bio *bp)
100221308Sache{
100321308Sache	struct g_mirror_disk *disk;
100421308Sache	struct g_consumer *cp;
100521308Sache	struct bio *cbp;
100621308Sache
100721308Sache	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
100821308Sache		if (disk->d_state == G_MIRROR_DISK_STATE_ACTIVE)
100921308Sache			break;
101021308Sache	}
101121308Sache	if (disk == NULL) {
101221308Sache		if (bp->bio_error == 0)
101321308Sache			bp->bio_error = ENXIO;
101421308Sache		g_io_deliver(bp, bp->bio_error);
101521308Sache		return;
101621308Sache	}
101721308Sache	cbp = g_clone_bio(bp);
101821308Sache	if (cbp == NULL) {
101921308Sache		if (bp->bio_error == 0)
102021308Sache			bp->bio_error = ENOMEM;
102121308Sache		g_io_deliver(bp, bp->bio_error);
102221308Sache		return;
102321308Sache	}
102421308Sache	/*
102521308Sache	 * Fill in the component buf structure.
102621308Sache	 */
102721308Sache	cp = disk->d_consumer;
102821308Sache	cbp->bio_done = g_mirror_done;
102921308Sache	cbp->bio_to = cp->provider;
103021308Sache	G_MIRROR_LOGREQ(3, cbp, "Sending request.");
103121308Sache	KASSERT(cp->acr > 0 && cp->ace > 0,
103221308Sache	    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
103321308Sache	    cp->acw, cp->ace));
103421308Sache	g_io_request(cbp, cp);
103575406Sache}
103621308Sache
103721308Sachestatic void
103821308Sacheg_mirror_request_round_robin(struct g_mirror_softc *sc, struct bio *bp)
103921308Sache{
104021308Sache	struct g_mirror_disk *disk;
104121308Sache	struct g_consumer *cp;
104221308Sache	struct bio *cbp;
104321308Sache
104421308Sache	disk = g_mirror_get_disk(sc);
104521308Sache	if (disk == NULL) {
104621308Sache		if (bp->bio_error == 0)
104721308Sache			bp->bio_error = ENXIO;
104821308Sache		g_io_deliver(bp, bp->bio_error);
104921308Sache		return;
105021308Sache	}
105121308Sache	cbp = g_clone_bio(bp);
105221308Sache	if (cbp == NULL) {
105321308Sache		if (bp->bio_error == 0)
105421308Sache			bp->bio_error = ENOMEM;
105521308Sache		g_io_deliver(bp, bp->bio_error);
105621308Sache		return;
105721308Sache	}
105821308Sache	/*
105921308Sache	 * Fill in the component buf structure.
106021308Sache	 */
106175406Sache	cp = disk->d_consumer;
106221308Sache	cbp->bio_done = g_mirror_done;
106321308Sache	cbp->bio_to = cp->provider;
106421308Sache	G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1065119610Sache	KASSERT(cp->acr > 0 && cp->ace > 0,
1066119610Sache	    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1067119610Sache	    cp->acw, cp->ace));
1068119610Sache	g_io_request(cbp, cp);
106921308Sache}
107021308Sache
107121308Sachestatic void
107221308Sacheg_mirror_request_load(struct g_mirror_softc *sc, struct bio *bp)
107321308Sache{
107421308Sache	struct g_mirror_disk *disk, *dp;
107521308Sache	struct g_consumer *cp;
1076119610Sache	struct bio *cbp;
107721308Sache	struct bintime curtime;
107821308Sache
107921308Sache	binuptime(&curtime);
108021308Sache	/*
108121308Sache	 * Find a disk which the smallest load.
108221308Sache	 */
108321308Sache	disk = NULL;
108421308Sache	LIST_FOREACH(dp, &sc->sc_disks, d_next) {
108521308Sache		if (dp->d_state != G_MIRROR_DISK_STATE_ACTIVE)
108621308Sache			continue;
108721308Sache		/* If disk wasn't used for more than 2 sec, use it. */
108821308Sache		if (curtime.sec - dp->d_last_used.sec >= 2) {
108921308Sache			disk = dp;
109021308Sache			break;
109121308Sache		}
109221308Sache		if (disk == NULL ||
109321308Sache		    bintime_cmp(&dp->d_delay, &disk->d_delay) < 0) {
109421308Sache			disk = dp;
109521308Sache		}
109621308Sache	}
109721308Sache	cbp = g_clone_bio(bp);
109821308Sache	if (cbp == NULL) {
109921308Sache		if (bp->bio_error == 0)
110021308Sache			bp->bio_error = ENOMEM;
1101119610Sache		g_io_deliver(bp, bp->bio_error);
1102119610Sache		return;
1103119610Sache	}
1104119610Sache	/*
110521308Sache	 * Fill in the component buf structure.
1106119610Sache	 */
110721308Sache	cp = disk->d_consumer;
110821308Sache	cbp->bio_done = g_mirror_done;
110921308Sache	cbp->bio_to = cp->provider;
111021308Sache	binuptime(&disk->d_last_used);
111121308Sache	G_MIRROR_LOGREQ(3, cbp, "Sending request.");
111221308Sache	KASSERT(cp->acr > 0 && cp->ace > 0,
111321308Sache	    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name, cp->acr,
1114119610Sache	    cp->acw, cp->ace));
1115119610Sache	g_io_request(cbp, cp);
1116119610Sache}
111721308Sache
1118119610Sachestatic void
111921308Sacheg_mirror_request_split(struct g_mirror_softc *sc, struct bio *bp)
112075406Sache{
1121119610Sache	struct bio_queue_head queue;
1122119610Sache	struct g_mirror_disk *disk;
1123119610Sache	struct g_consumer *cp;
1124119610Sache	struct bio *cbp;
112575406Sache	off_t left, mod, offset, slice;
112675406Sache	u_char *data;
112775406Sache	u_int ndisks;
1128119610Sache
112975406Sache	if (bp->bio_length <= sc->sc_slice) {
113021308Sache		g_mirror_request_round_robin(sc, bp);
113121308Sache		return;
113221308Sache	}
113321308Sache	ndisks = g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE);
113421308Sache	slice = bp->bio_length / ndisks;
113521308Sache	mod = slice % sc->sc_provider->sectorsize;
113621308Sache	if (mod != 0)
113721308Sache		slice += sc->sc_provider->sectorsize - mod;
113821308Sache	/*
113921308Sache	 * Allocate all bios before sending any request, so we can
114021308Sache	 * return ENOMEM in nice and clean way.
114121308Sache	 */
114221308Sache	left = bp->bio_length;
114321308Sache	offset = bp->bio_offset;
114421308Sache	data = bp->bio_data;
114521308Sache	bioq_init(&queue);
114621308Sache	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
114721308Sache		if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
114821308Sache			continue;
114921308Sache		cbp = g_clone_bio(bp);
115021308Sache		if (cbp == NULL) {
1151119610Sache			for (cbp = bioq_first(&queue); cbp != NULL;
1152119610Sache			    cbp = bioq_first(&queue)) {
1153119610Sache				bioq_remove(&queue, cbp);
115421308Sache				g_destroy_bio(cbp);
1155119610Sache			}
115621308Sache			if (bp->bio_error == 0)
115721308Sache				bp->bio_error = ENOMEM;
115821308Sache			g_io_deliver(bp, bp->bio_error);
115921308Sache			return;
116021308Sache		}
116121308Sache		bioq_insert_tail(&queue, cbp);
116221308Sache		cbp->bio_done = g_mirror_done;
1163119610Sache		cbp->bio_caller1 = disk;
116421308Sache		cbp->bio_to = disk->d_consumer->provider;
116521308Sache		cbp->bio_offset = offset;
116621308Sache		cbp->bio_data = data;
116721308Sache		cbp->bio_length = MIN(left, slice);
1168119610Sache		left -= cbp->bio_length;
1169119610Sache		if (left == 0)
1170119610Sache			break;
1171119610Sache		offset += cbp->bio_length;
1172119610Sache		data += cbp->bio_length;
1173119610Sache	}
1174119610Sache	for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
1175119610Sache		bioq_remove(&queue, cbp);
1176119610Sache		G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1177119610Sache		disk = cbp->bio_caller1;
1178119610Sache		cbp->bio_caller1 = NULL;
1179119610Sache		cp = disk->d_consumer;
1180119610Sache		KASSERT(cp->acr > 0 && cp->ace > 0,
1181119610Sache		    ("Consumer %s not opened (r%dw%de%d).", cp->provider->name,
118221308Sache		    cp->acr, cp->acw, cp->ace));
118321308Sache		g_io_request(cbp, disk->d_consumer);
118421308Sache	}
118521308Sache}
118675406Sache
118721308Sachestatic void
118821308Sacheg_mirror_register_request(struct bio *bp)
118921308Sache{
119021308Sache	struct g_mirror_softc *sc;
119121308Sache
119221308Sache	sc = bp->bio_to->geom->softc;
119321308Sache	switch (bp->bio_cmd) {
119421308Sache	case BIO_READ:
119521308Sache		switch (sc->sc_balance) {
119621308Sache		case G_MIRROR_BALANCE_LOAD:
1197119610Sache			g_mirror_request_load(sc, bp);
1198119610Sache			break;
1199119610Sache		case G_MIRROR_BALANCE_PREFER:
1200119610Sache			g_mirror_request_prefer(sc, bp);
120121308Sache			break;
1202119610Sache		case G_MIRROR_BALANCE_ROUND_ROBIN:
1203119610Sache			g_mirror_request_round_robin(sc, bp);
1204119610Sache			break;
1205119610Sache		case G_MIRROR_BALANCE_SPLIT:
1206119610Sache			g_mirror_request_split(sc, bp);
1207119610Sache			break;
120821308Sache		}
120921308Sache		return;
121021308Sache	case BIO_WRITE:
121121308Sache	case BIO_DELETE:
121221308Sache	    {
121321308Sache		struct g_mirror_disk *disk;
121421308Sache		struct g_mirror_disk_sync *sync;
121521308Sache		struct bio_queue_head queue;
121675406Sache		struct g_consumer *cp;
121721308Sache		struct bio *cbp;
121821308Sache
121921308Sache		/*
122021308Sache		 * Allocate all bios before sending any request, so we can
122121308Sache		 * return ENOMEM in nice and clean way.
122221308Sache		 */
122321308Sache		bioq_init(&queue);
122421308Sache		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1225119610Sache			sync = &disk->d_sync;
1226119610Sache			switch (disk->d_state) {
1227119610Sache			case G_MIRROR_DISK_STATE_ACTIVE:
1228119610Sache				break;
1229119610Sache			case G_MIRROR_DISK_STATE_SYNCHRONIZING:
1230119610Sache				if (bp->bio_offset >= sync->ds_offset)
123121308Sache					continue;
123221308Sache				else if (bp->bio_offset + bp->bio_length >
123321308Sache				    sync->ds_offset_done &&
123421308Sache				    (bp->bio_offset < sync->ds_resync ||
123521308Sache				     sync->ds_resync == -1)) {
123621308Sache					sync->ds_resync = bp->bio_offset -
123721308Sache					    (bp->bio_offset % MAXPHYS);
123821308Sache				}
123921308Sache				break;
124075406Sache			default:
124121308Sache				continue;
124221308Sache			}
124321308Sache			cbp = g_clone_bio(bp);
124421308Sache			if (cbp == NULL) {
124521308Sache				for (cbp = bioq_first(&queue); cbp != NULL;
124621308Sache				    cbp = bioq_first(&queue)) {
124721308Sache					bioq_remove(&queue, cbp);
124821308Sache					g_destroy_bio(cbp);
124921308Sache				}
125021308Sache				if (bp->bio_error == 0)
125121308Sache					bp->bio_error = ENOMEM;
125221308Sache				g_io_deliver(bp, bp->bio_error);
125321308Sache				return;
125421308Sache			}
125521308Sache			bioq_insert_tail(&queue, cbp);
125621308Sache			cbp->bio_done = g_mirror_done;
125721308Sache			cp = disk->d_consumer;
125821308Sache			cbp->bio_caller1 = cp;
125921308Sache			cbp->bio_to = cp->provider;
126021308Sache			KASSERT(cp->acw > 0 && cp->ace > 0,
126121308Sache			    ("Consumer %s not opened (r%dw%de%d).",
126221308Sache			    cp->provider->name, cp->acr, cp->acw, cp->ace));
126321308Sache		}
126421308Sache		for (cbp = bioq_first(&queue); cbp != NULL;
1265119610Sache		    cbp = bioq_first(&queue)) {
1266119610Sache			bioq_remove(&queue, cbp);
1267119610Sache			G_MIRROR_LOGREQ(3, cbp, "Sending request.");
1268119610Sache			cp = cbp->bio_caller1;
1269119610Sache			cbp->bio_caller1 = NULL;
127021308Sache			g_io_request(cbp, cp);
127121308Sache		}
127221308Sache		/*
127321308Sache		 * Bump syncid on first write.
127421308Sache		 */
127521308Sache		if (sc->sc_bump_syncid == G_MIRROR_BUMP_ON_FIRST_WRITE) {
127621308Sache			sc->sc_bump_syncid = 0;
127721308Sache			g_topology_lock();
127821308Sache			g_mirror_bump_syncid(sc);
127975406Sache			g_topology_unlock();
128075406Sache		}
128175406Sache		return;
128275406Sache	    }
128375406Sache	default:
128421308Sache		KASSERT(1 == 0, ("Invalid command here: %u (device=%s)",
128521308Sache		    bp->bio_cmd, sc->sc_name));
128621308Sache		break;
128721308Sache	}
128821308Sache}
128921308Sache
129021308Sachestatic int
129121308Sacheg_mirror_can_destroy(struct g_mirror_softc *sc)
129221308Sache{
1293119610Sache	struct g_geom *gp;
1294119610Sache	struct g_consumer *cp;
1295119610Sache
1296119610Sache	g_topology_assert();
1297119610Sache	gp = sc->sc_geom;
1298119610Sache	LIST_FOREACH(cp, &gp->consumer, consumer) {
1299119610Sache		if (g_mirror_is_busy(sc, cp))
1300119610Sache			return (0);
1301119610Sache	}
1302119610Sache	gp = sc->sc_sync.ds_geom;
1303119610Sache	LIST_FOREACH(cp, &gp->consumer, consumer) {
130421308Sache		if (g_mirror_is_busy(sc, cp))
1305119610Sache			return (0);
130621308Sache	}
130721308Sache	G_MIRROR_DEBUG(2, "No I/O requests for %s, it can be destroyed.",
130821308Sache	    sc->sc_name);
130921308Sache	return (1);
131021308Sache}
131121308Sache
131221308Sachestatic int
131321308Sacheg_mirror_try_destroy(struct g_mirror_softc *sc)
131421308Sache{
131521308Sache
1316119610Sache	if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_WAIT) != 0) {
1317119610Sache		g_topology_lock();
1318119610Sache		if (!g_mirror_can_destroy(sc)) {
131921308Sache			g_topology_unlock();
1320119610Sache			return (0);
132121308Sache		}
132221308Sache		g_topology_unlock();
132321308Sache		G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
132421308Sache		    &sc->sc_worker);
132521308Sache		wakeup(&sc->sc_worker);
132621308Sache		sc->sc_worker = NULL;
132721308Sache	} else {
132821308Sache		g_topology_lock();
132921308Sache		if (!g_mirror_can_destroy(sc)) {
133021308Sache			g_topology_unlock();
133121308Sache			return (0);
133221308Sache		}
1333119610Sache		g_mirror_destroy_device(sc);
133421308Sache		g_topology_unlock();
1335119610Sache		free(sc, M_MIRROR);
1336119610Sache	}
1337119610Sache	return (1);
133821308Sache}
133921308Sache
134021308Sache/*
134121308Sache * Worker thread.
134221308Sache */
134321308Sachestatic void
134421308Sacheg_mirror_worker(void *arg)
134521308Sache{
134621308Sache	struct g_mirror_softc *sc;
134721308Sache	struct g_mirror_disk *disk;
134821308Sache	struct g_mirror_disk_sync *sync;
134921308Sache	struct g_mirror_event *ep;
135021308Sache	struct bio *bp;
135121308Sache	u_int nreqs;
135275406Sache
135321308Sache	sc = arg;
135421308Sache	curthread->td_base_pri = PRIBIO;
135521308Sache
135621308Sache	nreqs = 0;
135721308Sache	for (;;) {
135821308Sache		G_MIRROR_DEBUG(5, "%s: Let's see...", __func__);
135921308Sache		/*
136021308Sache		 * First take a look at events.
1361119610Sache		 * This is important to handle events before any I/O requests.
136221308Sache		 */
136321308Sache		ep = g_mirror_event_get(sc);
136421308Sache		if (ep != NULL) {
136521308Sache			g_topology_lock();
136621308Sache			if ((ep->e_flags & G_MIRROR_EVENT_DEVICE) != 0) {
136721308Sache				/* Update only device status. */
136821308Sache				G_MIRROR_DEBUG(3,
136921308Sache				    "Running event for device %s.",
137021308Sache				    sc->sc_name);
137121308Sache				ep->e_error = 0;
137221308Sache				g_mirror_update_device(sc, 1);
137321308Sache			} else {
137421308Sache				/* Update disk status. */
137521308Sache				G_MIRROR_DEBUG(3, "Running event for disk %s.",
137621308Sache				     g_mirror_get_diskname(ep->e_disk));
137721308Sache				ep->e_error = g_mirror_update_disk(ep->e_disk,
137821308Sache				    ep->e_state);
137921308Sache				if (ep->e_error == 0)
138021308Sache					g_mirror_update_device(sc, 0);
138121308Sache			}
138221308Sache			g_topology_unlock();
138321308Sache			if ((ep->e_flags & G_MIRROR_EVENT_DONTWAIT) != 0) {
138421308Sache				KASSERT(ep->e_error == 0,
138521308Sache				    ("Error cannot be handled."));
138621308Sache				g_mirror_event_free(ep);
138721308Sache			} else {
138821308Sache				ep->e_flags |= G_MIRROR_EVENT_DONE;
138921308Sache				G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__,
139021308Sache				    ep);
139121308Sache				mtx_lock(&sc->sc_events_mtx);
139221308Sache				wakeup(ep);
139321308Sache				mtx_unlock(&sc->sc_events_mtx);
139421308Sache			}
139521308Sache			if ((sc->sc_flags &
139621308Sache			    G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
139721308Sache				if (g_mirror_try_destroy(sc))
139821308Sache					kthread_exit(0);
139921308Sache			}
140021308Sache			G_MIRROR_DEBUG(5, "%s: I'm here 1.", __func__);
140121308Sache			continue;
140221308Sache		}
140321308Sache		/*
140421308Sache		 * Now I/O requests.
140521308Sache		 */
140621308Sache		/* Get first request from the queue. */
140721308Sache		mtx_lock(&sc->sc_queue_mtx);
140821308Sache		bp = bioq_first(&sc->sc_queue);
140921308Sache		if (bp == NULL) {
141021308Sache			if ((sc->sc_flags &
141121308Sache			    G_MIRROR_DEVICE_FLAG_DESTROY) != 0) {
141221308Sache				mtx_unlock(&sc->sc_queue_mtx);
141321308Sache				if (g_mirror_try_destroy(sc))
141421308Sache					kthread_exit(0);
141521308Sache				mtx_lock(&sc->sc_queue_mtx);
141621308Sache			}
141721308Sache		}
141821308Sache		if (sc->sc_sync.ds_ndisks > 0 &&
141921308Sache		    (bp == NULL || nreqs > g_mirror_reqs_per_sync)) {
142021308Sache			mtx_unlock(&sc->sc_queue_mtx);
142121308Sache			/*
142275406Sache			 * It is time for synchronization...
142321308Sache			 */
142421308Sache			nreqs = 0;
142521308Sache			LIST_FOREACH(disk, &sc->sc_disks, d_next) {
142621308Sache				if (disk->d_state !=
142721308Sache				    G_MIRROR_DISK_STATE_SYNCHRONIZING) {
142821308Sache					continue;
142921308Sache				}
143021308Sache				sync = &disk->d_sync;
143121308Sache				if (sync->ds_offset >=
143221308Sache				    sc->sc_provider->mediasize) {
143321308Sache					continue;
143421308Sache				}
143521308Sache				if (sync->ds_offset > sync->ds_offset_done)
143621308Sache					continue;
143721308Sache				if (sync->ds_resync != -1) {
143821308Sache					sync->ds_offset = sync->ds_resync;
143921308Sache					sync->ds_offset_done = sync->ds_resync;
144075406Sache					sync->ds_resync = -1;
144121308Sache				}
144275406Sache				g_mirror_sync_one(disk);
144375406Sache			}
1444119610Sache			G_MIRROR_DEBUG(5, "%s: I'm here 2.", __func__);
144521308Sache			goto sleep;
144675406Sache		}
144721308Sache		if (bp == NULL) {
144821308Sache			MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w1", 0);
144921308Sache			G_MIRROR_DEBUG(5, "%s: I'm here 3.", __func__);
145021308Sache			continue;
145121308Sache		}
145221308Sache		nreqs++;
145321308Sache		bioq_remove(&sc->sc_queue, bp);
145421308Sache		mtx_unlock(&sc->sc_queue_mtx);
145521308Sache
145621308Sache		if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_REGULAR) != 0) {
145721308Sache			g_mirror_regular_request(bp);
145821308Sache		} else if ((bp->bio_cflags & G_MIRROR_BIO_FLAG_SYNC) != 0) {
145921308Sache			u_int timeout, sps;
146075406Sache
146121308Sache			g_mirror_sync_request(bp);
146275406Sachesleep:
146375406Sache			sps = atomic_load_acq_int(&g_mirror_syncs_per_sec);
146421308Sache			if (sps == 0) {
146521308Sache				G_MIRROR_DEBUG(5, "%s: I'm here 5.", __func__);
146621308Sache				continue;
146721308Sache			}
146821308Sache			mtx_lock(&sc->sc_queue_mtx);
1469119610Sache			if (bioq_first(&sc->sc_queue) != NULL) {
147021308Sache				mtx_unlock(&sc->sc_queue_mtx);
147175406Sache				G_MIRROR_DEBUG(5, "%s: I'm here 4.", __func__);
147221308Sache				continue;
147321308Sache			}
147421308Sache			timeout = hz / sps;
147521308Sache			if (timeout == 0)
147621308Sache				timeout = 1;
147721308Sache			MSLEEP(sc, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:w2",
147875406Sache			    timeout);
147921308Sache		} else {
148021308Sache			g_mirror_register_request(bp);
148121308Sache		}
148221308Sache		G_MIRROR_DEBUG(5, "%s: I'm here 6.", __func__);
148321308Sache	}
148421308Sache}
148521308Sache
1486/*
1487 * Open disk's consumer if needed.
1488 */
1489static void
1490g_mirror_update_access(struct g_mirror_disk *disk)
1491{
1492	struct g_provider *pp;
1493	struct g_consumer *cp;
1494	int acr, acw, ace, cpw, error;
1495
1496	g_topology_assert();
1497
1498	cp = disk->d_consumer;
1499	pp = disk->d_softc->sc_provider;
1500	if (pp == NULL) {
1501		acr = -cp->acr;
1502		acw = -cp->acw;
1503		ace = -cp->ace;
1504	} else {
1505		acr = pp->acr - cp->acr;
1506		acw = pp->acw - cp->acw;
1507		ace = pp->ace - cp->ace;
1508		/* Grab an extra "exclusive" bit. */
1509		if (pp->acr > 0 || pp->acw > 0 || pp->ace > 0)
1510			ace++;
1511	}
1512	if (acr == 0 && acw == 0 && ace == 0)
1513		return;
1514	cpw = cp->acw;
1515	error = g_access(cp, acr, acw, ace);
1516	G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", cp->provider->name, acr,
1517	    acw, ace, error);
1518	if (error != 0) {
1519		disk->d_softc->sc_bump_syncid = G_MIRROR_BUMP_ON_FIRST_WRITE;
1520		g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
1521		    G_MIRROR_EVENT_DONTWAIT);
1522		return;
1523	}
1524	if (cpw == 0 && cp->acw > 0) {
1525		G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as dirty.",
1526		    g_mirror_get_diskname(disk), disk->d_softc->sc_name);
1527		disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1528	} else if (cpw > 0 && cp->acw == 0) {
1529		G_MIRROR_DEBUG(1, "Disk %s (device %s) marked as clean.",
1530		    g_mirror_get_diskname(disk), disk->d_softc->sc_name);
1531		disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
1532	}
1533}
1534
1535static void
1536g_mirror_sync_start(struct g_mirror_disk *disk)
1537{
1538	struct g_mirror_softc *sc;
1539	struct g_consumer *cp;
1540	int error;
1541
1542	g_topology_assert();
1543
1544	sc = disk->d_softc;
1545	KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
1546	    ("Device not in RUNNING state (%s, %u).", sc->sc_name,
1547	    sc->sc_state));
1548	cp = disk->d_consumer;
1549	KASSERT(cp->acr == 0 && cp->acw == 0 && cp->ace == 0,
1550	    ("Consumer %s already opened.", cp->provider->name));
1551
1552	G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s.", sc->sc_name,
1553	    g_mirror_get_diskname(disk));
1554	error = g_access(cp, 0, 1, 1);
1555	G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", cp->provider->name, 0, 1,
1556	    1, error);
1557	if (error != 0) {
1558		g_mirror_event_send(disk, G_MIRROR_DISK_STATE_DISCONNECTED,
1559		    G_MIRROR_EVENT_DONTWAIT);
1560		return;
1561	}
1562	disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
1563	KASSERT(disk->d_sync.ds_consumer == NULL,
1564	    ("Sync consumer already exists (device=%s, disk=%s).",
1565	    sc->sc_name, g_mirror_get_diskname(disk)));
1566	disk->d_sync.ds_consumer = g_new_consumer(sc->sc_sync.ds_geom);
1567	disk->d_sync.ds_consumer->private = disk;
1568	error = g_attach(disk->d_sync.ds_consumer, disk->d_softc->sc_provider);
1569	KASSERT(error == 0, ("Cannot attach to %s (error=%d).",
1570	    disk->d_softc->sc_name, error));
1571	error = g_access(disk->d_sync.ds_consumer, 1, 0, 0);
1572	KASSERT(error == 0, ("Cannot open %s (error=%d).",
1573	    disk->d_softc->sc_name, error));
1574	disk->d_sync.ds_data = malloc(MAXPHYS, M_MIRROR, M_WAITOK);
1575	sc->sc_sync.ds_ndisks++;
1576}
1577
1578/*
1579 * Stop synchronization process.
1580 * type: 0 - synchronization finished
1581 *       1 - synchronization stopped
1582 */
1583static void
1584g_mirror_sync_stop(struct g_mirror_disk *disk, int type)
1585{
1586	struct g_consumer *cp;
1587
1588	g_topology_assert();
1589	KASSERT(disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
1590	    ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
1591	    g_mirror_disk_state2str(disk->d_state)));
1592	if (disk->d_sync.ds_consumer == NULL)
1593		return;
1594
1595	if (type == 0) {
1596		G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s finished.",
1597		    disk->d_softc->sc_name, g_mirror_get_diskname(disk));
1598	} else /* if (type == 1) */ {
1599		G_MIRROR_DEBUG(0, "Device %s: rebuilding provider %s stopped.",
1600		    disk->d_softc->sc_name, g_mirror_get_diskname(disk));
1601	}
1602	cp = disk->d_sync.ds_consumer;
1603	g_access(cp, -1, 0, 0);
1604	g_mirror_kill_consumer(disk->d_softc, cp);
1605	free(disk->d_sync.ds_data, M_MIRROR);
1606	disk->d_sync.ds_consumer = NULL;
1607	disk->d_softc->sc_sync.ds_ndisks--;
1608	cp = disk->d_consumer;
1609	KASSERT(cp->acr == 0 && cp->acw == 1 && cp->ace == 1,
1610	    ("Consumer %s not opened.", cp->provider->name));
1611	g_access(cp, 0, -1, -1);
1612	G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d", cp->provider->name, 0, -1,
1613	    -1, 0);
1614	disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
1615}
1616
1617static void
1618g_mirror_launch_provider(struct g_mirror_softc *sc)
1619{
1620	struct g_mirror_disk *disk;
1621	struct g_provider *pp;
1622
1623	g_topology_assert();
1624
1625	pp = g_new_providerf(sc->sc_geom, "mirror/%s", sc->sc_name);
1626	pp->mediasize = sc->sc_mediasize;
1627	pp->sectorsize = sc->sc_sectorsize;
1628	sc->sc_provider = pp;
1629	g_error_provider(pp, 0);
1630	G_MIRROR_DEBUG(0, "Device %s: provider %s launched.", sc->sc_name,
1631	    pp->name);
1632	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1633		if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
1634			g_mirror_sync_start(disk);
1635	}
1636}
1637
1638static void
1639g_mirror_destroy_provider(struct g_mirror_softc *sc)
1640{
1641	struct g_mirror_disk *disk;
1642	struct bio *bp;
1643
1644	g_topology_assert();
1645	KASSERT(sc->sc_provider != NULL, ("NULL provider (device=%s).",
1646	    sc->sc_name));
1647
1648	g_error_provider(sc->sc_provider, ENXIO);
1649	mtx_lock(&sc->sc_queue_mtx);
1650	while ((bp = bioq_first(&sc->sc_queue)) != NULL) {
1651		bioq_remove(&sc->sc_queue, bp);
1652		g_io_deliver(bp, ENXIO);
1653	}
1654	mtx_unlock(&sc->sc_queue_mtx);
1655	G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.", sc->sc_name,
1656	    sc->sc_provider->name);
1657	sc->sc_provider->flags |= G_PF_WITHER;
1658	g_orphan_provider(sc->sc_provider, ENXIO);
1659	sc->sc_provider = NULL;
1660	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1661		if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING)
1662			g_mirror_sync_stop(disk, 1);
1663	}
1664}
1665
1666static void
1667g_mirror_go(void *arg)
1668{
1669	struct g_mirror_softc *sc;
1670
1671	sc = arg;
1672	G_MIRROR_DEBUG(0, "Force device %s start due to timeout.", sc->sc_name);
1673	g_mirror_event_send(sc, 0,
1674	    G_MIRROR_EVENT_DONTWAIT | G_MIRROR_EVENT_DEVICE);
1675}
1676
1677static u_int
1678g_mirror_determine_state(struct g_mirror_disk *disk)
1679{
1680	struct g_mirror_softc *sc;
1681	u_int state;
1682
1683	sc = disk->d_softc;
1684	if (sc->sc_syncid == disk->d_sync.ds_syncid) {
1685		if ((disk->d_flags &
1686		    G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
1687			/* Disk does not need synchronization. */
1688			state = G_MIRROR_DISK_STATE_ACTIVE;
1689		} else {
1690			if ((sc->sc_flags &
1691			     G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0  ||
1692			    (disk->d_flags &
1693			     G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
1694				/*
1695				 * We can start synchronization from
1696				 * the stored offset.
1697				 */
1698				state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
1699			} else {
1700				state = G_MIRROR_DISK_STATE_STALE;
1701			}
1702		}
1703	} else if (disk->d_sync.ds_syncid < sc->sc_syncid) {
1704		/*
1705		 * Reset all synchronization data for this disk,
1706		 * because if it even was synchronized, it was
1707		 * synchronized to disks with different syncid.
1708		 */
1709		disk->d_flags |= G_MIRROR_DISK_FLAG_SYNCHRONIZING;
1710		disk->d_sync.ds_offset = 0;
1711		disk->d_sync.ds_offset_done = 0;
1712		disk->d_sync.ds_syncid = sc->sc_syncid;
1713		if ((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) == 0 ||
1714		    (disk->d_flags & G_MIRROR_DISK_FLAG_FORCE_SYNC) != 0) {
1715			state = G_MIRROR_DISK_STATE_SYNCHRONIZING;
1716		} else {
1717			state = G_MIRROR_DISK_STATE_STALE;
1718		}
1719	} else /* if (sc->sc_syncid < disk->d_sync.ds_syncid) */ {
1720		/*
1721		 * Not good, NOT GOOD!
1722		 * It means that mirror was started on stale disks
1723		 * and more fresh disk just arrive.
1724		 * If there were writes, mirror is fucked up, sorry.
1725		 * I think the best choice here is don't touch
1726		 * this disk and inform the user laudly.
1727		 */
1728		G_MIRROR_DEBUG(0, "Device %s was started before the freshest "
1729		    "disk (%s) arrives!! It will not be connected to the "
1730		    "running device.", sc->sc_name,
1731		    g_mirror_get_diskname(disk));
1732		g_mirror_destroy_disk(disk);
1733		state = G_MIRROR_DISK_STATE_NONE;
1734		/* Return immediately, because disk was destroyed. */
1735		return (state);
1736	}
1737	G_MIRROR_DEBUG(3, "State for %s disk: %s.",
1738	    g_mirror_get_diskname(disk), g_mirror_disk_state2str(state));
1739	return (state);
1740}
1741
1742/*
1743 * Update device state.
1744 */
1745static void
1746g_mirror_update_device(struct g_mirror_softc *sc, boolean_t force)
1747{
1748	struct g_mirror_disk *disk;
1749	u_int state;
1750
1751	g_topology_assert();
1752
1753	switch (sc->sc_state) {
1754	case G_MIRROR_DEVICE_STATE_STARTING:
1755	    {
1756		struct g_mirror_disk *pdisk;
1757		u_int dirty, ndisks, syncid;
1758
1759		KASSERT(sc->sc_provider == NULL,
1760		    ("Non-NULL provider in STARTING state (%s).", sc->sc_name));
1761		/*
1762		 * Are we ready? We are, if all disks are connected or
1763		 * if we have any disks and 'force' is true.
1764		 */
1765		if ((force && g_mirror_ndisks(sc, -1) > 0) ||
1766		    sc->sc_ndisks == g_mirror_ndisks(sc, -1)) {
1767			;
1768		} else if (g_mirror_ndisks(sc, -1) == 0) {
1769			/*
1770			 * Disks went down in starting phase, so destroy
1771			 * device.
1772			 */
1773			callout_drain(&sc->sc_callout);
1774			sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
1775			return;
1776		} else {
1777			return;
1778		}
1779
1780		/*
1781		 * Activate all disks with the biggest syncid.
1782		 */
1783		if (force) {
1784			/*
1785			 * If 'force' is true, we have been called due to
1786			 * timeout, so don't bother canceling timeout.
1787			 */
1788			ndisks = 0;
1789			LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1790				if ((disk->d_flags &
1791				    G_MIRROR_DISK_FLAG_SYNCHRONIZING) == 0) {
1792					ndisks++;
1793				}
1794			}
1795			if (ndisks == 0) {
1796				/* No valid disks found, destroy device. */
1797				sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
1798				return;
1799			}
1800		} else {
1801			/* Cancel timeout. */
1802			callout_drain(&sc->sc_callout);
1803		}
1804
1805		/*
1806		 * Find disk with the biggest syncid.
1807		 */
1808		syncid = 0;
1809		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1810			if (disk->d_sync.ds_syncid > syncid)
1811				syncid = disk->d_sync.ds_syncid;
1812		}
1813
1814		/*
1815		 * Here we need to look for dirty disks and if all disks
1816		 * with the biggest syncid are dirty, we have to choose
1817		 * one with the biggest priority and rebuild the rest.
1818		 */
1819		/*
1820		 * Find the number of dirty disks with the biggest syncid.
1821		 * Find the number of disks with the biggest syncid.
1822		 * While here, find a disk with the biggest priority.
1823		 */
1824		dirty = ndisks = 0;
1825		pdisk = NULL;
1826		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1827			if (disk->d_sync.ds_syncid != syncid)
1828				continue;
1829			if ((disk->d_flags &
1830			    G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
1831				continue;
1832			}
1833			ndisks++;
1834			if ((disk->d_flags & G_MIRROR_DISK_FLAG_DIRTY) != 0) {
1835				dirty++;
1836				if (pdisk == NULL ||
1837				    pdisk->d_priority < disk->d_priority) {
1838					pdisk = disk;
1839				}
1840			}
1841		}
1842		if (dirty == 0) {
1843			/* No dirty disks at all, great. */
1844		} else if (dirty == ndisks) {
1845			/*
1846			 * Force synchronization for all dirty disks except one
1847			 * with the biggest priority.
1848			 */
1849			KASSERT(pdisk != NULL, ("pdisk == NULL"));
1850			G_MIRROR_DEBUG(1, "Using disk %s (device %s) as a "
1851			    "master disk for synchronization.",
1852			    g_mirror_get_diskname(pdisk), sc->sc_name);
1853			LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1854				if (disk->d_sync.ds_syncid != syncid)
1855					continue;
1856				if ((disk->d_flags &
1857				    G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
1858					continue;
1859				}
1860				KASSERT((disk->d_flags &
1861				    G_MIRROR_DISK_FLAG_DIRTY) != 0,
1862				    ("Disk %s isn't marked as dirty.",
1863				    g_mirror_get_diskname(disk)));
1864				/* Skip the disk with the biggest priority. */
1865				if (disk == pdisk)
1866					continue;
1867				disk->d_sync.ds_syncid = 0;
1868			}
1869		} else if (dirty < ndisks) {
1870			/*
1871			 * Force synchronization for all dirty disks.
1872			 * We have some non-dirty disks.
1873			 */
1874			LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1875				if (disk->d_sync.ds_syncid != syncid)
1876					continue;
1877				if ((disk->d_flags &
1878				    G_MIRROR_DISK_FLAG_SYNCHRONIZING) != 0) {
1879					continue;
1880				}
1881				if ((disk->d_flags &
1882				    G_MIRROR_DISK_FLAG_DIRTY) == 0) {
1883					continue;
1884				}
1885				disk->d_sync.ds_syncid = 0;
1886			}
1887		}
1888
1889		/* Reset hint. */
1890		sc->sc_hint = NULL;
1891		sc->sc_syncid = syncid;
1892		if (force) {
1893			/* Remember to bump syncid on first write. */
1894			sc->sc_bump_syncid = G_MIRROR_BUMP_ON_FIRST_WRITE;
1895		}
1896		state = G_MIRROR_DEVICE_STATE_RUNNING;
1897		G_MIRROR_DEBUG(1, "Device %s state changed from %s to %s.",
1898		    sc->sc_name, g_mirror_device_state2str(sc->sc_state),
1899		    g_mirror_device_state2str(state));
1900		sc->sc_state = state;
1901		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
1902			state = g_mirror_determine_state(disk);
1903			g_mirror_event_send(disk, state,
1904			    G_MIRROR_EVENT_DONTWAIT);
1905			if (state == G_MIRROR_DISK_STATE_STALE) {
1906				sc->sc_bump_syncid =
1907				    G_MIRROR_BUMP_ON_FIRST_WRITE;
1908			}
1909		}
1910		wakeup(&g_mirror_class);
1911		break;
1912	    }
1913	case G_MIRROR_DEVICE_STATE_RUNNING:
1914		if (g_mirror_ndisks(sc, -1) == 0) {
1915			/*
1916			 * No disks at all, we need to destroy device.
1917			 */
1918			sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
1919			break;
1920		} else if (g_mirror_ndisks(sc,
1921		    G_MIRROR_DISK_STATE_ACTIVE) == 0 &&
1922		    g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
1923			/*
1924			 * No active disks, destroy provider.
1925			 */
1926			if (sc->sc_provider != NULL)
1927				g_mirror_destroy_provider(sc);
1928			break;
1929		} else if (g_mirror_ndisks(sc,
1930		    G_MIRROR_DISK_STATE_ACTIVE) > 0 &&
1931		    g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_NEW) == 0) {
1932			/*
1933			 * We have active disks, launch provider if it doesn't
1934			 * exist.
1935			 */
1936			if (sc->sc_provider == NULL)
1937				g_mirror_launch_provider(sc);
1938		}
1939		/*
1940		 * Bump syncid here, if we need to do it immediately.
1941		 */
1942		if (sc->sc_bump_syncid == G_MIRROR_BUMP_IMMEDIATELY) {
1943			sc->sc_bump_syncid = 0;
1944			g_mirror_bump_syncid(sc);
1945		}
1946		break;
1947	default:
1948		KASSERT(1 == 0, ("Wrong device state (%s, %s).",
1949		    sc->sc_name, g_mirror_device_state2str(sc->sc_state)));
1950		break;
1951	}
1952}
1953
1954/*
1955 * Update disk state and device state if needed.
1956 */
1957#define	DISK_STATE_CHANGED()	G_MIRROR_DEBUG(1,			\
1958	"Disk %s state changed from %s to %s (device %s).",		\
1959	g_mirror_get_diskname(disk),					\
1960	g_mirror_disk_state2str(disk->d_state),				\
1961	g_mirror_disk_state2str(state), sc->sc_name)
1962static int
1963g_mirror_update_disk(struct g_mirror_disk *disk, u_int state)
1964{
1965	struct g_mirror_softc *sc;
1966
1967	g_topology_assert();
1968
1969	sc = disk->d_softc;
1970again:
1971	G_MIRROR_DEBUG(3, "Changing disk %s state from %s to %s.",
1972	    g_mirror_get_diskname(disk), g_mirror_disk_state2str(disk->d_state),
1973	    g_mirror_disk_state2str(state));
1974	switch (state) {
1975	case G_MIRROR_DISK_STATE_NEW:
1976		/*
1977		 * Possible scenarios:
1978		 * 1. New disk arrive.
1979		 */
1980		/* Previous state should be NONE. */
1981		KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NONE,
1982		    ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
1983		    g_mirror_disk_state2str(disk->d_state)));
1984		DISK_STATE_CHANGED();
1985
1986		disk->d_state = state;
1987		if (LIST_EMPTY(&sc->sc_disks))
1988			LIST_INSERT_HEAD(&sc->sc_disks, disk, d_next);
1989		else {
1990			struct g_mirror_disk *dp;
1991
1992			LIST_FOREACH(dp, &sc->sc_disks, d_next) {
1993				if (disk->d_priority >= dp->d_priority) {
1994					LIST_INSERT_BEFORE(dp, disk, d_next);
1995					dp = NULL;
1996					break;
1997				}
1998				if (LIST_NEXT(dp, d_next) == NULL)
1999					break;
2000			}
2001			if (dp != NULL)
2002				LIST_INSERT_AFTER(dp, disk, d_next);
2003		}
2004		G_MIRROR_DEBUG(0, "Device %s: provider %s detected.",
2005		    sc->sc_name, g_mirror_get_diskname(disk));
2006		if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2007			break;
2008		KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2009		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2010		    g_mirror_device_state2str(sc->sc_state),
2011		    g_mirror_get_diskname(disk),
2012		    g_mirror_disk_state2str(disk->d_state)));
2013		state = g_mirror_determine_state(disk);
2014		if (state != G_MIRROR_DISK_STATE_NONE)
2015			goto again;
2016		break;
2017	case G_MIRROR_DISK_STATE_ACTIVE:
2018		/*
2019		 * Possible scenarios:
2020		 * 1. New disk does not need synchronization.
2021		 * 2. Synchronization process finished successfully.
2022		 */
2023		KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2024		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2025		    g_mirror_device_state2str(sc->sc_state),
2026		    g_mirror_get_diskname(disk),
2027		    g_mirror_disk_state2str(disk->d_state)));
2028		/* Previous state should be NEW or SYNCHRONIZING. */
2029		KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW ||
2030		    disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2031		    ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2032		    g_mirror_disk_state2str(disk->d_state)));
2033		DISK_STATE_CHANGED();
2034
2035		if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2036			disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2037		else if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2038			disk->d_flags &= ~G_MIRROR_DISK_FLAG_SYNCHRONIZING;
2039			disk->d_flags &= ~G_MIRROR_DISK_FLAG_FORCE_SYNC;
2040			g_mirror_sync_stop(disk, 0);
2041		}
2042		disk->d_state = state;
2043		disk->d_sync.ds_offset = 0;
2044		disk->d_sync.ds_offset_done = 0;
2045		g_mirror_update_access(disk);
2046		g_mirror_update_metadata(disk);
2047		G_MIRROR_DEBUG(0, "Device %s: provider %s activated.",
2048		    sc->sc_name, g_mirror_get_diskname(disk));
2049		break;
2050	case G_MIRROR_DISK_STATE_STALE:
2051		/*
2052		 * Possible scenarios:
2053		 * 1. Stale disk was connected.
2054		 */
2055		/* Previous state should be NEW. */
2056		KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2057		    ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2058		    g_mirror_disk_state2str(disk->d_state)));
2059		KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2060		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2061		    g_mirror_device_state2str(sc->sc_state),
2062		    g_mirror_get_diskname(disk),
2063		    g_mirror_disk_state2str(disk->d_state)));
2064		/*
2065		 * STALE state is only possible if device is marked
2066		 * NOAUTOSYNC.
2067		 */
2068		KASSERT((sc->sc_flags & G_MIRROR_DEVICE_FLAG_NOAUTOSYNC) != 0,
2069		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2070		    g_mirror_device_state2str(sc->sc_state),
2071		    g_mirror_get_diskname(disk),
2072		    g_mirror_disk_state2str(disk->d_state)));
2073		DISK_STATE_CHANGED();
2074
2075		disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2076		disk->d_state = state;
2077		g_mirror_update_metadata(disk);
2078		G_MIRROR_DEBUG(0, "Device %s: provider %s is stale.",
2079		    sc->sc_name, g_mirror_get_diskname(disk));
2080		break;
2081	case G_MIRROR_DISK_STATE_SYNCHRONIZING:
2082		/*
2083		 * Possible scenarios:
2084		 * 1. Disk which needs synchronization was connected.
2085		 */
2086		/* Previous state should be NEW. */
2087		KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2088		    ("Wrong disk state (%s, %s).", g_mirror_get_diskname(disk),
2089		    g_mirror_disk_state2str(disk->d_state)));
2090		KASSERT(sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING,
2091		    ("Wrong device state (%s, %s, %s, %s).", sc->sc_name,
2092		    g_mirror_device_state2str(sc->sc_state),
2093		    g_mirror_get_diskname(disk),
2094		    g_mirror_disk_state2str(disk->d_state)));
2095		DISK_STATE_CHANGED();
2096
2097		if (disk->d_state == G_MIRROR_DISK_STATE_NEW)
2098			disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2099		disk->d_state = state;
2100		if (sc->sc_provider != NULL) {
2101			g_mirror_sync_start(disk);
2102			g_mirror_update_metadata(disk);
2103		}
2104		break;
2105	case G_MIRROR_DISK_STATE_DISCONNECTED:
2106		/*
2107		 * Possible scenarios:
2108		 * 1. Device wasn't running yet, but disk disappear.
2109		 * 2. Disk was active and disapppear.
2110		 * 3. Disk disappear during synchronization process.
2111		 */
2112		if (sc->sc_state == G_MIRROR_DEVICE_STATE_RUNNING) {
2113			/*
2114			 * Previous state should be ACTIVE, STALE or
2115			 * SYNCHRONIZING.
2116			 */
2117			KASSERT(disk->d_state == G_MIRROR_DISK_STATE_ACTIVE ||
2118			    disk->d_state == G_MIRROR_DISK_STATE_STALE ||
2119			    disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING,
2120			    ("Wrong disk state (%s, %s).",
2121			    g_mirror_get_diskname(disk),
2122			    g_mirror_disk_state2str(disk->d_state)));
2123		} else if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING) {
2124			/* Previous state should be NEW. */
2125			KASSERT(disk->d_state == G_MIRROR_DISK_STATE_NEW,
2126			    ("Wrong disk state (%s, %s).",
2127			    g_mirror_get_diskname(disk),
2128			    g_mirror_disk_state2str(disk->d_state)));
2129			/*
2130			 * Reset bumping syncid if disk disappeared in STARTING
2131			 * state.
2132			 */
2133			if (sc->sc_bump_syncid == G_MIRROR_BUMP_ON_FIRST_WRITE)
2134				sc->sc_bump_syncid = 0;
2135#ifdef	INVARIANTS
2136		} else {
2137			KASSERT(1 == 0, ("Wrong device state (%s, %s, %s, %s).",
2138			    sc->sc_name,
2139			    g_mirror_device_state2str(sc->sc_state),
2140			    g_mirror_get_diskname(disk),
2141			    g_mirror_disk_state2str(disk->d_state)));
2142#endif
2143		}
2144		DISK_STATE_CHANGED();
2145		G_MIRROR_DEBUG(0, "Device %s: provider %s disconnected.",
2146		    sc->sc_name, g_mirror_get_diskname(disk));
2147
2148		g_mirror_destroy_disk(disk);
2149		break;
2150	case G_MIRROR_DISK_STATE_DESTROY:
2151	    {
2152		int error;
2153
2154		error = g_mirror_clear_metadata(disk);
2155		if (error != 0)
2156			return (error);
2157		DISK_STATE_CHANGED();
2158		G_MIRROR_DEBUG(0, "Device %s: provider %s destroyed.",
2159		    sc->sc_name, g_mirror_get_diskname(disk));
2160
2161		g_mirror_destroy_disk(disk);
2162		sc->sc_ndisks--;
2163		LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2164			g_mirror_update_metadata(disk);
2165		}
2166		break;
2167	    }
2168	default:
2169		KASSERT(1 == 0, ("Unknown state (%u).", state));
2170		break;
2171	}
2172	return (0);
2173}
2174#undef	DISK_STATE_CHANGED
2175
2176static int
2177g_mirror_read_metadata(struct g_consumer *cp, struct g_mirror_metadata *md)
2178{
2179	struct g_provider *pp;
2180	u_char *buf;
2181	int error;
2182
2183	g_topology_assert();
2184
2185	error = g_access(cp, 1, 0, 0);
2186	if (error != 0)
2187		return (error);
2188	pp = cp->provider;
2189	g_topology_unlock();
2190	/* Metadata are stored on last sector. */
2191	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
2192	    &error);
2193	g_topology_lock();
2194	if (buf == NULL) {
2195		g_access(cp, -1, 0, 0);
2196		return (error);
2197	}
2198	if (error != 0) {
2199		g_access(cp, -1, 0, 0);
2200		g_free(buf);
2201		return (error);
2202	}
2203	error = g_access(cp, -1, 0, 0);
2204	KASSERT(error == 0, ("Cannot decrease access count for %s.", pp->name));
2205
2206	/* Decode metadata. */
2207	error = mirror_metadata_decode(buf, md);
2208	g_free(buf);
2209	if (strcmp(md->md_magic, G_MIRROR_MAGIC) != 0)
2210		return (EINVAL);
2211	if (error != 0) {
2212		G_MIRROR_DEBUG(1, "MD5 metadata hash mismatch for provider %s.",
2213		    cp->provider->name);
2214		return (error);
2215	}
2216
2217	return (0);
2218}
2219
2220static int
2221g_mirror_check_metadata(struct g_mirror_softc *sc, struct g_provider *pp,
2222    struct g_mirror_metadata *md)
2223{
2224
2225	if (g_mirror_id2disk(sc, md->md_did) != NULL) {
2226		G_MIRROR_DEBUG(1, "Disk %s (id=%u) already exists, skipping.",
2227		    pp->name, md->md_did);
2228		return (EEXIST);
2229	}
2230	if (md->md_all != sc->sc_ndisks) {
2231		G_MIRROR_DEBUG(1,
2232		    "Invalid '%s' field on disk %s (device %s), skipping.",
2233		    "md_all", pp->name, sc->sc_name);
2234		return (EINVAL);
2235	}
2236	if (md->md_slice != sc->sc_slice) {
2237		G_MIRROR_DEBUG(1,
2238		    "Invalid '%s' field on disk %s (device %s), skipping.",
2239		    "md_slice", pp->name, sc->sc_name);
2240		return (EINVAL);
2241	}
2242	if (md->md_balance != sc->sc_balance) {
2243		G_MIRROR_DEBUG(1,
2244		    "Invalid '%s' field on disk %s (device %s), skipping.",
2245		    "md_balance", pp->name, sc->sc_name);
2246		return (EINVAL);
2247	}
2248	if (md->md_mediasize != sc->sc_mediasize) {
2249		G_MIRROR_DEBUG(1,
2250		    "Invalid '%s' field on disk %s (device %s), skipping.",
2251		    "md_mediasize", pp->name, sc->sc_name);
2252		return (EINVAL);
2253	}
2254	if (sc->sc_mediasize > pp->mediasize) {
2255		G_MIRROR_DEBUG(1,
2256		    "Invalid size of disk %s (device %s), skipping.", pp->name,
2257		    sc->sc_name);
2258		return (EINVAL);
2259	}
2260	if (md->md_sectorsize != sc->sc_sectorsize) {
2261		G_MIRROR_DEBUG(1,
2262		    "Invalid '%s' field on disk %s (device %s), skipping.",
2263		    "md_sectorsize", pp->name, sc->sc_name);
2264		return (EINVAL);
2265	}
2266	if ((sc->sc_sectorsize % pp->sectorsize) != 0) {
2267		G_MIRROR_DEBUG(1,
2268		    "Invalid sector size of disk %s (device %s), skipping.",
2269		    pp->name, sc->sc_name);
2270		return (EINVAL);
2271	}
2272	if ((md->md_mflags & ~G_MIRROR_DEVICE_FLAG_MASK) != 0) {
2273		G_MIRROR_DEBUG(1,
2274		    "Invalid device flags on disk %s (device %s), skipping.",
2275		    pp->name, sc->sc_name);
2276		return (EINVAL);
2277	}
2278	if ((md->md_dflags & ~G_MIRROR_DISK_FLAG_MASK) != 0) {
2279		G_MIRROR_DEBUG(1,
2280		    "Invalid disk flags on disk %s (device %s), skipping.",
2281		    pp->name, sc->sc_name);
2282		return (EINVAL);
2283	}
2284	return (0);
2285}
2286
2287static int
2288g_mirror_add_disk(struct g_mirror_softc *sc, struct g_provider *pp,
2289    struct g_mirror_metadata *md)
2290{
2291	struct g_mirror_disk *disk;
2292	int error;
2293
2294	g_topology_assert();
2295	G_MIRROR_DEBUG(2, "Adding disk %s.", pp->name);
2296
2297	error = g_mirror_check_metadata(sc, pp, md);
2298	if (error != 0)
2299		return (error);
2300	disk = g_mirror_init_disk(sc, pp, md, &error);
2301	if (disk == NULL)
2302		return (error);
2303	error = g_mirror_event_send(disk, G_MIRROR_DISK_STATE_NEW,
2304	    G_MIRROR_EVENT_WAIT);
2305	return (error);
2306}
2307
2308static int
2309g_mirror_access(struct g_provider *pp, int acr, int acw, int ace)
2310{
2311	struct g_mirror_softc *sc;
2312	struct g_mirror_disk *disk;
2313	int dcr, dcw, dce, err, error;
2314
2315	g_topology_assert();
2316	G_MIRROR_DEBUG(2, "Access request for %s: r%dw%de%d.", pp->name, acr,
2317	    acw, ace);
2318
2319	dcr = pp->acr + acr;
2320	dcw = pp->acw + acw;
2321	dce = pp->ace + ace;
2322
2323	/* On first open, grab an extra "exclusive" bit */
2324	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
2325		ace++;
2326	/* ... and let go of it on last close */
2327	if (dcr == 0 && dcw == 0 && dce == 0)
2328		ace--;
2329
2330	sc = pp->geom->softc;
2331	if (sc == NULL || LIST_EMPTY(&sc->sc_disks)) {
2332		if (acr <= 0 && acw <= 0 && ace <= 0)
2333			return (0);
2334		else
2335			return (ENXIO);
2336	}
2337	error = ENXIO;
2338	LIST_FOREACH(disk, &sc->sc_disks, d_next) {
2339		if (disk->d_state != G_MIRROR_DISK_STATE_ACTIVE)
2340			continue;
2341		err = g_access(disk->d_consumer, acr, acw, ace);
2342		G_MIRROR_DEBUG(2, "Access %s r%dw%de%d = %d",
2343		    g_mirror_get_diskname(disk), acr, acw, ace, err);
2344		if (err == 0) {
2345			/*
2346			 * Mark disk as dirty on open and unmark on close.
2347			 */
2348			if (pp->acw == 0 && dcw > 0) {
2349				G_MIRROR_DEBUG(1,
2350				    "Disk %s (device %s) marked as dirty.",
2351				    g_mirror_get_diskname(disk), sc->sc_name);
2352				disk->d_flags |= G_MIRROR_DISK_FLAG_DIRTY;
2353				g_mirror_update_metadata(disk);
2354			} else if (pp->acw > 0 && dcw == 0) {
2355				G_MIRROR_DEBUG(1,
2356				    "Disk %s (device %s) marked as clean.",
2357				    g_mirror_get_diskname(disk), sc->sc_name);
2358				disk->d_flags &= ~G_MIRROR_DISK_FLAG_DIRTY;
2359				g_mirror_update_metadata(disk);
2360			}
2361			error = 0;
2362		} else {
2363			sc->sc_bump_syncid = G_MIRROR_BUMP_ON_FIRST_WRITE;
2364			g_mirror_event_send(disk,
2365			    G_MIRROR_DISK_STATE_DISCONNECTED,
2366			    G_MIRROR_EVENT_DONTWAIT);
2367		}
2368	}
2369	/*
2370	 * Be sure to return 0 for negativate access requests.
2371	 * In case of some HW problems, it is possible that we don't have
2372	 * any active disk here, so loop above will be no-op and error will
2373	 * be ENXIO.
2374	 */
2375	if (error != 0 && acr <= 0 && acw <= 0 && ace <= 0)
2376		error = 0;
2377	return (error);
2378}
2379
2380static struct g_geom *
2381g_mirror_create(struct g_class *mp, const struct g_mirror_metadata *md)
2382{
2383	struct g_mirror_softc *sc;
2384	struct g_geom *gp;
2385	int error, timeout;
2386
2387	g_topology_assert();
2388	G_MIRROR_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
2389	    md->md_mid);
2390
2391	/* One disk is minimum. */
2392	if (md->md_all < 1)
2393		return (NULL);
2394	/*
2395	 * Action geom.
2396	 */
2397	gp = g_new_geomf(mp, "%s", md->md_name);
2398	sc = malloc(sizeof(*sc), M_MIRROR, M_WAITOK | M_ZERO);
2399	gp->start = g_mirror_start;
2400	gp->spoiled = g_mirror_spoiled;
2401	gp->orphan = g_mirror_orphan;
2402	gp->access = g_mirror_access;
2403	gp->dumpconf = g_mirror_dumpconf;
2404
2405	sc->sc_id = md->md_mid;
2406	sc->sc_slice = md->md_slice;
2407	sc->sc_balance = md->md_balance;
2408	sc->sc_mediasize = md->md_mediasize;
2409	sc->sc_sectorsize = md->md_sectorsize;
2410	sc->sc_ndisks = md->md_all;
2411	sc->sc_flags = md->md_mflags;
2412	sc->sc_bump_syncid = 0;
2413	bioq_init(&sc->sc_queue);
2414	mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
2415	LIST_INIT(&sc->sc_disks);
2416	TAILQ_INIT(&sc->sc_events);
2417	mtx_init(&sc->sc_events_mtx, "gmirror:events", NULL, MTX_DEF);
2418	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
2419	sc->sc_state = G_MIRROR_DEVICE_STATE_STARTING;
2420	gp->softc = sc;
2421	sc->sc_geom = gp;
2422	sc->sc_provider = NULL;
2423	/*
2424	 * Synchronization geom.
2425	 */
2426	gp = g_new_geomf(mp, "%s.sync", md->md_name);
2427	gp->softc = sc;
2428	gp->orphan = g_mirror_orphan;
2429	sc->sc_sync.ds_geom = gp;
2430	sc->sc_sync.ds_ndisks = 0;
2431	error = kthread_create(g_mirror_worker, sc, &sc->sc_worker, 0, 0,
2432	    "g_mirror %s", md->md_name);
2433	if (error != 0) {
2434		G_MIRROR_DEBUG(1, "Cannot create kernel thread for %s.",
2435		    sc->sc_name);
2436		g_destroy_geom(sc->sc_sync.ds_geom);
2437		mtx_destroy(&sc->sc_events_mtx);
2438		mtx_destroy(&sc->sc_queue_mtx);
2439		g_destroy_geom(sc->sc_geom);
2440		free(sc, M_MIRROR);
2441		return (NULL);
2442	}
2443
2444	G_MIRROR_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
2445
2446	/*
2447	 * Run timeout.
2448	 */
2449	timeout = atomic_load_acq_int(&g_mirror_timeout);
2450	callout_reset(&sc->sc_callout, timeout * hz, g_mirror_go, sc);
2451	return (sc->sc_geom);
2452}
2453
2454int
2455g_mirror_destroy(struct g_mirror_softc *sc, boolean_t force)
2456{
2457	struct g_provider *pp;
2458
2459	g_topology_assert();
2460
2461	if (sc == NULL)
2462		return (ENXIO);
2463	pp = sc->sc_provider;
2464	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
2465		if (force) {
2466			G_MIRROR_DEBUG(0, "Device %s is still open, so it "
2467			    "can't be definitely removed.", pp->name);
2468		} else {
2469			G_MIRROR_DEBUG(1,
2470			    "Device %s is still open (r%dw%de%d).", pp->name,
2471			    pp->acr, pp->acw, pp->ace);
2472			return (EBUSY);
2473		}
2474	}
2475
2476	sc->sc_flags |= G_MIRROR_DEVICE_FLAG_DESTROY;
2477	sc->sc_flags |= G_MIRROR_DEVICE_FLAG_WAIT;
2478	g_topology_unlock();
2479	G_MIRROR_DEBUG(4, "%s: Waking up %p.", __func__, sc);
2480	mtx_lock(&sc->sc_queue_mtx);
2481	wakeup(sc);
2482	mtx_unlock(&sc->sc_queue_mtx);
2483	G_MIRROR_DEBUG(4, "%s: Sleeping %p.", __func__, &sc->sc_worker);
2484	while (sc->sc_worker != NULL)
2485		tsleep(&sc->sc_worker, PRIBIO, "m:destroy", hz / 5);
2486	G_MIRROR_DEBUG(4, "%s: Woken up %p.", __func__, &sc->sc_worker);
2487	g_topology_lock();
2488	g_mirror_destroy_device(sc);
2489	free(sc, M_MIRROR);
2490	return (0);
2491}
2492
2493static void
2494g_mirror_taste_orphan(struct g_consumer *cp)
2495{
2496
2497	KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2498	    cp->provider->name));
2499}
2500
2501static struct g_geom *
2502g_mirror_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2503{
2504	struct g_mirror_metadata md;
2505	struct g_mirror_softc *sc;
2506	struct g_consumer *cp;
2507	struct g_geom *gp;
2508	int error;
2509
2510	g_topology_assert();
2511	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
2512	G_MIRROR_DEBUG(2, "Tasting %s.", pp->name);
2513
2514	gp = g_new_geomf(mp, "mirror:taste");
2515	/*
2516	 * This orphan function should be never called.
2517	 */
2518	gp->orphan = g_mirror_taste_orphan;
2519	cp = g_new_consumer(gp);
2520	g_attach(cp, pp);
2521	error = g_mirror_read_metadata(cp, &md);
2522	g_detach(cp);
2523	g_destroy_consumer(cp);
2524	g_destroy_geom(gp);
2525	if (error != 0)
2526		return (NULL);
2527	gp = NULL;
2528
2529	if (md.md_version > G_MIRROR_VERSION) {
2530		printf("geom_mirror.ko module is too old to handle %s.\n",
2531		    pp->name);
2532		return (NULL);
2533	}
2534	if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0)
2535		return (NULL);
2536	if ((md.md_dflags & G_MIRROR_DISK_FLAG_INACTIVE) != 0) {
2537		G_MIRROR_DEBUG(0,
2538		    "Device %s: provider %s marked as inactive, skipping.",
2539		    md.md_name, pp->name);
2540		return (NULL);
2541	}
2542	if (g_mirror_debug >= 2)
2543		mirror_metadata_dump(&md);
2544
2545	/*
2546	 * Let's check if device already exists.
2547	 */
2548	sc = NULL;
2549	LIST_FOREACH(gp, &mp->geom, geom) {
2550		sc = gp->softc;
2551		if (sc == NULL)
2552			continue;
2553		if (sc->sc_sync.ds_geom == gp)
2554			continue;
2555		if (strcmp(md.md_name, sc->sc_name) != 0)
2556			continue;
2557		if (md.md_mid != sc->sc_id) {
2558			G_MIRROR_DEBUG(0, "Device %s already configured.",
2559			    sc->sc_name);
2560			return (NULL);
2561		}
2562		break;
2563	}
2564	if (gp == NULL) {
2565		gp = g_mirror_create(mp, &md);
2566		if (gp == NULL) {
2567			G_MIRROR_DEBUG(0, "Cannot create device %s.",
2568			    md.md_name);
2569			return (NULL);
2570		}
2571		sc = gp->softc;
2572	}
2573	G_MIRROR_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
2574	error = g_mirror_add_disk(sc, pp, &md);
2575	if (error != 0) {
2576		G_MIRROR_DEBUG(0, "Cannot add disk %s to %s (error=%d).",
2577		    pp->name, gp->name, error);
2578		if (LIST_EMPTY(&sc->sc_disks))
2579			g_mirror_destroy(sc, 1);
2580		return (NULL);
2581	}
2582	return (gp);
2583}
2584
2585static int
2586g_mirror_destroy_geom(struct gctl_req *req __unused,
2587    struct g_class *mp __unused, struct g_geom *gp)
2588{
2589
2590	return (g_mirror_destroy(gp->softc, 0));
2591}
2592
2593static void
2594g_mirror_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2595    struct g_consumer *cp, struct g_provider *pp)
2596{
2597	struct g_mirror_softc *sc;
2598
2599	g_topology_assert();
2600
2601	sc = gp->softc;
2602	if (sc == NULL)
2603		return;
2604	/* Skip synchronization geom. */
2605	if (gp == sc->sc_sync.ds_geom)
2606		return;
2607	if (pp != NULL) {
2608		/* Nothing here. */
2609	} else if (cp != NULL) {
2610		struct g_mirror_disk *disk;
2611
2612		disk = cp->private;
2613		if (disk == NULL)
2614			return;
2615		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)disk->d_id);
2616		if (disk->d_state == G_MIRROR_DISK_STATE_SYNCHRONIZING) {
2617			sbuf_printf(sb, "%s<Synchronized>", indent);
2618			if (disk->d_sync.ds_offset_done == 0)
2619				sbuf_printf(sb, "0%%");
2620			else {
2621				sbuf_printf(sb, "%u%%",
2622				    (u_int)((disk->d_sync.ds_offset_done * 100) /
2623				    sc->sc_provider->mediasize));
2624			}
2625			sbuf_printf(sb, "</Synchronized>\n");
2626		}
2627		sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent,
2628		    disk->d_sync.ds_syncid);
2629		sbuf_printf(sb, "%s<Flags>", indent);
2630		if (disk->d_flags == 0)
2631			sbuf_printf(sb, "NONE");
2632		else {
2633			int first = 1;
2634
2635#define	ADD_FLAG(flag, name)	do {					\
2636	if ((disk->d_flags & (flag)) != 0) {				\
2637		if (!first)						\
2638			sbuf_printf(sb, ", ");				\
2639		else							\
2640			first = 0;					\
2641		sbuf_printf(sb, name);					\
2642	}								\
2643} while (0)
2644			ADD_FLAG(G_MIRROR_DISK_FLAG_DIRTY, "DIRTY");
2645			ADD_FLAG(G_MIRROR_DISK_FLAG_HARDCODED, "HARDCODED");
2646			ADD_FLAG(G_MIRROR_DISK_FLAG_INACTIVE, "INACTIVE");
2647			ADD_FLAG(G_MIRROR_DISK_FLAG_SYNCHRONIZING,
2648			    "SYNCHRONIZING");
2649			ADD_FLAG(G_MIRROR_DISK_FLAG_FORCE_SYNC, "FORCE_SYNC");
2650#undef	ADD_FLAG
2651		}
2652		sbuf_printf(sb, "</Flags>\n");
2653		sbuf_printf(sb, "%s<Priority>%u</Priority>\n", indent,
2654		    disk->d_priority);
2655		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2656		    g_mirror_disk_state2str(disk->d_state));
2657	} else {
2658		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
2659		sbuf_printf(sb, "%s<SyncID>%u</SyncID>\n", indent, sc->sc_syncid);
2660		sbuf_printf(sb, "%s<Flags>", indent);
2661		if (sc->sc_flags == 0)
2662			sbuf_printf(sb, "NONE");
2663		else {
2664			int first = 1;
2665
2666#define	ADD_FLAG(flag, name)	do {					\
2667	if ((sc->sc_flags & (flag)) != 0) {				\
2668		if (!first)						\
2669			sbuf_printf(sb, ", ");				\
2670		else							\
2671			first = 0;					\
2672		sbuf_printf(sb, name);					\
2673	}								\
2674} while (0)
2675			ADD_FLAG(G_MIRROR_DEVICE_FLAG_NOAUTOSYNC, "NOAUTOSYNC");
2676#undef	ADD_FLAG
2677		}
2678		sbuf_printf(sb, "</Flags>\n");
2679		sbuf_printf(sb, "%s<Slice>%u</Slice>\n", indent,
2680		    (u_int)sc->sc_slice);
2681		sbuf_printf(sb, "%s<Balance>%s</Balance>\n", indent,
2682		    balance_name(sc->sc_balance));
2683		sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
2684		    sc->sc_ndisks);
2685		sbuf_printf(sb, "%s<State>", indent);
2686		if (sc->sc_state == G_MIRROR_DEVICE_STATE_STARTING)
2687			sbuf_printf(sb, "%s", "STARTING");
2688		else if (sc->sc_ndisks ==
2689		    g_mirror_ndisks(sc, G_MIRROR_DISK_STATE_ACTIVE))
2690			sbuf_printf(sb, "%s", "COMPLETE");
2691		else
2692			sbuf_printf(sb, "%s", "DEGRADED");
2693		sbuf_printf(sb, "</State>\n");
2694	}
2695}
2696
2697static int
2698g_mirror_can_go(void)
2699{
2700	struct g_mirror_softc *sc;
2701	struct g_geom *gp;
2702	struct g_provider *pp;
2703	int can_go;
2704
2705	DROP_GIANT();
2706	can_go = 1;
2707	g_topology_lock();
2708	LIST_FOREACH(gp, &g_mirror_class.geom, geom) {
2709		sc = gp->softc;
2710		if (sc == NULL) {
2711			can_go = 0;
2712			break;
2713		}
2714		pp = sc->sc_provider;
2715		if (pp == NULL || pp->error != 0) {
2716			can_go = 0;
2717			break;
2718		}
2719	}
2720	g_topology_unlock();
2721	PICKUP_GIANT();
2722	return (can_go);
2723}
2724
2725static void
2726g_mirror_rootwait(void)
2727{
2728
2729	/*
2730	 * HACK: Wait for GEOM, because g_mirror_rootwait() can be called,
2731	 * HACK: before we get providers for tasting.
2732	 */
2733	tsleep(&g_mirror_class, PRIBIO, "mroot", hz * 3);
2734	/*
2735	 * Wait for mirrors in degraded state.
2736	 */
2737	for (;;) {
2738		if (g_mirror_can_go())
2739			break;
2740		tsleep(&g_mirror_class, PRIBIO, "mroot", hz);
2741	}
2742}
2743
2744SYSINIT(g_mirror_root, SI_SUB_RAID, SI_ORDER_FIRST, g_mirror_rootwait, NULL)
2745
2746DECLARE_GEOM_CLASS(g_mirror_class, g_mirror);
2747