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