xref: /freebsd/sys/geom/vinum/geom_vinum.c (revision 4c8945a06b01a5c8122cdeb402af36bb46a06acc)
1 /*-
2  *  Copyright (c) 2004, 2007 Lukas Ertl
3  *  Copyright (c) 2007, 2009 Ulf Lilleengen
4  *  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bio.h>
34 #include <sys/kernel.h>
35 #include <sys/kthread.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
41 #include <sys/systm.h>
42 
43 #include <geom/geom.h>
44 #include <geom/vinum/geom_vinum_var.h>
45 #include <geom/vinum/geom_vinum.h>
46 #include <geom/vinum/geom_vinum_raid5.h>
47 
48 SYSCTL_DECL(_kern_geom);
49 SYSCTL_NODE(_kern_geom, OID_AUTO, vinum, CTLFLAG_RW, 0, "GEOM_VINUM stuff");
50 u_int g_vinum_debug = 0;
51 TUNABLE_INT("kern.geom.vinum.debug", &g_vinum_debug);
52 SYSCTL_UINT(_kern_geom_vinum, OID_AUTO, debug, CTLFLAG_RW, &g_vinum_debug, 0,
53     "Debug level");
54 
55 static int	gv_create(struct g_geom *, struct gctl_req *);
56 static void	gv_attach(struct gv_softc *, struct gctl_req *);
57 static void	gv_detach(struct gv_softc *, struct gctl_req *);
58 static void	gv_parityop(struct gv_softc *, struct gctl_req *);
59 
60 
61 static void
62 gv_orphan(struct g_consumer *cp)
63 {
64 	struct g_geom *gp;
65 	struct gv_softc *sc;
66 	struct gv_drive *d;
67 
68 	g_topology_assert();
69 
70 	KASSERT(cp != NULL, ("gv_orphan: null cp"));
71 	gp = cp->geom;
72 	KASSERT(gp != NULL, ("gv_orphan: null gp"));
73 	sc = gp->softc;
74 	KASSERT(sc != NULL, ("gv_orphan: null sc"));
75 	d = cp->private;
76 	KASSERT(d != NULL, ("gv_orphan: null d"));
77 
78 	g_trace(G_T_TOPOLOGY, "gv_orphan(%s)", gp->name);
79 
80 	gv_post_event(sc, GV_EVENT_DRIVE_LOST, d, NULL, 0, 0);
81 }
82 
83 void
84 gv_start(struct bio *bp)
85 {
86 	struct g_geom *gp;
87 	struct gv_softc *sc;
88 
89 	gp = bp->bio_to->geom;
90 	sc = gp->softc;
91 
92 	switch (bp->bio_cmd) {
93 	case BIO_READ:
94 	case BIO_WRITE:
95 	case BIO_DELETE:
96 		break;
97 	case BIO_GETATTR:
98 	default:
99 		g_io_deliver(bp, EOPNOTSUPP);
100 		return;
101 	}
102 	mtx_lock(&sc->bqueue_mtx);
103 	bioq_disksort(sc->bqueue_down, bp);
104 	wakeup(sc);
105 	mtx_unlock(&sc->bqueue_mtx);
106 }
107 
108 void
109 gv_done(struct bio *bp)
110 {
111 	struct g_geom *gp;
112 	struct gv_softc *sc;
113 
114 	KASSERT(bp != NULL, ("NULL bp"));
115 
116 	gp = bp->bio_from->geom;
117 	sc = gp->softc;
118 
119 	mtx_lock(&sc->bqueue_mtx);
120 	bioq_disksort(sc->bqueue_up, bp);
121 	wakeup(sc);
122 	mtx_unlock(&sc->bqueue_mtx);
123 }
124 
125 int
126 gv_access(struct g_provider *pp, int dr, int dw, int de)
127 {
128 	struct g_geom *gp;
129 	struct gv_softc *sc;
130 	struct gv_drive *d, *d2;
131 	int error;
132 
133 	gp = pp->geom;
134 	sc = gp->softc;
135 	/*
136 	 * We want to modify the read count with the write count in case we have
137 	 * plexes in a RAID-5 organization.
138 	 */
139 	dr += dw;
140 
141 	LIST_FOREACH(d, &sc->drives, drive) {
142 		if (d->consumer == NULL)
143 			continue;
144 		error = g_access(d->consumer, dr, dw, de);
145 		if (error) {
146 			LIST_FOREACH(d2, &sc->drives, drive) {
147 				if (d == d2)
148 					break;
149 				g_access(d2->consumer, -dr, -dw, -de);
150 			}
151 			G_VINUM_DEBUG(0, "g_access '%s' failed: %d", d->name,
152 			    error);
153 			return (error);
154 		}
155 	}
156 	return (0);
157 }
158 
159 static void
160 gv_init(struct g_class *mp)
161 {
162 	struct g_geom *gp;
163 	struct gv_softc *sc;
164 
165 	g_trace(G_T_TOPOLOGY, "gv_init(%p)", mp);
166 
167 	gp = g_new_geomf(mp, "VINUM");
168 	gp->spoiled = gv_orphan;
169 	gp->orphan = gv_orphan;
170 	gp->access = gv_access;
171 	gp->start = gv_start;
172 	gp->softc = g_malloc(sizeof(struct gv_softc), M_WAITOK | M_ZERO);
173 	sc = gp->softc;
174 	sc->geom = gp;
175 	sc->bqueue_down = g_malloc(sizeof(struct bio_queue_head),
176 	    M_WAITOK | M_ZERO);
177 	sc->bqueue_up = g_malloc(sizeof(struct bio_queue_head),
178 	    M_WAITOK | M_ZERO);
179 	bioq_init(sc->bqueue_down);
180 	bioq_init(sc->bqueue_up);
181 	LIST_INIT(&sc->drives);
182 	LIST_INIT(&sc->subdisks);
183 	LIST_INIT(&sc->plexes);
184 	LIST_INIT(&sc->volumes);
185 	TAILQ_INIT(&sc->equeue);
186 	mtx_init(&sc->config_mtx, "gv_config", NULL, MTX_DEF);
187 	mtx_init(&sc->equeue_mtx, "gv_equeue", NULL, MTX_DEF);
188 	mtx_init(&sc->bqueue_mtx, "gv_bqueue", NULL, MTX_DEF);
189 	kproc_create(gv_worker, sc, &sc->worker, 0, 0, "gv_worker");
190 }
191 
192 static int
193 gv_unload(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
194 {
195 	struct gv_softc *sc;
196 
197 	g_trace(G_T_TOPOLOGY, "gv_unload(%p)", mp);
198 
199 	g_topology_assert();
200 	sc = gp->softc;
201 
202 	if (sc != NULL) {
203 		gv_worker_exit(sc);
204 		gp->softc = NULL;
205 		g_wither_geom(gp, ENXIO);
206 	}
207 
208 	return (0);
209 }
210 
211 /* Handle userland request of attaching object. */
212 static void
213 gv_attach(struct gv_softc *sc, struct gctl_req *req)
214 {
215 	struct gv_volume *v;
216 	struct gv_plex *p;
217 	struct gv_sd *s;
218 	off_t *offset;
219 	int *rename, type_child, type_parent;
220 	char *child, *parent;
221 
222 	child = gctl_get_param(req, "child", NULL);
223 	if (child == NULL) {
224 		gctl_error(req, "no child given");
225 		return;
226 	}
227 	parent = gctl_get_param(req, "parent", NULL);
228 	if (parent == NULL) {
229 		gctl_error(req, "no parent given");
230 		return;
231 	}
232 	offset = gctl_get_paraml(req, "offset", sizeof(*offset));
233 	if (offset == NULL) {
234 		gctl_error(req, "no offset given");
235 		return;
236 	}
237 	rename = gctl_get_paraml(req, "rename", sizeof(*rename));
238 	if (rename == NULL) {
239 		gctl_error(req, "no rename flag given");
240 		return;
241 	}
242 
243 	type_child = gv_object_type(sc, child);
244 	type_parent = gv_object_type(sc, parent);
245 
246 	switch (type_child) {
247 	case GV_TYPE_PLEX:
248 		if (type_parent != GV_TYPE_VOL) {
249 			gctl_error(req, "no such volume to attach to");
250 			return;
251 		}
252 		v = gv_find_vol(sc, parent);
253 		p = gv_find_plex(sc, child);
254 		gv_post_event(sc, GV_EVENT_ATTACH_PLEX, p, v, *offset, *rename);
255 		break;
256 	case GV_TYPE_SD:
257 		if (type_parent != GV_TYPE_PLEX) {
258 			gctl_error(req, "no such plex to attach to");
259 			return;
260 		}
261 		p = gv_find_plex(sc, parent);
262 		s = gv_find_sd(sc, child);
263 		gv_post_event(sc, GV_EVENT_ATTACH_SD, s, p, *offset, *rename);
264 		break;
265 	default:
266 		gctl_error(req, "invalid child type");
267 		break;
268 	}
269 }
270 
271 /* Handle userland request of detaching object. */
272 static void
273 gv_detach(struct gv_softc *sc, struct gctl_req *req)
274 {
275 	struct gv_plex *p;
276 	struct gv_sd *s;
277 	int *flags, type;
278 	char *object;
279 
280 	object = gctl_get_param(req, "object", NULL);
281 	if (object == NULL) {
282 		gctl_error(req, "no argument given");
283 		return;
284 	}
285 
286 	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
287 	type = gv_object_type(sc, object);
288 	switch (type) {
289 	case GV_TYPE_PLEX:
290 		p = gv_find_plex(sc, object);
291 		gv_post_event(sc, GV_EVENT_DETACH_PLEX, p, NULL, *flags, 0);
292 		break;
293 	case GV_TYPE_SD:
294 		s = gv_find_sd(sc, object);
295 		gv_post_event(sc, GV_EVENT_DETACH_SD, s, NULL, *flags, 0);
296 		break;
297 	default:
298 		gctl_error(req, "invalid object type");
299 		break;
300 	}
301 }
302 
303 /* Handle userland requests for creating new objects. */
304 static int
305 gv_create(struct g_geom *gp, struct gctl_req *req)
306 {
307 	struct gv_softc *sc;
308 	struct gv_drive *d, *d2;
309 	struct gv_plex *p, *p2;
310 	struct gv_sd *s, *s2;
311 	struct gv_volume *v, *v2;
312 	struct g_provider *pp;
313 	int error, i, *drives, *flags, *plexes, *subdisks, *volumes;
314 	char buf[20];
315 
316 	g_topology_assert();
317 
318 	sc = gp->softc;
319 
320 	/* Find out how many of each object have been passed in. */
321 	volumes = gctl_get_paraml(req, "volumes", sizeof(*volumes));
322 	plexes = gctl_get_paraml(req, "plexes", sizeof(*plexes));
323 	subdisks = gctl_get_paraml(req, "subdisks", sizeof(*subdisks));
324 	drives = gctl_get_paraml(req, "drives", sizeof(*drives));
325 	if (volumes == NULL || plexes == NULL || subdisks == NULL ||
326 	    drives == NULL) {
327 		gctl_error(req, "number of objects not given");
328 		return (-1);
329 	}
330 	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
331 	if (flags == NULL) {
332 		gctl_error(req, "flags not given");
333 		return (-1);
334 	}
335 
336 	/* First, handle drive definitions ... */
337 	for (i = 0; i < *drives; i++) {
338 		snprintf(buf, sizeof(buf), "drive%d", i);
339 		d2 = gctl_get_paraml(req, buf, sizeof(*d2));
340 		if (d2 == NULL) {
341 			gctl_error(req, "no drive definition given");
342 			return (-1);
343 		}
344 		/*
345 		 * Make sure that the device specified in the drive config is
346 		 * an active GEOM provider.
347 		 */
348 		pp = g_provider_by_name(d2->device);
349 		if (pp == NULL) {
350 			gctl_error(req, "%s: device not found", d2->device);
351 			goto error;
352 		}
353 		if (gv_find_drive(sc, d2->name) != NULL) {
354 			/* Ignore error. */
355 			if (*flags & GV_FLAG_F)
356 				continue;
357 			gctl_error(req, "drive '%s' already exists", d2->name);
358 			goto error;
359 		}
360 		if (gv_find_drive_device(sc, d2->device) != NULL) {
361 			gctl_error(req, "device '%s' already configured in "
362 			    "gvinum", d2->device);
363 			goto error;
364 		}
365 
366 
367 		d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO);
368 		bcopy(d2, d, sizeof(*d));
369 
370 		gv_post_event(sc, GV_EVENT_CREATE_DRIVE, d, NULL, 0, 0);
371 	}
372 
373 	/* ... then volume definitions ... */
374 	for (i = 0; i < *volumes; i++) {
375 		error = 0;
376 		snprintf(buf, sizeof(buf), "volume%d", i);
377 		v2 = gctl_get_paraml(req, buf, sizeof(*v2));
378 		if (v2 == NULL) {
379 			gctl_error(req, "no volume definition given");
380 			return (-1);
381 		}
382 		if (gv_find_vol(sc, v2->name) != NULL) {
383 			/* Ignore error. */
384 			if (*flags & GV_FLAG_F)
385 				continue;
386 			gctl_error(req, "volume '%s' already exists", v2->name);
387 			goto error;
388 		}
389 
390 		v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO);
391 		bcopy(v2, v, sizeof(*v));
392 
393 		gv_post_event(sc, GV_EVENT_CREATE_VOLUME, v, NULL, 0, 0);
394 	}
395 
396 	/* ... then plex definitions ... */
397 	for (i = 0; i < *plexes; i++) {
398 		error = 0;
399 		snprintf(buf, sizeof(buf), "plex%d", i);
400 		p2 = gctl_get_paraml(req, buf, sizeof(*p2));
401 		if (p2 == NULL) {
402 			gctl_error(req, "no plex definition given");
403 			return (-1);
404 		}
405 		if (gv_find_plex(sc, p2->name) != NULL) {
406 			/* Ignore error. */
407 			if (*flags & GV_FLAG_F)
408 				continue;
409 			gctl_error(req, "plex '%s' already exists", p2->name);
410 			goto error;
411 		}
412 
413 		p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO);
414 		bcopy(p2, p, sizeof(*p));
415 
416 		gv_post_event(sc, GV_EVENT_CREATE_PLEX, p, NULL, 0, 0);
417 	}
418 
419 	/* ... and, finally, subdisk definitions. */
420 	for (i = 0; i < *subdisks; i++) {
421 		error = 0;
422 		snprintf(buf, sizeof(buf), "sd%d", i);
423 		s2 = gctl_get_paraml(req, buf, sizeof(*s2));
424 		if (s2 == NULL) {
425 			gctl_error(req, "no subdisk definition given");
426 			return (-1);
427 		}
428 		if (gv_find_sd(sc, s2->name) != NULL) {
429 			/* Ignore error. */
430 			if (*flags & GV_FLAG_F)
431 				continue;
432 			gctl_error(req, "sd '%s' already exists", s2->name);
433 			goto error;
434 		}
435 
436 		s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO);
437 		bcopy(s2, s, sizeof(*s));
438 
439 		gv_post_event(sc, GV_EVENT_CREATE_SD, s, NULL, 0, 0);
440 	}
441 
442 error:
443 	gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc, NULL, 0, 0);
444 	gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
445 
446 	return (0);
447 }
448 
449 static void
450 gv_config(struct gctl_req *req, struct g_class *mp, char const *verb)
451 {
452 	struct g_geom *gp;
453 	struct gv_softc *sc;
454 	struct sbuf *sb;
455 	char *comment;
456 
457 	g_topology_assert();
458 
459 	gp = LIST_FIRST(&mp->geom);
460 	sc = gp->softc;
461 
462 	if (!strcmp(verb, "attach")) {
463 		gv_attach(sc, req);
464 
465 	} else if (!strcmp(verb, "concat")) {
466 		gv_concat(gp, req);
467 
468 	} else if (!strcmp(verb, "detach")) {
469 		gv_detach(sc, req);
470 
471 	} else if (!strcmp(verb, "list")) {
472 		gv_list(gp, req);
473 
474 	/* Save our configuration back to disk. */
475 	} else if (!strcmp(verb, "saveconfig")) {
476 		gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0);
477 
478 	/* Return configuration in string form. */
479 	} else if (!strcmp(verb, "getconfig")) {
480 		comment = gctl_get_param(req, "comment", NULL);
481 		if (comment == NULL) {
482 			gctl_error(req, "no comment parameter given");
483 			return;
484 		}
485 		sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN);
486 		gv_format_config(sc, sb, 0, comment);
487 		sbuf_finish(sb);
488 		gctl_set_param(req, "config", sbuf_data(sb), sbuf_len(sb) + 1);
489 		sbuf_delete(sb);
490 
491 	} else if (!strcmp(verb, "create")) {
492 		gv_create(gp, req);
493 
494 	} else if (!strcmp(verb, "mirror")) {
495 		gv_mirror(gp, req);
496 
497 	} else if (!strcmp(verb, "move")) {
498 		gv_move(gp, req);
499 
500 	} else if (!strcmp(verb, "raid5")) {
501 		gv_raid5(gp, req);
502 
503 	} else if (!strcmp(verb, "rebuildparity") ||
504 	    !strcmp(verb, "checkparity")) {
505 		gv_parityop(sc, req);
506 
507 	} else if (!strcmp(verb, "remove")) {
508 		gv_remove(gp, req);
509 
510 	} else if (!strcmp(verb, "rename")) {
511 		gv_rename(gp, req);
512 
513 	} else if (!strcmp(verb, "resetconfig")) {
514 		gv_post_event(sc, GV_EVENT_RESET_CONFIG, sc, NULL, 0, 0);
515 
516 	} else if (!strcmp(verb, "start")) {
517 		gv_start_obj(gp, req);
518 
519 	} else if (!strcmp(verb, "stripe")) {
520 		gv_stripe(gp, req);
521 
522 	} else if (!strcmp(verb, "setstate")) {
523 		gv_setstate(gp, req);
524 	} else
525 		gctl_error(req, "Unknown verb parameter");
526 }
527 
528 static void
529 gv_parityop(struct gv_softc *sc, struct gctl_req *req)
530 {
531 	struct gv_plex *p;
532 	int *flags, *rebuild, type;
533 	char *plex;
534 
535 	plex = gctl_get_param(req, "plex", NULL);
536 	if (plex == NULL) {
537 		gctl_error(req, "no plex given");
538 		return;
539 	}
540 
541 	flags = gctl_get_paraml(req, "flags", sizeof(*flags));
542 	if (flags == NULL) {
543 		gctl_error(req, "no flags given");
544 		return;
545 	}
546 
547 	rebuild = gctl_get_paraml(req, "rebuild", sizeof(*rebuild));
548 	if (rebuild == NULL) {
549 		gctl_error(req, "no operation given");
550 		return;
551 	}
552 
553 	type = gv_object_type(sc, plex);
554 	if (type != GV_TYPE_PLEX) {
555 		gctl_error(req, "'%s' is not a plex", plex);
556 		return;
557 	}
558 	p = gv_find_plex(sc, plex);
559 
560 	if (p->state != GV_PLEX_UP) {
561 		gctl_error(req, "plex %s is not completely accessible",
562 		    p->name);
563 		return;
564 	}
565 
566 	if (p->org != GV_PLEX_RAID5) {
567 		gctl_error(req, "plex %s is not a RAID5 plex", p->name);
568 		return;
569 	}
570 
571 	/* Put it in the event queue. */
572 	/* XXX: The state of the plex might have changed when this event is
573 	 * picked up ... We should perhaps check this afterwards. */
574 	if (*rebuild)
575 		gv_post_event(sc, GV_EVENT_PARITY_REBUILD, p, NULL, 0, 0);
576 	else
577 		gv_post_event(sc, GV_EVENT_PARITY_CHECK, p, NULL, 0, 0);
578 }
579 
580 
581 static struct g_geom *
582 gv_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
583 {
584 	struct g_geom *gp;
585 	struct g_consumer *cp;
586 	struct gv_softc *sc;
587 	struct gv_hdr vhdr;
588 	int error;
589 
590  	g_topology_assert();
591 	g_trace(G_T_TOPOLOGY, "gv_taste(%s, %s)", mp->name, pp->name);
592 
593 	gp = LIST_FIRST(&mp->geom);
594 	if (gp == NULL) {
595 		G_VINUM_DEBUG(0, "error: tasting, but not initialized?");
596 		return (NULL);
597 	}
598 	sc = gp->softc;
599 
600 	cp = g_new_consumer(gp);
601 	if (g_attach(cp, pp) != 0) {
602 		g_destroy_consumer(cp);
603 		return (NULL);
604 	}
605 	if (g_access(cp, 1, 0, 0) != 0) {
606 		g_detach(cp);
607 		g_destroy_consumer(cp);
608 		return (NULL);
609 	}
610 	g_topology_unlock();
611 
612 	error = gv_read_header(cp, &vhdr);
613 
614 	g_topology_lock();
615 	g_access(cp, -1, 0, 0);
616 	g_detach(cp);
617 	g_destroy_consumer(cp);
618 
619 	/* Check if what we've been given is a valid vinum drive. */
620 	if (!error)
621 		gv_post_event(sc, GV_EVENT_DRIVE_TASTED, pp, NULL, 0, 0);
622 
623 	return (NULL);
624 }
625 
626 void
627 gv_worker(void *arg)
628 {
629 	struct g_provider *pp;
630 	struct gv_softc *sc;
631 	struct gv_event *ev;
632 	struct gv_volume *v;
633 	struct gv_plex *p;
634 	struct gv_sd *s;
635 	struct gv_drive *d;
636 	struct bio *bp;
637 	int newstate, flags, err, rename;
638 	char *newname;
639 	off_t offset;
640 
641 	sc = arg;
642 	KASSERT(sc != NULL, ("NULL sc"));
643 	for (;;) {
644 		/* Look at the events first... */
645 		ev = gv_get_event(sc);
646 		if (ev != NULL) {
647 			gv_remove_event(sc, ev);
648 
649 			switch (ev->type) {
650 			case GV_EVENT_DRIVE_TASTED:
651 				G_VINUM_DEBUG(2, "event 'drive tasted'");
652 				pp = ev->arg1;
653 				gv_drive_tasted(sc, pp);
654 				break;
655 
656 			case GV_EVENT_DRIVE_LOST:
657 				G_VINUM_DEBUG(2, "event 'drive lost'");
658 				d = ev->arg1;
659 				gv_drive_lost(sc, d);
660 				break;
661 
662 			case GV_EVENT_CREATE_DRIVE:
663 				G_VINUM_DEBUG(2, "event 'create drive'");
664 				d = ev->arg1;
665 				gv_create_drive(sc, d);
666 				break;
667 
668 			case GV_EVENT_CREATE_VOLUME:
669 				G_VINUM_DEBUG(2, "event 'create volume'");
670 				v = ev->arg1;
671 				gv_create_volume(sc, v);
672 				break;
673 
674 			case GV_EVENT_CREATE_PLEX:
675 				G_VINUM_DEBUG(2, "event 'create plex'");
676 				p = ev->arg1;
677 				gv_create_plex(sc, p);
678 				break;
679 
680 			case GV_EVENT_CREATE_SD:
681 				G_VINUM_DEBUG(2, "event 'create sd'");
682 				s = ev->arg1;
683 				gv_create_sd(sc, s);
684 				break;
685 
686 			case GV_EVENT_RM_DRIVE:
687 				G_VINUM_DEBUG(2, "event 'remove drive'");
688 				d = ev->arg1;
689 				flags = ev->arg3;
690 				gv_rm_drive(sc, d, flags);
691 				/*gv_setup_objects(sc);*/
692 				break;
693 
694 			case GV_EVENT_RM_VOLUME:
695 				G_VINUM_DEBUG(2, "event 'remove volume'");
696 				v = ev->arg1;
697 				gv_rm_vol(sc, v);
698 				/*gv_setup_objects(sc);*/
699 				break;
700 
701 			case GV_EVENT_RM_PLEX:
702 				G_VINUM_DEBUG(2, "event 'remove plex'");
703 				p = ev->arg1;
704 				gv_rm_plex(sc, p);
705 				/*gv_setup_objects(sc);*/
706 				break;
707 
708 			case GV_EVENT_RM_SD:
709 				G_VINUM_DEBUG(2, "event 'remove sd'");
710 				s = ev->arg1;
711 				gv_rm_sd(sc, s);
712 				/*gv_setup_objects(sc);*/
713 				break;
714 
715 			case GV_EVENT_SAVE_CONFIG:
716 				G_VINUM_DEBUG(2, "event 'save config'");
717 				gv_save_config(sc);
718 				break;
719 
720 			case GV_EVENT_SET_SD_STATE:
721 				G_VINUM_DEBUG(2, "event 'setstate sd'");
722 				s = ev->arg1;
723 				newstate = ev->arg3;
724 				flags = ev->arg4;
725 				err = gv_set_sd_state(s, newstate, flags);
726 				if (err)
727 					G_VINUM_DEBUG(0, "error setting subdisk"
728 					    " state: error code %d", err);
729 				break;
730 
731 			case GV_EVENT_SET_DRIVE_STATE:
732 				G_VINUM_DEBUG(2, "event 'setstate drive'");
733 				d = ev->arg1;
734 				newstate = ev->arg3;
735 				flags = ev->arg4;
736 				err = gv_set_drive_state(d, newstate, flags);
737 				if (err)
738 					G_VINUM_DEBUG(0, "error setting drive "
739 					    "state: error code %d", err);
740 				break;
741 
742 			case GV_EVENT_SET_VOL_STATE:
743 				G_VINUM_DEBUG(2, "event 'setstate volume'");
744 				v = ev->arg1;
745 				newstate = ev->arg3;
746 				flags = ev->arg4;
747 				err = gv_set_vol_state(v, newstate, flags);
748 				if (err)
749 					G_VINUM_DEBUG(0, "error setting volume "
750 					    "state: error code %d", err);
751 				break;
752 
753 			case GV_EVENT_SET_PLEX_STATE:
754 				G_VINUM_DEBUG(2, "event 'setstate plex'");
755 				p = ev->arg1;
756 				newstate = ev->arg3;
757 				flags = ev->arg4;
758 				err = gv_set_plex_state(p, newstate, flags);
759 				if (err)
760 					G_VINUM_DEBUG(0, "error setting plex "
761 					    "state: error code %d", err);
762 				break;
763 
764 			case GV_EVENT_SETUP_OBJECTS:
765 				G_VINUM_DEBUG(2, "event 'setup objects'");
766 				gv_setup_objects(sc);
767 				break;
768 
769 			case GV_EVENT_RESET_CONFIG:
770 				G_VINUM_DEBUG(2, "event 'resetconfig'");
771 				err = gv_resetconfig(sc);
772 				if (err)
773 					G_VINUM_DEBUG(0, "error resetting "
774 					    "config: error code %d", err);
775 				break;
776 
777 			case GV_EVENT_PARITY_REBUILD:
778 				/*
779 				 * Start the rebuild. The gv_plex_done will
780 				 * handle issuing of the remaining rebuild bio's
781 				 * until it's finished.
782 				 */
783 				G_VINUM_DEBUG(2, "event 'rebuild'");
784 				p = ev->arg1;
785 				if (p->state != GV_PLEX_UP) {
786 					G_VINUM_DEBUG(0, "plex %s is not "
787 					    "completely accessible", p->name);
788 					break;
789 				}
790 				if (p->flags & GV_PLEX_SYNCING ||
791 				    p->flags & GV_PLEX_REBUILDING ||
792 				    p->flags & GV_PLEX_GROWING) {
793 					G_VINUM_DEBUG(0, "plex %s is busy with "
794 					    "syncing or parity build", p->name);
795 					break;
796 				}
797 				p->synced = 0;
798 				p->flags |= GV_PLEX_REBUILDING;
799 				g_topology_assert_not();
800 				g_topology_lock();
801 				err = gv_access(p->vol_sc->provider, 1, 1, 0);
802 				if (err) {
803 					G_VINUM_DEBUG(0, "unable to access "
804 					    "provider");
805 					break;
806 				}
807 				g_topology_unlock();
808 				gv_parity_request(p, GV_BIO_CHECK |
809 				    GV_BIO_PARITY, 0);
810 				break;
811 
812 			case GV_EVENT_PARITY_CHECK:
813 				/* Start parity check. */
814 				G_VINUM_DEBUG(2, "event 'check'");
815 				p = ev->arg1;
816 				if (p->state != GV_PLEX_UP) {
817 					G_VINUM_DEBUG(0, "plex %s is not "
818 					    "completely accessible", p->name);
819 					break;
820 				}
821 				if (p->flags & GV_PLEX_SYNCING ||
822 				    p->flags & GV_PLEX_REBUILDING ||
823 				    p->flags & GV_PLEX_GROWING) {
824 					G_VINUM_DEBUG(0, "plex %s is busy with "
825 					    "syncing or parity build", p->name);
826 					break;
827 				}
828 				p->synced = 0;
829 				g_topology_assert_not();
830 				g_topology_lock();
831 				err = gv_access(p->vol_sc->provider, 1, 1, 0);
832 				if (err) {
833 					G_VINUM_DEBUG(0, "unable to access "
834 					    "provider");
835 					break;
836 				}
837 				g_topology_unlock();
838 				gv_parity_request(p, GV_BIO_CHECK, 0);
839 				break;
840 
841 			case GV_EVENT_START_PLEX:
842 				G_VINUM_DEBUG(2, "event 'start' plex");
843 				p = ev->arg1;
844 				gv_start_plex(p);
845 				break;
846 
847 			case GV_EVENT_START_VOLUME:
848 				G_VINUM_DEBUG(2, "event 'start' volume");
849 				v = ev->arg1;
850 				gv_start_vol(v);
851 				break;
852 
853 			case GV_EVENT_ATTACH_PLEX:
854 				G_VINUM_DEBUG(2, "event 'attach' plex");
855 				p = ev->arg1;
856 				v = ev->arg2;
857 				rename = ev->arg4;
858 				err = gv_attach_plex(p, v, rename);
859 				if (err)
860 					G_VINUM_DEBUG(0, "error attaching %s to"
861 					    " %s: error code %d", p->name,
862 					    v->name, err);
863 				break;
864 
865 			case GV_EVENT_ATTACH_SD:
866 				G_VINUM_DEBUG(2, "event 'attach' sd");
867 				s = ev->arg1;
868 				p = ev->arg2;
869 				offset = ev->arg3;
870 				rename = ev->arg4;
871 				err = gv_attach_sd(s, p, offset, rename);
872 				if (err)
873 					G_VINUM_DEBUG(0, "error attaching %s to"
874 					    " %s: error code %d", s->name,
875 					    p->name, err);
876 				break;
877 
878 			case GV_EVENT_DETACH_PLEX:
879 				G_VINUM_DEBUG(2, "event 'detach' plex");
880 				p = ev->arg1;
881 				flags = ev->arg3;
882 				err = gv_detach_plex(p, flags);
883 				if (err)
884 					G_VINUM_DEBUG(0, "error detaching %s: "
885 					    "error code %d", p->name, err);
886 				break;
887 
888 			case GV_EVENT_DETACH_SD:
889 				G_VINUM_DEBUG(2, "event 'detach' sd");
890 				s = ev->arg1;
891 				flags = ev->arg3;
892 				err = gv_detach_sd(s, flags);
893 				if (err)
894 					G_VINUM_DEBUG(0, "error detaching %s: "
895 					    "error code %d", s->name, err);
896 				break;
897 
898 			case GV_EVENT_RENAME_VOL:
899 				G_VINUM_DEBUG(2, "event 'rename' volume");
900 				v = ev->arg1;
901 				newname = ev->arg2;
902 				flags = ev->arg3;
903 				err = gv_rename_vol(sc, v, newname, flags);
904 				if (err)
905 					G_VINUM_DEBUG(0, "error renaming %s to "
906 					    "%s: error code %d", v->name,
907 					    newname, err);
908 				g_free(newname);
909 				/* Destroy and recreate the provider if we can. */
910 				if (gv_provider_is_open(v->provider)) {
911 					G_VINUM_DEBUG(0, "unable to rename "
912 					    "provider to %s: provider in use",
913 					    v->name);
914 					break;
915 				}
916 				g_topology_lock();
917 				g_wither_provider(v->provider, ENOENT);
918 				g_topology_unlock();
919 				v->provider = NULL;
920 				gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc,
921 				    NULL, 0, 0);
922 				break;
923 
924 			case GV_EVENT_RENAME_PLEX:
925 				G_VINUM_DEBUG(2, "event 'rename' plex");
926 				p = ev->arg1;
927 				newname = ev->arg2;
928 				flags = ev->arg3;
929 				err = gv_rename_plex(sc, p, newname, flags);
930 				if (err)
931 					G_VINUM_DEBUG(0, "error renaming %s to "
932 					    "%s: error code %d", p->name,
933 					    newname, err);
934 				g_free(newname);
935 				break;
936 
937 			case GV_EVENT_RENAME_SD:
938 				G_VINUM_DEBUG(2, "event 'rename' sd");
939 				s = ev->arg1;
940 				newname = ev->arg2;
941 				flags = ev->arg3;
942 				err = gv_rename_sd(sc, s, newname, flags);
943 				if (err)
944 					G_VINUM_DEBUG(0, "error renaming %s to "
945 					    "%s: error code %d", s->name,
946 					    newname, err);
947 				g_free(newname);
948 				break;
949 
950 			case GV_EVENT_RENAME_DRIVE:
951 				G_VINUM_DEBUG(2, "event 'rename' drive");
952 				d = ev->arg1;
953 				newname = ev->arg2;
954 				flags = ev->arg3;
955 				err = gv_rename_drive(sc, d, newname, flags);
956 				if (err)
957 					G_VINUM_DEBUG(0, "error renaming %s to "
958 					    "%s: error code %d", d->name,
959 					    newname, err);
960 				g_free(newname);
961 				break;
962 
963 			case GV_EVENT_MOVE_SD:
964 				G_VINUM_DEBUG(2, "event 'move' sd");
965 				s = ev->arg1;
966 				d = ev->arg2;
967 				flags = ev->arg3;
968 				err = gv_move_sd(sc, s, d, flags);
969 				if (err)
970 					G_VINUM_DEBUG(0, "error moving %s to "
971 					    "%s: error code %d", s->name,
972 					    d->name, err);
973 				break;
974 
975 			case GV_EVENT_THREAD_EXIT:
976 				G_VINUM_DEBUG(2, "event 'thread exit'");
977 				g_free(ev);
978 				mtx_lock(&sc->equeue_mtx);
979 				mtx_lock(&sc->bqueue_mtx);
980 				gv_cleanup(sc);
981 				mtx_destroy(&sc->bqueue_mtx);
982 				mtx_destroy(&sc->equeue_mtx);
983 				g_free(sc->bqueue_down);
984 				g_free(sc->bqueue_up);
985 				g_free(sc);
986 				kproc_exit(0);
987 				/* NOTREACHED */
988 
989 			default:
990 				G_VINUM_DEBUG(1, "unknown event %d", ev->type);
991 			}
992 
993 			g_free(ev);
994 			continue;
995 		}
996 
997 		/* ... then do I/O processing. */
998 		mtx_lock(&sc->bqueue_mtx);
999 		/* First do new requests. */
1000 		bp = bioq_takefirst(sc->bqueue_down);
1001 		if (bp != NULL) {
1002 			mtx_unlock(&sc->bqueue_mtx);
1003 			/* A bio that interfered with another bio. */
1004 			if (bp->bio_pflags & GV_BIO_ONHOLD) {
1005 				s = bp->bio_caller1;
1006 				p = s->plex_sc;
1007 				/* Is it still locked out? */
1008 				if (gv_stripe_active(p, bp)) {
1009 					/* Park the bio on the waiting queue. */
1010 					bioq_disksort(p->wqueue, bp);
1011 				} else {
1012 					bp->bio_pflags &= ~GV_BIO_ONHOLD;
1013 					g_io_request(bp, s->drive_sc->consumer);
1014 				}
1015 			/* A special request requireing special handling. */
1016 			} else if (bp->bio_pflags & GV_BIO_INTERNAL) {
1017 				p = bp->bio_caller1;
1018 				gv_plex_start(p, bp);
1019 			} else {
1020 				gv_volume_start(sc, bp);
1021 			}
1022 			mtx_lock(&sc->bqueue_mtx);
1023 		}
1024 		/* Then do completed requests. */
1025 		bp = bioq_takefirst(sc->bqueue_up);
1026 		if (bp == NULL) {
1027 			msleep(sc, &sc->bqueue_mtx, PRIBIO, "-", hz/10);
1028 			mtx_unlock(&sc->bqueue_mtx);
1029 			continue;
1030 		}
1031 		mtx_unlock(&sc->bqueue_mtx);
1032 		gv_bio_done(sc, bp);
1033 	}
1034 }
1035 
1036 #define	VINUM_CLASS_NAME "VINUM"
1037 
1038 static struct g_class g_vinum_class	= {
1039 	.name = VINUM_CLASS_NAME,
1040 	.version = G_VERSION,
1041 	.init = gv_init,
1042 	.taste = gv_taste,
1043 	.ctlreq = gv_config,
1044 	.destroy_geom = gv_unload,
1045 };
1046 
1047 DECLARE_GEOM_CLASS(g_vinum_class, g_vinum);
1048