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