xref: /freebsd/sys/geom/raid/g_raid.c (revision 7aa65846327fe5bc7e5961c2f7fd0c61f2ec0b01)
1 /*-
2  * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/limits.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/bio.h>
38 #include <sys/sbuf.h>
39 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/eventhandler.h>
42 #include <vm/uma.h>
43 #include <geom/geom.h>
44 #include <sys/proc.h>
45 #include <sys/kthread.h>
46 #include <sys/sched.h>
47 #include <geom/raid/g_raid.h>
48 #include "g_raid_md_if.h"
49 #include "g_raid_tr_if.h"
50 
51 static MALLOC_DEFINE(M_RAID, "raid_data", "GEOM_RAID Data");
52 
53 SYSCTL_DECL(_kern_geom);
54 SYSCTL_NODE(_kern_geom, OID_AUTO, raid, CTLFLAG_RW, 0, "GEOM_RAID stuff");
55 u_int g_raid_aggressive_spare = 0;
56 TUNABLE_INT("kern.geom.raid.aggressive_spare", &g_raid_aggressive_spare);
57 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, aggressive_spare, CTLFLAG_RW,
58     &g_raid_aggressive_spare, 0, "Use disks without metadata as spare");
59 u_int g_raid_debug = 0;
60 TUNABLE_INT("kern.geom.raid.debug", &g_raid_debug);
61 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, debug, CTLFLAG_RW, &g_raid_debug, 0,
62     "Debug level");
63 int g_raid_read_err_thresh = 10;
64 TUNABLE_INT("kern.geom.raid.read_err_thresh", &g_raid_read_err_thresh);
65 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, read_err_thresh, CTLFLAG_RW,
66     &g_raid_read_err_thresh, 0,
67     "Number of read errors equated to disk failure");
68 u_int g_raid_start_timeout = 30;
69 TUNABLE_INT("kern.geom.raid.start_timeout", &g_raid_start_timeout);
70 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, start_timeout, CTLFLAG_RW,
71     &g_raid_start_timeout, 0,
72     "Time to wait for all array components");
73 static u_int g_raid_clean_time = 5;
74 TUNABLE_INT("kern.geom.raid.clean_time", &g_raid_clean_time);
75 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, clean_time, CTLFLAG_RW,
76     &g_raid_clean_time, 0, "Mark volume as clean when idling");
77 static u_int g_raid_disconnect_on_failure = 1;
78 TUNABLE_INT("kern.geom.raid.disconnect_on_failure",
79     &g_raid_disconnect_on_failure);
80 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, disconnect_on_failure, CTLFLAG_RW,
81     &g_raid_disconnect_on_failure, 0, "Disconnect component on I/O failure.");
82 static u_int g_raid_name_format = 0;
83 TUNABLE_INT("kern.geom.raid.name_format", &g_raid_name_format);
84 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, name_format, CTLFLAG_RW,
85     &g_raid_name_format, 0, "Providers name format.");
86 static u_int g_raid_idle_threshold = 1000000;
87 TUNABLE_INT("kern.geom.raid.idle_threshold", &g_raid_idle_threshold);
88 SYSCTL_UINT(_kern_geom_raid, OID_AUTO, idle_threshold, CTLFLAG_RW,
89     &g_raid_idle_threshold, 1000000,
90     "Time in microseconds to consider a volume idle.");
91 
92 #define	MSLEEP(rv, ident, mtx, priority, wmesg, timeout)	do {	\
93 	G_RAID_DEBUG(4, "%s: Sleeping %p.", __func__, (ident));		\
94 	rv = msleep((ident), (mtx), (priority), (wmesg), (timeout));	\
95 	G_RAID_DEBUG(4, "%s: Woken up %p.", __func__, (ident));		\
96 } while (0)
97 
98 LIST_HEAD(, g_raid_md_class) g_raid_md_classes =
99     LIST_HEAD_INITIALIZER(g_raid_md_classes);
100 
101 LIST_HEAD(, g_raid_tr_class) g_raid_tr_classes =
102     LIST_HEAD_INITIALIZER(g_raid_tr_classes);
103 
104 LIST_HEAD(, g_raid_volume) g_raid_volumes =
105     LIST_HEAD_INITIALIZER(g_raid_volumes);
106 
107 static eventhandler_tag g_raid_pre_sync = NULL;
108 static int g_raid_started = 0;
109 
110 static int g_raid_destroy_geom(struct gctl_req *req, struct g_class *mp,
111     struct g_geom *gp);
112 static g_taste_t g_raid_taste;
113 static void g_raid_init(struct g_class *mp);
114 static void g_raid_fini(struct g_class *mp);
115 
116 struct g_class g_raid_class = {
117 	.name = G_RAID_CLASS_NAME,
118 	.version = G_VERSION,
119 	.ctlreq = g_raid_ctl,
120 	.taste = g_raid_taste,
121 	.destroy_geom = g_raid_destroy_geom,
122 	.init = g_raid_init,
123 	.fini = g_raid_fini
124 };
125 
126 static void g_raid_destroy_provider(struct g_raid_volume *vol);
127 static int g_raid_update_disk(struct g_raid_disk *disk, u_int event);
128 static int g_raid_update_subdisk(struct g_raid_subdisk *subdisk, u_int event);
129 static int g_raid_update_volume(struct g_raid_volume *vol, u_int event);
130 static int g_raid_update_node(struct g_raid_softc *sc, u_int event);
131 static void g_raid_dumpconf(struct sbuf *sb, const char *indent,
132     struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
133 static void g_raid_start(struct bio *bp);
134 static void g_raid_start_request(struct bio *bp);
135 static void g_raid_disk_done(struct bio *bp);
136 static void g_raid_poll(struct g_raid_softc *sc);
137 
138 static const char *
139 g_raid_node_event2str(int event)
140 {
141 
142 	switch (event) {
143 	case G_RAID_NODE_E_WAKE:
144 		return ("WAKE");
145 	case G_RAID_NODE_E_START:
146 		return ("START");
147 	default:
148 		return ("INVALID");
149 	}
150 }
151 
152 const char *
153 g_raid_disk_state2str(int state)
154 {
155 
156 	switch (state) {
157 	case G_RAID_DISK_S_NONE:
158 		return ("NONE");
159 	case G_RAID_DISK_S_OFFLINE:
160 		return ("OFFLINE");
161 	case G_RAID_DISK_S_FAILED:
162 		return ("FAILED");
163 	case G_RAID_DISK_S_STALE_FAILED:
164 		return ("STALE_FAILED");
165 	case G_RAID_DISK_S_SPARE:
166 		return ("SPARE");
167 	case G_RAID_DISK_S_STALE:
168 		return ("STALE");
169 	case G_RAID_DISK_S_ACTIVE:
170 		return ("ACTIVE");
171 	default:
172 		return ("INVALID");
173 	}
174 }
175 
176 static const char *
177 g_raid_disk_event2str(int event)
178 {
179 
180 	switch (event) {
181 	case G_RAID_DISK_E_DISCONNECTED:
182 		return ("DISCONNECTED");
183 	default:
184 		return ("INVALID");
185 	}
186 }
187 
188 const char *
189 g_raid_subdisk_state2str(int state)
190 {
191 
192 	switch (state) {
193 	case G_RAID_SUBDISK_S_NONE:
194 		return ("NONE");
195 	case G_RAID_SUBDISK_S_FAILED:
196 		return ("FAILED");
197 	case G_RAID_SUBDISK_S_NEW:
198 		return ("NEW");
199 	case G_RAID_SUBDISK_S_REBUILD:
200 		return ("REBUILD");
201 	case G_RAID_SUBDISK_S_UNINITIALIZED:
202 		return ("UNINITIALIZED");
203 	case G_RAID_SUBDISK_S_STALE:
204 		return ("STALE");
205 	case G_RAID_SUBDISK_S_RESYNC:
206 		return ("RESYNC");
207 	case G_RAID_SUBDISK_S_ACTIVE:
208 		return ("ACTIVE");
209 	default:
210 		return ("INVALID");
211 	}
212 }
213 
214 static const char *
215 g_raid_subdisk_event2str(int event)
216 {
217 
218 	switch (event) {
219 	case G_RAID_SUBDISK_E_NEW:
220 		return ("NEW");
221 	case G_RAID_SUBDISK_E_DISCONNECTED:
222 		return ("DISCONNECTED");
223 	default:
224 		return ("INVALID");
225 	}
226 }
227 
228 const char *
229 g_raid_volume_state2str(int state)
230 {
231 
232 	switch (state) {
233 	case G_RAID_VOLUME_S_STARTING:
234 		return ("STARTING");
235 	case G_RAID_VOLUME_S_BROKEN:
236 		return ("BROKEN");
237 	case G_RAID_VOLUME_S_DEGRADED:
238 		return ("DEGRADED");
239 	case G_RAID_VOLUME_S_SUBOPTIMAL:
240 		return ("SUBOPTIMAL");
241 	case G_RAID_VOLUME_S_OPTIMAL:
242 		return ("OPTIMAL");
243 	case G_RAID_VOLUME_S_UNSUPPORTED:
244 		return ("UNSUPPORTED");
245 	case G_RAID_VOLUME_S_STOPPED:
246 		return ("STOPPED");
247 	default:
248 		return ("INVALID");
249 	}
250 }
251 
252 static const char *
253 g_raid_volume_event2str(int event)
254 {
255 
256 	switch (event) {
257 	case G_RAID_VOLUME_E_UP:
258 		return ("UP");
259 	case G_RAID_VOLUME_E_DOWN:
260 		return ("DOWN");
261 	case G_RAID_VOLUME_E_START:
262 		return ("START");
263 	case G_RAID_VOLUME_E_STARTMD:
264 		return ("STARTMD");
265 	default:
266 		return ("INVALID");
267 	}
268 }
269 
270 const char *
271 g_raid_volume_level2str(int level, int qual)
272 {
273 
274 	switch (level) {
275 	case G_RAID_VOLUME_RL_RAID0:
276 		return ("RAID0");
277 	case G_RAID_VOLUME_RL_RAID1:
278 		return ("RAID1");
279 	case G_RAID_VOLUME_RL_RAID3:
280 		return ("RAID3");
281 	case G_RAID_VOLUME_RL_RAID4:
282 		return ("RAID4");
283 	case G_RAID_VOLUME_RL_RAID5:
284 		if (qual == G_RAID_VOLUME_RLQ_R5RA)
285 			return ("RAID5RA");
286 		if (qual == G_RAID_VOLUME_RLQ_R5RS)
287 			return ("RAID5RS");
288 		if (qual == G_RAID_VOLUME_RLQ_R5LA)
289 			return ("RAID5LA");
290 		if (qual == G_RAID_VOLUME_RLQ_R5LS)
291 			return ("RAID5LS");
292 		return ("RAID5");
293 	case G_RAID_VOLUME_RL_RAID6:
294 		return ("RAID6");
295 	case G_RAID_VOLUME_RL_RAID1E:
296 		return ("RAID1E");
297 	case G_RAID_VOLUME_RL_SINGLE:
298 		return ("SINGLE");
299 	case G_RAID_VOLUME_RL_CONCAT:
300 		return ("CONCAT");
301 	case G_RAID_VOLUME_RL_RAID5E:
302 		return ("RAID5E");
303 	case G_RAID_VOLUME_RL_RAID5EE:
304 		return ("RAID5EE");
305 	default:
306 		return ("UNKNOWN");
307 	}
308 }
309 
310 int
311 g_raid_volume_str2level(const char *str, int *level, int *qual)
312 {
313 
314 	*level = G_RAID_VOLUME_RL_UNKNOWN;
315 	*qual = G_RAID_VOLUME_RLQ_NONE;
316 	if (strcasecmp(str, "RAID0") == 0)
317 		*level = G_RAID_VOLUME_RL_RAID0;
318 	else if (strcasecmp(str, "RAID1") == 0)
319 		*level = G_RAID_VOLUME_RL_RAID1;
320 	else if (strcasecmp(str, "RAID3") == 0)
321 		*level = G_RAID_VOLUME_RL_RAID3;
322 	else if (strcasecmp(str, "RAID4") == 0)
323 		*level = G_RAID_VOLUME_RL_RAID4;
324 	else if (strcasecmp(str, "RAID5RA") == 0) {
325 		*level = G_RAID_VOLUME_RL_RAID5;
326 		*qual = G_RAID_VOLUME_RLQ_R5RA;
327 	} else if (strcasecmp(str, "RAID5RS") == 0) {
328 		*level = G_RAID_VOLUME_RL_RAID5;
329 		*qual = G_RAID_VOLUME_RLQ_R5RS;
330 	} else if (strcasecmp(str, "RAID5") == 0 ||
331 		   strcasecmp(str, "RAID5LA") == 0) {
332 		*level = G_RAID_VOLUME_RL_RAID5;
333 		*qual = G_RAID_VOLUME_RLQ_R5LA;
334 	} else if (strcasecmp(str, "RAID5LS") == 0) {
335 		*level = G_RAID_VOLUME_RL_RAID5;
336 		*qual = G_RAID_VOLUME_RLQ_R5LS;
337 	} else if (strcasecmp(str, "RAID6") == 0)
338 		*level = G_RAID_VOLUME_RL_RAID6;
339 	else if (strcasecmp(str, "RAID10") == 0 ||
340 		 strcasecmp(str, "RAID1E") == 0)
341 		*level = G_RAID_VOLUME_RL_RAID1E;
342 	else if (strcasecmp(str, "SINGLE") == 0)
343 		*level = G_RAID_VOLUME_RL_SINGLE;
344 	else if (strcasecmp(str, "CONCAT") == 0)
345 		*level = G_RAID_VOLUME_RL_CONCAT;
346 	else if (strcasecmp(str, "RAID5E") == 0)
347 		*level = G_RAID_VOLUME_RL_RAID5E;
348 	else if (strcasecmp(str, "RAID5EE") == 0)
349 		*level = G_RAID_VOLUME_RL_RAID5EE;
350 	else
351 		return (-1);
352 	return (0);
353 }
354 
355 const char *
356 g_raid_get_diskname(struct g_raid_disk *disk)
357 {
358 
359 	if (disk->d_consumer == NULL || disk->d_consumer->provider == NULL)
360 		return ("[unknown]");
361 	return (disk->d_consumer->provider->name);
362 }
363 
364 void
365 g_raid_report_disk_state(struct g_raid_disk *disk)
366 {
367 	struct g_raid_subdisk *sd;
368 	int len, state;
369 	uint32_t s;
370 
371 	if (disk->d_consumer == NULL)
372 		return;
373 	if (disk->d_state == G_RAID_DISK_S_FAILED ||
374 	    disk->d_state == G_RAID_DISK_S_STALE_FAILED) {
375 		s = G_STATE_FAILED;
376 	} else {
377 		state = G_RAID_SUBDISK_S_ACTIVE;
378 		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
379 			if (sd->sd_state < state)
380 				state = sd->sd_state;
381 		}
382 		if (state == G_RAID_SUBDISK_S_FAILED)
383 			s = G_STATE_FAILED;
384 		else if (state == G_RAID_SUBDISK_S_NEW ||
385 		    state == G_RAID_SUBDISK_S_REBUILD)
386 			s = G_STATE_REBUILD;
387 		else if (state == G_RAID_SUBDISK_S_STALE ||
388 		    state == G_RAID_SUBDISK_S_RESYNC)
389 			s = G_STATE_RESYNC;
390 		else
391 			s = G_STATE_ACTIVE;
392 	}
393 	len = sizeof(s);
394 	g_io_getattr("GEOM::setstate", disk->d_consumer, &len, &s);
395 	G_RAID_DEBUG1(2, disk->d_softc, "Disk %s state reported as %d.",
396 	    g_raid_get_diskname(disk), s);
397 }
398 
399 void
400 g_raid_change_disk_state(struct g_raid_disk *disk, int state)
401 {
402 
403 	G_RAID_DEBUG1(0, disk->d_softc, "Disk %s state changed from %s to %s.",
404 	    g_raid_get_diskname(disk),
405 	    g_raid_disk_state2str(disk->d_state),
406 	    g_raid_disk_state2str(state));
407 	disk->d_state = state;
408 	g_raid_report_disk_state(disk);
409 }
410 
411 void
412 g_raid_change_subdisk_state(struct g_raid_subdisk *sd, int state)
413 {
414 
415 	G_RAID_DEBUG1(0, sd->sd_softc,
416 	    "Subdisk %s:%d-%s state changed from %s to %s.",
417 	    sd->sd_volume->v_name, sd->sd_pos,
418 	    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]",
419 	    g_raid_subdisk_state2str(sd->sd_state),
420 	    g_raid_subdisk_state2str(state));
421 	sd->sd_state = state;
422 	if (sd->sd_disk)
423 		g_raid_report_disk_state(sd->sd_disk);
424 }
425 
426 void
427 g_raid_change_volume_state(struct g_raid_volume *vol, int state)
428 {
429 
430 	G_RAID_DEBUG1(0, vol->v_softc,
431 	    "Volume %s state changed from %s to %s.",
432 	    vol->v_name,
433 	    g_raid_volume_state2str(vol->v_state),
434 	    g_raid_volume_state2str(state));
435 	vol->v_state = state;
436 }
437 
438 /*
439  * --- Events handling functions ---
440  * Events in geom_raid are used to maintain subdisks and volumes status
441  * from one thread to simplify locking.
442  */
443 static void
444 g_raid_event_free(struct g_raid_event *ep)
445 {
446 
447 	free(ep, M_RAID);
448 }
449 
450 int
451 g_raid_event_send(void *arg, int event, int flags)
452 {
453 	struct g_raid_softc *sc;
454 	struct g_raid_event *ep;
455 	int error;
456 
457 	if ((flags & G_RAID_EVENT_VOLUME) != 0) {
458 		sc = ((struct g_raid_volume *)arg)->v_softc;
459 	} else if ((flags & G_RAID_EVENT_DISK) != 0) {
460 		sc = ((struct g_raid_disk *)arg)->d_softc;
461 	} else if ((flags & G_RAID_EVENT_SUBDISK) != 0) {
462 		sc = ((struct g_raid_subdisk *)arg)->sd_softc;
463 	} else {
464 		sc = arg;
465 	}
466 	ep = malloc(sizeof(*ep), M_RAID,
467 	    sx_xlocked(&sc->sc_lock) ? M_WAITOK : M_NOWAIT);
468 	if (ep == NULL)
469 		return (ENOMEM);
470 	ep->e_tgt = arg;
471 	ep->e_event = event;
472 	ep->e_flags = flags;
473 	ep->e_error = 0;
474 	G_RAID_DEBUG1(4, sc, "Sending event %p. Waking up %p.", ep, sc);
475 	mtx_lock(&sc->sc_queue_mtx);
476 	TAILQ_INSERT_TAIL(&sc->sc_events, ep, e_next);
477 	mtx_unlock(&sc->sc_queue_mtx);
478 	wakeup(sc);
479 
480 	if ((flags & G_RAID_EVENT_WAIT) == 0)
481 		return (0);
482 
483 	sx_assert(&sc->sc_lock, SX_XLOCKED);
484 	G_RAID_DEBUG1(4, sc, "Sleeping on %p.", ep);
485 	sx_xunlock(&sc->sc_lock);
486 	while ((ep->e_flags & G_RAID_EVENT_DONE) == 0) {
487 		mtx_lock(&sc->sc_queue_mtx);
488 		MSLEEP(error, ep, &sc->sc_queue_mtx, PRIBIO | PDROP, "m:event",
489 		    hz * 5);
490 	}
491 	error = ep->e_error;
492 	g_raid_event_free(ep);
493 	sx_xlock(&sc->sc_lock);
494 	return (error);
495 }
496 
497 static void
498 g_raid_event_cancel(struct g_raid_softc *sc, void *tgt)
499 {
500 	struct g_raid_event *ep, *tmpep;
501 
502 	sx_assert(&sc->sc_lock, SX_XLOCKED);
503 
504 	mtx_lock(&sc->sc_queue_mtx);
505 	TAILQ_FOREACH_SAFE(ep, &sc->sc_events, e_next, tmpep) {
506 		if (ep->e_tgt != tgt)
507 			continue;
508 		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
509 		if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0)
510 			g_raid_event_free(ep);
511 		else {
512 			ep->e_error = ECANCELED;
513 			wakeup(ep);
514 		}
515 	}
516 	mtx_unlock(&sc->sc_queue_mtx);
517 }
518 
519 static int
520 g_raid_event_check(struct g_raid_softc *sc, void *tgt)
521 {
522 	struct g_raid_event *ep;
523 	int	res = 0;
524 
525 	sx_assert(&sc->sc_lock, SX_XLOCKED);
526 
527 	mtx_lock(&sc->sc_queue_mtx);
528 	TAILQ_FOREACH(ep, &sc->sc_events, e_next) {
529 		if (ep->e_tgt != tgt)
530 			continue;
531 		res = 1;
532 		break;
533 	}
534 	mtx_unlock(&sc->sc_queue_mtx);
535 	return (res);
536 }
537 
538 /*
539  * Return the number of disks in given state.
540  * If state is equal to -1, count all connected disks.
541  */
542 u_int
543 g_raid_ndisks(struct g_raid_softc *sc, int state)
544 {
545 	struct g_raid_disk *disk;
546 	u_int n;
547 
548 	sx_assert(&sc->sc_lock, SX_LOCKED);
549 
550 	n = 0;
551 	TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
552 		if (disk->d_state == state || state == -1)
553 			n++;
554 	}
555 	return (n);
556 }
557 
558 /*
559  * Return the number of subdisks in given state.
560  * If state is equal to -1, count all connected disks.
561  */
562 u_int
563 g_raid_nsubdisks(struct g_raid_volume *vol, int state)
564 {
565 	struct g_raid_subdisk *subdisk;
566 	struct g_raid_softc *sc;
567 	u_int i, n ;
568 
569 	sc = vol->v_softc;
570 	sx_assert(&sc->sc_lock, SX_LOCKED);
571 
572 	n = 0;
573 	for (i = 0; i < vol->v_disks_count; i++) {
574 		subdisk = &vol->v_subdisks[i];
575 		if ((state == -1 &&
576 		     subdisk->sd_state != G_RAID_SUBDISK_S_NONE) ||
577 		    subdisk->sd_state == state)
578 			n++;
579 	}
580 	return (n);
581 }
582 
583 /*
584  * Return the first subdisk in given state.
585  * If state is equal to -1, then the first connected disks.
586  */
587 struct g_raid_subdisk *
588 g_raid_get_subdisk(struct g_raid_volume *vol, int state)
589 {
590 	struct g_raid_subdisk *sd;
591 	struct g_raid_softc *sc;
592 	u_int i;
593 
594 	sc = vol->v_softc;
595 	sx_assert(&sc->sc_lock, SX_LOCKED);
596 
597 	for (i = 0; i < vol->v_disks_count; i++) {
598 		sd = &vol->v_subdisks[i];
599 		if ((state == -1 &&
600 		     sd->sd_state != G_RAID_SUBDISK_S_NONE) ||
601 		    sd->sd_state == state)
602 			return (sd);
603 	}
604 	return (NULL);
605 }
606 
607 struct g_consumer *
608 g_raid_open_consumer(struct g_raid_softc *sc, const char *name)
609 {
610 	struct g_consumer *cp;
611 	struct g_provider *pp;
612 
613 	g_topology_assert();
614 
615 	if (strncmp(name, "/dev/", 5) == 0)
616 		name += 5;
617 	pp = g_provider_by_name(name);
618 	if (pp == NULL)
619 		return (NULL);
620 	cp = g_new_consumer(sc->sc_geom);
621 	if (g_attach(cp, pp) != 0) {
622 		g_destroy_consumer(cp);
623 		return (NULL);
624 	}
625 	if (g_access(cp, 1, 1, 1) != 0) {
626 		g_detach(cp);
627 		g_destroy_consumer(cp);
628 		return (NULL);
629 	}
630 	return (cp);
631 }
632 
633 static u_int
634 g_raid_nrequests(struct g_raid_softc *sc, struct g_consumer *cp)
635 {
636 	struct bio *bp;
637 	u_int nreqs = 0;
638 
639 	mtx_lock(&sc->sc_queue_mtx);
640 	TAILQ_FOREACH(bp, &sc->sc_queue.queue, bio_queue) {
641 		if (bp->bio_from == cp)
642 			nreqs++;
643 	}
644 	mtx_unlock(&sc->sc_queue_mtx);
645 	return (nreqs);
646 }
647 
648 u_int
649 g_raid_nopens(struct g_raid_softc *sc)
650 {
651 	struct g_raid_volume *vol;
652 	u_int opens;
653 
654 	opens = 0;
655 	TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
656 		if (vol->v_provider_open != 0)
657 			opens++;
658 	}
659 	return (opens);
660 }
661 
662 static int
663 g_raid_consumer_is_busy(struct g_raid_softc *sc, struct g_consumer *cp)
664 {
665 
666 	if (cp->index > 0) {
667 		G_RAID_DEBUG1(2, sc,
668 		    "I/O requests for %s exist, can't destroy it now.",
669 		    cp->provider->name);
670 		return (1);
671 	}
672 	if (g_raid_nrequests(sc, cp) > 0) {
673 		G_RAID_DEBUG1(2, sc,
674 		    "I/O requests for %s in queue, can't destroy it now.",
675 		    cp->provider->name);
676 		return (1);
677 	}
678 	return (0);
679 }
680 
681 static void
682 g_raid_destroy_consumer(void *arg, int flags __unused)
683 {
684 	struct g_consumer *cp;
685 
686 	g_topology_assert();
687 
688 	cp = arg;
689 	G_RAID_DEBUG(1, "Consumer %s destroyed.", cp->provider->name);
690 	g_detach(cp);
691 	g_destroy_consumer(cp);
692 }
693 
694 void
695 g_raid_kill_consumer(struct g_raid_softc *sc, struct g_consumer *cp)
696 {
697 	struct g_provider *pp;
698 	int retaste_wait;
699 
700 	g_topology_assert_not();
701 
702 	g_topology_lock();
703 	cp->private = NULL;
704 	if (g_raid_consumer_is_busy(sc, cp))
705 		goto out;
706 	pp = cp->provider;
707 	retaste_wait = 0;
708 	if (cp->acw == 1) {
709 		if ((pp->geom->flags & G_GEOM_WITHER) == 0)
710 			retaste_wait = 1;
711 	}
712 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
713 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
714 	if (retaste_wait) {
715 		/*
716 		 * After retaste event was send (inside g_access()), we can send
717 		 * event to detach and destroy consumer.
718 		 * A class, which has consumer to the given provider connected
719 		 * will not receive retaste event for the provider.
720 		 * This is the way how I ignore retaste events when I close
721 		 * consumers opened for write: I detach and destroy consumer
722 		 * after retaste event is sent.
723 		 */
724 		g_post_event(g_raid_destroy_consumer, cp, M_WAITOK, NULL);
725 		goto out;
726 	}
727 	G_RAID_DEBUG(1, "Consumer %s destroyed.", pp->name);
728 	g_detach(cp);
729 	g_destroy_consumer(cp);
730 out:
731 	g_topology_unlock();
732 }
733 
734 static void
735 g_raid_orphan(struct g_consumer *cp)
736 {
737 	struct g_raid_disk *disk;
738 
739 	g_topology_assert();
740 
741 	disk = cp->private;
742 	if (disk == NULL)
743 		return;
744 	g_raid_event_send(disk, G_RAID_DISK_E_DISCONNECTED,
745 	    G_RAID_EVENT_DISK);
746 }
747 
748 static int
749 g_raid_clean(struct g_raid_volume *vol, int acw)
750 {
751 	struct g_raid_softc *sc;
752 	int timeout;
753 
754 	sc = vol->v_softc;
755 	g_topology_assert_not();
756 	sx_assert(&sc->sc_lock, SX_XLOCKED);
757 
758 //	if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
759 //		return (0);
760 	if (!vol->v_dirty)
761 		return (0);
762 	if (vol->v_writes > 0)
763 		return (0);
764 	if (acw > 0 || (acw == -1 &&
765 	    vol->v_provider != NULL && vol->v_provider->acw > 0)) {
766 		timeout = g_raid_clean_time - (time_uptime - vol->v_last_write);
767 		if (timeout > 0)
768 			return (timeout);
769 	}
770 	vol->v_dirty = 0;
771 	G_RAID_DEBUG1(1, sc, "Volume %s marked as clean.",
772 	    vol->v_name);
773 	g_raid_write_metadata(sc, vol, NULL, NULL);
774 	return (0);
775 }
776 
777 static void
778 g_raid_dirty(struct g_raid_volume *vol)
779 {
780 	struct g_raid_softc *sc;
781 
782 	sc = vol->v_softc;
783 	g_topology_assert_not();
784 	sx_assert(&sc->sc_lock, SX_XLOCKED);
785 
786 //	if ((sc->sc_flags & G_RAID_DEVICE_FLAG_NOFAILSYNC) != 0)
787 //		return;
788 	vol->v_dirty = 1;
789 	G_RAID_DEBUG1(1, sc, "Volume %s marked as dirty.",
790 	    vol->v_name);
791 	g_raid_write_metadata(sc, vol, NULL, NULL);
792 }
793 
794 void
795 g_raid_tr_flush_common(struct g_raid_tr_object *tr, struct bio *bp)
796 {
797 	struct g_raid_softc *sc;
798 	struct g_raid_volume *vol;
799 	struct g_raid_subdisk *sd;
800 	struct bio_queue_head queue;
801 	struct bio *cbp;
802 	int i;
803 
804 	vol = tr->tro_volume;
805 	sc = vol->v_softc;
806 
807 	/*
808 	 * Allocate all bios before sending any request, so we can return
809 	 * ENOMEM in nice and clean way.
810 	 */
811 	bioq_init(&queue);
812 	for (i = 0; i < vol->v_disks_count; i++) {
813 		sd = &vol->v_subdisks[i];
814 		if (sd->sd_state == G_RAID_SUBDISK_S_NONE ||
815 		    sd->sd_state == G_RAID_SUBDISK_S_FAILED)
816 			continue;
817 		cbp = g_clone_bio(bp);
818 		if (cbp == NULL)
819 			goto failure;
820 		cbp->bio_caller1 = sd;
821 		bioq_insert_tail(&queue, cbp);
822 	}
823 	for (cbp = bioq_first(&queue); cbp != NULL;
824 	    cbp = bioq_first(&queue)) {
825 		bioq_remove(&queue, cbp);
826 		sd = cbp->bio_caller1;
827 		cbp->bio_caller1 = NULL;
828 		g_raid_subdisk_iostart(sd, cbp);
829 	}
830 	return;
831 failure:
832 	for (cbp = bioq_first(&queue); cbp != NULL;
833 	    cbp = bioq_first(&queue)) {
834 		bioq_remove(&queue, cbp);
835 		g_destroy_bio(cbp);
836 	}
837 	if (bp->bio_error == 0)
838 		bp->bio_error = ENOMEM;
839 	g_raid_iodone(bp, bp->bio_error);
840 }
841 
842 static void
843 g_raid_tr_kerneldump_common_done(struct bio *bp)
844 {
845 
846 	bp->bio_flags |= BIO_DONE;
847 }
848 
849 int
850 g_raid_tr_kerneldump_common(struct g_raid_tr_object *tr,
851     void *virtual, vm_offset_t physical, off_t offset, size_t length)
852 {
853 	struct g_raid_softc *sc;
854 	struct g_raid_volume *vol;
855 	struct bio bp;
856 
857 	vol = tr->tro_volume;
858 	sc = vol->v_softc;
859 
860 	bzero(&bp, sizeof(bp));
861 	bp.bio_cmd = BIO_WRITE;
862 	bp.bio_done = g_raid_tr_kerneldump_common_done;
863 	bp.bio_attribute = NULL;
864 	bp.bio_offset = offset;
865 	bp.bio_length = length;
866 	bp.bio_data = virtual;
867 	bp.bio_to = vol->v_provider;
868 
869 	g_raid_start(&bp);
870 	while (!(bp.bio_flags & BIO_DONE)) {
871 		G_RAID_DEBUG1(4, sc, "Poll...");
872 		g_raid_poll(sc);
873 		DELAY(10);
874 	}
875 
876 	return (bp.bio_error != 0 ? EIO : 0);
877 }
878 
879 static int
880 g_raid_dump(void *arg,
881     void *virtual, vm_offset_t physical, off_t offset, size_t length)
882 {
883 	struct g_raid_volume *vol;
884 	int error;
885 
886 	vol = (struct g_raid_volume *)arg;
887 	G_RAID_DEBUG1(3, vol->v_softc, "Dumping at off %llu len %llu.",
888 	    (long long unsigned)offset, (long long unsigned)length);
889 
890 	error = G_RAID_TR_KERNELDUMP(vol->v_tr,
891 	    virtual, physical, offset, length);
892 	return (error);
893 }
894 
895 static void
896 g_raid_kerneldump(struct g_raid_softc *sc, struct bio *bp)
897 {
898 	struct g_kerneldump *gkd;
899 	struct g_provider *pp;
900 	struct g_raid_volume *vol;
901 
902 	gkd = (struct g_kerneldump*)bp->bio_data;
903 	pp = bp->bio_to;
904 	vol = pp->private;
905 	g_trace(G_T_TOPOLOGY, "g_raid_kerneldump(%s, %jd, %jd)",
906 		pp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
907 	gkd->di.dumper = g_raid_dump;
908 	gkd->di.priv = vol;
909 	gkd->di.blocksize = vol->v_sectorsize;
910 	gkd->di.maxiosize = DFLTPHYS;
911 	gkd->di.mediaoffset = gkd->offset;
912 	if ((gkd->offset + gkd->length) > vol->v_mediasize)
913 		gkd->length = vol->v_mediasize - gkd->offset;
914 	gkd->di.mediasize = gkd->length;
915 	g_io_deliver(bp, 0);
916 }
917 
918 static void
919 g_raid_start(struct bio *bp)
920 {
921 	struct g_raid_softc *sc;
922 
923 	sc = bp->bio_to->geom->softc;
924 	/*
925 	 * If sc == NULL or there are no valid disks, provider's error
926 	 * should be set and g_raid_start() should not be called at all.
927 	 */
928 //	KASSERT(sc != NULL && sc->sc_state == G_RAID_VOLUME_S_RUNNING,
929 //	    ("Provider's error should be set (error=%d)(mirror=%s).",
930 //	    bp->bio_to->error, bp->bio_to->name));
931 	G_RAID_LOGREQ(3, bp, "Request received.");
932 
933 	switch (bp->bio_cmd) {
934 	case BIO_READ:
935 	case BIO_WRITE:
936 	case BIO_DELETE:
937 	case BIO_FLUSH:
938 		break;
939 	case BIO_GETATTR:
940 		if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
941 			g_raid_kerneldump(sc, bp);
942 		else
943 			g_io_deliver(bp, EOPNOTSUPP);
944 		return;
945 	default:
946 		g_io_deliver(bp, EOPNOTSUPP);
947 		return;
948 	}
949 	mtx_lock(&sc->sc_queue_mtx);
950 	bioq_disksort(&sc->sc_queue, bp);
951 	mtx_unlock(&sc->sc_queue_mtx);
952 	if (!dumping) {
953 		G_RAID_DEBUG1(4, sc, "Waking up %p.", sc);
954 		wakeup(sc);
955 	}
956 }
957 
958 static int
959 g_raid_bio_overlaps(const struct bio *bp, off_t lstart, off_t len)
960 {
961 	/*
962 	 * 5 cases:
963 	 * (1) bp entirely below NO
964 	 * (2) bp entirely above NO
965 	 * (3) bp start below, but end in range YES
966 	 * (4) bp entirely within YES
967 	 * (5) bp starts within, ends above YES
968 	 *
969 	 * lock range 10-19 (offset 10 length 10)
970 	 * (1) 1-5: first if kicks it out
971 	 * (2) 30-35: second if kicks it out
972 	 * (3) 5-15: passes both ifs
973 	 * (4) 12-14: passes both ifs
974 	 * (5) 19-20: passes both
975 	 */
976 	off_t lend = lstart + len - 1;
977 	off_t bstart = bp->bio_offset;
978 	off_t bend = bp->bio_offset + bp->bio_length - 1;
979 
980 	if (bend < lstart)
981 		return (0);
982 	if (lend < bstart)
983 		return (0);
984 	return (1);
985 }
986 
987 static int
988 g_raid_is_in_locked_range(struct g_raid_volume *vol, const struct bio *bp)
989 {
990 	struct g_raid_lock *lp;
991 
992 	sx_assert(&vol->v_softc->sc_lock, SX_LOCKED);
993 
994 	LIST_FOREACH(lp, &vol->v_locks, l_next) {
995 		if (g_raid_bio_overlaps(bp, lp->l_offset, lp->l_length))
996 			return (1);
997 	}
998 	return (0);
999 }
1000 
1001 static void
1002 g_raid_start_request(struct bio *bp)
1003 {
1004 	struct g_raid_softc *sc;
1005 	struct g_raid_volume *vol;
1006 
1007 	sc = bp->bio_to->geom->softc;
1008 	sx_assert(&sc->sc_lock, SX_LOCKED);
1009 	vol = bp->bio_to->private;
1010 
1011 	/*
1012 	 * Check to see if this item is in a locked range.  If so,
1013 	 * queue it to our locked queue and return.  We'll requeue
1014 	 * it when the range is unlocked.  Internal I/O for the
1015 	 * rebuild/rescan/recovery process is excluded from this
1016 	 * check so we can actually do the recovery.
1017 	 */
1018 	if (!(bp->bio_cflags & G_RAID_BIO_FLAG_SPECIAL) &&
1019 	    g_raid_is_in_locked_range(vol, bp)) {
1020 		G_RAID_LOGREQ(3, bp, "Defer request.");
1021 		bioq_insert_tail(&vol->v_locked, bp);
1022 		return;
1023 	}
1024 
1025 	/*
1026 	 * If we're actually going to do the write/delete, then
1027 	 * update the idle stats for the volume.
1028 	 */
1029 	if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1030 		if (!vol->v_dirty)
1031 			g_raid_dirty(vol);
1032 		vol->v_writes++;
1033 	}
1034 
1035 	/*
1036 	 * Put request onto inflight queue, so we can check if new
1037 	 * synchronization requests don't collide with it.  Then tell
1038 	 * the transformation layer to start the I/O.
1039 	 */
1040 	bioq_insert_tail(&vol->v_inflight, bp);
1041 	G_RAID_LOGREQ(4, bp, "Request started");
1042 	G_RAID_TR_IOSTART(vol->v_tr, bp);
1043 }
1044 
1045 static void
1046 g_raid_finish_with_locked_ranges(struct g_raid_volume *vol, struct bio *bp)
1047 {
1048 	off_t off, len;
1049 	struct bio *nbp;
1050 	struct g_raid_lock *lp;
1051 
1052 	vol->v_pending_lock = 0;
1053 	LIST_FOREACH(lp, &vol->v_locks, l_next) {
1054 		if (lp->l_pending) {
1055 			off = lp->l_offset;
1056 			len = lp->l_length;
1057 			lp->l_pending = 0;
1058 			TAILQ_FOREACH(nbp, &vol->v_inflight.queue, bio_queue) {
1059 				if (g_raid_bio_overlaps(nbp, off, len))
1060 					lp->l_pending++;
1061 			}
1062 			if (lp->l_pending) {
1063 				vol->v_pending_lock = 1;
1064 				G_RAID_DEBUG1(4, vol->v_softc,
1065 				    "Deferred lock(%jd, %jd) has %d pending",
1066 				    (intmax_t)off, (intmax_t)(off + len),
1067 				    lp->l_pending);
1068 				continue;
1069 			}
1070 			G_RAID_DEBUG1(4, vol->v_softc,
1071 			    "Deferred lock of %jd to %jd completed",
1072 			    (intmax_t)off, (intmax_t)(off + len));
1073 			G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1074 		}
1075 	}
1076 }
1077 
1078 void
1079 g_raid_iodone(struct bio *bp, int error)
1080 {
1081 	struct g_raid_softc *sc;
1082 	struct g_raid_volume *vol;
1083 
1084 	sc = bp->bio_to->geom->softc;
1085 	sx_assert(&sc->sc_lock, SX_LOCKED);
1086 	vol = bp->bio_to->private;
1087 	G_RAID_LOGREQ(3, bp, "Request done: %d.", error);
1088 
1089 	/* Update stats if we done write/delete. */
1090 	if (bp->bio_cmd == BIO_WRITE || bp->bio_cmd == BIO_DELETE) {
1091 		vol->v_writes--;
1092 		vol->v_last_write = time_uptime;
1093 	}
1094 
1095 	bioq_remove(&vol->v_inflight, bp);
1096 	if (vol->v_pending_lock && g_raid_is_in_locked_range(vol, bp))
1097 		g_raid_finish_with_locked_ranges(vol, bp);
1098 	getmicrouptime(&vol->v_last_done);
1099 	g_io_deliver(bp, error);
1100 }
1101 
1102 int
1103 g_raid_lock_range(struct g_raid_volume *vol, off_t off, off_t len,
1104     struct bio *ignore, void *argp)
1105 {
1106 	struct g_raid_softc *sc;
1107 	struct g_raid_lock *lp;
1108 	struct bio *bp;
1109 
1110 	sc = vol->v_softc;
1111 	lp = malloc(sizeof(*lp), M_RAID, M_WAITOK | M_ZERO);
1112 	LIST_INSERT_HEAD(&vol->v_locks, lp, l_next);
1113 	lp->l_offset = off;
1114 	lp->l_length = len;
1115 	lp->l_callback_arg = argp;
1116 
1117 	lp->l_pending = 0;
1118 	TAILQ_FOREACH(bp, &vol->v_inflight.queue, bio_queue) {
1119 		if (bp != ignore && g_raid_bio_overlaps(bp, off, len))
1120 			lp->l_pending++;
1121 	}
1122 
1123 	/*
1124 	 * If there are any writes that are pending, we return EBUSY.  All
1125 	 * callers will have to wait until all pending writes clear.
1126 	 */
1127 	if (lp->l_pending > 0) {
1128 		vol->v_pending_lock = 1;
1129 		G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd deferred %d pend",
1130 		    (intmax_t)off, (intmax_t)(off+len), lp->l_pending);
1131 		return (EBUSY);
1132 	}
1133 	G_RAID_DEBUG1(4, sc, "Locking range %jd to %jd",
1134 	    (intmax_t)off, (intmax_t)(off+len));
1135 	G_RAID_TR_LOCKED(vol->v_tr, lp->l_callback_arg);
1136 	return (0);
1137 }
1138 
1139 int
1140 g_raid_unlock_range(struct g_raid_volume *vol, off_t off, off_t len)
1141 {
1142 	struct g_raid_lock *lp;
1143 	struct g_raid_softc *sc;
1144 	struct bio *bp;
1145 
1146 	sc = vol->v_softc;
1147 	LIST_FOREACH(lp, &vol->v_locks, l_next) {
1148 		if (lp->l_offset == off && lp->l_length == len) {
1149 			LIST_REMOVE(lp, l_next);
1150 			/* XXX
1151 			 * Right now we just put them all back on the queue
1152 			 * and hope for the best.  We hope this because any
1153 			 * locked ranges will go right back on this list
1154 			 * when the worker thread runs.
1155 			 * XXX
1156 			 */
1157 			G_RAID_DEBUG1(4, sc, "Unlocked %jd to %jd",
1158 			    (intmax_t)lp->l_offset,
1159 			    (intmax_t)(lp->l_offset+lp->l_length));
1160 			mtx_lock(&sc->sc_queue_mtx);
1161 			while ((bp = bioq_takefirst(&vol->v_locked)) != NULL)
1162 				bioq_disksort(&sc->sc_queue, bp);
1163 			mtx_unlock(&sc->sc_queue_mtx);
1164 			free(lp, M_RAID);
1165 			return (0);
1166 		}
1167 	}
1168 	return (EINVAL);
1169 }
1170 
1171 void
1172 g_raid_subdisk_iostart(struct g_raid_subdisk *sd, struct bio *bp)
1173 {
1174 	struct g_consumer *cp;
1175 	struct g_raid_disk *disk, *tdisk;
1176 
1177 	bp->bio_caller1 = sd;
1178 
1179 	/*
1180 	 * Make sure that the disk is present. Generally it is a task of
1181 	 * transformation layers to not send requests to absent disks, but
1182 	 * it is better to be safe and report situation then sorry.
1183 	 */
1184 	if (sd->sd_disk == NULL) {
1185 		G_RAID_LOGREQ(0, bp, "Warning! I/O request to an absent disk!");
1186 nodisk:
1187 		bp->bio_from = NULL;
1188 		bp->bio_to = NULL;
1189 		bp->bio_error = ENXIO;
1190 		g_raid_disk_done(bp);
1191 		return;
1192 	}
1193 	disk = sd->sd_disk;
1194 	if (disk->d_state != G_RAID_DISK_S_ACTIVE &&
1195 	    disk->d_state != G_RAID_DISK_S_FAILED) {
1196 		G_RAID_LOGREQ(0, bp, "Warning! I/O request to a disk in a "
1197 		    "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
1198 		goto nodisk;
1199 	}
1200 
1201 	cp = disk->d_consumer;
1202 	bp->bio_from = cp;
1203 	bp->bio_to = cp->provider;
1204 	cp->index++;
1205 
1206 	/* Update average disks load. */
1207 	TAILQ_FOREACH(tdisk, &sd->sd_softc->sc_disks, d_next) {
1208 		if (tdisk->d_consumer == NULL)
1209 			tdisk->d_load = 0;
1210 		else
1211 			tdisk->d_load = (tdisk->d_consumer->index *
1212 			    G_RAID_SUBDISK_LOAD_SCALE + tdisk->d_load * 7) / 8;
1213 	}
1214 
1215 	disk->d_last_offset = bp->bio_offset + bp->bio_length;
1216 	if (dumping) {
1217 		G_RAID_LOGREQ(3, bp, "Sending dumping request.");
1218 		if (bp->bio_cmd == BIO_WRITE) {
1219 			bp->bio_error = g_raid_subdisk_kerneldump(sd,
1220 			    bp->bio_data, 0, bp->bio_offset, bp->bio_length);
1221 		} else
1222 			bp->bio_error = EOPNOTSUPP;
1223 		g_raid_disk_done(bp);
1224 	} else {
1225 		bp->bio_done = g_raid_disk_done;
1226 		bp->bio_offset += sd->sd_offset;
1227 		G_RAID_LOGREQ(3, bp, "Sending request.");
1228 		g_io_request(bp, cp);
1229 	}
1230 }
1231 
1232 int
1233 g_raid_subdisk_kerneldump(struct g_raid_subdisk *sd,
1234     void *virtual, vm_offset_t physical, off_t offset, size_t length)
1235 {
1236 
1237 	if (sd->sd_disk == NULL)
1238 		return (ENXIO);
1239 	if (sd->sd_disk->d_kd.di.dumper == NULL)
1240 		return (EOPNOTSUPP);
1241 	return (dump_write(&sd->sd_disk->d_kd.di,
1242 	    virtual, physical,
1243 	    sd->sd_disk->d_kd.di.mediaoffset + sd->sd_offset + offset,
1244 	    length));
1245 }
1246 
1247 static void
1248 g_raid_disk_done(struct bio *bp)
1249 {
1250 	struct g_raid_softc *sc;
1251 	struct g_raid_subdisk *sd;
1252 
1253 	sd = bp->bio_caller1;
1254 	sc = sd->sd_softc;
1255 	mtx_lock(&sc->sc_queue_mtx);
1256 	bioq_disksort(&sc->sc_queue, bp);
1257 	mtx_unlock(&sc->sc_queue_mtx);
1258 	if (!dumping)
1259 		wakeup(sc);
1260 }
1261 
1262 static void
1263 g_raid_disk_done_request(struct bio *bp)
1264 {
1265 	struct g_raid_softc *sc;
1266 	struct g_raid_disk *disk;
1267 	struct g_raid_subdisk *sd;
1268 	struct g_raid_volume *vol;
1269 
1270 	g_topology_assert_not();
1271 
1272 	G_RAID_LOGREQ(3, bp, "Disk request done: %d.", bp->bio_error);
1273 	sd = bp->bio_caller1;
1274 	sc = sd->sd_softc;
1275 	vol = sd->sd_volume;
1276 	if (bp->bio_from != NULL) {
1277 		bp->bio_from->index--;
1278 		disk = bp->bio_from->private;
1279 		if (disk == NULL)
1280 			g_raid_kill_consumer(sc, bp->bio_from);
1281 	}
1282 	bp->bio_offset -= sd->sd_offset;
1283 
1284 	G_RAID_TR_IODONE(vol->v_tr, sd, bp);
1285 }
1286 
1287 static void
1288 g_raid_handle_event(struct g_raid_softc *sc, struct g_raid_event *ep)
1289 {
1290 
1291 	if ((ep->e_flags & G_RAID_EVENT_VOLUME) != 0)
1292 		ep->e_error = g_raid_update_volume(ep->e_tgt, ep->e_event);
1293 	else if ((ep->e_flags & G_RAID_EVENT_DISK) != 0)
1294 		ep->e_error = g_raid_update_disk(ep->e_tgt, ep->e_event);
1295 	else if ((ep->e_flags & G_RAID_EVENT_SUBDISK) != 0)
1296 		ep->e_error = g_raid_update_subdisk(ep->e_tgt, ep->e_event);
1297 	else
1298 		ep->e_error = g_raid_update_node(ep->e_tgt, ep->e_event);
1299 	if ((ep->e_flags & G_RAID_EVENT_WAIT) == 0) {
1300 		KASSERT(ep->e_error == 0,
1301 		    ("Error cannot be handled."));
1302 		g_raid_event_free(ep);
1303 	} else {
1304 		ep->e_flags |= G_RAID_EVENT_DONE;
1305 		G_RAID_DEBUG1(4, sc, "Waking up %p.", ep);
1306 		mtx_lock(&sc->sc_queue_mtx);
1307 		wakeup(ep);
1308 		mtx_unlock(&sc->sc_queue_mtx);
1309 	}
1310 }
1311 
1312 /*
1313  * Worker thread.
1314  */
1315 static void
1316 g_raid_worker(void *arg)
1317 {
1318 	struct g_raid_softc *sc;
1319 	struct g_raid_event *ep;
1320 	struct g_raid_volume *vol;
1321 	struct bio *bp;
1322 	struct timeval now, t;
1323 	int timeout, rv;
1324 
1325 	sc = arg;
1326 	thread_lock(curthread);
1327 	sched_prio(curthread, PRIBIO);
1328 	thread_unlock(curthread);
1329 
1330 	sx_xlock(&sc->sc_lock);
1331 	for (;;) {
1332 		mtx_lock(&sc->sc_queue_mtx);
1333 		/*
1334 		 * First take a look at events.
1335 		 * This is important to handle events before any I/O requests.
1336 		 */
1337 		bp = NULL;
1338 		vol = NULL;
1339 		rv = 0;
1340 		ep = TAILQ_FIRST(&sc->sc_events);
1341 		if (ep != NULL)
1342 			TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1343 		else if ((bp = bioq_takefirst(&sc->sc_queue)) != NULL)
1344 			;
1345 		else {
1346 			getmicrouptime(&now);
1347 			t = now;
1348 			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1349 				if (bioq_first(&vol->v_inflight) == NULL &&
1350 				    vol->v_tr &&
1351 				    timevalcmp(&vol->v_last_done, &t, < ))
1352 					t = vol->v_last_done;
1353 			}
1354 			timevalsub(&t, &now);
1355 			timeout = g_raid_idle_threshold +
1356 			    t.tv_sec * 1000000 + t.tv_usec;
1357 			if (timeout > 0) {
1358 				/*
1359 				 * Two steps to avoid overflows at HZ=1000
1360 				 * and idle timeouts > 2.1s.  Some rounding
1361 				 * errors can occur, but they are < 1tick,
1362 				 * which is deemed to be close enough for
1363 				 * this purpose.
1364 				 */
1365 				int micpertic = 1000000 / hz;
1366 				timeout = (timeout + micpertic - 1) / micpertic;
1367 				sx_xunlock(&sc->sc_lock);
1368 				MSLEEP(rv, sc, &sc->sc_queue_mtx,
1369 				    PRIBIO | PDROP, "-", timeout);
1370 				sx_xlock(&sc->sc_lock);
1371 				goto process;
1372 			} else
1373 				rv = EWOULDBLOCK;
1374 		}
1375 		mtx_unlock(&sc->sc_queue_mtx);
1376 process:
1377 		if (ep != NULL) {
1378 			g_raid_handle_event(sc, ep);
1379 		} else if (bp != NULL) {
1380 			if (bp->bio_to != NULL &&
1381 			    bp->bio_to->geom == sc->sc_geom)
1382 				g_raid_start_request(bp);
1383 			else
1384 				g_raid_disk_done_request(bp);
1385 		} else if (rv == EWOULDBLOCK) {
1386 			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
1387 				if (vol->v_writes == 0 && vol->v_dirty)
1388 					g_raid_clean(vol, -1);
1389 				if (bioq_first(&vol->v_inflight) == NULL &&
1390 				    vol->v_tr) {
1391 					t.tv_sec = g_raid_idle_threshold / 1000000;
1392 					t.tv_usec = g_raid_idle_threshold % 1000000;
1393 					timevaladd(&t, &vol->v_last_done);
1394 					getmicrouptime(&now);
1395 					if (timevalcmp(&t, &now, <= )) {
1396 						G_RAID_TR_IDLE(vol->v_tr);
1397 						vol->v_last_done = now;
1398 					}
1399 				}
1400 			}
1401 		}
1402 		if (sc->sc_stopping == G_RAID_DESTROY_HARD)
1403 			g_raid_destroy_node(sc, 1);	/* May not return. */
1404 	}
1405 }
1406 
1407 static void
1408 g_raid_poll(struct g_raid_softc *sc)
1409 {
1410 	struct g_raid_event *ep;
1411 	struct bio *bp;
1412 
1413 	sx_xlock(&sc->sc_lock);
1414 	mtx_lock(&sc->sc_queue_mtx);
1415 	/*
1416 	 * First take a look at events.
1417 	 * This is important to handle events before any I/O requests.
1418 	 */
1419 	ep = TAILQ_FIRST(&sc->sc_events);
1420 	if (ep != NULL) {
1421 		TAILQ_REMOVE(&sc->sc_events, ep, e_next);
1422 		mtx_unlock(&sc->sc_queue_mtx);
1423 		g_raid_handle_event(sc, ep);
1424 		goto out;
1425 	}
1426 	bp = bioq_takefirst(&sc->sc_queue);
1427 	if (bp != NULL) {
1428 		mtx_unlock(&sc->sc_queue_mtx);
1429 		if (bp->bio_from == NULL ||
1430 		    bp->bio_from->geom != sc->sc_geom)
1431 			g_raid_start_request(bp);
1432 		else
1433 			g_raid_disk_done_request(bp);
1434 	}
1435 out:
1436 	sx_xunlock(&sc->sc_lock);
1437 }
1438 
1439 static void
1440 g_raid_launch_provider(struct g_raid_volume *vol)
1441 {
1442 	struct g_raid_disk *disk;
1443 	struct g_raid_softc *sc;
1444 	struct g_provider *pp;
1445 	char name[G_RAID_MAX_VOLUMENAME];
1446 	off_t off;
1447 
1448 	sc = vol->v_softc;
1449 	sx_assert(&sc->sc_lock, SX_LOCKED);
1450 
1451 	g_topology_lock();
1452 	/* Try to name provider with volume name. */
1453 	snprintf(name, sizeof(name), "raid/%s", vol->v_name);
1454 	if (g_raid_name_format == 0 || vol->v_name[0] == 0 ||
1455 	    g_provider_by_name(name) != NULL) {
1456 		/* Otherwise use sequential volume number. */
1457 		snprintf(name, sizeof(name), "raid/r%d", vol->v_global_id);
1458 	}
1459 	pp = g_new_providerf(sc->sc_geom, "%s", name);
1460 	pp->private = vol;
1461 	pp->mediasize = vol->v_mediasize;
1462 	pp->sectorsize = vol->v_sectorsize;
1463 	pp->stripesize = 0;
1464 	pp->stripeoffset = 0;
1465 	if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID1 ||
1466 	    vol->v_raid_level == G_RAID_VOLUME_RL_RAID3 ||
1467 	    vol->v_raid_level == G_RAID_VOLUME_RL_SINGLE ||
1468 	    vol->v_raid_level == G_RAID_VOLUME_RL_CONCAT) {
1469 		if ((disk = vol->v_subdisks[0].sd_disk) != NULL &&
1470 		    disk->d_consumer != NULL &&
1471 		    disk->d_consumer->provider != NULL) {
1472 			pp->stripesize = disk->d_consumer->provider->stripesize;
1473 			off = disk->d_consumer->provider->stripeoffset;
1474 			pp->stripeoffset = off + vol->v_subdisks[0].sd_offset;
1475 			if (off > 0)
1476 				pp->stripeoffset %= off;
1477 		}
1478 		if (vol->v_raid_level == G_RAID_VOLUME_RL_RAID3) {
1479 			pp->stripesize *= (vol->v_disks_count - 1);
1480 			pp->stripeoffset *= (vol->v_disks_count - 1);
1481 		}
1482 	} else
1483 		pp->stripesize = vol->v_strip_size;
1484 	vol->v_provider = pp;
1485 	g_error_provider(pp, 0);
1486 	g_topology_unlock();
1487 	G_RAID_DEBUG1(0, sc, "Provider %s for volume %s created.",
1488 	    pp->name, vol->v_name);
1489 }
1490 
1491 static void
1492 g_raid_destroy_provider(struct g_raid_volume *vol)
1493 {
1494 	struct g_raid_softc *sc;
1495 	struct g_provider *pp;
1496 	struct bio *bp, *tmp;
1497 
1498 	g_topology_assert_not();
1499 	sc = vol->v_softc;
1500 	pp = vol->v_provider;
1501 	KASSERT(pp != NULL, ("NULL provider (volume=%s).", vol->v_name));
1502 
1503 	g_topology_lock();
1504 	g_error_provider(pp, ENXIO);
1505 	mtx_lock(&sc->sc_queue_mtx);
1506 	TAILQ_FOREACH_SAFE(bp, &sc->sc_queue.queue, bio_queue, tmp) {
1507 		if (bp->bio_to != pp)
1508 			continue;
1509 		bioq_remove(&sc->sc_queue, bp);
1510 		g_io_deliver(bp, ENXIO);
1511 	}
1512 	mtx_unlock(&sc->sc_queue_mtx);
1513 	G_RAID_DEBUG1(0, sc, "Provider %s for volume %s destroyed.",
1514 	    pp->name, vol->v_name);
1515 	g_wither_provider(pp, ENXIO);
1516 	g_topology_unlock();
1517 	vol->v_provider = NULL;
1518 }
1519 
1520 /*
1521  * Update device state.
1522  */
1523 static int
1524 g_raid_update_volume(struct g_raid_volume *vol, u_int event)
1525 {
1526 	struct g_raid_softc *sc;
1527 
1528 	sc = vol->v_softc;
1529 	sx_assert(&sc->sc_lock, SX_XLOCKED);
1530 
1531 	G_RAID_DEBUG1(2, sc, "Event %s for volume %s.",
1532 	    g_raid_volume_event2str(event),
1533 	    vol->v_name);
1534 	switch (event) {
1535 	case G_RAID_VOLUME_E_DOWN:
1536 		if (vol->v_provider != NULL)
1537 			g_raid_destroy_provider(vol);
1538 		break;
1539 	case G_RAID_VOLUME_E_UP:
1540 		if (vol->v_provider == NULL)
1541 			g_raid_launch_provider(vol);
1542 		break;
1543 	case G_RAID_VOLUME_E_START:
1544 		if (vol->v_tr)
1545 			G_RAID_TR_START(vol->v_tr);
1546 		return (0);
1547 	default:
1548 		if (sc->sc_md)
1549 			G_RAID_MD_VOLUME_EVENT(sc->sc_md, vol, event);
1550 		return (0);
1551 	}
1552 
1553 	/* Manage root mount release. */
1554 	if (vol->v_starting) {
1555 		vol->v_starting = 0;
1556 		G_RAID_DEBUG1(1, sc, "root_mount_rel %p", vol->v_rootmount);
1557 		root_mount_rel(vol->v_rootmount);
1558 		vol->v_rootmount = NULL;
1559 	}
1560 	if (vol->v_stopping && vol->v_provider_open == 0)
1561 		g_raid_destroy_volume(vol);
1562 	return (0);
1563 }
1564 
1565 /*
1566  * Update subdisk state.
1567  */
1568 static int
1569 g_raid_update_subdisk(struct g_raid_subdisk *sd, u_int event)
1570 {
1571 	struct g_raid_softc *sc;
1572 	struct g_raid_volume *vol;
1573 
1574 	sc = sd->sd_softc;
1575 	vol = sd->sd_volume;
1576 	sx_assert(&sc->sc_lock, SX_XLOCKED);
1577 
1578 	G_RAID_DEBUG1(2, sc, "Event %s for subdisk %s:%d-%s.",
1579 	    g_raid_subdisk_event2str(event),
1580 	    vol->v_name, sd->sd_pos,
1581 	    sd->sd_disk ? g_raid_get_diskname(sd->sd_disk) : "[none]");
1582 	if (vol->v_tr)
1583 		G_RAID_TR_EVENT(vol->v_tr, sd, event);
1584 
1585 	return (0);
1586 }
1587 
1588 /*
1589  * Update disk state.
1590  */
1591 static int
1592 g_raid_update_disk(struct g_raid_disk *disk, u_int event)
1593 {
1594 	struct g_raid_softc *sc;
1595 
1596 	sc = disk->d_softc;
1597 	sx_assert(&sc->sc_lock, SX_XLOCKED);
1598 
1599 	G_RAID_DEBUG1(2, sc, "Event %s for disk %s.",
1600 	    g_raid_disk_event2str(event),
1601 	    g_raid_get_diskname(disk));
1602 
1603 	if (sc->sc_md)
1604 		G_RAID_MD_EVENT(sc->sc_md, disk, event);
1605 	return (0);
1606 }
1607 
1608 /*
1609  * Node event.
1610  */
1611 static int
1612 g_raid_update_node(struct g_raid_softc *sc, u_int event)
1613 {
1614 	sx_assert(&sc->sc_lock, SX_XLOCKED);
1615 
1616 	G_RAID_DEBUG1(2, sc, "Event %s for the array.",
1617 	    g_raid_node_event2str(event));
1618 
1619 	if (event == G_RAID_NODE_E_WAKE)
1620 		return (0);
1621 	if (sc->sc_md)
1622 		G_RAID_MD_EVENT(sc->sc_md, NULL, event);
1623 	return (0);
1624 }
1625 
1626 static int
1627 g_raid_access(struct g_provider *pp, int acr, int acw, int ace)
1628 {
1629 	struct g_raid_volume *vol;
1630 	struct g_raid_softc *sc;
1631 	int dcw, opens, error = 0;
1632 
1633 	g_topology_assert();
1634 	sc = pp->geom->softc;
1635 	vol = pp->private;
1636 	KASSERT(sc != NULL, ("NULL softc (provider=%s).", pp->name));
1637 	KASSERT(vol != NULL, ("NULL volume (provider=%s).", pp->name));
1638 
1639 	G_RAID_DEBUG1(2, sc, "Access request for %s: r%dw%de%d.", pp->name,
1640 	    acr, acw, ace);
1641 	dcw = pp->acw + acw;
1642 
1643 	g_topology_unlock();
1644 	sx_xlock(&sc->sc_lock);
1645 	/* Deny new opens while dying. */
1646 	if (sc->sc_stopping != 0 && (acr > 0 || acw > 0 || ace > 0)) {
1647 		error = ENXIO;
1648 		goto out;
1649 	}
1650 	if (dcw == 0 && vol->v_dirty)
1651 		g_raid_clean(vol, dcw);
1652 	vol->v_provider_open += acr + acw + ace;
1653 	/* Handle delayed node destruction. */
1654 	if (sc->sc_stopping == G_RAID_DESTROY_DELAYED &&
1655 	    vol->v_provider_open == 0) {
1656 		/* Count open volumes. */
1657 		opens = g_raid_nopens(sc);
1658 		if (opens == 0) {
1659 			sc->sc_stopping = G_RAID_DESTROY_HARD;
1660 			/* Wake up worker to make it selfdestruct. */
1661 			g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
1662 		}
1663 	}
1664 	/* Handle open volume destruction. */
1665 	if (vol->v_stopping && vol->v_provider_open == 0)
1666 		g_raid_destroy_volume(vol);
1667 out:
1668 	sx_xunlock(&sc->sc_lock);
1669 	g_topology_lock();
1670 	return (error);
1671 }
1672 
1673 struct g_raid_softc *
1674 g_raid_create_node(struct g_class *mp,
1675     const char *name, struct g_raid_md_object *md)
1676 {
1677 	struct g_raid_softc *sc;
1678 	struct g_geom *gp;
1679 	int error;
1680 
1681 	g_topology_assert();
1682 	G_RAID_DEBUG(1, "Creating array %s.", name);
1683 
1684 	gp = g_new_geomf(mp, "%s", name);
1685 	sc = malloc(sizeof(*sc), M_RAID, M_WAITOK | M_ZERO);
1686 	gp->start = g_raid_start;
1687 	gp->orphan = g_raid_orphan;
1688 	gp->access = g_raid_access;
1689 	gp->dumpconf = g_raid_dumpconf;
1690 
1691 	sc->sc_md = md;
1692 	sc->sc_geom = gp;
1693 	sc->sc_flags = 0;
1694 	TAILQ_INIT(&sc->sc_volumes);
1695 	TAILQ_INIT(&sc->sc_disks);
1696 	sx_init(&sc->sc_lock, "gmirror:lock");
1697 	mtx_init(&sc->sc_queue_mtx, "gmirror:queue", NULL, MTX_DEF);
1698 	TAILQ_INIT(&sc->sc_events);
1699 	bioq_init(&sc->sc_queue);
1700 	gp->softc = sc;
1701 	error = kproc_create(g_raid_worker, sc, &sc->sc_worker, 0, 0,
1702 	    "g_raid %s", name);
1703 	if (error != 0) {
1704 		G_RAID_DEBUG(0, "Cannot create kernel thread for %s.", name);
1705 		mtx_destroy(&sc->sc_queue_mtx);
1706 		sx_destroy(&sc->sc_lock);
1707 		g_destroy_geom(sc->sc_geom);
1708 		free(sc, M_RAID);
1709 		return (NULL);
1710 	}
1711 
1712 	G_RAID_DEBUG1(0, sc, "Array %s created.", name);
1713 	return (sc);
1714 }
1715 
1716 struct g_raid_volume *
1717 g_raid_create_volume(struct g_raid_softc *sc, const char *name, int id)
1718 {
1719 	struct g_raid_volume	*vol, *vol1;
1720 	int i;
1721 
1722 	G_RAID_DEBUG1(1, sc, "Creating volume %s.", name);
1723 	vol = malloc(sizeof(*vol), M_RAID, M_WAITOK | M_ZERO);
1724 	vol->v_softc = sc;
1725 	strlcpy(vol->v_name, name, G_RAID_MAX_VOLUMENAME);
1726 	vol->v_state = G_RAID_VOLUME_S_STARTING;
1727 	vol->v_raid_level = G_RAID_VOLUME_RL_UNKNOWN;
1728 	vol->v_raid_level_qualifier = G_RAID_VOLUME_RLQ_UNKNOWN;
1729 	bioq_init(&vol->v_inflight);
1730 	bioq_init(&vol->v_locked);
1731 	LIST_INIT(&vol->v_locks);
1732 	for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
1733 		vol->v_subdisks[i].sd_softc = sc;
1734 		vol->v_subdisks[i].sd_volume = vol;
1735 		vol->v_subdisks[i].sd_pos = i;
1736 		vol->v_subdisks[i].sd_state = G_RAID_DISK_S_NONE;
1737 	}
1738 
1739 	/* Find free ID for this volume. */
1740 	g_topology_lock();
1741 	vol1 = vol;
1742 	if (id >= 0) {
1743 		LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1744 			if (vol1->v_global_id == id)
1745 				break;
1746 		}
1747 	}
1748 	if (vol1 != NULL) {
1749 		for (id = 0; ; id++) {
1750 			LIST_FOREACH(vol1, &g_raid_volumes, v_global_next) {
1751 				if (vol1->v_global_id == id)
1752 					break;
1753 			}
1754 			if (vol1 == NULL)
1755 				break;
1756 		}
1757 	}
1758 	vol->v_global_id = id;
1759 	LIST_INSERT_HEAD(&g_raid_volumes, vol, v_global_next);
1760 	g_topology_unlock();
1761 
1762 	/* Delay root mounting. */
1763 	vol->v_rootmount = root_mount_hold("GRAID");
1764 	G_RAID_DEBUG1(1, sc, "root_mount_hold %p", vol->v_rootmount);
1765 	vol->v_starting = 1;
1766 	TAILQ_INSERT_TAIL(&sc->sc_volumes, vol, v_next);
1767 	return (vol);
1768 }
1769 
1770 struct g_raid_disk *
1771 g_raid_create_disk(struct g_raid_softc *sc)
1772 {
1773 	struct g_raid_disk	*disk;
1774 
1775 	G_RAID_DEBUG1(1, sc, "Creating disk.");
1776 	disk = malloc(sizeof(*disk), M_RAID, M_WAITOK | M_ZERO);
1777 	disk->d_softc = sc;
1778 	disk->d_state = G_RAID_DISK_S_NONE;
1779 	TAILQ_INIT(&disk->d_subdisks);
1780 	TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
1781 	return (disk);
1782 }
1783 
1784 int g_raid_start_volume(struct g_raid_volume *vol)
1785 {
1786 	struct g_raid_tr_class *class;
1787 	struct g_raid_tr_object *obj;
1788 	int status;
1789 
1790 	G_RAID_DEBUG1(2, vol->v_softc, "Starting volume %s.", vol->v_name);
1791 	LIST_FOREACH(class, &g_raid_tr_classes, trc_list) {
1792 		G_RAID_DEBUG1(2, vol->v_softc,
1793 		    "Tasting volume %s for %s transformation.",
1794 		    vol->v_name, class->name);
1795 		obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
1796 		    M_WAITOK);
1797 		obj->tro_class = class;
1798 		obj->tro_volume = vol;
1799 		status = G_RAID_TR_TASTE(obj, vol);
1800 		if (status != G_RAID_TR_TASTE_FAIL)
1801 			break;
1802 		kobj_delete((kobj_t)obj, M_RAID);
1803 	}
1804 	if (class == NULL) {
1805 		G_RAID_DEBUG1(0, vol->v_softc,
1806 		    "No transformation module found for %s.",
1807 		    vol->v_name);
1808 		vol->v_tr = NULL;
1809 		g_raid_change_volume_state(vol, G_RAID_VOLUME_S_UNSUPPORTED);
1810 		g_raid_event_send(vol, G_RAID_VOLUME_E_DOWN,
1811 		    G_RAID_EVENT_VOLUME);
1812 		return (-1);
1813 	}
1814 	G_RAID_DEBUG1(2, vol->v_softc,
1815 	    "Transformation module %s chosen for %s.",
1816 	    class->name, vol->v_name);
1817 	vol->v_tr = obj;
1818 	return (0);
1819 }
1820 
1821 int
1822 g_raid_destroy_node(struct g_raid_softc *sc, int worker)
1823 {
1824 	struct g_raid_volume *vol, *tmpv;
1825 	struct g_raid_disk *disk, *tmpd;
1826 	int error = 0;
1827 
1828 	sc->sc_stopping = G_RAID_DESTROY_HARD;
1829 	TAILQ_FOREACH_SAFE(vol, &sc->sc_volumes, v_next, tmpv) {
1830 		if (g_raid_destroy_volume(vol))
1831 			error = EBUSY;
1832 	}
1833 	if (error)
1834 		return (error);
1835 	TAILQ_FOREACH_SAFE(disk, &sc->sc_disks, d_next, tmpd) {
1836 		if (g_raid_destroy_disk(disk))
1837 			error = EBUSY;
1838 	}
1839 	if (error)
1840 		return (error);
1841 	if (sc->sc_md) {
1842 		G_RAID_MD_FREE(sc->sc_md);
1843 		kobj_delete((kobj_t)sc->sc_md, M_RAID);
1844 		sc->sc_md = NULL;
1845 	}
1846 	if (sc->sc_geom != NULL) {
1847 		G_RAID_DEBUG1(0, sc, "Array %s destroyed.", sc->sc_name);
1848 		g_topology_lock();
1849 		sc->sc_geom->softc = NULL;
1850 		g_wither_geom(sc->sc_geom, ENXIO);
1851 		g_topology_unlock();
1852 		sc->sc_geom = NULL;
1853 	} else
1854 		G_RAID_DEBUG(1, "Array destroyed.");
1855 	if (worker) {
1856 		g_raid_event_cancel(sc, sc);
1857 		mtx_destroy(&sc->sc_queue_mtx);
1858 		sx_xunlock(&sc->sc_lock);
1859 		sx_destroy(&sc->sc_lock);
1860 		wakeup(&sc->sc_stopping);
1861 		free(sc, M_RAID);
1862 		curthread->td_pflags &= ~TDP_GEOM;
1863 		G_RAID_DEBUG(1, "Thread exiting.");
1864 		kproc_exit(0);
1865 	} else {
1866 		/* Wake up worker to make it selfdestruct. */
1867 		g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
1868 	}
1869 	return (0);
1870 }
1871 
1872 int
1873 g_raid_destroy_volume(struct g_raid_volume *vol)
1874 {
1875 	struct g_raid_softc *sc;
1876 	struct g_raid_disk *disk;
1877 	int i;
1878 
1879 	sc = vol->v_softc;
1880 	G_RAID_DEBUG1(2, sc, "Destroying volume %s.", vol->v_name);
1881 	vol->v_stopping = 1;
1882 	if (vol->v_state != G_RAID_VOLUME_S_STOPPED) {
1883 		if (vol->v_tr) {
1884 			G_RAID_TR_STOP(vol->v_tr);
1885 			return (EBUSY);
1886 		} else
1887 			vol->v_state = G_RAID_VOLUME_S_STOPPED;
1888 	}
1889 	if (g_raid_event_check(sc, vol) != 0)
1890 		return (EBUSY);
1891 	if (vol->v_provider != NULL)
1892 		return (EBUSY);
1893 	if (vol->v_provider_open != 0)
1894 		return (EBUSY);
1895 	if (vol->v_tr) {
1896 		G_RAID_TR_FREE(vol->v_tr);
1897 		kobj_delete((kobj_t)vol->v_tr, M_RAID);
1898 		vol->v_tr = NULL;
1899 	}
1900 	if (vol->v_rootmount)
1901 		root_mount_rel(vol->v_rootmount);
1902 	g_topology_lock();
1903 	LIST_REMOVE(vol, v_global_next);
1904 	g_topology_unlock();
1905 	TAILQ_REMOVE(&sc->sc_volumes, vol, v_next);
1906 	for (i = 0; i < G_RAID_MAX_SUBDISKS; i++) {
1907 		g_raid_event_cancel(sc, &vol->v_subdisks[i]);
1908 		disk = vol->v_subdisks[i].sd_disk;
1909 		if (disk == NULL)
1910 			continue;
1911 		TAILQ_REMOVE(&disk->d_subdisks, &vol->v_subdisks[i], sd_next);
1912 	}
1913 	G_RAID_DEBUG1(2, sc, "Volume %s destroyed.", vol->v_name);
1914 	if (sc->sc_md)
1915 		G_RAID_MD_FREE_VOLUME(sc->sc_md, vol);
1916 	g_raid_event_cancel(sc, vol);
1917 	free(vol, M_RAID);
1918 	if (sc->sc_stopping == G_RAID_DESTROY_HARD) {
1919 		/* Wake up worker to let it selfdestruct. */
1920 		g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
1921 	}
1922 	return (0);
1923 }
1924 
1925 int
1926 g_raid_destroy_disk(struct g_raid_disk *disk)
1927 {
1928 	struct g_raid_softc *sc;
1929 	struct g_raid_subdisk *sd, *tmp;
1930 
1931 	sc = disk->d_softc;
1932 	G_RAID_DEBUG1(2, sc, "Destroying disk.");
1933 	if (disk->d_consumer) {
1934 		g_raid_kill_consumer(sc, disk->d_consumer);
1935 		disk->d_consumer = NULL;
1936 	}
1937 	TAILQ_FOREACH_SAFE(sd, &disk->d_subdisks, sd_next, tmp) {
1938 		g_raid_change_subdisk_state(sd, G_RAID_SUBDISK_S_NONE);
1939 		g_raid_event_send(sd, G_RAID_SUBDISK_E_DISCONNECTED,
1940 		    G_RAID_EVENT_SUBDISK);
1941 		TAILQ_REMOVE(&disk->d_subdisks, sd, sd_next);
1942 		sd->sd_disk = NULL;
1943 	}
1944 	TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
1945 	if (sc->sc_md)
1946 		G_RAID_MD_FREE_DISK(sc->sc_md, disk);
1947 	g_raid_event_cancel(sc, disk);
1948 	free(disk, M_RAID);
1949 	return (0);
1950 }
1951 
1952 int
1953 g_raid_destroy(struct g_raid_softc *sc, int how)
1954 {
1955 	int opens;
1956 
1957 	g_topology_assert_not();
1958 	if (sc == NULL)
1959 		return (ENXIO);
1960 	sx_assert(&sc->sc_lock, SX_XLOCKED);
1961 
1962 	/* Count open volumes. */
1963 	opens = g_raid_nopens(sc);
1964 
1965 	/* React on some opened volumes. */
1966 	if (opens > 0) {
1967 		switch (how) {
1968 		case G_RAID_DESTROY_SOFT:
1969 			G_RAID_DEBUG1(1, sc,
1970 			    "%d volumes are still open.",
1971 			    opens);
1972 			return (EBUSY);
1973 		case G_RAID_DESTROY_DELAYED:
1974 			G_RAID_DEBUG1(1, sc,
1975 			    "Array will be destroyed on last close.");
1976 			sc->sc_stopping = G_RAID_DESTROY_DELAYED;
1977 			return (EBUSY);
1978 		case G_RAID_DESTROY_HARD:
1979 			G_RAID_DEBUG1(1, sc,
1980 			    "%d volumes are still open.",
1981 			    opens);
1982 		}
1983 	}
1984 
1985 	/* Mark node for destruction. */
1986 	sc->sc_stopping = G_RAID_DESTROY_HARD;
1987 	/* Wake up worker to let it selfdestruct. */
1988 	g_raid_event_send(sc, G_RAID_NODE_E_WAKE, 0);
1989 	/* Sleep until node destroyed. */
1990 	sx_sleep(&sc->sc_stopping, &sc->sc_lock,
1991 	    PRIBIO | PDROP, "r:destroy", 0);
1992 	return (0);
1993 }
1994 
1995 static void
1996 g_raid_taste_orphan(struct g_consumer *cp)
1997 {
1998 
1999 	KASSERT(1 == 0, ("%s called while tasting %s.", __func__,
2000 	    cp->provider->name));
2001 }
2002 
2003 static struct g_geom *
2004 g_raid_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
2005 {
2006 	struct g_consumer *cp;
2007 	struct g_geom *gp, *geom;
2008 	struct g_raid_md_class *class;
2009 	struct g_raid_md_object *obj;
2010 	int status;
2011 
2012 	g_topology_assert();
2013 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
2014 	G_RAID_DEBUG(2, "Tasting provider %s.", pp->name);
2015 
2016 	gp = g_new_geomf(mp, "mirror:taste");
2017 	/*
2018 	 * This orphan function should be never called.
2019 	 */
2020 	gp->orphan = g_raid_taste_orphan;
2021 	cp = g_new_consumer(gp);
2022 	g_attach(cp, pp);
2023 
2024 	geom = NULL;
2025 	LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2026 		G_RAID_DEBUG(2, "Tasting provider %s for %s metadata.",
2027 		    pp->name, class->name);
2028 		obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2029 		    M_WAITOK);
2030 		obj->mdo_class = class;
2031 		status = G_RAID_MD_TASTE(obj, mp, cp, &geom);
2032 		if (status != G_RAID_MD_TASTE_NEW)
2033 			kobj_delete((kobj_t)obj, M_RAID);
2034 		if (status != G_RAID_MD_TASTE_FAIL)
2035 			break;
2036 	}
2037 
2038 	g_detach(cp);
2039 	g_destroy_consumer(cp);
2040 	g_destroy_geom(gp);
2041 	G_RAID_DEBUG(2, "Tasting provider %s done.", pp->name);
2042 	return (geom);
2043 }
2044 
2045 int
2046 g_raid_create_node_format(const char *format, struct g_geom **gp)
2047 {
2048 	struct g_raid_md_class *class;
2049 	struct g_raid_md_object *obj;
2050 	int status;
2051 
2052 	G_RAID_DEBUG(2, "Creating array for %s metadata.", format);
2053 	LIST_FOREACH(class, &g_raid_md_classes, mdc_list) {
2054 		if (strcasecmp(class->name, format) == 0)
2055 			break;
2056 	}
2057 	if (class == NULL) {
2058 		G_RAID_DEBUG(1, "No support for %s metadata.", format);
2059 		return (G_RAID_MD_TASTE_FAIL);
2060 	}
2061 	obj = (void *)kobj_create((kobj_class_t)class, M_RAID,
2062 	    M_WAITOK);
2063 	obj->mdo_class = class;
2064 	status = G_RAID_MD_CREATE(obj, &g_raid_class, gp);
2065 	if (status != G_RAID_MD_TASTE_NEW)
2066 		kobj_delete((kobj_t)obj, M_RAID);
2067 	return (status);
2068 }
2069 
2070 static int
2071 g_raid_destroy_geom(struct gctl_req *req __unused,
2072     struct g_class *mp __unused, struct g_geom *gp)
2073 {
2074 	struct g_raid_softc *sc;
2075 	int error;
2076 
2077 	g_topology_unlock();
2078 	sc = gp->softc;
2079 	sx_xlock(&sc->sc_lock);
2080 	g_cancel_event(sc);
2081 	error = g_raid_destroy(gp->softc, G_RAID_DESTROY_SOFT);
2082 	if (error != 0)
2083 		sx_xunlock(&sc->sc_lock);
2084 	g_topology_lock();
2085 	return (error);
2086 }
2087 
2088 void g_raid_write_metadata(struct g_raid_softc *sc, struct g_raid_volume *vol,
2089     struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2090 {
2091 
2092 	if (sc->sc_stopping == G_RAID_DESTROY_HARD)
2093 		return;
2094 	if (sc->sc_md)
2095 		G_RAID_MD_WRITE(sc->sc_md, vol, sd, disk);
2096 }
2097 
2098 void g_raid_fail_disk(struct g_raid_softc *sc,
2099     struct g_raid_subdisk *sd, struct g_raid_disk *disk)
2100 {
2101 
2102 	if (disk == NULL)
2103 		disk = sd->sd_disk;
2104 	if (disk == NULL) {
2105 		G_RAID_DEBUG1(0, sc, "Warning! Fail request to an absent disk!");
2106 		return;
2107 	}
2108 	if (disk->d_state != G_RAID_DISK_S_ACTIVE) {
2109 		G_RAID_DEBUG1(0, sc, "Warning! Fail request to a disk in a "
2110 		    "wrong state (%s)!", g_raid_disk_state2str(disk->d_state));
2111 		return;
2112 	}
2113 	if (sc->sc_md)
2114 		G_RAID_MD_FAIL_DISK(sc->sc_md, sd, disk);
2115 }
2116 
2117 static void
2118 g_raid_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
2119     struct g_consumer *cp, struct g_provider *pp)
2120 {
2121 	struct g_raid_softc *sc;
2122 	struct g_raid_volume *vol;
2123 	struct g_raid_subdisk *sd;
2124 	struct g_raid_disk *disk;
2125 	int i, s;
2126 
2127 	g_topology_assert();
2128 
2129 	sc = gp->softc;
2130 	if (sc == NULL)
2131 		return;
2132 	if (pp != NULL) {
2133 		vol = pp->private;
2134 		g_topology_unlock();
2135 		sx_xlock(&sc->sc_lock);
2136 		sbuf_printf(sb, "%s<Label>%s</Label>\n", indent,
2137 		    vol->v_name);
2138 		sbuf_printf(sb, "%s<RAIDLevel>%s</RAIDLevel>\n", indent,
2139 		    g_raid_volume_level2str(vol->v_raid_level,
2140 		    vol->v_raid_level_qualifier));
2141 		sbuf_printf(sb,
2142 		    "%s<Transformation>%s</Transformation>\n", indent,
2143 		    vol->v_tr ? vol->v_tr->tro_class->name : "NONE");
2144 		sbuf_printf(sb, "%s<Components>%u</Components>\n", indent,
2145 		    vol->v_disks_count);
2146 		sbuf_printf(sb, "%s<Strip>%u</Strip>\n", indent,
2147 		    vol->v_strip_size);
2148 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2149 		    g_raid_volume_state2str(vol->v_state));
2150 		sbuf_printf(sb, "%s<Dirty>%s</Dirty>\n", indent,
2151 		    vol->v_dirty ? "Yes" : "No");
2152 		sbuf_printf(sb, "%s<Subdisks>", indent);
2153 		for (i = 0; i < vol->v_disks_count; i++) {
2154 			sd = &vol->v_subdisks[i];
2155 			if (sd->sd_disk != NULL &&
2156 			    sd->sd_disk->d_consumer != NULL) {
2157 				sbuf_printf(sb, "%s ",
2158 				    g_raid_get_diskname(sd->sd_disk));
2159 			} else {
2160 				sbuf_printf(sb, "NONE ");
2161 			}
2162 			sbuf_printf(sb, "(%s",
2163 			    g_raid_subdisk_state2str(sd->sd_state));
2164 			if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2165 			    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2166 				sbuf_printf(sb, " %d%%",
2167 				    (int)(sd->sd_rebuild_pos * 100 /
2168 				     sd->sd_size));
2169 			}
2170 			sbuf_printf(sb, ")");
2171 			if (i + 1 < vol->v_disks_count)
2172 				sbuf_printf(sb, ", ");
2173 		}
2174 		sbuf_printf(sb, "</Subdisks>\n");
2175 		sx_xunlock(&sc->sc_lock);
2176 		g_topology_lock();
2177 	} else if (cp != NULL) {
2178 		disk = cp->private;
2179 		if (disk == NULL)
2180 			return;
2181 		g_topology_unlock();
2182 		sx_xlock(&sc->sc_lock);
2183 		sbuf_printf(sb, "%s<State>%s", indent,
2184 		    g_raid_disk_state2str(disk->d_state));
2185 		if (!TAILQ_EMPTY(&disk->d_subdisks)) {
2186 			sbuf_printf(sb, " (");
2187 			TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2188 				sbuf_printf(sb, "%s",
2189 				    g_raid_subdisk_state2str(sd->sd_state));
2190 				if (sd->sd_state == G_RAID_SUBDISK_S_REBUILD ||
2191 				    sd->sd_state == G_RAID_SUBDISK_S_RESYNC) {
2192 					sbuf_printf(sb, " %d%%",
2193 					    (int)(sd->sd_rebuild_pos * 100 /
2194 					     sd->sd_size));
2195 				}
2196 				if (TAILQ_NEXT(sd, sd_next))
2197 					sbuf_printf(sb, ", ");
2198 			}
2199 			sbuf_printf(sb, ")");
2200 		}
2201 		sbuf_printf(sb, "</State>\n");
2202 		sbuf_printf(sb, "%s<Subdisks>", indent);
2203 		TAILQ_FOREACH(sd, &disk->d_subdisks, sd_next) {
2204 			sbuf_printf(sb, "r%d(%s):%d@%ju",
2205 			    sd->sd_volume->v_global_id,
2206 			    sd->sd_volume->v_name,
2207 			    sd->sd_pos, sd->sd_offset);
2208 			if (TAILQ_NEXT(sd, sd_next))
2209 				sbuf_printf(sb, ", ");
2210 		}
2211 		sbuf_printf(sb, "</Subdisks>\n");
2212 		sbuf_printf(sb, "%s<ReadErrors>%d</ReadErrors>\n", indent,
2213 		    disk->d_read_errs);
2214 		sx_xunlock(&sc->sc_lock);
2215 		g_topology_lock();
2216 	} else {
2217 		g_topology_unlock();
2218 		sx_xlock(&sc->sc_lock);
2219 		if (sc->sc_md) {
2220 			sbuf_printf(sb, "%s<Metadata>%s</Metadata>\n", indent,
2221 			    sc->sc_md->mdo_class->name);
2222 		}
2223 		if (!TAILQ_EMPTY(&sc->sc_volumes)) {
2224 			s = 0xff;
2225 			TAILQ_FOREACH(vol, &sc->sc_volumes, v_next) {
2226 				if (vol->v_state < s)
2227 					s = vol->v_state;
2228 			}
2229 			sbuf_printf(sb, "%s<State>%s</State>\n", indent,
2230 			    g_raid_volume_state2str(s));
2231 		}
2232 		sx_xunlock(&sc->sc_lock);
2233 		g_topology_lock();
2234 	}
2235 }
2236 
2237 static void
2238 g_raid_shutdown_pre_sync(void *arg, int howto)
2239 {
2240 	struct g_class *mp;
2241 	struct g_geom *gp, *gp2;
2242 	struct g_raid_softc *sc;
2243 	int error;
2244 
2245 	mp = arg;
2246 	DROP_GIANT();
2247 	g_topology_lock();
2248 	LIST_FOREACH_SAFE(gp, &mp->geom, geom, gp2) {
2249 		if ((sc = gp->softc) == NULL)
2250 			continue;
2251 		g_topology_unlock();
2252 		sx_xlock(&sc->sc_lock);
2253 		g_cancel_event(sc);
2254 		error = g_raid_destroy(sc, G_RAID_DESTROY_DELAYED);
2255 		if (error != 0)
2256 			sx_xunlock(&sc->sc_lock);
2257 		g_topology_lock();
2258 	}
2259 	g_topology_unlock();
2260 	PICKUP_GIANT();
2261 }
2262 
2263 static void
2264 g_raid_init(struct g_class *mp)
2265 {
2266 
2267 	g_raid_pre_sync = EVENTHANDLER_REGISTER(shutdown_pre_sync,
2268 	    g_raid_shutdown_pre_sync, mp, SHUTDOWN_PRI_FIRST);
2269 	if (g_raid_pre_sync == NULL)
2270 		G_RAID_DEBUG(0, "Warning! Cannot register shutdown event.");
2271 	g_raid_started = 1;
2272 }
2273 
2274 static void
2275 g_raid_fini(struct g_class *mp)
2276 {
2277 
2278 	if (g_raid_pre_sync != NULL)
2279 		EVENTHANDLER_DEREGISTER(shutdown_pre_sync, g_raid_pre_sync);
2280 	g_raid_started = 0;
2281 }
2282 
2283 int
2284 g_raid_md_modevent(module_t mod, int type, void *arg)
2285 {
2286 	struct g_raid_md_class *class, *c, *nc;
2287 	int error;
2288 
2289 	error = 0;
2290 	class = arg;
2291 	switch (type) {
2292 	case MOD_LOAD:
2293 		c = LIST_FIRST(&g_raid_md_classes);
2294 		if (c == NULL || c->mdc_priority > class->mdc_priority)
2295 			LIST_INSERT_HEAD(&g_raid_md_classes, class, mdc_list);
2296 		else {
2297 			while ((nc = LIST_NEXT(c, mdc_list)) != NULL &&
2298 			    nc->mdc_priority < class->mdc_priority)
2299 				c = nc;
2300 			LIST_INSERT_AFTER(c, class, mdc_list);
2301 		}
2302 		if (g_raid_started)
2303 			g_retaste(&g_raid_class);
2304 		break;
2305 	case MOD_UNLOAD:
2306 		LIST_REMOVE(class, mdc_list);
2307 		break;
2308 	default:
2309 		error = EOPNOTSUPP;
2310 		break;
2311 	}
2312 
2313 	return (error);
2314 }
2315 
2316 int
2317 g_raid_tr_modevent(module_t mod, int type, void *arg)
2318 {
2319 	struct g_raid_tr_class *class, *c, *nc;
2320 	int error;
2321 
2322 	error = 0;
2323 	class = arg;
2324 	switch (type) {
2325 	case MOD_LOAD:
2326 		c = LIST_FIRST(&g_raid_tr_classes);
2327 		if (c == NULL || c->trc_priority > class->trc_priority)
2328 			LIST_INSERT_HEAD(&g_raid_tr_classes, class, trc_list);
2329 		else {
2330 			while ((nc = LIST_NEXT(c, trc_list)) != NULL &&
2331 			    nc->trc_priority < class->trc_priority)
2332 				c = nc;
2333 			LIST_INSERT_AFTER(c, class, trc_list);
2334 		}
2335 		break;
2336 	case MOD_UNLOAD:
2337 		LIST_REMOVE(class, trc_list);
2338 		break;
2339 	default:
2340 		error = EOPNOTSUPP;
2341 		break;
2342 	}
2343 
2344 	return (error);
2345 }
2346 
2347 /*
2348  * Use local implementation of DECLARE_GEOM_CLASS(g_raid_class, g_raid)
2349  * to reduce module priority, allowing submodules to register them first.
2350  */
2351 static moduledata_t g_raid_mod = {
2352 	"g_raid",
2353 	g_modevent,
2354 	&g_raid_class
2355 };
2356 DECLARE_MODULE(g_raid, g_raid_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD);
2357 MODULE_VERSION(geom_raid, 0);
2358