xref: /freebsd/sys/geom/raid/g_raid.c (revision 5d807a0e)
189b17223SAlexander Motin /*-
289b17223SAlexander Motin  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
389b17223SAlexander Motin  * All rights reserved.
489b17223SAlexander Motin  *
589b17223SAlexander Motin  * Redistribution and use in source and binary forms, with or without
689b17223SAlexander Motin  * modification, are permitted provided that the following conditions
789b17223SAlexander Motin  * are met:
889b17223SAlexander Motin  * 1. Redistributions of source code must retain the above copyright
989b17223SAlexander Motin  *    notice, this list of conditions and the following disclaimer.
1089b17223SAlexander Motin  * 2. Redistributions in binary form must reproduce the above copyright
1189b17223SAlexander Motin  *    notice, this list of conditions and the following disclaimer in the
1289b17223SAlexander Motin  *    documentation and/or other materials provided with the distribution.
1389b17223SAlexander Motin  *
1489b17223SAlexander Motin  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1589b17223SAlexander Motin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1689b17223SAlexander Motin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1789b17223SAlexander Motin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
1889b17223SAlexander Motin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1989b17223SAlexander Motin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2089b17223SAlexander Motin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2189b17223SAlexander Motin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2289b17223SAlexander Motin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2389b17223SAlexander Motin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2489b17223SAlexander Motin  * SUCH DAMAGE.
2589b17223SAlexander Motin  */
2689b17223SAlexander Motin 
2789b17223SAlexander Motin #include <sys/cdefs.h>
2889b17223SAlexander Motin __FBSDID("$FreeBSD$");
2989b17223SAlexander Motin 
3089b17223SAlexander Motin #include <sys/param.h>
3189b17223SAlexander Motin #include <sys/systm.h>
3289b17223SAlexander Motin #include <sys/kernel.h>
3389b17223SAlexander Motin #include <sys/module.h>
3489b17223SAlexander Motin #include <sys/limits.h>
3589b17223SAlexander Motin #include <sys/lock.h>
3689b17223SAlexander Motin #include <sys/mutex.h>
3789b17223SAlexander Motin #include <sys/bio.h>
385d807a0eSAndrey V. Elsukov #include <sys/sbuf.h>
3989b17223SAlexander Motin #include <sys/sysctl.h>
4089b17223SAlexander Motin #include <sys/malloc.h>
4189b17223SAlexander Motin #include <sys/eventhandler.h>
4289b17223SAlexander Motin #include <vm/uma.h>
4389b17223SAlexander Motin #include <geom/geom.h>
4489b17223SAlexander Motin #include <sys/proc.h>
4589b17223SAlexander Motin #include <sys/kthread.h>
4689b17223SAlexander Motin #include <sys/sched.h>
4789b17223SAlexander Motin #include <geom/raid/g_raid.h>
4889b17223SAlexander Motin #include "g_raid_md_if.h"
4989b17223SAlexander Motin #include "g_raid_tr_if.h"
5089b17223SAlexander Motin 
5189b17223SAlexander Motin static MALLOC_DEFINE(M_RAID, "raid_data", "GEOM_RAID Data");
5289b17223SAlexander Motin 
5389b17223SAlexander Motin SYSCTL_DECL(_kern_geom);
5489b17223SAlexander Motin SYSCTL_NODE(_kern_geom, OID_AUTO, raid, CTLFLAG_RW, 0, "GEOM_RAID stuff");
5589b17223SAlexander Motin u_int g_raid_aggressive_spare = 0;
5689b17223SAlexander Motin TUNABLE_INT("kern.geom.raid.aggressive_spare", &g_raid_aggressive_spare);
5789b17223SAlexander Motin SYSCTL_UINT(_kern_geom_raid, OID_AUTO, aggressive_spare, CTLFLAG_RW,
5889b17223SAlexander Motin     &g_raid_aggressive_spare, 0, "Use disks without metadata as spare");
59fe51d6c1SAlexander Motin u_int g_raid_debug = 0;
6089b17223SAlexander Motin TUNABLE_INT("kern.geom.raid.debug", &g_raid_debug);
6189b17223SAlexander Motin SYSCTL_UINT(_kern_geom_raid, OID_AUTO, debug, CTLFLAG_RW, &g_raid_debug, 0,
6289b17223SAlexander Motin     "Debug level");
6389b17223SAlexander Motin int g_raid_read_err_thresh = 10;
6489b17223SAlexander Motin TUNABLE_INT("kern.geom.raid.read_err_thresh", &g_raid_read_err_thresh);
6589b17223SAlexander Motin SYSCTL_UINT(_kern_geom_raid, OID_AUTO, read_err_thresh, CTLFLAG_RW,
6689b17223SAlexander Motin     &g_raid_read_err_thresh, 0,
6789b17223SAlexander Motin     "Number of read errors equated to disk failure");
6889b17223SAlexander Motin u_int g_raid_start_timeout = 30;
6989b17223SAlexander Motin TUNABLE_INT("kern.geom.raid.start_timeout", &g_raid_start_timeout);
7089b17223SAlexander Motin SYSCTL_UINT(_kern_geom_raid, OID_AUTO, start_timeout, CTLFLAG_RW,
7189b17223SAlexander Motin     &g_raid_start_timeout, 0,
7289b17223SAlexander Motin     "Time to wait for all array components");
7389b17223SAlexander Motin static u_int g_raid_clean_time = 5;
7489b17223SAlexander Motin TUNABLE_INT("kern.geom.raid.clean_time", &g_raid_clean_time);
7589b17223SAlexander Motin SYSCTL_UINT(_kern_geom_raid, OID_AUTO, clean_time, CTLFLAG_RW,
7689b17223SAlexander Motin     &g_raid_clean_time, 0, "Mark volume as clean when idling");
7789b17223SAlexander Motin static u_int g_raid_disconnect_on_failure = 1;
7889b17223SAlexander Motin TUNABLE_INT("kern.geom.raid.disconnect_on_failure",
7989b17223SAlexander Motin     &g_raid_disconnect_on_failure);
8089b17223SAlexander Motin SYSCTL_UINT(_kern_geom_raid, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
8189b17223SAlexander Motin     &g_raid_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
8289b17223SAlexander Motin static u_int g_raid_name_format = 0;
8389b17223SAlexander Motin TUNABLE_INT("kern.geom.raid.name_format", &g_raid_name_format);
8489b17223SAlexander Motin SYSCTL_UINT(_kern_geom_raid, OID_AUTO, name_format, CTLFLAG_RW,
8589b17223SAlexander Motin     &g_raid_name_format, 0, "Providers name format.");
8689b17223SAlexander Motin static u_int g_raid_idle_threshold = 1000000;
8789b17223SAlexander Motin TUNABLE_INT("kern.geom.raid.idle_threshold", &g_raid_idle_threshold);
8889b17223SAlexander Motin SYSCTL_UINT(_kern_geom_raid, OID_AUTO, idle_threshold, CTLFLAG_RW,
8989b17223SAlexander Motin     &g_raid_idle_threshold, 1000000,
9089b17223SAlexander Motin     "Time in microseconds to consider a volume idle.");
9189b17223SAlexander Motin 
9289b17223SAlexander Motin #define	MSLEEP(rv, ident, mtx, priority, wmesg, timeout)	do {	\
9389b17223SAlexander Motin 	G_RAID_DEBUG(4, "%s: Sleeping %p.", __func__, (ident));		\
9489b17223SAlexander Motin 	rv = msleep((ident), (mtx), (priority), (wmesg), (timeout));	\
9589b17223SAlexander Motin 	G_RAID_DEBUG(4, "%s: Woken up %p.", __func__, (ident));		\
9689b17223SAlexander Motin } while (0)
9789b17223SAlexander Motin 
9889b17223SAlexander Motin LIST_HEAD(, g_raid_md_class) g_raid_md_classes =
9989b17223SAlexander Motin     LIST_HEAD_INITIALIZER(g_raid_md_classes);
10089b17223SAlexander Motin 
10189b17223SAlexander Motin LIST_HEAD(, g_raid_tr_class) g_raid_tr_classes =
10289b17223SAlexander Motin     LIST_HEAD_INITIALIZER(g_raid_tr_classes);
10389b17223SAlexander Motin 
10489b17223SAlexander Motin LIST_HEAD(, g_raid_volume) g_raid_volumes =
10589b17223SAlexander Motin     LIST_HEAD_INITIALIZER(g_raid_volumes);
10689b17223SAlexander Motin 
10789b17223SAlexander Motin static eventhandler_tag g_raid_pre_sync = NULL;
10889b17223SAlexander Motin static int g_raid_started = 0;
10989b17223SAlexander Motin 
11089b17223SAlexander Motin static int g_raid_destroy_geom(struct gctl_req *req, struct g_class *mp,
11189b17223SAlexander Motin     struct g_geom *gp);
11289b17223SAlexander Motin static g_taste_t g_raid_taste;
11389b17223SAlexander Motin static void g_raid_init(struct g_class *mp);
11489b17223SAlexander Motin static void g_raid_fini(struct g_class *mp);
11589b17223SAlexander Motin 
11689b17223SAlexander Motin struct g_class g_raid_class = {
11789b17223SAlexander Motin 	.name = G_RAID_CLASS_NAME,
11889b17223SAlexander Motin 	.version = G_VERSION,
11989b17223SAlexander Motin 	.ctlreq = g_raid_ctl,
12089b17223SAlexander Motin 	.taste = g_raid_taste,
12189b17223SAlexander Motin 	.destroy_geom = g_raid_destroy_geom,
12289b17223SAlexander Motin 	.init = g_raid_init,
12389b17223SAlexander Motin 	.fini = g_raid_fini
12489b17223SAlexander Motin };
12589b17223SAlexander Motin 
12689b17223SAlexander Motin static void g_raid_destroy_provider(struct g_raid_volume *vol);
12789b17223SAlexander Motin static int g_raid_update_disk(struct g_raid_disk *disk, u_int event);
12889b17223SAlexander Motin static int g_raid_update_subdisk(struct g_raid_subdisk *subdisk, u_int event);
12989b17223SAlexander Motin static int g_raid_update_volume(struct g_raid_volume *vol, u_int event);
13089b17223SAlexander Motin static int g_raid_update_node(struct g_raid_softc *sc, u_int event);
13189b17223SAlexander Motin static void g_raid_dumpconf(struct sbuf *sb, const char *indent,
13289b17223SAlexander Motin     struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
13389b17223SAlexander Motin static void g_raid_start(struct bio *bp);
13489b17223SAlexander Motin static void g_raid_start_request(struct bio *bp);
13589b17223SAlexander Motin static void g_raid_disk_done(struct bio *bp);
13689b17223SAlexander Motin static void g_raid_poll(struct g_raid_softc *sc);
13789b17223SAlexander Motin 
13889b17223SAlexander Motin static const char *
13989b17223SAlexander Motin g_raid_node_event2str(int event)
14089b17223SAlexander Motin {
14189b17223SAlexander Motin 
14289b17223SAlexander Motin 	switch (event) {
14389b17223SAlexander Motin 	case G_RAID_NODE_E_WAKE:
14489b17223SAlexander Motin 		return ("WAKE");
14589b17223SAlexander Motin 	case G_RAID_NODE_E_START:
14689b17223SAlexander Motin 		return ("START");
14789b17223SAlexander Motin 	default:
14889b17223SAlexander Motin 		return ("INVALID");
14989b17223SAlexander Motin 	}
15089b17223SAlexander Motin }
15189b17223SAlexander Motin 
15289b17223SAlexander Motin const char *
15389b17223SAlexander Motin g_raid_disk_state2str(int state)
15489b17223SAlexander Motin {
15589b17223SAlexander Motin 
15689b17223SAlexander Motin 	switch (state) {
15789b17223SAlexander Motin 	case G_RAID_DISK_S_NONE:
15889b17223SAlexander Motin 		return ("NONE");
15989b17223SAlexander Motin 	case G_RAID_DISK_S_OFFLINE:
16089b17223SAlexander Motin 		return ("OFFLINE");
16189b17223SAlexander Motin 	case G_RAID_DISK_S_FAILED:
16289b17223SAlexander Motin 		return ("FAILED");
16389b17223SAlexander Motin 	case G_RAID_DISK_S_STALE_FAILED:
16489b17223SAlexander Motin 		return ("STALE_FAILED");
16589b17223SAlexander Motin 	case G_RAID_DISK_S_SPARE:
16689b17223SAlexander Motin 		return ("SPARE");
16789b17223SAlexander Motin 	case G_RAID_DISK_S_STALE:
16889b17223SAlexander Motin 		return ("STALE");
16989b17223SAlexander Motin 	case G_RAID_DISK_S_ACTIVE:
17089b17223SAlexander Motin 		return ("ACTIVE");
17189b17223SAlexander Motin 	default:
17289b17223SAlexander Motin 		return ("INVALID");
17389b17223SAlexander Motin 	}
17489b17223SAlexander Motin }
17589b17223SAlexander Motin 
17689b17223SAlexander Motin static const char *
17789b17223SAlexander Motin g_raid_disk_event2str(int event)
17889b17223SAlexander Motin {
17989b17223SAlexander Motin 
18089b17223SAlexander Motin 	switch (event) {
18189b17223SAlexander Motin 	case G_RAID_DISK_E_DISCONNECTED:
18289b17223SAlexander Motin 		return ("DISCONNECTED");
18389b17223SAlexander Motin 	default:
18489b17223SAlexander Motin 		return ("INVALID");
18589b17223SAlexander Motin 	}
18689b17223SAlexander Motin }
18789b17223SAlexander Motin 
18889b17223SAlexander Motin const char *
18989b17223SAlexander Motin g_raid_subdisk_state2str(int state)
19089b17223SAlexander Motin {
19189b17223SAlexander Motin 
19289b17223SAlexander Motin 	switch (state) {
19389b17223SAlexander Motin 	case G_RAID_SUBDISK_S_NONE:
19489b17223SAlexander Motin 		return ("NONE");
19589b17223SAlexander Motin 	case G_RAID_SUBDISK_S_FAILED:
19689b17223SAlexander Motin 		return ("FAILED");
19789b17223SAlexander Motin 	case G_RAID_SUBDISK_S_NEW:
19889b17223SAlexander Motin 		return ("NEW");
19989b17223SAlexander Motin 	case G_RAID_SUBDISK_S_REBUILD:
20089b17223SAlexander Motin 		return ("REBUILD");
20189b17223SAlexander Motin 	case G_RAID_SUBDISK_S_UNINITIALIZED:
20289b17223SAlexander Motin 		return ("UNINITIALIZED");
20389b17223SAlexander Motin 	case G_RAID_SUBDISK_S_STALE:
20489b17223SAlexander Motin 		return ("STALE");
20589b17223SAlexander Motin 	case G_RAID_SUBDISK_S_RESYNC:
20689b17223SAlexander Motin 		return ("RESYNC");
20789b17223SAlexander Motin 	case G_RAID_SUBDISK_S_ACTIVE:
20889b17223SAlexander Motin 		return ("ACTIVE");
20989b17223SAlexander Motin 	default:
21089b17223SAlexander Motin 		return ("INVALID");
21189b17223SAlexander Motin 	}
21289b17223SAlexander Motin }
21389b17223SAlexander Motin 
21489b17223SAlexander Motin static const char *
21589b17223SAlexander Motin g_raid_subdisk_event2str(int event)
21689b17223SAlexander Motin {
21789b17223SAlexander Motin 
21889b17223SAlexander Motin 	switch (event) {
21989b17223SAlexander Motin 	case G_RAID_SUBDISK_E_NEW:
22089b17223SAlexander Motin 		return ("NEW");
22189b17223SAlexander Motin 	case G_RAID_SUBDISK_E_DISCONNECTED:
22289b17223SAlexander Motin 		return ("DISCONNECTED");
22389b17223SAlexander Motin 	default:
22489b17223SAlexander Motin 		return ("INVALID");
22589b17223SAlexander Motin 	}
22689b17223SAlexander Motin }
22789b17223SAlexander Motin 
22889b17223SAlexander Motin const char *
22989b17223SAlexander Motin g_raid_volume_state2str(int state)
23089b17223SAlexander Motin {
23189b17223SAlexander Motin 
23289b17223SAlexander Motin 	switch (state) {
23389b17223SAlexander Motin 	case G_RAID_VOLUME_S_STARTING:
23489b17223SAlexander Motin 		return ("STARTING");
23589b17223SAlexander Motin 	case G_RAID_VOLUME_S_BROKEN:
23689b17223SAlexander Motin 		return ("BROKEN");
23789b17223SAlexander Motin 	case G_RAID_VOLUME_S_DEGRADED:
23889b17223SAlexander Motin 		return ("DEGRADED");
23989b17223SAlexander Motin 	case G_RAID_VOLUME_S_SUBOPTIMAL:
24089b17223SAlexander Motin 		return ("SUBOPTIMAL");
24189b17223SAlexander Motin 	case G_RAID_VOLUME_S_OPTIMAL:
24289b17223SAlexander Motin 		return ("OPTIMAL");
24389b17223SAlexander Motin 	case G_RAID_VOLUME_S_UNSUPPORTED:
24489b17223SAlexander Motin 		return ("UNSUPPORTED");
24589b17223SAlexander Motin 	case G_RAID_VOLUME_S_STOPPED:
24689b17223SAlexander Motin 		return ("STOPPED");
24789b17223SAlexander Motin 	default:
24889b17223SAlexander Motin 		return ("INVALID");
24989b17223SAlexander Motin 	}
25089b17223SAlexander Motin }
25189b17223SAlexander Motin 
25289b17223SAlexander Motin static const char *
25389b17223SAlexander Motin g_raid_volume_event2str(int event)
25489b17223SAlexander Motin {
25589b17223SAlexander Motin 
25689b17223SAlexander Motin 	switch (event) {
25789b17223SAlexander Motin 	case G_RAID_VOLUME_E_UP:
25889b17223SAlexander Motin 		return ("UP");
25989b17223SAlexander Motin 	case G_RAID_VOLUME_E_DOWN:
26089b17223SAlexander Motin 		return ("DOWN");
26189b17223SAlexander Motin 	case G_RAID_VOLUME_E_START:
26289b17223SAlexander Motin 		return ("START");
26389b17223SAlexander Motin 	case G_RAID_VOLUME_E_STARTMD:
26489b17223SAlexander Motin 		return ("STARTMD");
26589b17223SAlexander Motin 	default:
26689b17223SAlexander Motin 		return ("INVALID");
26789b17223SAlexander Motin 	}
26889b17223SAlexander Motin }
26989b17223SAlexander Motin 
27089b17223SAlexander Motin const char *
27189b17223SAlexander Motin g_raid_volume_level2str(int level, int qual)
27289b17223SAlexander Motin {
27389b17223SAlexander Motin 
27489b17223SAlexander Motin 	switch (level) {
27589b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID0:
27689b17223SAlexander Motin 		return ("RAID0");
27789b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID1:
27889b17223SAlexander Motin 		return ("RAID1");
27989b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID3:
28089b17223SAlexander Motin 		return ("RAID3");
28189b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID4:
28289b17223SAlexander Motin 		return ("RAID4");
28389b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID5:
28489b17223SAlexander Motin 		return ("RAID5");
28589b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID6:
28689b17223SAlexander Motin 		return ("RAID6");
28789b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID1E:
28889b17223SAlexander Motin 		return ("RAID1E");
28989b17223SAlexander Motin 	case G_RAID_VOLUME_RL_SINGLE:
29089b17223SAlexander Motin 		return ("SINGLE");
29189b17223SAlexander Motin 	case G_RAID_VOLUME_RL_CONCAT:
29289b17223SAlexander Motin 		return ("CONCAT");
29389b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID5E:
29489b17223SAlexander Motin 		return ("RAID5E");
29589b17223SAlexander Motin 	case G_RAID_VOLUME_RL_RAID5EE:
29689b17223SAlexander Motin 		return ("RAID5EE");
29789b17223SAlexander Motin 	default:
29889b17223SAlexander Motin 		return ("UNKNOWN");
29989b17223SAlexander Motin 	}
30089b17223SAlexander Motin }
30189b17223SAlexander Motin 
30289b17223SAlexander Motin int
30389b17223SAlexander Motin g_raid_volume_str2level(const char *str, int *level, int *qual)
30489b17223SAlexander Motin {
30589b17223SAlexander Motin 
30689b17223SAlexander Motin 	*level = G_RAID_VOLUME_RL_UNKNOWN;
30789b17223SAlexander Motin 	*qual = G_RAID_VOLUME_RLQ_NONE;
30889b17223SAlexander Motin 	if (strcasecmp(str, "RAID0") == 0)
30989b17223SAlexander Motin 		*level = G_RAID_VOLUME_RL_RAID0;
31089b17223SAlexander Motin 	else if (strcasecmp(str, "RAID1") == 0)
31189b17223SAlexander Motin 		*level = G_RAID_VOLUME_RL_RAID1;
31289b17223SAlexander Motin 	else if (strcasecmp(str, "RAID3") == 0)
31389b17223SAlexander Motin 		*level = G_RAID_VOLUME_RL_RAID3;
31489b17223SAlexander Motin 	else if (strcasecmp(str, "RAID4") == 0)
31589b17223SAlexander Motin 		*level = G_RAID_VOLUME_RL_RAID4;
31689b17223SAlexander Motin 	else if (strcasecmp(str, "RAID5") == 0)
31789b17223SAlexander Motin 		*level = G_RAID_VOLUME_RL_RAID5;
31889b17223SAlexander Motin 	else if (strcasecmp(str, "RAID6") == 0)
31989b17223SAlexander Motin 		*level = G_RAID_VOLUME_RL_RAID6;
32089b17223SAlexander Motin 	else if (strcasecmp(str, "RAID10") == 0 ||
32189b17223SAlexander Motin 		 strcasecmp(str, "RAID1E") == 0)
32289b17223SAlexander Motin 		*level = G_RAID_VOLUME_RL_RAID1E;
32389b17223SAlexander Motin 	else if (strcasecmp(str, "SINGLE") == 0)
32489b17223SAlexander Motin 		*level = G_RAID_VOLUME_RL_SINGLE;
32589b17223SAlexander Motin 	else if (strcasecmp(str, "CONCAT") == 0)
32689b17223SAlexander Motin 		*level = G_RAID_VOLUME_RL_CONCAT;
32789b17223SAlexander Motin 	else if (strcasecmp(str, "RAID5E") == 0)
32889b17223SAlexander Motin 		*level = G_RAID_VOLUME_RL_RAID5E;
32989b17223SAlexander Motin 	else if (strcasecmp(str, "RAID5EE") == 0)
33089b17223SAlexander Motin 		*level = G_RAID_VOLUME_RL_RAID5EE;
33189b17223SAlexander Motin 	else
33289b17223SAlexander Motin 		return (-1);
33389b17223SAlexander Motin 	return (0);
33489b17223SAlexander Motin }
33589b17223SAlexander Motin 
33689b17223SAlexander Motin const char *
33789b17223SAlexander Motin g_raid_get_diskname(struct g_raid_disk *disk)
33889b17223SAlexander Motin {
33989b17223SAlexander Motin 
34089b17223SAlexander Motin 	if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
34189b17223SAlexander Motin 		return ("[unknown]");
34289b17223SAlexander Motin 	return (disk->d_consumer->provider->name);
34389b17223SAlexander Motin }
34489b17223SAlexander Motin 
34589b17223SAlexander Motin void
34689b17223SAlexander Motin g_raid_report_disk_state(struct g_raid_disk *disk)
34789b17223SAlexander Motin {
34889b17223SAlexander Motin 	struct g_raid_subdisk *sd;
34989b17223SAlexander Motin 	int len, state;
35089b17223SAlexander Motin 	uint32_t s;
35189b17223SAlexander Motin 
35289b17223SAlexander Motin 	if (disk->d_consumer == NULL)
35389b17223SAlexander Motin 		return;
35489b17223SAlexander Motin 	if (disk->d_state == G_RAID_DISK_S_FAILED ||
35589b17223SAlexander Motin 	    disk->d_state == G_RAID_DISK_S_STALE_FAILED) {
35689b17223SAlexander Motin 		s = G_STATE_FAILED;
35789b17223SAlexander Motin 	} else {
35889b17223SAlexander Motin 		state = G_RAID_SUBDISK_S_ACTIVE;
35989b17223SAlexander Motin 		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
36089b17223SAlexander Motin 			if (sd->sd_state < state)
36189b17223SAlexander Motin 				state = sd->sd_state;
36289b17223SAlexander Motin 		}
36389b17223SAlexander Motin 		if (state == G_RAID_SUBDISK_S_FAILED)
36489b17223SAlexander Motin 			s = G_STATE_FAILED;
36589b17223SAlexander Motin 		else if (state == G_RAID_SUBDISK_S_NEW ||
36689b17223SAlexander Motin 		    state == G_RAID_SUBDISK_S_REBUILD)
36789b17223SAlexander Motin 			s = G_STATE_REBUILD;
36889b17223SAlexander Motin 		else if (state == G_RAID_SUBDISK_S_STALE ||
36989b17223SAlexander Motin 		    state == G_RAID_SUBDISK_S_RESYNC)
37089b17223SAlexander Motin 			s = G_STATE_RESYNC;
37189b17223SAlexander Motin 		else
37289b17223SAlexander Motin 			s = G_STATE_ACTIVE;
37389b17223SAlexander Motin 	}
37489b17223SAlexander Motin 	len = sizeof(s);
37589b17223SAlexander Motin 	g_io_getattr("GEOM::setstate", disk->d_consumer, &len, &s);
37689b17223SAlexander Motin 	G_RAID_DEBUG1(2, disk->d_softc, "Disk %s state reported as %d.",
37789b17223SAlexander Motin 	    g_raid_get_diskname(disk), s);
37889b17223SAlexander Motin }
37989b17223SAlexander Motin 
38089b17223SAlexander Motin void
38189b17223SAlexander Motin g_raid_change_disk_state(struct g_raid_disk *disk, int state)
38289b17223SAlexander Motin {
38389b17223SAlexander Motin 
38489b17223SAlexander Motin 	G_RAID_DEBUG1(0, disk->d_softc, "Disk %s state changed from %s to %s.",
38589b17223SAlexander Motin 	    g_raid_get_diskname(disk),
38689b17223SAlexander Motin 	    g_raid_disk_state2str(disk->d_state),
38789b17223SAlexander Motin 	    g_raid_disk_state2str(state));
38889b17223SAlexander Motin 	disk->d_state = state;
38989b17223SAlexander Motin 	g_raid_report_disk_state(disk);
39089b17223SAlexander Motin }
39189b17223SAlexander Motin 
39289b17223SAlexander Motin void
39389b17223SAlexander Motin g_raid_change_subdisk_state(struct g_raid_subdisk *sd, int state)
39489b17223SAlexander Motin {
39589b17223SAlexander Motin 
39689b17223SAlexander Motin 	G_RAID_DEBUG1(0, sd->sd_softc,
39789b17223SAlexander Motin 	    "Subdisk %s:%d-%s state changed from %s to %s.",
39889b17223SAlexander Motin 	    sd->sd_volume->v_name, sd->sd_pos,
39989b17223SAlexander Motin 	    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]",
40089b17223SAlexander Motin 	    g_raid_subdisk_state2str(sd->sd_state),
40189b17223SAlexander Motin 	    g_raid_subdisk_state2str(state));
40289b17223SAlexander Motin 	sd->sd_state = state;
40389b17223SAlexander Motin 	if (sd->sd_disk)
40489b17223SAlexander Motin 		g_raid_report_disk_state(sd->sd_disk);
40589b17223SAlexander Motin }
40689b17223SAlexander Motin 
40789b17223SAlexander Motin void
40889b17223SAlexander Motin g_raid_change_volume_state(struct g_raid_volume *vol, int state)
40989b17223SAlexander Motin {
41089b17223SAlexander Motin 
41189b17223SAlexander Motin 	G_RAID_DEBUG1(0, vol->v_softc,
41289b17223SAlexander Motin 	    "Volume %s state changed from %s to %s.",
41389b17223SAlexander Motin 	    vol->v_name,
41489b17223SAlexander Motin 	    g_raid_volume_state2str(vol->v_state),
41589b17223SAlexander Motin 	    g_raid_volume_state2str(state));
41689b17223SAlexander Motin 	vol->v_state = state;
41789b17223SAlexander Motin }
41889b17223SAlexander Motin 
41989b17223SAlexander Motin /*
42089b17223SAlexander Motin  * --- Events handling functions ---
42189b17223SAlexander Motin  * Events in geom_raid are used to maintain subdisks and volumes status
42289b17223SAlexander Motin  * from one thread to simplify locking.
42389b17223SAlexander Motin  */
42489b17223SAlexander Motin static void
42589b17223SAlexander Motin g_raid_event_free(struct g_raid_event *ep)
42689b17223SAlexander Motin {
42789b17223SAlexander Motin 
42889b17223SAlexander Motin 	free(ep, M_RAID);
42989b17223SAlexander Motin }
43089b17223SAlexander Motin 
43189b17223SAlexander Motin int
43289b17223SAlexander Motin g_raid_event_send(void *arg, int event, int flags)
43389b17223SAlexander Motin {
43489b17223SAlexander Motin 	struct g_raid_softc *sc;
43589b17223SAlexander Motin 	struct g_raid_event *ep;
43689b17223SAlexander Motin 	int error;
43789b17223SAlexander Motin 
43889b17223SAlexander Motin 	if ((flags & G_RAID_EVENT_VOLUME) != 0) {
43989b17223SAlexander Motin 		sc = ((struct g_raid_volume *)arg)->v_softc;
44089b17223SAlexander Motin 	} else if ((flags & G_RAID_EVENT_DISK) != 0) {
44189b17223SAlexander Motin 		sc = ((struct g_raid_disk *)arg)->d_softc;
44289b17223SAlexander Motin 	} else if ((flags & G_RAID_EVENT_SUBDISK) != 0) {
44389b17223SAlexander Motin 		sc = ((struct g_raid_subdisk *)arg)->sd_softc;
44489b17223SAlexander Motin 	} else {
44589b17223SAlexander Motin 		sc = arg;
44689b17223SAlexander Motin 	}
44789b17223SAlexander Motin 	ep = malloc(sizeof(*ep), M_RAID,
44889b17223SAlexander Motin 	    sx_xlocked(&sc->sc_lock) ? M_WAITOK : M_NOWAIT);
44989b17223SAlexander Motin 	if (ep == NULL)
45089b17223SAlexander Motin 		return (ENOMEM);
45189b17223SAlexander Motin 	ep->e_tgt = arg;
45289b17223SAlexander Motin 	ep->e_event = event;
45389b17223SAlexander Motin 	ep->e_flags = flags;
45489b17223SAlexander Motin 	ep->e_error = 0;
45589b17223SAlexander Motin 	G_RAID_DEBUG1(4, sc, "Sending event %p. Waking up %p.", ep, sc);
45689b17223SAlexander Motin 	mtx_lock(&sc->sc_queue_mtx);
45789b17223SAlexander Motin 	TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
45889b17223SAlexander Motin 	mtx_unlock(&sc->sc_queue_mtx);
45989b17223SAlexander Motin 	wakeup(sc);
46089b17223SAlexander Motin 
46189b17223SAlexander Motin 	if ((flags & G_RAID_EVENT_WAIT) == 0)
46289b17223SAlexander Motin 		return (0);
46389b17223SAlexander Motin 
46489b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_XLOCKED);
46589b17223SAlexander Motin 	G_RAID_DEBUG1(4, sc, "Sleeping on %p.", ep);
46689b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
46789b17223SAlexander Motin 	while ((ep->e_flags & G_RAID_EVENT_DONE) == 0) {
46889b17223SAlexander Motin 		mtx_lock(&sc->sc_queue_mtx);
46989b17223SAlexander Motin 		MSLEEP(error, ep, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:event",
47089b17223SAlexander Motin 		    hz * 5);
47189b17223SAlexander Motin 	}
47289b17223SAlexander Motin 	error = ep->e_error;
47389b17223SAlexander Motin 	g_raid_event_free(ep);
47489b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
47589b17223SAlexander Motin 	return (error);
47689b17223SAlexander Motin }
47789b17223SAlexander Motin 
47889b17223SAlexander Motin static void
47989b17223SAlexander Motin g_raid_event_cancel(struct g_raid_softc *sc, void *tgt)
48089b17223SAlexander Motin {
48189b17223SAlexander Motin 	struct g_raid_event *ep, *tmpep;
48289b17223SAlexander Motin 
48389b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_XLOCKED);
48489b17223SAlexander Motin 
48589b17223SAlexander Motin 	mtx_lock(&sc->sc_queue_mtx);
48689b17223SAlexander Motin 	TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
48789b17223SAlexander Motin 		if (ep->e_tgt != tgt)
48889b17223SAlexander Motin 			continue;
48989b17223SAlexander Motin 		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
49089b17223SAlexander Motin 		if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0)
49189b17223SAlexander Motin 			g_raid_event_free(ep);
49289b17223SAlexander Motin 		else {
49389b17223SAlexander Motin 			ep->e_error = ECANCELED;
49489b17223SAlexander Motin 			wakeup(ep);
49589b17223SAlexander Motin 		}
49689b17223SAlexander Motin 	}
49789b17223SAlexander Motin 	mtx_unlock(&sc->sc_queue_mtx);
49889b17223SAlexander Motin }
49989b17223SAlexander Motin 
50089b17223SAlexander Motin static int
50189b17223SAlexander Motin g_raid_event_check(struct g_raid_softc *sc, void *tgt)
50289b17223SAlexander Motin {
50389b17223SAlexander Motin 	struct g_raid_event *ep;
50489b17223SAlexander Motin 	int	res = 0;
50589b17223SAlexander Motin 
50689b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_XLOCKED);
50789b17223SAlexander Motin 
50889b17223SAlexander Motin 	mtx_lock(&sc->sc_queue_mtx);
50989b17223SAlexander Motin 	TAILQ_FOREACH(ep, &sc->sc_events, e_next) {
51089b17223SAlexander Motin 		if (ep->e_tgt != tgt)
51189b17223SAlexander Motin 			continue;
51289b17223SAlexander Motin 		res = 1;
51389b17223SAlexander Motin 		break;
51489b17223SAlexander Motin 	}
51589b17223SAlexander Motin 	mtx_unlock(&sc->sc_queue_mtx);
51689b17223SAlexander Motin 	return (res);
51789b17223SAlexander Motin }
51889b17223SAlexander Motin 
51989b17223SAlexander Motin /*
52089b17223SAlexander Motin  * Return the number of disks in given state.
52189b17223SAlexander Motin  * If state is equal to -1, count all connected disks.
52289b17223SAlexander Motin  */
52389b17223SAlexander Motin u_int
52489b17223SAlexander Motin g_raid_ndisks(struct g_raid_softc *sc, int state)
52589b17223SAlexander Motin {
52689b17223SAlexander Motin 	struct g_raid_disk *disk;
52789b17223SAlexander Motin 	u_int n;
52889b17223SAlexander Motin 
52989b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_LOCKED);
53089b17223SAlexander Motin 
53189b17223SAlexander Motin 	n = 0;
53289b17223SAlexander Motin 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
53389b17223SAlexander Motin 		if (disk->d_state == state || state == -1)
53489b17223SAlexander Motin 			n++;
53589b17223SAlexander Motin 	}
53689b17223SAlexander Motin 	return (n);
53789b17223SAlexander Motin }
53889b17223SAlexander Motin 
53989b17223SAlexander Motin /*
54089b17223SAlexander Motin  * Return the number of subdisks in given state.
54189b17223SAlexander Motin  * If state is equal to -1, count all connected disks.
54289b17223SAlexander Motin  */
54389b17223SAlexander Motin u_int
54489b17223SAlexander Motin g_raid_nsubdisks(struct g_raid_volume *vol, int state)
54589b17223SAlexander Motin {
54689b17223SAlexander Motin 	struct g_raid_subdisk *subdisk;
54789b17223SAlexander Motin 	struct g_raid_softc *sc;
54889b17223SAlexander Motin 	u_int i, n ;
54989b17223SAlexander Motin 
55089b17223SAlexander Motin 	sc = vol->v_softc;
55189b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_LOCKED);
55289b17223SAlexander Motin 
55389b17223SAlexander Motin 	n = 0;
55489b17223SAlexander Motin 	for (i = 0; i < vol->v_disks_count; i++) {
55589b17223SAlexander Motin 		subdisk = &vol->v_subdisks[i];
55689b17223SAlexander Motin 		if ((state == -1 &&
55789b17223SAlexander Motin 		     subdisk->sd_state != G_RAID_SUBDISK_S_NONE) ||
55889b17223SAlexander Motin 		    subdisk->sd_state == state)
55989b17223SAlexander Motin 			n++;
56089b17223SAlexander Motin 	}
56189b17223SAlexander Motin 	return (n);
56289b17223SAlexander Motin }
56389b17223SAlexander Motin 
56489b17223SAlexander Motin /*
56589b17223SAlexander Motin  * Return the first subdisk in given state.
56689b17223SAlexander Motin  * If state is equal to -1, then the first connected disks.
56789b17223SAlexander Motin  */
56889b17223SAlexander Motin struct g_raid_subdisk *
56989b17223SAlexander Motin g_raid_get_subdisk(struct g_raid_volume *vol, int state)
57089b17223SAlexander Motin {
57189b17223SAlexander Motin 	struct g_raid_subdisk *sd;
57289b17223SAlexander Motin 	struct g_raid_softc *sc;
57389b17223SAlexander Motin 	u_int i;
57489b17223SAlexander Motin 
57589b17223SAlexander Motin 	sc = vol->v_softc;
57689b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_LOCKED);
57789b17223SAlexander Motin 
57889b17223SAlexander Motin 	for (i = 0; i < vol->v_disks_count; i++) {
57989b17223SAlexander Motin 		sd = &vol->v_subdisks[i];
58089b17223SAlexander Motin 		if ((state == -1 &&
58189b17223SAlexander Motin 		     sd->sd_state != G_RAID_SUBDISK_S_NONE) ||
58289b17223SAlexander Motin 		    sd->sd_state == state)
58389b17223SAlexander Motin 			return (sd);
58489b17223SAlexander Motin 	}
58589b17223SAlexander Motin 	return (NULL);
58689b17223SAlexander Motin }
58789b17223SAlexander Motin 
58889b17223SAlexander Motin struct g_consumer *
58989b17223SAlexander Motin g_raid_open_consumer(struct g_raid_softc *sc, const char *name)
59089b17223SAlexander Motin {
59189b17223SAlexander Motin 	struct g_consumer *cp;
59289b17223SAlexander Motin 	struct g_provider *pp;
59389b17223SAlexander Motin 
59489b17223SAlexander Motin 	g_topology_assert();
59589b17223SAlexander Motin 
59689b17223SAlexander Motin 	if (strncmp(name, "/dev/", 5) == 0)
59789b17223SAlexander Motin 		name += 5;
59889b17223SAlexander Motin 	pp = g_provider_by_name(name);
59989b17223SAlexander Motin 	if (pp == NULL)
60089b17223SAlexander Motin 		return (NULL);
60189b17223SAlexander Motin 	cp = g_new_consumer(sc->sc_geom);
60289b17223SAlexander Motin 	if (g_attach(cp, pp) != 0) {
60389b17223SAlexander Motin 		g_destroy_consumer(cp);
60489b17223SAlexander Motin 		return (NULL);
60589b17223SAlexander Motin 	}
60689b17223SAlexander Motin 	if (g_access(cp, 1, 1, 1) != 0) {
60789b17223SAlexander Motin 		g_detach(cp);
60889b17223SAlexander Motin 		g_destroy_consumer(cp);
60989b17223SAlexander Motin 		return (NULL);
61089b17223SAlexander Motin 	}
61189b17223SAlexander Motin 	return (cp);
61289b17223SAlexander Motin }
61389b17223SAlexander Motin 
61489b17223SAlexander Motin static u_int
61589b17223SAlexander Motin g_raid_nrequests(struct g_raid_softc *sc, struct g_consumer *cp)
61689b17223SAlexander Motin {
61789b17223SAlexander Motin 	struct bio *bp;
61889b17223SAlexander Motin 	u_int nreqs = 0;
61989b17223SAlexander Motin 
62089b17223SAlexander Motin 	mtx_lock(&sc->sc_queue_mtx);
62189b17223SAlexander Motin 	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
62289b17223SAlexander Motin 		if (bp->bio_from == cp)
62389b17223SAlexander Motin 			nreqs++;
62489b17223SAlexander Motin 	}
62589b17223SAlexander Motin 	mtx_unlock(&sc->sc_queue_mtx);
62689b17223SAlexander Motin 	return (nreqs);
62789b17223SAlexander Motin }
62889b17223SAlexander Motin 
62989b17223SAlexander Motin u_int
63089b17223SAlexander Motin g_raid_nopens(struct g_raid_softc *sc)
63189b17223SAlexander Motin {
63289b17223SAlexander Motin 	struct g_raid_volume *vol;
63389b17223SAlexander Motin 	u_int opens;
63489b17223SAlexander Motin 
63589b17223SAlexander Motin 	opens = 0;
63689b17223SAlexander Motin 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
63789b17223SAlexander Motin 		if (vol->v_provider_open != 0)
63889b17223SAlexander Motin 			opens++;
63989b17223SAlexander Motin 	}
64089b17223SAlexander Motin 	return (opens);
64189b17223SAlexander Motin }
64289b17223SAlexander Motin 
64389b17223SAlexander Motin static int
64489b17223SAlexander Motin g_raid_consumer_is_busy(struct g_raid_softc *sc, struct g_consumer *cp)
64589b17223SAlexander Motin {
64689b17223SAlexander Motin 
64789b17223SAlexander Motin 	if (cp->index > 0) {
64889b17223SAlexander Motin 		G_RAID_DEBUG1(2, sc,
64989b17223SAlexander Motin 		    "I/O requests for %s exist, can't destroy it now.",
65089b17223SAlexander Motin 		    cp->provider->name);
65189b17223SAlexander Motin 		return (1);
65289b17223SAlexander Motin 	}
65389b17223SAlexander Motin 	if (g_raid_nrequests(sc, cp) > 0) {
65489b17223SAlexander Motin 		G_RAID_DEBUG1(2, sc,
65589b17223SAlexander Motin 		    "I/O requests for %s in queue, can't destroy it now.",
65689b17223SAlexander Motin 		    cp->provider->name);
65789b17223SAlexander Motin 		return (1);
65889b17223SAlexander Motin 	}
65989b17223SAlexander Motin 	return (0);
66089b17223SAlexander Motin }
66189b17223SAlexander Motin 
66289b17223SAlexander Motin static void
66389b17223SAlexander Motin g_raid_destroy_consumer(void *arg, int flags __unused)
66489b17223SAlexander Motin {
66589b17223SAlexander Motin 	struct g_consumer *cp;
66689b17223SAlexander Motin 
66789b17223SAlexander Motin 	g_topology_assert();
66889b17223SAlexander Motin 
66989b17223SAlexander Motin 	cp = arg;
67089b17223SAlexander Motin 	G_RAID_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
67189b17223SAlexander Motin 	g_detach(cp);
67289b17223SAlexander Motin 	g_destroy_consumer(cp);
67389b17223SAlexander Motin }
67489b17223SAlexander Motin 
67589b17223SAlexander Motin void
67689b17223SAlexander Motin g_raid_kill_consumer(struct g_raid_softc *sc, struct g_consumer *cp)
67789b17223SAlexander Motin {
67889b17223SAlexander Motin 	struct g_provider *pp;
67989b17223SAlexander Motin 	int retaste_wait;
68089b17223SAlexander Motin 
68189b17223SAlexander Motin 	g_topology_assert_not();
68289b17223SAlexander Motin 
68389b17223SAlexander Motin 	g_topology_lock();
68489b17223SAlexander Motin 	cp->private = NULL;
68589b17223SAlexander Motin 	if (g_raid_consumer_is_busy(sc, cp))
68689b17223SAlexander Motin 		goto out;
68789b17223SAlexander Motin 	pp = cp->provider;
68889b17223SAlexander Motin 	retaste_wait = 0;
68989b17223SAlexander Motin 	if (cp->acw == 1) {
69089b17223SAlexander Motin 		if ((pp->geom->flags & G_GEOM_WITHER) == 0)
69189b17223SAlexander Motin 			retaste_wait = 1;
69289b17223SAlexander Motin 	}
69389b17223SAlexander Motin 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
69489b17223SAlexander Motin 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
69589b17223SAlexander Motin 	if (retaste_wait) {
69689b17223SAlexander Motin 		/*
69789b17223SAlexander Motin 		 * After retaste event was send (inside g_access()), we can send
69889b17223SAlexander Motin 		 * event to detach and destroy consumer.
69989b17223SAlexander Motin 		 * A class, which has consumer to the given provider connected
70089b17223SAlexander Motin 		 * will not receive retaste event for the provider.
70189b17223SAlexander Motin 		 * This is the way how I ignore retaste events when I close
70289b17223SAlexander Motin 		 * consumers opened for write: I detach and destroy consumer
70389b17223SAlexander Motin 		 * after retaste event is sent.
70489b17223SAlexander Motin 		 */
70589b17223SAlexander Motin 		g_post_event(g_raid_destroy_consumer, cp, M_WAITOK, NULL);
70689b17223SAlexander Motin 		goto out;
70789b17223SAlexander Motin 	}
70889b17223SAlexander Motin 	G_RAID_DEBUG(1, "Consumer %s destroyed.", pp->name);
70989b17223SAlexander Motin 	g_detach(cp);
71089b17223SAlexander Motin 	g_destroy_consumer(cp);
71189b17223SAlexander Motin out:
71289b17223SAlexander Motin 	g_topology_unlock();
71389b17223SAlexander Motin }
71489b17223SAlexander Motin 
71589b17223SAlexander Motin static void
71689b17223SAlexander Motin g_raid_orphan(struct g_consumer *cp)
71789b17223SAlexander Motin {
71889b17223SAlexander Motin 	struct g_raid_disk *disk;
71989b17223SAlexander Motin 
72089b17223SAlexander Motin 	g_topology_assert();
72189b17223SAlexander Motin 
72289b17223SAlexander Motin 	disk = cp->private;
72389b17223SAlexander Motin 	if (disk == NULL)
72489b17223SAlexander Motin 		return;
72589b17223SAlexander Motin 	g_raid_event_send(disk, G_RAID_DISK_E_DISCONNECTED,
72689b17223SAlexander Motin 	    G_RAID_EVENT_DISK);
72789b17223SAlexander Motin }
72889b17223SAlexander Motin 
72989b17223SAlexander Motin static int
73089b17223SAlexander Motin g_raid_clean(struct g_raid_volume *vol, int acw)
73189b17223SAlexander Motin {
73289b17223SAlexander Motin 	struct g_raid_softc *sc;
73389b17223SAlexander Motin 	int timeout;
73489b17223SAlexander Motin 
73589b17223SAlexander Motin 	sc = vol->v_softc;
73689b17223SAlexander Motin 	g_topology_assert_not();
73789b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_XLOCKED);
73889b17223SAlexander Motin 
73989b17223SAlexander Motin //	if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
74089b17223SAlexander Motin //		return (0);
74189b17223SAlexander Motin 	if (!vol->v_dirty)
74289b17223SAlexander Motin 		return (0);
74389b17223SAlexander Motin 	if (vol->v_writes > 0)
74489b17223SAlexander Motin 		return (0);
74589b17223SAlexander Motin 	if (acw > 0 || (acw == -1 &&
74689b17223SAlexander Motin 	    vol->v_provider != NULL && vol->v_provider->acw > 0)) {
74789b17223SAlexander Motin 		timeout = g_raid_clean_time - (time_uptime - vol->v_last_write);
74889b17223SAlexander Motin 		if (timeout > 0)
74989b17223SAlexander Motin 			return (timeout);
75089b17223SAlexander Motin 	}
75189b17223SAlexander Motin 	vol->v_dirty = 0;
75289b17223SAlexander Motin 	G_RAID_DEBUG1(1, sc, "Volume %s marked as clean.",
75389b17223SAlexander Motin 	    vol->v_name);
75489b17223SAlexander Motin 	g_raid_write_metadata(sc, vol, NULL, NULL);
75589b17223SAlexander Motin 	return (0);
75689b17223SAlexander Motin }
75789b17223SAlexander Motin 
75889b17223SAlexander Motin static void
75989b17223SAlexander Motin g_raid_dirty(struct g_raid_volume *vol)
76089b17223SAlexander Motin {
76189b17223SAlexander Motin 	struct g_raid_softc *sc;
76289b17223SAlexander Motin 
76389b17223SAlexander Motin 	sc = vol->v_softc;
76489b17223SAlexander Motin 	g_topology_assert_not();
76589b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_XLOCKED);
76689b17223SAlexander Motin 
76789b17223SAlexander Motin //	if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
76889b17223SAlexander Motin //		return;
76989b17223SAlexander Motin 	vol->v_dirty = 1;
77089b17223SAlexander Motin 	G_RAID_DEBUG1(1, sc, "Volume %s marked as dirty.",
77189b17223SAlexander Motin 	    vol->v_name);
77289b17223SAlexander Motin 	g_raid_write_metadata(sc, vol, NULL, NULL);
77389b17223SAlexander Motin }
77489b17223SAlexander Motin 
77589b17223SAlexander Motin void
77689b17223SAlexander Motin g_raid_tr_flush_common(struct g_raid_tr_object *tr, struct bio *bp)
77789b17223SAlexander Motin {
77889b17223SAlexander Motin 	struct g_raid_softc *sc;
77989b17223SAlexander Motin 	struct g_raid_volume *vol;
78089b17223SAlexander Motin 	struct g_raid_subdisk *sd;
78189b17223SAlexander Motin 	struct bio_queue_head queue;
78289b17223SAlexander Motin 	struct bio *cbp;
78389b17223SAlexander Motin 	int i;
78489b17223SAlexander Motin 
78589b17223SAlexander Motin 	vol = tr->tro_volume;
78689b17223SAlexander Motin 	sc = vol->v_softc;
78789b17223SAlexander Motin 
78889b17223SAlexander Motin 	/*
78989b17223SAlexander Motin 	 * Allocate all bios before sending any request, so we can return
79089b17223SAlexander Motin 	 * ENOMEM in nice and clean way.
79189b17223SAlexander Motin 	 */
79289b17223SAlexander Motin 	bioq_init(&queue);
79389b17223SAlexander Motin 	for (i = 0; i < vol->v_disks_count; i++) {
79489b17223SAlexander Motin 		sd = &vol->v_subdisks[i];
79589b17223SAlexander Motin 		if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
79689b17223SAlexander Motin 		    sd->sd_state == G_RAID_SUBDISK_S_FAILED)
79789b17223SAlexander Motin 			continue;
79889b17223SAlexander Motin 		cbp = g_clone_bio(bp);
79989b17223SAlexander Motin 		if (cbp == NULL)
80089b17223SAlexander Motin 			goto failure;
80189b17223SAlexander Motin 		cbp->bio_caller1 = sd;
80289b17223SAlexander Motin 		bioq_insert_tail(&queue, cbp);
80389b17223SAlexander Motin 	}
80489b17223SAlexander Motin 	for (cbp = bioq_first(&queue); cbp != NULL;
80589b17223SAlexander Motin 	    cbp = bioq_first(&queue)) {
80689b17223SAlexander Motin 		bioq_remove(&queue, cbp);
80789b17223SAlexander Motin 		sd = cbp->bio_caller1;
80889b17223SAlexander Motin 		cbp->bio_caller1 = NULL;
80989b17223SAlexander Motin 		g_raid_subdisk_iostart(sd, cbp);
81089b17223SAlexander Motin 	}
81189b17223SAlexander Motin 	return;
81289b17223SAlexander Motin failure:
81389b17223SAlexander Motin 	for (cbp = bioq_first(&queue); cbp != NULL;
81489b17223SAlexander Motin 	    cbp = bioq_first(&queue)) {
81589b17223SAlexander Motin 		bioq_remove(&queue, cbp);
81689b17223SAlexander Motin 		g_destroy_bio(cbp);
81789b17223SAlexander Motin 	}
81889b17223SAlexander Motin 	if (bp->bio_error == 0)
81989b17223SAlexander Motin 		bp->bio_error = ENOMEM;
82089b17223SAlexander Motin 	g_raid_iodone(bp, bp->bio_error);
82189b17223SAlexander Motin }
82289b17223SAlexander Motin 
82389b17223SAlexander Motin static void
82489b17223SAlexander Motin g_raid_tr_kerneldump_common_done(struct bio *bp)
82589b17223SAlexander Motin {
82689b17223SAlexander Motin 
82789b17223SAlexander Motin 	bp->bio_flags |= BIO_DONE;
82889b17223SAlexander Motin }
82989b17223SAlexander Motin 
83089b17223SAlexander Motin int
83189b17223SAlexander Motin g_raid_tr_kerneldump_common(struct g_raid_tr_object *tr,
83289b17223SAlexander Motin     void *virtual, vm_offset_t physical, off_t offset, size_t length)
83389b17223SAlexander Motin {
83489b17223SAlexander Motin 	struct g_raid_softc *sc;
83589b17223SAlexander Motin 	struct g_raid_volume *vol;
83689b17223SAlexander Motin 	struct bio bp;
83789b17223SAlexander Motin 
83889b17223SAlexander Motin 	vol = tr->tro_volume;
83989b17223SAlexander Motin 	sc = vol->v_softc;
84089b17223SAlexander Motin 
84189b17223SAlexander Motin 	bzero(&bp, sizeof(bp));
84289b17223SAlexander Motin 	bp.bio_cmd = BIO_WRITE;
84389b17223SAlexander Motin 	bp.bio_done = g_raid_tr_kerneldump_common_done;
84489b17223SAlexander Motin 	bp.bio_attribute = NULL;
84589b17223SAlexander Motin 	bp.bio_offset = offset;
84689b17223SAlexander Motin 	bp.bio_length = length;
84789b17223SAlexander Motin 	bp.bio_data = virtual;
84889b17223SAlexander Motin 	bp.bio_to = vol->v_provider;
84989b17223SAlexander Motin 
85089b17223SAlexander Motin 	g_raid_start(&bp);
85189b17223SAlexander Motin 	while (!(bp.bio_flags & BIO_DONE)) {
85289b17223SAlexander Motin 		G_RAID_DEBUG1(4, sc, "Poll...");
85389b17223SAlexander Motin 		g_raid_poll(sc);
85489b17223SAlexander Motin 		DELAY(10);
85589b17223SAlexander Motin 	}
85689b17223SAlexander Motin 
85789b17223SAlexander Motin 	return (bp.bio_error != 0 ? EIO : 0);
85889b17223SAlexander Motin }
85989b17223SAlexander Motin 
86089b17223SAlexander Motin static int
86189b17223SAlexander Motin g_raid_dump(void *arg,
86289b17223SAlexander Motin     void *virtual, vm_offset_t physical, off_t offset, size_t length)
86389b17223SAlexander Motin {
86489b17223SAlexander Motin 	struct g_raid_volume *vol;
86589b17223SAlexander Motin 	int error;
86689b17223SAlexander Motin 
86789b17223SAlexander Motin 	vol = (struct g_raid_volume *)arg;
86889b17223SAlexander Motin 	G_RAID_DEBUG1(3, vol->v_softc, "Dumping at off %llu len %llu.",
86989b17223SAlexander Motin 	    (long long unsigned)offset, (long long unsigned)length);
87089b17223SAlexander Motin 
87189b17223SAlexander Motin 	error = G_RAID_TR_KERNELDUMP(vol->v_tr,
87289b17223SAlexander Motin 	    virtual, physical, offset, length);
87389b17223SAlexander Motin 	return (error);
87489b17223SAlexander Motin }
87589b17223SAlexander Motin 
87689b17223SAlexander Motin static void
87789b17223SAlexander Motin g_raid_kerneldump(struct g_raid_softc *sc, struct bio *bp)
87889b17223SAlexander Motin {
87989b17223SAlexander Motin 	struct g_kerneldump *gkd;
88089b17223SAlexander Motin 	struct g_provider *pp;
88189b17223SAlexander Motin 	struct g_raid_volume *vol;
88289b17223SAlexander Motin 
88389b17223SAlexander Motin 	gkd = (struct g_kerneldump*)bp->bio_data;
88489b17223SAlexander Motin 	pp = bp->bio_to;
88589b17223SAlexander Motin 	vol = pp->private;
88689b17223SAlexander Motin 	g_trace(G_T_TOPOLOGY, "g_raid_kerneldump(%s, %jd, %jd)",
88789b17223SAlexander Motin 		pp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
88889b17223SAlexander Motin 	gkd->di.dumper = g_raid_dump;
88989b17223SAlexander Motin 	gkd->di.priv = vol;
89089b17223SAlexander Motin 	gkd->di.blocksize = vol->v_sectorsize;
89189b17223SAlexander Motin 	gkd->di.maxiosize = DFLTPHYS;
89289b17223SAlexander Motin 	gkd->di.mediaoffset = gkd->offset;
89389b17223SAlexander Motin 	if ((gkd->offset + gkd->length) > vol->v_mediasize)
89489b17223SAlexander Motin 		gkd->length = vol->v_mediasize - gkd->offset;
89589b17223SAlexander Motin 	gkd->di.mediasize = gkd->length;
89689b17223SAlexander Motin 	g_io_deliver(bp, 0);
89789b17223SAlexander Motin }
89889b17223SAlexander Motin 
89989b17223SAlexander Motin static void
90089b17223SAlexander Motin g_raid_start(struct bio *bp)
90189b17223SAlexander Motin {
90289b17223SAlexander Motin 	struct g_raid_softc *sc;
90389b17223SAlexander Motin 
90489b17223SAlexander Motin 	sc = bp->bio_to->geom->softc;
90589b17223SAlexander Motin 	/*
90689b17223SAlexander Motin 	 * If sc == NULL or there are no valid disks, provider's error
90789b17223SAlexander Motin 	 * should be set and g_raid_start() should not be called at all.
90889b17223SAlexander Motin 	 */
90989b17223SAlexander Motin //	KASSERT(sc != NULL && sc->sc_state == G_RAID_VOLUME_S_RUNNING,
91089b17223SAlexander Motin //	    ("Provider's error should be set (error=%d)(mirror=%s).",
91189b17223SAlexander Motin //	    bp->bio_to->error, bp->bio_to->name));
91289b17223SAlexander Motin 	G_RAID_LOGREQ(3, bp, "Request received.");
91389b17223SAlexander Motin 
91489b17223SAlexander Motin 	switch (bp->bio_cmd) {
91589b17223SAlexander Motin 	case BIO_READ:
91689b17223SAlexander Motin 	case BIO_WRITE:
91789b17223SAlexander Motin 	case BIO_DELETE:
91889b17223SAlexander Motin 	case BIO_FLUSH:
91989b17223SAlexander Motin 		break;
92089b17223SAlexander Motin 	case BIO_GETATTR:
92189b17223SAlexander Motin 		if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
92289b17223SAlexander Motin 			g_raid_kerneldump(sc, bp);
92389b17223SAlexander Motin 		else
92489b17223SAlexander Motin 			g_io_deliver(bp, EOPNOTSUPP);
92589b17223SAlexander Motin 		return;
92689b17223SAlexander Motin 	default:
92789b17223SAlexander Motin 		g_io_deliver(bp, EOPNOTSUPP);
92889b17223SAlexander Motin 		return;
92989b17223SAlexander Motin 	}
93089b17223SAlexander Motin 	mtx_lock(&sc->sc_queue_mtx);
93189b17223SAlexander Motin 	bioq_disksort(&sc->sc_queue, bp);
93289b17223SAlexander Motin 	mtx_unlock(&sc->sc_queue_mtx);
93389b17223SAlexander Motin 	if (!dumping) {
93489b17223SAlexander Motin 		G_RAID_DEBUG1(4, sc, "Waking up %p.", sc);
93589b17223SAlexander Motin 		wakeup(sc);
93689b17223SAlexander Motin 	}
93789b17223SAlexander Motin }
93889b17223SAlexander Motin 
93989b17223SAlexander Motin static int
94089b17223SAlexander Motin g_raid_bio_overlaps(const struct bio *bp, off_t lstart, off_t len)
94189b17223SAlexander Motin {
94289b17223SAlexander Motin 	/*
94389b17223SAlexander Motin 	 * 5 cases:
94489b17223SAlexander Motin 	 * (1) bp entirely below NO
94589b17223SAlexander Motin 	 * (2) bp entirely above NO
94689b17223SAlexander Motin 	 * (3) bp start below, but end in range YES
94789b17223SAlexander Motin 	 * (4) bp entirely within YES
94889b17223SAlexander Motin 	 * (5) bp starts within, ends above YES
94989b17223SAlexander Motin 	 *
95089b17223SAlexander Motin 	 * lock range 10-19 (offset 10 length 10)
95189b17223SAlexander Motin 	 * (1) 1-5: first if kicks it out
95289b17223SAlexander Motin 	 * (2) 30-35: second if kicks it out
95389b17223SAlexander Motin 	 * (3) 5-15: passes both ifs
95489b17223SAlexander Motin 	 * (4) 12-14: passes both ifs
95589b17223SAlexander Motin 	 * (5) 19-20: passes both
95689b17223SAlexander Motin 	 */
95789b17223SAlexander Motin 	off_t lend = lstart + len - 1;
95889b17223SAlexander Motin 	off_t bstart = bp->bio_offset;
95989b17223SAlexander Motin 	off_t bend = bp->bio_offset + bp->bio_length - 1;
96089b17223SAlexander Motin 
96189b17223SAlexander Motin 	if (bend < lstart)
96289b17223SAlexander Motin 		return (0);
96389b17223SAlexander Motin 	if (lend < bstart)
96489b17223SAlexander Motin 		return (0);
96589b17223SAlexander Motin 	return (1);
96689b17223SAlexander Motin }
96789b17223SAlexander Motin 
96889b17223SAlexander Motin static int
96989b17223SAlexander Motin g_raid_is_in_locked_range(struct g_raid_volume *vol, const struct bio *bp)
97089b17223SAlexander Motin {
97189b17223SAlexander Motin 	struct g_raid_lock *lp;
97289b17223SAlexander Motin 
97389b17223SAlexander Motin 	sx_assert(&vol->v_softc->sc_lock, SX_LOCKED);
97489b17223SAlexander Motin 
97589b17223SAlexander Motin 	LIST_FOREACH(lp, &vol->v_locks, l_next) {
97689b17223SAlexander Motin 		if (g_raid_bio_overlaps(bp, lp->l_offset, lp->l_length))
97789b17223SAlexander Motin 			return (1);
97889b17223SAlexander Motin 	}
97989b17223SAlexander Motin 	return (0);
98089b17223SAlexander Motin }
98189b17223SAlexander Motin 
98289b17223SAlexander Motin static void
98389b17223SAlexander Motin g_raid_start_request(struct bio *bp)
98489b17223SAlexander Motin {
98589b17223SAlexander Motin 	struct g_raid_softc *sc;
98689b17223SAlexander Motin 	struct g_raid_volume *vol;
98789b17223SAlexander Motin 
98889b17223SAlexander Motin 	sc = bp->bio_to->geom->softc;
98989b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_LOCKED);
99089b17223SAlexander Motin 	vol = bp->bio_to->private;
99189b17223SAlexander Motin 
99289b17223SAlexander Motin 	/*
99389b17223SAlexander Motin 	 * Check to see if this item is in a locked range.  If so,
99489b17223SAlexander Motin 	 * queue it to our locked queue and return.  We'll requeue
99589b17223SAlexander Motin 	 * it when the range is unlocked.  Internal I/O for the
99689b17223SAlexander Motin 	 * rebuild/rescan/recovery process is excluded from this
99789b17223SAlexander Motin 	 * check so we can actually do the recovery.
99889b17223SAlexander Motin 	 */
99989b17223SAlexander Motin 	if (!(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL) &&
100089b17223SAlexander Motin 	    g_raid_is_in_locked_range(vol, bp)) {
100189b17223SAlexander Motin 		G_RAID_LOGREQ(3, bp, "Defer request.");
100289b17223SAlexander Motin 		bioq_insert_tail(&vol->v_locked, bp);
100389b17223SAlexander Motin 		return;
100489b17223SAlexander Motin 	}
100589b17223SAlexander Motin 
100689b17223SAlexander Motin 	/*
100789b17223SAlexander Motin 	 * If we're actually going to do the write/delete, then
100889b17223SAlexander Motin 	 * update the idle stats for the volume.
100989b17223SAlexander Motin 	 */
101089b17223SAlexander Motin 	if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
101189b17223SAlexander Motin 		if (!vol->v_dirty)
101289b17223SAlexander Motin 			g_raid_dirty(vol);
101389b17223SAlexander Motin 		vol->v_writes++;
101489b17223SAlexander Motin 	}
101589b17223SAlexander Motin 
101689b17223SAlexander Motin 	/*
101789b17223SAlexander Motin 	 * Put request onto inflight queue, so we can check if new
101889b17223SAlexander Motin 	 * synchronization requests don't collide with it.  Then tell
101989b17223SAlexander Motin 	 * the transformation layer to start the I/O.
102089b17223SAlexander Motin 	 */
102189b17223SAlexander Motin 	bioq_insert_tail(&vol->v_inflight, bp);
102289b17223SAlexander Motin 	G_RAID_LOGREQ(4, bp, "Request started");
102389b17223SAlexander Motin 	G_RAID_TR_IOSTART(vol->v_tr, bp);
102489b17223SAlexander Motin }
102589b17223SAlexander Motin 
102689b17223SAlexander Motin static void
102789b17223SAlexander Motin g_raid_finish_with_locked_ranges(struct g_raid_volume *vol, struct bio *bp)
102889b17223SAlexander Motin {
102989b17223SAlexander Motin 	off_t off, len;
103089b17223SAlexander Motin 	struct bio *nbp;
103189b17223SAlexander Motin 	struct g_raid_lock *lp;
103289b17223SAlexander Motin 
103389b17223SAlexander Motin 	vol->v_pending_lock = 0;
103489b17223SAlexander Motin 	LIST_FOREACH(lp, &vol->v_locks, l_next) {
103589b17223SAlexander Motin 		if (lp->l_pending) {
103689b17223SAlexander Motin 			off = lp->l_offset;
103789b17223SAlexander Motin 			len = lp->l_length;
103889b17223SAlexander Motin 			lp->l_pending = 0;
103989b17223SAlexander Motin 			TAILQ_FOREACH(nbp, &vol->v_inflight.queue, bio_queue) {
104089b17223SAlexander Motin 				if (g_raid_bio_overlaps(nbp, off, len))
104189b17223SAlexander Motin 					lp->l_pending++;
104289b17223SAlexander Motin 			}
104389b17223SAlexander Motin 			if (lp->l_pending) {
104489b17223SAlexander Motin 				vol->v_pending_lock = 1;
104589b17223SAlexander Motin 				G_RAID_DEBUG1(4, vol->v_softc,
104689b17223SAlexander Motin 				    "Deferred lock(%jd, %jd) has %d pending",
104789b17223SAlexander Motin 				    (intmax_t)off, (intmax_t)(off + len),
104889b17223SAlexander Motin 				    lp->l_pending);
104989b17223SAlexander Motin 				continue;
105089b17223SAlexander Motin 			}
105189b17223SAlexander Motin 			G_RAID_DEBUG1(4, vol->v_softc,
105289b17223SAlexander Motin 			    "Deferred lock of %jd to %jd completed",
105389b17223SAlexander Motin 			    (intmax_t)off, (intmax_t)(off + len));
105489b17223SAlexander Motin 			G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
105589b17223SAlexander Motin 		}
105689b17223SAlexander Motin 	}
105789b17223SAlexander Motin }
105889b17223SAlexander Motin 
105989b17223SAlexander Motin void
106089b17223SAlexander Motin g_raid_iodone(struct bio *bp, int error)
106189b17223SAlexander Motin {
106289b17223SAlexander Motin 	struct g_raid_softc *sc;
106389b17223SAlexander Motin 	struct g_raid_volume *vol;
106489b17223SAlexander Motin 
106589b17223SAlexander Motin 	sc = bp->bio_to->geom->softc;
106689b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_LOCKED);
106789b17223SAlexander Motin 	vol = bp->bio_to->private;
106889b17223SAlexander Motin 	G_RAID_LOGREQ(3, bp, "Request done: %d.", error);
106989b17223SAlexander Motin 
107089b17223SAlexander Motin 	/* Update stats if we done write/delete. */
107189b17223SAlexander Motin 	if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
107289b17223SAlexander Motin 		vol->v_writes--;
107389b17223SAlexander Motin 		vol->v_last_write = time_uptime;
107489b17223SAlexander Motin 	}
107589b17223SAlexander Motin 
107689b17223SAlexander Motin 	bioq_remove(&vol->v_inflight, bp);
107789b17223SAlexander Motin 	if (vol->v_pending_lock && g_raid_is_in_locked_range(vol, bp))
107889b17223SAlexander Motin 		g_raid_finish_with_locked_ranges(vol, bp);
107989b17223SAlexander Motin 	getmicrouptime(&vol->v_last_done);
108089b17223SAlexander Motin 	g_io_deliver(bp, error);
108189b17223SAlexander Motin }
108289b17223SAlexander Motin 
108389b17223SAlexander Motin int
108489b17223SAlexander Motin g_raid_lock_range(struct g_raid_volume *vol, off_t off, off_t len,
108589b17223SAlexander Motin     struct bio *ignore, void *argp)
108689b17223SAlexander Motin {
108789b17223SAlexander Motin 	struct g_raid_softc *sc;
108889b17223SAlexander Motin 	struct g_raid_lock *lp;
108989b17223SAlexander Motin 	struct bio *bp;
109089b17223SAlexander Motin 
109189b17223SAlexander Motin 	sc = vol->v_softc;
109289b17223SAlexander Motin 	lp = malloc(sizeof(*lp), M_RAID, M_WAITOK | M_ZERO);
109389b17223SAlexander Motin 	LIST_INSERT_HEAD(&vol->v_locks, lp, l_next);
109489b17223SAlexander Motin 	lp->l_offset = off;
109589b17223SAlexander Motin 	lp->l_length = len;
109689b17223SAlexander Motin 	lp->l_callback_arg = argp;
109789b17223SAlexander Motin 
109889b17223SAlexander Motin 	lp->l_pending = 0;
109989b17223SAlexander Motin 	TAILQ_FOREACH(bp, &vol->v_inflight.queue, bio_queue) {
110089b17223SAlexander Motin 		if (bp != ignore && g_raid_bio_overlaps(bp, off, len))
110189b17223SAlexander Motin 			lp->l_pending++;
110289b17223SAlexander Motin 	}
110389b17223SAlexander Motin 
110489b17223SAlexander Motin 	/*
110589b17223SAlexander Motin 	 * If there are any writes that are pending, we return EBUSY.  All
110689b17223SAlexander Motin 	 * callers will have to wait until all pending writes clear.
110789b17223SAlexander Motin 	 */
110889b17223SAlexander Motin 	if (lp->l_pending > 0) {
110989b17223SAlexander Motin 		vol->v_pending_lock = 1;
111089b17223SAlexander Motin 		G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd deferred %d pend",
111189b17223SAlexander Motin 		    (intmax_t)off, (intmax_t)(off+len), lp->l_pending);
111289b17223SAlexander Motin 		return (EBUSY);
111389b17223SAlexander Motin 	}
111489b17223SAlexander Motin 	G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd",
111589b17223SAlexander Motin 	    (intmax_t)off, (intmax_t)(off+len));
111689b17223SAlexander Motin 	G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
111789b17223SAlexander Motin 	return (0);
111889b17223SAlexander Motin }
111989b17223SAlexander Motin 
112089b17223SAlexander Motin int
112189b17223SAlexander Motin g_raid_unlock_range(struct g_raid_volume *vol, off_t off, off_t len)
112289b17223SAlexander Motin {
112389b17223SAlexander Motin 	struct g_raid_lock *lp;
112489b17223SAlexander Motin 	struct g_raid_softc *sc;
112589b17223SAlexander Motin 	struct bio *bp;
112689b17223SAlexander Motin 
112789b17223SAlexander Motin 	sc = vol->v_softc;
112889b17223SAlexander Motin 	LIST_FOREACH(lp, &vol->v_locks, l_next) {
112989b17223SAlexander Motin 		if (lp->l_offset == off && lp->l_length == len) {
113089b17223SAlexander Motin 			LIST_REMOVE(lp, l_next);
113189b17223SAlexander Motin 			/* XXX
113289b17223SAlexander Motin 			 * Right now we just put them all back on the queue
113389b17223SAlexander Motin 			 * and hope for the best.  We hope this because any
113489b17223SAlexander Motin 			 * locked ranges will go right back on this list
113589b17223SAlexander Motin 			 * when the worker thread runs.
113689b17223SAlexander Motin 			 * XXX
113789b17223SAlexander Motin 			 */
113889b17223SAlexander Motin 			G_RAID_DEBUG1(4, sc, "Unlocked %jd to %jd",
113989b17223SAlexander Motin 			    (intmax_t)lp->l_offset,
114089b17223SAlexander Motin 			    (intmax_t)(lp->l_offset+lp->l_length));
114189b17223SAlexander Motin 			mtx_lock(&sc->sc_queue_mtx);
114289b17223SAlexander Motin 			while ((bp = bioq_takefirst(&vol->v_locked)) != NULL)
114389b17223SAlexander Motin 				bioq_disksort(&sc->sc_queue, bp);
114489b17223SAlexander Motin 			mtx_unlock(&sc->sc_queue_mtx);
114589b17223SAlexander Motin 			free(lp, M_RAID);
114689b17223SAlexander Motin 			return (0);
114789b17223SAlexander Motin 		}
114889b17223SAlexander Motin 	}
114989b17223SAlexander Motin 	return (EINVAL);
115089b17223SAlexander Motin }
115189b17223SAlexander Motin 
115289b17223SAlexander Motin void
115389b17223SAlexander Motin g_raid_subdisk_iostart(struct g_raid_subdisk *sd, struct bio *bp)
115489b17223SAlexander Motin {
115589b17223SAlexander Motin 	struct g_consumer *cp;
115689b17223SAlexander Motin 	struct g_raid_disk *disk, *tdisk;
115789b17223SAlexander Motin 
115889b17223SAlexander Motin 	bp->bio_caller1 = sd;
115989b17223SAlexander Motin 
116089b17223SAlexander Motin 	/*
116189b17223SAlexander Motin 	 * Make sure that the disk is present. Generally it is a task of
116289b17223SAlexander Motin 	 * transformation layers to not send requests to absent disks, but
116389b17223SAlexander Motin 	 * it is better to be safe and report situation then sorry.
116489b17223SAlexander Motin 	 */
116589b17223SAlexander Motin 	if (sd->sd_disk == NULL) {
116689b17223SAlexander Motin 		G_RAID_LOGREQ(0, bp, "Warning! I/O request to an absent disk!");
116789b17223SAlexander Motin nodisk:
116889b17223SAlexander Motin 		bp->bio_from = NULL;
116989b17223SAlexander Motin 		bp->bio_to = NULL;
117089b17223SAlexander Motin 		bp->bio_error = ENXIO;
117189b17223SAlexander Motin 		g_raid_disk_done(bp);
117289b17223SAlexander Motin 		return;
117389b17223SAlexander Motin 	}
117489b17223SAlexander Motin 	disk = sd->sd_disk;
117589b17223SAlexander Motin 	if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
117689b17223SAlexander Motin 	    disk->d_state != G_RAID_DISK_S_FAILED) {
117789b17223SAlexander Motin 		G_RAID_LOGREQ(0, bp, "Warning! I/O request to a disk in a "
117889b17223SAlexander Motin 		    "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
117989b17223SAlexander Motin 		goto nodisk;
118089b17223SAlexander Motin 	}
118189b17223SAlexander Motin 
118289b17223SAlexander Motin 	cp = disk->d_consumer;
118389b17223SAlexander Motin 	bp->bio_from = cp;
118489b17223SAlexander Motin 	bp->bio_to = cp->provider;
118589b17223SAlexander Motin 	cp->index++;
118689b17223SAlexander Motin 
118789b17223SAlexander Motin 	/* Update average disks load. */
118889b17223SAlexander Motin 	TAILQ_FOREACH(tdisk, &sd->sd_softc->sc_disks, d_next) {
118989b17223SAlexander Motin 		if (tdisk->d_consumer == NULL)
119089b17223SAlexander Motin 			tdisk->d_load = 0;
119189b17223SAlexander Motin 		else
119289b17223SAlexander Motin 			tdisk->d_load = (tdisk->d_consumer->index *
119389b17223SAlexander Motin 			    G_RAID_SUBDISK_LOAD_SCALE + tdisk->d_load * 7) / 8;
119489b17223SAlexander Motin 	}
119589b17223SAlexander Motin 
119689b17223SAlexander Motin 	disk->d_last_offset = bp->bio_offset + bp->bio_length;
119789b17223SAlexander Motin 	if (dumping) {
119889b17223SAlexander Motin 		G_RAID_LOGREQ(3, bp, "Sending dumping request.");
119989b17223SAlexander Motin 		if (bp->bio_cmd == BIO_WRITE) {
120089b17223SAlexander Motin 			bp->bio_error = g_raid_subdisk_kerneldump(sd,
120189b17223SAlexander Motin 			    bp->bio_data, 0, bp->bio_offset, bp->bio_length);
120289b17223SAlexander Motin 		} else
120389b17223SAlexander Motin 			bp->bio_error = EOPNOTSUPP;
120489b17223SAlexander Motin 		g_raid_disk_done(bp);
120589b17223SAlexander Motin 	} else {
120689b17223SAlexander Motin 		bp->bio_done = g_raid_disk_done;
120789b17223SAlexander Motin 		bp->bio_offset += sd->sd_offset;
120889b17223SAlexander Motin 		G_RAID_LOGREQ(3, bp, "Sending request.");
120989b17223SAlexander Motin 		g_io_request(bp, cp);
121089b17223SAlexander Motin 	}
121189b17223SAlexander Motin }
121289b17223SAlexander Motin 
121389b17223SAlexander Motin int
121489b17223SAlexander Motin g_raid_subdisk_kerneldump(struct g_raid_subdisk *sd,
121589b17223SAlexander Motin     void *virtual, vm_offset_t physical, off_t offset, size_t length)
121689b17223SAlexander Motin {
121789b17223SAlexander Motin 
121889b17223SAlexander Motin 	if (sd->sd_disk == NULL)
121989b17223SAlexander Motin 		return (ENXIO);
122089b17223SAlexander Motin 	if (sd->sd_disk->d_kd.di.dumper == NULL)
122189b17223SAlexander Motin 		return (EOPNOTSUPP);
122289b17223SAlexander Motin 	return (dump_write(&sd->sd_disk->d_kd.di,
122389b17223SAlexander Motin 	    virtual, physical,
122489b17223SAlexander Motin 	    sd->sd_disk->d_kd.di.mediaoffset + sd->sd_offset + offset,
122589b17223SAlexander Motin 	    length));
122689b17223SAlexander Motin }
122789b17223SAlexander Motin 
122889b17223SAlexander Motin static void
122989b17223SAlexander Motin g_raid_disk_done(struct bio *bp)
123089b17223SAlexander Motin {
123189b17223SAlexander Motin 	struct g_raid_softc *sc;
123289b17223SAlexander Motin 	struct g_raid_subdisk *sd;
123389b17223SAlexander Motin 
123489b17223SAlexander Motin 	sd = bp->bio_caller1;
123589b17223SAlexander Motin 	sc = sd->sd_softc;
123689b17223SAlexander Motin 	mtx_lock(&sc->sc_queue_mtx);
123789b17223SAlexander Motin 	bioq_disksort(&sc->sc_queue, bp);
123889b17223SAlexander Motin 	mtx_unlock(&sc->sc_queue_mtx);
123989b17223SAlexander Motin 	if (!dumping)
124089b17223SAlexander Motin 		wakeup(sc);
124189b17223SAlexander Motin }
124289b17223SAlexander Motin 
124389b17223SAlexander Motin static void
124489b17223SAlexander Motin g_raid_disk_done_request(struct bio *bp)
124589b17223SAlexander Motin {
124689b17223SAlexander Motin 	struct g_raid_softc *sc;
124789b17223SAlexander Motin 	struct g_raid_disk *disk;
124889b17223SAlexander Motin 	struct g_raid_subdisk *sd;
124989b17223SAlexander Motin 	struct g_raid_volume *vol;
125089b17223SAlexander Motin 
125189b17223SAlexander Motin 	g_topology_assert_not();
125289b17223SAlexander Motin 
125389b17223SAlexander Motin 	G_RAID_LOGREQ(3, bp, "Disk request done: %d.", bp->bio_error);
125489b17223SAlexander Motin 	sd = bp->bio_caller1;
125589b17223SAlexander Motin 	sc = sd->sd_softc;
125689b17223SAlexander Motin 	vol = sd->sd_volume;
125789b17223SAlexander Motin 	if (bp->bio_from != NULL) {
125889b17223SAlexander Motin 		bp->bio_from->index--;
125989b17223SAlexander Motin 		disk = bp->bio_from->private;
126089b17223SAlexander Motin 		if (disk == NULL)
126189b17223SAlexander Motin 			g_raid_kill_consumer(sc, bp->bio_from);
126289b17223SAlexander Motin 	}
126389b17223SAlexander Motin 	bp->bio_offset -= sd->sd_offset;
126489b17223SAlexander Motin 
126589b17223SAlexander Motin 	G_RAID_TR_IODONE(vol->v_tr, sd, bp);
126689b17223SAlexander Motin }
126789b17223SAlexander Motin 
126889b17223SAlexander Motin static void
126989b17223SAlexander Motin g_raid_handle_event(struct g_raid_softc *sc, struct g_raid_event *ep)
127089b17223SAlexander Motin {
127189b17223SAlexander Motin 
127289b17223SAlexander Motin 	if ((ep->e_flags & G_RAID_EVENT_VOLUME) != 0)
127389b17223SAlexander Motin 		ep->e_error = g_raid_update_volume(ep->e_tgt, ep->e_event);
127489b17223SAlexander Motin 	else if ((ep->e_flags & G_RAID_EVENT_DISK) != 0)
127589b17223SAlexander Motin 		ep->e_error = g_raid_update_disk(ep->e_tgt, ep->e_event);
127689b17223SAlexander Motin 	else if ((ep->e_flags & G_RAID_EVENT_SUBDISK) != 0)
127789b17223SAlexander Motin 		ep->e_error = g_raid_update_subdisk(ep->e_tgt, ep->e_event);
127889b17223SAlexander Motin 	else
127989b17223SAlexander Motin 		ep->e_error = g_raid_update_node(ep->e_tgt, ep->e_event);
128089b17223SAlexander Motin 	if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0) {
128189b17223SAlexander Motin 		KASSERT(ep->e_error == 0,
128289b17223SAlexander Motin 		    ("Error cannot be handled."));
128389b17223SAlexander Motin 		g_raid_event_free(ep);
128489b17223SAlexander Motin 	} else {
128589b17223SAlexander Motin 		ep->e_flags |= G_RAID_EVENT_DONE;
128689b17223SAlexander Motin 		G_RAID_DEBUG1(4, sc, "Waking up %p.", ep);
128789b17223SAlexander Motin 		mtx_lock(&sc->sc_queue_mtx);
128889b17223SAlexander Motin 		wakeup(ep);
128989b17223SAlexander Motin 		mtx_unlock(&sc->sc_queue_mtx);
129089b17223SAlexander Motin 	}
129189b17223SAlexander Motin }
129289b17223SAlexander Motin 
129389b17223SAlexander Motin /*
129489b17223SAlexander Motin  * Worker thread.
129589b17223SAlexander Motin  */
129689b17223SAlexander Motin static void
129789b17223SAlexander Motin g_raid_worker(void *arg)
129889b17223SAlexander Motin {
129989b17223SAlexander Motin 	struct g_raid_softc *sc;
130089b17223SAlexander Motin 	struct g_raid_event *ep;
130189b17223SAlexander Motin 	struct g_raid_volume *vol;
130289b17223SAlexander Motin 	struct bio *bp;
130389b17223SAlexander Motin 	struct timeval now, t;
130489b17223SAlexander Motin 	int timeout, rv;
130589b17223SAlexander Motin 
130689b17223SAlexander Motin 	sc = arg;
130789b17223SAlexander Motin 	thread_lock(curthread);
130889b17223SAlexander Motin 	sched_prio(curthread, PRIBIO);
130989b17223SAlexander Motin 	thread_unlock(curthread);
131089b17223SAlexander Motin 
131189b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
131289b17223SAlexander Motin 	for (;;) {
131389b17223SAlexander Motin 		mtx_lock(&sc->sc_queue_mtx);
131489b17223SAlexander Motin 		/*
131589b17223SAlexander Motin 		 * First take a look at events.
131689b17223SAlexander Motin 		 * This is important to handle events before any I/O requests.
131789b17223SAlexander Motin 		 */
131889b17223SAlexander Motin 		bp = NULL;
131989b17223SAlexander Motin 		vol = NULL;
132089b17223SAlexander Motin 		rv = 0;
132189b17223SAlexander Motin 		ep = TAILQ_FIRST(&sc->sc_events);
132289b17223SAlexander Motin 		if (ep != NULL)
132389b17223SAlexander Motin 			TAILQ_REMOVE(&sc->sc_events, ep, e_next);
132489b17223SAlexander Motin 		else if ((bp = bioq_takefirst(&sc->sc_queue)) != NULL)
132589b17223SAlexander Motin 			;
132689b17223SAlexander Motin 		else {
132789b17223SAlexander Motin 			getmicrouptime(&now);
132889b17223SAlexander Motin 			t = now;
132989b17223SAlexander Motin 			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
133089b17223SAlexander Motin 				if (bioq_first(&vol->v_inflight) == NULL &&
133189b17223SAlexander Motin 				    vol->v_tr &&
133289b17223SAlexander Motin 				    timevalcmp(&vol->v_last_done, &t, < ))
133389b17223SAlexander Motin 					t = vol->v_last_done;
133489b17223SAlexander Motin 			}
133589b17223SAlexander Motin 			timevalsub(&t, &now);
133689b17223SAlexander Motin 			timeout = g_raid_idle_threshold +
133789b17223SAlexander Motin 			    t.tv_sec * 1000000 + t.tv_usec;
133889b17223SAlexander Motin 			if (timeout > 0) {
133989b17223SAlexander Motin 				/*
134089b17223SAlexander Motin 				 * Two steps to avoid overflows at HZ=1000
134189b17223SAlexander Motin 				 * and idle timeouts > 2.1s.  Some rounding
134289b17223SAlexander Motin 				 * errors can occur, but they are < 1tick,
134389b17223SAlexander Motin 				 * which is deemed to be close enough for
134489b17223SAlexander Motin 				 * this purpose.
134589b17223SAlexander Motin 				 */
134689b17223SAlexander Motin 				int micpertic = 1000000 / hz;
134789b17223SAlexander Motin 				timeout = (timeout + micpertic - 1) / micpertic;
134889b17223SAlexander Motin 				sx_xunlock(&sc->sc_lock);
134989b17223SAlexander Motin 				MSLEEP(rv, sc, &sc->sc_queue_mtx,
135089b17223SAlexander Motin 				    PRIBIO | PDROP, "-", timeout);
135189b17223SAlexander Motin 				sx_xlock(&sc->sc_lock);
135289b17223SAlexander Motin 				goto process;
135389b17223SAlexander Motin 			} else
135489b17223SAlexander Motin 				rv = EWOULDBLOCK;
135589b17223SAlexander Motin 		}
135689b17223SAlexander Motin 		mtx_unlock(&sc->sc_queue_mtx);
135789b17223SAlexander Motin process:
135889b17223SAlexander Motin 		if (ep != NULL) {
135989b17223SAlexander Motin 			g_raid_handle_event(sc, ep);
136089b17223SAlexander Motin 		} else if (bp != NULL) {
136189b17223SAlexander Motin 			if (bp->bio_to != NULL &&
136289b17223SAlexander Motin 			    bp->bio_to->geom == sc->sc_geom)
136389b17223SAlexander Motin 				g_raid_start_request(bp);
136489b17223SAlexander Motin 			else
136589b17223SAlexander Motin 				g_raid_disk_done_request(bp);
136689b17223SAlexander Motin 		} else if (rv == EWOULDBLOCK) {
136789b17223SAlexander Motin 			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
136889b17223SAlexander Motin 				if (vol->v_writes == 0 && vol->v_dirty)
136989b17223SAlexander Motin 					g_raid_clean(vol, -1);
137089b17223SAlexander Motin 				if (bioq_first(&vol->v_inflight) == NULL &&
137189b17223SAlexander Motin 				    vol->v_tr) {
137289b17223SAlexander Motin 					t.tv_sec = g_raid_idle_threshold / 1000000;
137389b17223SAlexander Motin 					t.tv_usec = g_raid_idle_threshold % 1000000;
137489b17223SAlexander Motin 					timevaladd(&t, &vol->v_last_done);
137589b17223SAlexander Motin 					getmicrouptime(&now);
137689b17223SAlexander Motin 					if (timevalcmp(&t, &now, <= )) {
137789b17223SAlexander Motin 						G_RAID_TR_IDLE(vol->v_tr);
137889b17223SAlexander Motin 						vol->v_last_done = now;
137989b17223SAlexander Motin 					}
138089b17223SAlexander Motin 				}
138189b17223SAlexander Motin 			}
138289b17223SAlexander Motin 		}
138389b17223SAlexander Motin 		if (sc->sc_stopping == G_RAID_DESTROY_HARD)
138489b17223SAlexander Motin 			g_raid_destroy_node(sc, 1);	/* May not return. */
138589b17223SAlexander Motin 	}
138689b17223SAlexander Motin }
138789b17223SAlexander Motin 
138889b17223SAlexander Motin static void
138989b17223SAlexander Motin g_raid_poll(struct g_raid_softc *sc)
139089b17223SAlexander Motin {
139189b17223SAlexander Motin 	struct g_raid_event *ep;
139289b17223SAlexander Motin 	struct bio *bp;
139389b17223SAlexander Motin 
139489b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
139589b17223SAlexander Motin 	mtx_lock(&sc->sc_queue_mtx);
139689b17223SAlexander Motin 	/*
139789b17223SAlexander Motin 	 * First take a look at events.
139889b17223SAlexander Motin 	 * This is important to handle events before any I/O requests.
139989b17223SAlexander Motin 	 */
140089b17223SAlexander Motin 	ep = TAILQ_FIRST(&sc->sc_events);
140189b17223SAlexander Motin 	if (ep != NULL) {
140289b17223SAlexander Motin 		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
140389b17223SAlexander Motin 		mtx_unlock(&sc->sc_queue_mtx);
140489b17223SAlexander Motin 		g_raid_handle_event(sc, ep);
140589b17223SAlexander Motin 		goto out;
140689b17223SAlexander Motin 	}
140789b17223SAlexander Motin 	bp = bioq_takefirst(&sc->sc_queue);
140889b17223SAlexander Motin 	if (bp != NULL) {
140989b17223SAlexander Motin 		mtx_unlock(&sc->sc_queue_mtx);
141089b17223SAlexander Motin 		if (bp->bio_from == NULL ||
141189b17223SAlexander Motin 		    bp->bio_from->geom != sc->sc_geom)
141289b17223SAlexander Motin 			g_raid_start_request(bp);
141389b17223SAlexander Motin 		else
141489b17223SAlexander Motin 			g_raid_disk_done_request(bp);
141589b17223SAlexander Motin 	}
141689b17223SAlexander Motin out:
141789b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
141889b17223SAlexander Motin }
141989b17223SAlexander Motin 
142089b17223SAlexander Motin static void
142189b17223SAlexander Motin g_raid_launch_provider(struct g_raid_volume *vol)
142289b17223SAlexander Motin {
142389b17223SAlexander Motin 	struct g_raid_disk *disk;
142489b17223SAlexander Motin 	struct g_raid_softc *sc;
142589b17223SAlexander Motin 	struct g_provider *pp;
142689b17223SAlexander Motin 	char name[G_RAID_MAX_VOLUMENAME];
142789b17223SAlexander Motin 	off_t off;
142889b17223SAlexander Motin 
142989b17223SAlexander Motin 	sc = vol->v_softc;
143089b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_LOCKED);
143189b17223SAlexander Motin 
143289b17223SAlexander Motin 	g_topology_lock();
143389b17223SAlexander Motin 	/* Try to name provider with volume name. */
143489b17223SAlexander Motin 	snprintf(name, sizeof(name), "raid/%s", vol->v_name);
143589b17223SAlexander Motin 	if (g_raid_name_format == 0 || vol->v_name[0] == 0 ||
143689b17223SAlexander Motin 	    g_provider_by_name(name) != NULL) {
143789b17223SAlexander Motin 		/* Otherwise use sequential volume number. */
143889b17223SAlexander Motin 		snprintf(name, sizeof(name), "raid/r%d", vol->v_global_id);
143989b17223SAlexander Motin 	}
144089b17223SAlexander Motin 	pp = g_new_providerf(sc->sc_geom, "%s", name);
144189b17223SAlexander Motin 	pp->private = vol;
144289b17223SAlexander Motin 	pp->mediasize = vol->v_mediasize;
144389b17223SAlexander Motin 	pp->sectorsize = vol->v_sectorsize;
144489b17223SAlexander Motin 	pp->stripesize = 0;
144589b17223SAlexander Motin 	pp->stripeoffset = 0;
144689b17223SAlexander Motin 	if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
144789b17223SAlexander Motin 	    vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 ||
144889b17223SAlexander Motin 	    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE ||
144989b17223SAlexander Motin 	    vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT) {
145089b17223SAlexander Motin 		if ((disk = vol->v_subdisks[0].sd_disk) != NULL &&
145189b17223SAlexander Motin 		    disk->d_consumer != NULL &&
145289b17223SAlexander Motin 		    disk->d_consumer->provider != NULL) {
145389b17223SAlexander Motin 			pp->stripesize = disk->d_consumer->provider->stripesize;
145489b17223SAlexander Motin 			off = disk->d_consumer->provider->stripeoffset;
145589b17223SAlexander Motin 			pp->stripeoffset = off + vol->v_subdisks[0].sd_offset;
145689b17223SAlexander Motin 			if (off > 0)
145789b17223SAlexander Motin 				pp->stripeoffset %= off;
145889b17223SAlexander Motin 		}
145989b17223SAlexander Motin 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3) {
146089b17223SAlexander Motin 			pp->stripesize *= (vol->v_disks_count - 1);
146189b17223SAlexander Motin 			pp->stripeoffset *= (vol->v_disks_count - 1);
146289b17223SAlexander Motin 		}
146389b17223SAlexander Motin 	} else
146489b17223SAlexander Motin 		pp->stripesize = vol->v_strip_size;
146589b17223SAlexander Motin 	vol->v_provider = pp;
146689b17223SAlexander Motin 	g_error_provider(pp, 0);
146789b17223SAlexander Motin 	g_topology_unlock();
146889b17223SAlexander Motin 	G_RAID_DEBUG1(0, sc, "Provider %s for volume %s created.",
146989b17223SAlexander Motin 	    pp->name, vol->v_name);
147089b17223SAlexander Motin }
147189b17223SAlexander Motin 
147289b17223SAlexander Motin static void
147389b17223SAlexander Motin g_raid_destroy_provider(struct g_raid_volume *vol)
147489b17223SAlexander Motin {
147589b17223SAlexander Motin 	struct g_raid_softc *sc;
147689b17223SAlexander Motin 	struct g_provider *pp;
147789b17223SAlexander Motin 	struct bio *bp, *tmp;
147889b17223SAlexander Motin 
147989b17223SAlexander Motin 	g_topology_assert_not();
148089b17223SAlexander Motin 	sc = vol->v_softc;
148189b17223SAlexander Motin 	pp = vol->v_provider;
148289b17223SAlexander Motin 	KASSERT(pp != NULL, ("NULL provider (volume=%s).", vol->v_name));
148389b17223SAlexander Motin 
148489b17223SAlexander Motin 	g_topology_lock();
148589b17223SAlexander Motin 	g_error_provider(pp, ENXIO);
148689b17223SAlexander Motin 	mtx_lock(&sc->sc_queue_mtx);
148789b17223SAlexander Motin 	TAILQ_FOREACH_SAFE(bp, &sc->sc_queue.queue, bio_queue, tmp) {
148889b17223SAlexander Motin 		if (bp->bio_to != pp)
148989b17223SAlexander Motin 			continue;
149089b17223SAlexander Motin 		bioq_remove(&sc->sc_queue, bp);
149189b17223SAlexander Motin 		g_io_deliver(bp, ENXIO);
149289b17223SAlexander Motin 	}
149389b17223SAlexander Motin 	mtx_unlock(&sc->sc_queue_mtx);
149489b17223SAlexander Motin 	G_RAID_DEBUG1(0, sc, "Provider %s for volume %s destroyed.",
149589b17223SAlexander Motin 	    pp->name, vol->v_name);
149689b17223SAlexander Motin 	g_wither_provider(pp, ENXIO);
149789b17223SAlexander Motin 	g_topology_unlock();
149889b17223SAlexander Motin 	vol->v_provider = NULL;
149989b17223SAlexander Motin }
150089b17223SAlexander Motin 
150189b17223SAlexander Motin /*
150289b17223SAlexander Motin  * Update device state.
150389b17223SAlexander Motin  */
150489b17223SAlexander Motin static int
150589b17223SAlexander Motin g_raid_update_volume(struct g_raid_volume *vol, u_int event)
150689b17223SAlexander Motin {
150789b17223SAlexander Motin 	struct g_raid_softc *sc;
150889b17223SAlexander Motin 
150989b17223SAlexander Motin 	sc = vol->v_softc;
151089b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_XLOCKED);
151189b17223SAlexander Motin 
151289b17223SAlexander Motin 	G_RAID_DEBUG1(2, sc, "Event %s for volume %s.",
151389b17223SAlexander Motin 	    g_raid_volume_event2str(event),
151489b17223SAlexander Motin 	    vol->v_name);
151589b17223SAlexander Motin 	switch (event) {
151689b17223SAlexander Motin 	case G_RAID_VOLUME_E_DOWN:
151789b17223SAlexander Motin 		if (vol->v_provider != NULL)
151889b17223SAlexander Motin 			g_raid_destroy_provider(vol);
151989b17223SAlexander Motin 		break;
152089b17223SAlexander Motin 	case G_RAID_VOLUME_E_UP:
152189b17223SAlexander Motin 		if (vol->v_provider == NULL)
152289b17223SAlexander Motin 			g_raid_launch_provider(vol);
152389b17223SAlexander Motin 		break;
152489b17223SAlexander Motin 	case G_RAID_VOLUME_E_START:
152589b17223SAlexander Motin 		if (vol->v_tr)
152689b17223SAlexander Motin 			G_RAID_TR_START(vol->v_tr);
152789b17223SAlexander Motin 		return (0);
152889b17223SAlexander Motin 	default:
152989b17223SAlexander Motin 		if (sc->sc_md)
153089b17223SAlexander Motin 			G_RAID_MD_VOLUME_EVENT(sc->sc_md, vol, event);
153189b17223SAlexander Motin 		return (0);
153289b17223SAlexander Motin 	}
153389b17223SAlexander Motin 
153489b17223SAlexander Motin 	/* Manage root mount release. */
153589b17223SAlexander Motin 	if (vol->v_starting) {
153689b17223SAlexander Motin 		vol->v_starting = 0;
153789b17223SAlexander Motin 		G_RAID_DEBUG1(1, sc, "root_mount_rel %p", vol->v_rootmount);
153889b17223SAlexander Motin 		root_mount_rel(vol->v_rootmount);
153989b17223SAlexander Motin 		vol->v_rootmount = NULL;
154089b17223SAlexander Motin 	}
154189b17223SAlexander Motin 	if (vol->v_stopping && vol->v_provider_open == 0)
154289b17223SAlexander Motin 		g_raid_destroy_volume(vol);
154389b17223SAlexander Motin 	return (0);
154489b17223SAlexander Motin }
154589b17223SAlexander Motin 
154689b17223SAlexander Motin /*
154789b17223SAlexander Motin  * Update subdisk state.
154889b17223SAlexander Motin  */
154989b17223SAlexander Motin static int
155089b17223SAlexander Motin g_raid_update_subdisk(struct g_raid_subdisk *sd, u_int event)
155189b17223SAlexander Motin {
155289b17223SAlexander Motin 	struct g_raid_softc *sc;
155389b17223SAlexander Motin 	struct g_raid_volume *vol;
155489b17223SAlexander Motin 
155589b17223SAlexander Motin 	sc = sd->sd_softc;
155689b17223SAlexander Motin 	vol = sd->sd_volume;
155789b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_XLOCKED);
155889b17223SAlexander Motin 
155989b17223SAlexander Motin 	G_RAID_DEBUG1(2, sc, "Event %s for subdisk %s:%d-%s.",
156089b17223SAlexander Motin 	    g_raid_subdisk_event2str(event),
156189b17223SAlexander Motin 	    vol->v_name, sd->sd_pos,
156289b17223SAlexander Motin 	    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
156389b17223SAlexander Motin 	if (vol->v_tr)
156489b17223SAlexander Motin 		G_RAID_TR_EVENT(vol->v_tr, sd, event);
156589b17223SAlexander Motin 
156689b17223SAlexander Motin 	return (0);
156789b17223SAlexander Motin }
156889b17223SAlexander Motin 
156989b17223SAlexander Motin /*
157089b17223SAlexander Motin  * Update disk state.
157189b17223SAlexander Motin  */
157289b17223SAlexander Motin static int
157389b17223SAlexander Motin g_raid_update_disk(struct g_raid_disk *disk, u_int event)
157489b17223SAlexander Motin {
157589b17223SAlexander Motin 	struct g_raid_softc *sc;
157689b17223SAlexander Motin 
157789b17223SAlexander Motin 	sc = disk->d_softc;
157889b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_XLOCKED);
157989b17223SAlexander Motin 
158089b17223SAlexander Motin 	G_RAID_DEBUG1(2, sc, "Event %s for disk %s.",
158189b17223SAlexander Motin 	    g_raid_disk_event2str(event),
158289b17223SAlexander Motin 	    g_raid_get_diskname(disk));
158389b17223SAlexander Motin 
158489b17223SAlexander Motin 	if (sc->sc_md)
158589b17223SAlexander Motin 		G_RAID_MD_EVENT(sc->sc_md, disk, event);
158689b17223SAlexander Motin 	return (0);
158789b17223SAlexander Motin }
158889b17223SAlexander Motin 
158989b17223SAlexander Motin /*
159089b17223SAlexander Motin  * Node event.
159189b17223SAlexander Motin  */
159289b17223SAlexander Motin static int
159389b17223SAlexander Motin g_raid_update_node(struct g_raid_softc *sc, u_int event)
159489b17223SAlexander Motin {
159589b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_XLOCKED);
159689b17223SAlexander Motin 
159789b17223SAlexander Motin 	G_RAID_DEBUG1(2, sc, "Event %s for the array.",
159889b17223SAlexander Motin 	    g_raid_node_event2str(event));
159989b17223SAlexander Motin 
160089b17223SAlexander Motin 	if (event == G_RAID_NODE_E_WAKE)
160189b17223SAlexander Motin 		return (0);
160289b17223SAlexander Motin 	if (sc->sc_md)
160389b17223SAlexander Motin 		G_RAID_MD_EVENT(sc->sc_md, NULL, event);
160489b17223SAlexander Motin 	return (0);
160589b17223SAlexander Motin }
160689b17223SAlexander Motin 
160789b17223SAlexander Motin static int
160889b17223SAlexander Motin g_raid_access(struct g_provider *pp, int acr, int acw, int ace)
160989b17223SAlexander Motin {
161089b17223SAlexander Motin 	struct g_raid_volume *vol;
161189b17223SAlexander Motin 	struct g_raid_softc *sc;
161214e2cd0aSAlexander Motin 	int dcw, opens, error = 0;
161389b17223SAlexander Motin 
161489b17223SAlexander Motin 	g_topology_assert();
161589b17223SAlexander Motin 	sc = pp->geom->softc;
161689b17223SAlexander Motin 	vol = pp->private;
161789b17223SAlexander Motin 	KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
161889b17223SAlexander Motin 	KASSERT(vol != NULL, ("NULL volume (provider=%s).", pp->name));
161989b17223SAlexander Motin 
162089b17223SAlexander Motin 	G_RAID_DEBUG1(2, sc, "Access request for %s: r%dw%de%d.", pp->name,
162189b17223SAlexander Motin 	    acr, acw, ace);
162289b17223SAlexander Motin 	dcw = pp->acw + acw;
162389b17223SAlexander Motin 
162489b17223SAlexander Motin 	g_topology_unlock();
162589b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
162689b17223SAlexander Motin 	/* Deny new opens while dying. */
162789b17223SAlexander Motin 	if (sc->sc_stopping != 0 && (acr > 0 || acw > 0 || ace > 0)) {
162889b17223SAlexander Motin 		error = ENXIO;
162989b17223SAlexander Motin 		goto out;
163089b17223SAlexander Motin 	}
163189b17223SAlexander Motin 	if (dcw == 0 && vol->v_dirty)
163289b17223SAlexander Motin 		g_raid_clean(vol, dcw);
163389b17223SAlexander Motin 	vol->v_provider_open += acr + acw + ace;
163489b17223SAlexander Motin 	/* Handle delayed node destruction. */
163589b17223SAlexander Motin 	if (sc->sc_stopping == G_RAID_DESTROY_DELAYED &&
163689b17223SAlexander Motin 	    vol->v_provider_open == 0) {
163789b17223SAlexander Motin 		/* Count open volumes. */
163889b17223SAlexander Motin 		opens = g_raid_nopens(sc);
163989b17223SAlexander Motin 		if (opens == 0) {
164089b17223SAlexander Motin 			sc->sc_stopping = G_RAID_DESTROY_HARD;
164189b17223SAlexander Motin 			/* Wake up worker to make it selfdestruct. */
164289b17223SAlexander Motin 			g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
164389b17223SAlexander Motin 		}
164489b17223SAlexander Motin 	}
164589b17223SAlexander Motin 	/* Handle open volume destruction. */
164689b17223SAlexander Motin 	if (vol->v_stopping && vol->v_provider_open == 0)
164789b17223SAlexander Motin 		g_raid_destroy_volume(vol);
164889b17223SAlexander Motin out:
164989b17223SAlexander Motin 	sx_xunlock(&sc->sc_lock);
165089b17223SAlexander Motin 	g_topology_lock();
165189b17223SAlexander Motin 	return (error);
165289b17223SAlexander Motin }
165389b17223SAlexander Motin 
165489b17223SAlexander Motin struct g_raid_softc *
165589b17223SAlexander Motin g_raid_create_node(struct g_class *mp,
165689b17223SAlexander Motin     const char *name, struct g_raid_md_object *md)
165789b17223SAlexander Motin {
165889b17223SAlexander Motin 	struct g_raid_softc *sc;
165989b17223SAlexander Motin 	struct g_geom *gp;
166089b17223SAlexander Motin 	int error;
166189b17223SAlexander Motin 
166289b17223SAlexander Motin 	g_topology_assert();
166389b17223SAlexander Motin 	G_RAID_DEBUG(1, "Creating array %s.", name);
166489b17223SAlexander Motin 
166589b17223SAlexander Motin 	gp = g_new_geomf(mp, "%s", name);
166689b17223SAlexander Motin 	sc = malloc(sizeof(*sc), M_RAID, M_WAITOK | M_ZERO);
166789b17223SAlexander Motin 	gp->start = g_raid_start;
166889b17223SAlexander Motin 	gp->orphan = g_raid_orphan;
166989b17223SAlexander Motin 	gp->access = g_raid_access;
167089b17223SAlexander Motin 	gp->dumpconf = g_raid_dumpconf;
167189b17223SAlexander Motin 
167289b17223SAlexander Motin 	sc->sc_md = md;
167389b17223SAlexander Motin 	sc->sc_geom = gp;
167489b17223SAlexander Motin 	sc->sc_flags = 0;
167589b17223SAlexander Motin 	TAILQ_INIT(&sc->sc_volumes);
167689b17223SAlexander Motin 	TAILQ_INIT(&sc->sc_disks);
167789b17223SAlexander Motin 	sx_init(&sc->sc_lock, "gmirror:lock");
167889b17223SAlexander Motin 	mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
167989b17223SAlexander Motin 	TAILQ_INIT(&sc->sc_events);
168089b17223SAlexander Motin 	bioq_init(&sc->sc_queue);
168189b17223SAlexander Motin 	gp->softc = sc;
168289b17223SAlexander Motin 	error = kproc_create(g_raid_worker, sc, &sc->sc_worker, 0, 0,
168389b17223SAlexander Motin 	    "g_raid %s", name);
168489b17223SAlexander Motin 	if (error != 0) {
168589b17223SAlexander Motin 		G_RAID_DEBUG(0, "Cannot create kernel thread for %s.", name);
168689b17223SAlexander Motin 		mtx_destroy(&sc->sc_queue_mtx);
168789b17223SAlexander Motin 		sx_destroy(&sc->sc_lock);
168889b17223SAlexander Motin 		g_destroy_geom(sc->sc_geom);
168989b17223SAlexander Motin 		free(sc, M_RAID);
169089b17223SAlexander Motin 		return (NULL);
169189b17223SAlexander Motin 	}
169289b17223SAlexander Motin 
169389b17223SAlexander Motin 	G_RAID_DEBUG1(0, sc, "Array %s created.", name);
169489b17223SAlexander Motin 	return (sc);
169589b17223SAlexander Motin }
169689b17223SAlexander Motin 
169789b17223SAlexander Motin struct g_raid_volume *
169889b17223SAlexander Motin g_raid_create_volume(struct g_raid_softc *sc, const char *name, int id)
169989b17223SAlexander Motin {
170089b17223SAlexander Motin 	struct g_raid_volume	*vol, *vol1;
170189b17223SAlexander Motin 	int i;
170289b17223SAlexander Motin 
170389b17223SAlexander Motin 	G_RAID_DEBUG1(1, sc, "Creating volume %s.", name);
170489b17223SAlexander Motin 	vol = malloc(sizeof(*vol), M_RAID, M_WAITOK | M_ZERO);
170589b17223SAlexander Motin 	vol->v_softc = sc;
170689b17223SAlexander Motin 	strlcpy(vol->v_name, name, G_RAID_MAX_VOLUMENAME);
170789b17223SAlexander Motin 	vol->v_state = G_RAID_VOLUME_S_STARTING;
170889b17223SAlexander Motin 	vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
170989b17223SAlexander Motin 	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_UNKNOWN;
171089b17223SAlexander Motin 	bioq_init(&vol->v_inflight);
171189b17223SAlexander Motin 	bioq_init(&vol->v_locked);
171289b17223SAlexander Motin 	LIST_INIT(&vol->v_locks);
171389b17223SAlexander Motin 	for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
171489b17223SAlexander Motin 		vol->v_subdisks[i].sd_softc = sc;
171589b17223SAlexander Motin 		vol->v_subdisks[i].sd_volume = vol;
171689b17223SAlexander Motin 		vol->v_subdisks[i].sd_pos = i;
171789b17223SAlexander Motin 		vol->v_subdisks[i].sd_state = G_RAID_DISK_S_NONE;
171889b17223SAlexander Motin 	}
171989b17223SAlexander Motin 
172089b17223SAlexander Motin 	/* Find free ID for this volume. */
172189b17223SAlexander Motin 	g_topology_lock();
172289b17223SAlexander Motin 	vol1 = vol;
172389b17223SAlexander Motin 	if (id >= 0) {
172489b17223SAlexander Motin 		LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
172589b17223SAlexander Motin 			if (vol1->v_global_id == id)
172689b17223SAlexander Motin 				break;
172789b17223SAlexander Motin 		}
172889b17223SAlexander Motin 	}
172989b17223SAlexander Motin 	if (vol1 != NULL) {
173089b17223SAlexander Motin 		for (id = 0; ; id++) {
173189b17223SAlexander Motin 			LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
173289b17223SAlexander Motin 				if (vol1->v_global_id == id)
173389b17223SAlexander Motin 					break;
173489b17223SAlexander Motin 			}
173589b17223SAlexander Motin 			if (vol1 == NULL)
173689b17223SAlexander Motin 				break;
173789b17223SAlexander Motin 		}
173889b17223SAlexander Motin 	}
173989b17223SAlexander Motin 	vol->v_global_id = id;
174089b17223SAlexander Motin 	LIST_INSERT_HEAD(&g_raid_volumes, vol, v_global_next);
174189b17223SAlexander Motin 	g_topology_unlock();
174289b17223SAlexander Motin 
174389b17223SAlexander Motin 	/* Delay root mounting. */
174489b17223SAlexander Motin 	vol->v_rootmount = root_mount_hold("GRAID");
174589b17223SAlexander Motin 	G_RAID_DEBUG1(1, sc, "root_mount_hold %p", vol->v_rootmount);
174689b17223SAlexander Motin 	vol->v_starting = 1;
174789b17223SAlexander Motin 	TAILQ_INSERT_TAIL(&sc->sc_volumes, vol, v_next);
174889b17223SAlexander Motin 	return (vol);
174989b17223SAlexander Motin }
175089b17223SAlexander Motin 
175189b17223SAlexander Motin struct g_raid_disk *
175289b17223SAlexander Motin g_raid_create_disk(struct g_raid_softc *sc)
175389b17223SAlexander Motin {
175489b17223SAlexander Motin 	struct g_raid_disk	*disk;
175589b17223SAlexander Motin 
175689b17223SAlexander Motin 	G_RAID_DEBUG1(1, sc, "Creating disk.");
175789b17223SAlexander Motin 	disk = malloc(sizeof(*disk), M_RAID, M_WAITOK | M_ZERO);
175889b17223SAlexander Motin 	disk->d_softc = sc;
175989b17223SAlexander Motin 	disk->d_state = G_RAID_DISK_S_NONE;
176089b17223SAlexander Motin 	TAILQ_INIT(&disk->d_subdisks);
176189b17223SAlexander Motin 	TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
176289b17223SAlexander Motin 	return (disk);
176389b17223SAlexander Motin }
176489b17223SAlexander Motin 
176589b17223SAlexander Motin int g_raid_start_volume(struct g_raid_volume *vol)
176689b17223SAlexander Motin {
176789b17223SAlexander Motin 	struct g_raid_tr_class *class;
176889b17223SAlexander Motin 	struct g_raid_tr_object *obj;
176989b17223SAlexander Motin 	int status;
177089b17223SAlexander Motin 
177189b17223SAlexander Motin 	G_RAID_DEBUG1(2, vol->v_softc, "Starting volume %s.", vol->v_name);
177289b17223SAlexander Motin 	LIST_FOREACH(class, &g_raid_tr_classes, trc_list) {
177389b17223SAlexander Motin 		G_RAID_DEBUG1(2, vol->v_softc,
177489b17223SAlexander Motin 		    "Tasting volume %s for %s transformation.",
177589b17223SAlexander Motin 		    vol->v_name, class->name);
177689b17223SAlexander Motin 		obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
177789b17223SAlexander Motin 		    M_WAITOK);
177889b17223SAlexander Motin 		obj->tro_class = class;
177989b17223SAlexander Motin 		obj->tro_volume = vol;
178089b17223SAlexander Motin 		status = G_RAID_TR_TASTE(obj, vol);
178189b17223SAlexander Motin 		if (status != G_RAID_TR_TASTE_FAIL)
178289b17223SAlexander Motin 			break;
178389b17223SAlexander Motin 		kobj_delete((kobj_t)obj, M_RAID);
178489b17223SAlexander Motin 	}
178589b17223SAlexander Motin 	if (class == NULL) {
178689b17223SAlexander Motin 		G_RAID_DEBUG1(0, vol->v_softc,
178789b17223SAlexander Motin 		    "No transformation module found for %s.",
178889b17223SAlexander Motin 		    vol->v_name);
178989b17223SAlexander Motin 		vol->v_tr = NULL;
179089b17223SAlexander Motin 		g_raid_change_volume_state(vol, G_RAID_VOLUME_S_UNSUPPORTED);
179189b17223SAlexander Motin 		g_raid_event_send(vol, G_RAID_VOLUME_E_DOWN,
179289b17223SAlexander Motin 		    G_RAID_EVENT_VOLUME);
179389b17223SAlexander Motin 		return (-1);
179489b17223SAlexander Motin 	}
179589b17223SAlexander Motin 	G_RAID_DEBUG1(2, vol->v_softc,
179689b17223SAlexander Motin 	    "Transformation module %s chosen for %s.",
179789b17223SAlexander Motin 	    class->name, vol->v_name);
179889b17223SAlexander Motin 	vol->v_tr = obj;
179989b17223SAlexander Motin 	return (0);
180089b17223SAlexander Motin }
180189b17223SAlexander Motin 
180289b17223SAlexander Motin int
180389b17223SAlexander Motin g_raid_destroy_node(struct g_raid_softc *sc, int worker)
180489b17223SAlexander Motin {
180589b17223SAlexander Motin 	struct g_raid_volume *vol, *tmpv;
180689b17223SAlexander Motin 	struct g_raid_disk *disk, *tmpd;
180789b17223SAlexander Motin 	int error = 0;
180889b17223SAlexander Motin 
180989b17223SAlexander Motin 	sc->sc_stopping = G_RAID_DESTROY_HARD;
181089b17223SAlexander Motin 	TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tmpv) {
181189b17223SAlexander Motin 		if (g_raid_destroy_volume(vol))
181289b17223SAlexander Motin 			error = EBUSY;
181389b17223SAlexander Motin 	}
181489b17223SAlexander Motin 	if (error)
181589b17223SAlexander Motin 		return (error);
181689b17223SAlexander Motin 	TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tmpd) {
181789b17223SAlexander Motin 		if (g_raid_destroy_disk(disk))
181889b17223SAlexander Motin 			error = EBUSY;
181989b17223SAlexander Motin 	}
182089b17223SAlexander Motin 	if (error)
182189b17223SAlexander Motin 		return (error);
182289b17223SAlexander Motin 	if (sc->sc_md) {
182389b17223SAlexander Motin 		G_RAID_MD_FREE(sc->sc_md);
182489b17223SAlexander Motin 		kobj_delete((kobj_t)sc->sc_md, M_RAID);
182589b17223SAlexander Motin 		sc->sc_md = NULL;
182689b17223SAlexander Motin 	}
182789b17223SAlexander Motin 	if (sc->sc_geom != NULL) {
182889b17223SAlexander Motin 		G_RAID_DEBUG1(0, sc, "Array %s destroyed.", sc->sc_name);
182989b17223SAlexander Motin 		g_topology_lock();
183089b17223SAlexander Motin 		sc->sc_geom->softc = NULL;
183189b17223SAlexander Motin 		g_wither_geom(sc->sc_geom, ENXIO);
183289b17223SAlexander Motin 		g_topology_unlock();
183389b17223SAlexander Motin 		sc->sc_geom = NULL;
183489b17223SAlexander Motin 	} else
183589b17223SAlexander Motin 		G_RAID_DEBUG(1, "Array destroyed.");
183689b17223SAlexander Motin 	if (worker) {
183789b17223SAlexander Motin 		g_raid_event_cancel(sc, sc);
183889b17223SAlexander Motin 		mtx_destroy(&sc->sc_queue_mtx);
183989b17223SAlexander Motin 		sx_xunlock(&sc->sc_lock);
184089b17223SAlexander Motin 		sx_destroy(&sc->sc_lock);
184189b17223SAlexander Motin 		wakeup(&sc->sc_stopping);
184289b17223SAlexander Motin 		free(sc, M_RAID);
184389b17223SAlexander Motin 		curthread->td_pflags &= ~TDP_GEOM;
184489b17223SAlexander Motin 		G_RAID_DEBUG(1, "Thread exiting.");
184589b17223SAlexander Motin 		kproc_exit(0);
184689b17223SAlexander Motin 	} else {
184789b17223SAlexander Motin 		/* Wake up worker to make it selfdestruct. */
184889b17223SAlexander Motin 		g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
184989b17223SAlexander Motin 	}
185089b17223SAlexander Motin 	return (0);
185189b17223SAlexander Motin }
185289b17223SAlexander Motin 
185389b17223SAlexander Motin int
185489b17223SAlexander Motin g_raid_destroy_volume(struct g_raid_volume *vol)
185589b17223SAlexander Motin {
185689b17223SAlexander Motin 	struct g_raid_softc *sc;
185789b17223SAlexander Motin 	struct g_raid_disk *disk;
185889b17223SAlexander Motin 	int i;
185989b17223SAlexander Motin 
186089b17223SAlexander Motin 	sc = vol->v_softc;
186189b17223SAlexander Motin 	G_RAID_DEBUG1(2, sc, "Destroying volume %s.", vol->v_name);
186289b17223SAlexander Motin 	vol->v_stopping = 1;
186389b17223SAlexander Motin 	if (vol->v_state != G_RAID_VOLUME_S_STOPPED) {
186489b17223SAlexander Motin 		if (vol->v_tr) {
186589b17223SAlexander Motin 			G_RAID_TR_STOP(vol->v_tr);
186689b17223SAlexander Motin 			return (EBUSY);
186789b17223SAlexander Motin 		} else
186889b17223SAlexander Motin 			vol->v_state = G_RAID_VOLUME_S_STOPPED;
186989b17223SAlexander Motin 	}
187089b17223SAlexander Motin 	if (g_raid_event_check(sc, vol) != 0)
187189b17223SAlexander Motin 		return (EBUSY);
187289b17223SAlexander Motin 	if (vol->v_provider != NULL)
187389b17223SAlexander Motin 		return (EBUSY);
187489b17223SAlexander Motin 	if (vol->v_provider_open != 0)
187589b17223SAlexander Motin 		return (EBUSY);
187689b17223SAlexander Motin 	if (vol->v_tr) {
187789b17223SAlexander Motin 		G_RAID_TR_FREE(vol->v_tr);
187889b17223SAlexander Motin 		kobj_delete((kobj_t)vol->v_tr, M_RAID);
187989b17223SAlexander Motin 		vol->v_tr = NULL;
188089b17223SAlexander Motin 	}
188189b17223SAlexander Motin 	if (vol->v_rootmount)
188289b17223SAlexander Motin 		root_mount_rel(vol->v_rootmount);
188389b17223SAlexander Motin 	g_topology_lock();
188489b17223SAlexander Motin 	LIST_REMOVE(vol, v_global_next);
188589b17223SAlexander Motin 	g_topology_unlock();
188689b17223SAlexander Motin 	TAILQ_REMOVE(&sc->sc_volumes, vol, v_next);
188789b17223SAlexander Motin 	for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
188889b17223SAlexander Motin 		g_raid_event_cancel(sc, &vol->v_subdisks[i]);
188989b17223SAlexander Motin 		disk = vol->v_subdisks[i].sd_disk;
189089b17223SAlexander Motin 		if (disk == NULL)
189189b17223SAlexander Motin 			continue;
189289b17223SAlexander Motin 		TAILQ_REMOVE(&disk->d_subdisks, &vol->v_subdisks[i], sd_next);
189389b17223SAlexander Motin 	}
189489b17223SAlexander Motin 	G_RAID_DEBUG1(2, sc, "Volume %s destroyed.", vol->v_name);
189589b17223SAlexander Motin 	if (sc->sc_md)
189689b17223SAlexander Motin 		G_RAID_MD_FREE_VOLUME(sc->sc_md, vol);
189789b17223SAlexander Motin 	g_raid_event_cancel(sc, vol);
189889b17223SAlexander Motin 	free(vol, M_RAID);
189989b17223SAlexander Motin 	if (sc->sc_stopping == G_RAID_DESTROY_HARD) {
190089b17223SAlexander Motin 		/* Wake up worker to let it selfdestruct. */
190189b17223SAlexander Motin 		g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
190289b17223SAlexander Motin 	}
190389b17223SAlexander Motin 	return (0);
190489b17223SAlexander Motin }
190589b17223SAlexander Motin 
190689b17223SAlexander Motin int
190789b17223SAlexander Motin g_raid_destroy_disk(struct g_raid_disk *disk)
190889b17223SAlexander Motin {
190989b17223SAlexander Motin 	struct g_raid_softc *sc;
191089b17223SAlexander Motin 	struct g_raid_subdisk *sd, *tmp;
191189b17223SAlexander Motin 
191289b17223SAlexander Motin 	sc = disk->d_softc;
191389b17223SAlexander Motin 	G_RAID_DEBUG1(2, sc, "Destroying disk.");
191489b17223SAlexander Motin 	if (disk->d_consumer) {
191589b17223SAlexander Motin 		g_raid_kill_consumer(sc, disk->d_consumer);
191689b17223SAlexander Motin 		disk->d_consumer = NULL;
191789b17223SAlexander Motin 	}
191889b17223SAlexander Motin 	TAILQ_FOREACH_SAFE(sd, &disk->d_subdisks, sd_next, tmp) {
191989b17223SAlexander Motin 		g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_NONE);
192089b17223SAlexander Motin 		g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
192189b17223SAlexander Motin 		    G_RAID_EVENT_SUBDISK);
192289b17223SAlexander Motin 		TAILQ_REMOVE(&disk->d_subdisks, sd, sd_next);
192389b17223SAlexander Motin 		sd->sd_disk = NULL;
192489b17223SAlexander Motin 	}
192589b17223SAlexander Motin 	TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
192689b17223SAlexander Motin 	if (sc->sc_md)
192789b17223SAlexander Motin 		G_RAID_MD_FREE_DISK(sc->sc_md, disk);
192889b17223SAlexander Motin 	g_raid_event_cancel(sc, disk);
192989b17223SAlexander Motin 	free(disk, M_RAID);
193089b17223SAlexander Motin 	return (0);
193189b17223SAlexander Motin }
193289b17223SAlexander Motin 
193389b17223SAlexander Motin int
193489b17223SAlexander Motin g_raid_destroy(struct g_raid_softc *sc, int how)
193589b17223SAlexander Motin {
193689b17223SAlexander Motin 	int opens;
193789b17223SAlexander Motin 
193889b17223SAlexander Motin 	g_topology_assert_not();
193989b17223SAlexander Motin 	if (sc == NULL)
194089b17223SAlexander Motin 		return (ENXIO);
194189b17223SAlexander Motin 	sx_assert(&sc->sc_lock, SX_XLOCKED);
194289b17223SAlexander Motin 
194389b17223SAlexander Motin 	/* Count open volumes. */
194489b17223SAlexander Motin 	opens = g_raid_nopens(sc);
194589b17223SAlexander Motin 
194689b17223SAlexander Motin 	/* React on some opened volumes. */
194789b17223SAlexander Motin 	if (opens > 0) {
194889b17223SAlexander Motin 		switch (how) {
194989b17223SAlexander Motin 		case G_RAID_DESTROY_SOFT:
195089b17223SAlexander Motin 			G_RAID_DEBUG1(1, sc,
195189b17223SAlexander Motin 			    "%d volumes are still open.",
195289b17223SAlexander Motin 			    opens);
195389b17223SAlexander Motin 			return (EBUSY);
195489b17223SAlexander Motin 		case G_RAID_DESTROY_DELAYED:
195589b17223SAlexander Motin 			G_RAID_DEBUG1(1, sc,
195689b17223SAlexander Motin 			    "Array will be destroyed on last close.");
195789b17223SAlexander Motin 			sc->sc_stopping = G_RAID_DESTROY_DELAYED;
195889b17223SAlexander Motin 			return (EBUSY);
195989b17223SAlexander Motin 		case G_RAID_DESTROY_HARD:
196089b17223SAlexander Motin 			G_RAID_DEBUG1(1, sc,
196189b17223SAlexander Motin 			    "%d volumes are still open.",
196289b17223SAlexander Motin 			    opens);
196389b17223SAlexander Motin 		}
196489b17223SAlexander Motin 	}
196589b17223SAlexander Motin 
196689b17223SAlexander Motin 	/* Mark node for destruction. */
196789b17223SAlexander Motin 	sc->sc_stopping = G_RAID_DESTROY_HARD;
196889b17223SAlexander Motin 	/* Wake up worker to let it selfdestruct. */
196989b17223SAlexander Motin 	g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
197089b17223SAlexander Motin 	/* Sleep until node destroyed. */
197189b17223SAlexander Motin 	sx_sleep(&sc->sc_stopping, &sc->sc_lock,
197289b17223SAlexander Motin 	    PRIBIO | PDROP, "r:destroy", 0);
197389b17223SAlexander Motin 	return (0);
197489b17223SAlexander Motin }
197589b17223SAlexander Motin 
197689b17223SAlexander Motin static void
197789b17223SAlexander Motin g_raid_taste_orphan(struct g_consumer *cp)
197889b17223SAlexander Motin {
197989b17223SAlexander Motin 
198089b17223SAlexander Motin 	KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
198189b17223SAlexander Motin 	    cp->provider->name));
198289b17223SAlexander Motin }
198389b17223SAlexander Motin 
198489b17223SAlexander Motin static struct g_geom *
198589b17223SAlexander Motin g_raid_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
198689b17223SAlexander Motin {
198789b17223SAlexander Motin 	struct g_consumer *cp;
198889b17223SAlexander Motin 	struct g_geom *gp, *geom;
198989b17223SAlexander Motin 	struct g_raid_md_class *class;
199089b17223SAlexander Motin 	struct g_raid_md_object *obj;
199189b17223SAlexander Motin 	int status;
199289b17223SAlexander Motin 
199389b17223SAlexander Motin 	g_topology_assert();
199489b17223SAlexander Motin 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
199589b17223SAlexander Motin 	G_RAID_DEBUG(2, "Tasting provider %s.", pp->name);
199689b17223SAlexander Motin 
199789b17223SAlexander Motin 	gp = g_new_geomf(mp, "mirror:taste");
199889b17223SAlexander Motin 	/*
199989b17223SAlexander Motin 	 * This orphan function should be never called.
200089b17223SAlexander Motin 	 */
200189b17223SAlexander Motin 	gp->orphan = g_raid_taste_orphan;
200289b17223SAlexander Motin 	cp = g_new_consumer(gp);
200389b17223SAlexander Motin 	g_attach(cp, pp);
200489b17223SAlexander Motin 
200589b17223SAlexander Motin 	geom = NULL;
200689b17223SAlexander Motin 	LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
200789b17223SAlexander Motin 		G_RAID_DEBUG(2, "Tasting provider %s for %s metadata.",
200889b17223SAlexander Motin 		    pp->name, class->name);
200989b17223SAlexander Motin 		obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
201089b17223SAlexander Motin 		    M_WAITOK);
201189b17223SAlexander Motin 		obj->mdo_class = class;
201289b17223SAlexander Motin 		status = G_RAID_MD_TASTE(obj, mp, cp, &geom);
201389b17223SAlexander Motin 		if (status != G_RAID_MD_TASTE_NEW)
201489b17223SAlexander Motin 			kobj_delete((kobj_t)obj, M_RAID);
201589b17223SAlexander Motin 		if (status != G_RAID_MD_TASTE_FAIL)
201689b17223SAlexander Motin 			break;
201789b17223SAlexander Motin 	}
201889b17223SAlexander Motin 
201989b17223SAlexander Motin 	g_detach(cp);
202089b17223SAlexander Motin 	g_destroy_consumer(cp);
202189b17223SAlexander Motin 	g_destroy_geom(gp);
202289b17223SAlexander Motin 	G_RAID_DEBUG(2, "Tasting provider %s done.", pp->name);
202389b17223SAlexander Motin 	return (geom);
202489b17223SAlexander Motin }
202589b17223SAlexander Motin 
202689b17223SAlexander Motin int
202789b17223SAlexander Motin g_raid_create_node_format(const char *format, struct g_geom **gp)
202889b17223SAlexander Motin {
202989b17223SAlexander Motin 	struct g_raid_md_class *class;
203089b17223SAlexander Motin 	struct g_raid_md_object *obj;
203189b17223SAlexander Motin 	int status;
203289b17223SAlexander Motin 
203389b17223SAlexander Motin 	G_RAID_DEBUG(2, "Creating array for %s metadata.", format);
203489b17223SAlexander Motin 	LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
203589b17223SAlexander Motin 		if (strcasecmp(class->name, format) == 0)
203689b17223SAlexander Motin 			break;
203789b17223SAlexander Motin 	}
203889b17223SAlexander Motin 	if (class == NULL) {
203989b17223SAlexander Motin 		G_RAID_DEBUG(1, "No support for %s metadata.", format);
204089b17223SAlexander Motin 		return (G_RAID_MD_TASTE_FAIL);
204189b17223SAlexander Motin 	}
204289b17223SAlexander Motin 	obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
204389b17223SAlexander Motin 	    M_WAITOK);
204489b17223SAlexander Motin 	obj->mdo_class = class;
204589b17223SAlexander Motin 	status = G_RAID_MD_CREATE(obj, &g_raid_class, gp);
204689b17223SAlexander Motin 	if (status != G_RAID_MD_TASTE_NEW)
204789b17223SAlexander Motin 		kobj_delete((kobj_t)obj, M_RAID);
204889b17223SAlexander Motin 	return (status);
204989b17223SAlexander Motin }
205089b17223SAlexander Motin 
205189b17223SAlexander Motin static int
205289b17223SAlexander Motin g_raid_destroy_geom(struct gctl_req *req __unused,
205389b17223SAlexander Motin     struct g_class *mp __unused, struct g_geom *gp)
205489b17223SAlexander Motin {
205589b17223SAlexander Motin 	struct g_raid_softc *sc;
205689b17223SAlexander Motin 	int error;
205789b17223SAlexander Motin 
205889b17223SAlexander Motin 	g_topology_unlock();
205989b17223SAlexander Motin 	sc = gp->softc;
206089b17223SAlexander Motin 	sx_xlock(&sc->sc_lock);
206189b17223SAlexander Motin 	g_cancel_event(sc);
206289b17223SAlexander Motin 	error = g_raid_destroy(gp->softc, G_RAID_DESTROY_SOFT);
206389b17223SAlexander Motin 	if (error != 0)
206489b17223SAlexander Motin 		sx_xunlock(&sc->sc_lock);
206589b17223SAlexander Motin 	g_topology_lock();
206689b17223SAlexander Motin 	return (error);
206789b17223SAlexander Motin }
206889b17223SAlexander Motin 
206989b17223SAlexander Motin void g_raid_write_metadata(struct g_raid_softc *sc, struct g_raid_volume *vol,
207089b17223SAlexander Motin     struct g_raid_subdisk *sd, struct g_raid_disk *disk)
207189b17223SAlexander Motin {
207289b17223SAlexander Motin 
207389b17223SAlexander Motin 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
207489b17223SAlexander Motin 		return;
207589b17223SAlexander Motin 	if (sc->sc_md)
207689b17223SAlexander Motin 		G_RAID_MD_WRITE(sc->sc_md, vol, sd, disk);
207789b17223SAlexander Motin }
207889b17223SAlexander Motin 
207989b17223SAlexander Motin void g_raid_fail_disk(struct g_raid_softc *sc,
208089b17223SAlexander Motin     struct g_raid_subdisk *sd, struct g_raid_disk *disk)
208189b17223SAlexander Motin {
208289b17223SAlexander Motin 
208389b17223SAlexander Motin 	if (disk == NULL)
208489b17223SAlexander Motin 		disk = sd->sd_disk;
208589b17223SAlexander Motin 	if (disk == NULL) {
208689b17223SAlexander Motin 		G_RAID_DEBUG1(0, sc, "Warning! Fail request to an absent disk!");
208789b17223SAlexander Motin 		return;
208889b17223SAlexander Motin 	}
208989b17223SAlexander Motin 	if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
209089b17223SAlexander Motin 		G_RAID_DEBUG1(0, sc, "Warning! Fail request to a disk in a "
209189b17223SAlexander Motin 		    "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
209289b17223SAlexander Motin 		return;
209389b17223SAlexander Motin 	}
209489b17223SAlexander Motin 	if (sc->sc_md)
209589b17223SAlexander Motin 		G_RAID_MD_FAIL_DISK(sc->sc_md, sd, disk);
209689b17223SAlexander Motin }
209789b17223SAlexander Motin 
209889b17223SAlexander Motin static void
209989b17223SAlexander Motin g_raid_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
210089b17223SAlexander Motin     struct g_consumer *cp, struct g_provider *pp)
210189b17223SAlexander Motin {
210289b17223SAlexander Motin 	struct g_raid_softc *sc;
210389b17223SAlexander Motin 	struct g_raid_volume *vol;
210489b17223SAlexander Motin 	struct g_raid_subdisk *sd;
210589b17223SAlexander Motin 	struct g_raid_disk *disk;
210689b17223SAlexander Motin 	int i, s;
210789b17223SAlexander Motin 
210889b17223SAlexander Motin 	g_topology_assert();
210989b17223SAlexander Motin 
211089b17223SAlexander Motin 	sc = gp->softc;
211189b17223SAlexander Motin 	if (sc == NULL)
211289b17223SAlexander Motin 		return;
211389b17223SAlexander Motin 	if (pp != NULL) {
211489b17223SAlexander Motin 		vol = pp->private;
211589b17223SAlexander Motin 		g_topology_unlock();
211689b17223SAlexander Motin 		sx_xlock(&sc->sc_lock);
211789b17223SAlexander Motin 		sbuf_printf(sb, "%s<Label>%s</Label>\n", indent,
211889b17223SAlexander Motin 		    vol->v_name);
211989b17223SAlexander Motin 		sbuf_printf(sb, "%s<RAIDLevel>%s</RAIDLevel>\n", indent,
212089b17223SAlexander Motin 		    g_raid_volume_level2str(vol->v_raid_level,
212189b17223SAlexander Motin 		    vol->v_raid_level_qualifier));
212289b17223SAlexander Motin 		sbuf_printf(sb,
212389b17223SAlexander Motin 		    "%s<Transformation>%s</Transformation>\n", indent,
212489b17223SAlexander Motin 		    vol->v_tr ? vol->v_tr->tro_class->name : "NONE");
212589b17223SAlexander Motin 		sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
212689b17223SAlexander Motin 		    vol->v_disks_count);
212789b17223SAlexander Motin 		sbuf_printf(sb, "%s<Strip>%u</Strip>\n", indent,
212889b17223SAlexander Motin 		    vol->v_strip_size);
212989b17223SAlexander Motin 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
213089b17223SAlexander Motin 		    g_raid_volume_state2str(vol->v_state));
213189b17223SAlexander Motin 		sbuf_printf(sb, "%s<Dirty>%s</Dirty>\n", indent,
213289b17223SAlexander Motin 		    vol->v_dirty ? "Yes" : "No");
213389b17223SAlexander Motin 		sbuf_printf(sb, "%s<Subdisks>", indent);
213489b17223SAlexander Motin 		for (i = 0; i < vol->v_disks_count; i++) {
213589b17223SAlexander Motin 			sd = &vol->v_subdisks[i];
213689b17223SAlexander Motin 			if (sd->sd_disk != NULL &&
213789b17223SAlexander Motin 			    sd->sd_disk->d_consumer != NULL) {
213889b17223SAlexander Motin 				sbuf_printf(sb, "%s ",
213989b17223SAlexander Motin 				    g_raid_get_diskname(sd->sd_disk));
214089b17223SAlexander Motin 			} else {
214189b17223SAlexander Motin 				sbuf_printf(sb, "NONE ");
214289b17223SAlexander Motin 			}
214389b17223SAlexander Motin 			sbuf_printf(sb, "(%s",
214489b17223SAlexander Motin 			    g_raid_subdisk_state2str(sd->sd_state));
214589b17223SAlexander Motin 			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
214689b17223SAlexander Motin 			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
214789b17223SAlexander Motin 				sbuf_printf(sb, " %d%%",
214889b17223SAlexander Motin 				    (int)(sd->sd_rebuild_pos * 100 /
214989b17223SAlexander Motin 				     sd->sd_size));
215089b17223SAlexander Motin 			}
215189b17223SAlexander Motin 			sbuf_printf(sb, ")");
215289b17223SAlexander Motin 			if (i + 1 < vol->v_disks_count)
215389b17223SAlexander Motin 				sbuf_printf(sb, ", ");
215489b17223SAlexander Motin 		}
215589b17223SAlexander Motin 		sbuf_printf(sb, "</Subdisks>\n");
215689b17223SAlexander Motin 		sx_xunlock(&sc->sc_lock);
215789b17223SAlexander Motin 		g_topology_lock();
215889b17223SAlexander Motin 	} else if (cp != NULL) {
215989b17223SAlexander Motin 		disk = cp->private;
216089b17223SAlexander Motin 		if (disk == NULL)
216189b17223SAlexander Motin 			return;
216289b17223SAlexander Motin 		g_topology_unlock();
216389b17223SAlexander Motin 		sx_xlock(&sc->sc_lock);
216489b17223SAlexander Motin 		sbuf_printf(sb, "%s<State>%s", indent,
216589b17223SAlexander Motin 		    g_raid_disk_state2str(disk->d_state));
216689b17223SAlexander Motin 		if (!TAILQ_EMPTY(&disk->d_subdisks)) {
216789b17223SAlexander Motin 			sbuf_printf(sb, " (");
216889b17223SAlexander Motin 			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
216989b17223SAlexander Motin 				sbuf_printf(sb, "%s",
217089b17223SAlexander Motin 				    g_raid_subdisk_state2str(sd->sd_state));
217189b17223SAlexander Motin 				if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
217289b17223SAlexander Motin 				    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
217389b17223SAlexander Motin 					sbuf_printf(sb, " %d%%",
217489b17223SAlexander Motin 					    (int)(sd->sd_rebuild_pos * 100 /
217589b17223SAlexander Motin 					     sd->sd_size));
217689b17223SAlexander Motin 				}
217789b17223SAlexander Motin 				if (TAILQ_NEXT(sd, sd_next))
217889b17223SAlexander Motin 					sbuf_printf(sb, ", ");
217989b17223SAlexander Motin 			}
218089b17223SAlexander Motin 			sbuf_printf(sb, ")");
218189b17223SAlexander Motin 		}
218289b17223SAlexander Motin 		sbuf_printf(sb, "</State>\n");
218389b17223SAlexander Motin 		sbuf_printf(sb, "%s<Subdisks>", indent);
218489b17223SAlexander Motin 		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
218589b17223SAlexander Motin 			sbuf_printf(sb, "r%d(%s):%d@%ju",
218689b17223SAlexander Motin 			    sd->sd_volume->v_global_id,
218789b17223SAlexander Motin 			    sd->sd_volume->v_name,
218889b17223SAlexander Motin 			    sd->sd_pos, sd->sd_offset);
218989b17223SAlexander Motin 			if (TAILQ_NEXT(sd, sd_next))
219089b17223SAlexander Motin 				sbuf_printf(sb, ", ");
219189b17223SAlexander Motin 		}
219289b17223SAlexander Motin 		sbuf_printf(sb, "</Subdisks>\n");
219389b17223SAlexander Motin 		sbuf_printf(sb, "%s<ReadErrors>%d</ReadErrors>\n", indent,
219489b17223SAlexander Motin 		    disk->d_read_errs);
219589b17223SAlexander Motin 		sx_xunlock(&sc->sc_lock);
219689b17223SAlexander Motin 		g_topology_lock();
219789b17223SAlexander Motin 	} else {
219889b17223SAlexander Motin 		g_topology_unlock();
219989b17223SAlexander Motin 		sx_xlock(&sc->sc_lock);
220089b17223SAlexander Motin 		if (sc->sc_md) {
220189b17223SAlexander Motin 			sbuf_printf(sb, "%s<Metadata>%s</Metadata>\n", indent,
220289b17223SAlexander Motin 			    sc->sc_md->mdo_class->name);
220389b17223SAlexander Motin 		}
220489b17223SAlexander Motin 		if (!TAILQ_EMPTY(&sc->sc_volumes)) {
220589b17223SAlexander Motin 			s = 0xff;
220689b17223SAlexander Motin 			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
220789b17223SAlexander Motin 				if (vol->v_state < s)
220889b17223SAlexander Motin 					s = vol->v_state;
220989b17223SAlexander Motin 			}
221089b17223SAlexander Motin 			sbuf_printf(sb, "%s<State>%s</State>\n", indent,
221189b17223SAlexander Motin 			    g_raid_volume_state2str(s));
221289b17223SAlexander Motin 		}
221389b17223SAlexander Motin 		sx_xunlock(&sc->sc_lock);
221489b17223SAlexander Motin 		g_topology_lock();
221589b17223SAlexander Motin 	}
221689b17223SAlexander Motin }
221789b17223SAlexander Motin 
221889b17223SAlexander Motin static void
221989b17223SAlexander Motin g_raid_shutdown_pre_sync(void *arg, int howto)
222089b17223SAlexander Motin {
222189b17223SAlexander Motin 	struct g_class *mp;
222289b17223SAlexander Motin 	struct g_geom *gp, *gp2;
222389b17223SAlexander Motin 	struct g_raid_softc *sc;
222489b17223SAlexander Motin 	int error;
222589b17223SAlexander Motin 
222689b17223SAlexander Motin 	mp = arg;
222789b17223SAlexander Motin 	DROP_GIANT();
222889b17223SAlexander Motin 	g_topology_lock();
222989b17223SAlexander Motin 	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
223089b17223SAlexander Motin 		if ((sc = gp->softc) == NULL)
223189b17223SAlexander Motin 			continue;
223289b17223SAlexander Motin 		g_topology_unlock();
223389b17223SAlexander Motin 		sx_xlock(&sc->sc_lock);
223489b17223SAlexander Motin 		g_cancel_event(sc);
223589b17223SAlexander Motin 		error = g_raid_destroy(sc, G_RAID_DESTROY_DELAYED);
223689b17223SAlexander Motin 		if (error != 0)
223789b17223SAlexander Motin 			sx_xunlock(&sc->sc_lock);
223889b17223SAlexander Motin 		g_topology_lock();
223989b17223SAlexander Motin 	}
224089b17223SAlexander Motin 	g_topology_unlock();
224189b17223SAlexander Motin 	PICKUP_GIANT();
224289b17223SAlexander Motin }
224389b17223SAlexander Motin 
224489b17223SAlexander Motin static void
224589b17223SAlexander Motin g_raid_init(struct g_class *mp)
224689b17223SAlexander Motin {
224789b17223SAlexander Motin 
224889b17223SAlexander Motin 	g_raid_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
224989b17223SAlexander Motin 	    g_raid_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
225089b17223SAlexander Motin 	if (g_raid_pre_sync == NULL)
225189b17223SAlexander Motin 		G_RAID_DEBUG(0, "Warning! Cannot register shutdown event.");
225289b17223SAlexander Motin 	g_raid_started = 1;
225389b17223SAlexander Motin }
225489b17223SAlexander Motin 
225589b17223SAlexander Motin static void
225689b17223SAlexander Motin g_raid_fini(struct g_class *mp)
225789b17223SAlexander Motin {
225889b17223SAlexander Motin 
225989b17223SAlexander Motin 	if (g_raid_pre_sync != NULL)
226089b17223SAlexander Motin 		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_raid_pre_sync);
226189b17223SAlexander Motin 	g_raid_started = 0;
226289b17223SAlexander Motin }
226389b17223SAlexander Motin 
226489b17223SAlexander Motin int
226589b17223SAlexander Motin g_raid_md_modevent(module_t mod, int type, void *arg)
226689b17223SAlexander Motin {
226789b17223SAlexander Motin 	struct g_raid_md_class *class, *c, *nc;
226889b17223SAlexander Motin 	int error;
226989b17223SAlexander Motin 
227089b17223SAlexander Motin 	error = 0;
227189b17223SAlexander Motin 	class = arg;
227289b17223SAlexander Motin 	switch (type) {
227389b17223SAlexander Motin 	case MOD_LOAD:
227489b17223SAlexander Motin 		c = LIST_FIRST(&g_raid_md_classes);
227589b17223SAlexander Motin 		if (c == NULL || c->mdc_priority > class->mdc_priority)
227689b17223SAlexander Motin 			LIST_INSERT_HEAD(&g_raid_md_classes, class, mdc_list);
227789b17223SAlexander Motin 		else {
227889b17223SAlexander Motin 			while ((nc = LIST_NEXT(c, mdc_list)) != NULL &&
227989b17223SAlexander Motin 			    nc->mdc_priority < class->mdc_priority)
228089b17223SAlexander Motin 				c = nc;
228189b17223SAlexander Motin 			LIST_INSERT_AFTER(c, class, mdc_list);
228289b17223SAlexander Motin 		}
228389b17223SAlexander Motin 		if (g_raid_started)
228489b17223SAlexander Motin 			g_retaste(&g_raid_class);
228589b17223SAlexander Motin 		break;
228689b17223SAlexander Motin 	case MOD_UNLOAD:
228789b17223SAlexander Motin 		LIST_REMOVE(class, mdc_list);
228889b17223SAlexander Motin 		break;
228989b17223SAlexander Motin 	default:
229089b17223SAlexander Motin 		error = EOPNOTSUPP;
229189b17223SAlexander Motin 		break;
229289b17223SAlexander Motin 	}
229389b17223SAlexander Motin 
229489b17223SAlexander Motin 	return (error);
229589b17223SAlexander Motin }
229689b17223SAlexander Motin 
229789b17223SAlexander Motin int
229889b17223SAlexander Motin g_raid_tr_modevent(module_t mod, int type, void *arg)
229989b17223SAlexander Motin {
230089b17223SAlexander Motin 	struct g_raid_tr_class *class, *c, *nc;
230189b17223SAlexander Motin 	int error;
230289b17223SAlexander Motin 
230389b17223SAlexander Motin 	error = 0;
230489b17223SAlexander Motin 	class = arg;
230589b17223SAlexander Motin 	switch (type) {
230689b17223SAlexander Motin 	case MOD_LOAD:
230789b17223SAlexander Motin 		c = LIST_FIRST(&g_raid_tr_classes);
230889b17223SAlexander Motin 		if (c == NULL || c->trc_priority > class->trc_priority)
230989b17223SAlexander Motin 			LIST_INSERT_HEAD(&g_raid_tr_classes, class, trc_list);
231089b17223SAlexander Motin 		else {
231189b17223SAlexander Motin 			while ((nc = LIST_NEXT(c, trc_list)) != NULL &&
231289b17223SAlexander Motin 			    nc->trc_priority < class->trc_priority)
231389b17223SAlexander Motin 				c = nc;
231489b17223SAlexander Motin 			LIST_INSERT_AFTER(c, class, trc_list);
231589b17223SAlexander Motin 		}
231689b17223SAlexander Motin 		break;
231789b17223SAlexander Motin 	case MOD_UNLOAD:
231889b17223SAlexander Motin 		LIST_REMOVE(class, trc_list);
231989b17223SAlexander Motin 		break;
232089b17223SAlexander Motin 	default:
232189b17223SAlexander Motin 		error = EOPNOTSUPP;
232289b17223SAlexander Motin 		break;
232389b17223SAlexander Motin 	}
232489b17223SAlexander Motin 
232589b17223SAlexander Motin 	return (error);
232689b17223SAlexander Motin }
232789b17223SAlexander Motin 
232889b17223SAlexander Motin /*
232989b17223SAlexander Motin  * Use local implementation of DECLARE_GEOM_CLASS(g_raid_class, g_raid)
233089b17223SAlexander Motin  * to reduce module priority, allowing submodules to register them first.
233189b17223SAlexander Motin  */
233289b17223SAlexander Motin static moduledata_t g_raid_mod = {
233389b17223SAlexander Motin 	"g_raid",
233489b17223SAlexander Motin 	g_modevent,
233589b17223SAlexander Motin 	&g_raid_class
233689b17223SAlexander Motin };
233789b17223SAlexander Motin DECLARE_MODULE(g_raid, g_raid_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD);
233889b17223SAlexander Motin MODULE_VERSION(geom_raid, 0);
2339