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;
56240465SmavTUNABLE_INT("kern.geom.raid.enable", &g_raid_enable);
57240465SmavSYSCTL_INT(_kern_geom_raid, OID_AUTO, enable, CTLFLAG_RW,
58240465Smav    &g_raid_enable, 0, "Enable on-disk metadata taste");
59219974Smavu_int g_raid_aggressive_spare = 0;
60219974SmavTUNABLE_INT("kern.geom.raid.aggressive_spare", &g_raid_aggressive_spare);
61219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, aggressive_spare, CTLFLAG_RW,
62219974Smav    &g_raid_aggressive_spare, 0, "Use disks without metadata as spare");
63220790Smavu_int g_raid_debug = 0;
64219974SmavTUNABLE_INT("kern.geom.raid.debug", &g_raid_debug);
65219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, debug, CTLFLAG_RW, &g_raid_debug, 0,
66219974Smav    "Debug level");
67219974Smavint g_raid_read_err_thresh = 10;
68219974SmavTUNABLE_INT("kern.geom.raid.read_err_thresh", &g_raid_read_err_thresh);
69219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, read_err_thresh, CTLFLAG_RW,
70219974Smav    &g_raid_read_err_thresh, 0,
71219974Smav    "Number of read errors equated to disk failure");
72219974Smavu_int g_raid_start_timeout = 30;
73219974SmavTUNABLE_INT("kern.geom.raid.start_timeout", &g_raid_start_timeout);
74219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, start_timeout, CTLFLAG_RW,
75219974Smav    &g_raid_start_timeout, 0,
76219974Smav    "Time to wait for all array components");
77219974Smavstatic u_int g_raid_clean_time = 5;
78219974SmavTUNABLE_INT("kern.geom.raid.clean_time", &g_raid_clean_time);
79219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, clean_time, CTLFLAG_RW,
80219974Smav    &g_raid_clean_time, 0, "Mark volume as clean when idling");
81219974Smavstatic u_int g_raid_disconnect_on_failure = 1;
82219974SmavTUNABLE_INT("kern.geom.raid.disconnect_on_failure",
83219974Smav    &g_raid_disconnect_on_failure);
84219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
85219974Smav    &g_raid_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
86219974Smavstatic u_int g_raid_name_format = 0;
87219974SmavTUNABLE_INT("kern.geom.raid.name_format", &g_raid_name_format);
88219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, name_format, CTLFLAG_RW,
89219974Smav    &g_raid_name_format, 0, "Providers name format.");
90219974Smavstatic u_int g_raid_idle_threshold = 1000000;
91219974SmavTUNABLE_INT("kern.geom.raid.idle_threshold", &g_raid_idle_threshold);
92219974SmavSYSCTL_UINT(_kern_geom_raid, OID_AUTO, idle_threshold, CTLFLAG_RW,
93219974Smav    &g_raid_idle_threshold, 1000000,
94219974Smav    "Time in microseconds to consider a volume idle.");
95248068Ssbrunostatic u_int ar_legacy_aliases = 1;
96248068SsbrunoSYSCTL_INT(_kern_geom_raid, OID_AUTO, legacy_aliases, CTLFLAG_RW,
97248068Ssbruno           &ar_legacy_aliases, 0, "Create aliases named as the legacy ataraid style.");
98248068SsbrunoTUNABLE_INT("kern.geom_raid.legacy_aliases", &ar_legacy_aliases);
99219974Smav
100248068Ssbruno
101219974Smav#define	MSLEEP(rv, ident, mtx, priority, wmesg, timeout)	do {	\
102219974Smav	G_RAID_DEBUG(4, "%s: Sleeping %p.", __func__, (ident));		\
103219974Smav	rv = msleep((ident), (mtx), (priority), (wmesg), (timeout));	\
104219974Smav	G_RAID_DEBUG(4, "%s: Woken up %p.", __func__, (ident));		\
105219974Smav} while (0)
106219974Smav
107219974SmavLIST_HEAD(, g_raid_md_class) g_raid_md_classes =
108219974Smav    LIST_HEAD_INITIALIZER(g_raid_md_classes);
109219974Smav
110219974SmavLIST_HEAD(, g_raid_tr_class) g_raid_tr_classes =
111219974Smav    LIST_HEAD_INITIALIZER(g_raid_tr_classes);
112219974Smav
113219974SmavLIST_HEAD(, g_raid_volume) g_raid_volumes =
114219974Smav    LIST_HEAD_INITIALIZER(g_raid_volumes);
115219974Smav
116242314Smavstatic eventhandler_tag g_raid_post_sync = NULL;
117219974Smavstatic int g_raid_started = 0;
118242314Smavstatic int g_raid_shutdown = 0;
119219974Smav
120219974Smavstatic int g_raid_destroy_geom(struct gctl_req *req, struct g_class *mp,
121219974Smav    struct g_geom *gp);
122219974Smavstatic g_taste_t g_raid_taste;
123219974Smavstatic void g_raid_init(struct g_class *mp);
124219974Smavstatic void g_raid_fini(struct g_class *mp);
125219974Smav
126219974Smavstruct g_class g_raid_class = {
127219974Smav	.name = G_RAID_CLASS_NAME,
128219974Smav	.version = G_VERSION,
129219974Smav	.ctlreq = g_raid_ctl,
130219974Smav	.taste = g_raid_taste,
131219974Smav	.destroy_geom = g_raid_destroy_geom,
132219974Smav	.init = g_raid_init,
133219974Smav	.fini = g_raid_fini
134219974Smav};
135219974Smav
136219974Smavstatic void g_raid_destroy_provider(struct g_raid_volume *vol);
137219974Smavstatic int g_raid_update_disk(struct g_raid_disk *disk, u_int event);
138219974Smavstatic int g_raid_update_subdisk(struct g_raid_subdisk *subdisk, u_int event);
139219974Smavstatic int g_raid_update_volume(struct g_raid_volume *vol, u_int event);
140219974Smavstatic int g_raid_update_node(struct g_raid_softc *sc, u_int event);
141219974Smavstatic void g_raid_dumpconf(struct sbuf *sb, const char *indent,
142219974Smav    struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
143219974Smavstatic void g_raid_start(struct bio *bp);
144219974Smavstatic void g_raid_start_request(struct bio *bp);
145219974Smavstatic void g_raid_disk_done(struct bio *bp);
146219974Smavstatic void g_raid_poll(struct g_raid_softc *sc);
147219974Smav
148219974Smavstatic const char *
149219974Smavg_raid_node_event2str(int event)
150219974Smav{
151219974Smav
152219974Smav	switch (event) {
153219974Smav	case G_RAID_NODE_E_WAKE:
154219974Smav		return ("WAKE");
155219974Smav	case G_RAID_NODE_E_START:
156219974Smav		return ("START");
157219974Smav	default:
158219974Smav		return ("INVALID");
159219974Smav	}
160219974Smav}
161219974Smav
162219974Smavconst char *
163219974Smavg_raid_disk_state2str(int state)
164219974Smav{
165219974Smav
166219974Smav	switch (state) {
167219974Smav	case G_RAID_DISK_S_NONE:
168219974Smav		return ("NONE");
169219974Smav	case G_RAID_DISK_S_OFFLINE:
170219974Smav		return ("OFFLINE");
171245326Smav	case G_RAID_DISK_S_DISABLED:
172245326Smav		return ("DISABLED");
173219974Smav	case G_RAID_DISK_S_FAILED:
174219974Smav		return ("FAILED");
175219974Smav	case G_RAID_DISK_S_STALE_FAILED:
176219974Smav		return ("STALE_FAILED");
177219974Smav	case G_RAID_DISK_S_SPARE:
178219974Smav		return ("SPARE");
179219974Smav	case G_RAID_DISK_S_STALE:
180219974Smav		return ("STALE");
181219974Smav	case G_RAID_DISK_S_ACTIVE:
182219974Smav		return ("ACTIVE");
183219974Smav	default:
184219974Smav		return ("INVALID");
185219974Smav	}
186219974Smav}
187219974Smav
188219974Smavstatic const char *
189219974Smavg_raid_disk_event2str(int event)
190219974Smav{
191219974Smav
192219974Smav	switch (event) {
193219974Smav	case G_RAID_DISK_E_DISCONNECTED:
194219974Smav		return ("DISCONNECTED");
195219974Smav	default:
196219974Smav		return ("INVALID");
197219974Smav	}
198219974Smav}
199219974Smav
200219974Smavconst char *
201219974Smavg_raid_subdisk_state2str(int state)
202219974Smav{
203219974Smav
204219974Smav	switch (state) {
205219974Smav	case G_RAID_SUBDISK_S_NONE:
206219974Smav		return ("NONE");
207219974Smav	case G_RAID_SUBDISK_S_FAILED:
208219974Smav		return ("FAILED");
209219974Smav	case G_RAID_SUBDISK_S_NEW:
210219974Smav		return ("NEW");
211219974Smav	case G_RAID_SUBDISK_S_REBUILD:
212219974Smav		return ("REBUILD");
213219974Smav	case G_RAID_SUBDISK_S_UNINITIALIZED:
214219974Smav		return ("UNINITIALIZED");
215219974Smav	case G_RAID_SUBDISK_S_STALE:
216219974Smav		return ("STALE");
217219974Smav	case G_RAID_SUBDISK_S_RESYNC:
218219974Smav		return ("RESYNC");
219219974Smav	case G_RAID_SUBDISK_S_ACTIVE:
220219974Smav		return ("ACTIVE");
221219974Smav	default:
222219974Smav		return ("INVALID");
223219974Smav	}
224219974Smav}
225219974Smav
226219974Smavstatic const char *
227219974Smavg_raid_subdisk_event2str(int event)
228219974Smav{
229219974Smav
230219974Smav	switch (event) {
231219974Smav	case G_RAID_SUBDISK_E_NEW:
232219974Smav		return ("NEW");
233239175Smav	case G_RAID_SUBDISK_E_FAILED:
234239175Smav		return ("FAILED");
235219974Smav	case G_RAID_SUBDISK_E_DISCONNECTED:
236219974Smav		return ("DISCONNECTED");
237219974Smav	default:
238219974Smav		return ("INVALID");
239219974Smav	}
240219974Smav}
241219974Smav
242219974Smavconst char *
243219974Smavg_raid_volume_state2str(int state)
244219974Smav{
245219974Smav
246219974Smav	switch (state) {
247219974Smav	case G_RAID_VOLUME_S_STARTING:
248219974Smav		return ("STARTING");
249219974Smav	case G_RAID_VOLUME_S_BROKEN:
250219974Smav		return ("BROKEN");
251219974Smav	case G_RAID_VOLUME_S_DEGRADED:
252219974Smav		return ("DEGRADED");
253219974Smav	case G_RAID_VOLUME_S_SUBOPTIMAL:
254219974Smav		return ("SUBOPTIMAL");
255219974Smav	case G_RAID_VOLUME_S_OPTIMAL:
256219974Smav		return ("OPTIMAL");
257219974Smav	case G_RAID_VOLUME_S_UNSUPPORTED:
258219974Smav		return ("UNSUPPORTED");
259219974Smav	case G_RAID_VOLUME_S_STOPPED:
260219974Smav		return ("STOPPED");
261219974Smav	default:
262219974Smav		return ("INVALID");
263219974Smav	}
264219974Smav}
265219974Smav
266219974Smavstatic const char *
267219974Smavg_raid_volume_event2str(int event)
268219974Smav{
269219974Smav
270219974Smav	switch (event) {
271219974Smav	case G_RAID_VOLUME_E_UP:
272219974Smav		return ("UP");
273219974Smav	case G_RAID_VOLUME_E_DOWN:
274219974Smav		return ("DOWN");
275219974Smav	case G_RAID_VOLUME_E_START:
276219974Smav		return ("START");
277219974Smav	case G_RAID_VOLUME_E_STARTMD:
278219974Smav		return ("STARTMD");
279219974Smav	default:
280219974Smav		return ("INVALID");
281219974Smav	}
282219974Smav}
283219974Smav
284219974Smavconst char *
285219974Smavg_raid_volume_level2str(int level, int qual)
286219974Smav{
287219974Smav
288219974Smav	switch (level) {
289219974Smav	case G_RAID_VOLUME_RL_RAID0:
290219974Smav		return ("RAID0");
291219974Smav	case G_RAID_VOLUME_RL_RAID1:
292219974Smav		return ("RAID1");
293219974Smav	case G_RAID_VOLUME_RL_RAID3:
294234603Smav		if (qual == G_RAID_VOLUME_RLQ_R3P0)
295234603Smav			return ("RAID3-P0");
296234603Smav		if (qual == G_RAID_VOLUME_RLQ_R3PN)
297234603Smav			return ("RAID3-PN");
298219974Smav		return ("RAID3");
299219974Smav	case G_RAID_VOLUME_RL_RAID4:
300234603Smav		if (qual == G_RAID_VOLUME_RLQ_R4P0)
301234610Smav			return ("RAID4-P0");
302234603Smav		if (qual == G_RAID_VOLUME_RLQ_R4PN)
303234610Smav			return ("RAID4-PN");
304219974Smav		return ("RAID4");
305219974Smav	case G_RAID_VOLUME_RL_RAID5:
306234458Smav		if (qual == G_RAID_VOLUME_RLQ_R5RA)
307234603Smav			return ("RAID5-RA");
308234458Smav		if (qual == G_RAID_VOLUME_RLQ_R5RS)
309234603Smav			return ("RAID5-RS");
310234458Smav		if (qual == G_RAID_VOLUME_RLQ_R5LA)
311234603Smav			return ("RAID5-LA");
312234458Smav		if (qual == G_RAID_VOLUME_RLQ_R5LS)
313234603Smav			return ("RAID5-LS");
314219974Smav		return ("RAID5");
315219974Smav	case G_RAID_VOLUME_RL_RAID6:
316234603Smav		if (qual == G_RAID_VOLUME_RLQ_R6RA)
317234603Smav			return ("RAID6-RA");
318234603Smav		if (qual == G_RAID_VOLUME_RLQ_R6RS)
319234603Smav			return ("RAID6-RS");
320234603Smav		if (qual == G_RAID_VOLUME_RLQ_R6LA)
321234603Smav			return ("RAID6-LA");
322234603Smav		if (qual == G_RAID_VOLUME_RLQ_R6LS)
323234603Smav			return ("RAID6-LS");
324219974Smav		return ("RAID6");
325234603Smav	case G_RAID_VOLUME_RL_RAIDMDF:
326234603Smav		if (qual == G_RAID_VOLUME_RLQ_RMDFRA)
327234603Smav			return ("RAIDMDF-RA");
328234603Smav		if (qual == G_RAID_VOLUME_RLQ_RMDFRS)
329234603Smav			return ("RAIDMDF-RS");
330234603Smav		if (qual == G_RAID_VOLUME_RLQ_RMDFLA)
331234603Smav			return ("RAIDMDF-LA");
332234603Smav		if (qual == G_RAID_VOLUME_RLQ_RMDFLS)
333234603Smav			return ("RAIDMDF-LS");
334234603Smav		return ("RAIDMDF");
335219974Smav	case G_RAID_VOLUME_RL_RAID1E:
336234603Smav		if (qual == G_RAID_VOLUME_RLQ_R1EA)
337234603Smav			return ("RAID1E-A");
338234603Smav		if (qual == G_RAID_VOLUME_RLQ_R1EO)
339234603Smav			return ("RAID1E-O");
340219974Smav		return ("RAID1E");
341219974Smav	case G_RAID_VOLUME_RL_SINGLE:
342219974Smav		return ("SINGLE");
343219974Smav	case G_RAID_VOLUME_RL_CONCAT:
344219974Smav		return ("CONCAT");
345219974Smav	case G_RAID_VOLUME_RL_RAID5E:
346234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5ERA)
347234603Smav			return ("RAID5E-RA");
348234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5ERS)
349234603Smav			return ("RAID5E-RS");
350234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5ELA)
351234603Smav			return ("RAID5E-LA");
352234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5ELS)
353234603Smav			return ("RAID5E-LS");
354219974Smav		return ("RAID5E");
355219974Smav	case G_RAID_VOLUME_RL_RAID5EE:
356234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5EERA)
357234603Smav			return ("RAID5EE-RA");
358234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5EERS)
359234603Smav			return ("RAID5EE-RS");
360234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5EELA)
361234603Smav			return ("RAID5EE-LA");
362234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5EELS)
363234603Smav			return ("RAID5EE-LS");
364219974Smav		return ("RAID5EE");
365234603Smav	case G_RAID_VOLUME_RL_RAID5R:
366234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5RRA)
367234603Smav			return ("RAID5R-RA");
368234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5RRS)
369234603Smav			return ("RAID5R-RS");
370234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5RLA)
371234603Smav			return ("RAID5R-LA");
372234603Smav		if (qual == G_RAID_VOLUME_RLQ_R5RLS)
373234603Smav			return ("RAID5R-LS");
374234603Smav		return ("RAID5E");
375219974Smav	default:
376219974Smav		return ("UNKNOWN");
377219974Smav	}
378219974Smav}
379219974Smav
380219974Smavint
381219974Smavg_raid_volume_str2level(const char *str, int *level, int *qual)
382219974Smav{
383219974Smav
384219974Smav	*level = G_RAID_VOLUME_RL_UNKNOWN;
385219974Smav	*qual = G_RAID_VOLUME_RLQ_NONE;
386219974Smav	if (strcasecmp(str, "RAID0") == 0)
387219974Smav		*level = G_RAID_VOLUME_RL_RAID0;
388219974Smav	else if (strcasecmp(str, "RAID1") == 0)
389219974Smav		*level = G_RAID_VOLUME_RL_RAID1;
390234603Smav	else if (strcasecmp(str, "RAID3-P0") == 0) {
391219974Smav		*level = G_RAID_VOLUME_RL_RAID3;
392234603Smav		*qual = G_RAID_VOLUME_RLQ_R3P0;
393234993Smav	} else if (strcasecmp(str, "RAID3-PN") == 0 ||
394234603Smav		   strcasecmp(str, "RAID3") == 0) {
395234603Smav		*level = G_RAID_VOLUME_RL_RAID3;
396234993Smav		*qual = G_RAID_VOLUME_RLQ_R3PN;
397234603Smav	} else if (strcasecmp(str, "RAID4-P0") == 0) {
398219974Smav		*level = G_RAID_VOLUME_RL_RAID4;
399234603Smav		*qual = G_RAID_VOLUME_RLQ_R4P0;
400234993Smav	} else if (strcasecmp(str, "RAID4-PN") == 0 ||
401234603Smav		   strcasecmp(str, "RAID4") == 0) {
402234603Smav		*level = G_RAID_VOLUME_RL_RAID4;
403234993Smav		*qual = G_RAID_VOLUME_RLQ_R4PN;
404234603Smav	} else if (strcasecmp(str, "RAID5-RA") == 0) {
405219974Smav		*level = G_RAID_VOLUME_RL_RAID5;
406234458Smav		*qual = G_RAID_VOLUME_RLQ_R5RA;
407234603Smav	} else if (strcasecmp(str, "RAID5-RS") == 0) {
408234458Smav		*level = G_RAID_VOLUME_RL_RAID5;
409234458Smav		*qual = G_RAID_VOLUME_RLQ_R5RS;
410234458Smav	} else if (strcasecmp(str, "RAID5") == 0 ||
411234603Smav		   strcasecmp(str, "RAID5-LA") == 0) {
412234458Smav		*level = G_RAID_VOLUME_RL_RAID5;
413234458Smav		*qual = G_RAID_VOLUME_RLQ_R5LA;
414234603Smav	} else if (strcasecmp(str, "RAID5-LS") == 0) {
415234458Smav		*level = G_RAID_VOLUME_RL_RAID5;
416234458Smav		*qual = G_RAID_VOLUME_RLQ_R5LS;
417234603Smav	} else if (strcasecmp(str, "RAID6-RA") == 0) {
418219974Smav		*level = G_RAID_VOLUME_RL_RAID6;
419234603Smav		*qual = G_RAID_VOLUME_RLQ_R6RA;
420234603Smav	} else if (strcasecmp(str, "RAID6-RS") == 0) {
421234603Smav		*level = G_RAID_VOLUME_RL_RAID6;
422234603Smav		*qual = G_RAID_VOLUME_RLQ_R6RS;
423234603Smav	} else if (strcasecmp(str, "RAID6") == 0 ||
424234603Smav		   strcasecmp(str, "RAID6-LA") == 0) {
425234603Smav		*level = G_RAID_VOLUME_RL_RAID6;
426234603Smav		*qual = G_RAID_VOLUME_RLQ_R6LA;
427234603Smav	} else if (strcasecmp(str, "RAID6-LS") == 0) {
428234603Smav		*level = G_RAID_VOLUME_RL_RAID6;
429234603Smav		*qual = G_RAID_VOLUME_RLQ_R6LS;
430234603Smav	} else if (strcasecmp(str, "RAIDMDF-RA") == 0) {
431234603Smav		*level = G_RAID_VOLUME_RL_RAIDMDF;
432234603Smav		*qual = G_RAID_VOLUME_RLQ_RMDFRA;
433234603Smav	} else if (strcasecmp(str, "RAIDMDF-RS") == 0) {
434234603Smav		*level = G_RAID_VOLUME_RL_RAIDMDF;
435234603Smav		*qual = G_RAID_VOLUME_RLQ_RMDFRS;
436234603Smav	} else if (strcasecmp(str, "RAIDMDF") == 0 ||
437234603Smav		   strcasecmp(str, "RAIDMDF-LA") == 0) {
438234603Smav		*level = G_RAID_VOLUME_RL_RAIDMDF;
439234603Smav		*qual = G_RAID_VOLUME_RLQ_RMDFLA;
440234603Smav	} else if (strcasecmp(str, "RAIDMDF-LS") == 0) {
441234603Smav		*level = G_RAID_VOLUME_RL_RAIDMDF;
442234603Smav		*qual = G_RAID_VOLUME_RLQ_RMDFLS;
443234603Smav	} else if (strcasecmp(str, "RAID10") == 0 ||
444234603Smav		   strcasecmp(str, "RAID1E") == 0 ||
445234603Smav		   strcasecmp(str, "RAID1E-A") == 0) {
446219974Smav		*level = G_RAID_VOLUME_RL_RAID1E;
447234603Smav		*qual = G_RAID_VOLUME_RLQ_R1EA;
448234603Smav	} else if (strcasecmp(str, "RAID1E-O") == 0) {
449234603Smav		*level = G_RAID_VOLUME_RL_RAID1E;
450234603Smav		*qual = G_RAID_VOLUME_RLQ_R1EO;
451234603Smav	} else if (strcasecmp(str, "SINGLE") == 0)
452219974Smav		*level = G_RAID_VOLUME_RL_SINGLE;
453219974Smav	else if (strcasecmp(str, "CONCAT") == 0)
454219974Smav		*level = G_RAID_VOLUME_RL_CONCAT;
455234603Smav	else if (strcasecmp(str, "RAID5E-RA") == 0) {
456219974Smav		*level = G_RAID_VOLUME_RL_RAID5E;
457234603Smav		*qual = G_RAID_VOLUME_RLQ_R5ERA;
458234603Smav	} else if (strcasecmp(str, "RAID5E-RS") == 0) {
459234603Smav		*level = G_RAID_VOLUME_RL_RAID5E;
460234603Smav		*qual = G_RAID_VOLUME_RLQ_R5ERS;
461234603Smav	} else if (strcasecmp(str, "RAID5E") == 0 ||
462234603Smav		   strcasecmp(str, "RAID5E-LA") == 0) {
463234603Smav		*level = G_RAID_VOLUME_RL_RAID5E;
464234603Smav		*qual = G_RAID_VOLUME_RLQ_R5ELA;
465234603Smav	} else if (strcasecmp(str, "RAID5E-LS") == 0) {
466234603Smav		*level = G_RAID_VOLUME_RL_RAID5E;
467234603Smav		*qual = G_RAID_VOLUME_RLQ_R5ELS;
468234603Smav	} else if (strcasecmp(str, "RAID5EE-RA") == 0) {
469219974Smav		*level = G_RAID_VOLUME_RL_RAID5EE;
470234603Smav		*qual = G_RAID_VOLUME_RLQ_R5EERA;
471234603Smav	} else if (strcasecmp(str, "RAID5EE-RS") == 0) {
472234603Smav		*level = G_RAID_VOLUME_RL_RAID5EE;
473234603Smav		*qual = G_RAID_VOLUME_RLQ_R5EERS;
474234603Smav	} else if (strcasecmp(str, "RAID5EE") == 0 ||
475234603Smav		   strcasecmp(str, "RAID5EE-LA") == 0) {
476234603Smav		*level = G_RAID_VOLUME_RL_RAID5EE;
477234603Smav		*qual = G_RAID_VOLUME_RLQ_R5EELA;
478234603Smav	} else if (strcasecmp(str, "RAID5EE-LS") == 0) {
479234603Smav		*level = G_RAID_VOLUME_RL_RAID5EE;
480234603Smav		*qual = G_RAID_VOLUME_RLQ_R5EELS;
481234603Smav	} else if (strcasecmp(str, "RAID5R-RA") == 0) {
482234603Smav		*level = G_RAID_VOLUME_RL_RAID5R;
483234603Smav		*qual = G_RAID_VOLUME_RLQ_R5RRA;
484234603Smav	} else if (strcasecmp(str, "RAID5R-RS") == 0) {
485234603Smav		*level = G_RAID_VOLUME_RL_RAID5R;
486234603Smav		*qual = G_RAID_VOLUME_RLQ_R5RRS;
487234603Smav	} else if (strcasecmp(str, "RAID5R") == 0 ||
488234603Smav		   strcasecmp(str, "RAID5R-LA") == 0) {
489234603Smav		*level = G_RAID_VOLUME_RL_RAID5R;
490234603Smav		*qual = G_RAID_VOLUME_RLQ_R5RLA;
491234603Smav	} else if (strcasecmp(str, "RAID5R-LS") == 0) {
492234603Smav		*level = G_RAID_VOLUME_RL_RAID5R;
493234603Smav		*qual = G_RAID_VOLUME_RLQ_R5RLS;
494234603Smav	} else
495219974Smav		return (-1);
496219974Smav	return (0);
497219974Smav}
498219974Smav
499219974Smavconst char *
500219974Smavg_raid_get_diskname(struct g_raid_disk *disk)
501219974Smav{
502219974Smav
503219974Smav	if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
504219974Smav		return ("[unknown]");
505219974Smav	return (disk->d_consumer->provider->name);
506219974Smav}
507219974Smav
508219974Smavvoid
509242323Smavg_raid_get_disk_info(struct g_raid_disk *disk)
510242323Smav{
511242323Smav	struct g_consumer *cp = disk->d_consumer;
512242323Smav	int error, len;
513242323Smav
514242323Smav	/* Read kernel dumping information. */
515242323Smav	disk->d_kd.offset = 0;
516242323Smav	disk->d_kd.length = OFF_MAX;
517242323Smav	len = sizeof(disk->d_kd);
518242323Smav	error = g_io_getattr("GEOM::kerneldump", cp, &len, &disk->d_kd);
519242323Smav	if (error)
520242323Smav		disk->d_kd.di.dumper = NULL;
521242323Smav	if (disk->d_kd.di.dumper == NULL)
522242323Smav		G_RAID_DEBUG1(2, disk->d_softc,
523242323Smav		    "Dumping not supported by %s: %d.",
524242323Smav		    cp->provider->name, error);
525242323Smav
526242323Smav	/* Read BIO_DELETE support. */
527242323Smav	error = g_getattr("GEOM::candelete", cp, &disk->d_candelete);
528242323Smav	if (error)
529242323Smav		disk->d_candelete = 0;
530242323Smav	if (!disk->d_candelete)
531242323Smav		G_RAID_DEBUG1(2, disk->d_softc,
532242323Smav		    "BIO_DELETE not supported by %s: %d.",
533242323Smav		    cp->provider->name, error);
534242323Smav}
535242323Smav
536242323Smavvoid
537219974Smavg_raid_report_disk_state(struct g_raid_disk *disk)
538219974Smav{
539219974Smav	struct g_raid_subdisk *sd;
540219974Smav	int len, state;
541219974Smav	uint32_t s;
542219974Smav
543219974Smav	if (disk->d_consumer == NULL)
544219974Smav		return;
545245326Smav	if (disk->d_state == G_RAID_DISK_S_DISABLED) {
546245363Smav		s = G_STATE_ACTIVE; /* XXX */
547245326Smav	} else if (disk->d_state == G_RAID_DISK_S_FAILED ||
548219974Smav	    disk->d_state == G_RAID_DISK_S_STALE_FAILED) {
549219974Smav		s = G_STATE_FAILED;
550219974Smav	} else {
551219974Smav		state = G_RAID_SUBDISK_S_ACTIVE;
552219974Smav		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
553219974Smav			if (sd->sd_state < state)
554219974Smav				state = sd->sd_state;
555219974Smav		}
556219974Smav		if (state == G_RAID_SUBDISK_S_FAILED)
557219974Smav			s = G_STATE_FAILED;
558219974Smav		else if (state == G_RAID_SUBDISK_S_NEW ||
559219974Smav		    state == G_RAID_SUBDISK_S_REBUILD)
560219974Smav			s = G_STATE_REBUILD;
561219974Smav		else if (state == G_RAID_SUBDISK_S_STALE ||
562219974Smav		    state == G_RAID_SUBDISK_S_RESYNC)
563219974Smav			s = G_STATE_RESYNC;
564219974Smav		else
565219974Smav			s = G_STATE_ACTIVE;
566219974Smav	}
567219974Smav	len = sizeof(s);
568219974Smav	g_io_getattr("GEOM::setstate", disk->d_consumer, &len, &s);
569219974Smav	G_RAID_DEBUG1(2, disk->d_softc, "Disk %s state reported as %d.",
570219974Smav	    g_raid_get_diskname(disk), s);
571219974Smav}
572219974Smav
573219974Smavvoid
574219974Smavg_raid_change_disk_state(struct g_raid_disk *disk, int state)
575219974Smav{
576219974Smav
577219974Smav	G_RAID_DEBUG1(0, disk->d_softc, "Disk %s state changed from %s to %s.",
578219974Smav	    g_raid_get_diskname(disk),
579219974Smav	    g_raid_disk_state2str(disk->d_state),
580219974Smav	    g_raid_disk_state2str(state));
581219974Smav	disk->d_state = state;
582219974Smav	g_raid_report_disk_state(disk);
583219974Smav}
584219974Smav
585219974Smavvoid
586219974Smavg_raid_change_subdisk_state(struct g_raid_subdisk *sd, int state)
587219974Smav{
588219974Smav
589219974Smav	G_RAID_DEBUG1(0, sd->sd_softc,
590219974Smav	    "Subdisk %s:%d-%s state changed from %s to %s.",
591219974Smav	    sd->sd_volume->v_name, sd->sd_pos,
592219974Smav	    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]",
593219974Smav	    g_raid_subdisk_state2str(sd->sd_state),
594219974Smav	    g_raid_subdisk_state2str(state));
595219974Smav	sd->sd_state = state;
596219974Smav	if (sd->sd_disk)
597219974Smav		g_raid_report_disk_state(sd->sd_disk);
598219974Smav}
599219974Smav
600219974Smavvoid
601219974Smavg_raid_change_volume_state(struct g_raid_volume *vol, int state)
602219974Smav{
603219974Smav
604219974Smav	G_RAID_DEBUG1(0, vol->v_softc,
605219974Smav	    "Volume %s state changed from %s to %s.",
606219974Smav	    vol->v_name,
607219974Smav	    g_raid_volume_state2str(vol->v_state),
608219974Smav	    g_raid_volume_state2str(state));
609219974Smav	vol->v_state = state;
610219974Smav}
611219974Smav
612219974Smav/*
613219974Smav * --- Events handling functions ---
614219974Smav * Events in geom_raid are used to maintain subdisks and volumes status
615219974Smav * from one thread to simplify locking.
616219974Smav */
617219974Smavstatic void
618219974Smavg_raid_event_free(struct g_raid_event *ep)
619219974Smav{
620219974Smav
621219974Smav	free(ep, M_RAID);
622219974Smav}
623219974Smav
624219974Smavint
625219974Smavg_raid_event_send(void *arg, int event, int flags)
626219974Smav{
627219974Smav	struct g_raid_softc *sc;
628219974Smav	struct g_raid_event *ep;
629219974Smav	int error;
630219974Smav
631219974Smav	if ((flags & G_RAID_EVENT_VOLUME) != 0) {
632219974Smav		sc = ((struct g_raid_volume *)arg)->v_softc;
633219974Smav	} else if ((flags & G_RAID_EVENT_DISK) != 0) {
634219974Smav		sc = ((struct g_raid_disk *)arg)->d_softc;
635219974Smav	} else if ((flags & G_RAID_EVENT_SUBDISK) != 0) {
636219974Smav		sc = ((struct g_raid_subdisk *)arg)->sd_softc;
637219974Smav	} else {
638219974Smav		sc = arg;
639219974Smav	}
640219974Smav	ep = malloc(sizeof(*ep), M_RAID,
641219974Smav	    sx_xlocked(&sc->sc_lock) ? M_WAITOK : M_NOWAIT);
642219974Smav	if (ep == NULL)
643219974Smav		return (ENOMEM);
644219974Smav	ep->e_tgt = arg;
645219974Smav	ep->e_event = event;
646219974Smav	ep->e_flags = flags;
647219974Smav	ep->e_error = 0;
648219974Smav	G_RAID_DEBUG1(4, sc, "Sending event %p. Waking up %p.", ep, sc);
649219974Smav	mtx_lock(&sc->sc_queue_mtx);
650219974Smav	TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
651219974Smav	mtx_unlock(&sc->sc_queue_mtx);
652219974Smav	wakeup(sc);
653219974Smav
654219974Smav	if ((flags & G_RAID_EVENT_WAIT) == 0)
655219974Smav		return (0);
656219974Smav
657219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
658219974Smav	G_RAID_DEBUG1(4, sc, "Sleeping on %p.", ep);
659219974Smav	sx_xunlock(&sc->sc_lock);
660219974Smav	while ((ep->e_flags & G_RAID_EVENT_DONE) == 0) {
661219974Smav		mtx_lock(&sc->sc_queue_mtx);
662219974Smav		MSLEEP(error, ep, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:event",
663219974Smav		    hz * 5);
664219974Smav	}
665219974Smav	error = ep->e_error;
666219974Smav	g_raid_event_free(ep);
667219974Smav	sx_xlock(&sc->sc_lock);
668219974Smav	return (error);
669219974Smav}
670219974Smav
671219974Smavstatic void
672219974Smavg_raid_event_cancel(struct g_raid_softc *sc, void *tgt)
673219974Smav{
674219974Smav	struct g_raid_event *ep, *tmpep;
675219974Smav
676219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
677219974Smav
678219974Smav	mtx_lock(&sc->sc_queue_mtx);
679219974Smav	TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
680219974Smav		if (ep->e_tgt != tgt)
681219974Smav			continue;
682219974Smav		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
683219974Smav		if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0)
684219974Smav			g_raid_event_free(ep);
685219974Smav		else {
686219974Smav			ep->e_error = ECANCELED;
687219974Smav			wakeup(ep);
688219974Smav		}
689219974Smav	}
690219974Smav	mtx_unlock(&sc->sc_queue_mtx);
691219974Smav}
692219974Smav
693219974Smavstatic int
694219974Smavg_raid_event_check(struct g_raid_softc *sc, void *tgt)
695219974Smav{
696219974Smav	struct g_raid_event *ep;
697219974Smav	int	res = 0;
698219974Smav
699219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
700219974Smav
701219974Smav	mtx_lock(&sc->sc_queue_mtx);
702219974Smav	TAILQ_FOREACH(ep, &sc->sc_events, e_next) {
703219974Smav		if (ep->e_tgt != tgt)
704219974Smav			continue;
705219974Smav		res = 1;
706219974Smav		break;
707219974Smav	}
708219974Smav	mtx_unlock(&sc->sc_queue_mtx);
709219974Smav	return (res);
710219974Smav}
711219974Smav
712219974Smav/*
713219974Smav * Return the number of disks in given state.
714219974Smav * If state is equal to -1, count all connected disks.
715219974Smav */
716219974Smavu_int
717219974Smavg_raid_ndisks(struct g_raid_softc *sc, int state)
718219974Smav{
719219974Smav	struct g_raid_disk *disk;
720219974Smav	u_int n;
721219974Smav
722219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
723219974Smav
724219974Smav	n = 0;
725219974Smav	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
726219974Smav		if (disk->d_state == state || state == -1)
727219974Smav			n++;
728219974Smav	}
729219974Smav	return (n);
730219974Smav}
731219974Smav
732219974Smav/*
733219974Smav * Return the number of subdisks in given state.
734219974Smav * If state is equal to -1, count all connected disks.
735219974Smav */
736219974Smavu_int
737219974Smavg_raid_nsubdisks(struct g_raid_volume *vol, int state)
738219974Smav{
739219974Smav	struct g_raid_subdisk *subdisk;
740219974Smav	struct g_raid_softc *sc;
741219974Smav	u_int i, n ;
742219974Smav
743219974Smav	sc = vol->v_softc;
744219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
745219974Smav
746219974Smav	n = 0;
747219974Smav	for (i = 0; i < vol->v_disks_count; i++) {
748219974Smav		subdisk = &vol->v_subdisks[i];
749219974Smav		if ((state == -1 &&
750219974Smav		     subdisk->sd_state != G_RAID_SUBDISK_S_NONE) ||
751219974Smav		    subdisk->sd_state == state)
752219974Smav			n++;
753219974Smav	}
754219974Smav	return (n);
755219974Smav}
756219974Smav
757219974Smav/*
758219974Smav * Return the first subdisk in given state.
759219974Smav * If state is equal to -1, then the first connected disks.
760219974Smav */
761219974Smavstruct g_raid_subdisk *
762219974Smavg_raid_get_subdisk(struct g_raid_volume *vol, int state)
763219974Smav{
764219974Smav	struct g_raid_subdisk *sd;
765219974Smav	struct g_raid_softc *sc;
766219974Smav	u_int i;
767219974Smav
768219974Smav	sc = vol->v_softc;
769219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
770219974Smav
771219974Smav	for (i = 0; i < vol->v_disks_count; i++) {
772219974Smav		sd = &vol->v_subdisks[i];
773219974Smav		if ((state == -1 &&
774219974Smav		     sd->sd_state != G_RAID_SUBDISK_S_NONE) ||
775219974Smav		    sd->sd_state == state)
776219974Smav			return (sd);
777219974Smav	}
778219974Smav	return (NULL);
779219974Smav}
780219974Smav
781219974Smavstruct g_consumer *
782219974Smavg_raid_open_consumer(struct g_raid_softc *sc, const char *name)
783219974Smav{
784219974Smav	struct g_consumer *cp;
785219974Smav	struct g_provider *pp;
786219974Smav
787219974Smav	g_topology_assert();
788219974Smav
789219974Smav	if (strncmp(name, "/dev/", 5) == 0)
790219974Smav		name += 5;
791219974Smav	pp = g_provider_by_name(name);
792219974Smav	if (pp == NULL)
793219974Smav		return (NULL);
794219974Smav	cp = g_new_consumer(sc->sc_geom);
795260385Sscottl	cp->flags |= G_CF_DIRECT_RECEIVE;
796219974Smav	if (g_attach(cp, pp) != 0) {
797219974Smav		g_destroy_consumer(cp);
798219974Smav		return (NULL);
799219974Smav	}
800219974Smav	if (g_access(cp, 1, 1, 1) != 0) {
801219974Smav		g_detach(cp);
802219974Smav		g_destroy_consumer(cp);
803219974Smav		return (NULL);
804219974Smav	}
805219974Smav	return (cp);
806219974Smav}
807219974Smav
808219974Smavstatic u_int
809219974Smavg_raid_nrequests(struct g_raid_softc *sc, struct g_consumer *cp)
810219974Smav{
811219974Smav	struct bio *bp;
812219974Smav	u_int nreqs = 0;
813219974Smav
814219974Smav	mtx_lock(&sc->sc_queue_mtx);
815219974Smav	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
816219974Smav		if (bp->bio_from == cp)
817219974Smav			nreqs++;
818219974Smav	}
819219974Smav	mtx_unlock(&sc->sc_queue_mtx);
820219974Smav	return (nreqs);
821219974Smav}
822219974Smav
823219974Smavu_int
824219974Smavg_raid_nopens(struct g_raid_softc *sc)
825219974Smav{
826219974Smav	struct g_raid_volume *vol;
827219974Smav	u_int opens;
828219974Smav
829219974Smav	opens = 0;
830219974Smav	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
831219974Smav		if (vol->v_provider_open != 0)
832219974Smav			opens++;
833219974Smav	}
834219974Smav	return (opens);
835219974Smav}
836219974Smav
837219974Smavstatic int
838219974Smavg_raid_consumer_is_busy(struct g_raid_softc *sc, struct g_consumer *cp)
839219974Smav{
840219974Smav
841219974Smav	if (cp->index > 0) {
842219974Smav		G_RAID_DEBUG1(2, sc,
843219974Smav		    "I/O requests for %s exist, can't destroy it now.",
844219974Smav		    cp->provider->name);
845219974Smav		return (1);
846219974Smav	}
847219974Smav	if (g_raid_nrequests(sc, cp) > 0) {
848219974Smav		G_RAID_DEBUG1(2, sc,
849219974Smav		    "I/O requests for %s in queue, can't destroy it now.",
850219974Smav		    cp->provider->name);
851219974Smav		return (1);
852219974Smav	}
853219974Smav	return (0);
854219974Smav}
855219974Smav
856219974Smavstatic void
857219974Smavg_raid_destroy_consumer(void *arg, int flags __unused)
858219974Smav{
859219974Smav	struct g_consumer *cp;
860219974Smav
861219974Smav	g_topology_assert();
862219974Smav
863219974Smav	cp = arg;
864219974Smav	G_RAID_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
865219974Smav	g_detach(cp);
866219974Smav	g_destroy_consumer(cp);
867219974Smav}
868219974Smav
869219974Smavvoid
870219974Smavg_raid_kill_consumer(struct g_raid_softc *sc, struct g_consumer *cp)
871219974Smav{
872219974Smav	struct g_provider *pp;
873219974Smav	int retaste_wait;
874219974Smav
875219974Smav	g_topology_assert_not();
876219974Smav
877219974Smav	g_topology_lock();
878219974Smav	cp->private = NULL;
879219974Smav	if (g_raid_consumer_is_busy(sc, cp))
880219974Smav		goto out;
881219974Smav	pp = cp->provider;
882219974Smav	retaste_wait = 0;
883219974Smav	if (cp->acw == 1) {
884219974Smav		if ((pp->geom->flags & G_GEOM_WITHER) == 0)
885219974Smav			retaste_wait = 1;
886219974Smav	}
887219974Smav	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
888219974Smav		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
889219974Smav	if (retaste_wait) {
890219974Smav		/*
891219974Smav		 * After retaste event was send (inside g_access()), we can send
892219974Smav		 * event to detach and destroy consumer.
893219974Smav		 * A class, which has consumer to the given provider connected
894219974Smav		 * will not receive retaste event for the provider.
895219974Smav		 * This is the way how I ignore retaste events when I close
896219974Smav		 * consumers opened for write: I detach and destroy consumer
897219974Smav		 * after retaste event is sent.
898219974Smav		 */
899219974Smav		g_post_event(g_raid_destroy_consumer, cp, M_WAITOK, NULL);
900219974Smav		goto out;
901219974Smav	}
902219974Smav	G_RAID_DEBUG(1, "Consumer %s destroyed.", pp->name);
903219974Smav	g_detach(cp);
904219974Smav	g_destroy_consumer(cp);
905219974Smavout:
906219974Smav	g_topology_unlock();
907219974Smav}
908219974Smav
909219974Smavstatic void
910219974Smavg_raid_orphan(struct g_consumer *cp)
911219974Smav{
912219974Smav	struct g_raid_disk *disk;
913219974Smav
914219974Smav	g_topology_assert();
915219974Smav
916219974Smav	disk = cp->private;
917219974Smav	if (disk == NULL)
918219974Smav		return;
919219974Smav	g_raid_event_send(disk, G_RAID_DISK_E_DISCONNECTED,
920219974Smav	    G_RAID_EVENT_DISK);
921219974Smav}
922219974Smav
923242314Smavstatic void
924219974Smavg_raid_clean(struct g_raid_volume *vol, int acw)
925219974Smav{
926219974Smav	struct g_raid_softc *sc;
927219974Smav	int timeout;
928219974Smav
929219974Smav	sc = vol->v_softc;
930219974Smav	g_topology_assert_not();
931219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
932219974Smav
933219974Smav//	if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
934242314Smav//		return;
935219974Smav	if (!vol->v_dirty)
936242314Smav		return;
937219974Smav	if (vol->v_writes > 0)
938242314Smav		return;
939219974Smav	if (acw > 0 || (acw == -1 &&
940219974Smav	    vol->v_provider != NULL && vol->v_provider->acw > 0)) {
941219974Smav		timeout = g_raid_clean_time - (time_uptime - vol->v_last_write);
942242314Smav		if (!g_raid_shutdown && timeout > 0)
943242314Smav			return;
944219974Smav	}
945219974Smav	vol->v_dirty = 0;
946219974Smav	G_RAID_DEBUG1(1, sc, "Volume %s marked as clean.",
947219974Smav	    vol->v_name);
948219974Smav	g_raid_write_metadata(sc, vol, NULL, NULL);
949219974Smav}
950219974Smav
951219974Smavstatic void
952219974Smavg_raid_dirty(struct g_raid_volume *vol)
953219974Smav{
954219974Smav	struct g_raid_softc *sc;
955219974Smav
956219974Smav	sc = vol->v_softc;
957219974Smav	g_topology_assert_not();
958219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
959219974Smav
960219974Smav//	if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
961219974Smav//		return;
962219974Smav	vol->v_dirty = 1;
963219974Smav	G_RAID_DEBUG1(1, sc, "Volume %s marked as dirty.",
964219974Smav	    vol->v_name);
965219974Smav	g_raid_write_metadata(sc, vol, NULL, NULL);
966219974Smav}
967219974Smav
968219974Smavvoid
969219974Smavg_raid_tr_flush_common(struct g_raid_tr_object *tr, struct bio *bp)
970219974Smav{
971219974Smav	struct g_raid_softc *sc;
972219974Smav	struct g_raid_volume *vol;
973219974Smav	struct g_raid_subdisk *sd;
974219974Smav	struct bio_queue_head queue;
975219974Smav	struct bio *cbp;
976219974Smav	int i;
977219974Smav
978219974Smav	vol = tr->tro_volume;
979219974Smav	sc = vol->v_softc;
980219974Smav
981219974Smav	/*
982219974Smav	 * Allocate all bios before sending any request, so we can return
983219974Smav	 * ENOMEM in nice and clean way.
984219974Smav	 */
985219974Smav	bioq_init(&queue);
986219974Smav	for (i = 0; i < vol->v_disks_count; i++) {
987219974Smav		sd = &vol->v_subdisks[i];
988219974Smav		if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
989219974Smav		    sd->sd_state == G_RAID_SUBDISK_S_FAILED)
990219974Smav			continue;
991219974Smav		cbp = g_clone_bio(bp);
992219974Smav		if (cbp == NULL)
993219974Smav			goto failure;
994219974Smav		cbp->bio_caller1 = sd;
995219974Smav		bioq_insert_tail(&queue, cbp);
996219974Smav	}
997260385Sscottl	while ((cbp = bioq_takefirst(&queue)) != NULL) {
998219974Smav		sd = cbp->bio_caller1;
999219974Smav		cbp->bio_caller1 = NULL;
1000219974Smav		g_raid_subdisk_iostart(sd, cbp);
1001219974Smav	}
1002219974Smav	return;
1003219974Smavfailure:
1004260385Sscottl	while ((cbp = bioq_takefirst(&queue)) != NULL)
1005219974Smav		g_destroy_bio(cbp);
1006219974Smav	if (bp->bio_error == 0)
1007219974Smav		bp->bio_error = ENOMEM;
1008219974Smav	g_raid_iodone(bp, bp->bio_error);
1009219974Smav}
1010219974Smav
1011219974Smavstatic void
1012219974Smavg_raid_tr_kerneldump_common_done(struct bio *bp)
1013219974Smav{
1014219974Smav
1015219974Smav	bp->bio_flags |= BIO_DONE;
1016219974Smav}
1017219974Smav
1018219974Smavint
1019219974Smavg_raid_tr_kerneldump_common(struct g_raid_tr_object *tr,
1020219974Smav    void *virtual, vm_offset_t physical, off_t offset, size_t length)
1021219974Smav{
1022219974Smav	struct g_raid_softc *sc;
1023219974Smav	struct g_raid_volume *vol;
1024219974Smav	struct bio bp;
1025219974Smav
1026219974Smav	vol = tr->tro_volume;
1027219974Smav	sc = vol->v_softc;
1028219974Smav
1029219974Smav	bzero(&bp, sizeof(bp));
1030219974Smav	bp.bio_cmd = BIO_WRITE;
1031219974Smav	bp.bio_done = g_raid_tr_kerneldump_common_done;
1032219974Smav	bp.bio_attribute = NULL;
1033219974Smav	bp.bio_offset = offset;
1034219974Smav	bp.bio_length = length;
1035219974Smav	bp.bio_data = virtual;
1036219974Smav	bp.bio_to = vol->v_provider;
1037219974Smav
1038219974Smav	g_raid_start(&bp);
1039219974Smav	while (!(bp.bio_flags & BIO_DONE)) {
1040219974Smav		G_RAID_DEBUG1(4, sc, "Poll...");
1041219974Smav		g_raid_poll(sc);
1042219974Smav		DELAY(10);
1043219974Smav	}
1044219974Smav
1045219974Smav	return (bp.bio_error != 0 ? EIO : 0);
1046219974Smav}
1047219974Smav
1048219974Smavstatic int
1049219974Smavg_raid_dump(void *arg,
1050219974Smav    void *virtual, vm_offset_t physical, off_t offset, size_t length)
1051219974Smav{
1052219974Smav	struct g_raid_volume *vol;
1053219974Smav	int error;
1054219974Smav
1055219974Smav	vol = (struct g_raid_volume *)arg;
1056219974Smav	G_RAID_DEBUG1(3, vol->v_softc, "Dumping at off %llu len %llu.",
1057219974Smav	    (long long unsigned)offset, (long long unsigned)length);
1058219974Smav
1059219974Smav	error = G_RAID_TR_KERNELDUMP(vol->v_tr,
1060219974Smav	    virtual, physical, offset, length);
1061219974Smav	return (error);
1062219974Smav}
1063219974Smav
1064219974Smavstatic void
1065219974Smavg_raid_kerneldump(struct g_raid_softc *sc, struct bio *bp)
1066219974Smav{
1067219974Smav	struct g_kerneldump *gkd;
1068219974Smav	struct g_provider *pp;
1069219974Smav	struct g_raid_volume *vol;
1070219974Smav
1071219974Smav	gkd = (struct g_kerneldump*)bp->bio_data;
1072219974Smav	pp = bp->bio_to;
1073219974Smav	vol = pp->private;
1074219974Smav	g_trace(G_T_TOPOLOGY, "g_raid_kerneldump(%s, %jd, %jd)",
1075219974Smav		pp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
1076219974Smav	gkd->di.dumper = g_raid_dump;
1077219974Smav	gkd->di.priv = vol;
1078219974Smav	gkd->di.blocksize = vol->v_sectorsize;
1079219974Smav	gkd->di.maxiosize = DFLTPHYS;
1080219974Smav	gkd->di.mediaoffset = gkd->offset;
1081219974Smav	if ((gkd->offset + gkd->length) > vol->v_mediasize)
1082219974Smav		gkd->length = vol->v_mediasize - gkd->offset;
1083219974Smav	gkd->di.mediasize = gkd->length;
1084219974Smav	g_io_deliver(bp, 0);
1085219974Smav}
1086219974Smav
1087219974Smavstatic void
1088242323Smavg_raid_candelete(struct g_raid_softc *sc, struct bio *bp)
1089242323Smav{
1090242323Smav	struct g_provider *pp;
1091242323Smav	struct g_raid_volume *vol;
1092242323Smav	struct g_raid_subdisk *sd;
1093242323Smav	int *val;
1094242323Smav	int i;
1095242323Smav
1096242323Smav	val = (int *)bp->bio_data;
1097242323Smav	pp = bp->bio_to;
1098242323Smav	vol = pp->private;
1099242323Smav	*val = 0;
1100242323Smav	for (i = 0; i < vol->v_disks_count; i++) {
1101242323Smav		sd = &vol->v_subdisks[i];
1102242323Smav		if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
1103242323Smav			continue;
1104242323Smav		if (sd->sd_disk->d_candelete) {
1105242323Smav			*val = 1;
1106242323Smav			break;
1107242323Smav		}
1108242323Smav	}
1109242323Smav	g_io_deliver(bp, 0);
1110242323Smav}
1111242323Smav
1112242323Smavstatic void
1113219974Smavg_raid_start(struct bio *bp)
1114219974Smav{
1115219974Smav	struct g_raid_softc *sc;
1116219974Smav
1117219974Smav	sc = bp->bio_to->geom->softc;
1118219974Smav	/*
1119219974Smav	 * If sc == NULL or there are no valid disks, provider's error
1120219974Smav	 * should be set and g_raid_start() should not be called at all.
1121219974Smav	 */
1122219974Smav//	KASSERT(sc != NULL && sc->sc_state == G_RAID_VOLUME_S_RUNNING,
1123219974Smav//	    ("Provider's error should be set (error=%d)(mirror=%s).",
1124219974Smav//	    bp->bio_to->error, bp->bio_to->name));
1125219974Smav	G_RAID_LOGREQ(3, bp, "Request received.");
1126219974Smav
1127219974Smav	switch (bp->bio_cmd) {
1128219974Smav	case BIO_READ:
1129219974Smav	case BIO_WRITE:
1130219974Smav	case BIO_DELETE:
1131219974Smav	case BIO_FLUSH:
1132219974Smav		break;
1133219974Smav	case BIO_GETATTR:
1134242323Smav		if (!strcmp(bp->bio_attribute, "GEOM::candelete"))
1135242323Smav			g_raid_candelete(sc, bp);
1136242323Smav		else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
1137219974Smav			g_raid_kerneldump(sc, bp);
1138219974Smav		else
1139219974Smav			g_io_deliver(bp, EOPNOTSUPP);
1140219974Smav		return;
1141219974Smav	default:
1142219974Smav		g_io_deliver(bp, EOPNOTSUPP);
1143219974Smav		return;
1144219974Smav	}
1145219974Smav	mtx_lock(&sc->sc_queue_mtx);
1146219974Smav	bioq_disksort(&sc->sc_queue, bp);
1147219974Smav	mtx_unlock(&sc->sc_queue_mtx);
1148219974Smav	if (!dumping) {
1149219974Smav		G_RAID_DEBUG1(4, sc, "Waking up %p.", sc);
1150219974Smav		wakeup(sc);
1151219974Smav	}
1152219974Smav}
1153219974Smav
1154219974Smavstatic int
1155219974Smavg_raid_bio_overlaps(const struct bio *bp, off_t lstart, off_t len)
1156219974Smav{
1157219974Smav	/*
1158219974Smav	 * 5 cases:
1159219974Smav	 * (1) bp entirely below NO
1160219974Smav	 * (2) bp entirely above NO
1161219974Smav	 * (3) bp start below, but end in range YES
1162219974Smav	 * (4) bp entirely within YES
1163219974Smav	 * (5) bp starts within, ends above YES
1164219974Smav	 *
1165219974Smav	 * lock range 10-19 (offset 10 length 10)
1166219974Smav	 * (1) 1-5: first if kicks it out
1167219974Smav	 * (2) 30-35: second if kicks it out
1168219974Smav	 * (3) 5-15: passes both ifs
1169219974Smav	 * (4) 12-14: passes both ifs
1170219974Smav	 * (5) 19-20: passes both
1171219974Smav	 */
1172219974Smav	off_t lend = lstart + len - 1;
1173219974Smav	off_t bstart = bp->bio_offset;
1174219974Smav	off_t bend = bp->bio_offset + bp->bio_length - 1;
1175219974Smav
1176219974Smav	if (bend < lstart)
1177219974Smav		return (0);
1178219974Smav	if (lend < bstart)
1179219974Smav		return (0);
1180219974Smav	return (1);
1181219974Smav}
1182219974Smav
1183219974Smavstatic int
1184219974Smavg_raid_is_in_locked_range(struct g_raid_volume *vol, const struct bio *bp)
1185219974Smav{
1186219974Smav	struct g_raid_lock *lp;
1187219974Smav
1188219974Smav	sx_assert(&vol->v_softc->sc_lock, SX_LOCKED);
1189219974Smav
1190219974Smav	LIST_FOREACH(lp, &vol->v_locks, l_next) {
1191219974Smav		if (g_raid_bio_overlaps(bp, lp->l_offset, lp->l_length))
1192219974Smav			return (1);
1193219974Smav	}
1194219974Smav	return (0);
1195219974Smav}
1196219974Smav
1197219974Smavstatic void
1198219974Smavg_raid_start_request(struct bio *bp)
1199219974Smav{
1200219974Smav	struct g_raid_softc *sc;
1201219974Smav	struct g_raid_volume *vol;
1202219974Smav
1203219974Smav	sc = bp->bio_to->geom->softc;
1204219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
1205219974Smav	vol = bp->bio_to->private;
1206219974Smav
1207219974Smav	/*
1208219974Smav	 * Check to see if this item is in a locked range.  If so,
1209219974Smav	 * queue it to our locked queue and return.  We'll requeue
1210219974Smav	 * it when the range is unlocked.  Internal I/O for the
1211219974Smav	 * rebuild/rescan/recovery process is excluded from this
1212219974Smav	 * check so we can actually do the recovery.
1213219974Smav	 */
1214219974Smav	if (!(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL) &&
1215219974Smav	    g_raid_is_in_locked_range(vol, bp)) {
1216219974Smav		G_RAID_LOGREQ(3, bp, "Defer request.");
1217219974Smav		bioq_insert_tail(&vol->v_locked, bp);
1218219974Smav		return;
1219219974Smav	}
1220219974Smav
1221219974Smav	/*
1222219974Smav	 * If we're actually going to do the write/delete, then
1223219974Smav	 * update the idle stats for the volume.
1224219974Smav	 */
1225219974Smav	if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1226219974Smav		if (!vol->v_dirty)
1227219974Smav			g_raid_dirty(vol);
1228219974Smav		vol->v_writes++;
1229219974Smav	}
1230219974Smav
1231219974Smav	/*
1232219974Smav	 * Put request onto inflight queue, so we can check if new
1233219974Smav	 * synchronization requests don't collide with it.  Then tell
1234219974Smav	 * the transformation layer to start the I/O.
1235219974Smav	 */
1236219974Smav	bioq_insert_tail(&vol->v_inflight, bp);
1237219974Smav	G_RAID_LOGREQ(4, bp, "Request started");
1238219974Smav	G_RAID_TR_IOSTART(vol->v_tr, bp);
1239219974Smav}
1240219974Smav
1241219974Smavstatic void
1242219974Smavg_raid_finish_with_locked_ranges(struct g_raid_volume *vol, struct bio *bp)
1243219974Smav{
1244219974Smav	off_t off, len;
1245219974Smav	struct bio *nbp;
1246219974Smav	struct g_raid_lock *lp;
1247219974Smav
1248219974Smav	vol->v_pending_lock = 0;
1249219974Smav	LIST_FOREACH(lp, &vol->v_locks, l_next) {
1250219974Smav		if (lp->l_pending) {
1251219974Smav			off = lp->l_offset;
1252219974Smav			len = lp->l_length;
1253219974Smav			lp->l_pending = 0;
1254219974Smav			TAILQ_FOREACH(nbp, &vol->v_inflight.queue, bio_queue) {
1255219974Smav				if (g_raid_bio_overlaps(nbp, off, len))
1256219974Smav					lp->l_pending++;
1257219974Smav			}
1258219974Smav			if (lp->l_pending) {
1259219974Smav				vol->v_pending_lock = 1;
1260219974Smav				G_RAID_DEBUG1(4, vol->v_softc,
1261219974Smav				    "Deferred lock(%jd, %jd) has %d pending",
1262219974Smav				    (intmax_t)off, (intmax_t)(off + len),
1263219974Smav				    lp->l_pending);
1264219974Smav				continue;
1265219974Smav			}
1266219974Smav			G_RAID_DEBUG1(4, vol->v_softc,
1267219974Smav			    "Deferred lock of %jd to %jd completed",
1268219974Smav			    (intmax_t)off, (intmax_t)(off + len));
1269219974Smav			G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1270219974Smav		}
1271219974Smav	}
1272219974Smav}
1273219974Smav
1274219974Smavvoid
1275219974Smavg_raid_iodone(struct bio *bp, int error)
1276219974Smav{
1277219974Smav	struct g_raid_softc *sc;
1278219974Smav	struct g_raid_volume *vol;
1279219974Smav
1280219974Smav	sc = bp->bio_to->geom->softc;
1281219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
1282219974Smav	vol = bp->bio_to->private;
1283219974Smav	G_RAID_LOGREQ(3, bp, "Request done: %d.", error);
1284219974Smav
1285219974Smav	/* Update stats if we done write/delete. */
1286219974Smav	if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1287219974Smav		vol->v_writes--;
1288219974Smav		vol->v_last_write = time_uptime;
1289219974Smav	}
1290219974Smav
1291219974Smav	bioq_remove(&vol->v_inflight, bp);
1292219974Smav	if (vol->v_pending_lock && g_raid_is_in_locked_range(vol, bp))
1293219974Smav		g_raid_finish_with_locked_ranges(vol, bp);
1294219974Smav	getmicrouptime(&vol->v_last_done);
1295219974Smav	g_io_deliver(bp, error);
1296219974Smav}
1297219974Smav
1298219974Smavint
1299219974Smavg_raid_lock_range(struct g_raid_volume *vol, off_t off, off_t len,
1300219974Smav    struct bio *ignore, void *argp)
1301219974Smav{
1302219974Smav	struct g_raid_softc *sc;
1303219974Smav	struct g_raid_lock *lp;
1304219974Smav	struct bio *bp;
1305219974Smav
1306219974Smav	sc = vol->v_softc;
1307219974Smav	lp = malloc(sizeof(*lp), M_RAID, M_WAITOK | M_ZERO);
1308219974Smav	LIST_INSERT_HEAD(&vol->v_locks, lp, l_next);
1309219974Smav	lp->l_offset = off;
1310219974Smav	lp->l_length = len;
1311219974Smav	lp->l_callback_arg = argp;
1312219974Smav
1313219974Smav	lp->l_pending = 0;
1314219974Smav	TAILQ_FOREACH(bp, &vol->v_inflight.queue, bio_queue) {
1315219974Smav		if (bp != ignore && g_raid_bio_overlaps(bp, off, len))
1316219974Smav			lp->l_pending++;
1317219974Smav	}
1318219974Smav
1319219974Smav	/*
1320219974Smav	 * If there are any writes that are pending, we return EBUSY.  All
1321219974Smav	 * callers will have to wait until all pending writes clear.
1322219974Smav	 */
1323219974Smav	if (lp->l_pending > 0) {
1324219974Smav		vol->v_pending_lock = 1;
1325219974Smav		G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd deferred %d pend",
1326219974Smav		    (intmax_t)off, (intmax_t)(off+len), lp->l_pending);
1327219974Smav		return (EBUSY);
1328219974Smav	}
1329219974Smav	G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd",
1330219974Smav	    (intmax_t)off, (intmax_t)(off+len));
1331219974Smav	G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1332219974Smav	return (0);
1333219974Smav}
1334219974Smav
1335219974Smavint
1336219974Smavg_raid_unlock_range(struct g_raid_volume *vol, off_t off, off_t len)
1337219974Smav{
1338219974Smav	struct g_raid_lock *lp;
1339219974Smav	struct g_raid_softc *sc;
1340219974Smav	struct bio *bp;
1341219974Smav
1342219974Smav	sc = vol->v_softc;
1343219974Smav	LIST_FOREACH(lp, &vol->v_locks, l_next) {
1344219974Smav		if (lp->l_offset == off && lp->l_length == len) {
1345219974Smav			LIST_REMOVE(lp, l_next);
1346219974Smav			/* XXX
1347219974Smav			 * Right now we just put them all back on the queue
1348219974Smav			 * and hope for the best.  We hope this because any
1349219974Smav			 * locked ranges will go right back on this list
1350219974Smav			 * when the worker thread runs.
1351219974Smav			 * XXX
1352219974Smav			 */
1353219974Smav			G_RAID_DEBUG1(4, sc, "Unlocked %jd to %jd",
1354219974Smav			    (intmax_t)lp->l_offset,
1355219974Smav			    (intmax_t)(lp->l_offset+lp->l_length));
1356219974Smav			mtx_lock(&sc->sc_queue_mtx);
1357219974Smav			while ((bp = bioq_takefirst(&vol->v_locked)) != NULL)
1358219974Smav				bioq_disksort(&sc->sc_queue, bp);
1359219974Smav			mtx_unlock(&sc->sc_queue_mtx);
1360219974Smav			free(lp, M_RAID);
1361219974Smav			return (0);
1362219974Smav		}
1363219974Smav	}
1364219974Smav	return (EINVAL);
1365219974Smav}
1366219974Smav
1367219974Smavvoid
1368219974Smavg_raid_subdisk_iostart(struct g_raid_subdisk *sd, struct bio *bp)
1369219974Smav{
1370219974Smav	struct g_consumer *cp;
1371219974Smav	struct g_raid_disk *disk, *tdisk;
1372219974Smav
1373219974Smav	bp->bio_caller1 = sd;
1374219974Smav
1375219974Smav	/*
1376219974Smav	 * Make sure that the disk is present. Generally it is a task of
1377219974Smav	 * transformation layers to not send requests to absent disks, but
1378219974Smav	 * it is better to be safe and report situation then sorry.
1379219974Smav	 */
1380219974Smav	if (sd->sd_disk == NULL) {
1381219974Smav		G_RAID_LOGREQ(0, bp, "Warning! I/O request to an absent disk!");
1382219974Smavnodisk:
1383219974Smav		bp->bio_from = NULL;
1384219974Smav		bp->bio_to = NULL;
1385219974Smav		bp->bio_error = ENXIO;
1386219974Smav		g_raid_disk_done(bp);
1387219974Smav		return;
1388219974Smav	}
1389219974Smav	disk = sd->sd_disk;
1390219974Smav	if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
1391219974Smav	    disk->d_state != G_RAID_DISK_S_FAILED) {
1392219974Smav		G_RAID_LOGREQ(0, bp, "Warning! I/O request to a disk in a "
1393219974Smav		    "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
1394219974Smav		goto nodisk;
1395219974Smav	}
1396219974Smav
1397219974Smav	cp = disk->d_consumer;
1398219974Smav	bp->bio_from = cp;
1399219974Smav	bp->bio_to = cp->provider;
1400219974Smav	cp->index++;
1401219974Smav
1402219974Smav	/* Update average disks load. */
1403219974Smav	TAILQ_FOREACH(tdisk, &sd->sd_softc->sc_disks, d_next) {
1404219974Smav		if (tdisk->d_consumer == NULL)
1405219974Smav			tdisk->d_load = 0;
1406219974Smav		else
1407219974Smav			tdisk->d_load = (tdisk->d_consumer->index *
1408219974Smav			    G_RAID_SUBDISK_LOAD_SCALE + tdisk->d_load * 7) / 8;
1409219974Smav	}
1410219974Smav
1411219974Smav	disk->d_last_offset = bp->bio_offset + bp->bio_length;
1412219974Smav	if (dumping) {
1413219974Smav		G_RAID_LOGREQ(3, bp, "Sending dumping request.");
1414219974Smav		if (bp->bio_cmd == BIO_WRITE) {
1415219974Smav			bp->bio_error = g_raid_subdisk_kerneldump(sd,
1416219974Smav			    bp->bio_data, 0, bp->bio_offset, bp->bio_length);
1417219974Smav		} else
1418219974Smav			bp->bio_error = EOPNOTSUPP;
1419219974Smav		g_raid_disk_done(bp);
1420219974Smav	} else {
1421219974Smav		bp->bio_done = g_raid_disk_done;
1422219974Smav		bp->bio_offset += sd->sd_offset;
1423219974Smav		G_RAID_LOGREQ(3, bp, "Sending request.");
1424219974Smav		g_io_request(bp, cp);
1425219974Smav	}
1426219974Smav}
1427219974Smav
1428219974Smavint
1429219974Smavg_raid_subdisk_kerneldump(struct g_raid_subdisk *sd,
1430219974Smav    void *virtual, vm_offset_t physical, off_t offset, size_t length)
1431219974Smav{
1432219974Smav
1433219974Smav	if (sd->sd_disk == NULL)
1434219974Smav		return (ENXIO);
1435219974Smav	if (sd->sd_disk->d_kd.di.dumper == NULL)
1436219974Smav		return (EOPNOTSUPP);
1437219974Smav	return (dump_write(&sd->sd_disk->d_kd.di,
1438219974Smav	    virtual, physical,
1439219974Smav	    sd->sd_disk->d_kd.di.mediaoffset + sd->sd_offset + offset,
1440219974Smav	    length));
1441219974Smav}
1442219974Smav
1443219974Smavstatic void
1444219974Smavg_raid_disk_done(struct bio *bp)
1445219974Smav{
1446219974Smav	struct g_raid_softc *sc;
1447219974Smav	struct g_raid_subdisk *sd;
1448219974Smav
1449219974Smav	sd = bp->bio_caller1;
1450219974Smav	sc = sd->sd_softc;
1451219974Smav	mtx_lock(&sc->sc_queue_mtx);
1452219974Smav	bioq_disksort(&sc->sc_queue, bp);
1453219974Smav	mtx_unlock(&sc->sc_queue_mtx);
1454219974Smav	if (!dumping)
1455219974Smav		wakeup(sc);
1456219974Smav}
1457219974Smav
1458219974Smavstatic void
1459219974Smavg_raid_disk_done_request(struct bio *bp)
1460219974Smav{
1461219974Smav	struct g_raid_softc *sc;
1462219974Smav	struct g_raid_disk *disk;
1463219974Smav	struct g_raid_subdisk *sd;
1464219974Smav	struct g_raid_volume *vol;
1465219974Smav
1466219974Smav	g_topology_assert_not();
1467219974Smav
1468219974Smav	G_RAID_LOGREQ(3, bp, "Disk request done: %d.", bp->bio_error);
1469219974Smav	sd = bp->bio_caller1;
1470219974Smav	sc = sd->sd_softc;
1471219974Smav	vol = sd->sd_volume;
1472219974Smav	if (bp->bio_from != NULL) {
1473219974Smav		bp->bio_from->index--;
1474219974Smav		disk = bp->bio_from->private;
1475219974Smav		if (disk == NULL)
1476219974Smav			g_raid_kill_consumer(sc, bp->bio_from);
1477219974Smav	}
1478219974Smav	bp->bio_offset -= sd->sd_offset;
1479219974Smav
1480219974Smav	G_RAID_TR_IODONE(vol->v_tr, sd, bp);
1481219974Smav}
1482219974Smav
1483219974Smavstatic void
1484219974Smavg_raid_handle_event(struct g_raid_softc *sc, struct g_raid_event *ep)
1485219974Smav{
1486219974Smav
1487219974Smav	if ((ep->e_flags & G_RAID_EVENT_VOLUME) != 0)
1488219974Smav		ep->e_error = g_raid_update_volume(ep->e_tgt, ep->e_event);
1489219974Smav	else if ((ep->e_flags & G_RAID_EVENT_DISK) != 0)
1490219974Smav		ep->e_error = g_raid_update_disk(ep->e_tgt, ep->e_event);
1491219974Smav	else if ((ep->e_flags & G_RAID_EVENT_SUBDISK) != 0)
1492219974Smav		ep->e_error = g_raid_update_subdisk(ep->e_tgt, ep->e_event);
1493219974Smav	else
1494219974Smav		ep->e_error = g_raid_update_node(ep->e_tgt, ep->e_event);
1495219974Smav	if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0) {
1496219974Smav		KASSERT(ep->e_error == 0,
1497219974Smav		    ("Error cannot be handled."));
1498219974Smav		g_raid_event_free(ep);
1499219974Smav	} else {
1500219974Smav		ep->e_flags |= G_RAID_EVENT_DONE;
1501219974Smav		G_RAID_DEBUG1(4, sc, "Waking up %p.", ep);
1502219974Smav		mtx_lock(&sc->sc_queue_mtx);
1503219974Smav		wakeup(ep);
1504219974Smav		mtx_unlock(&sc->sc_queue_mtx);
1505219974Smav	}
1506219974Smav}
1507219974Smav
1508219974Smav/*
1509219974Smav * Worker thread.
1510219974Smav */
1511219974Smavstatic void
1512219974Smavg_raid_worker(void *arg)
1513219974Smav{
1514219974Smav	struct g_raid_softc *sc;
1515219974Smav	struct g_raid_event *ep;
1516219974Smav	struct g_raid_volume *vol;
1517219974Smav	struct bio *bp;
1518219974Smav	struct timeval now, t;
1519219974Smav	int timeout, rv;
1520219974Smav
1521219974Smav	sc = arg;
1522219974Smav	thread_lock(curthread);
1523219974Smav	sched_prio(curthread, PRIBIO);
1524219974Smav	thread_unlock(curthread);
1525219974Smav
1526219974Smav	sx_xlock(&sc->sc_lock);
1527219974Smav	for (;;) {
1528219974Smav		mtx_lock(&sc->sc_queue_mtx);
1529219974Smav		/*
1530219974Smav		 * First take a look at events.
1531219974Smav		 * This is important to handle events before any I/O requests.
1532219974Smav		 */
1533219974Smav		bp = NULL;
1534219974Smav		vol = NULL;
1535219974Smav		rv = 0;
1536219974Smav		ep = TAILQ_FIRST(&sc->sc_events);
1537219974Smav		if (ep != NULL)
1538219974Smav			TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1539219974Smav		else if ((bp = bioq_takefirst(&sc->sc_queue)) != NULL)
1540219974Smav			;
1541219974Smav		else {
1542219974Smav			getmicrouptime(&now);
1543219974Smav			t = now;
1544219974Smav			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1545219974Smav				if (bioq_first(&vol->v_inflight) == NULL &&
1546219974Smav				    vol->v_tr &&
1547219974Smav				    timevalcmp(&vol->v_last_done, &t, < ))
1548219974Smav					t = vol->v_last_done;
1549219974Smav			}
1550219974Smav			timevalsub(&t, &now);
1551219974Smav			timeout = g_raid_idle_threshold +
1552219974Smav			    t.tv_sec * 1000000 + t.tv_usec;
1553219974Smav			if (timeout > 0) {
1554219974Smav				/*
1555219974Smav				 * Two steps to avoid overflows at HZ=1000
1556219974Smav				 * and idle timeouts > 2.1s.  Some rounding
1557219974Smav				 * errors can occur, but they are < 1tick,
1558219974Smav				 * which is deemed to be close enough for
1559219974Smav				 * this purpose.
1560219974Smav				 */
1561219974Smav				int micpertic = 1000000 / hz;
1562219974Smav				timeout = (timeout + micpertic - 1) / micpertic;
1563219974Smav				sx_xunlock(&sc->sc_lock);
1564219974Smav				MSLEEP(rv, sc, &sc->sc_queue_mtx,
1565219974Smav				    PRIBIO | PDROP, "-", timeout);
1566219974Smav				sx_xlock(&sc->sc_lock);
1567219974Smav				goto process;
1568219974Smav			} else
1569219974Smav				rv = EWOULDBLOCK;
1570219974Smav		}
1571219974Smav		mtx_unlock(&sc->sc_queue_mtx);
1572219974Smavprocess:
1573219974Smav		if (ep != NULL) {
1574219974Smav			g_raid_handle_event(sc, ep);
1575219974Smav		} else if (bp != NULL) {
1576219974Smav			if (bp->bio_to != NULL &&
1577219974Smav			    bp->bio_to->geom == sc->sc_geom)
1578219974Smav				g_raid_start_request(bp);
1579219974Smav			else
1580219974Smav				g_raid_disk_done_request(bp);
1581219974Smav		} else if (rv == EWOULDBLOCK) {
1582219974Smav			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1583242314Smav				g_raid_clean(vol, -1);
1584219974Smav				if (bioq_first(&vol->v_inflight) == NULL &&
1585219974Smav				    vol->v_tr) {
1586219974Smav					t.tv_sec = g_raid_idle_threshold / 1000000;
1587219974Smav					t.tv_usec = g_raid_idle_threshold % 1000000;
1588219974Smav					timevaladd(&t, &vol->v_last_done);
1589219974Smav					getmicrouptime(&now);
1590219974Smav					if (timevalcmp(&t, &now, <= )) {
1591219974Smav						G_RAID_TR_IDLE(vol->v_tr);
1592219974Smav						vol->v_last_done = now;
1593219974Smav					}
1594219974Smav				}
1595219974Smav			}
1596219974Smav		}
1597219974Smav		if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1598219974Smav			g_raid_destroy_node(sc, 1);	/* May not return. */
1599219974Smav	}
1600219974Smav}
1601219974Smav
1602219974Smavstatic void
1603219974Smavg_raid_poll(struct g_raid_softc *sc)
1604219974Smav{
1605219974Smav	struct g_raid_event *ep;
1606219974Smav	struct bio *bp;
1607219974Smav
1608219974Smav	sx_xlock(&sc->sc_lock);
1609219974Smav	mtx_lock(&sc->sc_queue_mtx);
1610219974Smav	/*
1611219974Smav	 * First take a look at events.
1612219974Smav	 * This is important to handle events before any I/O requests.
1613219974Smav	 */
1614219974Smav	ep = TAILQ_FIRST(&sc->sc_events);
1615219974Smav	if (ep != NULL) {
1616219974Smav		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1617219974Smav		mtx_unlock(&sc->sc_queue_mtx);
1618219974Smav		g_raid_handle_event(sc, ep);
1619219974Smav		goto out;
1620219974Smav	}
1621219974Smav	bp = bioq_takefirst(&sc->sc_queue);
1622219974Smav	if (bp != NULL) {
1623219974Smav		mtx_unlock(&sc->sc_queue_mtx);
1624219974Smav		if (bp->bio_from == NULL ||
1625219974Smav		    bp->bio_from->geom != sc->sc_geom)
1626219974Smav			g_raid_start_request(bp);
1627219974Smav		else
1628219974Smav			g_raid_disk_done_request(bp);
1629219974Smav	}
1630219974Smavout:
1631219974Smav	sx_xunlock(&sc->sc_lock);
1632219974Smav}
1633219974Smav
1634219974Smavstatic void
1635219974Smavg_raid_launch_provider(struct g_raid_volume *vol)
1636219974Smav{
1637219974Smav	struct g_raid_disk *disk;
1638260385Sscottl	struct g_raid_subdisk *sd;
1639219974Smav	struct g_raid_softc *sc;
1640219974Smav	struct g_provider *pp;
1641219974Smav	char name[G_RAID_MAX_VOLUMENAME];
1642248068Ssbruno	char   announce_buf[80], buf1[32];
1643219974Smav	off_t off;
1644260385Sscottl	int i;
1645219974Smav
1646219974Smav	sc = vol->v_softc;
1647219974Smav	sx_assert(&sc->sc_lock, SX_LOCKED);
1648219974Smav
1649219974Smav	g_topology_lock();
1650219974Smav	/* Try to name provider with volume name. */
1651219974Smav	snprintf(name, sizeof(name), "raid/%s", vol->v_name);
1652219974Smav	if (g_raid_name_format == 0 || vol->v_name[0] == 0 ||
1653219974Smav	    g_provider_by_name(name) != NULL) {
1654219974Smav		/* Otherwise use sequential volume number. */
1655219974Smav		snprintf(name, sizeof(name), "raid/r%d", vol->v_global_id);
1656219974Smav	}
1657248068Ssbruno
1658248068Ssbruno	/*
1659248068Ssbruno	 * Create a /dev/ar%d that the old ataraid(4) stack once
1660248068Ssbruno	 * created as an alias for /dev/raid/r%d if requested.
1661248068Ssbruno	 * This helps going from stable/7 ataraid devices to newer
1662248068Ssbruno	 * FreeBSD releases. sbruno 07 MAY 2013
1663248068Ssbruno	 */
1664248068Ssbruno
1665248068Ssbruno        if (ar_legacy_aliases) {
1666248068Ssbruno		snprintf(announce_buf, sizeof(announce_buf),
1667248068Ssbruno                        "kern.devalias.%s", name);
1668248068Ssbruno                snprintf(buf1, sizeof(buf1),
1669248068Ssbruno                        "ar%d", vol->v_global_id);
1670248068Ssbruno                setenv(announce_buf, buf1);
1671248068Ssbruno        }
1672248068Ssbruno
1673219974Smav	pp = g_new_providerf(sc->sc_geom, "%s", name);
1674260385Sscottl	pp->flags |= G_PF_DIRECT_RECEIVE;
1675260385Sscottl	if (vol->v_tr->tro_class->trc_accept_unmapped) {
1676260385Sscottl		pp->flags |= G_PF_ACCEPT_UNMAPPED;
1677260385Sscottl		for (i = 0; i < vol->v_disks_count; i++) {
1678260385Sscottl			sd = &vol->v_subdisks[i];
1679260385Sscottl			if (sd->sd_state == G_RAID_SUBDISK_S_NONE)
1680260385Sscottl				continue;
1681260385Sscottl			if ((sd->sd_disk->d_consumer->provider->flags &
1682260385Sscottl			    G_PF_ACCEPT_UNMAPPED) == 0)
1683260385Sscottl				pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
1684260385Sscottl		}
1685260385Sscottl	}
1686219974Smav	pp->private = vol;
1687219974Smav	pp->mediasize = vol->v_mediasize;
1688219974Smav	pp->sectorsize = vol->v_sectorsize;
1689219974Smav	pp->stripesize = 0;
1690219974Smav	pp->stripeoffset = 0;
1691219974Smav	if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
1692219974Smav	    vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 ||
1693219974Smav	    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE ||
1694219974Smav	    vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT) {
1695219974Smav		if ((disk = vol->v_subdisks[0].sd_disk) != NULL &&
1696219974Smav		    disk->d_consumer != NULL &&
1697219974Smav		    disk->d_consumer->provider != NULL) {
1698219974Smav			pp->stripesize = disk->d_consumer->provider->stripesize;
1699219974Smav			off = disk->d_consumer->provider->stripeoffset;
1700219974Smav			pp->stripeoffset = off + vol->v_subdisks[0].sd_offset;
1701219974Smav			if (off > 0)
1702219974Smav				pp->stripeoffset %= off;
1703219974Smav		}
1704219974Smav		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3) {
1705219974Smav			pp->stripesize *= (vol->v_disks_count - 1);
1706219974Smav			pp->stripeoffset *= (vol->v_disks_count - 1);
1707219974Smav		}
1708219974Smav	} else
1709219974Smav		pp->stripesize = vol->v_strip_size;
1710219974Smav	vol->v_provider = pp;
1711219974Smav	g_error_provider(pp, 0);
1712219974Smav	g_topology_unlock();
1713219974Smav	G_RAID_DEBUG1(0, sc, "Provider %s for volume %s created.",
1714219974Smav	    pp->name, vol->v_name);
1715219974Smav}
1716219974Smav
1717219974Smavstatic void
1718219974Smavg_raid_destroy_provider(struct g_raid_volume *vol)
1719219974Smav{
1720219974Smav	struct g_raid_softc *sc;
1721219974Smav	struct g_provider *pp;
1722219974Smav	struct bio *bp, *tmp;
1723219974Smav
1724219974Smav	g_topology_assert_not();
1725219974Smav	sc = vol->v_softc;
1726219974Smav	pp = vol->v_provider;
1727219974Smav	KASSERT(pp != NULL, ("NULL provider (volume=%s).", vol->v_name));
1728219974Smav
1729219974Smav	g_topology_lock();
1730219974Smav	g_error_provider(pp, ENXIO);
1731219974Smav	mtx_lock(&sc->sc_queue_mtx);
1732219974Smav	TAILQ_FOREACH_SAFE(bp, &sc->sc_queue.queue, bio_queue, tmp) {
1733219974Smav		if (bp->bio_to != pp)
1734219974Smav			continue;
1735219974Smav		bioq_remove(&sc->sc_queue, bp);
1736219974Smav		g_io_deliver(bp, ENXIO);
1737219974Smav	}
1738219974Smav	mtx_unlock(&sc->sc_queue_mtx);
1739219974Smav	G_RAID_DEBUG1(0, sc, "Provider %s for volume %s destroyed.",
1740219974Smav	    pp->name, vol->v_name);
1741219974Smav	g_wither_provider(pp, ENXIO);
1742219974Smav	g_topology_unlock();
1743219974Smav	vol->v_provider = NULL;
1744219974Smav}
1745219974Smav
1746219974Smav/*
1747219974Smav * Update device state.
1748219974Smav */
1749219974Smavstatic int
1750219974Smavg_raid_update_volume(struct g_raid_volume *vol, u_int event)
1751219974Smav{
1752219974Smav	struct g_raid_softc *sc;
1753219974Smav
1754219974Smav	sc = vol->v_softc;
1755219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
1756219974Smav
1757219974Smav	G_RAID_DEBUG1(2, sc, "Event %s for volume %s.",
1758219974Smav	    g_raid_volume_event2str(event),
1759219974Smav	    vol->v_name);
1760219974Smav	switch (event) {
1761219974Smav	case G_RAID_VOLUME_E_DOWN:
1762219974Smav		if (vol->v_provider != NULL)
1763219974Smav			g_raid_destroy_provider(vol);
1764219974Smav		break;
1765219974Smav	case G_RAID_VOLUME_E_UP:
1766219974Smav		if (vol->v_provider == NULL)
1767219974Smav			g_raid_launch_provider(vol);
1768219974Smav		break;
1769219974Smav	case G_RAID_VOLUME_E_START:
1770219974Smav		if (vol->v_tr)
1771219974Smav			G_RAID_TR_START(vol->v_tr);
1772219974Smav		return (0);
1773219974Smav	default:
1774219974Smav		if (sc->sc_md)
1775219974Smav			G_RAID_MD_VOLUME_EVENT(sc->sc_md, vol, event);
1776219974Smav		return (0);
1777219974Smav	}
1778219974Smav
1779219974Smav	/* Manage root mount release. */
1780219974Smav	if (vol->v_starting) {
1781219974Smav		vol->v_starting = 0;
1782219974Smav		G_RAID_DEBUG1(1, sc, "root_mount_rel %p", vol->v_rootmount);
1783219974Smav		root_mount_rel(vol->v_rootmount);
1784219974Smav		vol->v_rootmount = NULL;
1785219974Smav	}
1786219974Smav	if (vol->v_stopping && vol->v_provider_open == 0)
1787219974Smav		g_raid_destroy_volume(vol);
1788219974Smav	return (0);
1789219974Smav}
1790219974Smav
1791219974Smav/*
1792219974Smav * Update subdisk state.
1793219974Smav */
1794219974Smavstatic int
1795219974Smavg_raid_update_subdisk(struct g_raid_subdisk *sd, u_int event)
1796219974Smav{
1797219974Smav	struct g_raid_softc *sc;
1798219974Smav	struct g_raid_volume *vol;
1799219974Smav
1800219974Smav	sc = sd->sd_softc;
1801219974Smav	vol = sd->sd_volume;
1802219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
1803219974Smav
1804219974Smav	G_RAID_DEBUG1(2, sc, "Event %s for subdisk %s:%d-%s.",
1805219974Smav	    g_raid_subdisk_event2str(event),
1806219974Smav	    vol->v_name, sd->sd_pos,
1807219974Smav	    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
1808219974Smav	if (vol->v_tr)
1809219974Smav		G_RAID_TR_EVENT(vol->v_tr, sd, event);
1810219974Smav
1811219974Smav	return (0);
1812219974Smav}
1813219974Smav
1814219974Smav/*
1815219974Smav * Update disk state.
1816219974Smav */
1817219974Smavstatic int
1818219974Smavg_raid_update_disk(struct g_raid_disk *disk, u_int event)
1819219974Smav{
1820219974Smav	struct g_raid_softc *sc;
1821219974Smav
1822219974Smav	sc = disk->d_softc;
1823219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
1824219974Smav
1825219974Smav	G_RAID_DEBUG1(2, sc, "Event %s for disk %s.",
1826219974Smav	    g_raid_disk_event2str(event),
1827219974Smav	    g_raid_get_diskname(disk));
1828219974Smav
1829219974Smav	if (sc->sc_md)
1830219974Smav		G_RAID_MD_EVENT(sc->sc_md, disk, event);
1831219974Smav	return (0);
1832219974Smav}
1833219974Smav
1834219974Smav/*
1835219974Smav * Node event.
1836219974Smav */
1837219974Smavstatic int
1838219974Smavg_raid_update_node(struct g_raid_softc *sc, u_int event)
1839219974Smav{
1840219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
1841219974Smav
1842219974Smav	G_RAID_DEBUG1(2, sc, "Event %s for the array.",
1843219974Smav	    g_raid_node_event2str(event));
1844219974Smav
1845219974Smav	if (event == G_RAID_NODE_E_WAKE)
1846219974Smav		return (0);
1847219974Smav	if (sc->sc_md)
1848219974Smav		G_RAID_MD_EVENT(sc->sc_md, NULL, event);
1849219974Smav	return (0);
1850219974Smav}
1851219974Smav
1852219974Smavstatic int
1853219974Smavg_raid_access(struct g_provider *pp, int acr, int acw, int ace)
1854219974Smav{
1855219974Smav	struct g_raid_volume *vol;
1856219974Smav	struct g_raid_softc *sc;
1857220210Smav	int dcw, opens, error = 0;
1858219974Smav
1859219974Smav	g_topology_assert();
1860219974Smav	sc = pp->geom->softc;
1861219974Smav	vol = pp->private;
1862219974Smav	KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
1863219974Smav	KASSERT(vol != NULL, ("NULL volume (provider=%s).", pp->name));
1864219974Smav
1865219974Smav	G_RAID_DEBUG1(2, sc, "Access request for %s: r%dw%de%d.", pp->name,
1866219974Smav	    acr, acw, ace);
1867219974Smav	dcw = pp->acw + acw;
1868219974Smav
1869219974Smav	g_topology_unlock();
1870219974Smav	sx_xlock(&sc->sc_lock);
1871219974Smav	/* Deny new opens while dying. */
1872219974Smav	if (sc->sc_stopping != 0 && (acr > 0 || acw > 0 || ace > 0)) {
1873219974Smav		error = ENXIO;
1874219974Smav		goto out;
1875219974Smav	}
1876254275Smav	/* Deny write opens for read-only volumes. */
1877254275Smav	if (vol->v_read_only && acw > 0) {
1878254275Smav		error = EROFS;
1879254275Smav		goto out;
1880254275Smav	}
1881242314Smav	if (dcw == 0)
1882219974Smav		g_raid_clean(vol, dcw);
1883219974Smav	vol->v_provider_open += acr + acw + ace;
1884219974Smav	/* Handle delayed node destruction. */
1885219974Smav	if (sc->sc_stopping == G_RAID_DESTROY_DELAYED &&
1886219974Smav	    vol->v_provider_open == 0) {
1887219974Smav		/* Count open volumes. */
1888219974Smav		opens = g_raid_nopens(sc);
1889219974Smav		if (opens == 0) {
1890219974Smav			sc->sc_stopping = G_RAID_DESTROY_HARD;
1891219974Smav			/* Wake up worker to make it selfdestruct. */
1892219974Smav			g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
1893219974Smav		}
1894219974Smav	}
1895219974Smav	/* Handle open volume destruction. */
1896219974Smav	if (vol->v_stopping && vol->v_provider_open == 0)
1897219974Smav		g_raid_destroy_volume(vol);
1898219974Smavout:
1899219974Smav	sx_xunlock(&sc->sc_lock);
1900219974Smav	g_topology_lock();
1901219974Smav	return (error);
1902219974Smav}
1903219974Smav
1904219974Smavstruct g_raid_softc *
1905219974Smavg_raid_create_node(struct g_class *mp,
1906219974Smav    const char *name, struct g_raid_md_object *md)
1907219974Smav{
1908219974Smav	struct g_raid_softc *sc;
1909219974Smav	struct g_geom *gp;
1910219974Smav	int error;
1911219974Smav
1912219974Smav	g_topology_assert();
1913219974Smav	G_RAID_DEBUG(1, "Creating array %s.", name);
1914219974Smav
1915219974Smav	gp = g_new_geomf(mp, "%s", name);
1916219974Smav	sc = malloc(sizeof(*sc), M_RAID, M_WAITOK | M_ZERO);
1917219974Smav	gp->start = g_raid_start;
1918219974Smav	gp->orphan = g_raid_orphan;
1919219974Smav	gp->access = g_raid_access;
1920219974Smav	gp->dumpconf = g_raid_dumpconf;
1921219974Smav
1922219974Smav	sc->sc_md = md;
1923219974Smav	sc->sc_geom = gp;
1924219974Smav	sc->sc_flags = 0;
1925219974Smav	TAILQ_INIT(&sc->sc_volumes);
1926219974Smav	TAILQ_INIT(&sc->sc_disks);
1927234816Smav	sx_init(&sc->sc_lock, "graid:lock");
1928234816Smav	mtx_init(&sc->sc_queue_mtx, "graid:queue", NULL, MTX_DEF);
1929219974Smav	TAILQ_INIT(&sc->sc_events);
1930219974Smav	bioq_init(&sc->sc_queue);
1931219974Smav	gp->softc = sc;
1932219974Smav	error = kproc_create(g_raid_worker, sc, &sc->sc_worker, 0, 0,
1933219974Smav	    "g_raid %s", name);
1934219974Smav	if (error != 0) {
1935219974Smav		G_RAID_DEBUG(0, "Cannot create kernel thread for %s.", name);
1936219974Smav		mtx_destroy(&sc->sc_queue_mtx);
1937219974Smav		sx_destroy(&sc->sc_lock);
1938219974Smav		g_destroy_geom(sc->sc_geom);
1939219974Smav		free(sc, M_RAID);
1940219974Smav		return (NULL);
1941219974Smav	}
1942219974Smav
1943219974Smav	G_RAID_DEBUG1(0, sc, "Array %s created.", name);
1944219974Smav	return (sc);
1945219974Smav}
1946219974Smav
1947219974Smavstruct g_raid_volume *
1948219974Smavg_raid_create_volume(struct g_raid_softc *sc, const char *name, int id)
1949219974Smav{
1950219974Smav	struct g_raid_volume	*vol, *vol1;
1951219974Smav	int i;
1952219974Smav
1953219974Smav	G_RAID_DEBUG1(1, sc, "Creating volume %s.", name);
1954219974Smav	vol = malloc(sizeof(*vol), M_RAID, M_WAITOK | M_ZERO);
1955219974Smav	vol->v_softc = sc;
1956219974Smav	strlcpy(vol->v_name, name, G_RAID_MAX_VOLUMENAME);
1957219974Smav	vol->v_state = G_RAID_VOLUME_S_STARTING;
1958219974Smav	vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
1959219974Smav	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_UNKNOWN;
1960235076Smav	vol->v_rotate_parity = 1;
1961219974Smav	bioq_init(&vol->v_inflight);
1962219974Smav	bioq_init(&vol->v_locked);
1963219974Smav	LIST_INIT(&vol->v_locks);
1964219974Smav	for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
1965219974Smav		vol->v_subdisks[i].sd_softc = sc;
1966219974Smav		vol->v_subdisks[i].sd_volume = vol;
1967219974Smav		vol->v_subdisks[i].sd_pos = i;
1968219974Smav		vol->v_subdisks[i].sd_state = G_RAID_DISK_S_NONE;
1969219974Smav	}
1970219974Smav
1971219974Smav	/* Find free ID for this volume. */
1972219974Smav	g_topology_lock();
1973219974Smav	vol1 = vol;
1974219974Smav	if (id >= 0) {
1975219974Smav		LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1976219974Smav			if (vol1->v_global_id == id)
1977219974Smav				break;
1978219974Smav		}
1979219974Smav	}
1980219974Smav	if (vol1 != NULL) {
1981219974Smav		for (id = 0; ; id++) {
1982219974Smav			LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1983219974Smav				if (vol1->v_global_id == id)
1984219974Smav					break;
1985219974Smav			}
1986219974Smav			if (vol1 == NULL)
1987219974Smav				break;
1988219974Smav		}
1989219974Smav	}
1990219974Smav	vol->v_global_id = id;
1991219974Smav	LIST_INSERT_HEAD(&g_raid_volumes, vol, v_global_next);
1992219974Smav	g_topology_unlock();
1993219974Smav
1994219974Smav	/* Delay root mounting. */
1995219974Smav	vol->v_rootmount = root_mount_hold("GRAID");
1996219974Smav	G_RAID_DEBUG1(1, sc, "root_mount_hold %p", vol->v_rootmount);
1997219974Smav	vol->v_starting = 1;
1998219974Smav	TAILQ_INSERT_TAIL(&sc->sc_volumes, vol, v_next);
1999219974Smav	return (vol);
2000219974Smav}
2001219974Smav
2002219974Smavstruct g_raid_disk *
2003219974Smavg_raid_create_disk(struct g_raid_softc *sc)
2004219974Smav{
2005219974Smav	struct g_raid_disk	*disk;
2006219974Smav
2007219974Smav	G_RAID_DEBUG1(1, sc, "Creating disk.");
2008219974Smav	disk = malloc(sizeof(*disk), M_RAID, M_WAITOK | M_ZERO);
2009219974Smav	disk->d_softc = sc;
2010219974Smav	disk->d_state = G_RAID_DISK_S_NONE;
2011219974Smav	TAILQ_INIT(&disk->d_subdisks);
2012219974Smav	TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
2013219974Smav	return (disk);
2014219974Smav}
2015219974Smav
2016219974Smavint g_raid_start_volume(struct g_raid_volume *vol)
2017219974Smav{
2018219974Smav	struct g_raid_tr_class *class;
2019219974Smav	struct g_raid_tr_object *obj;
2020219974Smav	int status;
2021219974Smav
2022219974Smav	G_RAID_DEBUG1(2, vol->v_softc, "Starting volume %s.", vol->v_name);
2023219974Smav	LIST_FOREACH(class, &g_raid_tr_classes, trc_list) {
2024240465Smav		if (!class->trc_enable)
2025240465Smav			continue;
2026219974Smav		G_RAID_DEBUG1(2, vol->v_softc,
2027219974Smav		    "Tasting volume %s for %s transformation.",
2028219974Smav		    vol->v_name, class->name);
2029219974Smav		obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2030219974Smav		    M_WAITOK);
2031219974Smav		obj->tro_class = class;
2032219974Smav		obj->tro_volume = vol;
2033219974Smav		status = G_RAID_TR_TASTE(obj, vol);
2034219974Smav		if (status != G_RAID_TR_TASTE_FAIL)
2035219974Smav			break;
2036219974Smav		kobj_delete((kobj_t)obj, M_RAID);
2037219974Smav	}
2038219974Smav	if (class == NULL) {
2039219974Smav		G_RAID_DEBUG1(0, vol->v_softc,
2040219974Smav		    "No transformation module found for %s.",
2041219974Smav		    vol->v_name);
2042219974Smav		vol->v_tr = NULL;
2043219974Smav		g_raid_change_volume_state(vol, G_RAID_VOLUME_S_UNSUPPORTED);
2044219974Smav		g_raid_event_send(vol, G_RAID_VOLUME_E_DOWN,
2045219974Smav		    G_RAID_EVENT_VOLUME);
2046219974Smav		return (-1);
2047219974Smav	}
2048219974Smav	G_RAID_DEBUG1(2, vol->v_softc,
2049219974Smav	    "Transformation module %s chosen for %s.",
2050219974Smav	    class->name, vol->v_name);
2051219974Smav	vol->v_tr = obj;
2052219974Smav	return (0);
2053219974Smav}
2054219974Smav
2055219974Smavint
2056219974Smavg_raid_destroy_node(struct g_raid_softc *sc, int worker)
2057219974Smav{
2058219974Smav	struct g_raid_volume *vol, *tmpv;
2059219974Smav	struct g_raid_disk *disk, *tmpd;
2060219974Smav	int error = 0;
2061219974Smav
2062219974Smav	sc->sc_stopping = G_RAID_DESTROY_HARD;
2063219974Smav	TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tmpv) {
2064219974Smav		if (g_raid_destroy_volume(vol))
2065219974Smav			error = EBUSY;
2066219974Smav	}
2067219974Smav	if (error)
2068219974Smav		return (error);
2069219974Smav	TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tmpd) {
2070219974Smav		if (g_raid_destroy_disk(disk))
2071219974Smav			error = EBUSY;
2072219974Smav	}
2073219974Smav	if (error)
2074219974Smav		return (error);
2075219974Smav	if (sc->sc_md) {
2076219974Smav		G_RAID_MD_FREE(sc->sc_md);
2077219974Smav		kobj_delete((kobj_t)sc->sc_md, M_RAID);
2078219974Smav		sc->sc_md = NULL;
2079219974Smav	}
2080219974Smav	if (sc->sc_geom != NULL) {
2081219974Smav		G_RAID_DEBUG1(0, sc, "Array %s destroyed.", sc->sc_name);
2082219974Smav		g_topology_lock();
2083219974Smav		sc->sc_geom->softc = NULL;
2084219974Smav		g_wither_geom(sc->sc_geom, ENXIO);
2085219974Smav		g_topology_unlock();
2086219974Smav		sc->sc_geom = NULL;
2087219974Smav	} else
2088219974Smav		G_RAID_DEBUG(1, "Array destroyed.");
2089219974Smav	if (worker) {
2090219974Smav		g_raid_event_cancel(sc, sc);
2091219974Smav		mtx_destroy(&sc->sc_queue_mtx);
2092219974Smav		sx_xunlock(&sc->sc_lock);
2093219974Smav		sx_destroy(&sc->sc_lock);
2094219974Smav		wakeup(&sc->sc_stopping);
2095219974Smav		free(sc, M_RAID);
2096219974Smav		curthread->td_pflags &= ~TDP_GEOM;
2097219974Smav		G_RAID_DEBUG(1, "Thread exiting.");
2098219974Smav		kproc_exit(0);
2099219974Smav	} else {
2100219974Smav		/* Wake up worker to make it selfdestruct. */
2101219974Smav		g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2102219974Smav	}
2103219974Smav	return (0);
2104219974Smav}
2105219974Smav
2106219974Smavint
2107219974Smavg_raid_destroy_volume(struct g_raid_volume *vol)
2108219974Smav{
2109219974Smav	struct g_raid_softc *sc;
2110219974Smav	struct g_raid_disk *disk;
2111219974Smav	int i;
2112219974Smav
2113219974Smav	sc = vol->v_softc;
2114219974Smav	G_RAID_DEBUG1(2, sc, "Destroying volume %s.", vol->v_name);
2115219974Smav	vol->v_stopping = 1;
2116219974Smav	if (vol->v_state != G_RAID_VOLUME_S_STOPPED) {
2117219974Smav		if (vol->v_tr) {
2118219974Smav			G_RAID_TR_STOP(vol->v_tr);
2119219974Smav			return (EBUSY);
2120219974Smav		} else
2121219974Smav			vol->v_state = G_RAID_VOLUME_S_STOPPED;
2122219974Smav	}
2123219974Smav	if (g_raid_event_check(sc, vol) != 0)
2124219974Smav		return (EBUSY);
2125219974Smav	if (vol->v_provider != NULL)
2126219974Smav		return (EBUSY);
2127219974Smav	if (vol->v_provider_open != 0)
2128219974Smav		return (EBUSY);
2129219974Smav	if (vol->v_tr) {
2130219974Smav		G_RAID_TR_FREE(vol->v_tr);
2131219974Smav		kobj_delete((kobj_t)vol->v_tr, M_RAID);
2132219974Smav		vol->v_tr = NULL;
2133219974Smav	}
2134219974Smav	if (vol->v_rootmount)
2135219974Smav		root_mount_rel(vol->v_rootmount);
2136219974Smav	g_topology_lock();
2137219974Smav	LIST_REMOVE(vol, v_global_next);
2138219974Smav	g_topology_unlock();
2139219974Smav	TAILQ_REMOVE(&sc->sc_volumes, vol, v_next);
2140219974Smav	for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
2141219974Smav		g_raid_event_cancel(sc, &vol->v_subdisks[i]);
2142219974Smav		disk = vol->v_subdisks[i].sd_disk;
2143219974Smav		if (disk == NULL)
2144219974Smav			continue;
2145219974Smav		TAILQ_REMOVE(&disk->d_subdisks, &vol->v_subdisks[i], sd_next);
2146219974Smav	}
2147219974Smav	G_RAID_DEBUG1(2, sc, "Volume %s destroyed.", vol->v_name);
2148219974Smav	if (sc->sc_md)
2149219974Smav		G_RAID_MD_FREE_VOLUME(sc->sc_md, vol);
2150219974Smav	g_raid_event_cancel(sc, vol);
2151219974Smav	free(vol, M_RAID);
2152219974Smav	if (sc->sc_stopping == G_RAID_DESTROY_HARD) {
2153219974Smav		/* Wake up worker to let it selfdestruct. */
2154219974Smav		g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2155219974Smav	}
2156219974Smav	return (0);
2157219974Smav}
2158219974Smav
2159219974Smavint
2160219974Smavg_raid_destroy_disk(struct g_raid_disk *disk)
2161219974Smav{
2162219974Smav	struct g_raid_softc *sc;
2163219974Smav	struct g_raid_subdisk *sd, *tmp;
2164219974Smav
2165219974Smav	sc = disk->d_softc;
2166219974Smav	G_RAID_DEBUG1(2, sc, "Destroying disk.");
2167219974Smav	if (disk->d_consumer) {
2168219974Smav		g_raid_kill_consumer(sc, disk->d_consumer);
2169219974Smav		disk->d_consumer = NULL;
2170219974Smav	}
2171219974Smav	TAILQ_FOREACH_SAFE(sd, &disk->d_subdisks, sd_next, tmp) {
2172219974Smav		g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_NONE);
2173219974Smav		g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
2174219974Smav		    G_RAID_EVENT_SUBDISK);
2175219974Smav		TAILQ_REMOVE(&disk->d_subdisks, sd, sd_next);
2176219974Smav		sd->sd_disk = NULL;
2177219974Smav	}
2178219974Smav	TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
2179219974Smav	if (sc->sc_md)
2180219974Smav		G_RAID_MD_FREE_DISK(sc->sc_md, disk);
2181219974Smav	g_raid_event_cancel(sc, disk);
2182219974Smav	free(disk, M_RAID);
2183219974Smav	return (0);
2184219974Smav}
2185219974Smav
2186219974Smavint
2187219974Smavg_raid_destroy(struct g_raid_softc *sc, int how)
2188219974Smav{
2189253706Smav	int error, opens;
2190219974Smav
2191219974Smav	g_topology_assert_not();
2192219974Smav	if (sc == NULL)
2193219974Smav		return (ENXIO);
2194219974Smav	sx_assert(&sc->sc_lock, SX_XLOCKED);
2195219974Smav
2196219974Smav	/* Count open volumes. */
2197219974Smav	opens = g_raid_nopens(sc);
2198219974Smav
2199219974Smav	/* React on some opened volumes. */
2200219974Smav	if (opens > 0) {
2201219974Smav		switch (how) {
2202219974Smav		case G_RAID_DESTROY_SOFT:
2203219974Smav			G_RAID_DEBUG1(1, sc,
2204219974Smav			    "%d volumes are still open.",
2205219974Smav			    opens);
2206253706Smav			sx_xunlock(&sc->sc_lock);
2207219974Smav			return (EBUSY);
2208219974Smav		case G_RAID_DESTROY_DELAYED:
2209219974Smav			G_RAID_DEBUG1(1, sc,
2210219974Smav			    "Array will be destroyed on last close.");
2211219974Smav			sc->sc_stopping = G_RAID_DESTROY_DELAYED;
2212253706Smav			sx_xunlock(&sc->sc_lock);
2213219974Smav			return (EBUSY);
2214219974Smav		case G_RAID_DESTROY_HARD:
2215219974Smav			G_RAID_DEBUG1(1, sc,
2216219974Smav			    "%d volumes are still open.",
2217219974Smav			    opens);
2218219974Smav		}
2219219974Smav	}
2220219974Smav
2221219974Smav	/* Mark node for destruction. */
2222219974Smav	sc->sc_stopping = G_RAID_DESTROY_HARD;
2223219974Smav	/* Wake up worker to let it selfdestruct. */
2224219974Smav	g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
2225219974Smav	/* Sleep until node destroyed. */
2226253706Smav	error = sx_sleep(&sc->sc_stopping, &sc->sc_lock,
2227253706Smav	    PRIBIO | PDROP, "r:destroy", hz * 3);
2228253706Smav	return (error == EWOULDBLOCK ? EBUSY : 0);
2229219974Smav}
2230219974Smav
2231219974Smavstatic void
2232219974Smavg_raid_taste_orphan(struct g_consumer *cp)
2233219974Smav{
2234219974Smav
2235219974Smav	KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2236219974Smav	    cp->provider->name));
2237219974Smav}
2238219974Smav
2239219974Smavstatic struct g_geom *
2240219974Smavg_raid_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2241219974Smav{
2242219974Smav	struct g_consumer *cp;
2243219974Smav	struct g_geom *gp, *geom;
2244219974Smav	struct g_raid_md_class *class;
2245219974Smav	struct g_raid_md_object *obj;
2246219974Smav	int status;
2247219974Smav
2248219974Smav	g_topology_assert();
2249219974Smav	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
2250240465Smav	if (!g_raid_enable)
2251240465Smav		return (NULL);
2252219974Smav	G_RAID_DEBUG(2, "Tasting provider %s.", pp->name);
2253219974Smav
2254265669Smav	geom = NULL;
2255265669Smav	status = G_RAID_MD_TASTE_FAIL;
2256234940Smav	gp = g_new_geomf(mp, "raid:taste");
2257219974Smav	/*
2258219974Smav	 * This orphan function should be never called.
2259219974Smav	 */
2260219974Smav	gp->orphan = g_raid_taste_orphan;
2261219974Smav	cp = g_new_consumer(gp);
2262260385Sscottl	cp->flags |= G_CF_DIRECT_RECEIVE;
2263219974Smav	g_attach(cp, pp);
2264265669Smav	if (g_access(cp, 1, 0, 0) != 0)
2265265669Smav		goto ofail;
2266219974Smav
2267219974Smav	LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2268240465Smav		if (!class->mdc_enable)
2269240465Smav			continue;
2270219974Smav		G_RAID_DEBUG(2, "Tasting provider %s for %s metadata.",
2271219974Smav		    pp->name, class->name);
2272219974Smav		obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2273219974Smav		    M_WAITOK);
2274219974Smav		obj->mdo_class = class;
2275219974Smav		status = G_RAID_MD_TASTE(obj, mp, cp, &geom);
2276219974Smav		if (status != G_RAID_MD_TASTE_NEW)
2277219974Smav			kobj_delete((kobj_t)obj, M_RAID);
2278219974Smav		if (status != G_RAID_MD_TASTE_FAIL)
2279219974Smav			break;
2280219974Smav	}
2281219974Smav
2282265669Smav	if (status == G_RAID_MD_TASTE_FAIL)
2283265669Smav		(void)g_access(cp, -1, 0, 0);
2284265669Smavofail:
2285219974Smav	g_detach(cp);
2286219974Smav	g_destroy_consumer(cp);
2287219974Smav	g_destroy_geom(gp);
2288219974Smav	G_RAID_DEBUG(2, "Tasting provider %s done.", pp->name);
2289219974Smav	return (geom);
2290219974Smav}
2291219974Smav
2292219974Smavint
2293234940Smavg_raid_create_node_format(const char *format, struct gctl_req *req,
2294234940Smav    struct g_geom **gp)
2295219974Smav{
2296219974Smav	struct g_raid_md_class *class;
2297219974Smav	struct g_raid_md_object *obj;
2298219974Smav	int status;
2299219974Smav
2300219974Smav	G_RAID_DEBUG(2, "Creating array for %s metadata.", format);
2301219974Smav	LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2302219974Smav		if (strcasecmp(class->name, format) == 0)
2303219974Smav			break;
2304219974Smav	}
2305219974Smav	if (class == NULL) {
2306219974Smav		G_RAID_DEBUG(1, "No support for %s metadata.", format);
2307219974Smav		return (G_RAID_MD_TASTE_FAIL);
2308219974Smav	}
2309219974Smav	obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2310219974Smav	    M_WAITOK);
2311219974Smav	obj->mdo_class = class;
2312234940Smav	status = G_RAID_MD_CREATE_REQ(obj, &g_raid_class, req, gp);
2313219974Smav	if (status != G_RAID_MD_TASTE_NEW)
2314219974Smav		kobj_delete((kobj_t)obj, M_RAID);
2315219974Smav	return (status);
2316219974Smav}
2317219974Smav
2318219974Smavstatic int
2319219974Smavg_raid_destroy_geom(struct gctl_req *req __unused,
2320219974Smav    struct g_class *mp __unused, struct g_geom *gp)
2321219974Smav{
2322219974Smav	struct g_raid_softc *sc;
2323219974Smav	int error;
2324219974Smav
2325219974Smav	g_topology_unlock();
2326219974Smav	sc = gp->softc;
2327219974Smav	sx_xlock(&sc->sc_lock);
2328219974Smav	g_cancel_event(sc);
2329219974Smav	error = g_raid_destroy(gp->softc, G_RAID_DESTROY_SOFT);
2330219974Smav	g_topology_lock();
2331219974Smav	return (error);
2332219974Smav}
2333219974Smav
2334219974Smavvoid g_raid_write_metadata(struct g_raid_softc *sc, struct g_raid_volume *vol,
2335219974Smav    struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2336219974Smav{
2337219974Smav
2338219974Smav	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2339219974Smav		return;
2340219974Smav	if (sc->sc_md)
2341219974Smav		G_RAID_MD_WRITE(sc->sc_md, vol, sd, disk);
2342219974Smav}
2343219974Smav
2344219974Smavvoid g_raid_fail_disk(struct g_raid_softc *sc,
2345219974Smav    struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2346219974Smav{
2347219974Smav
2348219974Smav	if (disk == NULL)
2349219974Smav		disk = sd->sd_disk;
2350219974Smav	if (disk == NULL) {
2351219974Smav		G_RAID_DEBUG1(0, sc, "Warning! Fail request to an absent disk!");
2352219974Smav		return;
2353219974Smav	}
2354219974Smav	if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2355219974Smav		G_RAID_DEBUG1(0, sc, "Warning! Fail request to a disk in a "
2356219974Smav		    "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
2357219974Smav		return;
2358219974Smav	}
2359219974Smav	if (sc->sc_md)
2360219974Smav		G_RAID_MD_FAIL_DISK(sc->sc_md, sd, disk);
2361219974Smav}
2362219974Smav
2363219974Smavstatic void
2364219974Smavg_raid_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2365219974Smav    struct g_consumer *cp, struct g_provider *pp)
2366219974Smav{
2367219974Smav	struct g_raid_softc *sc;
2368219974Smav	struct g_raid_volume *vol;
2369219974Smav	struct g_raid_subdisk *sd;
2370219974Smav	struct g_raid_disk *disk;
2371219974Smav	int i, s;
2372219974Smav
2373219974Smav	g_topology_assert();
2374219974Smav
2375219974Smav	sc = gp->softc;
2376219974Smav	if (sc == NULL)
2377219974Smav		return;
2378219974Smav	if (pp != NULL) {
2379219974Smav		vol = pp->private;
2380219974Smav		g_topology_unlock();
2381219974Smav		sx_xlock(&sc->sc_lock);
2382249974Smav		sbuf_printf(sb, "%s<descr>%s %s volume</descr>\n", indent,
2383249974Smav		    sc->sc_md->mdo_class->name,
2384249974Smav		    g_raid_volume_level2str(vol->v_raid_level,
2385249974Smav		    vol->v_raid_level_qualifier));
2386219974Smav		sbuf_printf(sb, "%s<Label>%s</Label>\n", indent,
2387219974Smav		    vol->v_name);
2388219974Smav		sbuf_printf(sb, "%s<RAIDLevel>%s</RAIDLevel>\n", indent,
2389219974Smav		    g_raid_volume_level2str(vol->v_raid_level,
2390219974Smav		    vol->v_raid_level_qualifier));
2391219974Smav		sbuf_printf(sb,
2392219974Smav		    "%s<Transformation>%s</Transformation>\n", indent,
2393219974Smav		    vol->v_tr ? vol->v_tr->tro_class->name : "NONE");
2394219974Smav		sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
2395219974Smav		    vol->v_disks_count);
2396219974Smav		sbuf_printf(sb, "%s<Strip>%u</Strip>\n", indent,
2397219974Smav		    vol->v_strip_size);
2398219974Smav		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2399219974Smav		    g_raid_volume_state2str(vol->v_state));
2400219974Smav		sbuf_printf(sb, "%s<Dirty>%s</Dirty>\n", indent,
2401219974Smav		    vol->v_dirty ? "Yes" : "No");
2402219974Smav		sbuf_printf(sb, "%s<Subdisks>", indent);
2403219974Smav		for (i = 0; i < vol->v_disks_count; i++) {
2404219974Smav			sd = &vol->v_subdisks[i];
2405219974Smav			if (sd->sd_disk != NULL &&
2406219974Smav			    sd->sd_disk->d_consumer != NULL) {
2407219974Smav				sbuf_printf(sb, "%s ",
2408219974Smav				    g_raid_get_diskname(sd->sd_disk));
2409219974Smav			} else {
2410219974Smav				sbuf_printf(sb, "NONE ");
2411219974Smav			}
2412219974Smav			sbuf_printf(sb, "(%s",
2413219974Smav			    g_raid_subdisk_state2str(sd->sd_state));
2414219974Smav			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2415219974Smav			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2416219974Smav				sbuf_printf(sb, " %d%%",
2417219974Smav				    (int)(sd->sd_rebuild_pos * 100 /
2418219974Smav				     sd->sd_size));
2419219974Smav			}
2420219974Smav			sbuf_printf(sb, ")");
2421219974Smav			if (i + 1 < vol->v_disks_count)
2422219974Smav				sbuf_printf(sb, ", ");
2423219974Smav		}
2424219974Smav		sbuf_printf(sb, "</Subdisks>\n");
2425219974Smav		sx_xunlock(&sc->sc_lock);
2426219974Smav		g_topology_lock();
2427219974Smav	} else if (cp != NULL) {
2428219974Smav		disk = cp->private;
2429219974Smav		if (disk == NULL)
2430219974Smav			return;
2431219974Smav		g_topology_unlock();
2432219974Smav		sx_xlock(&sc->sc_lock);
2433219974Smav		sbuf_printf(sb, "%s<State>%s", indent,
2434219974Smav		    g_raid_disk_state2str(disk->d_state));
2435219974Smav		if (!TAILQ_EMPTY(&disk->d_subdisks)) {
2436219974Smav			sbuf_printf(sb, " (");
2437219974Smav			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2438219974Smav				sbuf_printf(sb, "%s",
2439219974Smav				    g_raid_subdisk_state2str(sd->sd_state));
2440219974Smav				if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2441219974Smav				    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2442219974Smav					sbuf_printf(sb, " %d%%",
2443219974Smav					    (int)(sd->sd_rebuild_pos * 100 /
2444219974Smav					     sd->sd_size));
2445219974Smav				}
2446219974Smav				if (TAILQ_NEXT(sd, sd_next))
2447219974Smav					sbuf_printf(sb, ", ");
2448219974Smav			}
2449219974Smav			sbuf_printf(sb, ")");
2450219974Smav		}
2451219974Smav		sbuf_printf(sb, "</State>\n");
2452219974Smav		sbuf_printf(sb, "%s<Subdisks>", indent);
2453219974Smav		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2454219974Smav			sbuf_printf(sb, "r%d(%s):%d@%ju",
2455219974Smav			    sd->sd_volume->v_global_id,
2456219974Smav			    sd->sd_volume->v_name,
2457219974Smav			    sd->sd_pos, sd->sd_offset);
2458219974Smav			if (TAILQ_NEXT(sd, sd_next))
2459219974Smav				sbuf_printf(sb, ", ");
2460219974Smav		}
2461219974Smav		sbuf_printf(sb, "</Subdisks>\n");
2462219974Smav		sbuf_printf(sb, "%s<ReadErrors>%d</ReadErrors>\n", indent,
2463219974Smav		    disk->d_read_errs);
2464219974Smav		sx_xunlock(&sc->sc_lock);
2465219974Smav		g_topology_lock();
2466219974Smav	} else {
2467219974Smav		g_topology_unlock();
2468219974Smav		sx_xlock(&sc->sc_lock);
2469219974Smav		if (sc->sc_md) {
2470219974Smav			sbuf_printf(sb, "%s<Metadata>%s</Metadata>\n", indent,
2471219974Smav			    sc->sc_md->mdo_class->name);
2472219974Smav		}
2473219974Smav		if (!TAILQ_EMPTY(&sc->sc_volumes)) {
2474219974Smav			s = 0xff;
2475219974Smav			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2476219974Smav				if (vol->v_state < s)
2477219974Smav					s = vol->v_state;
2478219974Smav			}
2479219974Smav			sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2480219974Smav			    g_raid_volume_state2str(s));
2481219974Smav		}
2482219974Smav		sx_xunlock(&sc->sc_lock);
2483219974Smav		g_topology_lock();
2484219974Smav	}
2485219974Smav}
2486219974Smav
2487219974Smavstatic void
2488242314Smavg_raid_shutdown_post_sync(void *arg, int howto)
2489219974Smav{
2490219974Smav	struct g_class *mp;
2491219974Smav	struct g_geom *gp, *gp2;
2492219974Smav	struct g_raid_softc *sc;
2493242314Smav	struct g_raid_volume *vol;
2494219974Smav
2495219974Smav	mp = arg;
2496219974Smav	DROP_GIANT();
2497219974Smav	g_topology_lock();
2498242314Smav	g_raid_shutdown = 1;
2499219974Smav	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
2500219974Smav		if ((sc = gp->softc) == NULL)
2501219974Smav			continue;
2502219974Smav		g_topology_unlock();
2503219974Smav		sx_xlock(&sc->sc_lock);
2504242314Smav		TAILQ_FOREACH(vol, &sc->sc_volumes, v_next)
2505242314Smav			g_raid_clean(vol, -1);
2506219974Smav		g_cancel_event(sc);
2507253706Smav		g_raid_destroy(sc, G_RAID_DESTROY_DELAYED);
2508219974Smav		g_topology_lock();
2509219974Smav	}
2510219974Smav	g_topology_unlock();
2511219974Smav	PICKUP_GIANT();
2512219974Smav}
2513219974Smav
2514219974Smavstatic void
2515219974Smavg_raid_init(struct g_class *mp)
2516219974Smav{
2517219974Smav
2518242314Smav	g_raid_post_sync = EVENTHANDLER_REGISTER(shutdown_post_sync,
2519242314Smav	    g_raid_shutdown_post_sync, mp, SHUTDOWN_PRI_FIRST);
2520242314Smav	if (g_raid_post_sync == NULL)
2521219974Smav		G_RAID_DEBUG(0, "Warning! Cannot register shutdown event.");
2522219974Smav	g_raid_started = 1;
2523219974Smav}
2524219974Smav
2525219974Smavstatic void
2526219974Smavg_raid_fini(struct g_class *mp)
2527219974Smav{
2528219974Smav
2529242314Smav	if (g_raid_post_sync != NULL)
2530242314Smav		EVENTHANDLER_DEREGISTER(shutdown_post_sync, g_raid_post_sync);
2531219974Smav	g_raid_started = 0;
2532219974Smav}
2533219974Smav
2534219974Smavint
2535219974Smavg_raid_md_modevent(module_t mod, int type, void *arg)
2536219974Smav{
2537219974Smav	struct g_raid_md_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_md_classes);
2545219974Smav		if (c == NULL || c->mdc_priority > class->mdc_priority)
2546219974Smav			LIST_INSERT_HEAD(&g_raid_md_classes, class, mdc_list);
2547219974Smav		else {
2548219974Smav			while ((nc = LIST_NEXT(c, mdc_list)) != NULL &&
2549219974Smav			    nc->mdc_priority < class->mdc_priority)
2550219974Smav				c = nc;
2551219974Smav			LIST_INSERT_AFTER(c, class, mdc_list);
2552219974Smav		}
2553219974Smav		if (g_raid_started)
2554219974Smav			g_retaste(&g_raid_class);
2555219974Smav		break;
2556219974Smav	case MOD_UNLOAD:
2557219974Smav		LIST_REMOVE(class, mdc_list);
2558219974Smav		break;
2559219974Smav	default:
2560219974Smav		error = EOPNOTSUPP;
2561219974Smav		break;
2562219974Smav	}
2563219974Smav
2564219974Smav	return (error);
2565219974Smav}
2566219974Smav
2567219974Smavint
2568219974Smavg_raid_tr_modevent(module_t mod, int type, void *arg)
2569219974Smav{
2570219974Smav	struct g_raid_tr_class *class, *c, *nc;
2571219974Smav	int error;
2572219974Smav
2573219974Smav	error = 0;
2574219974Smav	class = arg;
2575219974Smav	switch (type) {
2576219974Smav	case MOD_LOAD:
2577219974Smav		c = LIST_FIRST(&g_raid_tr_classes);
2578219974Smav		if (c == NULL || c->trc_priority > class->trc_priority)
2579219974Smav			LIST_INSERT_HEAD(&g_raid_tr_classes, class, trc_list);
2580219974Smav		else {
2581219974Smav			while ((nc = LIST_NEXT(c, trc_list)) != NULL &&
2582219974Smav			    nc->trc_priority < class->trc_priority)
2583219974Smav				c = nc;
2584219974Smav			LIST_INSERT_AFTER(c, class, trc_list);
2585219974Smav		}
2586219974Smav		break;
2587219974Smav	case MOD_UNLOAD:
2588219974Smav		LIST_REMOVE(class, trc_list);
2589219974Smav		break;
2590219974Smav	default:
2591219974Smav		error = EOPNOTSUPP;
2592219974Smav		break;
2593219974Smav	}
2594219974Smav
2595219974Smav	return (error);
2596219974Smav}
2597219974Smav
2598219974Smav/*
2599219974Smav * Use local implementation of DECLARE_GEOM_CLASS(g_raid_class, g_raid)
2600219974Smav * to reduce module priority, allowing submodules to register them first.
2601219974Smav */
2602219974Smavstatic moduledata_t g_raid_mod = {
2603219974Smav	"g_raid",
2604219974Smav	g_modevent,
2605219974Smav	&g_raid_class
2606219974Smav};
2607219974SmavDECLARE_MODULE(g_raid, g_raid_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD);
2608219974SmavMODULE_VERSION(geom_raid, 0);
2609