g_raid.c revision 234603
1219974Smav/*-
2219974Smav * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
3219974Smav * All rights reserved.
4219974Smav *
5219974Smav * Redistribution and use in source and binary forms, with or without
6219974Smav * modification, are permitted provided that the following conditions
7219974Smav * are met:
8219974Smav * 1. Redistributions of source code must retain the above copyright
9219974Smav *    notice, this list of conditions and the following disclaimer.
10219974Smav * 2. Redistributions in binary form must reproduce the above copyright
11219974Smav *    notice, this list of conditions and the following disclaimer in the
12219974Smav *    documentation and/or other materials provided with the distribution.
13219974Smav *
14219974Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15219974Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16219974Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17219974Smav * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18219974Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19219974Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20219974Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21219974Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22219974Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23219974Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24219974Smav * SUCH DAMAGE.
25219974Smav */
26219974Smav
27219974Smav#include <sys/cdefs.h>
28219974Smav__FBSDID("$FreeBSD: head/sys/geom/raid/g_raid.c 234603 2012-04-23 13:04:02Z mav $");
29219974Smav
30219974Smav#include <sys/param.h>
31219974Smav#include <sys/systm.h>
32219974Smav#include <sys/kernel.h>
33219974Smav#include <sys/module.h>
34219974Smav#include <sys/limits.h>
35219974Smav#include <sys/lock.h>
36219974Smav#include <sys/mutex.h>
37219974Smav#include <sys/bio.h>
38223921Sae#include <sys/sbuf.h>
39219974Smav#include <sys/sysctl.h>
40219974Smav#include <sys/malloc.h>
41219974Smav#include <sys/eventhandler.h>
42219974Smav#include <vm/uma.h>
43219974Smav#include <geom/geom.h>
44219974Smav#include <sys/proc.h>
45219974Smav#include <sys/kthread.h>
46219974Smav#include <sys/sched.h>
47219974Smav#include <geom/raid/g_raid.h>
48219974Smav#include "g_raid_md_if.h"
49219974Smav#include "g_raid_tr_if.h"
50219974Smav
51219974Smavstatic MALLOC_DEFINE(M_RAID, "raid_data", "GEOM_RAID Data");
52219974Smav
53219974SmavSYSCTL_DECL(_kern_geom);
54219974SmavSYSCTL_NODE(_kern_geom, OID_AUTO, raid, CTLFLAG_RW, 0, "GEOM_RAID stuff");
55219974Smavu_int g_raid_aggressive_spare = 0;
56219974SmavTUNABLE_INT("kern.geom.raid.aggressive_spare", &g_raid_aggressive_spare);
57219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, aggressive_spare, CTLFLAG_RW,
58219974Smav    &g_raid_aggressive_spare, 0, "Use disks without metadata as spare");
59220790Smavu_int g_raid_debug = 0;
60219974SmavTUNABLE_INT("kern.geom.raid.debug", &g_raid_debug);
61219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, debug, CTLFLAG_RW, &g_raid_debug, 0,
62219974Smav    "Debug level");
63219974Smavint g_raid_read_err_thresh = 10;
64219974SmavTUNABLE_INT("kern.geom.raid.read_err_thresh", &g_raid_read_err_thresh);
65219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, read_err_thresh, CTLFLAG_RW,
66219974Smav    &g_raid_read_err_thresh, 0,
67219974Smav    "Number of read errors equated to disk failure");
68219974Smavu_int g_raid_start_timeout = 30;
69219974SmavTUNABLE_INT("kern.geom.raid.start_timeout", &g_raid_start_timeout);
70219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, start_timeout, CTLFLAG_RW,
71219974Smav    &g_raid_start_timeout, 0,
72219974Smav    "Time to wait for all array components");
73219974Smavstatic u_int g_raid_clean_time = 5;
74219974SmavTUNABLE_INT("kern.geom.raid.clean_time", &g_raid_clean_time);
75219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, clean_time, CTLFLAG_RW,
76219974Smav    &g_raid_clean_time, 0, "Mark volume as clean when idling");
77219974Smavstatic u_int g_raid_disconnect_on_failure = 1;
78219974SmavTUNABLE_INT("kern.geom.raid.disconnect_on_failure",
79219974Smav    &g_raid_disconnect_on_failure);
80219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
81219974Smav    &g_raid_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
82219974Smavstatic u_int g_raid_name_format = 0;
83219974SmavTUNABLE_INT("kern.geom.raid.name_format", &g_raid_name_format);
84219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, name_format, CTLFLAG_RW,
85219974Smav    &g_raid_name_format, 0, "Providers name format.");
86219974Smavstatic u_int g_raid_idle_threshold = 1000000;
87219974SmavTUNABLE_INT("kern.geom.raid.idle_threshold", &g_raid_idle_threshold);
88219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, idle_threshold, CTLFLAG_RW,
89219974Smav    &g_raid_idle_threshold, 1000000,
90219974Smav    "Time in microseconds to consider a volume idle.");
91219974Smav
92219974Smav#define	MSLEEP(rv, ident, mtx, priority, wmesg, timeout)	do {	\
93219974Smav	G_RAID_DEBUG(4, "%s: Sleeping %p.", __func__, (ident));		\
94219974Smav	rv = msleep((ident), (mtx), (priority), (wmesg), (timeout));	\
95219974Smav	G_RAID_DEBUG(4, "%s: Woken up %p.", __func__, (ident));		\
96219974Smav} while (0)
97219974Smav
98219974SmavLIST_HEAD(, g_raid_md_class) g_raid_md_classes =
99219974Smav    LIST_HEAD_INITIALIZER(g_raid_md_classes);
100219974Smav
101219974SmavLIST_HEAD(, g_raid_tr_class) g_raid_tr_classes =
102219974Smav    LIST_HEAD_INITIALIZER(g_raid_tr_classes);
103219974Smav
104219974SmavLIST_HEAD(, g_raid_volume) g_raid_volumes =
105219974Smav    LIST_HEAD_INITIALIZER(g_raid_volumes);
106219974Smav
107219974Smavstatic eventhandler_tag g_raid_pre_sync = NULL;
108219974Smavstatic int g_raid_started = 0;
109219974Smav
110219974Smavstatic int g_raid_destroy_geom(struct gctl_req *req, struct g_class *mp,
111219974Smav    struct g_geom *gp);
112219974Smavstatic g_taste_t g_raid_taste;
113219974Smavstatic void g_raid_init(struct g_class *mp);
114219974Smavstatic void g_raid_fini(struct g_class *mp);
115219974Smav
116219974Smavstruct g_class g_raid_class = {
117219974Smav	.name = G_RAID_CLASS_NAME,
118219974Smav	.version = G_VERSION,
119219974Smav	.ctlreq = g_raid_ctl,
120219974Smav	.taste = g_raid_taste,
121219974Smav	.destroy_geom = g_raid_destroy_geom,
122219974Smav	.init = g_raid_init,
123219974Smav	.fini = g_raid_fini
124219974Smav};
125219974Smav
126219974Smavstatic void g_raid_destroy_provider(struct g_raid_volume *vol);
127219974Smavstatic int g_raid_update_disk(struct g_raid_disk *disk, u_int event);
128219974Smavstatic int g_raid_update_subdisk(struct g_raid_subdisk *subdisk, u_int event);
129219974Smavstatic int g_raid_update_volume(struct g_raid_volume *vol, u_int event);
130219974Smavstatic int g_raid_update_node(struct g_raid_softc *sc, u_int event);
131219974Smavstatic void g_raid_dumpconf(struct sbuf *sb, const char *indent,
132219974Smav    struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
133219974Smavstatic void g_raid_start(struct bio *bp);
134219974Smavstatic void g_raid_start_request(struct bio *bp);
135219974Smavstatic void g_raid_disk_done(struct bio *bp);
136219974Smavstatic void g_raid_poll(struct g_raid_softc *sc);
137219974Smav
138219974Smavstatic const char *
139219974Smavg_raid_node_event2str(int event)
140219974Smav{
141219974Smav
142219974Smav	switch (event) {
143219974Smav	case G_RAID_NODE_E_WAKE:
144219974Smav		return ("WAKE");
145219974Smav	case G_RAID_NODE_E_START:
146219974Smav		return ("START");
147219974Smav	default:
148219974Smav		return ("INVALID");
149219974Smav	}
150219974Smav}
151219974Smav
152219974Smavconst char *
153219974Smavg_raid_disk_state2str(int state)
154219974Smav{
155219974Smav
156219974Smav	switch (state) {
157219974Smav	case G_RAID_DISK_S_NONE:
158219974Smav		return ("NONE");
159219974Smav	case G_RAID_DISK_S_OFFLINE:
160219974Smav		return ("OFFLINE");
161219974Smav	case G_RAID_DISK_S_FAILED:
162219974Smav		return ("FAILED");
163219974Smav	case G_RAID_DISK_S_STALE_FAILED:
164219974Smav		return ("STALE_FAILED");
165219974Smav	case G_RAID_DISK_S_SPARE:
166219974Smav		return ("SPARE");
167219974Smav	case G_RAID_DISK_S_STALE:
168219974Smav		return ("STALE");
169219974Smav	case G_RAID_DISK_S_ACTIVE:
170219974Smav		return ("ACTIVE");
171219974Smav	default:
172219974Smav		return ("INVALID");
173219974Smav	}
174219974Smav}
175219974Smav
176219974Smavstatic const char *
177219974Smavg_raid_disk_event2str(int event)
178219974Smav{
179219974Smav
180219974Smav	switch (event) {
181219974Smav	case G_RAID_DISK_E_DISCONNECTED:
182219974Smav		return ("DISCONNECTED");
183219974Smav	default:
184219974Smav		return ("INVALID");
185219974Smav	}
186219974Smav}
187219974Smav
188219974Smavconst char *
189219974Smavg_raid_subdisk_state2str(int state)
190219974Smav{
191219974Smav
192219974Smav	switch (state) {
193219974Smav	case G_RAID_SUBDISK_S_NONE:
194219974Smav		return ("NONE");
195219974Smav	case G_RAID_SUBDISK_S_FAILED:
196219974Smav		return ("FAILED");
197219974Smav	case G_RAID_SUBDISK_S_NEW:
198219974Smav		return ("NEW");
199219974Smav	case G_RAID_SUBDISK_S_REBUILD:
200219974Smav		return ("REBUILD");
201219974Smav	case G_RAID_SUBDISK_S_UNINITIALIZED:
202219974Smav		return ("UNINITIALIZED");
203219974Smav	case G_RAID_SUBDISK_S_STALE:
204219974Smav		return ("STALE");
205219974Smav	case G_RAID_SUBDISK_S_RESYNC:
206219974Smav		return ("RESYNC");
207219974Smav	case G_RAID_SUBDISK_S_ACTIVE:
208219974Smav		return ("ACTIVE");
209219974Smav	default:
210219974Smav		return ("INVALID");
211219974Smav	}
212219974Smav}
213219974Smav
214219974Smavstatic const char *
215219974Smavg_raid_subdisk_event2str(int event)
216219974Smav{
217219974Smav
218219974Smav	switch (event) {
219219974Smav	case G_RAID_SUBDISK_E_NEW:
220219974Smav		return ("NEW");
221219974Smav	case G_RAID_SUBDISK_E_DISCONNECTED:
222219974Smav		return ("DISCONNECTED");
223219974Smav	default:
224219974Smav		return ("INVALID");
225219974Smav	}
226219974Smav}
227219974Smav
228219974Smavconst char *
229219974Smavg_raid_volume_state2str(int state)
230219974Smav{
231219974Smav
232219974Smav	switch (state) {
233219974Smav	case G_RAID_VOLUME_S_STARTING:
234219974Smav		return ("STARTING");
235219974Smav	case G_RAID_VOLUME_S_BROKEN:
236219974Smav		return ("BROKEN");
237219974Smav	case G_RAID_VOLUME_S_DEGRADED:
238219974Smav		return ("DEGRADED");
239219974Smav	case G_RAID_VOLUME_S_SUBOPTIMAL:
240219974Smav		return ("SUBOPTIMAL");
241219974Smav	case G_RAID_VOLUME_S_OPTIMAL:
242219974Smav		return ("OPTIMAL");
243219974Smav	case G_RAID_VOLUME_S_UNSUPPORTED:
244219974Smav		return ("UNSUPPORTED");
245219974Smav	case G_RAID_VOLUME_S_STOPPED:
246219974Smav		return ("STOPPED");
247219974Smav	default:
248219974Smav		return ("INVALID");
249219974Smav	}
250219974Smav}
251219974Smav
252219974Smavstatic const char *
253219974Smavg_raid_volume_event2str(int event)
254219974Smav{
255219974Smav
256219974Smav	switch (event) {
257219974Smav	case G_RAID_VOLUME_E_UP:
258219974Smav		return ("UP");
259219974Smav	case G_RAID_VOLUME_E_DOWN:
260219974Smav		return ("DOWN");
261219974Smav	case G_RAID_VOLUME_E_START:
262219974Smav		return ("START");
263219974Smav	case G_RAID_VOLUME_E_STARTMD:
264219974Smav		return ("STARTMD");
265219974Smav	default:
266219974Smav		return ("INVALID");
267219974Smav	}
268219974Smav}
269219974Smav
270219974Smavconst char *
271219974Smavg_raid_volume_level2str(int level, int qual)
272219974Smav{
273219974Smav
274219974Smav	switch (level) {
275219974Smav	case G_RAID_VOLUME_RL_RAID0:
276219974Smav		return ("RAID0");
277219974Smav	case G_RAID_VOLUME_RL_RAID1:
278219974Smav		return ("RAID1");
279219974Smav	case G_RAID_VOLUME_RL_RAID3:
280234603Smav		if (qual == G_RAID_VOLUME_RLQ_R3P0)
281234603Smav			return ("RAID3-P0");
282234603Smav		if (qual == G_RAID_VOLUME_RLQ_R3PN)
283234603Smav			return ("RAID3-PN");
284219974Smav		return ("RAID3");
285219974Smav	case G_RAID_VOLUME_RL_RAID4:
286234603Smav		if (qual == G_RAID_VOLUME_RLQ_R4P0)
287234603Smav			return ("RAID3-P0");
288234603Smav		if (qual == G_RAID_VOLUME_RLQ_R4PN)
289234603Smav			return ("RAID3-PN");
290219974Smav		return ("RAID4");
291219974Smav	case G_RAID_VOLUME_RL_RAID5:
292234458Smav		if (qual == G_RAID_VOLUME_RLQ_R5RA)
293234603Smav			return ("RAID5-RA");
294234458Smav		if (qual == G_RAID_VOLUME_RLQ_R5RS)
295234603Smav			return ("RAID5-RS");
296234458Smav		if (qual == G_RAID_VOLUME_RLQ_R5LA)
297234603Smav			return ("RAID5-LA");
298234458Smav		if (qual == G_RAID_VOLUME_RLQ_R5LS)
299234603Smav			return ("RAID5-LS");
300219974Smav		return ("RAID5");
301219974Smav	case G_RAID_VOLUME_RL_RAID6:
302234603Smav		if (qual == G_RAID_VOLUME_RLQ_R6RA)
303234603Smav			return ("RAID6-RA");
304234603Smav		if (qual == G_RAID_VOLUME_RLQ_R6RS)
305234603Smav			return ("RAID6-RS");
306234603Smav		if (qual == G_RAID_VOLUME_RLQ_R6LA)
307234603Smav			return ("RAID6-LA");
308234603Smav		if (qual == G_RAID_VOLUME_RLQ_R6LS)
309234603Smav			return ("RAID6-LS");
310219974Smav		return ("RAID6");
311234603Smav	case G_RAID_VOLUME_RL_RAIDMDF:
312234603Smav		if (qual == G_RAID_VOLUME_RLQ_RMDFRA)
313234603Smav			return ("RAIDMDF-RA");
314234603Smav		if (qual == G_RAID_VOLUME_RLQ_RMDFRS)
315234603Smav			return ("RAIDMDF-RS");
316234603Smav		if (qual == G_RAID_VOLUME_RLQ_RMDFLA)
317234603Smav			return ("RAIDMDF-LA");
318234603Smav		if (qual == G_RAID_VOLUME_RLQ_RMDFLS)
319234603Smav			return ("RAIDMDF-LS");
320234603Smav		return ("RAIDMDF");
321219974Smav	case G_RAID_VOLUME_RL_RAID1E:
322234603Smav		if (qual == G_RAID_VOLUME_RLQ_R1EA)
323234603Smav			return ("RAID1E-A");
324234603Smav		if (qual == G_RAID_VOLUME_RLQ_R1EO)
325234603Smav			return ("RAID1E-O");
326219974Smav		return ("RAID1E");
327219974Smav	case G_RAID_VOLUME_RL_SINGLE:
328219974Smav		return ("SINGLE");
329219974Smav	case G_RAID_VOLUME_RL_CONCAT:
330219974Smav		return ("CONCAT");
331219974Smav	case G_RAID_VOLUME_RL_RAID5E:
332234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5ERA)
333234603Smav			return ("RAID5E-RA");
334234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5ERS)
335234603Smav			return ("RAID5E-RS");
336234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5ELA)
337234603Smav			return ("RAID5E-LA");
338234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5ELS)
339234603Smav			return ("RAID5E-LS");
340219974Smav		return ("RAID5E");
341219974Smav	case G_RAID_VOLUME_RL_RAID5EE:
342234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5EERA)
343234603Smav			return ("RAID5EE-RA");
344234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5EERS)
345234603Smav			return ("RAID5EE-RS");
346234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5EELA)
347234603Smav			return ("RAID5EE-LA");
348234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5EELS)
349234603Smav			return ("RAID5EE-LS");
350219974Smav		return ("RAID5EE");
351234603Smav	case G_RAID_VOLUME_RL_RAID5R:
352234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5RRA)
353234603Smav			return ("RAID5R-RA");
354234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5RRS)
355234603Smav			return ("RAID5R-RS");
356234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5RLA)
357234603Smav			return ("RAID5R-LA");
358234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5RLS)
359234603Smav			return ("RAID5R-LS");
360234603Smav		return ("RAID5E");
361219974Smav	default:
362219974Smav		return ("UNKNOWN");
363219974Smav	}
364219974Smav}
365219974Smav
366219974Smavint
367219974Smavg_raid_volume_str2level(const char *str, int *level, int *qual)
368219974Smav{
369219974Smav
370219974Smav	*level = G_RAID_VOLUME_RL_UNKNOWN;
371219974Smav	*qual = G_RAID_VOLUME_RLQ_NONE;
372219974Smav	if (strcasecmp(str, "RAID0") == 0)
373219974Smav		*level = G_RAID_VOLUME_RL_RAID0;
374219974Smav	else if (strcasecmp(str, "RAID1") == 0)
375219974Smav		*level = G_RAID_VOLUME_RL_RAID1;
376234603Smav	else if (strcasecmp(str, "RAID3-P0") == 0) {
377219974Smav		*level = G_RAID_VOLUME_RL_RAID3;
378234603Smav		*qual = G_RAID_VOLUME_RLQ_R3P0;
379234603Smav	} else if (strcasecmp(str, "RAID3-PN") == 0 &&
380234603Smav		   strcasecmp(str, "RAID3") == 0) {
381234603Smav		*level = G_RAID_VOLUME_RL_RAID3;
382234603Smav		*qual = G_RAID_VOLUME_RLQ_R3P0;
383234603Smav	} else if (strcasecmp(str, "RAID4-P0") == 0) {
384219974Smav		*level = G_RAID_VOLUME_RL_RAID4;
385234603Smav		*qual = G_RAID_VOLUME_RLQ_R4P0;
386234603Smav	} else if (strcasecmp(str, "RAID4-PN") == 0 &&
387234603Smav		   strcasecmp(str, "RAID4") == 0) {
388234603Smav		*level = G_RAID_VOLUME_RL_RAID4;
389234603Smav		*qual = G_RAID_VOLUME_RLQ_R4P0;
390234603Smav	} else if (strcasecmp(str, "RAID5-RA") == 0) {
391219974Smav		*level = G_RAID_VOLUME_RL_RAID5;
392234458Smav		*qual = G_RAID_VOLUME_RLQ_R5RA;
393234603Smav	} else if (strcasecmp(str, "RAID5-RS") == 0) {
394234458Smav		*level = G_RAID_VOLUME_RL_RAID5;
395234458Smav		*qual = G_RAID_VOLUME_RLQ_R5RS;
396234458Smav	} else if (strcasecmp(str, "RAID5") == 0 ||
397234603Smav		   strcasecmp(str, "RAID5-LA") == 0) {
398234458Smav		*level = G_RAID_VOLUME_RL_RAID5;
399234458Smav		*qual = G_RAID_VOLUME_RLQ_R5LA;
400234603Smav	} else if (strcasecmp(str, "RAID5-LS") == 0) {
401234458Smav		*level = G_RAID_VOLUME_RL_RAID5;
402234458Smav		*qual = G_RAID_VOLUME_RLQ_R5LS;
403234603Smav	} else if (strcasecmp(str, "RAID6-RA") == 0) {
404219974Smav		*level = G_RAID_VOLUME_RL_RAID6;
405234603Smav		*qual = G_RAID_VOLUME_RLQ_R6RA;
406234603Smav	} else if (strcasecmp(str, "RAID6-RS") == 0) {
407234603Smav		*level = G_RAID_VOLUME_RL_RAID6;
408234603Smav		*qual = G_RAID_VOLUME_RLQ_R6RS;
409234603Smav	} else if (strcasecmp(str, "RAID6") == 0 ||
410234603Smav		   strcasecmp(str, "RAID6-LA") == 0) {
411234603Smav		*level = G_RAID_VOLUME_RL_RAID6;
412234603Smav		*qual = G_RAID_VOLUME_RLQ_R6LA;
413234603Smav	} else if (strcasecmp(str, "RAID6-LS") == 0) {
414234603Smav		*level = G_RAID_VOLUME_RL_RAID6;
415234603Smav		*qual = G_RAID_VOLUME_RLQ_R6LS;
416234603Smav	} else if (strcasecmp(str, "RAIDMDF-RA") == 0) {
417234603Smav		*level = G_RAID_VOLUME_RL_RAIDMDF;
418234603Smav		*qual = G_RAID_VOLUME_RLQ_RMDFRA;
419234603Smav	} else if (strcasecmp(str, "RAIDMDF-RS") == 0) {
420234603Smav		*level = G_RAID_VOLUME_RL_RAIDMDF;
421234603Smav		*qual = G_RAID_VOLUME_RLQ_RMDFRS;
422234603Smav	} else if (strcasecmp(str, "RAIDMDF") == 0 ||
423234603Smav		   strcasecmp(str, "RAIDMDF-LA") == 0) {
424234603Smav		*level = G_RAID_VOLUME_RL_RAIDMDF;
425234603Smav		*qual = G_RAID_VOLUME_RLQ_RMDFLA;
426234603Smav	} else if (strcasecmp(str, "RAIDMDF-LS") == 0) {
427234603Smav		*level = G_RAID_VOLUME_RL_RAIDMDF;
428234603Smav		*qual = G_RAID_VOLUME_RLQ_RMDFLS;
429234603Smav	} else if (strcasecmp(str, "RAID10") == 0 ||
430234603Smav		   strcasecmp(str, "RAID1E") == 0 ||
431234603Smav		   strcasecmp(str, "RAID1E-A") == 0) {
432219974Smav		*level = G_RAID_VOLUME_RL_RAID1E;
433234603Smav		*qual = G_RAID_VOLUME_RLQ_R1EA;
434234603Smav	} else if (strcasecmp(str, "RAID1E-O") == 0) {
435234603Smav		*level = G_RAID_VOLUME_RL_RAID1E;
436234603Smav		*qual = G_RAID_VOLUME_RLQ_R1EO;
437234603Smav	} else if (strcasecmp(str, "SINGLE") == 0)
438219974Smav		*level = G_RAID_VOLUME_RL_SINGLE;
439219974Smav	else if (strcasecmp(str, "CONCAT") == 0)
440219974Smav		*level = G_RAID_VOLUME_RL_CONCAT;
441234603Smav	else if (strcasecmp(str, "RAID5E-RA") == 0) {
442219974Smav		*level = G_RAID_VOLUME_RL_RAID5E;
443234603Smav		*qual = G_RAID_VOLUME_RLQ_R5ERA;
444234603Smav	} else if (strcasecmp(str, "RAID5E-RS") == 0) {
445234603Smav		*level = G_RAID_VOLUME_RL_RAID5E;
446234603Smav		*qual = G_RAID_VOLUME_RLQ_R5ERS;
447234603Smav	} else if (strcasecmp(str, "RAID5E") == 0 ||
448234603Smav		   strcasecmp(str, "RAID5E-LA") == 0) {
449234603Smav		*level = G_RAID_VOLUME_RL_RAID5E;
450234603Smav		*qual = G_RAID_VOLUME_RLQ_R5ELA;
451234603Smav	} else if (strcasecmp(str, "RAID5E-LS") == 0) {
452234603Smav		*level = G_RAID_VOLUME_RL_RAID5E;
453234603Smav		*qual = G_RAID_VOLUME_RLQ_R5ELS;
454234603Smav	} else if (strcasecmp(str, "RAID5EE-RA") == 0) {
455219974Smav		*level = G_RAID_VOLUME_RL_RAID5EE;
456234603Smav		*qual = G_RAID_VOLUME_RLQ_R5EERA;
457234603Smav	} else if (strcasecmp(str, "RAID5EE-RS") == 0) {
458234603Smav		*level = G_RAID_VOLUME_RL_RAID5EE;
459234603Smav		*qual = G_RAID_VOLUME_RLQ_R5EERS;
460234603Smav	} else if (strcasecmp(str, "RAID5EE") == 0 ||
461234603Smav		   strcasecmp(str, "RAID5EE-LA") == 0) {
462234603Smav		*level = G_RAID_VOLUME_RL_RAID5EE;
463234603Smav		*qual = G_RAID_VOLUME_RLQ_R5EELA;
464234603Smav	} else if (strcasecmp(str, "RAID5EE-LS") == 0) {
465234603Smav		*level = G_RAID_VOLUME_RL_RAID5EE;
466234603Smav		*qual = G_RAID_VOLUME_RLQ_R5EELS;
467234603Smav	} else if (strcasecmp(str, "RAID5R-RA") == 0) {
468234603Smav		*level = G_RAID_VOLUME_RL_RAID5R;
469234603Smav		*qual = G_RAID_VOLUME_RLQ_R5RRA;
470234603Smav	} else if (strcasecmp(str, "RAID5R-RS") == 0) {
471234603Smav		*level = G_RAID_VOLUME_RL_RAID5R;
472234603Smav		*qual = G_RAID_VOLUME_RLQ_R5RRS;
473234603Smav	} else if (strcasecmp(str, "RAID5R") == 0 ||
474234603Smav		   strcasecmp(str, "RAID5R-LA") == 0) {
475234603Smav		*level = G_RAID_VOLUME_RL_RAID5R;
476234603Smav		*qual = G_RAID_VOLUME_RLQ_R5RLA;
477234603Smav	} else if (strcasecmp(str, "RAID5R-LS") == 0) {
478234603Smav		*level = G_RAID_VOLUME_RL_RAID5R;
479234603Smav		*qual = G_RAID_VOLUME_RLQ_R5RLS;
480234603Smav	} else
481219974Smav		return (-1);
482219974Smav	return (0);
483219974Smav}
484219974Smav
485219974Smavconst char *
486219974Smavg_raid_get_diskname(struct g_raid_disk *disk)
487219974Smav{
488219974Smav
489219974Smav	if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
490219974Smav		return ("[unknown]");
491219974Smav	return (disk->d_consumer->provider->name);
492219974Smav}
493219974Smav
494219974Smavvoid
495219974Smavg_raid_report_disk_state(struct g_raid_disk *disk)
496219974Smav{
497219974Smav	struct g_raid_subdisk *sd;
498219974Smav	int len, state;
499219974Smav	uint32_t s;
500219974Smav
501219974Smav	if (disk->d_consumer == NULL)
502219974Smav		return;
503219974Smav	if (disk->d_state == G_RAID_DISK_S_FAILED ||
504219974Smav	    disk->d_state == G_RAID_DISK_S_STALE_FAILED) {
505219974Smav		s = G_STATE_FAILED;
506219974Smav	} else {
507219974Smav		state = G_RAID_SUBDISK_S_ACTIVE;
508219974Smav		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
509219974Smav			if (sd->sd_state < state)
510219974Smav				state = sd->sd_state;
511219974Smav		}
512219974Smav		if (state == G_RAID_SUBDISK_S_FAILED)
513219974Smav			s = G_STATE_FAILED;
514219974Smav		else if (state == G_RAID_SUBDISK_S_NEW ||
515219974Smav		    state == G_RAID_SUBDISK_S_REBUILD)
516219974Smav			s = G_STATE_REBUILD;
517219974Smav		else if (state == G_RAID_SUBDISK_S_STALE ||
518219974Smav		    state == G_RAID_SUBDISK_S_RESYNC)
519219974Smav			s = G_STATE_RESYNC;
520219974Smav		else
521219974Smav			s = G_STATE_ACTIVE;
522219974Smav	}
523219974Smav	len = sizeof(s);
524219974Smav	g_io_getattr("GEOM::setstate", disk->d_consumer, &len, &s);
525219974Smav	G_RAID_DEBUG1(2, disk->d_softc, "Disk %s state reported as %d.",
526219974Smav	    g_raid_get_diskname(disk), s);
527219974Smav}
528219974Smav
529219974Smavvoid
530219974Smavg_raid_change_disk_state(struct g_raid_disk *disk, int state)
531219974Smav{
532219974Smav
533219974Smav	G_RAID_DEBUG1(0, disk->d_softc, "Disk %s state changed from %s to %s.",
534219974Smav	    g_raid_get_diskname(disk),
535219974Smav	    g_raid_disk_state2str(disk->d_state),
536219974Smav	    g_raid_disk_state2str(state));
537219974Smav	disk->d_state = state;
538219974Smav	g_raid_report_disk_state(disk);
539219974Smav}
540219974Smav
541219974Smavvoid
542219974Smavg_raid_change_subdisk_state(struct g_raid_subdisk *sd, int state)
543219974Smav{
544219974Smav
545219974Smav	G_RAID_DEBUG1(0, sd->sd_softc,
546219974Smav	    "Subdisk %s:%d-%s state changed from %s to %s.",
547219974Smav	    sd->sd_volume->v_name, sd->sd_pos,
548219974Smav	    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]",
549219974Smav	    g_raid_subdisk_state2str(sd->sd_state),
550219974Smav	    g_raid_subdisk_state2str(state));
551219974Smav	sd->sd_state = state;
552219974Smav	if (sd->sd_disk)
553219974Smav		g_raid_report_disk_state(sd->sd_disk);
554219974Smav}
555219974Smav
556219974Smavvoid
557219974Smavg_raid_change_volume_state(struct g_raid_volume *vol, int state)
558219974Smav{
559219974Smav
560219974Smav	G_RAID_DEBUG1(0, vol->v_softc,
561219974Smav	    "Volume %s state changed from %s to %s.",
562219974Smav	    vol->v_name,
563219974Smav	    g_raid_volume_state2str(vol->v_state),
564219974Smav	    g_raid_volume_state2str(state));
565219974Smav	vol->v_state = state;
566219974Smav}
567219974Smav
568219974Smav/*
569219974Smav * --- Events handling functions ---
570219974Smav * Events in geom_raid are used to maintain subdisks and volumes status
571219974Smav * from one thread to simplify locking.
572219974Smav */
573219974Smavstatic void
574219974Smavg_raid_event_free(struct g_raid_event *ep)
575219974Smav{
576219974Smav
577219974Smav	free(ep, M_RAID);
578219974Smav}
579219974Smav
580219974Smavint
581219974Smavg_raid_event_send(void *arg, int event, int flags)
582219974Smav{
583219974Smav	struct g_raid_softc *sc;
584219974Smav	struct g_raid_event *ep;
585219974Smav	int error;
586219974Smav
587219974Smav	if ((flags & G_RAID_EVENT_VOLUME) != 0) {
588219974Smav		sc = ((struct g_raid_volume *)arg)->v_softc;
589219974Smav	} else if ((flags & G_RAID_EVENT_DISK) != 0) {
590219974Smav		sc = ((struct g_raid_disk *)arg)->d_softc;
591219974Smav	} else if ((flags & G_RAID_EVENT_SUBDISK) != 0) {
592219974Smav		sc = ((struct g_raid_subdisk *)arg)->sd_softc;
593219974Smav	} else {
594219974Smav		sc = arg;
595219974Smav	}
596219974Smav	ep = malloc(sizeof(*ep), M_RAID,
597219974Smav	    sx_xlocked(&sc->sc_lock) ? M_WAITOK : M_NOWAIT);
598219974Smav	if (ep == NULL)
599219974Smav		return (ENOMEM);
600219974Smav	ep->e_tgt = arg;
601219974Smav	ep->e_event = event;
602219974Smav	ep->e_flags = flags;
603219974Smav	ep->e_error = 0;
604219974Smav	G_RAID_DEBUG1(4, sc, "Sending event %p. Waking up %p.", ep, sc);
605219974Smav	mtx_lock(&sc->sc_queue_mtx);
606219974Smav	TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
607219974Smav	mtx_unlock(&sc->sc_queue_mtx);
608219974Smav	wakeup(sc);
609219974Smav
610219974Smav	if ((flags & G_RAID_EVENT_WAIT) == 0)
611219974Smav		return (0);
612219974Smav
613219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
614219974Smav	G_RAID_DEBUG1(4, sc, "Sleeping on %p.", ep);
615219974Smav	sx_xunlock(&sc->sc_lock);
616219974Smav	while ((ep->e_flags & G_RAID_EVENT_DONE) == 0) {
617219974Smav		mtx_lock(&sc->sc_queue_mtx);
618219974Smav		MSLEEP(error, ep, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:event",
619219974Smav		    hz * 5);
620219974Smav	}
621219974Smav	error = ep->e_error;
622219974Smav	g_raid_event_free(ep);
623219974Smav	sx_xlock(&sc->sc_lock);
624219974Smav	return (error);
625219974Smav}
626219974Smav
627219974Smavstatic void
628219974Smavg_raid_event_cancel(struct g_raid_softc *sc, void *tgt)
629219974Smav{
630219974Smav	struct g_raid_event *ep, *tmpep;
631219974Smav
632219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
633219974Smav
634219974Smav	mtx_lock(&sc->sc_queue_mtx);
635219974Smav	TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
636219974Smav		if (ep->e_tgt != tgt)
637219974Smav			continue;
638219974Smav		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
639219974Smav		if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0)
640219974Smav			g_raid_event_free(ep);
641219974Smav		else {
642219974Smav			ep->e_error = ECANCELED;
643219974Smav			wakeup(ep);
644219974Smav		}
645219974Smav	}
646219974Smav	mtx_unlock(&sc->sc_queue_mtx);
647219974Smav}
648219974Smav
649219974Smavstatic int
650219974Smavg_raid_event_check(struct g_raid_softc *sc, void *tgt)
651219974Smav{
652219974Smav	struct g_raid_event *ep;
653219974Smav	int	res = 0;
654219974Smav
655219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
656219974Smav
657219974Smav	mtx_lock(&sc->sc_queue_mtx);
658219974Smav	TAILQ_FOREACH(ep, &sc->sc_events, e_next) {
659219974Smav		if (ep->e_tgt != tgt)
660219974Smav			continue;
661219974Smav		res = 1;
662219974Smav		break;
663219974Smav	}
664219974Smav	mtx_unlock(&sc->sc_queue_mtx);
665219974Smav	return (res);
666219974Smav}
667219974Smav
668219974Smav/*
669219974Smav * Return the number of disks in given state.
670219974Smav * If state is equal to -1, count all connected disks.
671219974Smav */
672219974Smavu_int
673219974Smavg_raid_ndisks(struct g_raid_softc *sc, int state)
674219974Smav{
675219974Smav	struct g_raid_disk *disk;
676219974Smav	u_int n;
677219974Smav
678219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
679219974Smav
680219974Smav	n = 0;
681219974Smav	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
682219974Smav		if (disk->d_state == state || state == -1)
683219974Smav			n++;
684219974Smav	}
685219974Smav	return (n);
686219974Smav}
687219974Smav
688219974Smav/*
689219974Smav * Return the number of subdisks in given state.
690219974Smav * If state is equal to -1, count all connected disks.
691219974Smav */
692219974Smavu_int
693219974Smavg_raid_nsubdisks(struct g_raid_volume *vol, int state)
694219974Smav{
695219974Smav	struct g_raid_subdisk *subdisk;
696219974Smav	struct g_raid_softc *sc;
697219974Smav	u_int i, n ;
698219974Smav
699219974Smav	sc = vol->v_softc;
700219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
701219974Smav
702219974Smav	n = 0;
703219974Smav	for (i = 0; i < vol->v_disks_count; i++) {
704219974Smav		subdisk = &vol->v_subdisks[i];
705219974Smav		if ((state == -1 &&
706219974Smav		     subdisk->sd_state != G_RAID_SUBDISK_S_NONE) ||
707219974Smav		    subdisk->sd_state == state)
708219974Smav			n++;
709219974Smav	}
710219974Smav	return (n);
711219974Smav}
712219974Smav
713219974Smav/*
714219974Smav * Return the first subdisk in given state.
715219974Smav * If state is equal to -1, then the first connected disks.
716219974Smav */
717219974Smavstruct g_raid_subdisk *
718219974Smavg_raid_get_subdisk(struct g_raid_volume *vol, int state)
719219974Smav{
720219974Smav	struct g_raid_subdisk *sd;
721219974Smav	struct g_raid_softc *sc;
722219974Smav	u_int i;
723219974Smav
724219974Smav	sc = vol->v_softc;
725219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
726219974Smav
727219974Smav	for (i = 0; i < vol->v_disks_count; i++) {
728219974Smav		sd = &vol->v_subdisks[i];
729219974Smav		if ((state == -1 &&
730219974Smav		     sd->sd_state != G_RAID_SUBDISK_S_NONE) ||
731219974Smav		    sd->sd_state == state)
732219974Smav			return (sd);
733219974Smav	}
734219974Smav	return (NULL);
735219974Smav}
736219974Smav
737219974Smavstruct g_consumer *
738219974Smavg_raid_open_consumer(struct g_raid_softc *sc, const char *name)
739219974Smav{
740219974Smav	struct g_consumer *cp;
741219974Smav	struct g_provider *pp;
742219974Smav
743219974Smav	g_topology_assert();
744219974Smav
745219974Smav	if (strncmp(name, "/dev/", 5) == 0)
746219974Smav		name += 5;
747219974Smav	pp = g_provider_by_name(name);
748219974Smav	if (pp == NULL)
749219974Smav		return (NULL);
750219974Smav	cp = g_new_consumer(sc->sc_geom);
751219974Smav	if (g_attach(cp, pp) != 0) {
752219974Smav		g_destroy_consumer(cp);
753219974Smav		return (NULL);
754219974Smav	}
755219974Smav	if (g_access(cp, 1, 1, 1) != 0) {
756219974Smav		g_detach(cp);
757219974Smav		g_destroy_consumer(cp);
758219974Smav		return (NULL);
759219974Smav	}
760219974Smav	return (cp);
761219974Smav}
762219974Smav
763219974Smavstatic u_int
764219974Smavg_raid_nrequests(struct g_raid_softc *sc, struct g_consumer *cp)
765219974Smav{
766219974Smav	struct bio *bp;
767219974Smav	u_int nreqs = 0;
768219974Smav
769219974Smav	mtx_lock(&sc->sc_queue_mtx);
770219974Smav	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
771219974Smav		if (bp->bio_from == cp)
772219974Smav			nreqs++;
773219974Smav	}
774219974Smav	mtx_unlock(&sc->sc_queue_mtx);
775219974Smav	return (nreqs);
776219974Smav}
777219974Smav
778219974Smavu_int
779219974Smavg_raid_nopens(struct g_raid_softc *sc)
780219974Smav{
781219974Smav	struct g_raid_volume *vol;
782219974Smav	u_int opens;
783219974Smav
784219974Smav	opens = 0;
785219974Smav	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
786219974Smav		if (vol->v_provider_open != 0)
787219974Smav			opens++;
788219974Smav	}
789219974Smav	return (opens);
790219974Smav}
791219974Smav
792219974Smavstatic int
793219974Smavg_raid_consumer_is_busy(struct g_raid_softc *sc, struct g_consumer *cp)
794219974Smav{
795219974Smav
796219974Smav	if (cp->index > 0) {
797219974Smav		G_RAID_DEBUG1(2, sc,
798219974Smav		    "I/O requests for %s exist, can't destroy it now.",
799219974Smav		    cp->provider->name);
800219974Smav		return (1);
801219974Smav	}
802219974Smav	if (g_raid_nrequests(sc, cp) > 0) {
803219974Smav		G_RAID_DEBUG1(2, sc,
804219974Smav		    "I/O requests for %s in queue, can't destroy it now.",
805219974Smav		    cp->provider->name);
806219974Smav		return (1);
807219974Smav	}
808219974Smav	return (0);
809219974Smav}
810219974Smav
811219974Smavstatic void
812219974Smavg_raid_destroy_consumer(void *arg, int flags __unused)
813219974Smav{
814219974Smav	struct g_consumer *cp;
815219974Smav
816219974Smav	g_topology_assert();
817219974Smav
818219974Smav	cp = arg;
819219974Smav	G_RAID_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
820219974Smav	g_detach(cp);
821219974Smav	g_destroy_consumer(cp);
822219974Smav}
823219974Smav
824219974Smavvoid
825219974Smavg_raid_kill_consumer(struct g_raid_softc *sc, struct g_consumer *cp)
826219974Smav{
827219974Smav	struct g_provider *pp;
828219974Smav	int retaste_wait;
829219974Smav
830219974Smav	g_topology_assert_not();
831219974Smav
832219974Smav	g_topology_lock();
833219974Smav	cp->private = NULL;
834219974Smav	if (g_raid_consumer_is_busy(sc, cp))
835219974Smav		goto out;
836219974Smav	pp = cp->provider;
837219974Smav	retaste_wait = 0;
838219974Smav	if (cp->acw == 1) {
839219974Smav		if ((pp->geom->flags & G_GEOM_WITHER) == 0)
840219974Smav			retaste_wait = 1;
841219974Smav	}
842219974Smav	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
843219974Smav		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
844219974Smav	if (retaste_wait) {
845219974Smav		/*
846219974Smav		 * After retaste event was send (inside g_access()), we can send
847219974Smav		 * event to detach and destroy consumer.
848219974Smav		 * A class, which has consumer to the given provider connected
849219974Smav		 * will not receive retaste event for the provider.
850219974Smav		 * This is the way how I ignore retaste events when I close
851219974Smav		 * consumers opened for write: I detach and destroy consumer
852219974Smav		 * after retaste event is sent.
853219974Smav		 */
854219974Smav		g_post_event(g_raid_destroy_consumer, cp, M_WAITOK, NULL);
855219974Smav		goto out;
856219974Smav	}
857219974Smav	G_RAID_DEBUG(1, "Consumer %s destroyed.", pp->name);
858219974Smav	g_detach(cp);
859219974Smav	g_destroy_consumer(cp);
860219974Smavout:
861219974Smav	g_topology_unlock();
862219974Smav}
863219974Smav
864219974Smavstatic void
865219974Smavg_raid_orphan(struct g_consumer *cp)
866219974Smav{
867219974Smav	struct g_raid_disk *disk;
868219974Smav
869219974Smav	g_topology_assert();
870219974Smav
871219974Smav	disk = cp->private;
872219974Smav	if (disk == NULL)
873219974Smav		return;
874219974Smav	g_raid_event_send(disk, G_RAID_DISK_E_DISCONNECTED,
875219974Smav	    G_RAID_EVENT_DISK);
876219974Smav}
877219974Smav
878219974Smavstatic int
879219974Smavg_raid_clean(struct g_raid_volume *vol, int acw)
880219974Smav{
881219974Smav	struct g_raid_softc *sc;
882219974Smav	int timeout;
883219974Smav
884219974Smav	sc = vol->v_softc;
885219974Smav	g_topology_assert_not();
886219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
887219974Smav
888219974Smav//	if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
889219974Smav//		return (0);
890219974Smav	if (!vol->v_dirty)
891219974Smav		return (0);
892219974Smav	if (vol->v_writes > 0)
893219974Smav		return (0);
894219974Smav	if (acw > 0 || (acw == -1 &&
895219974Smav	    vol->v_provider != NULL && vol->v_provider->acw > 0)) {
896219974Smav		timeout = g_raid_clean_time - (time_uptime - vol->v_last_write);
897219974Smav		if (timeout > 0)
898219974Smav			return (timeout);
899219974Smav	}
900219974Smav	vol->v_dirty = 0;
901219974Smav	G_RAID_DEBUG1(1, sc, "Volume %s marked as clean.",
902219974Smav	    vol->v_name);
903219974Smav	g_raid_write_metadata(sc, vol, NULL, NULL);
904219974Smav	return (0);
905219974Smav}
906219974Smav
907219974Smavstatic void
908219974Smavg_raid_dirty(struct g_raid_volume *vol)
909219974Smav{
910219974Smav	struct g_raid_softc *sc;
911219974Smav
912219974Smav	sc = vol->v_softc;
913219974Smav	g_topology_assert_not();
914219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
915219974Smav
916219974Smav//	if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
917219974Smav//		return;
918219974Smav	vol->v_dirty = 1;
919219974Smav	G_RAID_DEBUG1(1, sc, "Volume %s marked as dirty.",
920219974Smav	    vol->v_name);
921219974Smav	g_raid_write_metadata(sc, vol, NULL, NULL);
922219974Smav}
923219974Smav
924219974Smavvoid
925219974Smavg_raid_tr_flush_common(struct g_raid_tr_object *tr, struct bio *bp)
926219974Smav{
927219974Smav	struct g_raid_softc *sc;
928219974Smav	struct g_raid_volume *vol;
929219974Smav	struct g_raid_subdisk *sd;
930219974Smav	struct bio_queue_head queue;
931219974Smav	struct bio *cbp;
932219974Smav	int i;
933219974Smav
934219974Smav	vol = tr->tro_volume;
935219974Smav	sc = vol->v_softc;
936219974Smav
937219974Smav	/*
938219974Smav	 * Allocate all bios before sending any request, so we can return
939219974Smav	 * ENOMEM in nice and clean way.
940219974Smav	 */
941219974Smav	bioq_init(&queue);
942219974Smav	for (i = 0; i < vol->v_disks_count; i++) {
943219974Smav		sd = &vol->v_subdisks[i];
944219974Smav		if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
945219974Smav		    sd->sd_state == G_RAID_SUBDISK_S_FAILED)
946219974Smav			continue;
947219974Smav		cbp = g_clone_bio(bp);
948219974Smav		if (cbp == NULL)
949219974Smav			goto failure;
950219974Smav		cbp->bio_caller1 = sd;
951219974Smav		bioq_insert_tail(&queue, cbp);
952219974Smav	}
953219974Smav	for (cbp = bioq_first(&queue); cbp != NULL;
954219974Smav	    cbp = bioq_first(&queue)) {
955219974Smav		bioq_remove(&queue, cbp);
956219974Smav		sd = cbp->bio_caller1;
957219974Smav		cbp->bio_caller1 = NULL;
958219974Smav		g_raid_subdisk_iostart(sd, cbp);
959219974Smav	}
960219974Smav	return;
961219974Smavfailure:
962219974Smav	for (cbp = bioq_first(&queue); cbp != NULL;
963219974Smav	    cbp = bioq_first(&queue)) {
964219974Smav		bioq_remove(&queue, cbp);
965219974Smav		g_destroy_bio(cbp);
966219974Smav	}
967219974Smav	if (bp->bio_error == 0)
968219974Smav		bp->bio_error = ENOMEM;
969219974Smav	g_raid_iodone(bp, bp->bio_error);
970219974Smav}
971219974Smav
972219974Smavstatic void
973219974Smavg_raid_tr_kerneldump_common_done(struct bio *bp)
974219974Smav{
975219974Smav
976219974Smav	bp->bio_flags |= BIO_DONE;
977219974Smav}
978219974Smav
979219974Smavint
980219974Smavg_raid_tr_kerneldump_common(struct g_raid_tr_object *tr,
981219974Smav    void *virtual, vm_offset_t physical, off_t offset, size_t length)
982219974Smav{
983219974Smav	struct g_raid_softc *sc;
984219974Smav	struct g_raid_volume *vol;
985219974Smav	struct bio bp;
986219974Smav
987219974Smav	vol = tr->tro_volume;
988219974Smav	sc = vol->v_softc;
989219974Smav
990219974Smav	bzero(&bp, sizeof(bp));
991219974Smav	bp.bio_cmd = BIO_WRITE;
992219974Smav	bp.bio_done = g_raid_tr_kerneldump_common_done;
993219974Smav	bp.bio_attribute = NULL;
994219974Smav	bp.bio_offset = offset;
995219974Smav	bp.bio_length = length;
996219974Smav	bp.bio_data = virtual;
997219974Smav	bp.bio_to = vol->v_provider;
998219974Smav
999219974Smav	g_raid_start(&bp);
1000219974Smav	while (!(bp.bio_flags & BIO_DONE)) {
1001219974Smav		G_RAID_DEBUG1(4, sc, "Poll...");
1002219974Smav		g_raid_poll(sc);
1003219974Smav		DELAY(10);
1004219974Smav	}
1005219974Smav
1006219974Smav	return (bp.bio_error != 0 ? EIO : 0);
1007219974Smav}
1008219974Smav
1009219974Smavstatic int
1010219974Smavg_raid_dump(void *arg,
1011219974Smav    void *virtual, vm_offset_t physical, off_t offset, size_t length)
1012219974Smav{
1013219974Smav	struct g_raid_volume *vol;
1014219974Smav	int error;
1015219974Smav
1016219974Smav	vol = (struct g_raid_volume *)arg;
1017219974Smav	G_RAID_DEBUG1(3, vol->v_softc, "Dumping at off %llu len %llu.",
1018219974Smav	    (long long unsigned)offset, (long long unsigned)length);
1019219974Smav
1020219974Smav	error = G_RAID_TR_KERNELDUMP(vol->v_tr,
1021219974Smav	    virtual, physical, offset, length);
1022219974Smav	return (error);
1023219974Smav}
1024219974Smav
1025219974Smavstatic void
1026219974Smavg_raid_kerneldump(struct g_raid_softc *sc, struct bio *bp)
1027219974Smav{
1028219974Smav	struct g_kerneldump *gkd;
1029219974Smav	struct g_provider *pp;
1030219974Smav	struct g_raid_volume *vol;
1031219974Smav
1032219974Smav	gkd = (struct g_kerneldump*)bp->bio_data;
1033219974Smav	pp = bp->bio_to;
1034219974Smav	vol = pp->private;
1035219974Smav	g_trace(G_T_TOPOLOGY, "g_raid_kerneldump(%s, %jd, %jd)",
1036219974Smav		pp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
1037219974Smav	gkd->di.dumper = g_raid_dump;
1038219974Smav	gkd->di.priv = vol;
1039219974Smav	gkd->di.blocksize = vol->v_sectorsize;
1040219974Smav	gkd->di.maxiosize = DFLTPHYS;
1041219974Smav	gkd->di.mediaoffset = gkd->offset;
1042219974Smav	if ((gkd->offset + gkd->length) > vol->v_mediasize)
1043219974Smav		gkd->length = vol->v_mediasize - gkd->offset;
1044219974Smav	gkd->di.mediasize = gkd->length;
1045219974Smav	g_io_deliver(bp, 0);
1046219974Smav}
1047219974Smav
1048219974Smavstatic void
1049219974Smavg_raid_start(struct bio *bp)
1050219974Smav{
1051219974Smav	struct g_raid_softc *sc;
1052219974Smav
1053219974Smav	sc = bp->bio_to->geom->softc;
1054219974Smav	/*
1055219974Smav	 * If sc == NULL or there are no valid disks, provider's error
1056219974Smav	 * should be set and g_raid_start() should not be called at all.
1057219974Smav	 */
1058219974Smav//	KASSERT(sc != NULL && sc->sc_state == G_RAID_VOLUME_S_RUNNING,
1059219974Smav//	    ("Provider's error should be set (error=%d)(mirror=%s).",
1060219974Smav//	    bp->bio_to->error, bp->bio_to->name));
1061219974Smav	G_RAID_LOGREQ(3, bp, "Request received.");
1062219974Smav
1063219974Smav	switch (bp->bio_cmd) {
1064219974Smav	case BIO_READ:
1065219974Smav	case BIO_WRITE:
1066219974Smav	case BIO_DELETE:
1067219974Smav	case BIO_FLUSH:
1068219974Smav		break;
1069219974Smav	case BIO_GETATTR:
1070219974Smav		if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
1071219974Smav			g_raid_kerneldump(sc, bp);
1072219974Smav		else
1073219974Smav			g_io_deliver(bp, EOPNOTSUPP);
1074219974Smav		return;
1075219974Smav	default:
1076219974Smav		g_io_deliver(bp, EOPNOTSUPP);
1077219974Smav		return;
1078219974Smav	}
1079219974Smav	mtx_lock(&sc->sc_queue_mtx);
1080219974Smav	bioq_disksort(&sc->sc_queue, bp);
1081219974Smav	mtx_unlock(&sc->sc_queue_mtx);
1082219974Smav	if (!dumping) {
1083219974Smav		G_RAID_DEBUG1(4, sc, "Waking up %p.", sc);
1084219974Smav		wakeup(sc);
1085219974Smav	}
1086219974Smav}
1087219974Smav
1088219974Smavstatic int
1089219974Smavg_raid_bio_overlaps(const struct bio *bp, off_t lstart, off_t len)
1090219974Smav{
1091219974Smav	/*
1092219974Smav	 * 5 cases:
1093219974Smav	 * (1) bp entirely below NO
1094219974Smav	 * (2) bp entirely above NO
1095219974Smav	 * (3) bp start below, but end in range YES
1096219974Smav	 * (4) bp entirely within YES
1097219974Smav	 * (5) bp starts within, ends above YES
1098219974Smav	 *
1099219974Smav	 * lock range 10-19 (offset 10 length 10)
1100219974Smav	 * (1) 1-5: first if kicks it out
1101219974Smav	 * (2) 30-35: second if kicks it out
1102219974Smav	 * (3) 5-15: passes both ifs
1103219974Smav	 * (4) 12-14: passes both ifs
1104219974Smav	 * (5) 19-20: passes both
1105219974Smav	 */
1106219974Smav	off_t lend = lstart + len - 1;
1107219974Smav	off_t bstart = bp->bio_offset;
1108219974Smav	off_t bend = bp->bio_offset + bp->bio_length - 1;
1109219974Smav
1110219974Smav	if (bend < lstart)
1111219974Smav		return (0);
1112219974Smav	if (lend < bstart)
1113219974Smav		return (0);
1114219974Smav	return (1);
1115219974Smav}
1116219974Smav
1117219974Smavstatic int
1118219974Smavg_raid_is_in_locked_range(struct g_raid_volume *vol, const struct bio *bp)
1119219974Smav{
1120219974Smav	struct g_raid_lock *lp;
1121219974Smav
1122219974Smav	sx_assert(&vol->v_softc->sc_lock, SX_LOCKED);
1123219974Smav
1124219974Smav	LIST_FOREACH(lp, &vol->v_locks, l_next) {
1125219974Smav		if (g_raid_bio_overlaps(bp, lp->l_offset, lp->l_length))
1126219974Smav			return (1);
1127219974Smav	}
1128219974Smav	return (0);
1129219974Smav}
1130219974Smav
1131219974Smavstatic void
1132219974Smavg_raid_start_request(struct bio *bp)
1133219974Smav{
1134219974Smav	struct g_raid_softc *sc;
1135219974Smav	struct g_raid_volume *vol;
1136219974Smav
1137219974Smav	sc = bp->bio_to->geom->softc;
1138219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
1139219974Smav	vol = bp->bio_to->private;
1140219974Smav
1141219974Smav	/*
1142219974Smav	 * Check to see if this item is in a locked range.  If so,
1143219974Smav	 * queue it to our locked queue and return.  We'll requeue
1144219974Smav	 * it when the range is unlocked.  Internal I/O for the
1145219974Smav	 * rebuild/rescan/recovery process is excluded from this
1146219974Smav	 * check so we can actually do the recovery.
1147219974Smav	 */
1148219974Smav	if (!(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL) &&
1149219974Smav	    g_raid_is_in_locked_range(vol, bp)) {
1150219974Smav		G_RAID_LOGREQ(3, bp, "Defer request.");
1151219974Smav		bioq_insert_tail(&vol->v_locked, bp);
1152219974Smav		return;
1153219974Smav	}
1154219974Smav
1155219974Smav	/*
1156219974Smav	 * If we're actually going to do the write/delete, then
1157219974Smav	 * update the idle stats for the volume.
1158219974Smav	 */
1159219974Smav	if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1160219974Smav		if (!vol->v_dirty)
1161219974Smav			g_raid_dirty(vol);
1162219974Smav		vol->v_writes++;
1163219974Smav	}
1164219974Smav
1165219974Smav	/*
1166219974Smav	 * Put request onto inflight queue, so we can check if new
1167219974Smav	 * synchronization requests don't collide with it.  Then tell
1168219974Smav	 * the transformation layer to start the I/O.
1169219974Smav	 */
1170219974Smav	bioq_insert_tail(&vol->v_inflight, bp);
1171219974Smav	G_RAID_LOGREQ(4, bp, "Request started");
1172219974Smav	G_RAID_TR_IOSTART(vol->v_tr, bp);
1173219974Smav}
1174219974Smav
1175219974Smavstatic void
1176219974Smavg_raid_finish_with_locked_ranges(struct g_raid_volume *vol, struct bio *bp)
1177219974Smav{
1178219974Smav	off_t off, len;
1179219974Smav	struct bio *nbp;
1180219974Smav	struct g_raid_lock *lp;
1181219974Smav
1182219974Smav	vol->v_pending_lock = 0;
1183219974Smav	LIST_FOREACH(lp, &vol->v_locks, l_next) {
1184219974Smav		if (lp->l_pending) {
1185219974Smav			off = lp->l_offset;
1186219974Smav			len = lp->l_length;
1187219974Smav			lp->l_pending = 0;
1188219974Smav			TAILQ_FOREACH(nbp, &vol->v_inflight.queue, bio_queue) {
1189219974Smav				if (g_raid_bio_overlaps(nbp, off, len))
1190219974Smav					lp->l_pending++;
1191219974Smav			}
1192219974Smav			if (lp->l_pending) {
1193219974Smav				vol->v_pending_lock = 1;
1194219974Smav				G_RAID_DEBUG1(4, vol->v_softc,
1195219974Smav				    "Deferred lock(%jd, %jd) has %d pending",
1196219974Smav				    (intmax_t)off, (intmax_t)(off + len),
1197219974Smav				    lp->l_pending);
1198219974Smav				continue;
1199219974Smav			}
1200219974Smav			G_RAID_DEBUG1(4, vol->v_softc,
1201219974Smav			    "Deferred lock of %jd to %jd completed",
1202219974Smav			    (intmax_t)off, (intmax_t)(off + len));
1203219974Smav			G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1204219974Smav		}
1205219974Smav	}
1206219974Smav}
1207219974Smav
1208219974Smavvoid
1209219974Smavg_raid_iodone(struct bio *bp, int error)
1210219974Smav{
1211219974Smav	struct g_raid_softc *sc;
1212219974Smav	struct g_raid_volume *vol;
1213219974Smav
1214219974Smav	sc = bp->bio_to->geom->softc;
1215219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
1216219974Smav	vol = bp->bio_to->private;
1217219974Smav	G_RAID_LOGREQ(3, bp, "Request done: %d.", error);
1218219974Smav
1219219974Smav	/* Update stats if we done write/delete. */
1220219974Smav	if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1221219974Smav		vol->v_writes--;
1222219974Smav		vol->v_last_write = time_uptime;
1223219974Smav	}
1224219974Smav
1225219974Smav	bioq_remove(&vol->v_inflight, bp);
1226219974Smav	if (vol->v_pending_lock && g_raid_is_in_locked_range(vol, bp))
1227219974Smav		g_raid_finish_with_locked_ranges(vol, bp);
1228219974Smav	getmicrouptime(&vol->v_last_done);
1229219974Smav	g_io_deliver(bp, error);
1230219974Smav}
1231219974Smav
1232219974Smavint
1233219974Smavg_raid_lock_range(struct g_raid_volume *vol, off_t off, off_t len,
1234219974Smav    struct bio *ignore, void *argp)
1235219974Smav{
1236219974Smav	struct g_raid_softc *sc;
1237219974Smav	struct g_raid_lock *lp;
1238219974Smav	struct bio *bp;
1239219974Smav
1240219974Smav	sc = vol->v_softc;
1241219974Smav	lp = malloc(sizeof(*lp), M_RAID, M_WAITOK | M_ZERO);
1242219974Smav	LIST_INSERT_HEAD(&vol->v_locks, lp, l_next);
1243219974Smav	lp->l_offset = off;
1244219974Smav	lp->l_length = len;
1245219974Smav	lp->l_callback_arg = argp;
1246219974Smav
1247219974Smav	lp->l_pending = 0;
1248219974Smav	TAILQ_FOREACH(bp, &vol->v_inflight.queue, bio_queue) {
1249219974Smav		if (bp != ignore && g_raid_bio_overlaps(bp, off, len))
1250219974Smav			lp->l_pending++;
1251219974Smav	}
1252219974Smav
1253219974Smav	/*
1254219974Smav	 * If there are any writes that are pending, we return EBUSY.  All
1255219974Smav	 * callers will have to wait until all pending writes clear.
1256219974Smav	 */
1257219974Smav	if (lp->l_pending > 0) {
1258219974Smav		vol->v_pending_lock = 1;
1259219974Smav		G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd deferred %d pend",
1260219974Smav		    (intmax_t)off, (intmax_t)(off+len), lp->l_pending);
1261219974Smav		return (EBUSY);
1262219974Smav	}
1263219974Smav	G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd",
1264219974Smav	    (intmax_t)off, (intmax_t)(off+len));
1265219974Smav	G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1266219974Smav	return (0);
1267219974Smav}
1268219974Smav
1269219974Smavint
1270219974Smavg_raid_unlock_range(struct g_raid_volume *vol, off_t off, off_t len)
1271219974Smav{
1272219974Smav	struct g_raid_lock *lp;
1273219974Smav	struct g_raid_softc *sc;
1274219974Smav	struct bio *bp;
1275219974Smav
1276219974Smav	sc = vol->v_softc;
1277219974Smav	LIST_FOREACH(lp, &vol->v_locks, l_next) {
1278219974Smav		if (lp->l_offset == off && lp->l_length == len) {
1279219974Smav			LIST_REMOVE(lp, l_next);
1280219974Smav			/* XXX
1281219974Smav			 * Right now we just put them all back on the queue
1282219974Smav			 * and hope for the best.  We hope this because any
1283219974Smav			 * locked ranges will go right back on this list
1284219974Smav			 * when the worker thread runs.
1285219974Smav			 * XXX
1286219974Smav			 */
1287219974Smav			G_RAID_DEBUG1(4, sc, "Unlocked %jd to %jd",
1288219974Smav			    (intmax_t)lp->l_offset,
1289219974Smav			    (intmax_t)(lp->l_offset+lp->l_length));
1290219974Smav			mtx_lock(&sc->sc_queue_mtx);
1291219974Smav			while ((bp = bioq_takefirst(&vol->v_locked)) != NULL)
1292219974Smav				bioq_disksort(&sc->sc_queue, bp);
1293219974Smav			mtx_unlock(&sc->sc_queue_mtx);
1294219974Smav			free(lp, M_RAID);
1295219974Smav			return (0);
1296219974Smav		}
1297219974Smav	}
1298219974Smav	return (EINVAL);
1299219974Smav}
1300219974Smav
1301219974Smavvoid
1302219974Smavg_raid_subdisk_iostart(struct g_raid_subdisk *sd, struct bio *bp)
1303219974Smav{
1304219974Smav	struct g_consumer *cp;
1305219974Smav	struct g_raid_disk *disk, *tdisk;
1306219974Smav
1307219974Smav	bp->bio_caller1 = sd;
1308219974Smav
1309219974Smav	/*
1310219974Smav	 * Make sure that the disk is present. Generally it is a task of
1311219974Smav	 * transformation layers to not send requests to absent disks, but
1312219974Smav	 * it is better to be safe and report situation then sorry.
1313219974Smav	 */
1314219974Smav	if (sd->sd_disk == NULL) {
1315219974Smav		G_RAID_LOGREQ(0, bp, "Warning! I/O request to an absent disk!");
1316219974Smavnodisk:
1317219974Smav		bp->bio_from = NULL;
1318219974Smav		bp->bio_to = NULL;
1319219974Smav		bp->bio_error = ENXIO;
1320219974Smav		g_raid_disk_done(bp);
1321219974Smav		return;
1322219974Smav	}
1323219974Smav	disk = sd->sd_disk;
1324219974Smav	if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
1325219974Smav	    disk->d_state != G_RAID_DISK_S_FAILED) {
1326219974Smav		G_RAID_LOGREQ(0, bp, "Warning! I/O request to a disk in a "
1327219974Smav		    "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
1328219974Smav		goto nodisk;
1329219974Smav	}
1330219974Smav
1331219974Smav	cp = disk->d_consumer;
1332219974Smav	bp->bio_from = cp;
1333219974Smav	bp->bio_to = cp->provider;
1334219974Smav	cp->index++;
1335219974Smav
1336219974Smav	/* Update average disks load. */
1337219974Smav	TAILQ_FOREACH(tdisk, &sd->sd_softc->sc_disks, d_next) {
1338219974Smav		if (tdisk->d_consumer == NULL)
1339219974Smav			tdisk->d_load = 0;
1340219974Smav		else
1341219974Smav			tdisk->d_load = (tdisk->d_consumer->index *
1342219974Smav			    G_RAID_SUBDISK_LOAD_SCALE + tdisk->d_load * 7) / 8;
1343219974Smav	}
1344219974Smav
1345219974Smav	disk->d_last_offset = bp->bio_offset + bp->bio_length;
1346219974Smav	if (dumping) {
1347219974Smav		G_RAID_LOGREQ(3, bp, "Sending dumping request.");
1348219974Smav		if (bp->bio_cmd == BIO_WRITE) {
1349219974Smav			bp->bio_error = g_raid_subdisk_kerneldump(sd,
1350219974Smav			    bp->bio_data, 0, bp->bio_offset, bp->bio_length);
1351219974Smav		} else
1352219974Smav			bp->bio_error = EOPNOTSUPP;
1353219974Smav		g_raid_disk_done(bp);
1354219974Smav	} else {
1355219974Smav		bp->bio_done = g_raid_disk_done;
1356219974Smav		bp->bio_offset += sd->sd_offset;
1357219974Smav		G_RAID_LOGREQ(3, bp, "Sending request.");
1358219974Smav		g_io_request(bp, cp);
1359219974Smav	}
1360219974Smav}
1361219974Smav
1362219974Smavint
1363219974Smavg_raid_subdisk_kerneldump(struct g_raid_subdisk *sd,
1364219974Smav    void *virtual, vm_offset_t physical, off_t offset, size_t length)
1365219974Smav{
1366219974Smav
1367219974Smav	if (sd->sd_disk == NULL)
1368219974Smav		return (ENXIO);
1369219974Smav	if (sd->sd_disk->d_kd.di.dumper == NULL)
1370219974Smav		return (EOPNOTSUPP);
1371219974Smav	return (dump_write(&sd->sd_disk->d_kd.di,
1372219974Smav	    virtual, physical,
1373219974Smav	    sd->sd_disk->d_kd.di.mediaoffset + sd->sd_offset + offset,
1374219974Smav	    length));
1375219974Smav}
1376219974Smav
1377219974Smavstatic void
1378219974Smavg_raid_disk_done(struct bio *bp)
1379219974Smav{
1380219974Smav	struct g_raid_softc *sc;
1381219974Smav	struct g_raid_subdisk *sd;
1382219974Smav
1383219974Smav	sd = bp->bio_caller1;
1384219974Smav	sc = sd->sd_softc;
1385219974Smav	mtx_lock(&sc->sc_queue_mtx);
1386219974Smav	bioq_disksort(&sc->sc_queue, bp);
1387219974Smav	mtx_unlock(&sc->sc_queue_mtx);
1388219974Smav	if (!dumping)
1389219974Smav		wakeup(sc);
1390219974Smav}
1391219974Smav
1392219974Smavstatic void
1393219974Smavg_raid_disk_done_request(struct bio *bp)
1394219974Smav{
1395219974Smav	struct g_raid_softc *sc;
1396219974Smav	struct g_raid_disk *disk;
1397219974Smav	struct g_raid_subdisk *sd;
1398219974Smav	struct g_raid_volume *vol;
1399219974Smav
1400219974Smav	g_topology_assert_not();
1401219974Smav
1402219974Smav	G_RAID_LOGREQ(3, bp, "Disk request done: %d.", bp->bio_error);
1403219974Smav	sd = bp->bio_caller1;
1404219974Smav	sc = sd->sd_softc;
1405219974Smav	vol = sd->sd_volume;
1406219974Smav	if (bp->bio_from != NULL) {
1407219974Smav		bp->bio_from->index--;
1408219974Smav		disk = bp->bio_from->private;
1409219974Smav		if (disk == NULL)
1410219974Smav			g_raid_kill_consumer(sc, bp->bio_from);
1411219974Smav	}
1412219974Smav	bp->bio_offset -= sd->sd_offset;
1413219974Smav
1414219974Smav	G_RAID_TR_IODONE(vol->v_tr, sd, bp);
1415219974Smav}
1416219974Smav
1417219974Smavstatic void
1418219974Smavg_raid_handle_event(struct g_raid_softc *sc, struct g_raid_event *ep)
1419219974Smav{
1420219974Smav
1421219974Smav	if ((ep->e_flags & G_RAID_EVENT_VOLUME) != 0)
1422219974Smav		ep->e_error = g_raid_update_volume(ep->e_tgt, ep->e_event);
1423219974Smav	else if ((ep->e_flags & G_RAID_EVENT_DISK) != 0)
1424219974Smav		ep->e_error = g_raid_update_disk(ep->e_tgt, ep->e_event);
1425219974Smav	else if ((ep->e_flags & G_RAID_EVENT_SUBDISK) != 0)
1426219974Smav		ep->e_error = g_raid_update_subdisk(ep->e_tgt, ep->e_event);
1427219974Smav	else
1428219974Smav		ep->e_error = g_raid_update_node(ep->e_tgt, ep->e_event);
1429219974Smav	if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0) {
1430219974Smav		KASSERT(ep->e_error == 0,
1431219974Smav		    ("Error cannot be handled."));
1432219974Smav		g_raid_event_free(ep);
1433219974Smav	} else {
1434219974Smav		ep->e_flags |= G_RAID_EVENT_DONE;
1435219974Smav		G_RAID_DEBUG1(4, sc, "Waking up %p.", ep);
1436219974Smav		mtx_lock(&sc->sc_queue_mtx);
1437219974Smav		wakeup(ep);
1438219974Smav		mtx_unlock(&sc->sc_queue_mtx);
1439219974Smav	}
1440219974Smav}
1441219974Smav
1442219974Smav/*
1443219974Smav * Worker thread.
1444219974Smav */
1445219974Smavstatic void
1446219974Smavg_raid_worker(void *arg)
1447219974Smav{
1448219974Smav	struct g_raid_softc *sc;
1449219974Smav	struct g_raid_event *ep;
1450219974Smav	struct g_raid_volume *vol;
1451219974Smav	struct bio *bp;
1452219974Smav	struct timeval now, t;
1453219974Smav	int timeout, rv;
1454219974Smav
1455219974Smav	sc = arg;
1456219974Smav	thread_lock(curthread);
1457219974Smav	sched_prio(curthread, PRIBIO);
1458219974Smav	thread_unlock(curthread);
1459219974Smav
1460219974Smav	sx_xlock(&sc->sc_lock);
1461219974Smav	for (;;) {
1462219974Smav		mtx_lock(&sc->sc_queue_mtx);
1463219974Smav		/*
1464219974Smav		 * First take a look at events.
1465219974Smav		 * This is important to handle events before any I/O requests.
1466219974Smav		 */
1467219974Smav		bp = NULL;
1468219974Smav		vol = NULL;
1469219974Smav		rv = 0;
1470219974Smav		ep = TAILQ_FIRST(&sc->sc_events);
1471219974Smav		if (ep != NULL)
1472219974Smav			TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1473219974Smav		else if ((bp = bioq_takefirst(&sc->sc_queue)) != NULL)
1474219974Smav			;
1475219974Smav		else {
1476219974Smav			getmicrouptime(&now);
1477219974Smav			t = now;
1478219974Smav			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1479219974Smav				if (bioq_first(&vol->v_inflight) == NULL &&
1480219974Smav				    vol->v_tr &&
1481219974Smav				    timevalcmp(&vol->v_last_done, &t, < ))
1482219974Smav					t = vol->v_last_done;
1483219974Smav			}
1484219974Smav			timevalsub(&t, &now);
1485219974Smav			timeout = g_raid_idle_threshold +
1486219974Smav			    t.tv_sec * 1000000 + t.tv_usec;
1487219974Smav			if (timeout > 0) {
1488219974Smav				/*
1489219974Smav				 * Two steps to avoid overflows at HZ=1000
1490219974Smav				 * and idle timeouts > 2.1s.  Some rounding
1491219974Smav				 * errors can occur, but they are < 1tick,
1492219974Smav				 * which is deemed to be close enough for
1493219974Smav				 * this purpose.
1494219974Smav				 */
1495219974Smav				int micpertic = 1000000 / hz;
1496219974Smav				timeout = (timeout + micpertic - 1) / micpertic;
1497219974Smav				sx_xunlock(&sc->sc_lock);
1498219974Smav				MSLEEP(rv, sc, &sc->sc_queue_mtx,
1499219974Smav				    PRIBIO | PDROP, "-", timeout);
1500219974Smav				sx_xlock(&sc->sc_lock);
1501219974Smav				goto process;
1502219974Smav			} else
1503219974Smav				rv = EWOULDBLOCK;
1504219974Smav		}
1505219974Smav		mtx_unlock(&sc->sc_queue_mtx);
1506219974Smavprocess:
1507219974Smav		if (ep != NULL) {
1508219974Smav			g_raid_handle_event(sc, ep);
1509219974Smav		} else if (bp != NULL) {
1510219974Smav			if (bp->bio_to != NULL &&
1511219974Smav			    bp->bio_to->geom == sc->sc_geom)
1512219974Smav				g_raid_start_request(bp);
1513219974Smav			else
1514219974Smav				g_raid_disk_done_request(bp);
1515219974Smav		} else if (rv == EWOULDBLOCK) {
1516219974Smav			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1517219974Smav				if (vol->v_writes == 0 && vol->v_dirty)
1518219974Smav					g_raid_clean(vol, -1);
1519219974Smav				if (bioq_first(&vol->v_inflight) == NULL &&
1520219974Smav				    vol->v_tr) {
1521219974Smav					t.tv_sec = g_raid_idle_threshold / 1000000;
1522219974Smav					t.tv_usec = g_raid_idle_threshold % 1000000;
1523219974Smav					timevaladd(&t, &vol->v_last_done);
1524219974Smav					getmicrouptime(&now);
1525219974Smav					if (timevalcmp(&t, &now, <= )) {
1526219974Smav						G_RAID_TR_IDLE(vol->v_tr);
1527219974Smav						vol->v_last_done = now;
1528219974Smav					}
1529219974Smav				}
1530219974Smav			}
1531219974Smav		}
1532219974Smav		if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1533219974Smav			g_raid_destroy_node(sc, 1);	/* May not return. */
1534219974Smav	}
1535219974Smav}
1536219974Smav
1537219974Smavstatic void
1538219974Smavg_raid_poll(struct g_raid_softc *sc)
1539219974Smav{
1540219974Smav	struct g_raid_event *ep;
1541219974Smav	struct bio *bp;
1542219974Smav
1543219974Smav	sx_xlock(&sc->sc_lock);
1544219974Smav	mtx_lock(&sc->sc_queue_mtx);
1545219974Smav	/*
1546219974Smav	 * First take a look at events.
1547219974Smav	 * This is important to handle events before any I/O requests.
1548219974Smav	 */
1549219974Smav	ep = TAILQ_FIRST(&sc->sc_events);
1550219974Smav	if (ep != NULL) {
1551219974Smav		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1552219974Smav		mtx_unlock(&sc->sc_queue_mtx);
1553219974Smav		g_raid_handle_event(sc, ep);
1554219974Smav		goto out;
1555219974Smav	}
1556219974Smav	bp = bioq_takefirst(&sc->sc_queue);
1557219974Smav	if (bp != NULL) {
1558219974Smav		mtx_unlock(&sc->sc_queue_mtx);
1559219974Smav		if (bp->bio_from == NULL ||
1560219974Smav		    bp->bio_from->geom != sc->sc_geom)
1561219974Smav			g_raid_start_request(bp);
1562219974Smav		else
1563219974Smav			g_raid_disk_done_request(bp);
1564219974Smav	}
1565219974Smavout:
1566219974Smav	sx_xunlock(&sc->sc_lock);
1567219974Smav}
1568219974Smav
1569219974Smavstatic void
1570219974Smavg_raid_launch_provider(struct g_raid_volume *vol)
1571219974Smav{
1572219974Smav	struct g_raid_disk *disk;
1573219974Smav	struct g_raid_softc *sc;
1574219974Smav	struct g_provider *pp;
1575219974Smav	char name[G_RAID_MAX_VOLUMENAME];
1576219974Smav	off_t off;
1577219974Smav
1578219974Smav	sc = vol->v_softc;
1579219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
1580219974Smav
1581219974Smav	g_topology_lock();
1582219974Smav	/* Try to name provider with volume name. */
1583219974Smav	snprintf(name, sizeof(name), "raid/%s", vol->v_name);
1584219974Smav	if (g_raid_name_format == 0 || vol->v_name[0] == 0 ||
1585219974Smav	    g_provider_by_name(name) != NULL) {
1586219974Smav		/* Otherwise use sequential volume number. */
1587219974Smav		snprintf(name, sizeof(name), "raid/r%d", vol->v_global_id);
1588219974Smav	}
1589219974Smav	pp = g_new_providerf(sc->sc_geom, "%s", name);
1590219974Smav	pp->private = vol;
1591219974Smav	pp->mediasize = vol->v_mediasize;
1592219974Smav	pp->sectorsize = vol->v_sectorsize;
1593219974Smav	pp->stripesize = 0;
1594219974Smav	pp->stripeoffset = 0;
1595219974Smav	if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
1596219974Smav	    vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 ||
1597219974Smav	    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE ||
1598219974Smav	    vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT) {
1599219974Smav		if ((disk = vol->v_subdisks[0].sd_disk) != NULL &&
1600219974Smav		    disk->d_consumer != NULL &&
1601219974Smav		    disk->d_consumer->provider != NULL) {
1602219974Smav			pp->stripesize = disk->d_consumer->provider->stripesize;
1603219974Smav			off = disk->d_consumer->provider->stripeoffset;
1604219974Smav			pp->stripeoffset = off + vol->v_subdisks[0].sd_offset;
1605219974Smav			if (off > 0)
1606219974Smav				pp->stripeoffset %= off;
1607219974Smav		}
1608219974Smav		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3) {
1609219974Smav			pp->stripesize *= (vol->v_disks_count - 1);
1610219974Smav			pp->stripeoffset *= (vol->v_disks_count - 1);
1611219974Smav		}
1612219974Smav	} else
1613219974Smav		pp->stripesize = vol->v_strip_size;
1614219974Smav	vol->v_provider = pp;
1615219974Smav	g_error_provider(pp, 0);
1616219974Smav	g_topology_unlock();
1617219974Smav	G_RAID_DEBUG1(0, sc, "Provider %s for volume %s created.",
1618219974Smav	    pp->name, vol->v_name);
1619219974Smav}
1620219974Smav
1621219974Smavstatic void
1622219974Smavg_raid_destroy_provider(struct g_raid_volume *vol)
1623219974Smav{
1624219974Smav	struct g_raid_softc *sc;
1625219974Smav	struct g_provider *pp;
1626219974Smav	struct bio *bp, *tmp;
1627219974Smav
1628219974Smav	g_topology_assert_not();
1629219974Smav	sc = vol->v_softc;
1630219974Smav	pp = vol->v_provider;
1631219974Smav	KASSERT(pp != NULL, ("NULL provider (volume=%s).", vol->v_name));
1632219974Smav
1633219974Smav	g_topology_lock();
1634219974Smav	g_error_provider(pp, ENXIO);
1635219974Smav	mtx_lock(&sc->sc_queue_mtx);
1636219974Smav	TAILQ_FOREACH_SAFE(bp, &sc->sc_queue.queue, bio_queue, tmp) {
1637219974Smav		if (bp->bio_to != pp)
1638219974Smav			continue;
1639219974Smav		bioq_remove(&sc->sc_queue, bp);
1640219974Smav		g_io_deliver(bp, ENXIO);
1641219974Smav	}
1642219974Smav	mtx_unlock(&sc->sc_queue_mtx);
1643219974Smav	G_RAID_DEBUG1(0, sc, "Provider %s for volume %s destroyed.",
1644219974Smav	    pp->name, vol->v_name);
1645219974Smav	g_wither_provider(pp, ENXIO);
1646219974Smav	g_topology_unlock();
1647219974Smav	vol->v_provider = NULL;
1648219974Smav}
1649219974Smav
1650219974Smav/*
1651219974Smav * Update device state.
1652219974Smav */
1653219974Smavstatic int
1654219974Smavg_raid_update_volume(struct g_raid_volume *vol, u_int event)
1655219974Smav{
1656219974Smav	struct g_raid_softc *sc;
1657219974Smav
1658219974Smav	sc = vol->v_softc;
1659219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
1660219974Smav
1661219974Smav	G_RAID_DEBUG1(2, sc, "Event %s for volume %s.",
1662219974Smav	    g_raid_volume_event2str(event),
1663219974Smav	    vol->v_name);
1664219974Smav	switch (event) {
1665219974Smav	case G_RAID_VOLUME_E_DOWN:
1666219974Smav		if (vol->v_provider != NULL)
1667219974Smav			g_raid_destroy_provider(vol);
1668219974Smav		break;
1669219974Smav	case G_RAID_VOLUME_E_UP:
1670219974Smav		if (vol->v_provider == NULL)
1671219974Smav			g_raid_launch_provider(vol);
1672219974Smav		break;
1673219974Smav	case G_RAID_VOLUME_E_START:
1674219974Smav		if (vol->v_tr)
1675219974Smav			G_RAID_TR_START(vol->v_tr);
1676219974Smav		return (0);
1677219974Smav	default:
1678219974Smav		if (sc->sc_md)
1679219974Smav			G_RAID_MD_VOLUME_EVENT(sc->sc_md, vol, event);
1680219974Smav		return (0);
1681219974Smav	}
1682219974Smav
1683219974Smav	/* Manage root mount release. */
1684219974Smav	if (vol->v_starting) {
1685219974Smav		vol->v_starting = 0;
1686219974Smav		G_RAID_DEBUG1(1, sc, "root_mount_rel %p", vol->v_rootmount);
1687219974Smav		root_mount_rel(vol->v_rootmount);
1688219974Smav		vol->v_rootmount = NULL;
1689219974Smav	}
1690219974Smav	if (vol->v_stopping && vol->v_provider_open == 0)
1691219974Smav		g_raid_destroy_volume(vol);
1692219974Smav	return (0);
1693219974Smav}
1694219974Smav
1695219974Smav/*
1696219974Smav * Update subdisk state.
1697219974Smav */
1698219974Smavstatic int
1699219974Smavg_raid_update_subdisk(struct g_raid_subdisk *sd, u_int event)
1700219974Smav{
1701219974Smav	struct g_raid_softc *sc;
1702219974Smav	struct g_raid_volume *vol;
1703219974Smav
1704219974Smav	sc = sd->sd_softc;
1705219974Smav	vol = sd->sd_volume;
1706219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
1707219974Smav
1708219974Smav	G_RAID_DEBUG1(2, sc, "Event %s for subdisk %s:%d-%s.",
1709219974Smav	    g_raid_subdisk_event2str(event),
1710219974Smav	    vol->v_name, sd->sd_pos,
1711219974Smav	    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
1712219974Smav	if (vol->v_tr)
1713219974Smav		G_RAID_TR_EVENT(vol->v_tr, sd, event);
1714219974Smav
1715219974Smav	return (0);
1716219974Smav}
1717219974Smav
1718219974Smav/*
1719219974Smav * Update disk state.
1720219974Smav */
1721219974Smavstatic int
1722219974Smavg_raid_update_disk(struct g_raid_disk *disk, u_int event)
1723219974Smav{
1724219974Smav	struct g_raid_softc *sc;
1725219974Smav
1726219974Smav	sc = disk->d_softc;
1727219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
1728219974Smav
1729219974Smav	G_RAID_DEBUG1(2, sc, "Event %s for disk %s.",
1730219974Smav	    g_raid_disk_event2str(event),
1731219974Smav	    g_raid_get_diskname(disk));
1732219974Smav
1733219974Smav	if (sc->sc_md)
1734219974Smav		G_RAID_MD_EVENT(sc->sc_md, disk, event);
1735219974Smav	return (0);
1736219974Smav}
1737219974Smav
1738219974Smav/*
1739219974Smav * Node event.
1740219974Smav */
1741219974Smavstatic int
1742219974Smavg_raid_update_node(struct g_raid_softc *sc, u_int event)
1743219974Smav{
1744219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
1745219974Smav
1746219974Smav	G_RAID_DEBUG1(2, sc, "Event %s for the array.",
1747219974Smav	    g_raid_node_event2str(event));
1748219974Smav
1749219974Smav	if (event == G_RAID_NODE_E_WAKE)
1750219974Smav		return (0);
1751219974Smav	if (sc->sc_md)
1752219974Smav		G_RAID_MD_EVENT(sc->sc_md, NULL, event);
1753219974Smav	return (0);
1754219974Smav}
1755219974Smav
1756219974Smavstatic int
1757219974Smavg_raid_access(struct g_provider *pp, int acr, int acw, int ace)
1758219974Smav{
1759219974Smav	struct g_raid_volume *vol;
1760219974Smav	struct g_raid_softc *sc;
1761220210Smav	int dcw, opens, error = 0;
1762219974Smav
1763219974Smav	g_topology_assert();
1764219974Smav	sc = pp->geom->softc;
1765219974Smav	vol = pp->private;
1766219974Smav	KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
1767219974Smav	KASSERT(vol != NULL, ("NULL volume (provider=%s).", pp->name));
1768219974Smav
1769219974Smav	G_RAID_DEBUG1(2, sc, "Access request for %s: r%dw%de%d.", pp->name,
1770219974Smav	    acr, acw, ace);
1771219974Smav	dcw = pp->acw + acw;
1772219974Smav
1773219974Smav	g_topology_unlock();
1774219974Smav	sx_xlock(&sc->sc_lock);
1775219974Smav	/* Deny new opens while dying. */
1776219974Smav	if (sc->sc_stopping != 0 && (acr > 0 || acw > 0 || ace > 0)) {
1777219974Smav		error = ENXIO;
1778219974Smav		goto out;
1779219974Smav	}
1780219974Smav	if (dcw == 0 && vol->v_dirty)
1781219974Smav		g_raid_clean(vol, dcw);
1782219974Smav	vol->v_provider_open += acr + acw + ace;
1783219974Smav	/* Handle delayed node destruction. */
1784219974Smav	if (sc->sc_stopping == G_RAID_DESTROY_DELAYED &&
1785219974Smav	    vol->v_provider_open == 0) {
1786219974Smav		/* Count open volumes. */
1787219974Smav		opens = g_raid_nopens(sc);
1788219974Smav		if (opens == 0) {
1789219974Smav			sc->sc_stopping = G_RAID_DESTROY_HARD;
1790219974Smav			/* Wake up worker to make it selfdestruct. */
1791219974Smav			g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
1792219974Smav		}
1793219974Smav	}
1794219974Smav	/* Handle open volume destruction. */
1795219974Smav	if (vol->v_stopping && vol->v_provider_open == 0)
1796219974Smav		g_raid_destroy_volume(vol);
1797219974Smavout:
1798219974Smav	sx_xunlock(&sc->sc_lock);
1799219974Smav	g_topology_lock();
1800219974Smav	return (error);
1801219974Smav}
1802219974Smav
1803219974Smavstruct g_raid_softc *
1804219974Smavg_raid_create_node(struct g_class *mp,
1805219974Smav    const char *name, struct g_raid_md_object *md)
1806219974Smav{
1807219974Smav	struct g_raid_softc *sc;
1808219974Smav	struct g_geom *gp;
1809219974Smav	int error;
1810219974Smav
1811219974Smav	g_topology_assert();
1812219974Smav	G_RAID_DEBUG(1, "Creating array %s.", name);
1813219974Smav
1814219974Smav	gp = g_new_geomf(mp, "%s", name);
1815219974Smav	sc = malloc(sizeof(*sc), M_RAID, M_WAITOK | M_ZERO);
1816219974Smav	gp->start = g_raid_start;
1817219974Smav	gp->orphan = g_raid_orphan;
1818219974Smav	gp->access = g_raid_access;
1819219974Smav	gp->dumpconf = g_raid_dumpconf;
1820219974Smav
1821219974Smav	sc->sc_md = md;
1822219974Smav	sc->sc_geom = gp;
1823219974Smav	sc->sc_flags = 0;
1824219974Smav	TAILQ_INIT(&sc->sc_volumes);
1825219974Smav	TAILQ_INIT(&sc->sc_disks);
1826219974Smav	sx_init(&sc->sc_lock, "gmirror:lock");
1827219974Smav	mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
1828219974Smav	TAILQ_INIT(&sc->sc_events);
1829219974Smav	bioq_init(&sc->sc_queue);
1830219974Smav	gp->softc = sc;
1831219974Smav	error = kproc_create(g_raid_worker, sc, &sc->sc_worker, 0, 0,
1832219974Smav	    "g_raid %s", name);
1833219974Smav	if (error != 0) {
1834219974Smav		G_RAID_DEBUG(0, "Cannot create kernel thread for %s.", name);
1835219974Smav		mtx_destroy(&sc->sc_queue_mtx);
1836219974Smav		sx_destroy(&sc->sc_lock);
1837219974Smav		g_destroy_geom(sc->sc_geom);
1838219974Smav		free(sc, M_RAID);
1839219974Smav		return (NULL);
1840219974Smav	}
1841219974Smav
1842219974Smav	G_RAID_DEBUG1(0, sc, "Array %s created.", name);
1843219974Smav	return (sc);
1844219974Smav}
1845219974Smav
1846219974Smavstruct g_raid_volume *
1847219974Smavg_raid_create_volume(struct g_raid_softc *sc, const char *name, int id)
1848219974Smav{
1849219974Smav	struct g_raid_volume	*vol, *vol1;
1850219974Smav	int i;
1851219974Smav
1852219974Smav	G_RAID_DEBUG1(1, sc, "Creating volume %s.", name);
1853219974Smav	vol = malloc(sizeof(*vol), M_RAID, M_WAITOK | M_ZERO);
1854219974Smav	vol->v_softc = sc;
1855219974Smav	strlcpy(vol->v_name, name, G_RAID_MAX_VOLUMENAME);
1856219974Smav	vol->v_state = G_RAID_VOLUME_S_STARTING;
1857219974Smav	vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
1858219974Smav	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_UNKNOWN;
1859219974Smav	bioq_init(&vol->v_inflight);
1860219974Smav	bioq_init(&vol->v_locked);
1861219974Smav	LIST_INIT(&vol->v_locks);
1862219974Smav	for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
1863219974Smav		vol->v_subdisks[i].sd_softc = sc;
1864219974Smav		vol->v_subdisks[i].sd_volume = vol;
1865219974Smav		vol->v_subdisks[i].sd_pos = i;
1866219974Smav		vol->v_subdisks[i].sd_state = G_RAID_DISK_S_NONE;
1867219974Smav	}
1868219974Smav
1869219974Smav	/* Find free ID for this volume. */
1870219974Smav	g_topology_lock();
1871219974Smav	vol1 = vol;
1872219974Smav	if (id >= 0) {
1873219974Smav		LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1874219974Smav			if (vol1->v_global_id == id)
1875219974Smav				break;
1876219974Smav		}
1877219974Smav	}
1878219974Smav	if (vol1 != NULL) {
1879219974Smav		for (id = 0; ; id++) {
1880219974Smav			LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1881219974Smav				if (vol1->v_global_id == id)
1882219974Smav					break;
1883219974Smav			}
1884219974Smav			if (vol1 == NULL)
1885219974Smav				break;
1886219974Smav		}
1887219974Smav	}
1888219974Smav	vol->v_global_id = id;
1889219974Smav	LIST_INSERT_HEAD(&g_raid_volumes, vol, v_global_next);
1890219974Smav	g_topology_unlock();
1891219974Smav
1892219974Smav	/* Delay root mounting. */
1893219974Smav	vol->v_rootmount = root_mount_hold("GRAID");
1894219974Smav	G_RAID_DEBUG1(1, sc, "root_mount_hold %p", vol->v_rootmount);
1895219974Smav	vol->v_starting = 1;
1896219974Smav	TAILQ_INSERT_TAIL(&sc->sc_volumes, vol, v_next);
1897219974Smav	return (vol);
1898219974Smav}
1899219974Smav
1900219974Smavstruct g_raid_disk *
1901219974Smavg_raid_create_disk(struct g_raid_softc *sc)
1902219974Smav{
1903219974Smav	struct g_raid_disk	*disk;
1904219974Smav
1905219974Smav	G_RAID_DEBUG1(1, sc, "Creating disk.");
1906219974Smav	disk = malloc(sizeof(*disk), M_RAID, M_WAITOK | M_ZERO);
1907219974Smav	disk->d_softc = sc;
1908219974Smav	disk->d_state = G_RAID_DISK_S_NONE;
1909219974Smav	TAILQ_INIT(&disk->d_subdisks);
1910219974Smav	TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
1911219974Smav	return (disk);
1912219974Smav}
1913219974Smav
1914219974Smavint g_raid_start_volume(struct g_raid_volume *vol)
1915219974Smav{
1916219974Smav	struct g_raid_tr_class *class;
1917219974Smav	struct g_raid_tr_object *obj;
1918219974Smav	int status;
1919219974Smav
1920219974Smav	G_RAID_DEBUG1(2, vol->v_softc, "Starting volume %s.", vol->v_name);
1921219974Smav	LIST_FOREACH(class, &g_raid_tr_classes, trc_list) {
1922219974Smav		G_RAID_DEBUG1(2, vol->v_softc,
1923219974Smav		    "Tasting volume %s for %s transformation.",
1924219974Smav		    vol->v_name, class->name);
1925219974Smav		obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
1926219974Smav		    M_WAITOK);
1927219974Smav		obj->tro_class = class;
1928219974Smav		obj->tro_volume = vol;
1929219974Smav		status = G_RAID_TR_TASTE(obj, vol);
1930219974Smav		if (status != G_RAID_TR_TASTE_FAIL)
1931219974Smav			break;
1932219974Smav		kobj_delete((kobj_t)obj, M_RAID);
1933219974Smav	}
1934219974Smav	if (class == NULL) {
1935219974Smav		G_RAID_DEBUG1(0, vol->v_softc,
1936219974Smav		    "No transformation module found for %s.",
1937219974Smav		    vol->v_name);
1938219974Smav		vol->v_tr = NULL;
1939219974Smav		g_raid_change_volume_state(vol, G_RAID_VOLUME_S_UNSUPPORTED);
1940219974Smav		g_raid_event_send(vol, G_RAID_VOLUME_E_DOWN,
1941219974Smav		    G_RAID_EVENT_VOLUME);
1942219974Smav		return (-1);
1943219974Smav	}
1944219974Smav	G_RAID_DEBUG1(2, vol->v_softc,
1945219974Smav	    "Transformation module %s chosen for %s.",
1946219974Smav	    class->name, vol->v_name);
1947219974Smav	vol->v_tr = obj;
1948219974Smav	return (0);
1949219974Smav}
1950219974Smav
1951219974Smavint
1952219974Smavg_raid_destroy_node(struct g_raid_softc *sc, int worker)
1953219974Smav{
1954219974Smav	struct g_raid_volume *vol, *tmpv;
1955219974Smav	struct g_raid_disk *disk, *tmpd;
1956219974Smav	int error = 0;
1957219974Smav
1958219974Smav	sc->sc_stopping = G_RAID_DESTROY_HARD;
1959219974Smav	TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tmpv) {
1960219974Smav		if (g_raid_destroy_volume(vol))
1961219974Smav			error = EBUSY;
1962219974Smav	}
1963219974Smav	if (error)
1964219974Smav		return (error);
1965219974Smav	TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tmpd) {
1966219974Smav		if (g_raid_destroy_disk(disk))
1967219974Smav			error = EBUSY;
1968219974Smav	}
1969219974Smav	if (error)
1970219974Smav		return (error);
1971219974Smav	if (sc->sc_md) {
1972219974Smav		G_RAID_MD_FREE(sc->sc_md);
1973219974Smav		kobj_delete((kobj_t)sc->sc_md, M_RAID);
1974219974Smav		sc->sc_md = NULL;
1975219974Smav	}
1976219974Smav	if (sc->sc_geom != NULL) {
1977219974Smav		G_RAID_DEBUG1(0, sc, "Array %s destroyed.", sc->sc_name);
1978219974Smav		g_topology_lock();
1979219974Smav		sc->sc_geom->softc = NULL;
1980219974Smav		g_wither_geom(sc->sc_geom, ENXIO);
1981219974Smav		g_topology_unlock();
1982219974Smav		sc->sc_geom = NULL;
1983219974Smav	} else
1984219974Smav		G_RAID_DEBUG(1, "Array destroyed.");
1985219974Smav	if (worker) {
1986219974Smav		g_raid_event_cancel(sc, sc);
1987219974Smav		mtx_destroy(&sc->sc_queue_mtx);
1988219974Smav		sx_xunlock(&sc->sc_lock);
1989219974Smav		sx_destroy(&sc->sc_lock);
1990219974Smav		wakeup(&sc->sc_stopping);
1991219974Smav		free(sc, M_RAID);
1992219974Smav		curthread->td_pflags &= ~TDP_GEOM;
1993219974Smav		G_RAID_DEBUG(1, "Thread exiting.");
1994219974Smav		kproc_exit(0);
1995219974Smav	} else {
1996219974Smav		/* Wake up worker to make it selfdestruct. */
1997219974Smav		g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
1998219974Smav	}
1999219974Smav	return (0);
2000219974Smav}
2001219974Smav
2002219974Smavint
2003219974Smavg_raid_destroy_volume(struct g_raid_volume *vol)
2004219974Smav{
2005219974Smav	struct g_raid_softc *sc;
2006219974Smav	struct g_raid_disk *disk;
2007219974Smav	int i;
2008219974Smav
2009219974Smav	sc = vol->v_softc;
2010219974Smav	G_RAID_DEBUG1(2, sc, "Destroying volume %s.", vol->v_name);
2011219974Smav	vol->v_stopping = 1;
2012219974Smav	if (vol->v_state != G_RAID_VOLUME_S_STOPPED) {
2013219974Smav		if (vol->v_tr) {
2014219974Smav			G_RAID_TR_STOP(vol->v_tr);
2015219974Smav			return (EBUSY);
2016219974Smav		} else
2017219974Smav			vol->v_state = G_RAID_VOLUME_S_STOPPED;
2018219974Smav	}
2019219974Smav	if (g_raid_event_check(sc, vol) != 0)
2020219974Smav		return (EBUSY);
2021219974Smav	if (vol->v_provider != NULL)
2022219974Smav		return (EBUSY);
2023219974Smav	if (vol->v_provider_open != 0)
2024219974Smav		return (EBUSY);
2025219974Smav	if (vol->v_tr) {
2026219974Smav		G_RAID_TR_FREE(vol->v_tr);
2027219974Smav		kobj_delete((kobj_t)vol->v_tr, M_RAID);
2028219974Smav		vol->v_tr = NULL;
2029219974Smav	}
2030219974Smav	if (vol->v_rootmount)
2031219974Smav		root_mount_rel(vol->v_rootmount);
2032219974Smav	g_topology_lock();
2033219974Smav	LIST_REMOVE(vol, v_global_next);
2034219974Smav	g_topology_unlock();
2035219974Smav	TAILQ_REMOVE(&sc->sc_volumes, vol, v_next);
2036219974Smav	for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
2037219974Smav		g_raid_event_cancel(sc, &vol->v_subdisks[i]);
2038219974Smav		disk = vol->v_subdisks[i].sd_disk;
2039219974Smav		if (disk == NULL)
2040219974Smav			continue;
2041219974Smav		TAILQ_REMOVE(&disk->d_subdisks, &vol->v_subdisks[i], sd_next);
2042219974Smav	}
2043219974Smav	G_RAID_DEBUG1(2, sc, "Volume %s destroyed.", vol->v_name);
2044219974Smav	if (sc->sc_md)
2045219974Smav		G_RAID_MD_FREE_VOLUME(sc->sc_md, vol);
2046219974Smav	g_raid_event_cancel(sc, vol);
2047219974Smav	free(vol, M_RAID);
2048219974Smav	if (sc->sc_stopping == G_RAID_DESTROY_HARD) {
2049219974Smav		/* Wake up worker to let it selfdestruct. */
2050219974Smav		g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2051219974Smav	}
2052219974Smav	return (0);
2053219974Smav}
2054219974Smav
2055219974Smavint
2056219974Smavg_raid_destroy_disk(struct g_raid_disk *disk)
2057219974Smav{
2058219974Smav	struct g_raid_softc *sc;
2059219974Smav	struct g_raid_subdisk *sd, *tmp;
2060219974Smav
2061219974Smav	sc = disk->d_softc;
2062219974Smav	G_RAID_DEBUG1(2, sc, "Destroying disk.");
2063219974Smav	if (disk->d_consumer) {
2064219974Smav		g_raid_kill_consumer(sc, disk->d_consumer);
2065219974Smav		disk->d_consumer = NULL;
2066219974Smav	}
2067219974Smav	TAILQ_FOREACH_SAFE(sd, &disk->d_subdisks, sd_next, tmp) {
2068219974Smav		g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_NONE);
2069219974Smav		g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
2070219974Smav		    G_RAID_EVENT_SUBDISK);
2071219974Smav		TAILQ_REMOVE(&disk->d_subdisks, sd, sd_next);
2072219974Smav		sd->sd_disk = NULL;
2073219974Smav	}
2074219974Smav	TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
2075219974Smav	if (sc->sc_md)
2076219974Smav		G_RAID_MD_FREE_DISK(sc->sc_md, disk);
2077219974Smav	g_raid_event_cancel(sc, disk);
2078219974Smav	free(disk, M_RAID);
2079219974Smav	return (0);
2080219974Smav}
2081219974Smav
2082219974Smavint
2083219974Smavg_raid_destroy(struct g_raid_softc *sc, int how)
2084219974Smav{
2085219974Smav	int opens;
2086219974Smav
2087219974Smav	g_topology_assert_not();
2088219974Smav	if (sc == NULL)
2089219974Smav		return (ENXIO);
2090219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
2091219974Smav
2092219974Smav	/* Count open volumes. */
2093219974Smav	opens = g_raid_nopens(sc);
2094219974Smav
2095219974Smav	/* React on some opened volumes. */
2096219974Smav	if (opens > 0) {
2097219974Smav		switch (how) {
2098219974Smav		case G_RAID_DESTROY_SOFT:
2099219974Smav			G_RAID_DEBUG1(1, sc,
2100219974Smav			    "%d volumes are still open.",
2101219974Smav			    opens);
2102219974Smav			return (EBUSY);
2103219974Smav		case G_RAID_DESTROY_DELAYED:
2104219974Smav			G_RAID_DEBUG1(1, sc,
2105219974Smav			    "Array will be destroyed on last close.");
2106219974Smav			sc->sc_stopping = G_RAID_DESTROY_DELAYED;
2107219974Smav			return (EBUSY);
2108219974Smav		case G_RAID_DESTROY_HARD:
2109219974Smav			G_RAID_DEBUG1(1, sc,
2110219974Smav			    "%d volumes are still open.",
2111219974Smav			    opens);
2112219974Smav		}
2113219974Smav	}
2114219974Smav
2115219974Smav	/* Mark node for destruction. */
2116219974Smav	sc->sc_stopping = G_RAID_DESTROY_HARD;
2117219974Smav	/* Wake up worker to let it selfdestruct. */
2118219974Smav	g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2119219974Smav	/* Sleep until node destroyed. */
2120219974Smav	sx_sleep(&sc->sc_stopping, &sc->sc_lock,
2121219974Smav	    PRIBIO | PDROP, "r:destroy", 0);
2122219974Smav	return (0);
2123219974Smav}
2124219974Smav
2125219974Smavstatic void
2126219974Smavg_raid_taste_orphan(struct g_consumer *cp)
2127219974Smav{
2128219974Smav
2129219974Smav	KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2130219974Smav	    cp->provider->name));
2131219974Smav}
2132219974Smav
2133219974Smavstatic struct g_geom *
2134219974Smavg_raid_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2135219974Smav{
2136219974Smav	struct g_consumer *cp;
2137219974Smav	struct g_geom *gp, *geom;
2138219974Smav	struct g_raid_md_class *class;
2139219974Smav	struct g_raid_md_object *obj;
2140219974Smav	int status;
2141219974Smav
2142219974Smav	g_topology_assert();
2143219974Smav	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
2144219974Smav	G_RAID_DEBUG(2, "Tasting provider %s.", pp->name);
2145219974Smav
2146219974Smav	gp = g_new_geomf(mp, "mirror:taste");
2147219974Smav	/*
2148219974Smav	 * This orphan function should be never called.
2149219974Smav	 */
2150219974Smav	gp->orphan = g_raid_taste_orphan;
2151219974Smav	cp = g_new_consumer(gp);
2152219974Smav	g_attach(cp, pp);
2153219974Smav
2154219974Smav	geom = NULL;
2155219974Smav	LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2156219974Smav		G_RAID_DEBUG(2, "Tasting provider %s for %s metadata.",
2157219974Smav		    pp->name, class->name);
2158219974Smav		obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2159219974Smav		    M_WAITOK);
2160219974Smav		obj->mdo_class = class;
2161219974Smav		status = G_RAID_MD_TASTE(obj, mp, cp, &geom);
2162219974Smav		if (status != G_RAID_MD_TASTE_NEW)
2163219974Smav			kobj_delete((kobj_t)obj, M_RAID);
2164219974Smav		if (status != G_RAID_MD_TASTE_FAIL)
2165219974Smav			break;
2166219974Smav	}
2167219974Smav
2168219974Smav	g_detach(cp);
2169219974Smav	g_destroy_consumer(cp);
2170219974Smav	g_destroy_geom(gp);
2171219974Smav	G_RAID_DEBUG(2, "Tasting provider %s done.", pp->name);
2172219974Smav	return (geom);
2173219974Smav}
2174219974Smav
2175219974Smavint
2176219974Smavg_raid_create_node_format(const char *format, struct g_geom **gp)
2177219974Smav{
2178219974Smav	struct g_raid_md_class *class;
2179219974Smav	struct g_raid_md_object *obj;
2180219974Smav	int status;
2181219974Smav
2182219974Smav	G_RAID_DEBUG(2, "Creating array for %s metadata.", format);
2183219974Smav	LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2184219974Smav		if (strcasecmp(class->name, format) == 0)
2185219974Smav			break;
2186219974Smav	}
2187219974Smav	if (class == NULL) {
2188219974Smav		G_RAID_DEBUG(1, "No support for %s metadata.", format);
2189219974Smav		return (G_RAID_MD_TASTE_FAIL);
2190219974Smav	}
2191219974Smav	obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2192219974Smav	    M_WAITOK);
2193219974Smav	obj->mdo_class = class;
2194219974Smav	status = G_RAID_MD_CREATE(obj, &g_raid_class, gp);
2195219974Smav	if (status != G_RAID_MD_TASTE_NEW)
2196219974Smav		kobj_delete((kobj_t)obj, M_RAID);
2197219974Smav	return (status);
2198219974Smav}
2199219974Smav
2200219974Smavstatic int
2201219974Smavg_raid_destroy_geom(struct gctl_req *req __unused,
2202219974Smav    struct g_class *mp __unused, struct g_geom *gp)
2203219974Smav{
2204219974Smav	struct g_raid_softc *sc;
2205219974Smav	int error;
2206219974Smav
2207219974Smav	g_topology_unlock();
2208219974Smav	sc = gp->softc;
2209219974Smav	sx_xlock(&sc->sc_lock);
2210219974Smav	g_cancel_event(sc);
2211219974Smav	error = g_raid_destroy(gp->softc, G_RAID_DESTROY_SOFT);
2212219974Smav	if (error != 0)
2213219974Smav		sx_xunlock(&sc->sc_lock);
2214219974Smav	g_topology_lock();
2215219974Smav	return (error);
2216219974Smav}
2217219974Smav
2218219974Smavvoid g_raid_write_metadata(struct g_raid_softc *sc, struct g_raid_volume *vol,
2219219974Smav    struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2220219974Smav{
2221219974Smav
2222219974Smav	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2223219974Smav		return;
2224219974Smav	if (sc->sc_md)
2225219974Smav		G_RAID_MD_WRITE(sc->sc_md, vol, sd, disk);
2226219974Smav}
2227219974Smav
2228219974Smavvoid g_raid_fail_disk(struct g_raid_softc *sc,
2229219974Smav    struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2230219974Smav{
2231219974Smav
2232219974Smav	if (disk == NULL)
2233219974Smav		disk = sd->sd_disk;
2234219974Smav	if (disk == NULL) {
2235219974Smav		G_RAID_DEBUG1(0, sc, "Warning! Fail request to an absent disk!");
2236219974Smav		return;
2237219974Smav	}
2238219974Smav	if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2239219974Smav		G_RAID_DEBUG1(0, sc, "Warning! Fail request to a disk in a "
2240219974Smav		    "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
2241219974Smav		return;
2242219974Smav	}
2243219974Smav	if (sc->sc_md)
2244219974Smav		G_RAID_MD_FAIL_DISK(sc->sc_md, sd, disk);
2245219974Smav}
2246219974Smav
2247219974Smavstatic void
2248219974Smavg_raid_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2249219974Smav    struct g_consumer *cp, struct g_provider *pp)
2250219974Smav{
2251219974Smav	struct g_raid_softc *sc;
2252219974Smav	struct g_raid_volume *vol;
2253219974Smav	struct g_raid_subdisk *sd;
2254219974Smav	struct g_raid_disk *disk;
2255219974Smav	int i, s;
2256219974Smav
2257219974Smav	g_topology_assert();
2258219974Smav
2259219974Smav	sc = gp->softc;
2260219974Smav	if (sc == NULL)
2261219974Smav		return;
2262219974Smav	if (pp != NULL) {
2263219974Smav		vol = pp->private;
2264219974Smav		g_topology_unlock();
2265219974Smav		sx_xlock(&sc->sc_lock);
2266219974Smav		sbuf_printf(sb, "%s<Label>%s</Label>\n", indent,
2267219974Smav		    vol->v_name);
2268219974Smav		sbuf_printf(sb, "%s<RAIDLevel>%s</RAIDLevel>\n", indent,
2269219974Smav		    g_raid_volume_level2str(vol->v_raid_level,
2270219974Smav		    vol->v_raid_level_qualifier));
2271219974Smav		sbuf_printf(sb,
2272219974Smav		    "%s<Transformation>%s</Transformation>\n", indent,
2273219974Smav		    vol->v_tr ? vol->v_tr->tro_class->name : "NONE");
2274219974Smav		sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
2275219974Smav		    vol->v_disks_count);
2276219974Smav		sbuf_printf(sb, "%s<Strip>%u</Strip>\n", indent,
2277219974Smav		    vol->v_strip_size);
2278219974Smav		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2279219974Smav		    g_raid_volume_state2str(vol->v_state));
2280219974Smav		sbuf_printf(sb, "%s<Dirty>%s</Dirty>\n", indent,
2281219974Smav		    vol->v_dirty ? "Yes" : "No");
2282219974Smav		sbuf_printf(sb, "%s<Subdisks>", indent);
2283219974Smav		for (i = 0; i < vol->v_disks_count; i++) {
2284219974Smav			sd = &vol->v_subdisks[i];
2285219974Smav			if (sd->sd_disk != NULL &&
2286219974Smav			    sd->sd_disk->d_consumer != NULL) {
2287219974Smav				sbuf_printf(sb, "%s ",
2288219974Smav				    g_raid_get_diskname(sd->sd_disk));
2289219974Smav			} else {
2290219974Smav				sbuf_printf(sb, "NONE ");
2291219974Smav			}
2292219974Smav			sbuf_printf(sb, "(%s",
2293219974Smav			    g_raid_subdisk_state2str(sd->sd_state));
2294219974Smav			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2295219974Smav			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2296219974Smav				sbuf_printf(sb, " %d%%",
2297219974Smav				    (int)(sd->sd_rebuild_pos * 100 /
2298219974Smav				     sd->sd_size));
2299219974Smav			}
2300219974Smav			sbuf_printf(sb, ")");
2301219974Smav			if (i + 1 < vol->v_disks_count)
2302219974Smav				sbuf_printf(sb, ", ");
2303219974Smav		}
2304219974Smav		sbuf_printf(sb, "</Subdisks>\n");
2305219974Smav		sx_xunlock(&sc->sc_lock);
2306219974Smav		g_topology_lock();
2307219974Smav	} else if (cp != NULL) {
2308219974Smav		disk = cp->private;
2309219974Smav		if (disk == NULL)
2310219974Smav			return;
2311219974Smav		g_topology_unlock();
2312219974Smav		sx_xlock(&sc->sc_lock);
2313219974Smav		sbuf_printf(sb, "%s<State>%s", indent,
2314219974Smav		    g_raid_disk_state2str(disk->d_state));
2315219974Smav		if (!TAILQ_EMPTY(&disk->d_subdisks)) {
2316219974Smav			sbuf_printf(sb, " (");
2317219974Smav			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2318219974Smav				sbuf_printf(sb, "%s",
2319219974Smav				    g_raid_subdisk_state2str(sd->sd_state));
2320219974Smav				if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2321219974Smav				    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2322219974Smav					sbuf_printf(sb, " %d%%",
2323219974Smav					    (int)(sd->sd_rebuild_pos * 100 /
2324219974Smav					     sd->sd_size));
2325219974Smav				}
2326219974Smav				if (TAILQ_NEXT(sd, sd_next))
2327219974Smav					sbuf_printf(sb, ", ");
2328219974Smav			}
2329219974Smav			sbuf_printf(sb, ")");
2330219974Smav		}
2331219974Smav		sbuf_printf(sb, "</State>\n");
2332219974Smav		sbuf_printf(sb, "%s<Subdisks>", indent);
2333219974Smav		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2334219974Smav			sbuf_printf(sb, "r%d(%s):%d@%ju",
2335219974Smav			    sd->sd_volume->v_global_id,
2336219974Smav			    sd->sd_volume->v_name,
2337219974Smav			    sd->sd_pos, sd->sd_offset);
2338219974Smav			if (TAILQ_NEXT(sd, sd_next))
2339219974Smav				sbuf_printf(sb, ", ");
2340219974Smav		}
2341219974Smav		sbuf_printf(sb, "</Subdisks>\n");
2342219974Smav		sbuf_printf(sb, "%s<ReadErrors>%d</ReadErrors>\n", indent,
2343219974Smav		    disk->d_read_errs);
2344219974Smav		sx_xunlock(&sc->sc_lock);
2345219974Smav		g_topology_lock();
2346219974Smav	} else {
2347219974Smav		g_topology_unlock();
2348219974Smav		sx_xlock(&sc->sc_lock);
2349219974Smav		if (sc->sc_md) {
2350219974Smav			sbuf_printf(sb, "%s<Metadata>%s</Metadata>\n", indent,
2351219974Smav			    sc->sc_md->mdo_class->name);
2352219974Smav		}
2353219974Smav		if (!TAILQ_EMPTY(&sc->sc_volumes)) {
2354219974Smav			s = 0xff;
2355219974Smav			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2356219974Smav				if (vol->v_state < s)
2357219974Smav					s = vol->v_state;
2358219974Smav			}
2359219974Smav			sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2360219974Smav			    g_raid_volume_state2str(s));
2361219974Smav		}
2362219974Smav		sx_xunlock(&sc->sc_lock);
2363219974Smav		g_topology_lock();
2364219974Smav	}
2365219974Smav}
2366219974Smav
2367219974Smavstatic void
2368219974Smavg_raid_shutdown_pre_sync(void *arg, int howto)
2369219974Smav{
2370219974Smav	struct g_class *mp;
2371219974Smav	struct g_geom *gp, *gp2;
2372219974Smav	struct g_raid_softc *sc;
2373219974Smav	int error;
2374219974Smav
2375219974Smav	mp = arg;
2376219974Smav	DROP_GIANT();
2377219974Smav	g_topology_lock();
2378219974Smav	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
2379219974Smav		if ((sc = gp->softc) == NULL)
2380219974Smav			continue;
2381219974Smav		g_topology_unlock();
2382219974Smav		sx_xlock(&sc->sc_lock);
2383219974Smav		g_cancel_event(sc);
2384219974Smav		error = g_raid_destroy(sc, G_RAID_DESTROY_DELAYED);
2385219974Smav		if (error != 0)
2386219974Smav			sx_xunlock(&sc->sc_lock);
2387219974Smav		g_topology_lock();
2388219974Smav	}
2389219974Smav	g_topology_unlock();
2390219974Smav	PICKUP_GIANT();
2391219974Smav}
2392219974Smav
2393219974Smavstatic void
2394219974Smavg_raid_init(struct g_class *mp)
2395219974Smav{
2396219974Smav
2397219974Smav	g_raid_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
2398219974Smav	    g_raid_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
2399219974Smav	if (g_raid_pre_sync == NULL)
2400219974Smav		G_RAID_DEBUG(0, "Warning! Cannot register shutdown event.");
2401219974Smav	g_raid_started = 1;
2402219974Smav}
2403219974Smav
2404219974Smavstatic void
2405219974Smavg_raid_fini(struct g_class *mp)
2406219974Smav{
2407219974Smav
2408219974Smav	if (g_raid_pre_sync != NULL)
2409219974Smav		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_raid_pre_sync);
2410219974Smav	g_raid_started = 0;
2411219974Smav}
2412219974Smav
2413219974Smavint
2414219974Smavg_raid_md_modevent(module_t mod, int type, void *arg)
2415219974Smav{
2416219974Smav	struct g_raid_md_class *class, *c, *nc;
2417219974Smav	int error;
2418219974Smav
2419219974Smav	error = 0;
2420219974Smav	class = arg;
2421219974Smav	switch (type) {
2422219974Smav	case MOD_LOAD:
2423219974Smav		c = LIST_FIRST(&g_raid_md_classes);
2424219974Smav		if (c == NULL || c->mdc_priority > class->mdc_priority)
2425219974Smav			LIST_INSERT_HEAD(&g_raid_md_classes, class, mdc_list);
2426219974Smav		else {
2427219974Smav			while ((nc = LIST_NEXT(c, mdc_list)) != NULL &&
2428219974Smav			    nc->mdc_priority < class->mdc_priority)
2429219974Smav				c = nc;
2430219974Smav			LIST_INSERT_AFTER(c, class, mdc_list);
2431219974Smav		}
2432219974Smav		if (g_raid_started)
2433219974Smav			g_retaste(&g_raid_class);
2434219974Smav		break;
2435219974Smav	case MOD_UNLOAD:
2436219974Smav		LIST_REMOVE(class, mdc_list);
2437219974Smav		break;
2438219974Smav	default:
2439219974Smav		error = EOPNOTSUPP;
2440219974Smav		break;
2441219974Smav	}
2442219974Smav
2443219974Smav	return (error);
2444219974Smav}
2445219974Smav
2446219974Smavint
2447219974Smavg_raid_tr_modevent(module_t mod, int type, void *arg)
2448219974Smav{
2449219974Smav	struct g_raid_tr_class *class, *c, *nc;
2450219974Smav	int error;
2451219974Smav
2452219974Smav	error = 0;
2453219974Smav	class = arg;
2454219974Smav	switch (type) {
2455219974Smav	case MOD_LOAD:
2456219974Smav		c = LIST_FIRST(&g_raid_tr_classes);
2457219974Smav		if (c == NULL || c->trc_priority > class->trc_priority)
2458219974Smav			LIST_INSERT_HEAD(&g_raid_tr_classes, class, trc_list);
2459219974Smav		else {
2460219974Smav			while ((nc = LIST_NEXT(c, trc_list)) != NULL &&
2461219974Smav			    nc->trc_priority < class->trc_priority)
2462219974Smav				c = nc;
2463219974Smav			LIST_INSERT_AFTER(c, class, trc_list);
2464219974Smav		}
2465219974Smav		break;
2466219974Smav	case MOD_UNLOAD:
2467219974Smav		LIST_REMOVE(class, trc_list);
2468219974Smav		break;
2469219974Smav	default:
2470219974Smav		error = EOPNOTSUPP;
2471219974Smav		break;
2472219974Smav	}
2473219974Smav
2474219974Smav	return (error);
2475219974Smav}
2476219974Smav
2477219974Smav/*
2478219974Smav * Use local implementation of DECLARE_GEOM_CLASS(g_raid_class, g_raid)
2479219974Smav * to reduce module priority, allowing submodules to register them first.
2480219974Smav */
2481219974Smavstatic moduledata_t g_raid_mod = {
2482219974Smav	"g_raid",
2483219974Smav	g_modevent,
2484219974Smav	&g_raid_class
2485219974Smav};
2486219974SmavDECLARE_MODULE(g_raid, g_raid_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD);
2487219974SmavMODULE_VERSION(geom_raid, 0);
2488